Beispiel #1
0
        public async Task <OperationResult <TaxCalculationResponse> > CalculateTax(TaxCalculationRequest request)
        {
            OperationResult <TaxCalculationResponse> validatorResult = _validationRuleEngine.Validate(request);

            if (validatorResult.HasErrors)
            {
                return(validatorResult);
            }

            var calculationTypeMapping = await _calculationMappingRepository.GetByPostalCodeAsync(request.PostalCode).ConfigureAwait(false);

            var taxCalculator = _taxCalculatorFactory.GetCalculator(calculationTypeMapping.CalculationType);
            var taxYear       = await _taxYearRepository.GetTaxYearAsync(_clock.GetCurrentDateTime()).ConfigureAwait(false);

            var taxAmountResult = await taxCalculator.CalculateTaxAsync(taxYear, request.AnnualIncome).ConfigureAwait(false);

            if (taxAmountResult.HasErrors)
            {
                return(taxAmountResult.MapErrors <TaxCalculationResponse>());
            }

            var response = new TaxCalculationResponse
            {
                TaxYear         = taxYear.ToString(),
                CalculationType = calculationTypeMapping.CalculationType,
                TaxAmount       = taxAmountResult.Response
            };

            await SaveCalculation(request, response, taxYear);

            return(new OperationResult <TaxCalculationResponse>(response));
        }
Beispiel #2
0
        public double CalculateTotal(Order o)
        {
            //use a factory or ServiceLocator...
            var taxCalc   = _taxCalculatorFactory.GetCalculator(o);
            var taxAmount = taxCalc.Calculate(o);

            // other logic here as needed...

            return(o.Items.Sum(i => i.Price) + taxAmount);
        }