public virtual async Task <GiftVoucherModel> PrepareGiftVoucherModel(GiftVoucher giftVoucher)
        {
            var   model = giftVoucher.ToModel(_dateTimeService);
            Order order = null;

            if (giftVoucher.PurchasedWithOrderItem != null)
            {
                order = await _orderService.GetOrderByOrderItemId(giftVoucher.PurchasedWithOrderItem.Id);
            }

            var currency = await _currencyService.GetCurrencyByCode(giftVoucher.CurrencyCode);

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

            model.PurchasedWithOrderId     = giftVoucher.PurchasedWithOrderItem != null ? order?.Id : null;
            model.PurchasedWithOrderNumber = order?.OrderNumber ?? 0;
            model.RemainingAmountStr       = _priceFormatter.FormatPrice(giftVoucher.GetGiftVoucherRemainingAmount(), currency, _workContext.WorkingLanguage, true, false);
            model.AmountStr    = _priceFormatter.FormatPrice(giftVoucher.Amount, currency, _workContext.WorkingLanguage, true, false);
            model.CreatedOn    = _dateTimeService.ConvertToUserTime(giftVoucher.CreatedOnUtc, DateTimeKind.Utc);
            model.CurrencyCode = giftVoucher.CurrencyCode;
            return(model);
        }
Ejemplo n.º 2
0
        bool IsVoucherValid(GiftVoucher voucher)
        {
            bool isVoucherValid = false;

            if (voucher.GetType() == typeof(GiftVoucher))
            {
                isVoucherValid = true;
                _discountTotal = _discountTotal + voucher.Discount;
            }
            else if (!IsOfferVoucherAlreadyApplied)
            {
                if (voucher.GetType() == typeof(OfferVoucher))
                {
                    OfferVoucher offerVoucher = (OfferVoucher)voucher;
                    isVoucherValid = IsOfferVoucherValid(offerVoucher);
                }
                else if (voucher.GetType() == typeof(CategoryOfferVoucher))
                {
                    CategoryOfferVoucher offerVoucher = (CategoryOfferVoucher)voucher;
                    isVoucherValid = IsCategoryOfferVoucherValid(offerVoucher);
                }
            }
            else
            {
                _errorMessage = "You can only use one offer voucher at a time.";
            }

            return(isVoucherValid);
        }
Ejemplo n.º 3
0
        public void GiftVoucher_Redeemed_OK()
        {
            //Arrange
            var basket = new Model.Basket();

            var giftVoucher = new GiftVoucher("111-222", "�00 Gift Voucher", 5.00m);

            var hat = new BasketItem(1, "Hat", ProductCategoryHat, 10.50m);

            basket.AddBasketItem(hat);

            var jumper = new BasketItem(2, "Jumper", ProductCategoryTops, 54.65m);

            basket.AddBasketItem(jumper);

            //Act
            var addVoucherResult = basket.AddVoucher(giftVoucher);

            //Assert
            Assert.AreEqual(addVoucherResult, true);
            Assert.AreEqual(basket.BasketItems.Count, 2);
            Assert.AreEqual(basket.Vouchers.Count, 1);
            Assert.That(basket.VoucherNotAppliedMessage, Is.Null);
            Assert.AreEqual(basket.GetSubTotal(), 60.15m);
        }
        public virtual async Task DeleteGiftVoucher(GiftVoucher giftVoucher)
        {
            await _giftVoucherService.DeleteGiftVoucher(giftVoucher);

            //activity log
            await _customerActivityService.InsertActivity("DeleteGiftVoucher", giftVoucher.Id, _translationService.GetResource("ActivityLog.DeleteGiftVoucher"), giftVoucher.Code);
        }
Ejemplo n.º 5
0
        public void Voucher_WhenOfferVoucherInitialised_return_NotGiftVoucher()
        {
            GiftVoucher  giftVoucher = new GiftVoucher("YYY-YYY", 5);
            OfferVoucher expected    = new OfferVoucher("YYY-YYY", 5);

            expected.Should().NotBe(giftVoucher, "Gift vouchers have different rules to offer vouchers");
        }
        public virtual async Task NotifyRecipient(GiftVoucher giftVoucher, GiftVoucherModel model)
        {
            model = giftVoucher.ToModel(_dateTimeService);
            var order = await FillGiftVoucherModel(giftVoucher, model);

            var languageId = "";

            if (order != null)
            {
                var customerLang = await _languageService.GetLanguageById(order.CustomerLanguageId);

                if (customerLang == null)
                {
                    customerLang = (await _languageService.GetAllLanguages()).FirstOrDefault();
                }
                if (customerLang != null)
                {
                    languageId = customerLang.Id;
                }
            }
            else
            {
                languageId = _languageSettings.DefaultAdminLanguageId;
            }
            int queuedEmailId = await _messageProviderService.SendGiftVoucherMessage(giftVoucher, order, languageId);

            if (queuedEmailId > 0)
            {
                giftVoucher.IsRecipientNotified = true;
                await _giftVoucherService.UpdateGiftVoucher(giftVoucher);

                model.IsRecipientNotified = true;
            }
        }
        public static GiftVoucher ToEntity(this GiftVoucherModel model, GiftVoucher destination, IDateTimeService dateTimeService)
        {
            var giftVoucher = model.MapTo(destination);

            giftVoucher.ValidTo = model.ValidTo.ConvertToUtcTime(dateTimeService);
            return(giftVoucher);
        }
