示例#1
0
        public void TestGenericShift()
        {
            // regular work week with holidays and breaks
            schedule = new WorkSchedule("Regular 40 hour work week", "9 to 5");

            // holidays
            NonWorkingPeriod memorialDay = schedule.CreateNonWorkingPeriod("MEMORIAL DAY", "Memorial day", new LocalDateTime(2016, 5, 30, 0, 0, 0),
                                                                           Duration.FromHours(24));

            schedule.CreateNonWorkingPeriod("INDEPENDENCE DAY", "Independence day", new LocalDateTime(2016, 7, 4, 0, 0, 0),
                                            Duration.FromHours(24));
            schedule.CreateNonWorkingPeriod("LABOR DAY", "Labor day", new LocalDateTime(2016, 9, 5, 0, 0, 0),
                                            Duration.FromHours(24));
            schedule.CreateNonWorkingPeriod("THANKSGIVING", "Thanksgiving day and day after",
                                            new LocalDateTime(2016, 11, 24, 0, 0, 0), Duration.FromHours(48));
            schedule.CreateNonWorkingPeriod("CHRISTMAS SHUTDOWN", "Christmas week scheduled maintenance",
                                            new LocalDateTime(2016, 12, 25, 0, 30, 0), Duration.FromHours(168));

            // each shift duration
            Duration  shiftDuration = Duration.FromHours(8);
            LocalTime shift1Start   = new LocalTime(7, 0, 0);
            LocalTime shift2Start   = new LocalTime(15, 0, 0);

            // shift 1
            Shift shift1 = schedule.CreateShift("Shift1", "Shift #1", shift1Start, shiftDuration);

            // breaks
            shift1.CreateBreak("10AM", "10 am break", new LocalTime(10, 0, 0), Duration.FromMinutes(15));
            shift1.CreateBreak("LUNCH", "lunch", new LocalTime(12, 0, 0), Duration.FromHours(1));
            shift1.CreateBreak("2PM", "2 pm break", new LocalTime(14, 0, 0), Duration.FromMinutes(15));

            // shift 2
            Shift shift2 = schedule.CreateShift("Shift2", "Shift #2", shift2Start, shiftDuration);

            // shift 1, 5 days ON, 2 OFF
            Rotation rotation1 = new Rotation("Shift1", "Shift1");

            rotation1.AddSegment(shift1, 5, 2);

            // shift 2, 5 days ON, 2 OFF
            Rotation rotation2 = new Rotation("Shift2", "Shift2");

            rotation2.AddSegment(shift2, 5, 2);

            LocalDate startRotation = new LocalDate(2016, 1, 1);
            Team      team1         = schedule.CreateTeam("Team1", "Team #1", rotation1, startRotation);
            Team      team2         = schedule.CreateTeam("Team2", "Team #2", rotation2, startRotation);

            // same day
            LocalDateTime from = startRotation.PlusDays(7).At(shift1Start);
            LocalDateTime to;

            Duration totalWorking = Duration.Zero;

            // 21 days, team1
            Duration d = Duration.Zero;

            for (int i = 0; i < 21; i++)
            {
                to           = from.PlusDays(i);
                totalWorking = team1.CalculateWorkingTime(from, to);
                int dir = team1.GetDayInRotation(to.Date);

                Assert.IsTrue(totalWorking.Equals(d));

                TimePeriod period = rotation1.GetPeriods()[dir - 1];
                if (period is Shift)
                {
                    d = d.Plus(shiftDuration);
                }
            }
            Duration totalSchedule = totalWorking;

            // 21 days, team2
            from = startRotation.PlusDays(7).At(shift2Start);
            d    = Duration.Zero;

            for (int i = 0; i < 21; i++)
            {
                to           = from.PlusDays(i);
                totalWorking = team2.CalculateWorkingTime(from, to);
                int dir = team2.GetDayInRotation(to.Date);

                Assert.IsTrue(totalWorking.Equals(d));

                if (rotation1.GetPeriods()[dir - 1] is Shift)
                {
                    d = d.Plus(shiftDuration);
                }
            }
            totalSchedule = totalSchedule.Plus(totalWorking);

            Duration scheduleDuration   = schedule.CalculateWorkingTime(from, from.PlusDays(21));
            Duration nonWorkingDuration = schedule.CalculateNonWorkingTime(from, from.PlusDays(21));

            Assert.IsTrue(scheduleDuration.Plus(nonWorkingDuration).Equals(totalSchedule));

            // breaks
            Duration allBreaks = Duration.FromMinutes(90);

            Assert.IsTrue(shift1.CalculateBreakTime().Equals(allBreaks));

            // misc
            WorkSchedule schedule2 = new WorkSchedule();

            Shift shift3 = new Shift();

            shift3.Name = "Shift3";
            Assert.IsTrue(shift3.WorkSchedule == null);
            Assert.IsTrue(shift3.CompareTo(shift3) == 0);

            Team team3 = new Team();

            Assert.IsTrue(team3.WorkSchedule == null);

            RotationSegment segment = new RotationSegment();

            segment.Sequence      = 1;
            segment.StartingShift = shift2;
            segment.DaysOn        = 5;
            segment.DaysOff       = 2;
            Assert.IsTrue(segment.Rotation == null);

            Rotation rotation3 = new Rotation();

            rotation3.Name = "Rotation3";
            Assert.IsTrue(rotation3.CompareTo(rotation3) == 0);
            Assert.IsTrue(rotation3.RotationSegments.Count == 0);

            NonWorkingPeriod nwp = new NonWorkingPeriod();

            Assert.IsTrue(nwp.WorkSchedule == null);


            Assert.IsTrue(team1.WorkSchedule.Equals(schedule));


            Assert.IsTrue(!team1.IsDayOff(startRotation));


            Assert.IsTrue(team1.CompareTo(team1) == 0);
            team3.Rotation = rotation1;


            Assert.IsTrue(!memorialDay.IsInPeriod(new LocalDate(2016, 1, 1)));

            runBaseTest(schedule, Duration.FromHours(40), Duration.FromDays(7), new LocalDate(2016, 1, 1));
        }