/// <summary>
        /// Get active gift vouchers that are applied by a customer
        /// </summary>
        /// <param name="customer">Customer</param>
        /// <returns>Active gift vouchers</returns>
        private async Task <IList <GiftVoucher> > GetActiveGiftVouchers(Customer customer, Currency currency)
        {
            var result = new List <GiftVoucher>();

            if (customer == null)
            {
                return(result);
            }

            string[] couponCodes = customer.ParseAppliedCouponCodes(SystemCustomerFieldNames.GiftVoucherCoupons);
            foreach (var couponCode in couponCodes)
            {
                var giftVouchers = await _giftVoucherService.GetAllGiftVouchers(isGiftVoucherActivated : true, giftVoucherCouponCode : couponCode);

                foreach (var gc in giftVouchers)
                {
                    if (gc.IsGiftVoucherValid(currency))
                    {
                        result.Add(gc);
                    }
                }
            }

            return(result);
        }
        public async Task <bool> Handle(ActivatedValueForPurchasedGiftVouchersCommand request, CancellationToken cancellationToken)
        {
            if (request.Order == null)
            {
                throw new ArgumentNullException(nameof(request.Order));
            }

            foreach (var orderItem in request.Order.OrderItems)
            {
                var giftVouchers = await _giftVoucherService.GetAllGiftVouchers(purchasedWithOrderItemId : orderItem.Id,
                                                                                isGiftVoucherActivated : !request.Activate);

                foreach (var gc in giftVouchers)
                {
                    if (request.Activate)
                    {
                        //activate
                        bool isRecipientNotified = gc.IsRecipientNotified;
                        if (gc.GiftVoucherTypeId == GiftVoucherType.Virtual)
                        {
                            //send email for virtual gift voucher
                            if (!String.IsNullOrEmpty(gc.RecipientEmail) &&
                                !String.IsNullOrEmpty(gc.SenderEmail))
                            {
                                var customerLang = await _languageService.GetLanguageById(request.Order.CustomerLanguageId);

                                if (customerLang == null)
                                {
                                    customerLang = (await _languageService.GetAllLanguages()).FirstOrDefault();
                                }
                                if (customerLang == null)
                                {
                                    throw new Exception("No languages could be loaded");
                                }
                                int queuedEmailId = await _messageProviderService.SendGiftVoucherMessage(gc, request.Order, customerLang.Id);

                                if (queuedEmailId > 0)
                                {
                                    isRecipientNotified = true;
                                }
                            }
                        }
                        gc.IsGiftVoucherActivated = true;
                        gc.IsRecipientNotified    = isRecipientNotified;
                        await _giftVoucherService.UpdateGiftVoucher(gc);
                    }
                    else
                    {
                        //deactivate
                        gc.IsGiftVoucherActivated = false;
                        await _giftVoucherService.UpdateGiftVoucher(gc);
                    }
                }
            }

            return(true);
        }
        public virtual async Task <(IEnumerable <GiftVoucherModel> giftVoucherModels, int totalCount)> PrepareGiftVoucherModel(GiftVoucherListModel model, int pageIndex, int pageSize)
        {
            bool?isGiftVoucherActivated = null;

            if (model.ActivatedId == 1)
            {
                isGiftVoucherActivated = true;
            }
            else if (model.ActivatedId == 2)
            {
                isGiftVoucherActivated = false;
            }
            var giftVouchers = await _giftVoucherService.GetAllGiftVouchers(isGiftVoucherActivated : isGiftVoucherActivated,
                                                                            giftVoucherCouponCode : model.CouponCode,
                                                                            recipientName : model.RecipientName,
                                                                            pageIndex : pageIndex - 1, pageSize : pageSize);

            var giftvouchers = new List <GiftVoucherModel>();

            foreach (var item in giftVouchers)
            {
                var gift     = item.ToModel(_dateTimeService);
                var currency = await _currencyService.GetCurrencyByCode(item.CurrencyCode);

                if (currency == null)
                {
                    currency = await _currencyService.GetPrimaryStoreCurrency();
                }

                gift.RemainingAmountStr = _priceFormatter.FormatPrice(item.GetGiftVoucherRemainingAmount(), currency, _workContext.WorkingLanguage, true, false);
                gift.AmountStr          = _priceFormatter.FormatPrice(item.Amount, currency, _workContext.WorkingLanguage, true, false);
                gift.CreatedOn          = _dateTimeService.ConvertToUserTime(item.CreatedOnUtc, DateTimeKind.Utc);

                giftvouchers.Add(gift);
            }

            return(giftvouchers, giftVouchers.TotalCount);
        }
示例#4
0
        public async Task <IEnumerable <Gift> > GetAllGiftVouchers(string merchantId)
        {
            try
            {
                var vouchers = await _giftVoucherService.GetAllGiftVouchers(merchantId);

                foreach (var voucher in vouchers)
                {
                    string decryptedCode = CodeGenerator.Decrypt(voucher.Code);
                    voucher.Code = decryptedCode;
                }
                return(vouchers);
            }
            catch (SqlException ex)
            {
                _logger.LogError(ex, "Could not perform retrieve operation on vouchers of {Merchant}", merchantId);
                return(null);
            }
            catch (MongoException ex)
            {
                _logger.LogError(ex, "Could not perform retrieve operation on vouchers of {Merchant}", merchantId);
                return(null);
            }
        }