/// <summary> /// Calculates and applies taxes for the given order /// </summary> /// <param name="basket">The order to calculate taxes on.</param> /// <returns>The total amount of tax applied.</returns> /// <remarks>Any pre-existing tax line items must be removed from the order before the calculation.</remarks> public override LSDecimal Recalculate(Order order) { if (order == null) { throw new ArgumentNullException("order"); } // CREATE THE DOR GATEWAY INSTANCE DorGateway gateway = new DorGateway(order, this.TaxName, this.UseDebugMode); // BUILD A DICTIONARY OF TAXABLE TOTALS BY SHIPMENT Dictionary <int, decimal> shipmentTotals = new Dictionary <int, decimal>(); // LOOP ITEMS IN BASKET TO CALCULATE TAX OrderItemCollection newTaxItems = new OrderItemCollection(); foreach (OrderItem item in order.Items) { // SEE IF ITEM HAS A TAX CODE THAT APPLIES if (item.TaxCodeId > 0 && this.TaxCodes.Contains(item.TaxCodeId)) { if (shipmentTotals.ContainsKey(item.OrderShipmentId)) { shipmentTotals[item.OrderShipmentId] += (decimal)item.ExtendedPrice; } else { shipmentTotals[item.OrderShipmentId] = (decimal)item.ExtendedPrice; } } } // LOOP TAXABLE SHIPMENT TOTALS LSDecimal totalTax = 0; foreach (int shipmentId in shipmentTotals.Keys) { // SEE IF THERE IS A TAXINFO INSTANCE FOR THIS ITEM TaxInfo taxInfo = gateway.GetTaxInfo(shipmentId); if (taxInfo != null) { // A TAXINFO STRUCTURE EXISTS, SO CREATE TAX ITEM AND ADD TO BASKET OrderItem taxItem = TaxUtility.GenerateOrderItem(order.OrderId, shipmentId, shipmentTotals[shipmentId], taxInfo); totalTax += taxItem.ExtendedPrice; order.Items.Add(taxItem); } } // SAVE ORDER order.Items.Save(); // return total tax added to basekt return(newTaxItems.TotalPrice()); }
/// <summary> /// Calculates tax for the given basket /// </summary> /// <param name="basket">The basket</param> /// <param name="save">If true, items added to the basket are saved</param> /// <returns>Tax items are added to the basket and total calculated tax is returned</returns> public LSDecimal Calculate(Basket basket, bool save) { // CREATE THE DOR GATEWAY INSTANCE DorGateway gateway = new DorGateway(basket, this.TaxName, this.UseDebugMode); // BUILD A DICTIONARY OF TAXABLE TOTALS BY SHIPMENT Dictionary <int, decimal> shipmentTotals = new Dictionary <int, decimal>(); // LOOP ITEMS IN BASKET TO CALCULATE TAX BasketItemCollection newTaxItems = new BasketItemCollection(); foreach (BasketItem item in basket.Items) { // SEE IF ITEM HAS A TAX CODE THAT APPLIES if (item.TaxCodeId > 0 && this.TaxCodes.Contains(item.TaxCodeId)) { if (shipmentTotals.ContainsKey(item.BasketShipmentId)) { shipmentTotals[item.BasketShipmentId] += (decimal)item.ExtendedPrice; } else { shipmentTotals[item.BasketShipmentId] = (decimal)item.ExtendedPrice; } } } // LOOP TAXABLE SHIPMENT TOTALS LSDecimal totalTax = 0; foreach (int shipmentId in shipmentTotals.Keys) { // SEE IF THERE IS A TAXINFO INSTANCE FOR THIS ITEM TaxInfo taxInfo = gateway.GetTaxInfo(shipmentId); if (taxInfo != null) { // A TAXINFO STRUCTURE EXISTS, SO CREATE TAX ITEM AND ADD TO BASKET BasketItem taxItem = TaxUtility.GenerateBasketItem(basket.BasketId, shipmentId, shipmentTotals[shipmentId], taxInfo); totalTax += taxItem.ExtendedPrice; basket.Items.Add(taxItem); } } // SAVE BASKET IF INDICATED if (save) { basket.Items.Save(); } // return total tax added to basekt return(totalTax); }
/// <summary> /// Calculates tax for the order item using the specified rule /// </summary> /// <param name="orderId">Id of the order</param> /// <param name="shipmentId">Id of the shipment</param> /// <param name="taxableValue">Total value being taxed</param> /// <param name="taxInfo">The tax info structure to apply</param> /// <returns>The new order item</returns> /// <remarks>This method does no checking for validity, it only handles calculations /// and generation of an item for the given values.</remarks> public static OrderItem GenerateOrderItem(int orderId, int shipmentId, decimal taxableValue, TaxInfo taxInfo) { OrderItem taxLineItem = new OrderItem(); taxLineItem.OrderId = orderId; taxLineItem.OrderItemType = OrderItemType.Tax; taxLineItem.OrderShipmentId = shipmentId; taxLineItem.ParentItemId = 0; taxLineItem.Name = taxInfo.TaxName; taxLineItem.Sku = taxInfo.TaxSku; taxLineItem.Price = taxInfo.Calculate(taxableValue); taxLineItem.Quantity = 1; return(taxLineItem); }