Ejemplo n.º 8
0
        public void Basket4()
        {
            var product1 = new Product {
                Name = "Hat", Price = 25.00M, Category = ProductCategory.Clothing
            };
            var product2 = new Product {
                Name = "Jumper", Price = 26M, Category = ProductCategory.Clothing
            };
            var offerVoucher = new OfferVoucher {
                Name = "YYY-YYY", Discount = 5M, Threshold = 50
            };
            var giftVoucher = new GiftVoucher {
                Name = "XXX-XXX", Value = 5M
            };

            var basket = new Basket(new GiftVoucherService(), new ProductService(new Data.ProductRepository()));

            basket.AddProduct(1);
            basket.AddProduct(4);
            basket.Voucher = offerVoucher;
            basket.AddGiftVoucher(giftVoucher);

            Assert.AreEqual(41M, basket.Total);
            Assert.IsNull(basket.Message);
        }
Ejemplo n.º 9
0
        public void Gift_vouchers_can_only_be_redeemed_against_non_gift_voucher_products()
        {
            var product1 = new Product {
                Id = 1, Name = "Jumper", Price = 30M, Category = ProductCategory.Clothing
            };
            var product2 = new Product {
                Id = 2, Name = "ggg-ggg", Price = 20M, Category = ProductCategory.GiftVoucher
            };
            var voucher = new GiftVoucher {
                Name = "XXX-XXX", Value = 50M
            };

            var mock = new Mock <IProductService>();

            mock.Setup(s => s.Get(1)).Returns(product1);
            mock.Setup(s => s.Get(2)).Returns(product2);

            var basket = new Basket(new GiftVoucherService(), mock.Object);

            basket.AddProduct(1);
            basket.AddProduct(2);
            basket.AddGiftVoucher(voucher);

            Assert.AreEqual(50.00M, basket.Total);
            Assert.AreEqual("Gift vouchers can only be redeemed against non gift voucher products.", basket.Message);
        }
Ejemplo n.º 10
0
        public void Multiple_Gift_vouchers_can_be_redeemed()
        {
            var product1 = new Product {
                Id = 1, Name = "Jumper", Price = 30M, Category = ProductCategory.Clothing
            };
            var product2 = new Product {
                Id = 2, Name = "Suit", Price = 250M, Category = ProductCategory.Clothing
            };
            var voucher1 = new GiftVoucher {
                Name = "XXX-XXX", Value = 50M
            };
            var voucher2 = new GiftVoucher {
                Name = "ggg-ggg", Value = 25M
            };

            var mock = new Mock <IProductService>();

            mock.Setup(s => s.Get(1)).Returns(product1);
            mock.Setup(s => s.Get(2)).Returns(product2);

            var basket = new Basket(new GiftVoucherService(), mock.Object);

            basket.AddProduct(1);
            basket.AddProduct(2);
            basket.AddGiftVoucher(voucher1);
            basket.AddGiftVoucher(voucher2);

            Assert.AreEqual(205M, basket.Total);
            Assert.IsNull(basket.Message);
        }
Ejemplo n.º 11
0
        public void Multiple_voucher_total_value_is_greater_than_basket_total()
        {
            var product1 = new Product {
                Id = 1, Name = "Jumper", Price = 30M, Category = ProductCategory.Clothing
            };
            var voucher1 = new GiftVoucher {
                Name = "XXX-XXX", Value = 50M
            };
            var voucher2 = new GiftVoucher {
                Name = "XXX-XXX", Value = 50M
            };

            var mock = new Mock <IProductService>();

            mock.Setup(s => s.Get(1)).Returns(product1);

            var basket = new Basket(new GiftVoucherService(), mock.Object);

            basket.AddProduct(1);
            basket.AddGiftVoucher(voucher1);
            basket.AddGiftVoucher(voucher2);

            Assert.AreEqual(0M, basket.Total);
            Assert.AreEqual("You still have £70.00 left on the gift voucher", basket.Message);
        }
        public static GiftVoucherModel ToModel(this GiftVoucher entity, IDateTimeService dateTimeService)
        {
            var giftVoucher = entity.MapTo <GiftVoucher, GiftVoucherModel>();

            giftVoucher.ValidTo = entity.ValidTo.ConvertToUserTime(dateTimeService);
            return(giftVoucher);
        }
