Exemplo n.º 1
0
        /// <summary>
        /// Calculates and applies taxes for the given basket.
        /// </summary>
        /// <param name="basket">The basket to calculate taxes on.</param>
        /// <returns>The total amount of tax applied.</returns>
        /// <remarks>Any pre-existing tax line items will be removed from the basket before the calculation.</remarks>
        public static LSDecimal Calculate(Basket basket)
        {
            //CLEAR ANY EXISTING TAXES
            ClearExistingTaxes(basket);

            //DO NOT PROCESS TAXES IF USER BELONGS TO A TEXT EXEMPT GROUP
            User user = basket.User;

            if (user != null)
            {
                foreach (UserGroup userGroup in user.UserGroups)
                {
                    if (userGroup.Group != null && userGroup.Group.IsTaxExempt)
                    {
                        return(0);
                    }
                }
            }

            //INITIALIZE TAXES
            LSDecimal totalTax = 0;

            //FINALIZE ANY TAXES FROM INTEGRATED PROVIDERS
            TaxGatewayCollection taxGateways = Token.Instance.Store.TaxGateways;

            foreach (TaxGateway taxGateway in taxGateways)
            {
                ITaxProvider provider = taxGateway.GetProviderInstance();
                if (provider != null)
                {
                    try
                    {
                        totalTax += provider.Calculate(basket);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error("Could not calculate with the configured tax provider: " + taxGateway.ClassId, ex);
                    }
                }
                else
                {
                    Logger.Error("Could not load the configured tax provider: " + taxGateway.ClassId);
                }
            }
            return(totalTax);
        }