Example #1
0
        /// <summary>
        /// COCOKCUD0Z9LUKBN: Pay 2 take 3 on "Windmill Cookies" on up to 6 units, from Jan 24th to Feb 6th
        /// </summary>
        public void TestVoucherCOCOKCUD0Z9LUKBN()
        {
            IList <Tuple <Product, int> > products = new List <Tuple <Product, int> >();
            Product foodProduct = getMockProduct(8, "Windmill Cookies", 100, ProductCategoryType.Food);

            // Add 7 units of Windmill Cookies on products List
            products.Add(new Tuple <Product, int>(foodProduct, 7));
            // Another product to the list
            Product cleaningProduct = getMockProduct(15, "Atlantis detergent", 100, ProductCategoryType.Cleaning);

            products.Add(new Tuple <Product, int>(cleaningProduct, 1));
            // Create Vouchert pay two take three
            Voucher voucherDiscount = this.GetVoucher("COCOKCUD0Z9LUKBN");
            // In Range
            decimal result_discount = voucherDiscount.Calculate(products, new DateTime(DateTime.Now.Year, 1, 28));

            // from 8 units, total cost=800, Voucher valid from up to 6 units,
            // for every 3 units one is free, 3 units = 100 discount
            // total 7 units=> 3+3+1 = 7 => 100+100+0 => Total discount =200
            Assert.Equal(200, result_discount);
            // Out of Range 1
            decimal result_discount_zero = voucherDiscount.Calculate(products, new DateTime(DateTime.Now.Year, 1, 10));

            Assert.Equal(0, result_discount_zero);
            // Out of Range 2
            decimal result_discount_zero_2 = voucherDiscount.Calculate(products, new DateTime(DateTime.Now.Year, 3, 10));

            Assert.Equal(0, result_discount_zero_2);
            // Change vouchers config to upto 9  units and in range
            voucherDiscount.onUpTo = 9;
            decimal result_discount_up_to_9 = voucherDiscount.Calculate(products, new DateTime(DateTime.Now.Year, 1, 28));

            Assert.Equal(0, result_discount_up_to_9);
        }
Example #2
0
        public void TestVoucherCOCO1V1F8XOG1MZZ()
        {
            Product cleaningProduct = getMockProduct(15, "Atlantis detergent", 100, ProductCategoryType.Cleaning);
            IList <Tuple <Product, int> > products = new List <Tuple <Product, int> >();

            products.Add(new Tuple <Product, int>(cleaningProduct, 1));
            // 20 % off on Wednesdays and Thursdays, on Cleaning products,from Jan 27th to Feb 13th
            Voucher voucherDiscount = this.GetVoucher("COCO1V1F8XOG1MZZ");
            // In Range, 2020/2/3 is Wednesdays
            decimal result_discount = voucherDiscount.Calculate(products, new DateTime(2020, 2, 5));

            Assert.Equal(20, result_discount);
            // Out of Range 1
            decimal result_discount_zero = voucherDiscount.Calculate(products, new DateTime(2020, 1, 10));

            Assert.Equal(0, result_discount_zero);
            // Out of Range 2
            decimal result_discount_zero_2 = voucherDiscount.Calculate(products, new DateTime(2020, 6, 10));

            Assert.Equal(0, result_discount_zero_2);
            // Include 5 units of Generic mop, it cost 40.
            Product cleaningProduct_2 = getMockProduct(2, "Generic mop", 40, ProductCategoryType.Cleaning);

            products.Add(new Tuple <Product, int>(cleaningProduct_2, 5));
            decimal result_discount_2 = voucherDiscount.Calculate(products, new DateTime(2020, 1, 29));

            Assert.Equal(20 + 40, result_discount_2);
        }