Ejemplo n.º 13
0
        public Model.Basket RedeemGiftVoucher(int basketId, GiftVoucher giftVoucher)
        {
            var basket = _basketRepository.Get(basketId);

            basket.AddVoucher(giftVoucher);
            return(basket);
        }
Ejemplo n.º 14
0
        public void OfferGiftVoucher_Redeemed_OK()
        {
            //Arrange
            var basket = new Model.Basket();

            var hat = new BasketItem(1, "Hat", ProductCategoryHat, 25.00m);

            basket.AddBasketItem(hat);

            var jumper = new BasketItem(2, "Jumper", ProductCategoryTops, 26.00m);

            basket.AddBasketItem(jumper);

            var offerVoucher = new OfferVoucher("111-333", "�00 off baskets over �.00 Offer Voucher", 5.00m, 50.00m);
            var giftVoucher  = new GiftVoucher("111-222", "�00 Gift Voucher", 5.00m);

            //Act
            var addGiftVoucherResult  = basket.AddVoucher(giftVoucher);
            var addOfferVoucherResult = basket.AddVoucher(offerVoucher);

            //Assert
            Assert.AreEqual(addGiftVoucherResult, true);
            Assert.AreEqual(addOfferVoucherResult, true);
            Assert.AreEqual(basket.BasketItems.Count, 2);
            Assert.AreEqual(basket.Vouchers.Count, 2);
            Assert.That(basket.VoucherNotAppliedMessage, Is.Null);
            Assert.AreEqual(basket.GetSubTotal(), 41.00m);
        }
        public async Task Handle_Deactivate()
        {
            var command = new ActivatedValueForPurchasedGiftVouchersCommand()
            {
                Order = new Order()
            };
            var gift = new GiftVoucher()
            {
                RecipientEmail = "fdsf", SenderEmail = "aaa", IsGiftVoucherActivated = true
            };

            command.Activate = false;
            command.Order.OrderItems.Add(new OrderItem()
            {
            });
            _langService.Setup(c => c.GetLanguageById(It.IsAny <string>())).ReturnsAsync(new Language());
            _giftVoucherMock.Setup(c => c.GetAllGiftVouchers(It.IsAny <string>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(),
                                                             It.IsAny <bool?>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>()))
            .ReturnsAsync(new PagedList <GiftVoucher>()
            {
                gift
            });

            await _handler.Handle(command, default);

            Assert.IsFalse(gift.IsGiftVoucherActivated);
            _giftVoucherMock.Verify(c => c.UpdateGiftVoucher(gift));
        }
