/// <summary> /// Recalculates shipping charges for the given basket. /// </summary> /// <param name="basket">The basket to calculate shipping charges for.</param> public static void Calculate(Basket basket) { ClearExistingShipping(basket); foreach (BasketShipment shipment in basket.Shipments) { if (shipment.Warehouse != null) { ShipMethod shipMethod = shipment.ShipMethod; if (shipMethod != null) { ShipRateQuote rate = shipMethod.GetShipRateQuote(shipment); if (rate != null) { BasketItem shipRateLineItem = new BasketItem(); shipRateLineItem.BasketId = basket.BasketId; shipRateLineItem.OrderItemType = OrderItemType.Shipping; shipRateLineItem.BasketShipmentId = shipment.BasketShipmentId; shipRateLineItem.Name = shipMethod.Name; shipRateLineItem.Price = rate.Rate; shipRateLineItem.Quantity = 1; shipRateLineItem.TaxCodeId = shipMethod.TaxCodeId; shipRateLineItem.Save(); basket.Items.Add(shipRateLineItem); if (rate.Surcharge > 0) { shipRateLineItem = new BasketItem(); shipRateLineItem.BasketId = basket.BasketId; shipRateLineItem.OrderItemType = OrderItemType.Handling; shipRateLineItem.BasketShipmentId = shipment.BasketShipmentId; shipRateLineItem.Name = shipMethod.Name; shipRateLineItem.Price = rate.Surcharge; shipRateLineItem.Quantity = 1; shipRateLineItem.TaxCodeId = shipMethod.SurchargeTaxCodeId; shipRateLineItem.Save(); basket.Items.Add(shipRateLineItem); } } else { //rate quote could not be obtained for some reason. Logger.Warn("Failed to obtain rate quote for the given ship method '" + shipMethod.Name + "'"); //here we need to communicate back to the caller that the selected ship method can't be used shipment.ShipMethodId = 0; shipment.Save(); } } } } }
/// <summary> /// Gets a collection of ship rate quotes for the given shipment /// </summary> /// <param name="shipment">The basket shipment for which to get the rate quotes</param> /// <returns>A collection of ship rate quotes</returns> public static Collection <ShipRateQuote> QuoteForShipment(IShipment shipment) { Collection <ShipRateQuote> rateQuotes = new Collection <ShipRateQuote>(); //GET ALL OF THE POSSIBLE SHIPMETHODS ShipMethodCollection shipMethods = ShipMethodDataSource.LoadForShipment(shipment); foreach (ShipMethod method in shipMethods) { ShipRateQuote quote = method.GetShipRateQuote(shipment); if (quote != null) { rateQuotes.Add(quote); } } return(rateQuotes); }
/// <summary> /// Creates a copy of the current instance /// </summary> /// <returns>A copy of the current instance</returns> public ShipRateQuote Clone() { ShipRateQuote clonedQuote = new ShipRateQuote(); clonedQuote.ShipMethodId = this.ShipMethodId; clonedQuote.Rate = this.Rate; clonedQuote.Surcharge = this.Surcharge; if (_Warnings != null && _Warnings.Count > 0) { clonedQuote.AddWarnings(_Warnings); } if (this.ShipMethodLoaded) { clonedQuote.ShipMethod = this.ShipMethod; } return(clonedQuote); }
/// <summary> /// Gets an array of ship rate quotes for the given basket /// </summary> /// <param name="basket">The basket for which to get the ship rate quotes</param> /// <param name="destination">The destination address for which to get the rates</param> /// <returns>An array of ShipRateQuote objects</returns> public static ShipRateQuote[] QuoteForBasket(Basket basket, Address destination) { //******* //SUMMARY: LOOP ALL SHIPMENTS IN BASKET AND GET QUOTES FOR EACH //ADD QUOTES TOGETHER FOR SERVICES IN COMMON TO ALL SHIPMENTS AND RETURN TOTALS //******* //CREATE A DICTIONARY TO HOLD QUOTED RATES Dictionary <int, ShipRateQuoteStack> rateQuotes = new Dictionary <int, ShipRateQuoteStack>(); foreach (BasketShipment shipment in basket.Shipments) { //GET ALL OF THE POSSIBLE SHIPMETHODS Address tempAddress = shipment.Address; shipment.SetAddress(destination); ShipMethodCollection shipMethods = ShipMethodDataSource.LoadForShipment(shipment); foreach (ShipMethod method in shipMethods) { ShipRateQuote quote = method.GetShipRateQuote(shipment); if (quote != null) { if (rateQuotes.ContainsKey(method.ShipMethodId)) { rateQuotes[method.ShipMethodId].Add(quote); } else { rateQuotes.Add(method.ShipMethodId, new ShipRateQuoteStack(quote)); } } } shipment.SetAddress(tempAddress); } //NOW BUILD LIST OF QUOTES VALID FOR ALL SHIPMENTS List <ShipRateQuote> validQuotes = new List <ShipRateQuote>(); foreach (ShipRateQuoteStack item in rateQuotes.Values) { if (item.ShipmentCount == basket.Shipments.Count) { validQuotes.Add(item.ShipRateQuote); } } return(validQuotes.ToArray()); }
public ShipRateQuoteStack(ShipRateQuote shipRateQuote) { this.ShipRateQuote = shipRateQuote; this.ShipmentCount = 1; }
public void Add(ShipRateQuote shipRateQuote) { this.ShipRateQuote.Rate += shipRateQuote.Rate; this.ShipRateQuote.Surcharge += shipRateQuote.Surcharge; this.ShipmentCount++; }