@Test public void testCalculateShoppingCartInclusiveTax() 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 CartItem ship1 = context.mock(CartItem.class, "ship1"); final DeliveryBucket bucket1 = context.mock(DeliveryBucket.class, "bucket1"); final MutableShoppingCart cart = context.mock(MutableShoppingCart.class, "cart"); final MutableShoppingContext shoppingContext = context.mock(MutableShoppingContext.class, "ctx"); final Customer customer = context.mock(Customer.class, "customer"); final Shop shop = context.mock(Shop.class, "shop"); final TaxProvider.Tax tax = context.mock(TaxProvider.Tax.class, "tax"); final BigDecimal deliveryListCost = new BigDecimal("20.00"); final Total deliveryTotal = new TotalImpl( Total.ZERO, Total.ZERO, Total.ZERO, Total.ZERO, false, null, Total.ZERO, Total.ZERO, Total.ZERO, deliveryListCost, deliveryListCost, false, null, Total.ZERO, deliveryListCost, deliveryListCost, Total.ZERO, deliveryListCost, deliveryListCost ); final PromotionContext promotionContext = context.mock(PromotionContext.class, "promoCtx"); context.checking(new Expectations() {{ allowing(deliveryCostCalculationStrategy).calculate(cart); will(returnValue(deliveryTotal)); allowing(promotionContextFactory).getInstance("SHOP10", "EUR"); will(returnValue(promotionContext)); allowing(cart).getCustomerEmail(); will(returnValue("bob@doe.com")); allowing(shopService).getById(10L); will(returnValue(shop)); allowing(customerService).getCustomerByEmail("bob@doe.com", shop); will(returnValue(customer)); allowing(cart).getShoppingContext(); will(returnValue(shoppingContext)); allowing(shoppingContext).getShopId(); will(returnValue(10L)); allowing(shoppingContext).getShopCode(); will(returnValue("SHOP10")); allowing(shoppingContext).getCustomerShopCode(); will(returnValue("SHOP10")); allowing(shoppingContext).getCountryCode(); will(returnValue("GB")); allowing(shoppingContext).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).getProductSkuCode(); will(returnValue("A-001")); allowing(item1).getSupplierCode(); will(returnValue("Main")); allowing(item1).getPrice(); will(returnValue(new BigDecimal("20.00"))); allowing(item1).getNetPrice(); will(returnValue(new BigDecimal("16.66"))); allowing(item1).getGrossPrice(); will(returnValue(new BigDecimal("20.00"))); allowing(item1).getTaxRate(); will(returnValue(new BigDecimal("20.00"))); allowing(item1).isTaxExclusiveOfPrice(); will(returnValue(false)); allowing(item1).getSalePrice(); will(returnValue(new BigDecimal("22.50"))); allowing(item1).getListPrice(); will(returnValue(new BigDecimal("25.00"))); allowing(item1).getQty(); will(returnValue(new BigDecimal("2"))); 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).getNetPrice(); will(returnValue(new BigDecimal("33.33"))); allowing(item2).getGrossPrice(); will(returnValue(new BigDecimal("40.00"))); allowing(item2).getTaxRate(); will(returnValue(new BigDecimal("20.00"))); allowing(item2).isTaxExclusiveOfPrice(); will(returnValue(false)); allowing(item2).getSalePrice(); will(returnValue(new BigDecimal("40.00"))); allowing(item2).getListPrice(); will(returnValue(new BigDecimal("40.00"))); allowing(item2).getQty(); will(returnValue(new BigDecimal("1"))); oneOf(taxProvider).determineTax("SHOP10", "EUR", "GB", "GB-CAM", "A-001"); will(returnValue(tax)); 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(false)); oneOf(cart).setProductSkuTax("Main", "A-001", new BigDecimal("16.66"), new BigDecimal("20.00"), new BigDecimal("20.00"), "VAT", false); oneOf(cart).setProductSkuTax("Main", "A-002", new BigDecimal("33.33"), new BigDecimal("40.00"), new BigDecimal("20.00"), "VAT", false); allowing(cart).getShippingList(); will(returnValue(Collections.singletonList(ship1))); allowing(ship1).getProductSkuCode(); will(returnValue("B-001")); allowing(ship1).getPrice(); will(returnValue(new BigDecimal("10.00"))); allowing(ship1).getNetPrice(); will(returnValue(new BigDecimal("8.33"))); allowing(ship1).getGrossPrice(); will(returnValue(new BigDecimal("10.00"))); allowing(ship1).getTaxRate(); will(returnValue(new BigDecimal("20.00"))); allowing(ship1).isTaxExclusiveOfPrice(); will(returnValue(false)); allowing(ship1).getSalePrice(); will(returnValue(new BigDecimal("20.00"))); allowing(ship1).getListPrice(); will(returnValue(new BigDecimal("20.00"))); allowing(ship1).getQty(); will(returnValue(new BigDecimal("1"))); allowing(ship1).isPromoApplied(); will(returnValue(true)); allowing(ship1).getAppliedPromo(); will(returnValue("SHIP-50%")); allowing(ship1).getDeliveryBucket(); will(returnValue(bucket1)); oneOf(taxProvider).determineTax("SHOP10", "EUR", "GB", "GB-CAM", "B-001"); will(returnValue(tax)); oneOf(cart).setShippingTax("B-001", bucket1, new BigDecimal("8.33"), new BigDecimal("10.00"), new BigDecimal("20.00"), "VAT", false); }}); final Total rezTaxIncluded = new DefaultAmountCalculationStrategy(taxProvider, deliveryCostCalculationStrategy, promotionContextFactory, customerService, shopService) { @Override void applyItemLevelPromotions(final Customer cust, final MutableShoppingCart scart, final PromotionContext promoCtx) { assertSame(customer, cust); assertSame(cart, scart); assertSame(promotionContext, promoCtx); } @Override Total applyOrderLevelPromotions(final Customer cust, final MutableShoppingCart scart, final Total itemTotal, final PromotionContext promoCtx) { assertSame(customer, cust); assertSame(cart, scart); assertSame(promotionContext, promoCtx); return itemTotal; } @Override void applyShippingPromotions(final Customer cust, final MutableShoppingCart scart, final Total orderTotal, final PromotionContext promoCtx) { assertSame(customer, cust); assertSame(cart, scart); assertSame(promotionContext, promoCtx); } }.calculate(cart); assertEquals("90.00", rezTaxIncluded.getListSubTotal().toPlainString()); assertEquals("85.00", rezTaxIncluded.getSaleSubTotal().toPlainString()); assertEquals("40.00", rezTaxIncluded.getNonSaleSubTotal().toPlainString()); assertEquals("80.00", rezTaxIncluded.getPriceSubTotal().toPlainString()); assertFalse(rezTaxIncluded.isOrderPromoApplied()); assertNull(rezTaxIncluded.getAppliedOrderPromo()); assertEquals("80.00", rezTaxIncluded.getSubTotal().toPlainString()); assertEquals("80.00", rezTaxIncluded.getSubTotalAmount().toPlainString()); assertEquals("13.35", rezTaxIncluded.getSubTotalTax().toPlainString()); assertEquals("20.00", rezTaxIncluded.getDeliveryListCost().toPlainString()); assertTrue(rezTaxIncluded.isDeliveryPromoApplied()); assertEquals("10.00", rezTaxIncluded.getDeliveryCost().toPlainString()); assertEquals("10.00", rezTaxIncluded.getDeliveryCostAmount().toPlainString()); assertEquals("SHIP-50%", rezTaxIncluded.getAppliedDeliveryPromo()); assertEquals("1.67", rezTaxIncluded.getDeliveryTax().toPlainString()); assertEquals("90.00", rezTaxIncluded.getTotal().toPlainString()); assertEquals("110.00", rezTaxIncluded.getListTotalAmount().toPlainString()); assertEquals("90.00", rezTaxIncluded.getTotalAmount().toPlainString()); assertEquals("15.02", rezTaxIncluded.getTotalTax().toPlainString()); }
@Override public Total calculate(final MutableShoppingCart cart) { final Customer customer; if (StringUtils.isNotBlank(cart.getCustomerEmail())) { final Shop shop = shopService.getById(cart.getShoppingContext().getShopId()); if (shop != null) { customer = customerService.getCustomerByEmail(cart.getCustomerEmail(), shop); } else { customer = null; } } else { customer = null; } final PromotionContext promoCtx = promotionContextFactory.getInstance( cart.getShoppingContext().getCustomerShopCode(), cart.getCurrencyCode() ); applyItemLevelPromotions(customer, cart, promoCtx); final Total itemTotal = applyTaxToCartItemsAndCalculateItemTotal(cart); final Total deliveryCostTotalNoTax = deliveryCostCalculationStrategy.calculate(cart); final Total draftOrderTotal = deliveryCostTotalNoTax != null ? itemTotal.add(deliveryCostTotalNoTax) : itemTotal.add(ZERO_TOTAL); final Total orderTotal = applyOrderLevelPromotions(customer, cart, draftOrderTotal, promoCtx); applyShippingPromotions(customer, cart, orderTotal, promoCtx); final Total finalTotal = applyTaxToShippingAndCalculateOrderTotal(cart, orderTotal); return finalTotal; }
DefaultAmountCalculationStrategy implements AmountCalculationStrategy { @Override public Total calculate(final MutableShoppingCart cart) { final Customer customer; if (StringUtils.isNotBlank(cart.getCustomerEmail())) { final Shop shop = shopService.getById(cart.getShoppingContext().getShopId()); if (shop != null) { customer = customerService.getCustomerByEmail(cart.getCustomerEmail(), shop); } else { customer = null; } } else { customer = null; } final PromotionContext promoCtx = promotionContextFactory.getInstance( cart.getShoppingContext().getCustomerShopCode(), cart.getCurrencyCode() ); applyItemLevelPromotions(customer, cart, promoCtx); final Total itemTotal = applyTaxToCartItemsAndCalculateItemTotal(cart); final Total deliveryCostTotalNoTax = deliveryCostCalculationStrategy.calculate(cart); final Total draftOrderTotal = deliveryCostTotalNoTax != null ? itemTotal.add(deliveryCostTotalNoTax) : itemTotal.add(ZERO_TOTAL); final Total orderTotal = applyOrderLevelPromotions(customer, cart, draftOrderTotal, promoCtx); applyShippingPromotions(customer, cart, orderTotal, promoCtx); final Total finalTotal = applyTaxToShippingAndCalculateOrderTotal(cart, orderTotal); return finalTotal; } }
DefaultAmountCalculationStrategy implements AmountCalculationStrategy { @Override public Total calculate(final MutableShoppingCart cart) { final Customer customer; if (StringUtils.isNotBlank(cart.getCustomerEmail())) { final Shop shop = shopService.getById(cart.getShoppingContext().getShopId()); if (shop != null) { customer = customerService.getCustomerByEmail(cart.getCustomerEmail(), shop); } else { customer = null; } } else { customer = null; } final PromotionContext promoCtx = promotionContextFactory.getInstance( cart.getShoppingContext().getCustomerShopCode(), cart.getCurrencyCode() ); applyItemLevelPromotions(customer, cart, promoCtx); final Total itemTotal = applyTaxToCartItemsAndCalculateItemTotal(cart); final Total deliveryCostTotalNoTax = deliveryCostCalculationStrategy.calculate(cart); final Total draftOrderTotal = deliveryCostTotalNoTax != null ? itemTotal.add(deliveryCostTotalNoTax) : itemTotal.add(ZERO_TOTAL); final Total orderTotal = applyOrderLevelPromotions(customer, cart, draftOrderTotal, promoCtx); applyShippingPromotions(customer, cart, orderTotal, promoCtx); final Total finalTotal = applyTaxToShippingAndCalculateOrderTotal(cart, orderTotal); return finalTotal; } DefaultAmountCalculationStrategy(final TaxProvider taxProvider,
                                            final DeliveryCostCalculationStrategy deliveryCostCalculationStrategy,
                                            final PromotionContextFactory promotionContextFactory,
                                            final CustomerService customerService,
                                            final ShopService shopService); }
