public virtual IActionResult Edit(GiftCardModel model, bool continueEditing) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageGiftCards)) { return(AccessDeniedView()); } //try to get a gift card with the specified id var giftCard = _giftCardService.GetGiftCardById(model.Id); if (giftCard == null) { return(RedirectToAction("List")); } model.PurchasedWithOrderId = giftCard.PurchasedWithOrderItem != null ? (int?)giftCard.PurchasedWithOrderItem.OrderId : null; model.RemainingAmountStr = _priceFormatter.FormatPrice(_giftCardService.GetGiftCardRemainingAmount(giftCard), true, false); model.AmountStr = _priceFormatter.FormatPrice(giftCard.Amount, true, false); model.CreatedOn = _dateTimeHelper.ConvertToUserTime(giftCard.CreatedOnUtc, DateTimeKind.Utc); model.PrimaryStoreCurrencyCode = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId).CurrencyCode; model.PurchasedWithOrderNumber = giftCard.PurchasedWithOrderItem?.Order.CustomOrderNumber; if (ModelState.IsValid) { giftCard = model.ToEntity(giftCard); _giftCardService.UpdateGiftCard(giftCard); //activity log _customerActivityService.InsertActivity("EditGiftCard", string.Format(_localizationService.GetResource("ActivityLog.EditGiftCard"), giftCard.GiftCardCouponCode), giftCard); SuccessNotification(_localizationService.GetResource("Admin.GiftCards.Updated")); if (!continueEditing) { return(RedirectToAction("List")); } //selected tab SaveSelectedTabName(); return(RedirectToAction("Edit", new { id = giftCard.Id })); } //prepare model model = _giftCardModelFactory.PrepareGiftCardModel(model, giftCard, true); //if we got this far, something failed, redisplay form return(View(model)); }
public void Can_calculate_giftCard_remainingAmount() { RunWithTestServiceProvider(() => { var gc = _giftCardService.GetGiftCardById(2); _giftCardService.InsertGiftCardUsageHistory( new GiftCardUsageHistory { GiftCardId = gc.Id, UsedValue = 30 }); _giftCardService.InsertGiftCardUsageHistory( new GiftCardUsageHistory { GiftCardId = gc.Id, UsedValue = 20 }); _giftCardService.InsertGiftCardUsageHistory( new GiftCardUsageHistory { GiftCardId = gc.Id, UsedValue = 5 }); _giftCardService.GetGiftCardRemainingAmount(gc).Should().Be(45); }); }
public void Can_calculate_giftCard_remainingAmount() { var gc = new GiftCard { Amount = 100 }; gc.GiftCardUsageHistory.Add ( new GiftCardUsageHistory { UsedValue = 30 } ); gc.GiftCardUsageHistory.Add ( new GiftCardUsageHistory { UsedValue = 20 } ); gc.GiftCardUsageHistory.Add ( new GiftCardUsageHistory { UsedValue = 5 } ); _giftCardService.GetGiftCardRemainingAmount(gc).ShouldEqual(45); }
/// <summary> /// Prepare paged gift card list model /// </summary> /// <param name="searchModel">Gift card search model</param> /// <returns>Gift card list model</returns> public virtual GiftCardListModel PrepareGiftCardListModel(GiftCardSearchModel searchModel) { if (searchModel == null) { throw new ArgumentNullException(nameof(searchModel)); } //get parameters to filter gift cards var isActivatedOnly = searchModel.ActivatedId == 0 ? null : searchModel.ActivatedId == 1 ? true : (bool?)false; //get gift cards var giftCards = _giftCardService.GetAllGiftCards(isGiftCardActivated: isActivatedOnly, giftCardCouponCode: searchModel.CouponCode, recipientName: searchModel.RecipientName, pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize); //prepare list model var model = new GiftCardListModel { Data = giftCards.Select(giftCard => { //fill in model values from the entity var giftCardModel = giftCard.ToModel <GiftCardModel>(); //convert dates to the user time giftCardModel.CreatedOn = _dateTimeHelper.ConvertToUserTime(giftCard.CreatedOnUtc, DateTimeKind.Utc); //fill in additional values (not existing in the entity) giftCardModel.RemainingAmountStr = _priceFormatter.FormatPrice(_giftCardService.GetGiftCardRemainingAmount(giftCard), true, false); giftCardModel.AmountStr = _priceFormatter.FormatPrice(giftCard.Amount, true, false); return(giftCardModel); }), Total = giftCards.TotalCount }; return(model); }
public void CanCalculateGiftCardRemainingAmount() { _giftCardService.InsertGiftCardUsageHistory( new GiftCardUsageHistory { GiftCardId = _giftCard2.Id, UsedWithOrderId = 1, UsedValue = 30 }); _giftCardService.InsertGiftCardUsageHistory( new GiftCardUsageHistory { GiftCardId = _giftCard2.Id, UsedWithOrderId = 1, UsedValue = 20 }); _giftCardService.InsertGiftCardUsageHistory( new GiftCardUsageHistory { GiftCardId = _giftCard2.Id, UsedWithOrderId = 1, UsedValue = 5 }); _giftCardService.GetGiftCardRemainingAmount(_giftCard2).Should().Be(45); }
/// <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); }