Example #1
0
            private static bool IsPeriodActiveForDayAndTime(ValidationPeriod period, DayOfWeek day, TimeSpan time, bool testOnlyAfterMidnight)
            {
                var configuration = new PeriodRangeConfiguration
                {
                    StartTime                = period.StartingTimeForDay(day),
                    EndTime                  = period.EndingTimeForDay(day),
                    EndsTomorrow             = period.IsEndTimeAfterMidnightForDay(day),
                    IsActiveOnlyWithinBounds = period.IsTimeBoundedForDay(day),
                };

                int currentTime = Convert.ToInt32(time.TotalSeconds);

                return(IsTimeActiveForConfiguration(currentTime, configuration, testOnlyAfterMidnight));
            }
Example #2
0
            private static bool IsValidationPeriodActive(ValidationPeriod validationPeriod, DateTimeOffset transDateTime)
            {
                if (validationPeriod == null || string.IsNullOrEmpty(validationPeriod.PeriodId))
                {
                    // If no period Id given, then it is always a valid period
                    return(true);
                }

                DateTime transDate = transDateTime.Date;
                TimeSpan transTime = transDateTime.TimeOfDay;

                // Is the discount valid within the start and end date period?
                if (InternalValidationPeriod.IsDateWithinStartEndDate(transDate, validationPeriod.ValidFrom.Date, validationPeriod.ValidTo.Date))
                {
                    bool answerFound = false;
                    bool isActive    = false;

                    // does today's configuration tell if period is active?
                    if (IsRangeDefinedForDay(validationPeriod, transDate.DayOfWeek))
                    {
                        isActive    = IsPeriodActiveForDayAndTime(validationPeriod, transDate.DayOfWeek, transTime, false);
                        answerFound = true;
                    }

                    // if we don't know or got negative result, see if yesterday will activate it (if its range ends after midnight)
                    DayOfWeek yesterday = transDate.AddDays(-1).DayOfWeek;
                    bool      lastRangeDefinedAfterMidnight =
                        IsRangeDefinedForDay(validationPeriod, yesterday) && validationPeriod.IsEndTimeAfterMidnightForDay(yesterday);

                    if ((!answerFound || isActive == false) && lastRangeDefinedAfterMidnight)
                    {
                        // if yesterday makes it active, set isActive = true
                        isActive    = IsPeriodActiveForDayAndTime(validationPeriod, yesterday, transTime, true);
                        answerFound = true;
                    }

                    // if we still don't know, try using general configuration
                    if (!answerFound)
                    {
                        var configuration = new PeriodRangeConfiguration
                        {
                            StartTime = validationPeriod.StartingTime,
                            EndTime   = validationPeriod.EndingTime,
                            IsActiveOnlyWithinBounds = validationPeriod.IsTimeBounded != 0,
                            EndsTomorrow             = validationPeriod.IsEndTimeAfterMidnight != 0
                        };

                        if ((validationPeriod.StartingTime != 0) && (validationPeriod.EndingTime != 0))
                        {
                            int currentTime = Convert.ToInt32(transTime.TotalSeconds);
                            isActive    = IsTimeActiveForConfiguration(currentTime, configuration, false);
                            answerFound = true;
                        }
                    }

                    return(answerFound ? isActive : (validationPeriod.IsTimeBounded == 1));
                }

                // not within date range, so active if not set to be within date range
                return(validationPeriod.IsTimeBounded != 1);
            }