@Test public void testGetCartShippingTotalWithTaxExclInfoNet() throws Exception { final ShopService shopService = context.mock(ShopService.class, "shopService"); final ShoppingCart cart = context.mock(ShoppingCart.class, "cart"); final ShoppingContext cartCtx = context.mock(ShoppingContext.class, "cartCtx"); final Total cartTotal = context.mock(Total.class, "cartTotal"); final Shop shop = context.mock(Shop.class, "shop"); final CartItem shipping1 = context.mock(CartItem.class, "shipping1"); final CartItem shipping2 = context.mock(CartItem.class, "shipping2"); final CartItem shipping3 = context.mock(CartItem.class, "shipping3"); final CartItem shipping4 = context.mock(CartItem.class, "shipping4"); context.checking(new Expectations() {{ allowing(cart).getShoppingContext(); will(returnValue(cartCtx)); allowing(cartCtx).getShopId(); will(returnValue(234L)); allowing(cart).getCurrencyCode(); will(returnValue("EUR")); allowing(cart).getShippingList(); will(returnValue(Arrays.asList(shipping1, shipping2, shipping3, shipping4))); allowing(cart).getTotal(); will(returnValue(cartTotal)); allowing(cartTotal).getDeliveryListCost(); will(returnValue(new BigDecimal("80.00"))); allowing(cartTotal).getDeliveryCost(); will(returnValue(new BigDecimal("80.00"))); allowing(cartTotal).getDeliveryCostAmount(); will(returnValue(new BigDecimal("96.00"))); allowing(cartTotal).getDeliveryTax(); will(returnValue(new BigDecimal("16.00"))); allowing(shopService).getById(234L); will(returnValue(shop)); allowing(cartCtx).isTaxInfoEnabled(); will(returnValue(true)); allowing(cartCtx).isTaxInfoUseNet(); will(returnValue(true)); allowing(cartCtx).isTaxInfoShowAmount(); will(returnValue(true)); allowing(shipping1).getTaxCode(); will(returnValue("GROSS")); allowing(shipping1).getTaxRate(); will(returnValue(new BigDecimal("20.00"))); allowing(shipping2).getTaxCode(); will(returnValue("GROSS")); allowing(shipping2).getTaxRate(); will(returnValue(new BigDecimal("20.00"))); allowing(shipping3).getTaxCode(); will(returnValue("")); allowing(shipping3).getTaxRate(); will(returnValue(new BigDecimal("0.00"))); allowing(shipping4).getTaxCode(); will(returnValue("GROSS-2")); allowing(shipping4).getTaxRate(); will(returnValue(new BigDecimal("12.00"))); }}); final ShippingServiceFacade facade = new ShippingServiceFacadeImpl(null, null, shopService, null, null); final PriceModel model = facade.getCartShippingTotal(cart); assertNotNull(model); assertEquals(ShippingServiceFacadeImpl.CART_SHIPPING_TOTAL_REF, model.getRef()); assertEquals("EUR", model.getCurrency()); assertEquals("4", model.getQuantity().toPlainString()); assertEquals("80.00", model.getRegularPrice().toPlainString()); assertNull(model.getSalePrice()); assertTrue(model.isTaxInfoEnabled()); assertTrue(model.isTaxInfoUseNet()); assertTrue(model.isTaxInfoShowAmount()); assertEquals("GROSS,GROSS-2", model.getPriceTaxCode()); assertEquals("20.00", model.getPriceTaxRate().toPlainString()); assertTrue(model.isPriceTaxExclusive()); assertEquals("16.00", model.getPriceTax().toPlainString()); context.assertIsSatisfied(); }
@Override public PriceModel getCartShippingTotal(final ShoppingCart cart) { final String currency = cart.getCurrencyCode(); final BigDecimal deliveriesCount = new BigDecimal(cart.getShippingList().size()); final BigDecimal list = cart.getTotal().getDeliveryListCost(); final BigDecimal sale = cart.getTotal().getDeliveryCost(); final boolean showTax = cart.getShoppingContext().isTaxInfoEnabled(); final boolean showTaxNet = showTax && cart.getShoppingContext().isTaxInfoUseNet(); final boolean showTaxAmount = showTax && cart.getShoppingContext().isTaxInfoShowAmount(); if (showTax) { final BigDecimal costInclTax = cart.getTotal().getDeliveryCostAmount(); if (MoneyUtils.isPositive(costInclTax)) { final BigDecimal totalTax = cart.getTotal().getDeliveryTax(); final BigDecimal net = costInclTax.subtract(totalTax); final BigDecimal gross = costInclTax; final BigDecimal totalAdjusted = showTaxNet ? net : gross; final Set<String> taxes = new TreeSet<>(); final Set<BigDecimal> rates = new TreeSet<>(); for (final CartItem item : cart.getShippingList()) { if (StringUtils.isNotBlank(item.getTaxCode())) { taxes.add(item.getTaxCode()); } rates.add(item.getTaxRate()); } final BigDecimal taxRate; if (MoneyUtils.isPositive(totalTax) && rates.size() > 1) { taxRate = totalTax.multiply(MoneyUtils.HUNDRED) .divide(net, 1, BigDecimal.ROUND_HALF_UP) .setScale(Constants.MONEY_SCALE, BigDecimal.ROUND_HALF_UP); } else { taxRate = rates.iterator().next(); } final String tax = StringUtils.join(taxes, ','); final boolean exclusiveTax = costInclTax.compareTo(sale) > 0; if (MoneyUtils.isFirstBiggerThanSecond(list, sale)) { final MoneyUtils.Money listMoney = MoneyUtils.getMoney(list, taxRate, !exclusiveTax); final BigDecimal listAdjusted = showTaxNet ? listMoney.getNet() : listMoney.getGross(); return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, listAdjusted, totalAdjusted, showTax, showTaxNet, showTaxAmount, tax, taxRate, exclusiveTax, totalTax ); } return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, totalAdjusted, null, showTax, showTaxNet, showTaxAmount, tax, taxRate, exclusiveTax, totalTax ); } } if (MoneyUtils.isFirstBiggerThanSecond(list, sale)) { return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, list, sale ); } return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, sale, null ); }
ShippingServiceFacadeImpl implements ShippingServiceFacade { @Override public PriceModel getCartShippingTotal(final ShoppingCart cart) { final String currency = cart.getCurrencyCode(); final BigDecimal deliveriesCount = new BigDecimal(cart.getShippingList().size()); final BigDecimal list = cart.getTotal().getDeliveryListCost(); final BigDecimal sale = cart.getTotal().getDeliveryCost(); final boolean showTax = cart.getShoppingContext().isTaxInfoEnabled(); final boolean showTaxNet = showTax && cart.getShoppingContext().isTaxInfoUseNet(); final boolean showTaxAmount = showTax && cart.getShoppingContext().isTaxInfoShowAmount(); if (showTax) { final BigDecimal costInclTax = cart.getTotal().getDeliveryCostAmount(); if (MoneyUtils.isPositive(costInclTax)) { final BigDecimal totalTax = cart.getTotal().getDeliveryTax(); final BigDecimal net = costInclTax.subtract(totalTax); final BigDecimal gross = costInclTax; final BigDecimal totalAdjusted = showTaxNet ? net : gross; final Set<String> taxes = new TreeSet<>(); final Set<BigDecimal> rates = new TreeSet<>(); for (final CartItem item : cart.getShippingList()) { if (StringUtils.isNotBlank(item.getTaxCode())) { taxes.add(item.getTaxCode()); } rates.add(item.getTaxRate()); } final BigDecimal taxRate; if (MoneyUtils.isPositive(totalTax) && rates.size() > 1) { taxRate = totalTax.multiply(MoneyUtils.HUNDRED) .divide(net, 1, BigDecimal.ROUND_HALF_UP) .setScale(Constants.MONEY_SCALE, BigDecimal.ROUND_HALF_UP); } else { taxRate = rates.iterator().next(); } final String tax = StringUtils.join(taxes, ','); final boolean exclusiveTax = costInclTax.compareTo(sale) > 0; if (MoneyUtils.isFirstBiggerThanSecond(list, sale)) { final MoneyUtils.Money listMoney = MoneyUtils.getMoney(list, taxRate, !exclusiveTax); final BigDecimal listAdjusted = showTaxNet ? listMoney.getNet() : listMoney.getGross(); return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, listAdjusted, totalAdjusted, showTax, showTaxNet, showTaxAmount, tax, taxRate, exclusiveTax, totalTax ); } return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, totalAdjusted, null, showTax, showTaxNet, showTaxAmount, tax, taxRate, exclusiveTax, totalTax ); } } if (MoneyUtils.isFirstBiggerThanSecond(list, sale)) { return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, list, sale ); } return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, sale, null ); } }
ShippingServiceFacadeImpl implements ShippingServiceFacade { @Override public PriceModel getCartShippingTotal(final ShoppingCart cart) { final String currency = cart.getCurrencyCode(); final BigDecimal deliveriesCount = new BigDecimal(cart.getShippingList().size()); final BigDecimal list = cart.getTotal().getDeliveryListCost(); final BigDecimal sale = cart.getTotal().getDeliveryCost(); final boolean showTax = cart.getShoppingContext().isTaxInfoEnabled(); final boolean showTaxNet = showTax && cart.getShoppingContext().isTaxInfoUseNet(); final boolean showTaxAmount = showTax && cart.getShoppingContext().isTaxInfoShowAmount(); if (showTax) { final BigDecimal costInclTax = cart.getTotal().getDeliveryCostAmount(); if (MoneyUtils.isPositive(costInclTax)) { final BigDecimal totalTax = cart.getTotal().getDeliveryTax(); final BigDecimal net = costInclTax.subtract(totalTax); final BigDecimal gross = costInclTax; final BigDecimal totalAdjusted = showTaxNet ? net : gross; final Set<String> taxes = new TreeSet<>(); final Set<BigDecimal> rates = new TreeSet<>(); for (final CartItem item : cart.getShippingList()) { if (StringUtils.isNotBlank(item.getTaxCode())) { taxes.add(item.getTaxCode()); } rates.add(item.getTaxRate()); } final BigDecimal taxRate; if (MoneyUtils.isPositive(totalTax) && rates.size() > 1) { taxRate = totalTax.multiply(MoneyUtils.HUNDRED) .divide(net, 1, BigDecimal.ROUND_HALF_UP) .setScale(Constants.MONEY_SCALE, BigDecimal.ROUND_HALF_UP); } else { taxRate = rates.iterator().next(); } final String tax = StringUtils.join(taxes, ','); final boolean exclusiveTax = costInclTax.compareTo(sale) > 0; if (MoneyUtils.isFirstBiggerThanSecond(list, sale)) { final MoneyUtils.Money listMoney = MoneyUtils.getMoney(list, taxRate, !exclusiveTax); final BigDecimal listAdjusted = showTaxNet ? listMoney.getNet() : listMoney.getGross(); return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, listAdjusted, totalAdjusted, showTax, showTaxNet, showTaxAmount, tax, taxRate, exclusiveTax, totalTax ); } return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, totalAdjusted, null, showTax, showTaxNet, showTaxAmount, tax, taxRate, exclusiveTax, totalTax ); } } if (MoneyUtils.isFirstBiggerThanSecond(list, sale)) { return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, list, sale ); } return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, sale, null ); } ShippingServiceFacadeImpl(final CarrierService carrierService,
                                     final CarrierSlaService carrierSlaService,
                                     final ShopService shopService,
                                     final WarehouseService warehouseService,
                                     final DeliveryCostCalculationStrategy deliveryCostCalculationStrategy); }
