コード例 #1
0
        /// <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());
        }
コード例 #2
0
        /// <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);
        }