public async Task <HttpResponseMessage> Create(HttpRequestMessage request, string OTCheckIn, string OTCheckOut, ExplanationRequestViewModel explanationViewModel)
 {
     return(await CreateHttpResponse(request, () =>
     {
         var checkDelegate = new CheckDelegateModel();
         var dateNow = DateTime.Now.Date;
         var timesheet = _fingertimesheetService.GetById(explanationViewModel.TimeSheetId);
         var DateExRequestInPast = _commonService.GetDateExRequestInPast(timesheet.DayOfCheck);
         if (DateExRequestInPast.Date < dateNow)
         {
             return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_CREATE_EXREQUEST_IN_PAST_NOT_MSG);
         }
         var explanation = new ExplanationRequest();
         explanation.UpdateExplanationRequest(explanationViewModel);
         if (explanation.Actual == "Leave")
         {
             var entitleDayAppUser = _explanationRequestService.GetEntitleDayByUserID(explanation.CreatedBy);
             var timeSheet = _fingertimesheetService.GetById(explanationViewModel.TimeSheetId);
             if ((timeSheet.Absent == CommonConstants.TimeSheetAbsent && entitleDayAppUser.MaxEntitleDayAppUser - entitleDayAppUser.NumberDayOff >= 1) ||
                 ((timeSheet.Absent == CommonConstants.TimeSheetAbsentAfternoon || timeSheet.Absent == CommonConstants.TimeSheetAbsentMorning) &&
                  entitleDayAppUser.MaxEntitleDayAppUser - entitleDayAppUser.NumberDayOff >= 0.5))
             {
                 if (_explanationRequestService.Add(explanation, OTCheckIn, OTCheckOut))
                 {
                     var group = __groupService.GetGroupById(explanationViewModel.GroupId.ToString());
                     var dataDelegation = _configDelegationService.GetDelegationByUserId(explanation.CreatedBy);
                     if (dataDelegation.StartDate <= explanation.CreatedDate.Value.Date && dataDelegation.EndDate >= explanation.CreatedDate.Value.Date)
                     {
                         checkDelegate.CheckConfigDelegateDefault = true;
                         checkDelegate.AssignConfigDelegate = dataDelegation.AssignTo;
                         _configDelegationService.ChangeStatusAfterAddExplanationRequest(dataDelegation.AssignTo, explanation);
                     }
                     else
                     {
                         if (explanation.CreatedDate != null)
                         {
                             if (group.DelegateId != null && group.StartDate <= explanation.CreatedDate.Value.Date && group.EndDate >= explanation.CreatedDate.Value.Date)
                             {
                                 checkDelegate.CheckGroupDelegateDefault = true;
                                 _explanationRequestService.AddDelegateDefault(explanationViewModel.GroupId, explanation.ID);
                             }
                         }
                     }
                     return request.CreateResponse(HttpStatusCode.Created, checkDelegate);
                 }
             }
             else
             {
                 return request.CreateErrorResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_CREATE_EXREQUEST_NOT_ENOUGH_ENTITLE_DAY);
             }
         }
         if (_explanationRequestService.Add(explanation, OTCheckIn, OTCheckOut))
         {
             var groupData = __groupService.GetGroupById(explanationViewModel.GroupId.ToString());
             var dataDelegationConfig = _configDelegationService.GetDelegationByUserId(explanation.CreatedBy);
             if (dataDelegationConfig.StartDate <= explanation.CreatedDate.Value.Date && dataDelegationConfig.EndDate >= explanation.CreatedDate.Value.Date)
             {
                 checkDelegate.CheckConfigDelegateDefault = true;
                 checkDelegate.AssignConfigDelegate = dataDelegationConfig.AssignTo;
                 _configDelegationService.ChangeStatusAfterAddExplanationRequest(dataDelegationConfig.AssignTo, explanation);
             }
             else
             {
                 if (explanation.CreatedDate != null)
                 {
                     if (groupData.DelegateId != null && groupData.StartDate <= explanation.CreatedDate.Value.Date && groupData.EndDate >= explanation.CreatedDate.Value.Date)
                     {
                         checkDelegate.CheckGroupDelegateDefault = true;
                         _explanationRequestService.AddDelegateDefault(explanationViewModel.GroupId, explanation.ID);
                     }
                 }
             }
             return request.CreateResponse(HttpStatusCode.Created, checkDelegate);
         }
         return request.CreateErrorResponse(HttpStatusCode.BadRequest, MessageSystem.Create_Explanation_Error);
     }));
 }