Ejemplo n.º 16
0
 public void ApplyVoucher(GiftVoucher voucher)
 {
     if (IsVoucherValid(voucher))
     {
         _appliedVouchers.Add(voucher);
     }
     CalculateTotalPrice();
 }
        public ActionResult DeleteConfirmed(int id)
        {
            GiftVoucher giftVoucher = db.GiftVouchers.Find(id);

            db.GiftVouchers.Remove(giftVoucher);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 18
0
        public void GiftVoucher_AcceptsPositiveValue()
        {
            Action act = () =>
            {
                var sut = new GiftVoucher(10m);
            };

            act.ShouldNotThrow();
        }
Ejemplo n.º 19
0
        public void GiftVoucher_RejectsNegativeValue()
        {
            Action act = () =>
            {
                var sut = new GiftVoucher(-10m);
            };

            act.ShouldThrow <ArgumentException>()
            .WithMessage("Value cannot be negative.");
        }
Ejemplo n.º 20
0
        public void Basket_CalulatesWithoutVoucherForTooLargeDiscount()
        {
            var testProduct = new Product("Hat", 10.50m);
            var testVoucher = new GiftVoucher(15m);

            _basket.AddProduct(testProduct);
            _basket.AddVoucher(testVoucher);

            _basket.Total().Should().Be(10.50m);
        }
        public virtual async Task <GiftVoucher> UpdateGiftVoucherModel(GiftVoucher giftVoucher, GiftVoucherModel model)
        {
            giftVoucher = model.ToEntity(giftVoucher, _dateTimeService);
            await _giftVoucherService.UpdateGiftVoucher(giftVoucher);

            //activity log
            await _customerActivityService.InsertActivity("EditGiftVoucher", giftVoucher.Id, _translationService.GetResource("ActivityLog.EditGiftVoucher"), giftVoucher.Code);

            return(giftVoucher);
        }
 public ActionResult Edit([Bind(Include = "Id,Discount,Type,Name,Sku,Description,RedmeeOnline,Voided,ExpireId")] GiftVoucher giftVoucher)
 {
     if (ModelState.IsValid)
     {
         db.Entry(giftVoucher).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.ExpireId = new SelectList(db.Expires, "Id", "Every", giftVoucher.ExpireId);
     return(View(giftVoucher));
 }
Ejemplo n.º 23
0
        public void Basket_AcceptsGiftVoucher()
        {
            var testProduct = new Product("Hat", 10m);
            var testVoucher = new GiftVoucher(5m);

            _sut.AddProduct(testProduct);
            _sut.AddVoucher(testVoucher);

            _sut.Vouchers.Should().HaveCount(1);
            _sut.Vouchers.Single().Should().Be(testVoucher);
        }
Ejemplo n.º 24
0
        public void Add_VoucherShouldUpdateTotal()
        {
            var voucher = new GiftVoucher(10);

            _valueCalculator.Setup(o => o.CalculateValue(It.IsAny <IEnumerable <Product> >(), It.IsAny <IEnumerable <Voucher> >())).Returns(0);
            _voucherValidator.Setup(o => o.Validate(It.IsAny <IEnumerable <Product> >(), It.IsAny <IEnumerable <Voucher> >())).Returns((Enumerable.Empty <Voucher>(), Enumerable.Empty <Voucher>()));

            _basket.Add(voucher);

            _basket.TotalValue.Should().Be(0);
        }
Ejemplo n.º 25
0
        public void Basket_whenGiftVoucherAdded_return_BasketHasVoucher()
        {
            Cart cart = new Cart(new List <CartItem>()
            {
                new CartItem(new Product("Hat", 10.5, new Category("")), 1)
            });
            GiftVoucher giftVoucher = new GiftVoucher("XXX-XXX", 5);
            Basket      basket      = new Basket(cart, giftVoucher);

            Assert.IsTrue(basket.HasVouchers);
        }
Ejemplo n.º 26
0
        public void Basket_whenGiftVoucherAddedAndGiftCodeAppliedAndCodeIsValid_return_ValidCode()
        {
            Cart cart = new Cart(new List <CartItem>()
            {
                new CartItem(new Product("Hat", 10.5, new Category("")), 1)
            });
            GiftVoucher giftVoucher = new GiftVoucher("XXX-XXX", 5);
            Basket      basket      = new Basket(cart, giftVoucher);

            Assert.IsTrue(basket.ApplyVoucher("XXX-XXX"));
        }
Ejemplo n.º 27
0
        public void Basket_whenGiftVoucherAddedAndGiftCodeAppliedAndCodeIsValid_return_BasketTotal()
        {
            Cart        cart        = new Cart();
            Product     product1    = cart.Add(new Product("Hat", 10.50), 1);
            Product     product3    = cart.Add(new Product("Jumper", 54.65), 1);
            GiftVoucher giftVoucher = new GiftVoucher("XXX-XXX", 5.00);
            Basket      basket      = new Basket(cart, giftVoucher);
            bool        applied     = basket.ApplyVoucher("XXX-XXX");

            basket.Total.Should().Equals(60.15);
        }
Ejemplo n.º 28
0
        public void GivenProductsAndGiftVoucherThenCalculateBasketValue()
        {
            var hat         = new Product(10.50m);
            var jumper      = new Product(54.65m);
            var giftVoucher = new GiftVoucher(5m);

            _basket.Add(hat);
            _basket.Add(jumper);
            _basket.Add(giftVoucher);

            _basket.TotalValue.Should().Be(60.15m);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Deletes a gift voucher
        /// </summary>
        /// <param name="giftVoucher">Gift voucher</param>
        public virtual async Task DeleteGiftVoucher(GiftVoucher giftVoucher)
        {
            if (giftVoucher == null)
            {
                throw new ArgumentNullException(nameof(giftVoucher));
            }

            await _giftVoucherRepository.DeleteAsync(giftVoucher);

            //event notification
            await _mediator.EntityDeleted(giftVoucher);
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Inserts a gift voucher
        /// </summary>
        /// <param name="giftVoucher">Gift voucher</param>
        public virtual async Task InsertGiftVoucher(GiftVoucher giftVoucher)
        {
            if (giftVoucher == null)
            {
                throw new ArgumentNullException(nameof(giftVoucher));
            }
            giftVoucher.Code = giftVoucher.Code.ToLowerInvariant();
            await _giftVoucherRepository.InsertAsync(giftVoucher);

            //event notification
            await _mediator.EntityInserted(giftVoucher);
        }