/// <summary> /// Gets tax rate /// </summary> /// <param name="calculateTaxRequest">Tax calculation request</param> /// <returns>Tax</returns> public CalculateTaxResult GetTaxRate(CalculateTaxRequest calculateTaxRequest) { var result = new CalculateTaxResult { TaxRate = GetTaxRate(calculateTaxRequest.TaxCategoryId) }; return result; }
/// <summary> /// Gets tax rate /// </summary> /// <param name="calculateTaxRequest">Tax calculation request</param> /// <returns>Tax</returns> public CalculateTaxResult GetTaxRate(CalculateTaxRequest calculateTaxRequest) { var result = new CalculateTaxResult() { TaxRate = decimal.Zero }; return result; }
/// <summary> /// Gets tax rate /// </summary> /// <param name="calculateTaxRequest">Tax calculation request</param> /// <returns>Tax</returns> public CalculateTaxResult GetTaxRate(CalculateTaxRequest calculateTaxRequest) { var result = new CalculateTaxResult(); if (calculateTaxRequest.Address == null) { result.Errors.Add("Address is not set"); return result; } var taxRates = _taxRateService.GetAllTaxRates(calculateTaxRequest.TaxCategoryId, calculateTaxRequest.Address.Country != null ? calculateTaxRequest.Address.Country.Id: 0, calculateTaxRequest.Address.StateProvince != null ? calculateTaxRequest.Address.StateProvince.Id : 0, calculateTaxRequest.Address.ZipPostalCode); if (taxRates.Count > 0) result.TaxRate = taxRates[0].Percentage; return result; }
/// <summary> /// Gets tax rate /// </summary> /// <param name="calculateTaxRequest">Tax calculation request</param> /// <returns>Tax</returns> public CalculateTaxResult GetTaxRate(CalculateTaxRequest calculateTaxRequest) { var result = new CalculateTaxResult(); if (calculateTaxRequest.Address == null) { result.Errors.Add("Address is not set"); return result; } //first, load all tax rate records (cached) - loaded only once string cacheKey = ModelCacheEventConsumer.ALL_TAX_RATES_MODEL_KEY; var allTaxRates = _cacheManager.Get(cacheKey, () => _taxRateService .GetAllTaxRates() .Select(x => new TaxRateForCaching { Id = x.Id, StoreId = x.StoreId, TaxCategoryId = x.TaxCategoryId, CountryId = x.CountryId, StateProvinceId = x.StateProvinceId, Zip = x.Zip, Percentage = x.Percentage, } ) .ToList() ); int storeId = _storeContext.CurrentStore.Id; int taxCategoryId = calculateTaxRequest.TaxCategoryId; int countryId = calculateTaxRequest.Address.Country != null ? calculateTaxRequest.Address.Country.Id : 0; int stateProvinceId = calculateTaxRequest.Address.StateProvince != null ? calculateTaxRequest.Address.StateProvince.Id : 0; string zip = calculateTaxRequest.Address.ZipPostalCode; if (zip == null) zip = string.Empty; zip = zip.Trim(); var existingRates = new List<TaxRateForCaching>(); foreach (var taxRate in allTaxRates) { if (taxRate.CountryId == countryId && taxRate.TaxCategoryId == taxCategoryId) existingRates.Add(taxRate); } //filter by store var matchedByStore = new List<TaxRateForCaching>(); //first, find by a store ID foreach (var taxRate in existingRates) if (storeId == taxRate.StoreId) matchedByStore.Add(taxRate); //not found? use the default ones (ID == 0) if (!matchedByStore.Any()) foreach (var taxRate in existingRates) if (taxRate.StoreId == 0) matchedByStore.Add(taxRate); //filter by state/province var matchedByStateProvince = new List<TaxRateForCaching>(); //first, find by a state ID foreach (var taxRate in matchedByStore) if (stateProvinceId == taxRate.StateProvinceId) matchedByStateProvince.Add(taxRate); //not found? use the default ones (ID == 0) if (!matchedByStateProvince.Any()) foreach (var taxRate in matchedByStore) if (taxRate.StateProvinceId == 0) matchedByStateProvince.Add(taxRate); //filter by zip var matchedByZip = new List<TaxRateForCaching>(); foreach (var taxRate in matchedByStateProvince) if ((String.IsNullOrEmpty(zip) && String.IsNullOrEmpty(taxRate.Zip)) || (zip.Equals(taxRate.Zip, StringComparison.InvariantCultureIgnoreCase))) matchedByZip.Add(taxRate); if (!matchedByZip.Any()) foreach (var taxRate in matchedByStateProvince) if (String.IsNullOrWhiteSpace(taxRate.Zip)) matchedByZip.Add(taxRate); if (matchedByZip.Any()) result.TaxRate = matchedByZip[0].Percentage; return result; }
/// <summary> /// Gets tax rate /// </summary> /// <param name="calculateTaxRequest">Tax calculation request</param> /// <returns>Tax</returns> public CalculateTaxResult GetTaxRate(CalculateTaxRequest calculateTaxRequest) { var result = new CalculateTaxResult(); var address = calculateTaxRequest.Address; if (address == null) { result.AddError("Address is not set"); return result; } if (address.Country == null) { result.AddError("Country is not set"); return result; } //************************************************************************************************************** // As a Registered StrikeIron user, you can authenticate to a StrikeIron web service with either a // UserID/Password combination or a License Key. If you wish to use a License Key, // assign this value to the UserID field and set the Password field to null. //************************************************************************************************************** string userId = _strikeIronTaxSettings.UserId; string password = _strikeIronTaxSettings.Password; decimal taxRate = decimal.Zero; if (address.Country.TwoLetterIsoCode.ToLower() == "us") { if (String.IsNullOrEmpty(address.ZipPostalCode)) { result.AddError("Zip is not provided"); return result; } string error = ""; taxRate = GetTaxRateUsa(address.ZipPostalCode, userId, password, ref error); if (!String.IsNullOrEmpty(error)) { result.AddError(error); return result; } } else if (address.Country.TwoLetterIsoCode.ToLower() == "ca") { if (address.StateProvince == null) { result.AddError("Province is not set"); return result; } string error = ""; taxRate = GetTaxRateCanada(address.StateProvince.Abbreviation, userId, password, ref error); if (!String.IsNullOrEmpty(error)) { result.AddError(error); return result; } } else { result.AddError("Tax can be calculated only for USA zip or Canada province"); return result; } result.TaxRate = taxRate * 100; return result; }