Esempio n. 1
0
        public static TaxRate ToTaxRate(this taxDto.TaxRate taxRateDto, Currency currency)
        {
            var result = new TaxRate(currency)
            {
                Rate        = new Money(taxRateDto.Rate.Value, currency),
                PercentRate = (decimal)(taxRateDto.PercentRate ?? 0)
            };

            if (taxRateDto.Line != null)
            {
                result.Line = new TaxLine(currency)
                {
                    Code     = taxRateDto.Line.Code,
                    Id       = taxRateDto.Line.Id,
                    Name     = taxRateDto.Line.Name,
                    Quantity = taxRateDto.Line.Quantity ?? 1,
                    TaxType  = taxRateDto.Line.TaxType,
                    TypeName = taxRateDto.Line.TypeName,

                    Amount = new Money(taxRateDto.Line.Amount.Value, currency),
                    Price  = new Money(taxRateDto.Line.Price.Value, currency)
                };
                if (taxRateDto.TaxDetails != null)
                {
                    result.Line.TaxDetails = taxRateDto.TaxDetails.Select(x => x.ToTaxDetail(currency)).ToList();
                }
            }

            return(result);
        }
Esempio n. 2
0
        public virtual async Task EvaluateTaxesAsync(TaxEvaluationContext context, IEnumerable <ITaxable> owners)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (owners == null)
            {
                throw new ArgumentNullException(nameof(owners));
            }
            IList <taxDto.TaxRate> taxRates = new List <taxDto.TaxRate>();

            if (context.StoreTaxCalculationEnabled)
            {
                //Do not execute platform API for tax evaluation if fixed tax rate is used
                if (context.FixedTaxRate != 0)
                {
                    foreach (var line in context.Lines ?? Enumerable.Empty <TaxLine>())
                    {
                        var rate = new taxDto.TaxRate()
                        {
                            Rate     = (double)(line.Amount * context.FixedTaxRate * 0.01m).Amount,
                            Currency = context.Currency.Code,
                            Line     = line.ToTaxLineDto()
                        };
                        taxRates.Add(rate);
                    }
                }
                else
                {
                    var cacheKey = CacheKey.With(GetType(), context.GetCacheKey());
                    taxRates = await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, (cacheEntry) =>
                    {
                        cacheEntry.AddExpirationToken(TaxCacheRegion.CreateChangeToken());
                        return(_taxApi.EvaluateTaxesAsync(context.StoreId, context.ToTaxEvaluationContextDto()));
                    });
                }
            }
            ApplyTaxRates(taxRates, owners);
        }