public virtual async Task <GiftCardModel> PrepareGiftCardModel(GiftCard giftCard) { var model = giftCard.ToModel(); Order order = null; if (giftCard.PurchasedWithOrderItem != null) { order = await _orderService.GetOrderByOrderItemId(giftCard.PurchasedWithOrderItem.Id); } var currency = await _currencyService.GetCurrencyByCode(giftCard.CurrencyCode); if (currency == null) { currency = await _currencyService.GetPrimaryStoreCurrency(); } model.PurchasedWithOrderId = giftCard.PurchasedWithOrderItem != null ? order?.Id : null; model.PurchasedWithOrderNumber = order?.OrderNumber ?? 0; model.RemainingAmountStr = _priceFormatter.FormatPrice(giftCard.GetGiftCardRemainingAmount(), true, currency, _workContext.WorkingLanguage, true, false); model.AmountStr = _priceFormatter.FormatPrice(giftCard.Amount, true, currency, _workContext.WorkingLanguage, true, false); model.CreatedOn = _dateTimeHelper.ConvertToUserTime(giftCard.CreatedOnUtc, DateTimeKind.Utc); model.CurrencyCode = giftCard.CurrencyCode; return(model); }
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 } ); gc.GetGiftCardRemainingAmount().ShouldEqual(45); }
/// <summary> /// Is gift card valid /// </summary> /// <param name="giftCard">Gift card</param> /// <returns>Result</returns> public static bool IsGiftCardValid(this GiftCard giftCard) { if (!giftCard.IsGiftCardActivated) { return(false); } decimal remainingAmount = giftCard.GetGiftCardRemainingAmount(); if (remainingAmount > decimal.Zero) { return(true); } return(false); }
public virtual async Task <Order> FillGiftCardModel(GiftCard giftCard, GiftCardModel model) { Order order = null; if (giftCard.PurchasedWithOrderItem != null) { order = await _orderService.GetOrderByOrderItemId(giftCard.PurchasedWithOrderItem.Id); } model.PurchasedWithOrderId = giftCard.PurchasedWithOrderItem != null ? order.Id : null; model.RemainingAmountStr = _priceFormatter.FormatPrice(giftCard.GetGiftCardRemainingAmount(), true, false); model.AmountStr = _priceFormatter.FormatPrice(giftCard.Amount, true, false); model.CreatedOn = _dateTimeHelper.ConvertToUserTime(giftCard.CreatedOnUtc, DateTimeKind.Utc); model.PrimaryStoreCurrencyCode = (await _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId)).CurrencyCode; return(order); }
public virtual GiftCardModel PrepareGiftCardModel(GiftCard giftCard) { var model = giftCard.ToModel(); Order order = null; if (giftCard.PurchasedWithOrderItem != null) { order = _orderService.GetOrderByOrderItemId(giftCard.PurchasedWithOrderItem.Id); } model.PurchasedWithOrderId = giftCard.PurchasedWithOrderItem != null ? order?.Id : null; model.PurchasedWithOrderNumber = order?.OrderNumber ?? 0; model.RemainingAmountStr = _priceFormatter.FormatPrice(giftCard.GetGiftCardRemainingAmount(), 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; return(model); }
/// <summary> /// Prepare gift card model /// </summary> /// <param name="model">Gift card model</param> /// <param name="giftCard">Gift card</param> /// <param name="excludeProperties">Whether to exclude populating of some properties of model</param> /// <returns>Gift card model</returns> public virtual GiftCardModel PrepareGiftCardModel(GiftCardModel model, GiftCard giftCard, bool excludeProperties = false) { if (giftCard != null) { //fill in model values from the entity model = model ?? giftCard.ToModel(); model.PurchasedWithOrderId = giftCard.PurchasedWithOrderItem?.OrderId; model.RemainingAmountStr = _priceFormatter.FormatPrice(giftCard.GetGiftCardRemainingAmount(), true, false); model.AmountStr = _priceFormatter.FormatPrice(giftCard.Amount, true, false); model.CreatedOn = _dateTimeHelper.ConvertToUserTime(giftCard.CreatedOnUtc, DateTimeKind.Utc); model.PurchasedWithOrderNumber = giftCard.PurchasedWithOrderItem?.Order?.CustomOrderNumber; //prepare nested search model PrepareGiftCardUsageHistorySearchModel(model.GiftCardUsageHistorySearchModel, giftCard); } model.PrimaryStoreCurrencyCode = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId)?.CurrencyCode; return(model); }
protected virtual object CreateModelPart(GiftCard part, MessageContext messageContext) { Guard.NotNull(messageContext, nameof(messageContext)); Guard.NotNull(part, nameof(part)); var m = new Dictionary <string, object> { { "Id", part.Id }, { "SenderName", part.SenderName.NullEmpty() }, { "SenderEmail", part.SenderEmail.NullEmpty() }, { "RecipientName", part.RecipientName.NullEmpty() }, { "RecipientEmail", part.RecipientEmail.NullEmpty() }, { "Amount", _services.Resolve <IPriceFormatter>().FormatPrice(part.Amount, true, false) }, { "CouponCode", part.GiftCardCouponCode.NullEmpty() } }; // Message var message = (string)null; if (part.Message.HasValue()) { message = HtmlUtils.FormatText(part.Message, true, false, false, false, false, false); } m["Message"] = message; // RemainingAmount var remainingAmount = (string)null; var order = part?.PurchasedWithOrderItem?.Order; if (order != null) { var amount = _services.Resolve <ICurrencyService>().ConvertCurrency(part.GetGiftCardRemainingAmount(), order.CurrencyRate); remainingAmount = _services.Resolve <IPriceFormatter>().FormatPrice(amount, true, false); } m["RemainingAmount"] = remainingAmount; PublishModelPartCreatedEvent <GiftCard>(part, m); return(m); }
protected virtual object CreateModelPart(GiftCard part, MessageContext messageContext) { Guard.NotNull(messageContext, nameof(messageContext)); Guard.NotNull(part, nameof(part)); var m = new Dictionary <string, object> { { "Id", part.Id }, { "SenderName", part.SenderName.NullEmpty() }, { "SenderEmail", part.SenderEmail.NullEmpty() }, { "RecipientName", part.RecipientName.NullEmpty() }, { "RecipientEmail", part.RecipientEmail.NullEmpty() }, { "Amount", FormatPrice(part.Amount, messageContext) }, { "CouponCode", part.GiftCardCouponCode.NullEmpty() } }; // Message var message = (string)null; if (part.Message.HasValue()) { message = HtmlUtils.StripTags(part.Message); } m["Message"] = message; // RemainingAmount Money remainingAmount = null; var order = part?.PurchasedWithOrderItem?.Order; if (order != null) { remainingAmount = FormatPrice(part.GetGiftCardRemainingAmount(), order, messageContext); } m["RemainingAmount"] = remainingAmount; PublishModelPartCreatedEvent <GiftCard>(part, m); return(m); }