ShippingServiceFacadeImpl implements ShippingServiceFacade { @Override public PriceModel getCartShippingTotal(final ShoppingCart cart) { final String currency = cart.getCurrencyCode(); final BigDecimal deliveriesCount = new BigDecimal(cart.getShippingList().size()); final BigDecimal list = cart.getTotal().getDeliveryListCost(); final BigDecimal sale = cart.getTotal().getDeliveryCost(); final boolean showTax = cart.getShoppingContext().isTaxInfoEnabled(); final boolean showTaxNet = showTax && cart.getShoppingContext().isTaxInfoUseNet(); final boolean showTaxAmount = showTax && cart.getShoppingContext().isTaxInfoShowAmount(); if (showTax) { final BigDecimal costInclTax = cart.getTotal().getDeliveryCostAmount(); if (MoneyUtils.isPositive(costInclTax)) { final BigDecimal totalTax = cart.getTotal().getDeliveryTax(); final BigDecimal net = costInclTax.subtract(totalTax); final BigDecimal gross = costInclTax; final BigDecimal totalAdjusted = showTaxNet ? net : gross; final Set<String> taxes = new TreeSet<>(); final Set<BigDecimal> rates = new TreeSet<>(); for (final CartItem item : cart.getShippingList()) { if (StringUtils.isNotBlank(item.getTaxCode())) { taxes.add(item.getTaxCode()); } rates.add(item.getTaxRate()); } final BigDecimal taxRate; if (MoneyUtils.isPositive(totalTax) && rates.size() > 1) { taxRate = totalTax.multiply(MoneyUtils.HUNDRED) .divide(net, 1, BigDecimal.ROUND_HALF_UP) .setScale(Constants.MONEY_SCALE, BigDecimal.ROUND_HALF_UP); } else { taxRate = rates.iterator().next(); } final String tax = StringUtils.join(taxes, ','); final boolean exclusiveTax = costInclTax.compareTo(sale) > 0; if (MoneyUtils.isFirstBiggerThanSecond(list, sale)) { final MoneyUtils.Money listMoney = MoneyUtils.getMoney(list, taxRate, !exclusiveTax); final BigDecimal listAdjusted = showTaxNet ? listMoney.getNet() : listMoney.getGross(); return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, listAdjusted, totalAdjusted, showTax, showTaxNet, showTaxAmount, tax, taxRate, exclusiveTax, totalTax ); } return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, totalAdjusted, null, showTax, showTaxNet, showTaxAmount, tax, taxRate, exclusiveTax, totalTax ); } } if (MoneyUtils.isFirstBiggerThanSecond(list, sale)) { return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, list, sale ); } return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, sale, null ); } ShippingServiceFacadeImpl(final CarrierService carrierService,
                                     final CarrierSlaService carrierSlaService,
                                     final ShopService shopService,
                                     final WarehouseService warehouseService,
                                     final DeliveryCostCalculationStrategy deliveryCostCalculationStrategy); @Override Pair<Boolean, Boolean> isAddressNotRequired(final Collection<Long> carrierSlaIds); @Override boolean isSkippableAddress(final ShoppingCart shoppingCart); @Override List<Carrier> findCarriers(final ShoppingCart shoppingCart, final String supplier); @Override List<CarrierSla> getSortedCarrierSla(final ShoppingCart shoppingCart, final List<Carrier> carriersChoices); @Override Pair<Carrier, CarrierSla> getCarrierSla(final ShoppingCart shoppingCart, final String supplier, final List<Carrier> carriersChoices); @Override Pair<Carrier, CarrierSla> getCarrierSla(final long slaId, final List<Carrier> carriersChoices); @Override PriceModel getCartShippingTotal(final ShoppingCart cart); @Override PriceModel getCartShippingSupplierTotal(final ShoppingCart cart, final String supplier); @Override Map<String, String> getCartItemsSuppliers(final ShoppingCart cart); }
