private async Task PrepareTotal(OrderTotalsModel model, GetOrderTotals request) { var carttotal = await _orderTotalCalculationService.GetShoppingCartTotal(request.Cart); decimal?shoppingCartTotalBase = carttotal.shoppingCartTotal; decimal orderTotalDiscountAmountBase = carttotal.discountAmount; List <AppliedGiftCard> appliedGiftCards = carttotal.appliedGiftCards; int redeemedRewardPoints = carttotal.redeemedRewardPoints; decimal redeemedRewardPointsAmount = carttotal.redeemedRewardPointsAmount; if (shoppingCartTotalBase.HasValue) { decimal shoppingCartTotal = await _currencyService.ConvertFromPrimaryStoreCurrency(shoppingCartTotalBase.Value, request.Currency); model.OrderTotal = _priceFormatter.FormatPrice(shoppingCartTotal, true, false); } //discount if (orderTotalDiscountAmountBase > decimal.Zero) { decimal orderTotalDiscountAmount = await _currencyService.ConvertFromPrimaryStoreCurrency(orderTotalDiscountAmountBase, request.Currency); model.OrderTotalDiscount = _priceFormatter.FormatPrice(-orderTotalDiscountAmount, true, false); } //gift cards if (appliedGiftCards != null && appliedGiftCards.Any()) { foreach (var appliedGiftCard in appliedGiftCards) { await PrepareGiftCards(appliedGiftCard, model, request); } } //reward points to be spent (redeemed) if (redeemedRewardPointsAmount > decimal.Zero) { decimal redeemedRewardPointsAmountInCustomerCurrency = await _currencyService.ConvertFromPrimaryStoreCurrency(redeemedRewardPointsAmount, request.Currency); model.RedeemedRewardPoints = redeemedRewardPoints; model.RedeemedRewardPointsAmount = _priceFormatter.FormatPrice(-redeemedRewardPointsAmountInCustomerCurrency, true, false); } //reward points to be earned if (_rewardPointsSettings.Enabled && _rewardPointsSettings.DisplayHowMuchWillBeEarned && shoppingCartTotalBase.HasValue) { decimal?shippingBaseInclTax = model.RequiresShipping ? (await _orderTotalCalculationService.GetShoppingCartShippingTotal(request.Cart, true)).shoppingCartShippingTotal : 0; var earnRewardPoints = shoppingCartTotalBase.Value - shippingBaseInclTax.Value; if (earnRewardPoints > 0) { model.WillEarnRewardPoints = _orderTotalCalculationService .CalculateRewardPoints(request.Customer, earnRewardPoints); } } }
/// <summary> /// Prepare the order totals model /// </summary> /// <param name="cart">List of the shopping cart item</param> /// <param name="isEditable">Whether model is editable</param> /// <returns>Order totals model</returns> public override OrderTotalsModel PrepareOrderTotalsModel(IList <ShoppingCartItem> cart, bool isEditable) { var model = new OrderTotalsModel { IsEditable = isEditable }; if (cart.Any()) { //Avalara plugin changes PrepareTaxDetails(cart); //Avalara plugin changes //subtotal var subTotalIncludingTax = _workContext.TaxDisplayType == TaxDisplayType.IncludingTax && !_taxSettings.ForceTaxExclusionFromOrderSubtotal; _orderTotalCalculationService.GetShoppingCartSubTotal(cart, subTotalIncludingTax, out var orderSubTotalDiscountAmountBase, out var _, out var subTotalWithoutDiscountBase, out var _); var subtotalBase = subTotalWithoutDiscountBase; var subtotal = _currencyService.ConvertFromPrimaryStoreCurrency(subtotalBase, _workContext.WorkingCurrency); model.SubTotal = _priceFormatter.FormatPrice(subtotal, true, _workContext.WorkingCurrency, _workContext.WorkingLanguage, subTotalIncludingTax); if (orderSubTotalDiscountAmountBase > decimal.Zero) { var orderSubTotalDiscountAmount = _currencyService.ConvertFromPrimaryStoreCurrency(orderSubTotalDiscountAmountBase, _workContext.WorkingCurrency); model.SubTotalDiscount = _priceFormatter.FormatPrice(-orderSubTotalDiscountAmount, true, _workContext.WorkingCurrency, _workContext.WorkingLanguage, subTotalIncludingTax); } //LoadAllShippingRateComputationMethods var shippingRateComputationMethods = _shippingPluginManager.LoadActivePlugins(_workContext.CurrentCustomer, _storeContext.CurrentStore.Id); //shipping info model.RequiresShipping = _shoppingCartService.ShoppingCartRequiresShipping(cart); if (model.RequiresShipping) { var shoppingCartShippingBase = _orderTotalCalculationService.GetShoppingCartShippingTotal(cart, shippingRateComputationMethods); if (shoppingCartShippingBase.HasValue) { var shoppingCartShipping = _currencyService.ConvertFromPrimaryStoreCurrency(shoppingCartShippingBase.Value, _workContext.WorkingCurrency); model.Shipping = _priceFormatter.FormatShippingPrice(shoppingCartShipping, true); //selected shipping method var shippingOption = _genericAttributeService.GetAttribute <ShippingOption>(_workContext.CurrentCustomer, NopCustomerDefaults.SelectedShippingOptionAttribute, _storeContext.CurrentStore.Id); if (shippingOption != null) { model.SelectedShippingMethod = shippingOption.Name; } } } else { model.HideShippingTotal = _shippingSettings.HideShippingTotal; } //payment method fee var paymentMethodSystemName = _genericAttributeService.GetAttribute <string>(_workContext.CurrentCustomer, NopCustomerDefaults.SelectedPaymentMethodAttribute, _storeContext.CurrentStore.Id); var paymentMethodAdditionalFee = _paymentService.GetAdditionalHandlingFee(cart, paymentMethodSystemName); var paymentMethodAdditionalFeeWithTaxBase = _taxService.GetPaymentMethodAdditionalFee(paymentMethodAdditionalFee, _workContext.CurrentCustomer); if (paymentMethodAdditionalFeeWithTaxBase > decimal.Zero) { var paymentMethodAdditionalFeeWithTax = _currencyService.ConvertFromPrimaryStoreCurrency(paymentMethodAdditionalFeeWithTaxBase, _workContext.WorkingCurrency); model.PaymentMethodAdditionalFee = _priceFormatter.FormatPaymentMethodAdditionalFee(paymentMethodAdditionalFeeWithTax, true); } //tax var displayTax = true; var displayTaxRates = true; if (_taxSettings.HideTaxInOrderSummary && _workContext.TaxDisplayType == TaxDisplayType.IncludingTax) { displayTax = false; displayTaxRates = false; } else { var shoppingCartTaxBase = _orderTotalCalculationService.GetTaxTotal(cart, shippingRateComputationMethods, out var taxRates); //Avalara plugin changes //get tax details from the Avalara tax service, it may slightly differ from the original calculated tax var taxDetails = _httpContextAccessor.HttpContext.Session.Get <TaxDetails>(AvalaraTaxDefaults.TaxDetailsSessionValue); if (taxDetails != null) { //adjust tax total according to received value from the Avalara if (taxDetails.TaxTotal.HasValue) { shoppingCartTaxBase = taxDetails.TaxTotal.Value; } if (taxDetails.TaxRates?.Any() ?? false) { taxRates = new SortedDictionary <decimal, decimal>(taxDetails.TaxRates); } } //Avalara plugin changes var shoppingCartTax = _currencyService.ConvertFromPrimaryStoreCurrency(shoppingCartTaxBase, _workContext.WorkingCurrency); if (shoppingCartTaxBase == 0 && _taxSettings.HideZeroTax) { displayTax = false; displayTaxRates = false; } else { displayTaxRates = _taxSettings.DisplayTaxRates && taxRates.Any(); displayTax = !displayTaxRates; model.Tax = _priceFormatter.FormatPrice(shoppingCartTax, true, false); foreach (var tr in taxRates) { model.TaxRates.Add(new OrderTotalsModel.TaxRate { Rate = _priceFormatter.FormatTaxRate(tr.Key), Value = _priceFormatter.FormatPrice(_currencyService.ConvertFromPrimaryStoreCurrency(tr.Value, _workContext.WorkingCurrency), true, false), }); } } } model.DisplayTaxRates = displayTaxRates; model.DisplayTax = displayTax; //total var shoppingCartTotalBase = _orderTotalCalculationService.GetShoppingCartTotal(cart, out var orderTotalDiscountAmountBase, out var _, out var appliedGiftCards, out var redeemedRewardPoints, out var redeemedRewardPointsAmount); if (shoppingCartTotalBase.HasValue) { var shoppingCartTotal = _currencyService.ConvertFromPrimaryStoreCurrency(shoppingCartTotalBase.Value, _workContext.WorkingCurrency); model.OrderTotal = _priceFormatter.FormatPrice(shoppingCartTotal, true, false); } //discount if (orderTotalDiscountAmountBase > decimal.Zero) { var orderTotalDiscountAmount = _currencyService.ConvertFromPrimaryStoreCurrency(orderTotalDiscountAmountBase, _workContext.WorkingCurrency); model.OrderTotalDiscount = _priceFormatter.FormatPrice(-orderTotalDiscountAmount, true, false); } //gift cards if (appliedGiftCards != null && appliedGiftCards.Any()) { foreach (var appliedGiftCard in appliedGiftCards) { var gcModel = new OrderTotalsModel.GiftCard { Id = appliedGiftCard.GiftCard.Id, CouponCode = appliedGiftCard.GiftCard.GiftCardCouponCode, }; var amountCanBeUsed = _currencyService.ConvertFromPrimaryStoreCurrency(appliedGiftCard.AmountCanBeUsed, _workContext.WorkingCurrency); gcModel.Amount = _priceFormatter.FormatPrice(-amountCanBeUsed, true, false); var remainingAmountBase = _giftCardService.GetGiftCardRemainingAmount(appliedGiftCard.GiftCard) - appliedGiftCard.AmountCanBeUsed; var remainingAmount = _currencyService.ConvertFromPrimaryStoreCurrency(remainingAmountBase, _workContext.WorkingCurrency); gcModel.Remaining = _priceFormatter.FormatPrice(remainingAmount, true, false); model.GiftCards.Add(gcModel); } } //reward points to be spent (redeemed) if (redeemedRewardPointsAmount > decimal.Zero) { var redeemedRewardPointsAmountInCustomerCurrency = _currencyService.ConvertFromPrimaryStoreCurrency(redeemedRewardPointsAmount, _workContext.WorkingCurrency); model.RedeemedRewardPoints = redeemedRewardPoints; model.RedeemedRewardPointsAmount = _priceFormatter.FormatPrice(-redeemedRewardPointsAmountInCustomerCurrency, true, false); } //reward points to be earned if (_rewardPointsSettings.Enabled && _rewardPointsSettings.DisplayHowMuchWillBeEarned && shoppingCartTotalBase.HasValue) { //get shipping total var shippingBaseInclTax = !model.RequiresShipping ? 0 : _orderTotalCalculationService.GetShoppingCartShippingTotal(cart, true, shippingRateComputationMethods) ?? 0; //get total for reward points var totalForRewardPoints = _orderTotalCalculationService .CalculateApplicableOrderTotalForRewardPoints(shippingBaseInclTax, shoppingCartTotalBase.Value); if (totalForRewardPoints > decimal.Zero) { model.WillEarnRewardPoints = _orderTotalCalculationService.CalculateRewardPoints(_workContext.CurrentCustomer, totalForRewardPoints); } } } return(model); }