Example #3
0
        /// COCOG730CNSG8ZVX: 10% off on Bathroom and Sodas, from Jan 31th to Feb 9th
        public void TestVoucherCOCOG730CNSG8ZVX()
        {
            IList <Tuple <Product, int> > products = new List <Tuple <Product, int> >();

            Product cleaningProduct = getMockProduct(15, "Atlantis detergent", 100, ProductCategoryType.Cleaning);

            // Atlantis Detergent*******Cant:1*****Unit:100*****Total:100
            products.Add(new Tuple <Product, int>(cleaningProduct, 1));
            Product sodaProduct_1 = getMockProduct(3, "Nuke-Cola", 100, ProductCategoryType.Sodas);

            // Nuke-Cola****************Cant:3*****Unit:100*****Total:300
            products.Add(new Tuple <Product, int>(sodaProduct_1, 3));
            Product sodaProduct_2 = getMockProduct(4, "Sprute", 100, ProductCategoryType.Sodas);

            // Sprute*******************Cant:3*****Unit:100*****Total:400
            products.Add(new Tuple <Product, int>(sodaProduct_2, 1));

            Product bathroomProduct = getMockProduct(20, "Generic soap", 100, ProductCategoryType.Bathroom);

            products.Add(new Tuple <Product, int>(bathroomProduct, 1));

            PrintItemList(products);
            // Voucher discount 10%
            Voucher voucherDiscount = this.GetVoucher("COCOG730CNSG8ZVX");
            // In Range
            decimal result_discount = voucherDiscount.Calculate(products, new DateTime(DateTime.Now.Year, 2, 5));

            Assert.Equal(50, result_discount);
            // Out of Range 1
            decimal result_discount_zero = voucherDiscount.Calculate(products, new DateTime(DateTime.Now.Year, 1, 10));

            Assert.Equal(0, result_discount_zero);
            // Out of Range 2
            decimal result_discount_zero_2 = voucherDiscount.Calculate(products, new DateTime(DateTime.Now.Year, 3, 10));

            Assert.Equal(0, result_discount_zero_2);
            // Include 5 units of Generic mop, it cost 40.
            Product cleaningProduct_2 = getMockProduct(2, "Generic mop", 40, ProductCategoryType.Cleaning);

            products.Add(new Tuple <Product, int>(cleaningProduct_2, 5));

            decimal result_discount_2 = voucherDiscount.Calculate(products, new DateTime(DateTime.Now.Year, 1, 31));

            Assert.Equal(50, result_discount_2);
        }
Example #4
0
        // COCO0FLEQ287CC05: 50% off on the second unit (of the same product), on "Hang-    yourself toothpaste", only on Mondays, first half of February.
        public void TestVoucherCOCO0FLEQ287CC05()
        {
            IList <Tuple <Product, int> > products = new List <Tuple <Product, int> >();
            Product cleaningProduct = getMockProduct(15, "Hang-yourself toothpaste", 100, ProductCategoryType.Cleaning);

            // Atlantis Detergent*******Cant:1*****Unit:100*****Total:100
            products.Add(new Tuple <Product, int>(cleaningProduct, 1));
            Product sodaProduct_1 = getMockProduct(3, "Nuke-Cola", 100, ProductCategoryType.Sodas);

            // Nuke-Cola****************Cant:3*****Unit:100*****Total:300
            products.Add(new Tuple <Product, int>(sodaProduct_1, 3));
            Product sodaProduct_2 = getMockProduct(4, "Sprute", 100, ProductCategoryType.Sodas);

            // Sprute*******************Cant:3*****Unit:100*****Total:400
            products.Add(new Tuple <Product, int>(sodaProduct_2, 1));
            Product slurmSoda = getMockProduct(5, "Slurm", 100, ProductCategoryType.Sodas);

            // Sprute*******************Cant:3*****Unit:100*****Total:300
            products.Add(new Tuple <Product, int>(slurmSoda, 3));
            Product bathroomProduct = getMockProduct(20, "Generic soap", 100, ProductCategoryType.Bathroom);

            products.Add(new Tuple <Product, int>(bathroomProduct, 1));
            products.Add(new Tuple <Product, int>(getMockProduct(23, "Hang -yourself toothpaste", 100, ProductCategoryType.Bathroom), 2));
            // Show Item List
            PrintItemList(products);
            // Voucher 50% off on the second unit (of the same product), on "Hang-    yourself toothpaste", only on Mondays, first half of February.
            Voucher voucherDiscount = this.GetVoucher("COCO0FLEQ287CC05");
            // In Range the first of January of 2021 is Monday
            decimal result_discount_2 = voucherDiscount.Calculate(products, new DateTime(2021, 2, 1));

            // Hang-    yourself toothpaste price is 100
            // the 50% off second unit of yourself toothpaste is  = 50
            Assert.Equal(50, result_discount_2);

            // Not In Range, the second of January, 2021 is not Monday
            decimal result_discount_3 = voucherDiscount.Calculate(products, new DateTime(2021, 2, 2));

            Assert.Equal(0, result_discount_3);
        }