ShippingServiceFacadeImpl implements ShippingServiceFacade { @Override public PriceModel getCartShippingTotal(final ShoppingCart cart) { final String currency = cart.getCurrencyCode(); final BigDecimal deliveriesCount = new BigDecimal(cart.getShippingList().size()); final BigDecimal list = cart.getTotal().getDeliveryListCost(); final BigDecimal sale = cart.getTotal().getDeliveryCost(); final boolean showTax = cart.getShoppingContext().isTaxInfoEnabled(); final boolean showTaxNet = showTax && cart.getShoppingContext().isTaxInfoUseNet(); final boolean showTaxAmount = showTax && cart.getShoppingContext().isTaxInfoShowAmount(); if (showTax) { final BigDecimal costInclTax = cart.getTotal().getDeliveryCostAmount(); if (MoneyUtils.isPositive(costInclTax)) { final BigDecimal totalTax = cart.getTotal().getDeliveryTax(); final BigDecimal net = costInclTax.subtract(totalTax); final BigDecimal gross = costInclTax; final BigDecimal totalAdjusted = showTaxNet ? net : gross; final Set<String> taxes = new TreeSet<>(); final Set<BigDecimal> rates = new TreeSet<>(); for (final CartItem item : cart.getShippingList()) { if (StringUtils.isNotBlank(item.getTaxCode())) { taxes.add(item.getTaxCode()); } rates.add(item.getTaxRate()); } final BigDecimal taxRate; if (MoneyUtils.isPositive(totalTax) && rates.size() > 1) { taxRate = totalTax.multiply(MoneyUtils.HUNDRED) .divide(net, 1, BigDecimal.ROUND_HALF_UP) .setScale(Constants.MONEY_SCALE, BigDecimal.ROUND_HALF_UP); } else { taxRate = rates.iterator().next(); } final String tax = StringUtils.join(taxes, ','); final boolean exclusiveTax = costInclTax.compareTo(sale) > 0; if (MoneyUtils.isFirstBiggerThanSecond(list, sale)) { final MoneyUtils.Money listMoney = MoneyUtils.getMoney(list, taxRate, !exclusiveTax); final BigDecimal listAdjusted = showTaxNet ? listMoney.getNet() : listMoney.getGross(); return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, listAdjusted, totalAdjusted, showTax, showTaxNet, showTaxAmount, tax, taxRate, exclusiveTax, totalTax ); } return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, totalAdjusted, null, showTax, showTaxNet, showTaxAmount, tax, taxRate, exclusiveTax, totalTax ); } } if (MoneyUtils.isFirstBiggerThanSecond(list, sale)) { return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, list, sale ); } return new PriceModelImpl( CART_SHIPPING_TOTAL_REF, currency, deliveriesCount, false, false, sale, null ); } ShippingServiceFacadeImpl(final CarrierService carrierService,
                                     final CarrierSlaService carrierSlaService,
                                     final ShopService shopService,
                                     final WarehouseService warehouseService,
                                     final DeliveryCostCalculationStrategy deliveryCostCalculationStrategy); @Override Pair<Boolean, Boolean> isAddressNotRequired(final Collection<Long> carrierSlaIds); @Override boolean isSkippableAddress(final ShoppingCart shoppingCart); @Override List<Carrier> findCarriers(final ShoppingCart shoppingCart, final String supplier); @Override List<CarrierSla> getSortedCarrierSla(final ShoppingCart shoppingCart, final List<Carrier> carriersChoices); @Override Pair<Carrier, CarrierSla> getCarrierSla(final ShoppingCart shoppingCart, final String supplier, final List<Carrier> carriersChoices); @Override Pair<Carrier, CarrierSla> getCarrierSla(final long slaId, final List<Carrier> carriersChoices); @Override PriceModel getCartShippingTotal(final ShoppingCart cart); @Override PriceModel getCartShippingSupplierTotal(final ShoppingCart cart, final String supplier); @Override Map<String, String> getCartItemsSuppliers(final ShoppingCart cart);  }