예제 #1
0
        private async Task <IEnumerable <string> > VerifyHourEntries(FullTimeEntryViewModel vm)
        {
            var issues = new List <String>();

            if (vm.TimeEntryRow != null)
            {
                if (vm.TimeEntryRow.Any(z => z.AllDays().Any(x => x.Hours < 0 || x.OvertimeHours < 0)))
                {
                    issues.Add("Hours must be a non-negative number.");
                }
                if (vm.TimeEntryRow.Any(z => z.AllDays().Any(x => x.Hours + x.OvertimeHours > 24)))
                {
                    issues.Add("Regular + Overtime must never exceed 24 hours.");
                }
                if (vm.TimeEntryRow.Sum(x => x.AllDays().Sum(z => z.Hours)) > 40)
                {
                    issues.Add("Regular hours must never exceed 40 hours");
                }


                var entriesByJob = vm.TimeEntryRow.Select(te => new { jobId = te.SelectedJobId.Value, hasTime = te.AllDays().Any(h => h.Hours > 0 || h.OvertimeHours > 0) });
                var grouped      = entriesByJob.GroupBy(x => x.jobId);
                foreach (var jobGroup in grouped)
                {
                    var job = await _jobsRepository.GetForJobId(jobGroup.Key);

                    if (job.CoreInfo.JobStatusId != JobStatus.Enabled)
                    {
                        issues.Add($"Job {job.CoreInfo.FullJobCodeWithName} has been closed, either remove this entry, or have an administrator re-open the job");
                    }
                }
            }
            return(issues);
        }
예제 #2
0
        private WeekOfTimeDTO ToDTO(int employeeId, int weekId, FullTimeEntryViewModel vm)
        {
            var w = WeekDTO.CreateForWeekId(weekId);

            return(new WeekOfTimeDTO()
            {
                WeekId = weekId,
                Monday = ToDaysEntries(employeeId, weekId, w.GetDateFor(DayOfWeek.Monday), vm.TimeEntryRow, x => x.Monday),
                Tuesday = ToDaysEntries(employeeId, weekId, w.GetDateFor(DayOfWeek.Tuesday), vm.TimeEntryRow, x => x.Tuesday),
                Wednesday = ToDaysEntries(employeeId, weekId, w.GetDateFor(DayOfWeek.Wednesday), vm.TimeEntryRow, x => x.Wednesday),
                Thursday = ToDaysEntries(employeeId, weekId, w.GetDateFor(DayOfWeek.Thursday), vm.TimeEntryRow, x => x.Thursday),
                Friday = ToDaysEntries(employeeId, weekId, w.GetDateFor(DayOfWeek.Friday), vm.TimeEntryRow, x => x.Friday),
                Saturday = ToDaysEntries(employeeId, weekId, w.GetDateFor(DayOfWeek.Saturday), vm.TimeEntryRow, x => x.Saturday),
                Sunday = ToDaysEntries(employeeId, weekId, w.GetDateFor(DayOfWeek.Sunday), vm.TimeEntryRow, x => x.Sunday)
            });
        }
예제 #3
0
        public async Task <Result> SaveTimeEntriesAsync(int employeeId, int weekId, FullTimeEntryViewModel vm)
        {
            var statusAllowsSave = await GetSaveStatus(employeeId, weekId);

            if (!statusAllowsSave)
            {
                return(new Result(false, "Current status does not allow saving"));
            }
            var hoursIssues = (await VerifyHourEntries(vm)).ToList();

            if (hoursIssues.Any())
            {
                hoursIssues.Add(" -- NO CHANGES WHERE SAVED -- ");
                return(new Result(false, hoursIssues.ToArray()));
            }

            if (vm.TimeEntryRow != null)
            {
                await _timeService.SaveWeekAsync(employeeId, ToDTO(employeeId, weekId, vm));
            }
            return(new Result(true));
        }
예제 #4
0
        private async Task <ActionResult> ReloadPageForErrorCorrection(int weekId, int employeeId, FullTimeEntryViewModel vm, Result res)
        {
            var req = new WeekOfTimeEntriesRequest()
            {
                EmployeeId            = employeeId,
                RequestingUserIsAdmin = User.IsInRole(UserRoleName.Admin),
                RequestingUserName    = User.Identity.Name,
                WeekId = weekId
            };
            var vmDefault = await weekOfTimeEntriesQuery.GetFullTimeEntryViewModelAsync(req);

            foreach (var day in vmDefault.TimeEntryRow)
            {
                var match = vm.TimeEntryRow.Single(x => x.RowId == day.RowId);

                day.Monday.Hours    = match.Monday.Hours;
                day.Tuesday.Hours   = match.Tuesday.Hours;
                day.Wednesday.Hours = match.Wednesday.Hours;
                day.Thursday.Hours  = match.Thursday.Hours;
                day.Friday.Hours    = match.Friday.Hours;
                day.Saturday.Hours  = match.Saturday.Hours;
                day.Sunday.Hours    = match.Sunday.Hours;

                day.Monday.OvertimeHours    = match.Monday.OvertimeHours;
                day.Tuesday.OvertimeHours   = match.Tuesday.OvertimeHours;
                day.Wednesday.OvertimeHours = match.Wednesday.OvertimeHours;
                day.Thursday.OvertimeHours  = match.Thursday.OvertimeHours;
                day.Friday.OvertimeHours    = match.Friday.OvertimeHours;
                day.Saturday.OvertimeHours  = match.Saturday.OvertimeHours;
                day.Sunday.OvertimeHours    = match.Sunday.OvertimeHours;
            }
            NotificationsController.AddNotification(User.SafeUserName(), $"Timesheet was not saved {string.Join("<br />", res.Errors)}");
            ModelState.Clear();
            foreach (var err in res.Errors)
            {
                ModelState.AddModelError("", err);
            }
            return(View("Week", vmDefault));
        }
