示例#1
0
        /// <summary>
        /// Determines in a tax applies based on the given address.
        /// </summary>
        /// <param name="address">The address used to determine tax liability</param>
        /// <returns>True if the tax applies to the given address</returns>
        /// <remarks>This method does not check nexus, it only decides whether the shipzone filter is met by the given address.</remarks>
        public bool AppliesToAddress(TaxAddress address)
        {
            if (this.TaxRuleShipZones.Count == 0)
            {
                return(true);
            }
            ShipZoneCollection shipZones = address.ShipZones;

            foreach (ShipZone zone in shipZones)
            {
                if (this.TaxRuleShipZones.IndexOf(this.TaxRuleId, zone.ShipZoneId) > -1)
                {
                    return(true);
                }
            }
            return(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);
        }