Esempio n. 1
0
 public IHttpActionResult Total(CustomerOrder order)
 {
     if (!string.IsNullOrEmpty(_taxSettings.Username) && !string.IsNullOrEmpty(_taxSettings.Password)
         && !string.IsNullOrEmpty(_taxSettings.ServiceUrl)
         && !string.IsNullOrEmpty(_taxSettings.CompanyCode) && _taxSettings.IsEnabled)
     {
         var taxSvc = new JsonTaxSvc(_taxSettings.Username, _taxSettings.Password, _taxSettings.ServiceUrl);
         var request = order.ToAvaTaxRequest(_taxSettings.CompanyCode);
         var getTaxResult = taxSvc.GetTax(request);
         if (!getTaxResult.ResultCode.Equals(SeverityLevel.Success))
         {
             var error = string.Join(Environment.NewLine, getTaxResult.Messages.Select(m => m.Details));
             return BadRequest(error);
         }
         else
         {
             foreach (TaxLine taxLine in getTaxResult.TaxLines ?? Enumerable.Empty<TaxLine>())
             {
                 order.Items.ToArray()[Int32.Parse(taxLine.LineNo)].Tax = taxLine.Tax;
                 //foreach (TaxDetail taxDetail in taxLine.TaxDetails ?? Enumerable.Empty<TaxDetail>())
                 //{
                 //}
             }
             order.Tax = getTaxResult.TotalTax;
         }
     }
     else
     {
         return BadRequest();
     }
     return Ok(order);
 }
Esempio n. 2
0
        public IHttpActionResult TestConnection()
        {
            var absUri = new Uri(Request.RequestUri, RequestContext.VirtualPathRoot);

            if (!string.IsNullOrEmpty(_taxSettings.Username) && !string.IsNullOrEmpty(_taxSettings.Password)
                && !string.IsNullOrEmpty(_taxSettings.ServiceUrl)
                && !string.IsNullOrEmpty(_taxSettings.CompanyCode))
            {
                if (!_taxSettings.IsEnabled)
                    return BadRequest("Tax calculation disabled, enable before testing connection");

                var taxSvc = new JsonTaxSvc(_taxSettings.Username, _taxSettings.Password, _taxSettings.ServiceUrl);
                var retVal = taxSvc.Ping();
                if (retVal.ResultCode.Equals(SeverityLevel.Success))
                    return Ok(new[] {retVal});

                return BadRequest(string.Join(", ", retVal.Messages.Select(m => m.Summary)));
            }
            
            return BadRequest("AvaTax credentials not provided");
        }
Esempio n. 3
0
        public IHttpActionResult TestConnection()
        {
            IHttpActionResult retVal = BadRequest();
            LogInvoker<AvalaraLogger.TaxRequestContext>.Execute(log =>
            {
                if (!string.IsNullOrEmpty(_taxSettings.Username) && !string.IsNullOrEmpty(_taxSettings.Password)
                    && !string.IsNullOrEmpty(_taxSettings.ServiceUrl)
                    && !string.IsNullOrEmpty(_taxSettings.CompanyCode))
                {
                    if (!_taxSettings.IsEnabled)
                    {
                        retVal = BadRequest("Tax calculation disabled, enable before testing connection");
                        throw new Exception((retVal as BadRequestErrorMessageResult).Message);
                    }

                    var taxSvc = new JsonTaxSvc(_taxSettings.Username, _taxSettings.Password, _taxSettings.ServiceUrl);
                    var result = taxSvc.Ping();
                    if (!result.ResultCode.Equals(SeverityLevel.Success))
                    {
                        retVal =
                            BadRequest(string.Join(Environment.NewLine,
                                result.Messages.Where(ms => ms.Severity == SeverityLevel.Error).Select(
                            m => m.Summary + string.Format(" [{0} - {1}] ", m.RefersTo, m.Details == null ? string.Empty : string.Join(", ", m.Details)))));
                        throw new Exception((retVal as BadRequestErrorMessageResult).Message);
                    }

                    retVal = Ok(result);
                }
                else
                {
                    retVal = BadRequest("AvaTax credentials not provided");
                    throw new Exception((retVal as BadRequestErrorMessageResult).Message);
                }
            })
                .OnError(_logger, AvalaraLogger.EventCodes.TaxPingError)
                .OnSuccess(_logger, AvalaraLogger.EventCodes.Ping);

            return retVal;
        }
