Esempio n. 1
0
        public async Task <IActionResult> Edit(string id, string returnUrl, [Bind("Id,StartDate,EndDate,VacationType,Status,Comment")] VacationViewModel model)
        {
            if (id != model.Id)
            {
                return(NotFound());
            }

            Vacation vacation = await vacationRepository.GetByID(id);

            // add back old deducted days from user vacation balance
            await IncrementUserVacationBalance(new VacationViewModel(vacation));
            await MapToModel(vacation, model, vacation.UserID);

            // if the vacation got rejected don't validate
            if (model.Status != RequestStatus.Rejected)
            {
                // validate that requested vacation days is within the allowed user vacation days
                ValidateRequestedDaysAvailability(vacation);
            }
            if (ModelState.IsValid)
            {
                if (await IsExistingVacationConflicting(vacation.StartDate, vacation.EndDate, vacation.ID))
                {
                    TempData["StatusMessage"] = "The requested vacation overlaps with a previous one";
                }
                else
                {
                    // only update status if it's the admin
                    if (IsAdmin())
                    {
                        vacation.Status = model.Status;
                        switch (model.Status)
                        {
                        case RequestStatus.Approved:
                            await UpdateOffenses(vacation);

                            break;

                        case RequestStatus.Rejected:
                            await workingDayRepository.CheckAbsencesInRange(vacation.UserID, vacation.StartDate, DateTime.Today.AddDays(-1));

                            break;
                        }
                    }
                    // valid requested days & personal & pending or approved ? deduct requested days from vacation balance
                    await DeductUserVacationBalance(new VacationViewModel(vacation));

                    await vacationRepository.Update(vacation);

                    TempData["StatusMessage"] = "Vacation has been updated successfully.";
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return(Redirect(returnUrl));
                    }
                    return(RedirectToAction(nameof(Index)));
                }
            }
            return((vacation.EndDate - vacation.StartDate).TotalDays != 0.5 ? View(model) : View("EditHalfDay", model));
        }