示例#1
0
        /// <summary>
        /// Calculates the amount of tax for the given value
        /// </summary>
        /// <param name="value">The value to calculate tax for</param>
        /// <returns>The calculated tax</returns>
        public decimal Calculate(decimal value)
        {
            // THE RATE IS ALREADY IN PERCENTAGE, MULTIPLY IT TO COST TO GET TAX AMOUNT
            decimal tax = value * this.TaxRate;

            return(TaxHelper.Round((decimal)tax, 2, RoundingRule.Common));
        }
示例#2
0
        private static LSDecimal CalculateTax(TaxRule taxRule, LSDecimal price)
        {
            // DETERMINE TAX FOR TOTAL PRICE
            LSDecimal tempTax = ((price * taxRule.TaxRate) / 100);

            return(TaxHelper.Round((decimal)tempTax, 2, taxRule.RoundingRule));
        }
示例#3
0
        /// <summary>
        /// Calculates the Tax for the given price
        /// </summary>
        /// <param name="unitPrice">The unit price to calculate Tax for</param>
        /// <param name="quantity" >The Quantity of the line item</param>
        /// <param name="taxCodeId">The tax code that applies to this price</param>
        /// <param name="priority">The priority of the item - 0 for products, can be greater than 0 for non products items.</param>
        /// <param name="billingAddress">The billing address that applies to the price</param>
        /// <param name="shippingAddress">The shipping address that applies to the price</param>
        /// <param name="user">The user being taxed</param>
        /// <returns>The Tax details for the given price</returns>
        public static TaxInfo InternalGetTaxInfo(LSDecimal unitPrice, int quantity, int taxCodeId, int priority, TaxAddress billingAddress, TaxAddress shippingAddress, User user, bool priceIncludesTax)
        {
            bool      isNegativePrice = (unitPrice < 0);
            LSDecimal absPrice        = isNegativePrice ? (unitPrice * -1) : unitPrice;
            //INITIALIZE TAXES
            LSDecimal totalTax     = 0;
            LSDecimal totalTaxRate = 0;
            //GET ANY RULES THAT MAY APPLY TO THESE ADDRESSES
            List <TaxRule>     taxRules = TaxRuleHelper.GetPotentialTaxRules(taxCodeId, billingAddress, shippingAddress, user);
            List <ShopTaxItem> taxItems = new List <ShopTaxItem>();

            //LOOP ANY RULES AND CALCULATE THE IMPACT OF Tax
            foreach (TaxRule taxRule in taxRules)
            {
                //PREVENT INCORRECT COMPOUNDING, AND ENSURE TAX MEETS ADDRESS CRITERIA
                if (priority < taxRule.Priority && taxRule.AppliesToAddress(billingAddress, shippingAddress))
                {
                    int tempTaxItemCount = taxItems.Count;
                    if (taxRule.AppliesToTaxCode(taxCodeId))
                    {
                        LSDecimal tempTax = LineItemCalculator.CalculateTaxForItem(taxRule, absPrice, quantity, priceIncludesTax);
                        taxItems.Add(new ShopTaxItem(taxRule.TaxCodeId, tempTax, taxRule.TaxRate));
                        totalTax     += tempTax;
                        totalTaxRate += taxRule.TaxRate;
                    }
                    //CHECK IF THIS TAX APPLIES ON ANY PREEXISTING SHOPTAX ITEMS
                    for (int i = 0; i < tempTaxItemCount; i++)
                    {
                        ShopTaxItem thisItem = taxItems[i];
                        if (taxRule.AppliesToTaxCode(thisItem.TaxCodeId))
                        {
                            //TAX ON TAX, RATE MUST BE CALCULATED
                            LSDecimal tempTax = LineItemCalculator.CalculateTaxForItem(taxRule, thisItem.Price, 1, priceIncludesTax);
                            taxItems.Add(new ShopTaxItem(taxRule.TaxCodeId, tempTax, taxRule.TaxRate));
                            totalTax     += tempTax;
                            totalTaxRate += TaxHelper.Round(((decimal)thisItem.TaxRate * (decimal)taxRule.TaxRate) / 100, 2, taxRule.RoundingRule);
                        }
                    }
                }
            }
            if (priceIncludesTax)
            {
                //IF PRICE IS Tax INCLUSIVE, THEN CALCULATED Tax CANNOT EXCEED PRICE
                //THIS WOULD INDICATE A CONFIGURATION ERROR
                if (totalTax > absPrice)
                {
                    totalTax = unitPrice;
                }
                if (totalTaxRate > 100)
                {
                    totalTaxRate = 100;
                }
                if (isNegativePrice)
                {
                    totalTax = totalTax * -1;
                }
                LSDecimal actualPrice = unitPrice - totalTax;
                return(new TaxInfo(actualPrice, totalTax, totalTaxRate));
            }
            else
            {
                if (isNegativePrice)
                {
                    totalTax = totalTax * -1;
                }
                return(new TaxInfo(unitPrice, totalTax, totalTaxRate));
            }
        }