Exemplo n.º 1
0
        /// <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)
        {
            if (taxRateRequest.Address == null)
            {
                return new TaxRateResult {
                           Errors = new List <string> {
                               "Address is not set"
                           }
                }
            }
            ;

            //get tax rate
            var taxRate = await _avalaraTaxManager.GetTaxRateAsync(taxRateRequest);

            if (!taxRate.HasValue)
            {
                return new TaxRateResult {
                           Errors = new List <string> {
                               "No response from the service"
                           }
                }
            }
            ;

            return(new TaxRateResult {
                TaxRate = taxRate.Value
            });
        }
Exemplo n.º 2
0
        public async Task <TaxRateResult> GetTaxRateAsync(TaxRateRequest taxRateRequest)
        {
            var result = new TaxRateResult();

            //the tax rate calculation by country & state & zip
            if (taxRateRequest.Address == null)
            {
                result.Errors.Add("Address is not set");
                return(result);
            }

            var foundRecord = await _abcTaxService.GetAbcTaxRateAsync(
                taxRateRequest.CurrentStoreId,
                taxRateRequest.TaxCategoryId,
                taxRateRequest.Address
                );

            if (foundRecord == null)
            {
                return(result);
            }

            // get TaxJar rate if appropriate
            result.TaxRate = foundRecord.IsTaxJarEnabled ?
                             await _taxjarRateService.GetTaxJarRateAsync(taxRateRequest.Address) :
                             foundRecord.Percentage;

            return(result);
        }
        /// <summary>
        /// Gets tax rate
        /// </summary>
        /// <param name="taxRateRequest">Tax rate request</param>
        /// <returns>Tax</returns>
        public TaxRateResult GetTaxRate(TaxRateRequest taxRateRequest)
        {
            var result = new TaxRateResult();

            //the tax rate calculation by fixed rate
            if (!_countryStateZipSettings.CountryStateZipEnabled)
            {
                result.TaxRate = _settingService.GetSettingByKey <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    = _cacheKeyService.PrepareKeyForDefaultCache(ModelCacheEventConsumer.ALL_TAX_RATES_MODEL_KEY);
            var allTaxRates = _staticCacheManager.Get(cacheKey, () => _taxRateService.GetAllTaxRates().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);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Validate Request and Consumer
 /// </summary>
 /// <param name="zip"></param>
 public void ValidateTaxRateRequest(TaxRateRequest request)
 {
     if (string.IsNullOrEmpty(request.Zip))
     {
         throw new BadInputException("Zip.NullOrEmpty");
     }
 }
Exemplo n.º 5
0
        public async Task <TaxRateModel> GetTaxRates(TaxRateRequest request)
        {
            TaxJarRateResponse response = null;

            TaxRateModel mapped = mapper.Map <TaxRateModel>(request);

            if (request == null)
            {
                throw new ArgumentException(nameof(request), "Request cannot be null. Zip code is required.");
            }

            if (string.IsNullOrEmpty(request.Zip))
            {
                throw new ArgumentException(nameof(request.Zip), "Zip Code is required.");
            }

            if (!string.IsNullOrEmpty(request.City) || !string.IsNullOrEmpty(request.State) || !string.IsNullOrEmpty(request.Street) || !string.IsNullOrEmpty(request.Country))
            {
                response = await taxRepository.Get <TaxRateModel, TaxJarRateResponse>(mapped);
            }
            else
            {
                response = await taxRepository.Get <TaxJarRateResponse>(request.Zip);
            }

            return(mapper.Map <TaxRateModel>(response));
        }
        /// <summary>
        /// Method to Find Tax Rate
        /// </summary>
        /// <returns></returns>
        public async Task <TaxRateResponse> GetTaxRate(TaxRateRequest taxRateRequest)
        {
            //validate request
            _taxServiceValidator.ValidateTaxRateRequest(taxRateRequest);

            //Get Consumer Details
            var consumerDetail = _consumerHelper.GetConsumer();

            //Create api request
            var TaxRateResponseRequest = new TaxRateApiRequest()
            {
                Zip             = taxRateRequest.Zip,
                City            = taxRateRequest.City,
                Country         = taxRateRequest.Country,
                State           = taxRateRequest.State,
                Street          = taxRateRequest.Street,
                TaxApiAuthToken = consumerDetail.TaxApiAuthToken,
                TaxApiUrl       = consumerDetail.TaxApiUrl
            };

            //Call repository with valid request
            var response = await _taxRepository.GetTaxRate(TaxRateResponseRequest);

            return(response);
        }
Exemplo n.º 7
0
        public TaxRateResponse Get([FromQuery] TaxRateRequest request)
        {
            var rate = _taxService.GetTaxRate(request.Municipality, request.Day);

            return(new TaxRateResponse {
                Rate = rate
            });
        }
 public IEnumerable <TaxRateResponse> Execute(TaxRateRequest request)
 {
     return(Enum.GetValues(typeof(TaxRate))
            .Cast <TaxRate>()
            .Select(x => new TaxRateResponse()
     {
         Name = x.ToString(), Id = (int)x
     }));
 }
Exemplo n.º 9
0
        public void ReadMunicipalTaxRatesAtGivenDay_UnitTests_EmptyResults(string guid, string date, bool empty)
        {
            // Arrange
            var municipalityId = Guid.Parse(guid);
            var request        = new TaxRateRequest
            {
                Date = DateTime.Parse(date),
            };

            // Act
            var expected = empty;
            var result   = !this.sService.ReadMunicipalTaxRatesAtGivenDay(municipalityId, request).Any();

            // Assert
            Assert.Equal(expected, result);
        }
Exemplo n.º 10
0
        public void ReadMunicipalTaxRatesAtGivenDay_UnitTests_ExpectedBehavior1(string date, decimal tax)
        {
            // Arrange
            var municipalityId = Guid.Parse("7ebced2b-e2f9-45e0-bf75-111111111100");
            var request        = new TaxRateRequest
            {
                Date = DateTime.Parse(date),
            };

            // Act
            var expected = tax;
            var result   = this.sService.ReadMunicipalTaxRatesAtGivenDay(municipalityId, request).FirstOrDefault().Tax;

            // Assert
            Assert.Equal(expected, result);
        }
Exemplo n.º 11
0
        public async Task <IActionResult> Get([FromBody] TaxRateRequest request)
        {
            try
            {
                if (string.IsNullOrEmpty(request.Zip))
                {
                    return(BadRequest("Zip is required"));
                }

                if (string.IsNullOrEmpty(request.Country))
                {
                    return(BadRequest("Country is required"));
                }

                TaxRateModel taxRate = await taxService.GetTaxRates(request);

                return(Ok(JsonConvert.SerializeObject(mapper.Map <TaxRateResponse>(taxRate))));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
 /// <summary>
 /// Gets tax rate
 /// </summary>
 /// <param name="taxRateRequest">Tax rate request</param>
 /// <returns>Tax</returns>
 public Task <TaxRateResult> GetTaxRateAsync(TaxRateRequest taxRateRequest)
 {
     return(Task.FromResult(new TaxRateResult {
         TaxRate = 10
     }));
 }
Exemplo n.º 13
0
 /// <summary>
 /// Gets tax rate
 /// </summary>
 /// <param name="taxRateRequest">Tax rate request</param>
 /// <returns>Tax</returns>
 public TaxRateResult GetTaxRate(TaxRateRequest taxRateRequest)
 {
     return(new TaxRateResult {
         TaxRate = 10
     });
 }
        public async Task <IActionResult> GetTaxRate([FromQuery] TaxRateRequest taxRateRequest)
        {
            var getTaxRateResponse = await _taxService.GetTaxRate(taxRateRequest);

            return(Ok(getTaxRateResponse));
        }
Exemplo n.º 15
0
        public ActionResult <IEnumerable <TaxRateDto> > GetMunicipalTaxRatesAtGivenDay([FromQueryAttribute] TaxRateRequest request)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest());
            }

            var municipality = this.mService.ReadAll().FirstOrDefault(m => m.MunicipalityName == request.MunicipalityName);

            if (municipality == null)
            {
                return(this.NotFound("Municipality doesn't exists"));
            }

            var municipalityId = municipality.Id;

            var taxRatesDto = this.taxRatesService.ReadMunicipalTaxRatesAtGivenDay(municipalityId, request);

            if (taxRatesDto == Enumerable.Empty <TaxRateDto>())
            {
                return(this.NotFound("Tax rate doesn't exists"));
            }

            return(this.Ok(taxRatesDto));
        }
        public IEnumerable <TaxRateDto> ReadMunicipalTaxRatesAtGivenDay(Guid municipalityId, TaxRateRequest request)
        {
            var requestDate = request.Date;

            var allPossibleDates = this.repo.ReadAll().Where(x => x.MunicipalityId == municipalityId &&
                                                             x.TaxStartDate <= requestDate && x.TaxEndDate >= requestDate)
                                   .OrderBy(x => x, new ScheduleTypeComparer())
                                   .ToList();

            if (!allPossibleDates.Any())
            {
                return(Enumerable.Empty <TaxRateDto>());
            }

            // In case if different taxes will be inserted for the same date.
            var firstScheduleType = allPossibleDates.First().ScheduleType;

            return(this.mapper.Map <IEnumerable <TaxRateDto> >(allPossibleDates.Where(x => x.ScheduleType == firstScheduleType)));
        }