Esempio n. 1
0
        public IActionResult ListProjectWeeks(int?ProjectID, string FromWeekID = "", string ToWeekID = "")
        {
            if (ProjectID == null)
            {
                return(NotFound());
            }
            // TODO add regex check on from and to week id

            var weeks        = _context.Week.Where(e => e.ProjectID == ProjectID);
            var weekComparer = new CompareWeekModel();
            var WeekIDRegex  = new Regex(@"^\d\d-\d\d\d\d$");

            if (FromWeekID != null && FromWeekID != "" && WeekIDRegex.IsMatch(FromWeekID)) // FromWeekID is valid
            {
                if (ToWeekID != null && ToWeekID != "" && WeekIDRegex.IsMatch(ToWeekID))   // FromWeekID and ToWeekID is valid
                {
                }
                else // FromWeekID is valid and ToWeekID is invalid
                {
                    var fromDate = WeekModel.GetMondayFromWeekID(FromWeekID);
                    ToWeekID = WeekModel.GetIso8601WeekOfYear(fromDate.AddDays(21)) + "-" + DateTime.Now.Year;
                }
            }
            else // FromWeekID is invalid
            {
                if (ToWeekID != null && ToWeekID != "" && WeekIDRegex.IsMatch(ToWeekID)) // FromWeekID is invalid and ToWeekID is valid
                {
                    var toDate = WeekModel.GetMondayFromWeekID(ToWeekID);
                    FromWeekID = WeekModel.GetIso8601WeekOfYear(toDate.AddDays(-21)) + "-" + DateTime.Now.Year;
                }
                else // FromWeekID is invalid and ToWeekID is invalid
                {
                    FromWeekID = WeekModel.GetIso8601WeekOfYear(DateTime.Now) + "-" + DateTime.Now.Year;
                    ToWeekID   = WeekModel.GetIso8601WeekOfYear(DateTime.Now.AddDays(21)) + "-" + DateTime.Now.Year;
                }
            }
            weeks = weeks.Where(e => weekComparer.CompareWeekIDs(e.WeekID, ToWeekID) <= 0 && weekComparer.CompareWeekIDs(e.WeekID, FromWeekID) >= 0);

            var weekList = weeks.ToList();

            weekList.Sort(new CompareWeekModel());
            var weeksView = new List <DetailsWeekViewModel>();

            foreach (var week in weekList)
            {
                weeksView.Add(_WeeksRepository.PrepareWeekViewModel(_context, week));
            }
            var ListProjectWeekViewModel = new ListProjectWeekViewModel
            {
                ProjectID      = ProjectID.Value,
                FromWeekID     = FromWeekID,
                ToWeekID       = ToWeekID,
                WeekViewModels = weeksView
            };

            return(View(ListProjectWeekViewModel));
        }
Esempio n. 2
0
        public async Task <IActionResult> Create(CreateWeekViewModel createWeekViewModel)
        {
            // with this call, a week can be created from scratch or with some slots already referencing this week
            if (ModelState.IsValid)
            {
                var WeekIDRegex = new Regex(@"^\d\d-\d\d\d\d$");
                if (!WeekIDRegex.IsMatch(createWeekViewModel.WeekID))
                {
                    return(BadRequest());
                }
                var WeekIDInt = Int32.Parse(createWeekViewModel.WeekID.Substring(0, 2));
                if (WeekIDInt <= 0 || WeekIDInt > 52)
                {
                    return(BadRequest());
                }
                if (WeekExists(createWeekViewModel.ProjectID, createWeekViewModel.WeekID) ||
                    !_context.Project.Any(e => e.ProjectID == createWeekViewModel.ProjectID))
                {
                    return(BadRequest());
                }

                var projectID         = createWeekViewModel.ProjectID;
                var weekID            = createWeekViewModel.WeekID;
                var weekSlots         = _context.Slot.AsNoTracking().Where(e => e.ProjectID == projectID && e.WeekID == weekID).ToList();
                var weekTemplateSlots = _context.Slot.AsNoTracking().Where(e => e.ProjectID == projectID && e.WeekID == createWeekViewModel.WeekTemplateID).ToList();
                var activities        = _context.Activity.ToList();

                var newSlots = new List <SlotModel>();
                foreach (var weekTemplateSlot in weekTemplateSlots.Where(e => !weekSlots.Exists(d => d.ProjectID == e.ProjectID && d.ActivityID == e.ActivityID && d.EmployeeID == e.EmployeeID)))
                {
                    if (!activities.Single(e => e.ActivityID == weekTemplateSlot.ActivityID).Name.Contains("public holiday")) // do not clone public holidays
                    {
                        var dubletCount = newSlots.Where(e => e.ProjectID == projectID && e.WeekID == weekID && e.ActivityID == weekTemplateSlot.ActivityID).Count();
                        var employeeID  = "_" + (dubletCount + 1);
                        newSlots.Add(new SlotModel {
                            ProjectID = projectID, WeekID = weekID, ActivityID = weekTemplateSlot.ActivityID, EmployeeID = employeeID
                        });
                    }
                }
                if (!_context.Week.Any(e => e.WeekID.Substring(3, 4).Equals(weekID.Substring(3, 4)) && e.ProjectID == projectID)) // any week with a new year in its id?
                {
                    _WeeksRepository.WeightDecay(_context.ActivityRecord.ToList());                                               // divide all weights by two every time a new year of work is recorded
                }
                await _context.Slot.AddRangeAsync(newSlots);

                await _context.Week.AddAsync(new WeekModel
                {
                    ProjectID         = projectID,
                    WeekID            = weekID,
                    FirstDayOfTheWeek = WeekModel.GetMondayFromWeekID(createWeekViewModel.WeekID),
                    Notes1Monday      = createWeekViewModel.Notes1Monday,
                    Notes2Tuesday     = createWeekViewModel.Notes2Tuesday,
                    Notes3Wednesday   = createWeekViewModel.Notes3Wednesday,
                    Notes4Thursday    = createWeekViewModel.Notes4Thursday,
                    Notes5Friday      = createWeekViewModel.Notes5Friday,
                    Notes6Saturday    = createWeekViewModel.Notes6Saturday,
                    Notes7Sunday      = createWeekViewModel.Notes7Sunday,
                });

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Edit), new { projectID, weekID }));
            }
            return(View(createWeekViewModel));
        }