예제 #5
0
        public async Task <ActionResult> Save(int weekId, int employeeId, FullTimeEntryViewModel vm, string postType)
        {
            var currentUserId = await sessionAdapter.EmployeeIdAsync();

            if (!User.IsInRole(UserRoleName.Admin) && employeeId != currentUserId)
            {
                return(RedirectToAction("Index", new { weekId }));
            }

            if (postType == "Save" || postType == "Add Task" || postType == "Submit")
            {
                var res = await saveTimeEntriesCommand.SaveTimeEntriesAsync(employeeId, weekId, vm);

                if (res.Successful)
                {
                    NotificationsController.AddNotification(User.SafeUserName(), "Timesheet has been saved");
                }
                else
                {
                    return(await ReloadPageForErrorCorrection(weekId, employeeId, vm, res));
                }
            }

            if (postType == "Add Task")
            {
                var AddResult = await addNewJobTaskComboCommand.AddNewJobTaskCombo(employeeId, weekId, vm.NewEntry.SelectedTaskId ?? 0, vm.NewEntry.SelectedJobId ?? 0);

                if (AddResult.Successful)
                {
                    NotificationsController.AddNotification(User.SafeUserName(), "The selected task has been added.");
                }
                else
                {
                    NotificationsController.AddNotification(User.SafeUserName(), "Select task could not be added.");
                    return(await ReloadPageForErrorCorrection(weekId, employeeId, vm, AddResult));
                }
            }

            if (postType == "Copy Job/Tasks From Previous Week")
            {
                await copyPreviousWeekTimeCommand.CopyPreviousWeekTime(employeeId, weekId);
            }

            if (postType == "Submit")
            {
                var req = new TimeApprovalRequest(
                    approvingUserId: await sessionAdapter.EmployeeIdAsync(),
                    approvingUserIsAdmin: User.IsInRole(UserRoleName.Admin),
                    employeeId: employeeId,
                    newApprovalState: TimeApprovalStatus.Submitted,
                    weekId: weekId
                    );
                var res = await approveTimeCommand.ApplyApproval(req);

                NotificationsController.AddNotification(User.SafeUserName(), $"Timesheet is {TimeApprovalStatus.Submitted}");
            }

            if (postType == "Approve")
            {
                var req = new TimeApprovalRequest(
                    approvingUserId: await sessionAdapter.EmployeeIdAsync(),
                    approvingUserIsAdmin: User.IsInRole(UserRoleName.Admin),
                    employeeId: employeeId,
                    newApprovalState: TimeApprovalStatus.Approved,
                    weekId: weekId
                    );
                var res = await approveTimeCommand.ApplyApproval(req);

                NotificationsController.AddNotification(User.SafeUserName(), $"Timesheet is {TimeApprovalStatus.Submitted}");
            }

            if (postType == "Reject")
            {
                var req = new TimeApprovalRequest(
                    approvingUserId: await sessionAdapter.EmployeeIdAsync(),
                    approvingUserIsAdmin: User.IsInRole(UserRoleName.Admin),
                    employeeId: employeeId,
                    newApprovalState: TimeApprovalStatus.Rejected,
                    weekId: weekId
                    );
                var res = await approveTimeCommand.ApplyApproval(req);

                NotificationsController.AddNotification(User.SafeUserName(), $"Timesheet is {TimeApprovalStatus.Rejected}");
            }
            if (postType == "Save New Combination")
            {
                var rowId     = vm.SelectedRowId;
                var oldJobId  = int.Parse(rowId.Substring(0, rowId.IndexOf(".")));
                var oldTaskId = int.Parse(rowId.Substring(rowId.IndexOf(".") + 1));
                var res       = await modifyJobTaskComboCommand.ModifyJobTaskCombo(employeeId, weekId, vm.NewEntry.SelectedTaskId ?? 0, vm.NewEntry.SelectedJobId ?? 0, oldTaskId, oldJobId);
            }

            if (postType == "RemoveRow")
            {
                var rowId  = vm.SelectedRowId;
                var jobId  = rowId.Substring(0, rowId.IndexOf("."));
                var taskId = rowId.Substring(rowId.IndexOf(".") + 1);
                var res    = await removeRowCommand.RemoveRow(employeeId, weekId, int.Parse(taskId), int.Parse(jobId));
            }
            return(RedirectToAction(nameof(Edit), new { weekId = weekId, employeeId = employeeId }));
        }