コード例 #1
0
ファイル: Discount.cs プロジェクト: Woodhobit/PointOfSaleKata
        public void Validate(Notification note)
        {
            var validator = new DiscountValidator();

            validator.Validate(note, this);

            this.IsValid = !note.HasErrors;
        }
コード例 #2
0
        public void ValidateDiscountAmount_InvalidInput_ReturnsDomainException()
        {
            // Assign
            var validator       = new DiscountValidator();
            var order           = OrderFactory.CreateSimpleOrder();
            var invalidDiscount = 1;

            // Assert
            Assert.Throws <DomainException>(()
                                            // Act
                                            => validator.ValidateDiscountAmount(order, invalidDiscount));
        }
コード例 #3
0
        public void ValidateDiscountAmount_ValidInput_ReturnsVoid()
        {
            // Assign
            var validator = new DiscountValidator();
            var order     = OrderFactory.CreateSimpleOrder();

            // Act
            validator.ValidateDiscountAmount(order, voucherPercentageDiscount: 10);

            // Assert
            // No exception was thrown.
            Assert.True(true);
        }
コード例 #4
0
        private void AlphabeticalDiscount_ReturnsDiscount_WhenAnimalsAreValid()
        {
            //Arrange
            List <Animal> animals           = GetAnimals();
            var           discountValidator = new DiscountValidator();

            //Act
            var result = discountValidator.GetAlphabeticalDiscount(animals);

            //Assert
            var resultInt = Assert.IsType <int>(result);

            Assert.Equal(6, resultInt);
        }
コード例 #5
0
        private void GetMaximumPercentage_ReturnsMaxDiscount_WhenOverMaxDiscount()
        {
            //Arrange
            int discountPercentage = 70;
            var discountValidator  = new DiscountValidator();

            //Act
            var result = discountValidator.GetMaximumPercentage(discountPercentage, 60);

            //Assert
            var intResult = Assert.IsType <int>(result);

            Assert.Equal(60, intResult);
        }
コード例 #6
0
        private void ChanceOnDuckDiscount_ReturnsDiscount_WhenDuck()
        {
            //Arrange
            List <Animal> animals           = GetAnimals();
            var           discountValidator = new DiscountValidator();

            //Act
            var isDuck = discountValidator.AnimalsHasDuck(animals);
            var result = discountValidator.ChanceOnDuckDiscount(animals);


            //Assert
            Assert.IsType <int>(result);
            Assert.True(isDuck);
        }
コード例 #7
0
        private void BookingIsMondayOrTuesday_ReturnsDiscount_WhenValidDate()
        {
            //Arrange
            DateTime dateTime          = new DateTime(2020, 01, 14);
            var      discountValidator = new DiscountValidator();

            //Act
            var result = discountValidator.BookingIsMondayOrTuesday(dateTime);


            //Assert
            var intResult = Assert.IsType <int>(result);

            Assert.Equal(DayOfWeek.Tuesday, dateTime.DayOfWeek);
            Assert.Equal(15, intResult);
        }
コード例 #8
0
        private void ChanceOnDuckDiscount_ReturnsZeroDiscount_WhenAnimalsNull()
        {
            //Arrange
            List <Animal> animals           = null;
            var           discountValidator = new DiscountValidator();

            //Act
            var isDuck = discountValidator.AnimalsHasDuck(animals);
            var result = discountValidator.ChanceOnDuckDiscount(animals);


            //Assert
            var intResult = Assert.IsType <int>(result);

            Assert.Equal(0, intResult);
            Assert.False(isDuck);
        }
