@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_SETSHOP, "10"); put(ShoppingCartCommand.CMD_CHANGECURRENCY, "EUR"); put(ShoppingCartCommand.CMD_CHANGELOCALE, "en"); }}); assertEquals(MoneyUtils.ZERO, shoppingCart.getTotal().getTotal()); Map<String, String> params = new HashMap<>(); params.put(SetSkuQuantityToCartEventCommandImpl.CMD_ADDTOCART, "CC_TEST3"); params.put(ShoppingCartCommand.CMD_P_QTY, "3"); commands.execute(shoppingCart, (Map) params); assertEquals("21.00", shoppingCart.getTotal().getSubTotal().toPlainString()); params.clear(); params.put(RemoveSkuFromCartCommandImpl.CMD_REMOVEONESKU, "CC_TEST3"); commands.execute(shoppingCart, (Map) params); assertEquals("15.98", shoppingCart.getTotal().getSubTotal().toPlainString()); assertEquals(2, shoppingCart.getCartItemsCount()); }
@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) { final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); 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 remove one sku 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 toRemove = getQuantityValue(shopId, skuCode, supplier, mainItem.getQty()); for (final CartItem component : allInGroup) { final BigDecimal ratio = component.getQty().divide(mainItem.getQty(), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal toRemoveComponent = toRemove.multiply(ratio).setScale(Constants.INVENTORY_SCALE, BigDecimal.ROUND_HALF_EVEN).stripTrailingZeros(); if(!shoppingCart.removeCartItemQuantity(supplier, component.getProductSkuCode(), toRemoveComponent, itemGroup)) { LOG.warn("Cannot remove one sku with code {}:{} from cart", component.getProductSkuCode(), itemGroup); return; } } if (shoppingCart.removeCartItemQuantity(supplier, skuCode, toRemove, itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Cannot remove one sku with code {}:{} from cart", skuCode, itemGroup); } } else if(shoppingCart.removeCartItemQuantity(supplier, skuCode, getQuantityValue(shopId, skuCode, supplier, shoppingCart.getProductSkuQuantity(supplier, skuCode)), itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Can not remove one sku with code {} from cart", skuCode); } }
RemoveSkuFromCartCommandImpl 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) { final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); 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 remove one sku 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 toRemove = getQuantityValue(shopId, skuCode, supplier, mainItem.getQty()); for (final CartItem component : allInGroup) { final BigDecimal ratio = component.getQty().divide(mainItem.getQty(), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal toRemoveComponent = toRemove.multiply(ratio).setScale(Constants.INVENTORY_SCALE, BigDecimal.ROUND_HALF_EVEN).stripTrailingZeros(); if(!shoppingCart.removeCartItemQuantity(supplier, component.getProductSkuCode(), toRemoveComponent, itemGroup)) { LOG.warn("Cannot remove one sku with code {}:{} from cart", component.getProductSkuCode(), itemGroup); return; } } if (shoppingCart.removeCartItemQuantity(supplier, skuCode, toRemove, itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Cannot remove one sku with code {}:{} from cart", skuCode, itemGroup); } } else if(shoppingCart.removeCartItemQuantity(supplier, skuCode, getQuantityValue(shopId, skuCode, supplier, shoppingCart.getProductSkuQuantity(supplier, skuCode)), itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Can not remove one sku with code {} from cart", skuCode); } } }
RemoveSkuFromCartCommandImpl 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) { final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); 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 remove one sku 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 toRemove = getQuantityValue(shopId, skuCode, supplier, mainItem.getQty()); for (final CartItem component : allInGroup) { final BigDecimal ratio = component.getQty().divide(mainItem.getQty(), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal toRemoveComponent = toRemove.multiply(ratio).setScale(Constants.INVENTORY_SCALE, BigDecimal.ROUND_HALF_EVEN).stripTrailingZeros(); if(!shoppingCart.removeCartItemQuantity(supplier, component.getProductSkuCode(), toRemoveComponent, itemGroup)) { LOG.warn("Cannot remove one sku with code {}:{} from cart", component.getProductSkuCode(), itemGroup); return; } } if (shoppingCart.removeCartItemQuantity(supplier, skuCode, toRemove, itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Cannot remove one sku with code {}:{} from cart", skuCode, itemGroup); } } else if(shoppingCart.removeCartItemQuantity(supplier, skuCode, getQuantityValue(shopId, skuCode, supplier, shoppingCart.getProductSkuQuantity(supplier, skuCode)), itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Can not remove one sku with code {} from cart", skuCode); } } RemoveSkuFromCartCommandImpl(final ShoppingCartCommandRegistry registry,
                                        final PriceResolver priceResolver,
                                        final PricingPolicyProvider pricingPolicyProvider,
                                        final ProductService productService,
                                        final ShopService shopService,
                                        final ProductQuantityStrategy productQuantityStrategy); }
