Пример #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);
 }
        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);
        }