コード例 #1
0
ファイル: LeavesController.cs プロジェクト: xprasoulas/grid
        public ActionResult Update(LeaveUpdateModel vm)
        {
            if (ModelState.IsValid)
            {
                Employee currentUser = new Employee();


                if (vm.RequestedForUserId.Value != 0)
                {
                    currentUser = _employeeRepository.GetBy(l => l.Id == vm.RequestedForUserId);
                }
                else
                {
                    currentUser = _employeeRepository.GetBy(l => l.UserId == WebUser.Id);
                }
                if (currentUser.ReportingPersonId.HasValue)

                {
                    if (currentUser != null)
                    {
                        // Preprocess based on Duration, we dont get the end if it's a single day or half day
                        if (vm.Duration != LeaveDuration.MultipleDays)
                        {
                            vm.End = vm.Start;
                        }
                        var newLeave = new Leave
                        {
                            LeaveTypeId     = vm.LeaveTypeId,
                            Duration        = vm.Duration,
                            Start           = vm.Start,
                            End             = vm.End,
                            Reason          = vm.Reason,
                            Status          = LeaveStatus.Pending,
                            ApproverId      = currentUser.ReportingPersonId ?? 2,
                            Count           = vm.Count,
                            CreatedByUserId = WebUser.Id
                        };
                        if (vm.RequestedForUserId.HasValue && vm.RequestedForUserId != 0)
                        {
                            newLeave.RequestedForUserId = vm.RequestedForUserId;
                        }
                        else
                        {
                            var employee = _employeeRepository.GetBy(r => r.UserId == WebUser.Id);
                            newLeave.RequestedForUserId = employee.Id;
                        }
                        _leaveRepository.Create(newLeave);
                        _unitOfWork.Commit();

                        var employeeId    = newLeave.RequestedForUserId;
                        var loginEmployee = _employeeRepository.GetBy(r => r.Id == employeeId);
                        var userEmployee  = _userRepository.GetBy(r => r.Id == loginEmployee.UserId, "Person");

                        if (vm.Duration == LeaveDuration.FirstHalf || vm.Duration == LeaveDuration.SecondHalf)
                        {
                            var date   = vm.Start;
                            var exists = _timeSheetRepository.Any(t => t.CreatedByUserId == userEmployee.Id && t.Date == date.Date);
                            if (!exists)
                            {
                                var newTimeSheet = new TimeSheet
                                {
                                    State           = TimeSheetState.PendingApproval,
                                    Title           = $"{userEmployee.Person.Name}'s {"TimeSheet for "} {vm.Start.ToShortDateString()}",
                                    Date            = date.Date,
                                    TotalHours      = 4,
                                    Comments        = null,
                                    CreatedByUserId = userEmployee.Id
                                };

                                _timeSheetRepository.Create(newTimeSheet);

                                var project = _projectRepository.GetBy(i => i.Title == "Leave");
                                var newTimeSheetLineItem = new TimeSheetLineItem
                                {
                                    TimeSheetId = newTimeSheet.Id,
                                    ProjectId   = project.Id,
                                    TaskId      = null,
                                    TaskSummary = "Leave",
                                    Effort      = 4,
                                    Comments    = null,
                                    WorkType    = 2
                                };
                                _timeSheetLineItemRepository.Create(newTimeSheetLineItem);
                                _unitOfWork.Commit();

                                // Log Activity
                                var activity = new TimeSheetActivity
                                {
                                    TimeSheetId     = newTimeSheet.Id,
                                    Title           = "Created",
                                    Comment         = $"{userEmployee.Person.Name} created the TimeSheet at {DateTime.UtcNow.ToString("G")} with state {newTimeSheet.State} & hours {newTimeSheet.TotalHours}",
                                    CreatedByUserId = userEmployee.Id
                                };
                                _timeSheetActivityRepository.Create(activity);
                                _unitOfWork.Commit();
                            }
                        }
                        if (vm.Duration == LeaveDuration.OneFullDay)
                        {
                            var date   = vm.Start;
                            var exists = _timeSheetRepository.Any(t => t.CreatedByUserId == userEmployee.Id && t.Date == date.Date);
                            if (!exists)
                            {
                                var newTimeSheet = new TimeSheet
                                {
                                    State           = TimeSheetState.PendingApproval,
                                    Title           = $"{userEmployee.Person.Name}'s {"TimeSheet for "} {vm.Start.ToShortDateString()}",
                                    Date            = date.Date,
                                    TotalHours      = 8,
                                    Comments        = null,
                                    CreatedByUserId = userEmployee.Id
                                };
                                _timeSheetRepository.Create(newTimeSheet);

                                var project = _projectRepository.GetBy(i => i.Title == "Leave");
                                var newTimeSheetLineItem = new TimeSheetLineItem
                                {
                                    TimeSheetId = newTimeSheet.Id,
                                    ProjectId   = project.Id,
                                    TaskId      = null,
                                    TaskSummary = "Leave",
                                    Effort      = 8,
                                    Comments    = null,
                                    WorkType    = 2
                                };
                                _timeSheetLineItemRepository.Create(newTimeSheetLineItem);
                                _unitOfWork.Commit();

                                var activity = new TimeSheetActivity
                                {
                                    TimeSheetId     = newTimeSheet.Id,
                                    Title           = "Created",
                                    Comment         = $"{userEmployee.Person.Name} created the TimeSheet at {DateTime.UtcNow.ToString("G")} with state {newTimeSheet.State} & hours {newTimeSheet.TotalHours}",
                                    CreatedByUserId = userEmployee.Id
                                };
                                _timeSheetActivityRepository.Create(activity);
                                _unitOfWork.Commit();
                            }
                        }
                        if (vm.Duration == LeaveDuration.MultipleDays)
                        {
                            List <DateTime> allDates          = new List <DateTime>();
                            List <DateTime> datesForTimesheet = new List <DateTime>();
                            for (DateTime dates = vm.Start; dates <= vm.End; dates = dates.AddDays(1))
                            {
                                allDates.Add(dates);
                            }
                            foreach (var datetime in allDates)
                            {
                                if (datetime.DayOfWeek != DayOfWeek.Saturday && datetime.DayOfWeek != DayOfWeek.Sunday)
                                {
                                    datesForTimesheet.Add(datetime);
                                }
                            }
                            var date   = vm.Start;
                            var exists = _timeSheetRepository.Any(t => t.CreatedByUserId == userEmployee.Id && t.Date == date.Date);
                            if (!exists)
                            {
                                foreach (var timesheetdate in datesForTimesheet)
                                {
                                    var newTimeSheet = new TimeSheet
                                    {
                                        State           = TimeSheetState.PendingApproval,
                                        Title           = $"{userEmployee.Person.Name}'s {"TimeSheet for "}{timesheetdate.ToShortDateString()}",
                                        Date            = timesheetdate,
                                        TotalHours      = 8,
                                        Comments        = null,
                                        CreatedByUserId = userEmployee.Id
                                    };
                                    _timeSheetRepository.Create(newTimeSheet);

                                    var project = _projectRepository.GetBy(i => i.Title == "Leave");
                                    var newTimeSheetLineItem = new TimeSheetLineItem
                                    {
                                        TimeSheetId = newTimeSheet.Id,
                                        ProjectId   = project.Id,
                                        TaskId      = null,
                                        TaskSummary = "Leave",
                                        Effort      = 8,
                                        Comments    = null,
                                        WorkType    = 2
                                    };
                                    _timeSheetLineItemRepository.Create(newTimeSheetLineItem);
                                    _unitOfWork.Commit();

                                    // Log Activity
                                    var activity = new TimeSheetActivity
                                    {
                                        TimeSheetId     = newTimeSheet.Id,
                                        Title           = "Created",
                                        Comment         = $"{userEmployee.Person.Name} created the TimeSheet at {DateTime.UtcNow.ToString("G")} with state {newTimeSheet.State} & hours {newTimeSheet.TotalHours}",
                                        CreatedByUserId = userEmployee.Id
                                    };
                                    _timeSheetActivityRepository.Create(activity);
                                    _unitOfWork.Commit();
                                }
                            }
                        }

#if !DEBUG
                        // _emailComposerService.TestEmailSend(vm);
                        _emailComposerService.LeaveApplicationEmail(newLeave.Id);
                        //  _emailComposerService.TimeSheetSubmitted(newTimeSheet.Id);
#endif

                        return(Json(true));
                    }
                }
            }


            return(Json(false));
        }