Example #2
0
 public async Task <HttpResponseMessage> Create(HttpRequestMessage request, RequestViewModel requestViewModel)
 {
     return(await CreateHttpResponse(request, () =>
     {
         HttpResponseMessage response = null;
         if (!ModelState.IsValid)
         {
             response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
         }
         var dateNow = DateTime.Now.Date;
         if (requestViewModel.StartDate < dateNow)
         {
             return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_CREATE_REQUEST_IN_PAST_NOT_MSG);
         }
         else
         {
             requestViewModel.AppUser = new Models.AppUserViewModel();
             requestViewModel.CreatedDate = DateTime.Now;
             requestViewModel.UserId = AppUserManager.FindByNameAsync(User.Identity.Name).Result.Id;
             requestViewModel.AppUser.GroupId = AppUserManager.FindByNameAsync(User.Identity.Name).Result.GroupId ?? 0;
             requestViewModel.Status = true;
             //requestViewModel.ChangeStatusById = __groupService.GetGroupLeadIdByGroup(requestViewModel.AppUser.GroupId.Value);
             Request newRequest = new Request();
             newRequest.UpdateRequest(requestViewModel);
             //Check Request Day Off
             var model = _requestService.GetAllRequest(newRequest);
             if (model == 0)
             {
                 if (_requestService.checkUnitEntitleday(newRequest))
                 {
                     if (_requestService.Add(newRequest))
                     {
                         var responseData = Mapper.Map <Request, RequestViewModel>(newRequest);
                         var group = __groupService.GetGroupById(requestViewModel.AppUser.GroupId.ToString());
                         responseData.GroupName = group.Name;
                         var dataDelegation = _configDelegationService.GetDelegationByUserId(newRequest.UserId);
                         if (dataDelegation.StartDate <= newRequest.CreatedDate.Value.Date && dataDelegation.EndDate >= newRequest.CreatedDate.Value.Date)
                         {
                             responseData.CheckConfigDelegateDefault = true;
                             responseData.AssignConfigDelegate = dataDelegation.AssignTo;
                             _configDelegationService.ChangeStatusAfterAddRequest(dataDelegation.AssignTo, newRequest);
                         }
                         else
                         {
                             if (newRequest.CreatedDate != null)
                             {
                                 if (group.DelegateId != null && group.StartDate <= newRequest.CreatedDate.Value.Date && group.EndDate >= newRequest.CreatedDate.Value.Date)
                                 {
                                     responseData.CheckGroupDelegateDefault = true;
                                     responseData.AssignGroupDelegate = group.DelegateId;
                                     _requestService.CheckDelegateDefault(requestViewModel.AppUser.GroupId.ToString(), newRequest.ID, newRequest.ChangeStatusById);
                                 }
                             }
                         }
                         return request.CreateResponse(HttpStatusCode.Created, responseData);
                     }
                 }
                 else
                 {
                     return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_ENTITLEDAY);
                 }
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_CREATE_REQUEST_NOT_MSG);
             }
             if (model == 1)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME);
             }
             if (model == 2)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME_MORNING);
             }
             if (model == 3)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME_AFTERNOON);
             }
             if (model == 4)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME_LATECOMING);
             }
             if (model == 5)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME_EARLYLEAVING);
             }
             if (model == 6)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME);
             }
             if (model == 7)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_MORNING);
             }
             if (model == 8)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_MORNING_LATECOMING);
             }
             if (model == 9)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME);
             }
             if (model == 10)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_AFTERNOONLEAVE);
             }
             if (model == 11)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_AFTERNOONLEAVE_EARLYLEAVING);
             }
             if (model == 12)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME);
             }
             if (model == 13)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_MORNING_LATECOMING);
             }
             if (model == 14)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_LATECOMING_EARLYLEAVING);
             }
             if (model == 15)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME);
             }
             if (model == 16)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_EARLYLEAVING_AFTERNOONLEAVE);
             }
             if (model == 17)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_EARLYLEAVING);
             }
         }
         return response;
     }));
 }