public CalculateTaxResult GetTaxRate(CalculateTaxRequest calculateTaxRequest) { var result = new CalculateTaxResult(); if (calculateTaxRequest.Address == null) { result.Errors.Add("Address is not set"); return(result); } if (_taxSettings.EuVatEnabled) { if (!(calculateTaxRequest.Address.Country?.SubjectToVat ?? false)) { // Fallback to fixed rate (merchant country VAT rate). result.TaxRate = _settingService.GetSettingByKey <decimal>($"Tax.TaxProvider.FixedRate.TaxCategoryId{calculateTaxRequest.TaxCategoryId}"); return(result); } } var taxRates = _taxRateService.GetAllTaxRates( calculateTaxRequest.TaxCategoryId, calculateTaxRequest.Address.Country?.Id ?? 0, calculateTaxRequest.Address.StateProvince?.Id ?? 0, calculateTaxRequest.Address.ZipPostalCode); if (taxRates.Any()) { 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(); //the tax rate calculation by fixed rate if (!_countryStateZipSettings.CountryStateZipEnabled) { result.TaxRate = _settingService.GetSettingByKey <decimal>(string.Format(FixedOrByCountryStateZipDefaults.FixedRateSettingsKey, calculateTaxRequest.TaxCategoryId)); return(result); } //the tax rate calculation by country & state & zip if (calculateTaxRequest.Address == null) { result.Errors.Add("Address is not set"); return(result); } //first, load all tax rate records (cached) - loaded only once var cacheKey = ModelCacheEventConsumer.ALL_TAX_RATES_MODEL_KEY; var allTaxRates = _cacheManager.Get(cacheKey, () => _taxRateService.GetAllTaxRates().Select(taxRate => new TaxRateForCaching { Id = taxRate.Id, StoreId = taxRate.StoreId, TaxCategoryId = taxRate.TaxCategoryId, CountryId = taxRate.CountryId, StateProvinceId = taxRate.StateProvinceId, Zip = taxRate.Zip, Percentage = taxRate.Percentage }).ToList()); var storeId = calculateTaxRequest.CurrentStoreId; var taxCategoryId = calculateTaxRequest.TaxCategoryId; var countryId = calculateTaxRequest.Address.Country?.Id ?? 0; var stateProvinceId = calculateTaxRequest.Address.StateProvince?.Id ?? 0; var zip = calculateTaxRequest.Address.ZipPostalCode?.Trim() ?? string.Empty; var existingRates = allTaxRates.Where(taxRate => taxRate.CountryId == countryId && taxRate.TaxCategoryId == taxCategoryId); //filter by store var matchedByStore = existingRates.Where(taxRate => storeId == taxRate.StoreId || taxRate.StoreId == 0); //filter by state/province var matchedByStateProvince = matchedByStore.Where(taxRate => stateProvinceId == taxRate.StateProvinceId || taxRate.StateProvinceId == 0); //filter by zip var matchedByZip = matchedByStateProvince.Where(taxRate => string.IsNullOrWhiteSpace(taxRate.Zip) || taxRate.Zip.Equals(zip, StringComparison.InvariantCultureIgnoreCase)); //sort from particular to general, more particular cases will be the first var foundRecords = matchedByZip.OrderBy(r => r.StoreId == 0).ThenBy(r => r.StateProvinceId == 0).ThenBy(r => string.IsNullOrEmpty(r.Zip)); var foundRecord = foundRecords.FirstOrDefault(); if (foundRecord != null) { result.TaxRate = foundRecord.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() { 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 Task <CalculateTaxResult> GetTaxRate(CalculateTaxRequest calculateTaxRequest) { var result = new CalculateTaxResult { TaxRate = GetTaxRate(calculateTaxRequest.TaxCategoryId) }; return(Task.FromResult(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 async Task <CalculateTaxResult> GetTaxRate(CalculateTaxRequest calculateTaxRequest) { var result = new CalculateTaxResult(); if (calculateTaxRequest.Address == null) { result.Errors.Add("Address is not set"); return(result); } const string cacheKey = ModelCacheEventConsumer.ALL_TAX_RATES_MODEL_KEY; var allTaxRates = await _cacheManager.GetAsync(cacheKey, async() => { var taxes = await _taxRateService.GetAllTaxRates(); return(taxes.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 })); }); var storeId = _storeContext.CurrentStore.Id; var taxCategoryId = calculateTaxRequest.TaxCategoryId; var countryId = calculateTaxRequest.Address.CountryId; var stateProvinceId = calculateTaxRequest.Address.StateProvinceId; var zip = calculateTaxRequest.Address.ZipPostalCode; if (zip == null) { zip = string.Empty; } zip = zip.Trim(); var existingRates = allTaxRates.Where(taxRate => taxRate.CountryId == countryId && taxRate.TaxCategoryId == taxCategoryId).ToList(); //filter by store var matchedByStore = existingRates.Where(taxRate => storeId == taxRate.StoreId).ToList(); //not found? use the default ones (ID == 0) if (!matchedByStore.Any()) { matchedByStore.AddRange(existingRates.Where(taxRate => string.IsNullOrEmpty(taxRate.StoreId))); } //filter by state/province var matchedByStateProvince = matchedByStore.Where(taxRate => stateProvinceId == taxRate.StateProvinceId).ToList(); //not found? use the default ones (ID == 0) if (!matchedByStateProvince.Any()) { matchedByStateProvince.AddRange(matchedByStore.Where(taxRate => string.IsNullOrEmpty(taxRate.StateProvinceId))); } //filter by zip var matchedByZip = matchedByStateProvince.Where(taxRate => (string.IsNullOrEmpty(zip) && string.IsNullOrEmpty(taxRate.Zip)) || zip.Equals(taxRate.Zip, StringComparison.OrdinalIgnoreCase)).ToList(); if (!matchedByZip.Any()) { matchedByZip.AddRange(matchedByStateProvince.Where(taxRate => string.IsNullOrWhiteSpace(taxRate.Zip))); } 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(); 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() ); string storeId = _storeContext.CurrentStore.Id; string taxCategoryId = calculateTaxRequest.TaxCategoryId; string countryId = calculateTaxRequest.Address.CountryId; string stateProvinceId = calculateTaxRequest.Address.StateProvinceId; 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.Count == 0) { foreach (var taxRate in existingRates) { if (!String.IsNullOrEmpty(taxRate.StoreId)) { 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.Count == 0) { foreach (var taxRate in matchedByStore) { if (String.IsNullOrEmpty(taxRate.StateProvinceId)) { 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.Count == 0) { foreach (var taxRate in matchedByStateProvince) { if (String.IsNullOrWhiteSpace(taxRate.Zip)) { matchedByZip.Add(taxRate); } } } if (matchedByZip.Count > 0) { 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 _productMappingRepo = EngineContext.Current.Resolve <IRepository <ProductMapping> >(); var productMapping = _productMappingRepo.TableNoTracking.FirstOrDefault(x => x.ProductId == calculateTaxRequest.Product.Id); var result = new CalculateTaxResult(); var _taxCategoryMappingService = EngineContext.Current.Resolve <IWB_TaxCategoryMappingService>(); if (productMapping != null) { //choose the tax rate calculation method if (!_countryStateZipSettings.CountryStateZipEnabled) { if (calculateTaxRequest.Product.TaxCategoryId != 0) { result = new CalculateTaxResult { TaxRate = _settingService.GetSettingByKey <decimal>(string.Format("Tax.Worldbuy.TaxProvider.FixedOrByCountryStateZip.TaxCategoryId{0}", calculateTaxRequest.TaxCategoryId)), }; } else { var product = calculateTaxRequest.Product; if (product.ProductCategories.Count == 0) { } else { int taxCategoryId = _taxCategoryMappingService.GetTaxCategoryId(product); var curRate = _settingService.GetSettingByKey <decimal>(string.Format("Tax.Worldbuy.TaxProvider.FixedOrByCountryStateZip.TaxCategoryId{0}", taxCategoryId)); result = new CalculateTaxResult { TaxRate = curRate, }; } } } else { //the tax rate calculation by country & state & zip if (calculateTaxRequest.Address == null) { result.Errors.Add("Address is not set"); return(result); } //first, load all tax rate records (cached) - loaded only once const string cacheKey = WB_ModelCacheEventConsumer.ALL_TAX_RATES_MODEL_KEY; var allTaxRates = _cacheManager.Get(cacheKey, () => _taxRateService .GetAllTaxRates() .Select(x => new WB_TaxRateForCaching { Id = x.Id, StoreId = x.StoreId, TaxCategoryId = x.TaxCategoryId, CountryId = x.CountryId, StateProvinceId = x.StateProvinceId, Zip = x.Zip, Percentage = x.Percentage, }).ToList() ); var storeId = _storeContext.CurrentStore.Id; var taxCategoryId = calculateTaxRequest.TaxCategoryId; var countryId = calculateTaxRequest.Address.Country != null ? calculateTaxRequest.Address.Country.Id : 0; var stateProvinceId = calculateTaxRequest.Address.StateProvince != null ? calculateTaxRequest.Address.StateProvince.Id : 0; var zip = calculateTaxRequest.Address.ZipPostalCode; if (zip == null) { zip = string.Empty; } zip = zip.Trim(); var existingRates = allTaxRates.Where(taxRate => taxRate.CountryId == countryId && taxRate.TaxCategoryId == taxCategoryId).ToList(); //filter by store //first, find by a store ID var matchedByStore = existingRates.Where(taxRate => storeId == taxRate.StoreId).ToList(); //not found? use the default ones (ID == 0) if (!matchedByStore.Any()) { matchedByStore.AddRange(existingRates.Where(taxRate => taxRate.StoreId == 0)); } //filter by state/province //first, find by a state ID var matchedByStateProvince = matchedByStore.Where(taxRate => stateProvinceId == taxRate.StateProvinceId).ToList(); //not found? use the default ones (ID == 0) if (!matchedByStateProvince.Any()) { matchedByStateProvince.AddRange(matchedByStore.Where(taxRate => taxRate.StateProvinceId == 0)); } //filter by zip var matchedByZip = matchedByStateProvince.Where(taxRate => (string.IsNullOrEmpty(zip) && string.IsNullOrEmpty(taxRate.Zip)) || zip.Equals(taxRate.Zip, StringComparison.InvariantCultureIgnoreCase)).ToList(); if (!matchedByZip.Any()) { matchedByZip.AddRange(matchedByStateProvince.Where(taxRate => string.IsNullOrWhiteSpace(taxRate.Zip))); } 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); }
/// <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); } string licenseKey = _strikeIronTaxSettings.LicenseKey; decimal taxRate; 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, licenseKey, 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, licenseKey, 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); }