public async Task <IActionResult> RatesByCountryStateZipList(ConfigurationModel searchModel) { if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageTaxSettings)) { return(await AccessDeniedDataTablesJson()); } var records = await _taxRateService.GetAllTaxRatesAsync(searchModel.Page - 1, searchModel.PageSize); var gridModel = await new CountryStateZipListModel().PrepareToGridAsync(searchModel, records, () => { return(records.SelectAwait(async record => new CountryStateZipModel { Id = record.Id, StoreId = record.StoreId, StoreName = (await _storeService.GetStoreByIdAsync(record.StoreId))?.Name ?? "*", TaxCategoryId = record.TaxCategoryId, TaxCategoryName = (await _taxCategoryService.GetTaxCategoryByIdAsync(record.TaxCategoryId))?.Name ?? string.Empty, CountryId = record.CountryId, CountryName = (await _countryService.GetCountryByIdAsync(record.CountryId))?.Name ?? "Unavailable", StateProvinceId = record.StateProvinceId, StateProvinceName = (await _stateProvinceService.GetStateProvinceByIdAsync(record.StateProvinceId))?.Name ?? "*", Zip = !string.IsNullOrEmpty(record.Zip) ? record.Zip : "*", Percentage = record.Percentage })); }); return(Json(gridModel)); }
/// <summary> /// Handle tax category deleted event /// </summary> /// <param name="eventMessage">Event message</param> public async Task HandleEventAsync(EntityDeletedEvent <TaxCategory> eventMessage) { var taxCategory = eventMessage?.Entity; if (taxCategory == null) { return; } //delete an appropriate record when tax category is deleted var recordsToDelete = (await _taxRateService.GetAllTaxRatesAsync()).Where(taxRate => taxRate.TaxCategoryId == taxCategory.Id).ToList(); foreach (var taxRate in recordsToDelete) { await _taxRateService.DeleteTaxRateAsync(taxRate); } //delete saved fixed rate if exists var setting = await _settingService.GetSettingAsync(string.Format(FixedOrByCountryStateZipDefaults.FixedRateSettingsKey, taxCategory.Id)); if (setting != null) { await _settingService.DeleteSettingAsync(setting); } }
/// <summary> /// Gets tax rate /// </summary> /// <param name="taxRateRequest">Tax rate request</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the ax /// </returns> public async Task <TaxRateResult> GetTaxRateAsync(TaxRateRequest taxRateRequest) { var result = new TaxRateResult(); //the tax rate calculation by fixed rate if (!_countryStateZipSettings.CountryStateZipEnabled) { result.TaxRate = await _settingService.GetSettingByKeyAsync <decimal>(string.Format(FixedOrByCountryStateZipDefaults.FixedRateSettingsKey, taxRateRequest.TaxCategoryId)); return(result); } //the tax rate calculation by country & state & zip if (taxRateRequest.Address == null) { result.Errors.Add("Address is not set"); return(result); } //first, load all tax rate records (cached) - loaded only once var cacheKey = _staticCacheManager.PrepareKeyForDefaultCache(ModelCacheEventConsumer.ALL_TAX_RATES_MODEL_KEY); var allTaxRates = await _staticCacheManager.GetAsync(cacheKey, async() => (await _taxRateService.GetAllTaxRatesAsync()).Select(taxRate => new TaxRate { Id = taxRate.Id, StoreId = taxRate.StoreId, TaxCategoryId = taxRate.TaxCategoryId, CountryId = taxRate.CountryId, StateProvinceId = taxRate.StateProvinceId, Zip = taxRate.Zip, Percentage = taxRate.Percentage }).ToList()); var storeId = taxRateRequest.CurrentStoreId; var taxCategoryId = taxRateRequest.TaxCategoryId; var countryId = taxRateRequest.Address.CountryId; var stateProvinceId = taxRateRequest.Address.StateProvinceId; var zip = taxRateRequest.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); }