Example #5
0
        /// COCO2O1USLC6QR22: 30% off on the second unit (of the same product), on "Nuka-Cola", "Slurm" and "Diet Slurm", for all February
        public void TestVoucherCOCO2O1USLC6QR22()
        {
            IList <Tuple <Product, int> > products = new List <Tuple <Product, int> >();
            Product cleaningProduct = getMockProduct(15, "Atlantis detergent", 100, ProductCategoryType.Cleaning);

            // Atlantis Detergent*******Cant:1*****Unit:100*****Total:100
            products.Add(new Tuple <Product, int>(cleaningProduct, 1));
            Product sodaProduct_1 = getMockProduct(3, "Nuke-Cola", 100, ProductCategoryType.Sodas);

            // Nuke-Cola****************Cant:3*****Unit:100*****Total:300
            products.Add(new Tuple <Product, int>(sodaProduct_1, 3));
            Product sodaProduct_2 = getMockProduct(4, "Sprute", 100, ProductCategoryType.Sodas);

            // Sprute*******************Cant:3*****Unit:100*****Total:400
            products.Add(new Tuple <Product, int>(sodaProduct_2, 1));
            Product bathroomProduct = getMockProduct(20, "Generic soap", 100, ProductCategoryType.Bathroom);

            products.Add(new Tuple <Product, int>(bathroomProduct, 1));
            // Show Item List
            PrintItemList(products);
            // Voucher 30% off on the second unit (of the same product), on "Nuka-Cola","Slurm" and "Diet Slurm", for all February
            Voucher voucherDiscount = this.GetVoucher("COCO2O1USLC6QR22");
            // In Range
            decimal result_discount_2 = voucherDiscount.Calculate(products, new DateTime(DateTime.Now.Year, 2, 14));

            // Nuke-Colas price is 100, there are 3 units on the list => only one pair
            // the 30% of a unit is  = 30
            Assert.Equal(30, result_discount_2);
            Product slurmSoda = getMockProduct(5, "Slurm", 100, ProductCategoryType.Sodas);

            // Sprute*******************Cant:3*****Unit:100*****Total:300
            products.Add(new Tuple <Product, int>(slurmSoda, 5));
            // Slurm price is 100, there are 5 units on the list => there are 2 pairs
            // the 30% of a two units is  = 60
            decimal result_discount_3 = voucherDiscount.Calculate(products, new DateTime(DateTime.Now.Year, 2, 14));

            // slurms discount is 60 plus nuke-colas discount is 30
            Assert.Equal(60 + 30, result_discount_3);
        }
Example #6
0
        public async Task <ActionResult <Cart> > ApplyVoucher(String Owner, String VoucherId, DateTime date_voucher)
        {
            Cart cart;

            try
            {
                IList <Cart> carts = _cartRepository.GetAll();
                cart = carts.FirstOrDefault <Cart>(x => x.Owner == Owner);
                if (cart == null)
                {
                    throw new Exception(String.Format("The {0}'s cart  does not exist in the database", Owner));
                }

                IList <VoucherStore> voucherValids = await _voucherRepository.validVouchers(cart.StoreId);

                if (voucherValids.Any(c => c.VoucherId == VoucherId))
                {
                    Voucher voucher = await _voucherRepository.GetByID(VoucherId);

                    IList <Tuple <Product, int> > listProducts = new List <Tuple <Product, int> >();
                    decimal total = 0;
                    foreach (CartItem cartItem in cart.Items)
                    {
                        total = total + cartItem.Product.Price * cartItem.Cant;
                        listProducts.Add(cartItem.GetTuple());
                    }
                    decimal discount = voucher.Calculate(listProducts, date_voucher);
                    cart.Total          = total;
                    cart.Total_discount = total - discount;
                    return(cart);
                }
                else
                {
                    return(BadRequest(String.Format("Voucher {0} invalid in Store {1}", VoucherId, cart.StoreId)));
                }
            }
            catch (Exception ex)
            {
                if (ex is MiniMartException)
                {
                    return(BadRequest(((MiniMartException)ex).MinimartMessage));
                }
                else
                {
                    return(BadRequest(ex.Message));
                }
            }
        }