//The main method for generating shifts public void ScheduleGenerator(int weeknumber, int year) { // Instead of retreiving a reference to the lists from core multiple times // The method saves the reference for further use List <User> AllUsers = Core.Instance.GetAllUsers(); List <ShiftTemplate> AllShiftTemplates = Core.Instance.GetAllTemplates(); List <Holiday> AllHolidays = Core.Instance.GetAllHolidays(); //The templates are sorted so that the generetor generates from monday to sunday var SortedShiftTemplates = AllShiftTemplates.OrderBy(template => template.StartTime); AllShiftTemplates = SortedShiftTemplates.ToList(); // Since it is the beginning of a new week to generate for all users WorkInWeek is set to 0 foreach (User u in AllUsers) { u.WorkInWeek = 0; } // Local variables for further use int PossitiveDayCost = 0; int NegativeDayCost = 0; int resultDay = 0; int resultMonth = 0; // The loop which generates a Shift object for each template in the list of templates int TemplateCount = AllShiftTemplates.Count; for (int i = 0; i <= TemplateCount - 1; i++) { // The year startingdate is found by makeing a date with january the 1st for the selected year DateTime YearStartingDate = new DateTime(year, 1, 1); // The first day is calculated int FirstDayInYear = CalcFirstDayInYear(YearStartingDate); if (weeknumber == 1) { //If the date is one of the days not exsisting in the first week of the year, the loop continues to the next template if (AllShiftTemplates[i].StartTime.Day <= FirstDayInYear) { continue; } } int DayInYear = GetDayInYear(weeknumber, AllShiftTemplates[i].StartTime, FirstDayInYear); TotalDayToDayInMonth(DayInYear, year, ref resultDay, ref resultMonth); DateTime start = new DateTime(year, resultMonth, resultDay, AllShiftTemplates[i].StartTime.Hour, AllShiftTemplates[i].StartTime.Minute, AllShiftTemplates[i].StartTime.Second); DateTime end = new DateTime(year, resultMonth, resultDay, AllShiftTemplates[i].EndTime.Hour, AllShiftTemplates[i].EndTime.Minute, AllShiftTemplates[i].EndTime.Second); //If the date is a holiday the loop continues to next iteration if (IfDateIsNotHoliday(start, AllHolidays)) { // Sorting of Users. The compatible Users are found and saved in a list List <User> CompatibleUsers = new List <User>(); GetCompatibleUsers(AllUsers, AllShiftTemplates[i], CompatibleUsers); CalculateDayPrice(AllShiftTemplates[i].StartTime.Day, ref PossitiveDayCost, ref NegativeDayCost, false); // The SortUserList method determines the user to take the Shift based on different parameters string UserName = SortUserList(CompatibleUsers, PossitiveDayCost, NegativeDayCost, start); // The shift is created and saved to the Core and to the Database Shift resultShift = new Shift(start, end, Database.Instance.ListToString(AllShiftTemplates[i].Tag), UserName, weeknumber); resultShift.SaveShift(); Core.Instance.AddShiftToList(resultShift); } else { continue; } } }
public void AddShiftToList(Shift shift) { _allShifts.Add(shift); }