Exemplo n.º 1
0
        /// <summary>
        /// Clears all existing taxes and 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 will be removed from the order before the calculation.</remarks>
        public static LSDecimal Recalculate(Order order)
        {
            //CLEAR ANY EXISTING TAXES
            ClearExistingTaxes(order);

            //DO NOT PROCESS TAXES IF USER BELONGS TO A TEXT EXEMPT GROUP
            User user = order.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.Recalculate(order);
                    }
                    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);
        }