/// <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);
        }
Example #2
0
        public static TaxGatewayCollection LoadForStore(int maximumRows, int startRowIndex, string sortExpression)
        {
            int storeId = Token.Instance.StoreId;
            //CREATE THE DYNAMIC SQL TO LOAD OBJECT
            StringBuilder selectQuery = new StringBuilder();

            selectQuery.Append("SELECT");
            if (maximumRows > 0)
            {
                selectQuery.Append(" TOP " + (startRowIndex + maximumRows).ToString());
            }
            selectQuery.Append(" " + TaxGateway.GetColumnNames(string.Empty));
            selectQuery.Append(" FROM ac_TaxGateways");
            selectQuery.Append(" WHERE StoreId = @storeId");
            if (!string.IsNullOrEmpty(sortExpression))
            {
                selectQuery.Append(" ORDER BY " + sortExpression);
            }
            Database  database      = Token.Instance.Database;
            DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString());

            database.AddInParameter(selectCommand, "@storeId", System.Data.DbType.Int32, storeId);
            //EXECUTE THE COMMAND
            TaxGatewayCollection results = new TaxGatewayCollection();
            int thisIndex = 0;
            int rowCount  = 0;

            using (IDataReader dr = database.ExecuteReader(selectCommand))
            {
                while (dr.Read() && ((maximumRows < 1) || (rowCount < maximumRows)))
                {
                    if (thisIndex >= startRowIndex)
                    {
                        TaxGateway taxGateway = new TaxGateway();
                        TaxGateway.LoadDataReader(taxGateway, dr);
                        results.Add(taxGateway);
                        rowCount++;
                    }
                    thisIndex++;
                }
                dr.Close();
            }
            return(results);
        }