RemoveSkuFromCartCommandImpl 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) { final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); 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 remove one sku 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 toRemove = getQuantityValue(shopId, skuCode, supplier, mainItem.getQty()); for (final CartItem component : allInGroup) { final BigDecimal ratio = component.getQty().divide(mainItem.getQty(), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal toRemoveComponent = toRemove.multiply(ratio).setScale(Constants.INVENTORY_SCALE, BigDecimal.ROUND_HALF_EVEN).stripTrailingZeros(); if(!shoppingCart.removeCartItemQuantity(supplier, component.getProductSkuCode(), toRemoveComponent, itemGroup)) { LOG.warn("Cannot remove one sku with code {}:{} from cart", component.getProductSkuCode(), itemGroup); return; } } if (shoppingCart.removeCartItemQuantity(supplier, skuCode, toRemove, itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Cannot remove one sku with code {}:{} from cart", skuCode, itemGroup); } } else if(shoppingCart.removeCartItemQuantity(supplier, skuCode, getQuantityValue(shopId, skuCode, supplier, shoppingCart.getProductSkuQuantity(supplier, skuCode)), itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Can not remove one sku with code {} from cart", skuCode); } } RemoveSkuFromCartCommandImpl(final ShoppingCartCommandRegistry registry,
                                        final PriceResolver priceResolver,
                                        final PricingPolicyProvider pricingPolicyProvider,
                                        final ProductService productService,
                                        final ShopService shopService,
                                        final ProductQuantityStrategy productQuantityStrategy); @Override String getCmdKey(); }
RemoveSkuFromCartCommandImpl 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) { final long shopId = shoppingCart.getShoppingContext().getCustomerShopId(); 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 remove one sku 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 toRemove = getQuantityValue(shopId, skuCode, supplier, mainItem.getQty()); for (final CartItem component : allInGroup) { final BigDecimal ratio = component.getQty().divide(mainItem.getQty(), 10, BigDecimal.ROUND_HALF_UP); final BigDecimal toRemoveComponent = toRemove.multiply(ratio).setScale(Constants.INVENTORY_SCALE, BigDecimal.ROUND_HALF_EVEN).stripTrailingZeros(); if(!shoppingCart.removeCartItemQuantity(supplier, component.getProductSkuCode(), toRemoveComponent, itemGroup)) { LOG.warn("Cannot remove one sku with code {}:{} from cart", component.getProductSkuCode(), itemGroup); return; } } if (shoppingCart.removeCartItemQuantity(supplier, skuCode, toRemove, itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Cannot remove one sku with code {}:{} from cart", skuCode, itemGroup); } } else if(shoppingCart.removeCartItemQuantity(supplier, skuCode, getQuantityValue(shopId, skuCode, supplier, shoppingCart.getProductSkuQuantity(supplier, skuCode)), itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Can not remove one sku with code {} from cart", skuCode); } } RemoveSkuFromCartCommandImpl(final ShoppingCartCommandRegistry registry,
                                        final PriceResolver priceResolver,
                                        final PricingPolicyProvider pricingPolicyProvider,
                                        final ProductService productService,
                                        final ShopService shopService,
                                        final ProductQuantityStrategy productQuantityStrategy); @Override String getCmdKey();  }