DefaultAmountCalculationStrategy implements AmountCalculationStrategy { @Override public Total calculate(final MutableShoppingCart cart) { final Customer customer; if (StringUtils.isNotBlank(cart.getCustomerEmail())) { final Shop shop = shopService.getById(cart.getShoppingContext().getShopId()); if (shop != null) { customer = customerService.getCustomerByEmail(cart.getCustomerEmail(), shop); } else { customer = null; } } else { customer = null; } final PromotionContext promoCtx = promotionContextFactory.getInstance( cart.getShoppingContext().getCustomerShopCode(), cart.getCurrencyCode() ); applyItemLevelPromotions(customer, cart, promoCtx); final Total itemTotal = applyTaxToCartItemsAndCalculateItemTotal(cart); final Total deliveryCostTotalNoTax = deliveryCostCalculationStrategy.calculate(cart); final Total draftOrderTotal = deliveryCostTotalNoTax != null ? itemTotal.add(deliveryCostTotalNoTax) : itemTotal.add(ZERO_TOTAL); final Total orderTotal = applyOrderLevelPromotions(customer, cart, draftOrderTotal, promoCtx); applyShippingPromotions(customer, cart, orderTotal, promoCtx); final Total finalTotal = applyTaxToShippingAndCalculateOrderTotal(cart, orderTotal); return finalTotal; } 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 { @Override public Total calculate(final MutableShoppingCart cart) { final Customer customer; if (StringUtils.isNotBlank(cart.getCustomerEmail())) { final Shop shop = shopService.getById(cart.getShoppingContext().getShopId()); if (shop != null) { customer = customerService.getCustomerByEmail(cart.getCustomerEmail(), shop); } else { customer = null; } } else { customer = null; } final PromotionContext promoCtx = promotionContextFactory.getInstance( cart.getShoppingContext().getCustomerShopCode(), cart.getCurrencyCode() ); applyItemLevelPromotions(customer, cart, promoCtx); final Total itemTotal = applyTaxToCartItemsAndCalculateItemTotal(cart); final Total deliveryCostTotalNoTax = deliveryCostCalculationStrategy.calculate(cart); final Total draftOrderTotal = deliveryCostTotalNoTax != null ? itemTotal.add(deliveryCostTotalNoTax) : itemTotal.add(ZERO_TOTAL); final Total orderTotal = applyOrderLevelPromotions(customer, cart, draftOrderTotal, promoCtx); applyShippingPromotions(customer, cart, orderTotal, promoCtx); final Total finalTotal = applyTaxToShippingAndCalculateOrderTotal(cart, orderTotal); return finalTotal; } 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);  }