Esempio n. 1
0
        public virtual ValidationMessageCollection ValidateEdit(EditParameterSet parameterSet)
        {
            var  validationMessages            = new ValidationMessageCollection();
            bool overlappingPricingPeriods     = false;
            bool daysUnmatchedByPricingPeriods = false;

            var newPricingPeriods = parameterSet.PricingPeriods.Select(pricingPeriod => PricingPeriod.Add(new AddEditParameterSet
            {
                StartMonth = pricingPeriod.StartMonth,
                StartDay   = pricingPeriod.StartDay,
                EndMonth   = pricingPeriod.EndMonth,
                EndDay     = pricingPeriod.EndDay
            }));

            for (var date = new DateTime(2000, 1, 1); date < new DateTime(2001, 1, 1); date = date.AddDays(1))
            {
                var matchingPeriods = newPricingPeriods.Count(pricingPeriod => pricingPeriod.ContainsDate(date));

                if (matchingPeriods == 0)
                {
                    daysUnmatchedByPricingPeriods = true;
                }

                if (matchingPeriods > 1)
                {
                    overlappingPricingPeriods = true;
                }
            }

            if (daysUnmatchedByPricingPeriods)
            {
                validationMessages.AddError("Some days are not covered by pricing periods.");
            }

            if (overlappingPricingPeriods)
            {
                validationMessages.AddError("Some days are covered by more than one pricing period");
            }

            var firstPricingPeriod = newPricingPeriods
                                     .OrderBy(x => x.StartMonth)
                                     .ThenBy(x => x.StartDay)
                                     .First();

            var lastPricingPeriod = newPricingPeriods
                                    .OrderBy(x => x.StartMonth)
                                    .ThenBy(x => x.StartDay)
                                    .Last();

            if (firstPricingPeriod.StartMonth != 1 || firstPricingPeriod.StartDay != 1)
            {
                validationMessages.AddError("There must be a pricing period beginning January 1st");
            }

            if (lastPricingPeriod.EndMonth != 12 || lastPricingPeriod.EndDay != 31)
            {
                validationMessages.AddError("There must be a pricing period ending December 31st");
            }

            return(validationMessages);
        }