public QuoteRequestTotals CalculateTotals(QuoteRequest quote) { var retVal = new QuoteRequestTotals(); var cartFromQuote = quote.ToCartModel(); var store = _storeService.GetById(quote.StoreId); if (store != null) { //Calculate shipment total //firts try to get manual amount retVal.ShippingTotal = quote.ManualShippingTotal; if (retVal.ShippingTotal == 0 && quote.ShipmentMethod != null) { //calculate total by using shipment gateways var evalContext = new ShippingEvaluationContext(cartFromQuote); var rate = store.ShippingMethods.Where(x => x.IsActive && x.Code == quote.ShipmentMethod.ShipmentMethodCode) .SelectMany(x => x.CalculateRates(evalContext)) .Where(x => quote.ShipmentMethod.OptionName != null ? quote.ShipmentMethod.OptionName == x.OptionName : true) .FirstOrDefault(); retVal.ShippingTotal = rate != null ? rate.Rate : 0m; } //Calculate taxes var taxProvider = store.TaxProviders.Where(x => x.IsActive).OrderBy(x => x.Priority).FirstOrDefault(); if (taxProvider != null) { var taxRequest = quote.ToTaxRequest(); var taxEvalContext = new TaxEvaluationContext(taxRequest); retVal.TaxTotal = taxProvider.CalculateRates(taxEvalContext).Select(x => x.Rate).DefaultIfEmpty(0).Sum(x => x); } } //Calculate subtotal var items = quote.Items.Where(x => x.SelectedTierPrice != null); if (quote.Items != null) { retVal.OriginalSubTotalExlTax = items.Sum(x => x.SalePrice * x.SelectedTierPrice.Quantity); retVal.SubTotalExlTax = items.Sum(x => x.SelectedTierPrice.Price * x.SelectedTierPrice.Quantity); if (quote.ManualSubTotal > 0) { retVal.DiscountTotal = retVal.SubTotalExlTax - quote.ManualSubTotal; retVal.SubTotalExlTax = quote.ManualSubTotal; } else if (quote.ManualRelDiscountAmount > 0) { retVal.DiscountTotal = retVal.SubTotalExlTax * quote.ManualRelDiscountAmount * 0.01m; } } return retVal; }
public IHttpActionResult EvaluateTaxes(string storeId, [FromBody]coreTaxModel.TaxEvaluationContext evalContext) { var retVal = new List<coreTaxModel.TaxRate>(); var store = _storeService.GetById(storeId); if(storeId != null) { var activeTaxProvider = store.TaxProviders.FirstOrDefault(x => x.IsActive); if(activeTaxProvider != null) { retVal.AddRange(activeTaxProvider.CalculateRates(evalContext)); } } return Ok(retVal); }
public static taxCoreModel.TaxEvaluationContext ToTaxEvalContext(this coreModel.QuoteRequest quoteRequest) { var retVal = new taxCoreModel.TaxEvaluationContext(); retVal.Id = quoteRequest.Id; retVal.Code = quoteRequest.Number; retVal.Currency = quoteRequest.Currency; retVal.Address = quoteRequest.Addresses != null ? quoteRequest.Addresses.FirstOrDefault() : null; retVal.Type = quoteRequest.GetType().Name; foreach(var quoteItem in quoteRequest.Items) { var line = new taxCoreModel.TaxLine { Id = quoteItem.Id, Code = quoteItem.Sku, Name = quoteItem.Name, TaxType = quoteItem.TaxType, Amount = quoteItem.SelectedTierPrice.Price * quoteItem.SelectedTierPrice.Quantity }; retVal.Lines.Add(line); } return retVal; }
public static taxCoreModel.TaxEvaluationContext ToTaxEvalContext(this ShoppingCart cart) { var retVal = new taxCoreModel.TaxEvaluationContext(); retVal.Id = cart.Id; retVal.Code = cart.Name; retVal.Currency = cart.Currency; retVal.Address = cart.Addresses != null ? cart.Addresses.FirstOrDefault() : null; retVal.Type = cart.GetType().Name; foreach (var cartItem in cart.Items) { var line = new taxCoreModel.TaxLine { Id = cartItem.Id, Code = cartItem.Sku, Name = cartItem.Name, TaxType = cartItem.TaxType, Amount = cartItem.SalePrice * cartItem.Quantity }; retVal.Lines.Add(line); } return retVal; }
public static taxCoreModel.TaxEvaluationContext ToTaxEvalContext(this coreModel.QuoteRequest quoteRequest) { var retVal = new taxCoreModel.TaxEvaluationContext(); retVal.Id = quoteRequest.Id; retVal.Code = quoteRequest.Number; retVal.Currency = quoteRequest.Currency; retVal.Address = quoteRequest.Addresses != null?quoteRequest.Addresses.FirstOrDefault() : null; retVal.Type = quoteRequest.GetType().Name; foreach (var quoteItem in quoteRequest.Items) { var line = new taxCoreModel.TaxLine { Id = quoteItem.Id, Code = quoteItem.Sku, Name = quoteItem.Name, TaxType = quoteItem.TaxType, Amount = quoteItem.SelectedTierPrice.Price * quoteItem.SelectedTierPrice.Quantity }; retVal.Lines.Add(line); } return(retVal); }
public static taxCoreModel.TaxEvaluationContext ToTaxEvalContext(this ShoppingCart cart) { var retVal = new taxCoreModel.TaxEvaluationContext(); retVal.Id = cart.Id; retVal.Code = cart.Name; retVal.Currency = cart.Currency; retVal.Address = cart.Addresses != null?cart.Addresses.FirstOrDefault() : null; retVal.Type = cart.GetType().Name; foreach (var cartItem in cart.Items) { var line = new taxCoreModel.TaxLine { Id = cartItem.Id, Code = cartItem.Sku, Name = cartItem.Name, TaxType = cartItem.TaxType, Amount = cartItem.SalePrice * cartItem.Quantity }; retVal.Lines.Add(line); } return(retVal); }
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; }