Esempio n. 4
0
        public IHttpActionResult CancelTax(CustomerOrder order)
        {
            if (!string.IsNullOrEmpty(_taxSettings.Username) && !string.IsNullOrEmpty(_taxSettings.Password)
                && !string.IsNullOrEmpty(_taxSettings.ServiceUrl)
                && !string.IsNullOrEmpty(_taxSettings.CompanyCode) && _taxSettings.IsEnabled)
            {
                var taxSvc = new JsonTaxSvc(_taxSettings.Username, _taxSettings.Password, _taxSettings.ServiceUrl);
                var request = order.ToAvaTaxCancelRequest(_taxSettings.CompanyCode, CancelCode.DocVoided);
                var cancelTaxResult = taxSvc.CancelTax(request);
                if (!cancelTaxResult.ResultCode.Equals(SeverityLevel.Success))
                {
                    var error = string.Join(Environment.NewLine, cancelTaxResult.Messages.Select(m => m.Details));
                    return BadRequest(error);
                }

                return Ok(cancelTaxResult);
            }
            
            return BadRequest();
        }
        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;
        }        
        public virtual void CancelTaxDocument(CustomerOrder order)
        {
            LogInvoker<AvalaraLogger.TaxRequestContext>.Execute(log =>
            {
                if (IsEnabled && !string.IsNullOrEmpty(AccountNumber) && !string.IsNullOrEmpty(LicenseKey)
                    && !string.IsNullOrEmpty(ServiceUrl)
                    && !string.IsNullOrEmpty(CompanyCode))
                {                    
                    var request = order.ToAvaTaxCancelRequest(CompanyCode, CancelCode.DocDeleted);
                    if (request != null)
                    {
                        log.docCode = request.DocCode;
                        log.docType = request.DocType.ToString();

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

                        if (!getTaxResult.ResultCode.Equals(SeverityLevel.Success))
                        {
                            var error = string.Join(Environment.NewLine, getTaxResult.Messages.Select(m => m.Summary));
                            throw new Exception(error);
                        }
                    }
                }
                else
                {
                    throw new Exception("AvaTax credentials not provided or tax calculation disabled");
                }
            })
            .OnError(_logger, AvalaraLogger.EventCodes.TaxCalculationError)
            .OnSuccess(_logger, AvalaraLogger.EventCodes.GetTaxRequestTime);
        }
        public virtual void AdjustOrderTax(CustomerOrder originalOrder, CustomerOrder modifiedOrder)
        {
            LogInvoker<AvalaraLogger.TaxRequestContext>.Execute(log =>
            {
                if (IsEnabled && !string.IsNullOrEmpty(AccountNumber)
                    && !string.IsNullOrEmpty(LicenseKey)
                    && !string.IsNullOrEmpty(ServiceUrl)
                    && !string.IsNullOrEmpty(CompanyCode))
                {
                    //if all payments completed commit tax document in avalara
                    var isCommit = modifiedOrder.InPayments != null && modifiedOrder.InPayments.Any()
                        && modifiedOrder.InPayments.All(pi => pi.IsApproved);

                    Contact contact = null;
                    if (modifiedOrder.CustomerId != null)
                        contact = _customerSearchService.GetById(modifiedOrder.CustomerId);

                    var request = modifiedOrder.ToAvaTaxAdjustmentRequest(CompanyCode, contact, originalOrder, isCommit);
                    if (request != null)
                    {
                        log.docCode = request.ReferenceCode;
                        log.docType = request.DocType.ToString();
                        log.customerCode = request.CustomerCode;
                        log.amount = (double)originalOrder.Sum;

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

                        if (!getTaxResult.ResultCode.Equals(SeverityLevel.Success))
                        {
                            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 lineItem = modifiedOrder.Items.FirstOrDefault(x => x.Id == taxLine.LineNo);
                            if (lineItem != null)
                            {
                                lineItem.Tax += taxLine.Tax;
                                if (taxLine.TaxDetails != null && taxLine.TaxDetails.Any(td => !string.IsNullOrEmpty(td.TaxName)))
                                {

                                    var taxLines =
                                        taxLine.TaxDetails.Where(td => !string.IsNullOrEmpty(td.TaxName)).Select(taxDetail => taxDetail.ToDomainTaxDetail()).ToList();

                                    lineItem.TaxDetails = lineItem.TaxDetails == null ? taxLines : lineItem.TaxDetails.AddRange(taxLines);
                                }
                            }
                        }

                        modifiedOrder.Tax = 0;
                    }
                }
                else
                {
                    throw new Exception("AvaTax credentials not provided or tax calculation disabled");
                }
            })
            .OnError(_logger, AvalaraLogger.EventCodes.TaxCalculationError)
            .OnSuccess(_logger, AvalaraLogger.EventCodes.GetTaxRequestTime);
        }
        public virtual void CalculateOrderTax(CustomerOrder order)
        {
            LogInvoker<AvalaraLogger.TaxRequestContext>.Execute(log =>
            {
                if (IsEnabled && !string.IsNullOrEmpty(AccountNumber)
                    && !string.IsNullOrEmpty(LicenseKey)
                    && !string.IsNullOrEmpty(ServiceUrl)
                    && !string.IsNullOrEmpty(CompanyCode))
                {
                    //if all payments completed commit tax document in avalara
                    var isCommit = order.InPayments != null && order.InPayments.Any()
                        && order.InPayments.All(pi => pi.IsApproved);

                    Contact contact = null;
                    if (order.CustomerId != null)
                        contact = _customerSearchService.GetById(order.CustomerId);

                    var request = order.ToAvaTaxRequest(CompanyCode, contact, isCommit);
                    if (request != null)
                    {
                        log.docCode = request.DocCode;
                        log.docType = request.DocType.ToString();
                        log.customerCode = request.CustomerCode;
                        log.amount = (double)order.Sum;
                        log.isCommit = isCommit;

                        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);
                        }
                        
                        UpdateOrderTaxes(getTaxResult.TaxLines, order);

                        order.Tax = getTaxResult.TotalTax;
                    }
                    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);
        }
        public virtual void CalculateCartTax(ShoppingCart cart)
        {
            LogInvoker<AvalaraLogger.TaxRequestContext>.Execute(log =>
            {
                if (IsEnabled && !string.IsNullOrEmpty(AccountNumber) && !string.IsNullOrEmpty(LicenseKey)
                    && !string.IsNullOrEmpty(ServiceUrl)
                    && !string.IsNullOrEmpty(CompanyCode))
                {
                    Contact contact = null;
                    if (cart.CustomerId != null)
                        contact = _customerSearchService.GetById(cart.CustomerId);

                    var request = cart.ToAvaTaxRequest(CompanyCode, contact);
                    if (request != null)
                    {
                        log.docCode = request.DocCode;
                        log.customerCode = request.CustomerCode;
                        log.docType = request.DocType.ToString();
                        log.amount = (double)cart.Total;

                        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);
                        }

                        UpdateCartTaxes(getTaxResult.TaxLines, cart);
                    }
                    else
                    {
                        throw new Exception("Failed to create get tax request");
                    }
                }
                else
                {
                    throw new Exception("Tax calculation disabled or credentials not provided");
                }
            })
            .OnError(_logger, AvalaraLogger.EventCodes.TaxCalculationError)
            .OnSuccess(_logger, AvalaraLogger.EventCodes.GetTaxRequestTime);
        }