@Test public void testCalculateDraftOrderExclusiveTax() 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 CustomerOrder order = context.mock(CustomerOrder.class, "order"); context.checking(new Expectations() {{ allowing(order).getDelivery(); will(returnValue(Collections.EMPTY_LIST)); allowing(order).isPromoApplied(); will(returnValue(true)); allowing(order).getAppliedPromo(); will(returnValue("ORDER-25%")); allowing(order).getPrice(); will(returnValue(new BigDecimal("60.00"))); allowing(order).getNetPrice(); will(returnValue(new BigDecimal("60.00"))); allowing(order).getGrossPrice(); will(returnValue(new BigDecimal("72.00"))); allowing(order).getListPrice(); will(returnValue(new BigDecimal("90.00"))); allowing(order).getOrderDetail(); will(returnValue(Arrays.asList(item1, item2))); allowing(item1).getPrice(); will(returnValue(new BigDecimal("20.00"))); allowing(item1).getNetPrice(); will(returnValue(new BigDecimal("20.00"))); allowing(item1).getGrossPrice(); will(returnValue(new BigDecimal("24.00"))); allowing(item1).getTaxRate(); will(returnValue(new BigDecimal("20.00"))); allowing(item1).isTaxExclusiveOfPrice(); will(returnValue(true)); 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).getPrice(); will(returnValue(new BigDecimal("40.00"))); 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).getSalePrice(); will(returnValue(new BigDecimal("40.00"))); allowing(item2).getListPrice(); will(returnValue(new BigDecimal("40.00"))); allowing(item2).getQty(); will(returnValue(new BigDecimal("1"))); }}); final Total rezTaxExcluded = new DefaultAmountCalculationStrategy(taxProvider, deliveryCostCalculationStrategy, promotionContextFactory, customerService, shopService).calculate(order); assertEquals("90.00", rezTaxExcluded.getListSubTotal().toPlainString()); assertEquals("85.00", rezTaxExcluded.getSaleSubTotal().toPlainString()); assertEquals("40.00", rezTaxExcluded.getNonSaleSubTotal().toPlainString()); assertEquals("80.00", rezTaxExcluded.getPriceSubTotal().toPlainString()); assertTrue(rezTaxExcluded.isOrderPromoApplied()); assertEquals("ORDER-25%", rezTaxExcluded.getAppliedOrderPromo()); assertEquals("60.00", rezTaxExcluded.getSubTotal().toPlainString()); assertEquals("72.00", rezTaxExcluded.getSubTotalAmount().toPlainString()); assertEquals("12.00", rezTaxExcluded.getSubTotalTax().toPlainString()); assertEquals("0.00", rezTaxExcluded.getDeliveryListCost().toPlainString()); assertFalse(rezTaxExcluded.isDeliveryPromoApplied()); assertEquals("0.00", rezTaxExcluded.getDeliveryCost().toPlainString()); assertEquals("0.00", rezTaxExcluded.getDeliveryCostAmount().toPlainString()); assertNull(rezTaxExcluded.getAppliedDeliveryPromo()); assertEquals("0.00", rezTaxExcluded.getDeliveryTax().toPlainString()); assertEquals("60.00", rezTaxExcluded.getTotal().toPlainString()); assertEquals("108.00", rezTaxExcluded.getListTotalAmount().toPlainString()); assertEquals("72.00", rezTaxExcluded.getTotalAmount().toPlainString()); assertEquals("12.00", rezTaxExcluded.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);  }