public virtual void CalculateCartTax(ShoppingCart cart) { LogInvoker <AvalaraLogger.TaxRequestContext> .Execute(log => { Validate(); Member member = null; if (cart.CustomerId != null) { member = _memberService.GetByIds(new[] { cart.CustomerId }).FirstOrDefault(); } var request = cart.ToAvaTaxRequest(CompanyCode, member); 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); //register in avalara SalesOrder transaction 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); } } }) .OnError(_logger, AvalaraLogger.EventCodes.TaxCalculationError) .OnSuccess(_logger, AvalaraLogger.EventCodes.GetTaxRequestTime); }
private void CalculateCustomerOrderTaxes(CartChangeEvent context) { var cart = context.ModifiedCart; if (cart.Items.Any()) { cart.Items.ForEach(x => { x.TaxTotal = 0; x.TaxDetails = null; }); } if (_taxSettings.IsEnabled && !string.IsNullOrEmpty(_taxSettings.Username) && !string.IsNullOrEmpty(_taxSettings.Password) && !string.IsNullOrEmpty(_taxSettings.ServiceUrl) && !string.IsNullOrEmpty(_taxSettings.CompanyCode)) { var taxSvc = new JsonTaxSvc(_taxSettings.Username, _taxSettings.Password, _taxSettings.ServiceUrl); var request = cart.ToAvaTaxRequest(_taxSettings.CompanyCode); if (request != null) { //var store = _storeService.GetById(cart.StoreId); //if (store != null) //{ // request.Addresses.AddDistinct(new Address { AddressCode = "origin", Country = store.Country, Region = store.Region }); // request.Lines.ForEach(l => l.OriginCode = "origin"); //} var getTaxResult = taxSvc.GetTax(request); if (!getTaxResult.ResultCode.Equals(SeverityLevel.Success)) { var error = string.Join(Environment.NewLine, getTaxResult.Messages.Select(m => m.Summary)); OnError(new Exception(error)); } else { foreach (TaxLine taxLine in getTaxResult.TaxLines ?? Enumerable.Empty <TaxLine>()) { cart.Items.ToArray()[Int32.Parse(taxLine.LineNo)].TaxTotal = taxLine.Tax; //foreach (TaxDetail taxDetail in taxLine.TaxDetails ?? Enumerable.Empty<TaxDetail>()) //{ // cart.Items.ToArray()[Int32.Parse(taxLine.LineNo)].TaxDetails = new[] // { // new VirtoCommerce.Domain.Commerce.Model.TaxDetail // { // Amount = taxDetail.Tax, // Name = taxDetail.TaxName, // Rate = taxDetail.Rate // } // }; //} } } } } else { OnError(new Exception("Tax calculation disabled or credentials not provided")); } }
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 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); }
public virtual void AdjustOrderTax(CustomerOrder originalOrder, CustomerOrder modifiedOrder) { LogInvoker <AvalaraLogger.TaxRequestContext> .Execute(log => { Validate(); //if all payments completed commit tax document in avalara var isCommit = modifiedOrder.InPayments != null && modifiedOrder.InPayments.Any() && modifiedOrder.InPayments.All(pi => pi.IsApproved); Member member = null; if (modifiedOrder.CustomerId != null) { member = _memberService.GetByIds(new[] { modifiedOrder.CustomerId }).FirstOrDefault(); } var request = modifiedOrder.ToAvaTaxAdjustmentRequest(CompanyCode, member, 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); } } }) .OnError(_logger, AvalaraLogger.EventCodes.TaxCalculationError) .OnSuccess(_logger, AvalaraLogger.EventCodes.GetTaxRequestTime); }
public virtual void CalculateOrderTax(CustomerOrder order) { LogInvoker <AvalaraLogger.TaxRequestContext> .Execute(log => { Validate(); Member member = null; if (order.CustomerId != null) { member = _memberService.GetByIds(new[] { order.CustomerId }).FirstOrDefault(); } //if all payments completed commit tax document in avalara var isCommit = order.InPayments != null && order.InPayments.Any() && order.InPayments.All(pi => pi.IsApproved); //update transaction in avalara var request = order.ToAvaTaxRequest(CompanyCode, member, 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); //register in avalara SalesInvoice transaction 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); } } }) .OnError(_logger, AvalaraLogger.EventCodes.TaxCalculationError) .OnSuccess(_logger, AvalaraLogger.EventCodes.GetSalesInvoiceRequestTime); }
private void CalculateCustomerOrderTaxes(OrderChangeEvent context) { if (context.ModifiedOrder.IsCancelled) { return; } var order = context.ModifiedOrder; var originalOrder = context.OrigOrder; //do nothing if order Items quantities did not changed if ( originalOrder.Items.Any( li => !order.Items.Any(oli => oli.Id.Equals(li.Id)) || order.Items.Single(oli => li.Id.Equals(oli.Id)).Quantity < li.Quantity)) { return; } SlabInvoker <VirtoCommerceEventSource.TaxRequestContext> .Execute(slab => { if (_taxSettings.IsEnabled && !string.IsNullOrEmpty(_taxSettings.Username) && !string.IsNullOrEmpty(_taxSettings.Password) && !string.IsNullOrEmpty(_taxSettings.ServiceUrl) && !string.IsNullOrEmpty(_taxSettings.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(_taxSettings.CompanyCode, contact, isCommit); if (request != null) { slab.docCode = request.DocCode; slab.docType = request.DocType.ToString(); slab.customerCode = request.CustomerCode; slab.amount = (double)order.Sum; slab.isCommit = isCommit; var taxSvc = new JsonTaxSvc(_taxSettings.Username, _taxSettings.Password, _taxSettings.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); } //reset items taxes if (order.Items.Any()) { order.Items.ForEach(x => { x.Tax = 0; x.TaxDetails = null; }); } //reset order shipments taxes if (order.Shipments.Any()) { order.Shipments.ForEach(x => { x.Tax = 0; x.TaxDetails = null; }); } order.Tax = 0; foreach (var taxLine in getTaxResult.TaxLines ?? Enumerable.Empty <TaxLine>()) { var lineItem = order.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))) { lineItem.TaxDetails = taxLine.TaxDetails.Where(td => !string.IsNullOrEmpty(td.TaxName)).Select(taxDetail => new domainModel.TaxDetail { Amount = taxDetail.Tax, Name = taxDetail.TaxName, Rate = taxDetail.Rate }).ToList(); } } else { var shipment = order.Shipments.FirstOrDefault(s => s.Id != null ? s.Id.Equals(taxLine.LineNo) : s.ShipmentMethodCode.Equals(taxLine.LineNo)); if (shipment != null) { shipment.Tax = taxLine.Tax; if (taxLine.TaxDetails != null && taxLine.TaxDetails.Any(td => !string.IsNullOrEmpty(td.TaxName))) { shipment.TaxDetails = taxLine.TaxDetails.Where(td => !string.IsNullOrEmpty(td.TaxName)).Select(taxDetail => new domainModel.TaxDetail { Amount = taxDetail.Tax, Name = taxDetail.TaxName, Rate = taxDetail.Rate }).ToList(); } } } 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(VirtoCommerceEventSource.Log, VirtoCommerceEventSource.EventCodes.TaxCalculationError) .OnSuccess(VirtoCommerceEventSource.Log, VirtoCommerceEventSource.EventCodes.GetSalesInvoiceRequestTime); }
private void CalculatePartialReturnTaxes(OrderChangeEvent context) { //do nothing if order cancelled (should work another observer) if (context.ModifiedOrder.IsCancelled) { return; } var originalOrder = context.OrigOrder; var modifiedOrder = context.ModifiedOrder; //do nothing if order Items quantities did not changed if ( originalOrder.Items.All( li => modifiedOrder.Items.Any(oli => oli.Id.Equals(li.Id)) && modifiedOrder.Items.Single(oli => li.Id.Equals(oli.Id)).Quantity.Equals(li.Quantity))) { return; } //otherwise make partial return/add request SlabInvoker <VirtoCommerceEventSource.TaxRequestContext> .Execute(slab => { if (_taxSettings.IsEnabled && !string.IsNullOrEmpty(_taxSettings.Username) && !string.IsNullOrEmpty(_taxSettings.Password) && !string.IsNullOrEmpty(_taxSettings.ServiceUrl) && !string.IsNullOrEmpty(_taxSettings.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(_taxSettings.CompanyCode, contact, originalOrder, isCommit); if (request != null) { slab.docCode = request.ReferenceCode; slab.docType = request.DocType.ToString(); slab.customerCode = request.CustomerCode; slab.amount = (double)originalOrder.Sum; var taxSvc = new JsonTaxSvc(_taxSettings.Username, _taxSettings.Password, _taxSettings.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 <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 => new domainModel.TaxDetail { Amount = taxDetail.Tax, Name = taxDetail.TaxName, Rate = taxDetail.Rate }).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(VirtoCommerceEventSource.Log, VirtoCommerceEventSource.EventCodes.TaxCalculationError) .OnSuccess(VirtoCommerceEventSource.Log, VirtoCommerceEventSource.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); }
private void CalculateCustomerOrderTaxes(CartChangeEvent context) { SlabInvoker <VirtoCommerceEventSource.TaxRequestContext> .Execute(slab => { if (_taxSettings.IsEnabled && !string.IsNullOrEmpty(_taxSettings.Username) && !string.IsNullOrEmpty(_taxSettings.Password) && !string.IsNullOrEmpty(_taxSettings.ServiceUrl) && !string.IsNullOrEmpty(_taxSettings.CompanyCode)) { var cart = context.ModifiedCart; Contact contact = null; if (cart.CustomerId != null) { contact = _customerSearchService.GetById(cart.CustomerId); } var request = cart.ToAvaTaxRequest(_taxSettings.CompanyCode, contact); if (request != null) { slab.docCode = request.DocCode; slab.customerCode = request.CustomerCode; slab.docType = request.DocType.ToString(); slab.amount = (double)cart.Total; var taxSvc = new JsonTaxSvc(_taxSettings.Username, _taxSettings.Password, _taxSettings.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); } //reset all cart items taxes if (cart.Items.Any()) { cart.Items.ForEach(x => { x.TaxTotal = 0; x.TaxDetails = null; }); } //reset all cart shipments taxes if (cart.Shipments.Any()) { cart.Shipments.ForEach(x => { x.TaxTotal = 0; x.TaxDetails = null; }); } foreach (var taxLine in getTaxResult.TaxLines ?? Enumerable.Empty <TaxLine>()) { var lineItem = cart.Items.FirstOrDefault(x => x.Id == taxLine.LineNo); if (lineItem != null) { lineItem.TaxTotal = taxLine.Tax; if (taxLine.TaxDetails != null && taxLine.TaxDetails.Any(td => !string.IsNullOrEmpty(td.TaxName))) { lineItem.TaxDetails = taxLine.TaxDetails.Where(td => !string.IsNullOrEmpty(td.TaxName)).Select(taxDetail => new domainModel.TaxDetail { Amount = taxDetail.Tax, Name = taxDetail.TaxName, Rate = taxDetail.Rate }).ToList(); } } else { var shipment = cart.Shipments.FirstOrDefault(s => s.Id != null ? s.Id.Equals(taxLine.LineNo) : s.ShipmentMethodCode.Equals(taxLine.LineNo)); if (shipment != null) { shipment.TaxTotal = taxLine.Tax; if (taxLine.TaxDetails != null && taxLine.TaxDetails.Any(td => !string.IsNullOrEmpty(td.TaxName))) { shipment.TaxDetails = taxLine.TaxDetails.Where(td => !string.IsNullOrEmpty(td.TaxName)).Select(taxDetail => new domainModel.TaxDetail { Amount = taxDetail.Tax, Name = taxDetail.TaxName, Rate = taxDetail.Rate }).ToList(); } } } } } else { throw new Exception("Failed to create get tax request"); } } else { throw new Exception("Tax calculation disabled or credentials not provided"); } }) .OnError(VirtoCommerceEventSource.Log, VirtoCommerceEventSource.EventCodes.TaxCalculationError) .OnSuccess(VirtoCommerceEventSource.Log, VirtoCommerceEventSource.EventCodes.GetTaxRequestTime); }