public ActionResult Edit(int id)
        {
            var timesheet = timesheetService.GetTimesheet(id);

            if (timesheet == null)
            {
                return(HttpNotFound());
            }

            var canEdit    = !(timesheet.CarerPaymentGenerated || timesheet.CustomerPaymentGenerated);
            var headerInfo = new Dictionary <string, string>
            {
                { "Booking", timesheet.Agreement.ID.ToString() },
                { "Week Ending", timesheet.WeekEnding.ToShortDateString() },
                { "Reference", timesheet.Reference },
                { "Customer", $"{timesheet.Agreement.CustomerDisplayName} [{timesheet.Agreement.CustomerID}]" },
                { "Care Recipient", $"{timesheet.Agreement.CareRecipientDisplayName}" + (timesheet.Agreement.CareRecipientID.HasValue ? $" [{timesheet.Agreement.CareRecipientID}]" : "") },
                { "Payer", timesheet.Agreement.PayerDisplayName + (timesheet.Agreement.PayerId.HasValue ? $" [{timesheet.Agreement.PayerId}]" : "") },
                { "Submitted By", timesheet.SubmittedBy }
            };

            if (timesheet.SubmittedDate.HasValue)
            {
                headerInfo.Add("Submitted Date", timesheet.SubmittedDate.Value.ToString("g"));
            }

            ViewBag.CanEdit    = canEdit;
            ViewBag.HeaderInfo = headerInfo;

            var model = TimesheetWeekViewModel.ToViewModel(timesheet);

            return(View(model));
        }
        public ActionResult Edit(TimesheetWeekViewModel timesheet)
        {
            var success = true;

            try
            {
                var canEdit = !(timesheet.CarerPaymentGenerated || timesheet.CustomerPaymentGenerated);
                if (canEdit)
                {
                    SaveTimesheet(timesheet);
                }
            }
            catch (Exception ex)
            {
                success = false;
                loggingService.LogException(ex);
            }

            TempData["ShowExpenses"] = timesheet.ShowExpensesModal;
            TempData["ShowSummary"]  = timesheet.ShowSummaryModal;

            if (!timesheet.ShowExpensesModal && !timesheet.ShowSummaryModal)
            {
                SetFeedbackMessage(success,
                                   "Timesheet has been saved.",
                                   "There has been an error whilst saving the timesheet.");
            }

            return(RedirectToSamePage());
        }
        private void SaveTimesheet(TimesheetWeekViewModel timesheet, bool submit = false)
        {
            var oldTimesheet = timesheetService.GetTimesheet(timesheet.ID);

            oldTimesheet.Notes = timesheet.Notes;

            for (int i = 0; i < oldTimesheet.Days.Count; i++)
            {
                oldTimesheet.Days[i].Notes = timesheet.Days[i].Notes;
                for (int j = 0; j < oldTimesheet.Days[i].Periods.Count; j++)
                {
                    var startTime = Utilities.MergeDateAndTime(oldTimesheet.Days[i].Date, timesheet.Days[i].Periods[j].StartTime);
                    oldTimesheet.Days[i].Periods[j].StartTime = startTime;

                    var minsWorked = timesheet.Days[i].Periods[j].NumberOfHours.ConvertHHMMStringToMinutes();
                    oldTimesheet.Days[i].Periods[j].EndTime = startTime.AddMinutes(minsWorked);
                }
            }
            timesheetService.Save(oldTimesheet);
            if (submit)
            {
                SubmitTimesheet(oldTimesheet);
            }
        }