/// <summary>
 /// Loads the given TaxRule object from the given database data reader.
 /// </summary>
 /// <param name="taxRule">The TaxRule object to load.</param>
 /// <param name="dr">The database data reader to read data from.</param>
 public static void LoadDataReader(TaxRule taxRule, IDataReader dr)
 {
     //SET FIELDS FROM ROW DATA
     taxRule.TaxRuleId         = dr.GetInt32(0);
     taxRule.StoreId           = dr.GetInt32(1);
     taxRule.Name              = dr.GetString(2);
     taxRule.TaxRate           = dr.GetDecimal(3);
     taxRule.UseBillingAddress = dr.GetBoolean(4);
     taxRule.GroupRuleId       = dr.GetByte(5);
     taxRule.TaxCodeId         = NullableData.GetInt32(dr, 6);
     taxRule.Priority          = dr.GetInt16(7);
     taxRule.RoundingRuleId    = dr.GetByte(8);
     taxRule.UsePerItemTax     = dr.GetBoolean(9);
     taxRule.IsDirty           = false;
 }
        public static TaxRuleCollection LoadForTaxCodes(int[] taxCodeIds, TaxAddress taxAddress, User user)
        {
            //IF WE DO NOT HAVE VALID TAX CODES THERE ARE NO RULES
            if ((taxCodeIds == null) || (taxCodeIds.Length == 0))
            {
                return(new TaxRuleCollection());
            }
            TaxRuleCollection potentialTaxRules = new TaxRuleCollection();

            //FIRST DETERMINE THE ZONE(S) FOR THE Tax Address
            ShipZoneCollection shipmentZoneCollection = taxAddress.ShipZones;

            //BUILD A LIST OF ZONEIDS FOR QUERY CRITERIA
            StringBuilder selectQuery = new StringBuilder();

            selectQuery.Append("SELECT DISTINCT " + TaxRule.GetColumnNames("TR"));
            selectQuery.Append(" FROM (ac_TaxRules TR INNER JOIN ac_TaxRuleTaxCodes TRTC ON TR.TaxRuleId = TRTC.TaxRuleId)");
            selectQuery.Append(" LEFT JOIN ac_TaxRuleShipZones TRSZ ON TR.TaxRuleId = TRSZ.TaxRuleId");
            selectQuery.Append(" WHERE StoreId = @storeId ");
            if (taxCodeIds.Length == 1)
            {
                selectQuery.Append(" AND TRTC.TaxCodeId = " + taxCodeIds[0].ToString());
            }
            else
            {
                selectQuery.Append(" AND TRTC.TaxCodeId IN (" + AlwaysConvert.ToList(",", taxCodeIds) + ")");
            }

            //PROCESS SHIPZONE EXCLUSION
            selectQuery.Append(" AND (TRSZ.ShipZoneId IS NULL");
            for (int i = 0; i < shipmentZoneCollection.Count; i++)
            {
                selectQuery.Append(" OR TRSZ.ShipZoneId = @shipZoneId" + i.ToString());
            }
            selectQuery.Append(") ORDER BY TR.Priority");

            Database  database      = Token.Instance.Database;
            DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString());

            database.AddInParameter(selectCommand, "@storeId", System.Data.DbType.Int32, Token.Instance.StoreId);
            //ADD IN NUMBERED ZONE PARAMETERS
            for (int i = 0; i < shipmentZoneCollection.Count; i++)
            {
                database.AddInParameter(selectCommand, "@shipZoneId" + i.ToString(), System.Data.DbType.Int32, shipmentZoneCollection[i].ShipZoneId);
            }
            //EXECUTE THE COMMAND
            using (IDataReader dr = database.ExecuteReader(selectCommand))
            {
                while (dr.Read())
                {
                    TaxRule taxRule = new TaxRule();
                    TaxRule.LoadDataReader(taxRule, dr);
                    potentialTaxRules.Add(taxRule);
                }
                dr.Close();
            }

            //PROCESS GROUP EXCLUSIONS
            TaxRuleCollection potentialTaxRulesWithGroupFilter = new TaxRuleCollection();

            foreach (TaxRule taxRule in potentialTaxRules)
            {
                if (taxRule.AppliesToUser(user))
                {
                    potentialTaxRulesWithGroupFilter.Add(taxRule);
                }
            }

            //RETURN POTENTIAL TAXES
            return(potentialTaxRulesWithGroupFilter);
        }