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