Esempio n. 3
0
        public List <WeekModel> CreateRangeOfWeeks(
            ProjectModel project,
            WeekModel weekTemplate,
            string fromWeekID,
            string toWeekID,
            string employeeIDForLastSundayNight,
            List <SlotModel> templateSlots,
            List <ActivityModel> activities,
            List <TagModel> tags,
            List <EmployeeModel> employeesInProject,
            List <ActivityRecordModel> activityRecordsForEmployeesInProject,
            int numOfCreatedWeeksInThisYear,
            List <SlotModel> slotsInYear,
            out List <SlotModel> newWeeksSlots)
        {
            var EmployeesInProjectInfoInYear = new List <EmployeeInfo>();
            var FromWeekNumber = Int32.Parse(fromWeekID.Substring(0, 2));
            var ToWeekNumber   = Int32.Parse(toWeekID.Substring(0, 2));
            var Year           = fromWeekID.Substring(3, 4);
            var range          = ToWeekNumber - FromWeekNumber;
            var newWeeks       = new List <WeekModel>();

            newWeeksSlots = new List <SlotModel>();

            foreach (var employeeInProject in employeesInProject)
            {
                var weeksSum      = 0.0;                                                                   // sum from all other weeks in this year on this employee
                var weeksExpected = employeeInProject.WeeklyWorkHours * (numOfCreatedWeeksInThisYear + 1); // +1 because we havent created the first week yet
                foreach (var employeeSlotsInYear in slotsInYear.Where(e => e.EmployeeID == employeeInProject.EmployeeID && !e.WeekID.Equals(fromWeekID)))
                {
                    var activity = activities.Single(e => e.ActivityID == employeeSlotsInYear.ActivityID);
                    weeksSum += activity.Hours;
                }
                EmployeesInProjectInfoInYear.Add(new EmployeeInfo
                {
                    Employee        = employeeInProject,
                    ActivityRecords = activityRecordsForEmployeesInProject.Where(e => e.EmployeeID == employeeInProject.EmployeeID).ToList(),
                    WorkHourStats   = new WorkHourStats
                    {
                        WeeksSum      = weeksSum,
                        WeekExpected  = weeksExpected - weeksSum,
                        WeeksExpected = weeksExpected,
                    }
                });
            }

            for (int i = 0; i <= range; i++) // each week
            {
                var newWeekNumber       = FromWeekNumber + i;
                var newWeekNumberString = newWeekNumber < 10 ? "0" + newWeekNumber : "" + newWeekNumber;
                var newWeekID           = newWeekNumberString + "-" + Year;
                var week = new WeekModel {
                    ProjectID = project.ProjectID, WeekID = newWeekID, FirstDayOfTheWeek = WeekModel.GetMondayFromWeekID(newWeekID)
                };
                var weekSlots = new List <SlotModel>();
                foreach (var templateSlot in templateSlots)
                {
                    var activity       = activities.Single(e => e.ActivityID == templateSlot.ActivityID);
                    var tagsInActivity = tags.Where(e => e.ActivityID == activity.ActivityID);
                    var holidayTag     = tagsInActivity.SingleOrDefault(e => e.TagID.Contains("public holiday"));
                    if (holidayTag == null) // do not clone public holidays
                    {
                        var dubletCount = weekSlots.Where(e => e.ProjectID == week.ProjectID && e.WeekID == week.WeekID && e.ActivityID == templateSlot.ActivityID).Count();
                        var employeeID  = "_" + (dubletCount + 1);
                        weekSlots.Add(new SlotModel {
                            ProjectID = week.ProjectID, WeekID = week.WeekID, ActivityID = templateSlot.ActivityID, EmployeeID = employeeID
                        });
                    }
                }
                newWeeks.Add(week);
                List <SlotModel> newWeekSlots = CalculateWeek(project, week, employeeIDForLastSundayNight, weekSlots, activities, EmployeesInProjectInfoInYear);
                newWeeksSlots.AddRange(newWeekSlots);

                // Add WeeksSum and WeeksExpected and recalc WeekExpected in WorkHourStats for each employee in EmployeeInProjectInfoInYear
                foreach (var EmployeeInProjectInfoInYear in EmployeesInProjectInfoInYear)
                {
                    var WHS = EmployeeInProjectInfoInYear.WorkHourStats;
                    WHS.WeeksExpected += EmployeeInProjectInfoInYear.Employee.WeeklyWorkHours; // just add another week
                    WHS.WeekExpected   = WHS.WeeksExpected - WHS.WeeksSum;                     // recalc after another week was added
                }

                var sundayNightActivity = activities.Single(e => e.DayOfWeek == DaySlot.SUNDAY && e.TimeOfDay == TimeSlot.NIGHT);
                employeeIDForLastSundayNight = newWeekSlots.Single(e => e.ActivityID == sundayNightActivity.ActivityID).EmployeeID;
            }
            return(newWeeks);
        }