/// <summary> /// Calculates the shipping price for a sales order /// ### Complete this method /// </summary> /// Assumptions /// Price Based Shipping Rate Calculations: Total Price of all the products in the order is considered to calculate the shipping rate for Price Based Method /// Weight Based Shipping Rate Calcuations: Total Weight of all the products in the order is considered to calculate the shipping rate for Weight Based Method /// API Based Shipping Rate Calcuations: There is little bit confusion on this part of calculations. Here my understanding suggested that if product weighs between 10 to 30 I have added 5m to Weight based calculation /// I thought to get maximum from Price Based and Weight based and add the rate adjustment price to it. Similar approach is used for Rate Adjustment Percent. public decimal Calculate(SalesOrder salesOrder) { //throw new NotImplementedException(); decimal shippingCost = 0.00m; // decimal ShippingAPICost = 0.00m; if (salesOrder != null && salesOrder.Lines.Count() > 0) { decimal TotalPrice = 0.00m; decimal TotalWeight = 0.00m; foreach (SalesOrderLine orderLine in salesOrder.Lines) { TotalPrice += (orderLine.Price * orderLine.Quantity); if (orderLine.Product != null) { TotalWeight += (orderLine.Product.Weight * orderLine.Quantity); } } ShippingCalculator shippingCalculator = new ShippingCalculator(); var ShippingPriceCost = CalculateShippingRate(TotalPrice, shippingCalculator._priceRates); var ShippingWeightCost = CalculateShippingRate(TotalWeight, shippingCalculator._weightRates); //var ApiCalculatedWeight = ShippingApiClient.get var shippingapiclient = new ShippingApiClient(); var apiweightCost = shippingapiclient.GetRate(decimal.Parse(salesOrder.DeliveryPostalCode), decimal.Parse(salesOrder.DeliveryPostalCode), TotalWeight); var ShippingAPICost = CalculateShippingRate(TotalWeight, shippingCalculator._apiRates, apiweightCost); shippingCost = GetMax(ShippingPriceCost, ShippingWeightCost, ShippingAPICost); } return(shippingCost); }
/// <summary> /// Given an order, calculate the best shipping amount according to ShippingAPIRate rule /// </summary> /// <param name="order"></param> /// <returns>The shipping amount sent and total shipping cost</returns> public Tuple <decimal, decimal> CalculateThridPartyBasedOptimalShippingAmount(SalesOrder order) { var shippingCost = 0m; var shippingWeight = order.GetOrderShippingWeight(); if (shippingWeight >= 0 && shippingWeight < 10) { shippingCost = shippingWeight * _client.GetRate(0, 0, shippingWeight); } else if (shippingWeight >= 10 && shippingWeight <= 30) { shippingCost = shippingWeight * (_client.GetRate(0, 0, shippingWeight) + 5); } else if (shippingWeight > 30 && shippingWeight <= 35) { shippingCost = shippingWeight * (_client.GetRate(0, 0, shippingWeight) * 1.075m); } else if (shippingWeight > 35) { shippingCost = shippingWeight * _client.GetRate(0, 0, shippingWeight); } return(new Tuple <decimal, decimal>(shippingWeight, shippingCost)); }