コード例 #9
0
        // TODO: This method can replace the other AddToCart method once it's implemented.
        protected ActionResult AddToCart(Product product, Discount discount = null, IIndividualReward reward = null, Event @event = null)
        {
            var shoppingCart = GetShoppingCart();

            try
            {
                while (product.Discounts.Any())
                {
                    product.UnapplyDiscount(product.Discounts.First());
                }

                // Add the product to the order.
                var p = product.AddToOrder(shoppingCart);

                RewardsAccount pointAccount = null;

                // Validate that the discount can be applied
                // to the product using our validator.
                if (null != discount)
                {
                    bool eventAllowsDiscount  = shoppingCart.ShopperIsHost || shoppingCart.ShopperIsEventSa; // || shoppingCart.ShopperIsBookingRewardsOwner;
                    bool rewardAllowsDiscount = reward != null && reward.IsRewardDiscount(discount) && reward.AllowAddToCart(product, shoppingCart.Products);

                    if ((eventAllowsDiscount || rewardAllowsDiscount) && DiscountValidator.IsValidFor(discount, p, @event))
                    {
                        // TODO: Need to find a better way to map discounts and which point accounts
                        // are deducted based on the discount.
                        switch (discount.DiscountType)
                        {
                        case DiscountType.RewardsCash:
                            pointAccount            = shoppingCart.GetRewardsAccount(PointAccounts.HostRewardsCash);
                            discount.DiscountAmount = pointAccount.AmountRemaining;
                            break;

                        case DiscountType.RecruitingReward:
                            pointAccount            = shoppingCart.GetRewardsAccount(PointAccounts.RecruitingRewards);
                            discount.DiscountAmount = pointAccount.AmountRemaining;
                            break;

                        case DiscountType.EnrolleeReward:
                            pointAccount            = shoppingCart.GetRewardsAccount(PointAccounts.EnrolleeRewards);
                            discount.DiscountAmount = pointAccount.AmountRemaining;
                            break;

                        case DiscountType.HalfOffCredits:
                            pointAccount = shoppingCart.GetRewardsAccount(PointAccounts.Host12offcredits);
                            break;

                        //case DiscountType.BookingRewards:
                        //    pointAccount = shoppingCart.GetRewardsAccount(PointAccounts.BookingsRewardsCash);
                        //    discount.DiscountAmount = pointAccount.AmountRemaining;
                        //    break;
                        case DiscountType.HostSpecial:
                            // TODO: Look into why DI is broken on AJAX calls, seemingly.
                            var hostSpecialReward = new RewardService().GetHostSpecialReward(shoppingCart.EventId.Value);
                            // TODO: Need to validate that the discount exceeds the sales threshold in an IDiscountValidator
                            discount.DiscountAmount = hostSpecialReward.DiscountAmount;
                            break;

                        case DiscountType.Unknown:
                            break;

                        case DiscountType.Fixed:
                            break;

                        case DiscountType.Percent:
                            break;

                        case DiscountType.EBRewards:
                            break;

                        case DiscountType.SAHalfOff:
                            break;

                        case DiscountType.SAHalfOffOngoing:
                            break;

                        case DiscountType.NewProductsLaunchReward:
                            break;

                        case DiscountType.RetailPromoFixed:
                            break;

                        case DiscountType.RetailPromoPercent:
                            break;

                        case DiscountType.PromoCode:
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        if ((pointAccount == null && discount.DiscountAmount > 0M) || (pointAccount != null && pointAccount.AmountRemaining > 0M))
                        {
                            p.ApplyDiscount(discount);
                        }
                        else
                        {
                            // TODO: Need implement error handling at the client for JSON response
                            //ModelState.AddModelError("RewardsError", "The applied rewards amount exceeds the current balance.");
                        }
                    }
                }

                return(new JsonNetResult(new
                {
                    status = 200, cart = shoppingCart
                }));
            }
            catch (Exception)
            {
                return(new JsonNetResult(new
                {
                    status = 500, cart = shoppingCart
                }));
            }
        }
コード例 #10
0
        public async Task <IViewComponentResult> InvokeAsync(BookingProcess data)
        {
            List <Animal> animals = await GetAnimals(data.Animals.Select(e => e.ID).ToArray());

            List <Accessories> accessories = new List <Accessories>();
            List <Discounts>   discounts   = new List <Discounts>();

            DiscountValidator discountValidator = new DiscountValidator();

            if (data.Accessories != null)
            {
                accessories = await GetAccessories(data.Accessories.Select(e => e.ID).ToArray());
            }

            data.Animals     = animals;
            data.Accessories = accessories;

            #region Discount

            int totalDiscountPercentage = 0;
            if (discountValidator.HasThreeSameTypeAnimals(animals))
            {
                totalDiscountPercentage += 10;
                discounts.Add(new Discounts()
                {
                    Name     = "3 Types",
                    Discount = 10
                });
            }

            int duckDiscount = discountValidator.ChanceOnDuckDiscount(animals);

            if (duckDiscount > 0)
            {
                totalDiscountPercentage += duckDiscount;
                discounts.Add(new Discounts()
                {
                    Name     = "Eend",
                    Discount = duckDiscount
                }
                              );
            }

            int dateDiscount = discountValidator.BookingIsMondayOrTuesday(data.Booking.Date);
            if (dateDiscount > 0)
            {
                totalDiscountPercentage += dateDiscount;
                discounts.Add(new Discounts()
                {
                    Name     = data.Booking.Date.DayOfWeek.ToString(),
                    Discount = dateDiscount
                });
            }

            int alphabetDiscount = discountValidator.GetAlphabeticalDiscount(animals);
            if (alphabetDiscount > 0)
            {
                totalDiscountPercentage += alphabetDiscount;
                discounts.Add(new Discounts()
                {
                    Name     = "Alfabet",
                    Discount = alphabetDiscount
                });
            }

            totalDiscountPercentage = discountValidator.GetMaximumPercentage(totalDiscountPercentage, 60);
            data.TotalDiscount      = totalDiscountPercentage;

            #endregion

            #region TotalPrice

            double totalPrice = 0.0;
            foreach (Animal animal in animals)
            {
                totalPrice += animal.Price;
            }

            foreach (Accessories accessoriese in accessories)
            {
                totalPrice += accessoriese.Price;
            }

            totalPrice = totalPrice / 100 * (100 - totalDiscountPercentage);

            #endregion

            data.TotalPrice    = totalPrice;
            data.TotalDiscount = totalDiscountPercentage;
            data.Discounts     = discounts;

            #region SaveData

            await _clientInfoRepository.Create(data.ClientInfo);

            data.ClientInfoId = data.ClientInfo.ID;
            data.DateTime     = data.Booking.Date;

            data.BookingProcessAnimals     = new List <BookingProcessAnimal>();
            data.BookingProcessAccessories = new List <BookingProcessAccessories>();
            foreach (Animal animal in animals)
            {
                data.BookingProcessAnimals.Add(new BookingProcessAnimal()
                {
                    BookingProcessId = data.ID,
                    AnimalId         = animal.ID
                });
            }

            foreach (Accessories a in accessories)
            {
                data.BookingProcessAccessories.Add(new BookingProcessAccessories()
                {
                    BookingProcessId = data.ID,
                    AccessoriesId    = a.ID
                });
            }

            await _bookingProcessRepository.Create(data);

            #endregion

            return(View(data));
        }