@Test public void testExecute() { MutableShoppingCart shoppingCart = new ShoppingCartImpl(); shoppingCart.initialise(ctx().getBean("amountCalculationStrategy", AmountCalculationStrategy.class)); final ShoppingCartCommandFactory commands = ctx().getBean("shoppingCartCommandFactory", ShoppingCartCommandFactory.class); commands.execute(shoppingCart, new HashMap<String, Object>() {{ put(ShoppingCartCommand.CMD_CHANGECURRENCY, "EUR"); put(ShoppingCartCommand.CMD_CHANGELOCALE, "en"); put(ShoppingCartCommand.CMD_SETSHOP, "10"); }}); assertEquals(MoneyUtils.ZERO, shoppingCart.getTotal().getSubTotal()); Map<String, String> params = new HashMap<>(); params.put(SetSkuQuantityToCartEventCommandImpl.CMD_ADDTOCART, "CC_TEST4"); params.put(ShoppingCartCommand.CMD_P_QTY, "1"); commands.execute(shoppingCart, (Map) params); assertEquals("123.00", shoppingCart.getTotal().getSubTotal().toPlainString()); params.clear(); params.put(SetSkuQuantityToCartEventCommandImpl.CMD_SETQTYSKU, "CC_TEST4"); params.put(ShoppingCartCommand.CMD_P_QTY, "5"); commands.execute(shoppingCart, (Map) params); assertEquals("499.95", shoppingCart.getTotal().getSubTotal().toPlainString()); }
@Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (determineSkuPrice(shoppingCart, supplier, skuCode, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to set item quantity {} for supplier {} because could not resolve price", shoppingCart.getGuid(), skuCode, supplier); return; } final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); final BigDecimal cartQty = shoppingCart.getProductSkuQuantity(supplier, skuCode); final BigDecimal validQuantity = getQuantityValue(parameters, shopId, skuCode, supplier, cartQty); String skuName = skuCode; if (productSku != null) { skuName = new FailoverStringI18NModel( productSku.getDisplayName(), productSku.getName() ).getValue(shoppingCart.getCurrentLocale()); } if (StringUtils.isNotBlank(itemGroup)) { final Optional<CartItem> main = shoppingCart.getCartItemList().stream() .filter(item -> ShoppingCartUtils.isCartItem(item, supplier, skuCode, itemGroup)).findFirst(); if (!main.isPresent() || main.get().isNotSoldSeparately()) { LOG.warn("Cannot set sku quantity with code {}:{} from cart, it is not main in group", skuCode, itemGroup); return; } final CartItem mainItem = main.get(); final List<CartItem> allInGroup = shoppingCart.getCartItemList().stream() .filter(item -> itemGroup.equals(item.getItemGroup()) && !mainItem.getProductSkuCode().equals(item.getProductSkuCode())) .collect(Collectors.toList()); final BigDecimal toSet = validQuantity; for (final CartItem component : allInGroup) { final BigDecimal ratio = component.getQty().divide(mainItem.getQty(), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal toSetComponent = toSet.multiply(ratio).setScale(Constants.INVENTORY_SCALE, BigDecimal.ROUND_HALF_EVEN).stripTrailingZeros(); if(!shoppingCart.setProductSkuToCart(supplier, component.getProductSkuCode(), component.getProductName(), toSetComponent, itemGroup)) { LOG.warn("Cannot set sku quantity with code {}:{} from cart", component.getProductSkuCode(), itemGroup); return; } } if (shoppingCart.setProductSkuToCart(supplier, skuCode, skuName, validQuantity, itemGroup)) { recalculatePricesInCart(shoppingCart); LOG.debug("Set sku quantity with code {}:{} to qty {}", skuCode, itemGroup, validQuantity); markDirty(shoppingCart); } else { LOG.warn("Cannot set sku quantity with code {}:{} from cart", skuCode, itemGroup); } } else if (shoppingCart.setProductSkuToCart(supplier, skuCode, skuName, validQuantity, itemGroup)) { recalculatePricesInCart(shoppingCart); LOG.debug("Set sku quantity with code {} to qty {}", skuCode, validQuantity); markDirty(shoppingCart); } else { LOG.warn("Cannot set sku quantity with code {} from cart", skuCode); } }
SetSkuQuantityToCartEventCommandImpl extends AbstractSkuCartCommandImpl { @Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (determineSkuPrice(shoppingCart, supplier, skuCode, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to set item quantity {} for supplier {} because could not resolve price", shoppingCart.getGuid(), skuCode, supplier); return; } final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); final BigDecimal cartQty = shoppingCart.getProductSkuQuantity(supplier, skuCode); final BigDecimal validQuantity = getQuantityValue(parameters, shopId, skuCode, supplier, cartQty); String skuName = skuCode; if (productSku != null) { skuName = new FailoverStringI18NModel( productSku.getDisplayName(), productSku.getName() ).getValue(shoppingCart.getCurrentLocale()); } if (StringUtils.isNotBlank(itemGroup)) { final Optional<CartItem> main = shoppingCart.getCartItemList().stream() .filter(item -> ShoppingCartUtils.isCartItem(item, supplier, skuCode, itemGroup)).findFirst(); if (!main.isPresent() || main.get().isNotSoldSeparately()) { LOG.warn("Cannot set sku quantity with code {}:{} from cart, it is not main in group", skuCode, itemGroup); return; } final CartItem mainItem = main.get(); final List<CartItem> allInGroup = shoppingCart.getCartItemList().stream() .filter(item -> itemGroup.equals(item.getItemGroup()) && !mainItem.getProductSkuCode().equals(item.getProductSkuCode())) .collect(Collectors.toList()); final BigDecimal toSet = validQuantity; for (final CartItem component : allInGroup) { final BigDecimal ratio = component.getQty().divide(mainItem.getQty(), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal toSetComponent = toSet.multiply(ratio).setScale(Constants.INVENTORY_SCALE, BigDecimal.ROUND_HALF_EVEN).stripTrailingZeros(); if(!shoppingCart.setProductSkuToCart(supplier, component.getProductSkuCode(), component.getProductName(), toSetComponent, itemGroup)) { LOG.warn("Cannot set sku quantity with code {}:{} from cart", component.getProductSkuCode(), itemGroup); return; } } if (shoppingCart.setProductSkuToCart(supplier, skuCode, skuName, validQuantity, itemGroup)) { recalculatePricesInCart(shoppingCart); LOG.debug("Set sku quantity with code {}:{} to qty {}", skuCode, itemGroup, validQuantity); markDirty(shoppingCart); } else { LOG.warn("Cannot set sku quantity with code {}:{} from cart", skuCode, itemGroup); } } else if (shoppingCart.setProductSkuToCart(supplier, skuCode, skuName, validQuantity, itemGroup)) { recalculatePricesInCart(shoppingCart); LOG.debug("Set sku quantity with code {} to qty {}", skuCode, validQuantity); markDirty(shoppingCart); } else { LOG.warn("Cannot set sku quantity with code {} from cart", skuCode); } } }
SetSkuQuantityToCartEventCommandImpl extends AbstractSkuCartCommandImpl { @Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (determineSkuPrice(shoppingCart, supplier, skuCode, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to set item quantity {} for supplier {} because could not resolve price", shoppingCart.getGuid(), skuCode, supplier); return; } final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); final BigDecimal cartQty = shoppingCart.getProductSkuQuantity(supplier, skuCode); final BigDecimal validQuantity = getQuantityValue(parameters, shopId, skuCode, supplier, cartQty); String skuName = skuCode; if (productSku != null) { skuName = new FailoverStringI18NModel( productSku.getDisplayName(), productSku.getName() ).getValue(shoppingCart.getCurrentLocale()); } if (StringUtils.isNotBlank(itemGroup)) { final Optional<CartItem> main = shoppingCart.getCartItemList().stream() .filter(item -> ShoppingCartUtils.isCartItem(item, supplier, skuCode, itemGroup)).findFirst(); if (!main.isPresent() || main.get().isNotSoldSeparately()) { LOG.warn("Cannot set sku quantity with code {}:{} from cart, it is not main in group", skuCode, itemGroup); return; } final CartItem mainItem = main.get(); final List<CartItem> allInGroup = shoppingCart.getCartItemList().stream() .filter(item -> itemGroup.equals(item.getItemGroup()) && !mainItem.getProductSkuCode().equals(item.getProductSkuCode())) .collect(Collectors.toList()); final BigDecimal toSet = validQuantity; for (final CartItem component : allInGroup) { final BigDecimal ratio = component.getQty().divide(mainItem.getQty(), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal toSetComponent = toSet.multiply(ratio).setScale(Constants.INVENTORY_SCALE, BigDecimal.ROUND_HALF_EVEN).stripTrailingZeros(); if(!shoppingCart.setProductSkuToCart(supplier, component.getProductSkuCode(), component.getProductName(), toSetComponent, itemGroup)) { LOG.warn("Cannot set sku quantity with code {}:{} from cart", component.getProductSkuCode(), itemGroup); return; } } if (shoppingCart.setProductSkuToCart(supplier, skuCode, skuName, validQuantity, itemGroup)) { recalculatePricesInCart(shoppingCart); LOG.debug("Set sku quantity with code {}:{} to qty {}", skuCode, itemGroup, validQuantity); markDirty(shoppingCart); } else { LOG.warn("Cannot set sku quantity with code {}:{} from cart", skuCode, itemGroup); } } else if (shoppingCart.setProductSkuToCart(supplier, skuCode, skuName, validQuantity, itemGroup)) { recalculatePricesInCart(shoppingCart); LOG.debug("Set sku quantity with code {} to qty {}", skuCode, validQuantity); markDirty(shoppingCart); } else { LOG.warn("Cannot set sku quantity with code {} from cart", skuCode); } } SetSkuQuantityToCartEventCommandImpl(final ShoppingCartCommandRegistry registry,
                                                final PriceResolver priceResolver,
                                                final PricingPolicyProvider pricingPolicyProvider,
                                                final ProductService productService,
                                                final ShopService shopService,
                                                final ProductQuantityStrategy productQuantityStrategy); }
SetSkuQuantityToCartEventCommandImpl extends AbstractSkuCartCommandImpl { @Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (determineSkuPrice(shoppingCart, supplier, skuCode, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to set item quantity {} for supplier {} because could not resolve price", shoppingCart.getGuid(), skuCode, supplier); return; } final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); final BigDecimal cartQty = shoppingCart.getProductSkuQuantity(supplier, skuCode); final BigDecimal validQuantity = getQuantityValue(parameters, shopId, skuCode, supplier, cartQty); String skuName = skuCode; if (productSku != null) { skuName = new FailoverStringI18NModel( productSku.getDisplayName(), productSku.getName() ).getValue(shoppingCart.getCurrentLocale()); } if (StringUtils.isNotBlank(itemGroup)) { final Optional<CartItem> main = shoppingCart.getCartItemList().stream() .filter(item -> ShoppingCartUtils.isCartItem(item, supplier, skuCode, itemGroup)).findFirst(); if (!main.isPresent() || main.get().isNotSoldSeparately()) { LOG.warn("Cannot set sku quantity with code {}:{} from cart, it is not main in group", skuCode, itemGroup); return; } final CartItem mainItem = main.get(); final List<CartItem> allInGroup = shoppingCart.getCartItemList().stream() .filter(item -> itemGroup.equals(item.getItemGroup()) && !mainItem.getProductSkuCode().equals(item.getProductSkuCode())) .collect(Collectors.toList()); final BigDecimal toSet = validQuantity; for (final CartItem component : allInGroup) { final BigDecimal ratio = component.getQty().divide(mainItem.getQty(), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal toSetComponent = toSet.multiply(ratio).setScale(Constants.INVENTORY_SCALE, BigDecimal.ROUND_HALF_EVEN).stripTrailingZeros(); if(!shoppingCart.setProductSkuToCart(supplier, component.getProductSkuCode(), component.getProductName(), toSetComponent, itemGroup)) { LOG.warn("Cannot set sku quantity with code {}:{} from cart", component.getProductSkuCode(), itemGroup); return; } } if (shoppingCart.setProductSkuToCart(supplier, skuCode, skuName, validQuantity, itemGroup)) { recalculatePricesInCart(shoppingCart); LOG.debug("Set sku quantity with code {}:{} to qty {}", skuCode, itemGroup, validQuantity); markDirty(shoppingCart); } else { LOG.warn("Cannot set sku quantity with code {}:{} from cart", skuCode, itemGroup); } } else if (shoppingCart.setProductSkuToCart(supplier, skuCode, skuName, validQuantity, itemGroup)) { recalculatePricesInCart(shoppingCart); LOG.debug("Set sku quantity with code {} to qty {}", skuCode, validQuantity); markDirty(shoppingCart); } else { LOG.warn("Cannot set sku quantity with code {} from cart", skuCode); } } SetSkuQuantityToCartEventCommandImpl(final ShoppingCartCommandRegistry registry,
                                                final PriceResolver priceResolver,
                                                final PricingPolicyProvider pricingPolicyProvider,
                                                final ProductService productService,
                                                final ShopService shopService,
                                                final ProductQuantityStrategy productQuantityStrategy); @Override String getCmdKey(); }
SetSkuQuantityToCartEventCommandImpl extends AbstractSkuCartCommandImpl { @Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (determineSkuPrice(shoppingCart, supplier, skuCode, BigDecimal.ONE) == null) { LOG.debug("[{}] Unable to set item quantity {} for supplier {} because could not resolve price", shoppingCart.getGuid(), skuCode, supplier); return; } final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); final BigDecimal cartQty = shoppingCart.getProductSkuQuantity(supplier, skuCode); final BigDecimal validQuantity = getQuantityValue(parameters, shopId, skuCode, supplier, cartQty); String skuName = skuCode; if (productSku != null) { skuName = new FailoverStringI18NModel( productSku.getDisplayName(), productSku.getName() ).getValue(shoppingCart.getCurrentLocale()); } if (StringUtils.isNotBlank(itemGroup)) { final Optional<CartItem> main = shoppingCart.getCartItemList().stream() .filter(item -> ShoppingCartUtils.isCartItem(item, supplier, skuCode, itemGroup)).findFirst(); if (!main.isPresent() || main.get().isNotSoldSeparately()) { LOG.warn("Cannot set sku quantity with code {}:{} from cart, it is not main in group", skuCode, itemGroup); return; } final CartItem mainItem = main.get(); final List<CartItem> allInGroup = shoppingCart.getCartItemList().stream() .filter(item -> itemGroup.equals(item.getItemGroup()) && !mainItem.getProductSkuCode().equals(item.getProductSkuCode())) .collect(Collectors.toList()); final BigDecimal toSet = validQuantity; for (final CartItem component : allInGroup) { final BigDecimal ratio = component.getQty().divide(mainItem.getQty(), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal toSetComponent = toSet.multiply(ratio).setScale(Constants.INVENTORY_SCALE, BigDecimal.ROUND_HALF_EVEN).stripTrailingZeros(); if(!shoppingCart.setProductSkuToCart(supplier, component.getProductSkuCode(), component.getProductName(), toSetComponent, itemGroup)) { LOG.warn("Cannot set sku quantity with code {}:{} from cart", component.getProductSkuCode(), itemGroup); return; } } if (shoppingCart.setProductSkuToCart(supplier, skuCode, skuName, validQuantity, itemGroup)) { recalculatePricesInCart(shoppingCart); LOG.debug("Set sku quantity with code {}:{} to qty {}", skuCode, itemGroup, validQuantity); markDirty(shoppingCart); } else { LOG.warn("Cannot set sku quantity with code {}:{} from cart", skuCode, itemGroup); } } else if (shoppingCart.setProductSkuToCart(supplier, skuCode, skuName, validQuantity, itemGroup)) { recalculatePricesInCart(shoppingCart); LOG.debug("Set sku quantity with code {} to qty {}", skuCode, validQuantity); markDirty(shoppingCart); } else { LOG.warn("Cannot set sku quantity with code {} from cart", skuCode); } } SetSkuQuantityToCartEventCommandImpl(final ShoppingCartCommandRegistry registry,
                                                final PriceResolver priceResolver,
                                                final PricingPolicyProvider pricingPolicyProvider,
                                                final ProductService productService,
                                                final ShopService shopService,
                                                final ProductQuantityStrategy productQuantityStrategy); @Override String getCmdKey();  }