@Test public void testCalculateSubTotalWithNullPriceExclusiveTax() throws Exception { final TaxProvider taxProvider = context.mock(TaxProvider.class, "taxProvider"); final DeliveryCostCalculationStrategy deliveryCostCalculationStrategy = context.mock(DeliveryCostCalculationStrategy.class, "deliveryCost"); final PromotionContextFactory promotionContextFactory = context.mock(PromotionContextFactory.class, "promotion"); final CustomerService customerService = context.mock(CustomerService.class, "customerService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartItem item1 = context.mock(CartItem.class, "item1"); final CartItem item2 = context.mock(CartItem.class, "item2"); final MutableShoppingCart cart = context.mock(MutableShoppingCart.class, "cart"); final MutableShoppingContext cartContext = context.mock(MutableShoppingContext.class, "cartContext"); final TaxProvider.Tax tax = context.mock(TaxProvider.Tax.class, "tax"); context.checking(new Expectations() {{ allowing(cart).getShoppingContext(); will(returnValue(cartContext)); allowing(cartContext).getShopCode(); will(returnValue("SHOP10")); allowing(cartContext).getCountryCode(); will(returnValue("GB")); allowing(cartContext).getStateCode(); will(returnValue("GB-CAM")); allowing(cart).getCurrencyCode(); will(returnValue("EUR")); allowing(cart).getCartItemList(); will(returnValue(Arrays.asList(item1, item2))); allowing(item1).isGift(); will(returnValue(false)); allowing(item1).getPrice(); will(returnValue(null)); allowing(item1).getSalePrice(); will(returnValue(null)); allowing(item1).getListPrice(); will(returnValue(null)); allowing(item1).getQty(); will(returnValue(new BigDecimal("2"))); allowing(item1).getNetPrice(); will(returnValue(null)); allowing(item2).isGift(); will(returnValue(false)); allowing(item2).getProductSkuCode(); will(returnValue("A-002")); allowing(item2).getSupplierCode(); will(returnValue("Main")); allowing(item2).getPrice(); will(returnValue(new BigDecimal("40.00"))); allowing(item2).getSalePrice(); will(returnValue(new BigDecimal("40.00"))); allowing(item2).getListPrice(); will(returnValue(new BigDecimal("60.00"))); allowing(item2).getQty(); will(returnValue(new BigDecimal("1"))); allowing(item2).getNetPrice(); will(returnValue(new BigDecimal("40.00"))); allowing(item2).getGrossPrice(); will(returnValue(new BigDecimal("48.00"))); allowing(item2).getTaxRate(); will(returnValue(new BigDecimal("20.00"))); allowing(item2).isTaxExclusiveOfPrice(); will(returnValue(true)); allowing(item2).getQty(); will(returnValue(new BigDecimal("1"))); oneOf(taxProvider).determineTax("SHOP10", "EUR", "GB", "GB-CAM", "A-002"); will(returnValue(tax)); allowing(tax).getCode(); will(returnValue("VAT")); allowing(tax).getRate(); will(returnValue(TAX)); allowing(tax).isExcluded(); will(returnValue(true)); oneOf(cart).setProductSkuTax("Main", "A-002", new BigDecimal("40.00"), new BigDecimal("48.00"), new BigDecimal("20.00"), "VAT", true); }}); final Total itemTotal = new DefaultAmountCalculationStrategy(taxProvider, deliveryCostCalculationStrategy, promotionContextFactory, customerService, shopService) .applyTaxToCartItemsAndCalculateItemTotal(cart); assertEquals("60.00", itemTotal.getListSubTotal().toPlainString()); assertEquals("40.00", itemTotal.getSaleSubTotal().toPlainString()); assertEquals("0.00", itemTotal.getNonSaleSubTotal().toPlainString()); assertEquals("40.00", itemTotal.getPriceSubTotal().toPlainString()); assertFalse(itemTotal.isOrderPromoApplied()); assertNull(itemTotal.getAppliedOrderPromo()); assertEquals("40.00", itemTotal.getSubTotal().toPlainString()); assertEquals("8.00", itemTotal.getSubTotalTax().toPlainString()); assertEquals("48.00", itemTotal.getSubTotalAmount().toPlainString()); assertEquals("0.00", itemTotal.getDeliveryListCost().toPlainString()); assertEquals("0.00", itemTotal.getDeliveryCost().toPlainString()); assertFalse(itemTotal.isDeliveryPromoApplied()); assertNull(itemTotal.getAppliedDeliveryPromo()); assertEquals("0.00", itemTotal.getDeliveryTax().toPlainString()); assertEquals("0.00", itemTotal.getDeliveryCostAmount().toPlainString()); assertEquals("40.00", itemTotal.getTotal().toPlainString()); assertEquals("8.00", itemTotal.getTotalTax().toPlainString()); assertEquals("72.00", itemTotal.getListTotalAmount().toPlainString()); assertEquals("48.00", itemTotal.getTotalAmount().toPlainString()); }
protected Total applyTaxToCartItemsAndCalculateItemTotal(final MutableShoppingCart cart) { final ShoppingContext ctx = cart.getShoppingContext(); final String currency = cart.getCurrencyCode(); final CartItemPrices prices = new CartItemPrices(); final List<CartItem> items = cart.getCartItemList(); if (items != null) { for (final CartItem item : cart.getCartItemList()) { if (!item.isGift() && !MoneyUtils.isFirstBiggerThanOrEqualToSecond(BigDecimal.ZERO, item.getQty()) && item.getPrice() != null) { final TaxProvider.Tax tax = taxProvider.determineTax(ctx.getShopCode(), currency, ctx.getCountryCode(), ctx.getStateCode(), item.getProductSkuCode()); final BigDecimal price = item.getPrice(); final MoneyUtils.Money money = calculateMoney(price, tax.getRate(), !tax.isExcluded()); final BigDecimal netPrice = money.getNet(); final BigDecimal grossPrice = money.getGross(); cart.setProductSkuTax( item.getSupplierCode(), item.getProductSkuCode(), netPrice, grossPrice, tax.getRate(), tax.getCode(), tax.isExcluded() ); } prices.add(new CartItemPrices(item)); } } return new TotalImpl( prices.listPrice, prices.salePrice, prices.nonSalePrice, prices.finalPrice, false, null, prices.finalPrice, prices.finalTax, prices.grossFinalPrice, Total.ZERO, Total.ZERO, false, null, Total.ZERO, Total.ZERO, prices.finalPrice, prices.finalTax, prices.grossListPrice, prices.grossFinalPrice ); }
DefaultAmountCalculationStrategy implements AmountCalculationStrategy { protected Total applyTaxToCartItemsAndCalculateItemTotal(final MutableShoppingCart cart) { final ShoppingContext ctx = cart.getShoppingContext(); final String currency = cart.getCurrencyCode(); final CartItemPrices prices = new CartItemPrices(); final List<CartItem> items = cart.getCartItemList(); if (items != null) { for (final CartItem item : cart.getCartItemList()) { if (!item.isGift() && !MoneyUtils.isFirstBiggerThanOrEqualToSecond(BigDecimal.ZERO, item.getQty()) && item.getPrice() != null) { final TaxProvider.Tax tax = taxProvider.determineTax(ctx.getShopCode(), currency, ctx.getCountryCode(), ctx.getStateCode(), item.getProductSkuCode()); final BigDecimal price = item.getPrice(); final MoneyUtils.Money money = calculateMoney(price, tax.getRate(), !tax.isExcluded()); final BigDecimal netPrice = money.getNet(); final BigDecimal grossPrice = money.getGross(); cart.setProductSkuTax( item.getSupplierCode(), item.getProductSkuCode(), netPrice, grossPrice, tax.getRate(), tax.getCode(), tax.isExcluded() ); } prices.add(new CartItemPrices(item)); } } return new TotalImpl( prices.listPrice, prices.salePrice, prices.nonSalePrice, prices.finalPrice, false, null, prices.finalPrice, prices.finalTax, prices.grossFinalPrice, Total.ZERO, Total.ZERO, false, null, Total.ZERO, Total.ZERO, prices.finalPrice, prices.finalTax, prices.grossListPrice, prices.grossFinalPrice ); } }
DefaultAmountCalculationStrategy implements AmountCalculationStrategy { protected Total applyTaxToCartItemsAndCalculateItemTotal(final MutableShoppingCart cart) { final ShoppingContext ctx = cart.getShoppingContext(); final String currency = cart.getCurrencyCode(); final CartItemPrices prices = new CartItemPrices(); final List<CartItem> items = cart.getCartItemList(); if (items != null) { for (final CartItem item : cart.getCartItemList()) { if (!item.isGift() && !MoneyUtils.isFirstBiggerThanOrEqualToSecond(BigDecimal.ZERO, item.getQty()) && item.getPrice() != null) { final TaxProvider.Tax tax = taxProvider.determineTax(ctx.getShopCode(), currency, ctx.getCountryCode(), ctx.getStateCode(), item.getProductSkuCode()); final BigDecimal price = item.getPrice(); final MoneyUtils.Money money = calculateMoney(price, tax.getRate(), !tax.isExcluded()); final BigDecimal netPrice = money.getNet(); final BigDecimal grossPrice = money.getGross(); cart.setProductSkuTax( item.getSupplierCode(), item.getProductSkuCode(), netPrice, grossPrice, tax.getRate(), tax.getCode(), tax.isExcluded() ); } prices.add(new CartItemPrices(item)); } } return new TotalImpl( prices.listPrice, prices.salePrice, prices.nonSalePrice, prices.finalPrice, false, null, prices.finalPrice, prices.finalTax, prices.grossFinalPrice, Total.ZERO, Total.ZERO, false, null, Total.ZERO, Total.ZERO, prices.finalPrice, prices.finalTax, prices.grossListPrice, prices.grossFinalPrice ); } DefaultAmountCalculationStrategy(final TaxProvider taxProvider,
                                            final DeliveryCostCalculationStrategy deliveryCostCalculationStrategy,
                                            final PromotionContextFactory promotionContextFactory,
                                            final CustomerService customerService,
                                            final ShopService shopService); }
DefaultAmountCalculationStrategy implements AmountCalculationStrategy { protected Total applyTaxToCartItemsAndCalculateItemTotal(final MutableShoppingCart cart) { final ShoppingContext ctx = cart.getShoppingContext(); final String currency = cart.getCurrencyCode(); final CartItemPrices prices = new CartItemPrices(); final List<CartItem> items = cart.getCartItemList(); if (items != null) { for (final CartItem item : cart.getCartItemList()) { if (!item.isGift() && !MoneyUtils.isFirstBiggerThanOrEqualToSecond(BigDecimal.ZERO, item.getQty()) && item.getPrice() != null) { final TaxProvider.Tax tax = taxProvider.determineTax(ctx.getShopCode(), currency, ctx.getCountryCode(), ctx.getStateCode(), item.getProductSkuCode()); final BigDecimal price = item.getPrice(); final MoneyUtils.Money money = calculateMoney(price, tax.getRate(), !tax.isExcluded()); final BigDecimal netPrice = money.getNet(); final BigDecimal grossPrice = money.getGross(); cart.setProductSkuTax( item.getSupplierCode(), item.getProductSkuCode(), netPrice, grossPrice, tax.getRate(), tax.getCode(), tax.isExcluded() ); } prices.add(new CartItemPrices(item)); } } return new TotalImpl( prices.listPrice, prices.salePrice, prices.nonSalePrice, prices.finalPrice, false, null, prices.finalPrice, prices.finalTax, prices.grossFinalPrice, Total.ZERO, Total.ZERO, false, null, Total.ZERO, Total.ZERO, prices.finalPrice, prices.finalTax, prices.grossListPrice, prices.grossFinalPrice ); } DefaultAmountCalculationStrategy(final TaxProvider taxProvider,
                                            final DeliveryCostCalculationStrategy deliveryCostCalculationStrategy,
                                            final PromotionContextFactory promotionContextFactory,
                                            final CustomerService customerService,
                                            final ShopService shopService); @Override Total calculate(final MutableShoppingCart cart); @Override Total calculate(final CustomerOrder order, final CustomerOrderDelivery orderDelivery); @Override Total calculate(final CustomerOrder order); }
DefaultAmountCalculationStrategy implements AmountCalculationStrategy { protected Total applyTaxToCartItemsAndCalculateItemTotal(final MutableShoppingCart cart) { final ShoppingContext ctx = cart.getShoppingContext(); final String currency = cart.getCurrencyCode(); final CartItemPrices prices = new CartItemPrices(); final List<CartItem> items = cart.getCartItemList(); if (items != null) { for (final CartItem item : cart.getCartItemList()) { if (!item.isGift() && !MoneyUtils.isFirstBiggerThanOrEqualToSecond(BigDecimal.ZERO, item.getQty()) && item.getPrice() != null) { final TaxProvider.Tax tax = taxProvider.determineTax(ctx.getShopCode(), currency, ctx.getCountryCode(), ctx.getStateCode(), item.getProductSkuCode()); final BigDecimal price = item.getPrice(); final MoneyUtils.Money money = calculateMoney(price, tax.getRate(), !tax.isExcluded()); final BigDecimal netPrice = money.getNet(); final BigDecimal grossPrice = money.getGross(); cart.setProductSkuTax( item.getSupplierCode(), item.getProductSkuCode(), netPrice, grossPrice, tax.getRate(), tax.getCode(), tax.isExcluded() ); } prices.add(new CartItemPrices(item)); } } return new TotalImpl( prices.listPrice, prices.salePrice, prices.nonSalePrice, prices.finalPrice, false, null, prices.finalPrice, prices.finalTax, prices.grossFinalPrice, Total.ZERO, Total.ZERO, false, null, Total.ZERO, Total.ZERO, prices.finalPrice, prices.finalTax, prices.grossListPrice, prices.grossFinalPrice ); } DefaultAmountCalculationStrategy(final TaxProvider taxProvider,
                                            final DeliveryCostCalculationStrategy deliveryCostCalculationStrategy,
                                            final PromotionContextFactory promotionContextFactory,
                                            final CustomerService customerService,
                                            final ShopService shopService); @Override Total calculate(final MutableShoppingCart cart); @Override Total calculate(final CustomerOrder order, final CustomerOrderDelivery orderDelivery); @Override Total calculate(final CustomerOrder order);  }