コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <summary>
        /// Handle tax category deleted event
        /// </summary>
        /// <param name="eventMessage">Event message</param>
        public void HandleEvent(EntityDeleted <TaxCategory> eventMessage)
        {
            var taxCategory = eventMessage?.Entity;

            if (taxCategory == null)
            {
                return;
            }

            //delete an appropriate record when tax category is deleted
            var recordsToDelete = _taxRateService.GetAllTaxRates().Where(taxRate => taxRate.TaxCategoryId == taxCategory.Id).ToList();

            foreach (var taxRate in recordsToDelete)
            {
                _taxRateService.DeleteTaxRate(taxRate);
            }

            //delete saved fixed rate if exists
            var setting = _settingService.GetSetting(string.Format(FixedOrByCountryStateZipDefaults.FixedRateSettingsKey, taxCategory.Id));

            if (setting != null)
            {
                _settingService.DeleteSetting(setting);
            }
        }
        public IActionResult RatesByCountryStateZipList(ConfigurationModel searchModel)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageTaxSettings))
            {
                return(AccessDeniedDataTablesJson());
            }

            var records = _taxRateService.GetAllTaxRates(searchModel.Page - 1, searchModel.PageSize);

            var gridModel = new CountryStateZipListModel().PrepareToGrid(searchModel, records, () =>
            {
                return(records.Select(record => new CountryStateZipModel
                {
                    Id = record.Id,
                    StoreId = record.StoreId,
                    StoreName = _storeService.GetStoreById(record.StoreId)?.Name ?? "*",
                    TaxCategoryId = record.TaxCategoryId,
                    TaxCategoryName = _taxCategoryService.GetTaxCategoryById(record.TaxCategoryId)?.Name ?? string.Empty,
                    CountryId = record.CountryId,
                    CountryName = _countryService.GetCountryById(record.CountryId)?.Name ?? "Unavailable",
                    StateProvinceId = record.StateProvinceId,
                    StateProvinceName = _stateProvinceService.GetStateProvinceById(record.StateProvinceId)?.Name ?? "*",
                    Zip = !string.IsNullOrEmpty(record.Zip) ? record.Zip : "*",
                    Percentage = record.Percentage
                }));
            });

            return(Json(gridModel));
        }
コード例 #4
0
        public IActionResult RatesByCountryStateZipList(DataSourceRequest command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageTaxSettings))
            {
                return(AccessDeniedKendoGridJson());
            }

            var records       = _taxRateService.GetAllTaxRates(command.Page - 1, command.PageSize);
            var taxRatesModel = records.Select(record => new CountryStateZipModel
            {
                Id                = record.Id,
                StoreId           = record.StoreId,
                StoreName         = _storeService.GetStoreById(record.StoreId)?.Name ?? "*",
                TaxCategoryId     = record.TaxCategoryId,
                TaxCategoryName   = _taxCategoryService.GetTaxCategoryById(record.TaxCategoryId)?.Name ?? string.Empty,
                CountryId         = record.CountryId,
                CountryName       = _countryService.GetCountryById(record.CountryId)?.Name ?? "Unavailable",
                StateProvinceId   = record.StateProvinceId,
                StateProvinceName = _stateProvinceService.GetStateProvinceById(record.StateProvinceId)?.Name ?? "*",
                Zip               = !string.IsNullOrEmpty(record.Zip) ? record.Zip : "*",
                Percentage        = record.Percentage,
            }).ToList();

            var gridModel = new DataSourceResult
            {
                Data  = taxRatesModel,
                Total = records.TotalCount
            };

            return(Json(gridModel));
        }
コード例 #5
0
        public ActionResult RatesByCountryStateZipList(DataSourceRequest command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageTaxSettings))
            {
                return(Content("Access denied"));
            }

            var records       = _taxRateService.GetAllTaxRates(command.Page - 1, command.PageSize);
            var taxRatesModel = records
                                .Select(x =>
            {
                var m = new CountryStateZipModel
                {
                    Id              = x.Id,
                    StoreId         = x.StoreId,
                    TaxCategoryId   = x.TaxCategoryId,
                    CountryId       = x.CountryId,
                    StateProvinceId = x.StateProvinceId,
                    Zip             = x.Zip,
                    Percentage      = x.Percentage,
                };
                //store
                var store   = _storeService.GetStoreById(x.StoreId);
                m.StoreName = store != null ? store.Name : "*";
                //tax category
                var tc            = _taxCategoryService.GetTaxCategoryById(x.TaxCategoryId);
                m.TaxCategoryName = tc != null ? tc.Name : "";
                //country
                var c         = _countryService.GetCountryById(x.CountryId);
                m.CountryName = c != null ? c.Name : "Unavailable";
                //state
                var s = _stateProvinceService.GetStateProvinceById(x.StateProvinceId);
                m.StateProvinceName = s != null ? s.Name : "*";
                //zip
                m.Zip = !string.IsNullOrEmpty(x.Zip) ? x.Zip : "*";
                return(m);
            }).ToList();

            var gridModel = new DataSourceResult
            {
                Data  = taxRatesModel,
                Total = records.TotalCount
            };

            return(Json(gridModel));
        }
コード例 #6
0
        //tax category
        public void HandleEvent(EntityDeleted <TaxCategory> eventMessage)
        {
            if (eventMessage.Entity == null)
            {
                return;
            }

            //delete an appropriate record when tax category is deleted
            var recordsToDelete = _taxRateService.GetAllTaxRates().Where(tr => tr.TaxCategoryId == eventMessage.Entity.Id).ToList();

            foreach (var taxRate in recordsToDelete)
            {
                _taxRateService.DeleteTaxRate(taxRate);
            }

            var settingKey = string.Format("Tax.TaxProvider.FixedOrByCountryStateZip.TaxCategoryId{0}", eventMessage.Entity.Id);
            var setting    = _settingService.GetSetting(settingKey);

            if (setting != null)
            {
                _settingService.DeleteSetting(setting);
            }
        }
コード例 #7
0
        /// <summary>
        /// Gets tax rate
        /// </summary>
        /// <param name="calculateTaxRequest">Tax calculation request</param>
        /// <returns>Tax</returns>
        public CalculateTaxResult GetTaxRate(CalculateTaxRequest calculateTaxRequest)
        {
            var result = new CalculateTaxResult();

            //choose the tax rate calculation method
            if (!_countryStateZipSettings.CountryStateZipEnabled)
            {
                //the tax rate calculation by fixed rate
                result = new CalculateTaxResult
                {
                    TaxRate = _settingService.GetSettingByKey <decimal>(string.Format("Tax.TaxProvider.FixedOrByCountryStateZip.TaxCategoryId{0}", calculateTaxRequest.TaxCategoryId))
                };
            }
            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    = 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()
                                                             );

                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);
        }