WeightBasedPriceListDeliveryCostCalculationStrategy implements DeliveryCostCalculationStrategy { @Override public Total calculate(final MutableShoppingCart cart) { if (!cart.getCarrierSlaId().isEmpty()) { Total total = null; for (final Map.Entry<String, Long> supplierCarrierSla : cart.getCarrierSlaId().entrySet()) { final String supplier = supplierCarrierSla.getKey(); final Map<DeliveryBucket, List<CartItem>> cartBuckets = new HashMap<>(); for (final Map.Entry<DeliveryBucket, List<CartItem>> bucket : cart.getCartItemMap().entrySet()) { if (bucket.getKey().getSupplier().equals(supplier)) { cartBuckets.put(bucket.getKey(), bucket.getValue()); } } if (cartBuckets.isEmpty()) { continue; } final CarrierSla carrierSla = carrierSlaService.getById(supplierCarrierSla.getValue()); if (carrierSla != null && CarrierSla.WEIGHT_VOLUME.equals(carrierSla.getSlaType())) { final String carrierSlaGUID = carrierSla.getGuid(); final String carrierSlaName = new FailoverStringI18NModel( carrierSla.getDisplayName(), carrierSla.getName()).getValue(cart.getCurrentLocale()); final PricingPolicyProvider.PricingPolicy policy = pricingPolicyProvider.determinePricingPolicy( cart.getShoppingContext().getShopCode(), cart.getCurrencyCode(), cart.getCustomerEmail(), cart.getShoppingContext().getCountryCode(), cart.getShoppingContext().getStateCode() ); final BigDecimal qty = QTY; for (final Map.Entry<DeliveryBucket, List<CartItem>> bucketAndItems : cartBuckets.entrySet()) { final Pair<BigDecimal, BigDecimal> weightAndVolume = determineProductWeightAndVolume(bucketAndItems.getValue()); final boolean useWeight = MoneyUtils.isPositive(weightAndVolume.getFirst()); final boolean useVolume = MoneyUtils.isPositive(weightAndVolume.getSecond()); final SkuPrice priceByWeightMax = useWeight ? getSkuPrice(cart, carrierSlaGUID + "_KGMAX", policy, supplier, MAX) : null; final SkuPrice priceByWeight = useWeight && isValidPrice(priceByWeightMax) && MoneyUtils.isFirstBiggerThanOrEqualToSecond(priceByWeightMax.getQuantity(), weightAndVolume.getFirst()) ? getSkuPrice(cart, carrierSlaGUID + "_KG", policy, supplier, weightAndVolume.getFirst()) : null; final SkuPrice priceByVolumeMax = useVolume ? getSkuPrice(cart, carrierSlaGUID + "_M3MAX", policy, supplier, MAX) : null; final SkuPrice priceByVolume = useVolume && isValidPrice(priceByVolumeMax) && MoneyUtils.isFirstBiggerThanOrEqualToSecond(priceByVolumeMax.getQuantity(), weightAndVolume.getSecond()) ? getSkuPrice(cart, carrierSlaGUID + "_M3", policy, supplier, weightAndVolume.getSecond()) : null; SkuPrice price = null; if (useWeight && useVolume) { if (isValidPrice(priceByWeight) && isValidPrice(priceByVolume)) { final BigDecimal salePriceByWeight = MoneyUtils.minPositive(priceByWeight.getSalePriceForCalculation()); final BigDecimal salePriceByVolume = MoneyUtils.minPositive(priceByVolume.getSalePriceForCalculation()); if (MoneyUtils.isFirstBiggerThanOrEqualToSecond(salePriceByVolume, salePriceByWeight)) { price = priceByVolume; } else { price = priceByWeight; } } } else if (useWeight) { price = priceByWeight; } else if (useVolume) { price = priceByVolume; } if (isValidPrice(price)) { final BigDecimal salePrice = MoneyUtils.minPositive(price.getSalePriceForCalculation()); cart.addShippingToCart(bucketAndItems.getKey(), carrierSlaGUID, carrierSlaName, qty); cart.setShippingPrice(carrierSlaGUID, bucketAndItems.getKey(), salePrice, salePrice); final BigDecimal deliveryCost = salePrice.multiply(qty).setScale(Constants.MONEY_SCALE, RoundingMode.HALF_UP); final Total bucketTotal = new TotalImpl( Total.ZERO, Total.ZERO, Total.ZERO, Total.ZERO, false, null, Total.ZERO, Total.ZERO, Total.ZERO, deliveryCost, deliveryCost, false, null, Total.ZERO, deliveryCost, deliveryCost, Total.ZERO, deliveryCost, deliveryCost ); total = total == null ? bucketTotal : total.add(bucketTotal); } else { return null; } } } } return total; } return null; } WeightBasedPriceListDeliveryCostCalculationStrategy(final CarrierSlaService carrierSlaService,
                                                               final ProductService productService,
                                                               final PricingPolicyProvider pricingPolicyProvider,
                                                               final DeliveryCostRegionalPriceResolver deliveryCostRegionalPriceResolver); @Override Total calculate(final MutableShoppingCart cart);  }
@Test public void testCalculateNoneOf() throws Exception { final CarrierSlaService carrierSlaService = context.mock(CarrierSlaService.class, "carrierSlaService"); final MutableShoppingCart cart = context.mock(MutableShoppingCart.class, "cart"); context.checking(new Expectations() {{ allowing(cart).getCarrierSlaId(); will(returnValue(Collections.emptyMap())); }}); final Total delTotal = new WeightBasedPriceListDeliveryCostCalculationStrategy(carrierSlaService, null, null, null).calculate(cart); assertNull(delTotal); context.assertIsSatisfied(); }
@Test public void testCalculateSingleCarrierNotFound() throws Exception { final CarrierSlaService carrierSlaService = context.mock(CarrierSlaService.class, "carrierSlaService"); final MutableShoppingCart cart = context.mock(MutableShoppingCart.class, "cart"); final DeliveryBucket bucket1 = context.mock(DeliveryBucket.class, "bucket1"); context.checking(new Expectations() {{ allowing(cart).getCarrierSlaId(); will(returnValue(Collections.singletonMap("Main", 123L))); oneOf(cart).getCartItemMap(); will(returnValue(Collections.singletonMap(bucket1, Collections.emptyList()))); oneOf(bucket1).getSupplier(); will(returnValue("Main")); oneOf(carrierSlaService).getById(123L); will(returnValue(null)); }}); final Total delTotal = new WeightBasedPriceListDeliveryCostCalculationStrategy(carrierSlaService, null, null, null).calculate(cart); assertNull(delTotal); context.assertIsSatisfied(); }