diff --git a/modules/apps/commerce/commerce-api/src/main/java/com/liferay/commerce/order/CommerceOrderThreadLocal.java b/modules/apps/commerce/commerce-api/src/main/java/com/liferay/commerce/order/CommerceOrderThreadLocal.java index ac387a847e9e4c..fe8239fd65323e 100644 --- a/modules/apps/commerce/commerce-api/src/main/java/com/liferay/commerce/order/CommerceOrderThreadLocal.java +++ b/modules/apps/commerce/commerce-api/src/main/java/com/liferay/commerce/order/CommerceOrderThreadLocal.java @@ -9,6 +9,7 @@ /** * @author Brian I. Kim + * @author Crescenzo Rega */ public class CommerceOrderThreadLocal { @@ -16,13 +17,28 @@ public static boolean isDeleteInProcess() { return _deleteInProcess.get(); } + public static boolean isSkipValidateAccountOrdersLimit() { + return _skipValidateAccountOrdersLimit.get(); + } + public static void setDeleteInProcess(boolean deleteInProcess) { _deleteInProcess.set(deleteInProcess); } + public static void setSkipValidateAccountOrdersLimit( + boolean skipValidateAccountOrdersLimit) { + + _skipValidateAccountOrdersLimit.set(skipValidateAccountOrdersLimit); + } + private static final ThreadLocal _deleteInProcess = new CentralizedThreadLocal<>( CommerceOrderThreadLocal.class + "._deleteInProcess", () -> Boolean.FALSE); + private static final ThreadLocal _skipValidateAccountOrdersLimit = + new CentralizedThreadLocal<>( + CommerceReturnThreadLocal.class + + "._skipValidateAccountOrdersLimit", + () -> Boolean.FALSE); } \ No newline at end of file diff --git a/modules/apps/commerce/commerce-order-content-web/src/main/java/com/liferay/commerce/order/content/web/internal/importer/type/util/CommerceOrderImporterTypeUtil.java b/modules/apps/commerce/commerce-order-content-web/src/main/java/com/liferay/commerce/order/content/web/internal/importer/type/util/CommerceOrderImporterTypeUtil.java index 23aae10be534d4..1b87809a830d02 100644 --- a/modules/apps/commerce/commerce-order-content-web/src/main/java/com/liferay/commerce/order/content/web/internal/importer/type/util/CommerceOrderImporterTypeUtil.java +++ b/modules/apps/commerce/commerce-order-content-web/src/main/java/com/liferay/commerce/order/content/web/internal/importer/type/util/CommerceOrderImporterTypeUtil.java @@ -11,6 +11,7 @@ import com.liferay.commerce.exception.CommerceOrderValidatorException; import com.liferay.commerce.model.CommerceOrder; import com.liferay.commerce.model.CommerceOrderItem; +import com.liferay.commerce.order.CommerceOrderThreadLocal; import com.liferay.commerce.order.importer.item.CommerceOrderImporterItem; import com.liferay.commerce.order.importer.item.CommerceOrderImporterItemImpl; import com.liferay.commerce.price.CommerceOrderPriceCalculation; @@ -52,6 +53,8 @@ public static List getCommerceOrderImporterItems( UserLocalService userLocalService) throws Exception { + CommerceOrderThreadLocal.setSkipValidateAccountOrdersLimit(true); + CommerceOrder tempCommerceOrder = commerceOrderService.addCommerceOrder( commerceOrder.getGroupId(), commerceOrder.getCommerceAccountId(), commerceOrder.getCommerceCurrencyCode(), diff --git a/modules/apps/commerce/commerce-service/src/main/java/com/liferay/commerce/service/impl/CommerceOrderLocalServiceImpl.java b/modules/apps/commerce/commerce-service/src/main/java/com/liferay/commerce/service/impl/CommerceOrderLocalServiceImpl.java index 4e73210bcdfd3d..5cf24b47193357 100644 --- a/modules/apps/commerce/commerce-service/src/main/java/com/liferay/commerce/service/impl/CommerceOrderLocalServiceImpl.java +++ b/modules/apps/commerce/commerce-service/src/main/java/com/liferay/commerce/service/impl/CommerceOrderLocalServiceImpl.java @@ -2785,6 +2785,12 @@ private void _validateAccountOrdersLimit( long commerceChannelGroupId, long commerceAccountId) throws PortalException { + if (CommerceOrderThreadLocal.isSkipValidateAccountOrdersLimit()) { + CommerceOrderThreadLocal.setSkipValidateAccountOrdersLimit(false); + + return; + } + Group group = _groupLocalService.getGroup(commerceChannelGroupId); int pendingCommerceOrdersCount = diff --git a/modules/apps/commerce/commerce-test/src/testIntegration/java/com/liferay/commerce/internal/order/test/CommerceOrderTest.java b/modules/apps/commerce/commerce-test/src/testIntegration/java/com/liferay/commerce/internal/order/test/CommerceOrderTest.java index f36cc9d0a5e7ee..f5f9e413b710ac 100644 --- a/modules/apps/commerce/commerce-test/src/testIntegration/java/com/liferay/commerce/internal/order/test/CommerceOrderTest.java +++ b/modules/apps/commerce/commerce-test/src/testIntegration/java/com/liferay/commerce/internal/order/test/CommerceOrderTest.java @@ -23,6 +23,7 @@ import com.liferay.commerce.exception.CommerceOrderAccountLimitException; import com.liferay.commerce.model.CommerceAddress; import com.liferay.commerce.model.CommerceOrder; +import com.liferay.commerce.order.CommerceOrderThreadLocal; import com.liferay.commerce.order.engine.CommerceOrderEngine; import com.liferay.commerce.product.constants.CommerceChannelConstants; import com.liferay.commerce.product.model.CommerceChannel; @@ -987,6 +988,64 @@ public void testGetPlacedCommerceOrder() throws Exception { _accountEntryLocalService.deleteAccountEntry(accountEntry); } + @Test + public void testSkipValidateAccountOrdersLimit() throws Exception { + Settings settings = FallbackKeysSettingsUtil.getSettings( + new GroupServiceSettingsLocator( + _commerceChannel.getGroupId(), + CommerceConstants.SERVICE_NAME_COMMERCE_ORDER_FIELDS)); + + ModifiableSettings modifiableSettings = + settings.getModifiableSettings(); + + modifiableSettings.setValue("accountCartMaxAllowed", "1"); + + modifiableSettings.store(); + + AccountEntry accountEntry = + CommerceAccountTestUtil.addBusinessAccountEntry( + _user.getUserId(), "Test Business Account", null, null, + new long[] {_user.getUserId()}, null, _serviceContext); + + long commerceChannelGroupId = _commerceChannel.getGroupId(); + + _commerceOrderLocalService.addCommerceOrder( + _user.getUserId(), commerceChannelGroupId, + accountEntry.getAccountEntryId(), _commerceCurrency.getCode(), 0); + + try { + _commerceOrderLocalService.addCommerceOrder( + _user.getUserId(), commerceChannelGroupId, + accountEntry.getAccountEntryId(), _commerceCurrency.getCode(), + 0); + } + catch (CommerceOrderAccountLimitException + commerceOrderAccountLimitException) { + + Assert.assertNotNull(commerceOrderAccountLimitException); + } + + Assert.assertEquals( + 1, + _commerceOrderService.getPendingCommerceOrdersCount( + commerceChannelGroupId, accountEntry.getAccountEntryId(), + StringPool.BLANK)); + + CommerceOrderThreadLocal.setSkipValidateAccountOrdersLimit(true); + + _commerceOrderLocalService.addCommerceOrder( + _user.getUserId(), commerceChannelGroupId, + accountEntry.getAccountEntryId(), _commerceCurrency.getCode(), 0); + + Assert.assertEquals( + 2, + _commerceOrderService.getPendingCommerceOrdersCount( + commerceChannelGroupId, accountEntry.getAccountEntryId(), + StringPool.BLANK)); + + _accountEntries.add(accountEntry); + } + @Test public void testValidateAccountOrdersLimit() throws Exception { frutillaRule.scenario(