Esempio n. 1
0
        public async Task <List <CalendarItem> > CreateRecurrenceCalendarItemsAsync(CalendarItem item, DateTime start)
        {
            var calendarItems = new List <CalendarItem>();

            if (!item.RecurrenceEnd.HasValue || item.RecurrenceEnd > DateTime.UtcNow && item.Start >= start)
            {
                var department = await _departmentsService.GetDepartmentByIdAsync(item.DepartmentId, false);

                var      currentTime        = item.Start.TimeConverter(department);
                DateTime startTimeConverted = item.Start.TimeConverter(department);

                var length = item.GetDifferenceBetweenStartAndEnd();

                if (item.RecurrenceType == (int)RecurrenceTypes.Weekly)
                {
                    DateTime weekWorkingDate = currentTime;
                    int      weekProcssed    = 1;

                    var nextWeek = GetNextWeekValues(item, weekWorkingDate);
                    while (weekProcssed <= 52)
                    {
                        foreach (var day in nextWeek)
                        {
                            if (day != null)
                            {
                                if (calendarItems.All(x => x.Start.Date != day.Value.Date))
                                {
                                    var startDate = DateTimeHelpers.GetLocalDateTime(new DateTime(day.Value.Year, day.Value.Month, day.Value.Day,
                                                                                                  startTimeConverted.Hour, startTimeConverted.Minute, startTimeConverted.Second), department.TimeZone);

                                    var endDate = startDate.Add(length);

                                    calendarItems.Add(item.CreateRecurranceItem(startDate, endDate, department.TimeZone));
                                }
                            }
                        }

                        weekWorkingDate = weekWorkingDate.AddDays(7);
                        nextWeek        = GetNextWeekValues(item, weekWorkingDate);
                        weekProcssed++;
                    }
                }
                else if (item.RecurrenceType == (int)RecurrenceTypes.Monthly && item.RepeatOnDay == 0)
                {                 // This is a repeat on every 15th of the month
                    DateTime monthWorkingDate = currentTime;
                    int      monthProcessed   = 1;

                    while (monthProcessed <= 12)
                    {
                        var monthDate = monthWorkingDate.AddMonths(monthProcessed);

                        var startDate = DateTimeHelpers.GetLocalDateTime(new DateTime(monthDate.Year, monthDate.Month, item.RepeatOnMonth,
                                                                                      startTimeConverted.Hour, startTimeConverted.Minute, startTimeConverted.Second), department.TimeZone);

                        var endDate = startDate.Add(length);

                        calendarItems.Add(item.CreateRecurranceItem(startDate, endDate, department.TimeZone));

                        monthProcessed++;
                    }
                }
                else if (item.RecurrenceType == (int)RecurrenceTypes.Monthly && item.RepeatOnDay > 0)
                {                 // This is a repeat on every nth day of the month
                    DateTime monthWorkingDate = currentTime;
                    int      monthProcessed   = 1;

                    while (monthProcessed <= 12)
                    {
                        var monthDate = monthWorkingDate.AddMonths(monthProcessed);

                        var startDateDay = DateTimeHelpers.FindDay(monthDate.Year, monthDate.Month, (DayOfWeek)item.RepeatOnDay,
                                                                   item.RepeatOnMonth);

                        var startDate = DateTimeHelpers.GetLocalDateTime(new DateTime(monthDate.Year, monthDate.Month, startDateDay,
                                                                                      startTimeConverted.Hour, startTimeConverted.Minute, startTimeConverted.Second), department.TimeZone);

                        var endDate = startDate.Add(length);

                        calendarItems.Add(item.CreateRecurranceItem(startDate, endDate, department.TimeZone));

                        monthProcessed++;
                    }
                }
                else if (item.RecurrenceType == (int)RecurrenceTypes.Yearly)
                {
                    DateTime yearlyWorkingDate = currentTime;
                    yearlyWorkingDate = yearlyWorkingDate.AddYears(1);

                    var startDate = DateTimeHelpers.GetLocalDateTime(new DateTime(yearlyWorkingDate.Year, startTimeConverted.Month, startTimeConverted.Day,
                                                                                  startTimeConverted.Hour, startTimeConverted.Minute, startTimeConverted.Second), department.TimeZone);

                    var endDate = startDate.Add(length);

                    calendarItems.Add(item.CreateRecurranceItem(startDate, endDate, department.TimeZone));
                }
            }

            return(calendarItems);
        }