Esempio n. 1
0
        public ActionResult RatesByCountryStateZipList(DataSourceRequest command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageTaxSettings))
            {
                return(ErrorForKendoGridJson("Access denied"));
            }

            var records       = _taxRateService.GetAllTaxRates(command.Page - 1, command.PageSize);
            var taxRatesModel = records
                                .Select(x =>
            {
                var m = new WB_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));
        }
        //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);
            }
        }
Esempio n. 3
0
        /// <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);
        }