public override IEnumerable<TaxRate> CalculateRates(Domain.Common.IEvaluationContext context)
        {
            var taxEvalContext = context as TaxEvaluationContext;
            if (taxEvalContext == null)
            {
                throw new NullReferenceException("taxEvalContext");
            }

            var retVal = new List<TaxRate>();
            if (taxEvalContext.TaxRequest != null)
            {
                foreach (var line in taxEvalContext.TaxRequest.Lines)
                {
                    var rate = new TaxRate
                    {
                        Rate = line.Amount * Rate * 0.01m,
                        Currency = taxEvalContext.TaxRequest.Currency,
                        TaxProvider = this,
                        Line = line
                    };
                    retVal.Add(rate);
                }
            }
            return retVal;
        }
Exemplo n.º 2
0
        public void Update(TaxRate taxRate)
        {
            taxRate = Validate(taxRate);

            _taxRates.Attach(taxRate);
            _repository.Entry(taxRate).State = EntityState.Modified;
            _repository.SaveChanges();
        }
Exemplo n.º 3
0
        private TaxRate Validate(TaxRate taxRate)
        {
            if (!string.IsNullOrWhiteSpace(taxRate.Description))
                taxRate.Description = taxRate.Description.Trim();
            else
                throw new BusinessLogicException("A name or description is required for all tax rates.");

            return taxRate;
        }
Exemplo n.º 4
0
        public void Insert(TaxRate taxRate)
        {
            taxRate = Validate(taxRate);

            if (_taxRates.Any(i => i.Description.Equals(taxRate.Description, StringComparison.InvariantCultureIgnoreCase)))
                throw new BusinessLogicException("A tax rate with that name already exists.");

            _taxRates.Add(taxRate);
            _repository.SaveChanges();
        }
        private List<TaxRate> GetTaxRates(TaxEvaluationContext evalContext)
        {
            List<TaxRate> retVal = new List<TaxRate>();
            LogInvoker<AvalaraLogger.TaxRequestContext>.Execute(log =>
            {
                if (IsEnabled && !string.IsNullOrEmpty(AccountNumber)
                    && !string.IsNullOrEmpty(LicenseKey)
                    && !string.IsNullOrEmpty(ServiceUrl)
                    && !string.IsNullOrEmpty(CompanyCode))
                {                    
                    var request = evalContext.ToAvaTaxRequest(CompanyCode, false);
                    if (request != null)
                    {
                        log.docCode = request.DocCode;
                        log.docType = request.DocType.ToString();
                        log.customerCode = request.CustomerCode;                        

                        var taxSvc = new JsonTaxSvc(AccountNumber, LicenseKey, ServiceUrl);
                        var getTaxResult = taxSvc.GetTax(request);

                        if (!getTaxResult.ResultCode.Equals(SeverityLevel.Success))
                        {
                            //if tax calculation failed create exception with provided error info
                            var error = string.Join(Environment.NewLine, getTaxResult.Messages.Select(m => m.Summary));
                            throw new Exception(error);
                        }
                        
                        foreach (var taxLine in getTaxResult.TaxLines ?? Enumerable.Empty<AvaTaxCalcREST.TaxLine>())
                        {                            
                                var rate = new TaxRate
                                {
                                    Rate = taxLine.Tax,
                                    Currency = evalContext.Currency,
                                    TaxProvider = this,
                                    Line = evalContext.Lines.First(l => l.Id == taxLine.LineNo)
                                };
                                retVal.Add(rate);                            
                        }
                    }
                    else
                    {
                        throw new Exception("Failed to create get tax request");
                    }
                }
                else
                {
                    throw new Exception("Failed to create get tax request");
                }
            })
            .OnError(_logger, AvalaraLogger.EventCodes.TaxCalculationError)
            .OnSuccess(_logger, AvalaraLogger.EventCodes.GetSalesInvoiceRequestTime);

            return retVal;
        }        
Exemplo n.º 6
0
 public void Delete(TaxRate taxRate)
 {
     _taxRates.Attach(taxRate);
     _repository.Entry(taxRate).State = EntityState.Deleted;
     _repository.SaveChanges();
 }
Exemplo n.º 7
0
        public bool ChangeTndn(int tndnRate, DateTime beginDate)
        {
            TaxRate currentTndnTaxRate =
                db.TaxRates.FirstOrDefault(
                    m => m.TaxTypeId == 2 && DateTime.Now >= m.BeginDate && DateTime.Now <= m.EndDate);
            if (currentTndnTaxRate == null || currentTndnTaxRate.TaxRateValue == tndnRate)
            {
                return false;
            }

            currentTndnTaxRate.EndDate = beginDate.AddDays(-1);
            TaxRate taxRate = new TaxRate();
            taxRate.TaxTypeId = 2;
            taxRate.TaxRateValue = tndnRate;
            taxRate.BeginDate = beginDate;
            taxRate.EndDate = new DateTime(9999, 12, 31);

            db.TaxRates.Add(taxRate);

            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }
Exemplo n.º 8
0
        /// <summary>
        /// maps <paramref name="rate"/> from enum to 0..1
        /// </summary>
        /// <param name="rate"></param>
        /// <returns></returns>
        public static double GetTaxValue(TaxRate rate)
        {
            double num = Enum.GetValues(typeof(TaxRate)).Length;
            int index = 0;
            foreach (TaxRate r in Enum.GetValues(typeof(TaxRate)))
            {
                if (r == rate)
                    return index / num;

                index++;
            }

            return 0;
        }
 public void Initialization()
 {
     _taxRate = new TaxRate(0.23m);
     _product = new global::Domain.Assortment.Product(new ProductId(Guid.NewGuid()), "P1", "Product 1", _taxRate);
 }
 public void Initialization()
 {
     _taxRate = new TaxRate(0.23m);
 }