/// <summary> /// Gets the tax rules that may apply /// </summary> /// <param name="taxCodeIds">The tax code of the taxable item(s)</param> /// <param name="billingAddress">The billing address for the item(s)</param> /// <param name="shippingAddresses">The shipping addresses for the item(s)</param> /// <param name="user">The user shopping for the item(s)</param> /// <returns>A List of TaxRule records that may apply</returns> public static List <TaxRule> GetPotentialTaxRules(int[] taxCodeIds, TaxAddress billingAddress, List <TaxAddress> shippingAddresses, User user) { // BUILD THE KEY String cacheKey = GetTaxCodesKey(taxCodeIds) + "~" + GetAddressKey(billingAddress) + "~" + GetAddressKey(shippingAddresses) + "~" + user.UserId; Dictionary <string, List <TaxRule> > taxRulesDic = null; // CHECK THE HTTP CONTEXT FOR EXISTING RULES if (HttpContext.Current != null) { taxRulesDic = HttpContext.Current.Items["PotentialTaxRules"] as Dictionary <string, List <TaxRule> >; if (taxRulesDic != null) { if (taxRulesDic.ContainsKey(cacheKey)) { return(taxRulesDic[cacheKey]); } } else { taxRulesDic = new Dictionary <string, List <TaxRule> >(); } } List <TaxRule> allRules = new List <TaxRule>(); TaxRuleCollection countryRules = TaxRuleDataSource.LoadForTaxCodes(taxCodeIds, billingAddress, user); foreach (TaxRule rule in countryRules) { allRules.Add(rule); } foreach (TaxAddress shippingAddress in shippingAddresses) { countryRules = TaxRuleDataSource.LoadForTaxCodes(taxCodeIds, shippingAddress, user); foreach (TaxRule rule in countryRules) { if (!ListContainsTaxRule(allRules, rule.TaxRuleId)) { allRules.Add(rule); } } } // IF CONTEXT IS AVAILABLE STORE THE RESULTS if (HttpContext.Current != null) { taxRulesDic[cacheKey] = allRules; HttpContext.Current.Items["PotentialTaxRules"] = taxRulesDic; } return(allRules); }
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); }