private void UpdateDocument(HrpLeaveApp leave) { var document = _context.LeaveDocument.Where(d => d.UserName.ToUpper().Equals(leave.EmpNo.ToUpper())).OrderByDescending(d => d.DateCreated).FirstOrDefault(); if (document != null) { _context.LeaveDocument.Remove(document); _context.SaveChanges(); } }
public JsonResult ApplyLeave(HrpLeaveApp leave) { try { var settings = _context.Settings.FirstOrDefault(); settings.Initials = settings?.Initials ?? ""; if (string.IsNullOrEmpty(leave.Reliever) && settings.LeaveRelieverMandatory) { return(Json(new ReturnData <string> { Success = false, Message = "Kindly select your reliver" })); } leave.Sdate = leave.Sdate.GetValueOrDefault().AddDays(1); leave.Edate = leave.Edate.GetValueOrDefault().AddDays(1); var daysFromStartDay = (leave.Sdate - DateTime.UtcNow).GetValueOrDefault().TotalDays; leave.Emergency = leave.Emergency == null ? false : leave.Emergency; if (daysFromStartDay < 14 && settings.Initials.ToUpper().Equals("ABNO") && leave.LeaveType.ToUpper().Equals("ANNUAL LEAVE") && !(bool)leave.Emergency) { UpdateDocument(leave); return(Json(new ReturnData <string> { Success = false, Message = "Sorry, leave start date must be atleast two weeks from the application date" })); } leave.Rdate = DateTime.UtcNow; var token = _tokenValidator.Validate(HttpContext); if (!token.Success) { return(Json(new ReturnData <string> { Success = false, NotAuthenticated = true, Message = $"Unauthorized:-{token.Message}", })); } if (token.Role != Role.Staff) { return(Json(new ReturnData <string> { Success = false, NotAuthenticated = true, Message = "Sorry, you are not authorized to perform this action", })); } var app = _unisolApiProxy.ApplyLeave(leave, settings.LeaveRelieverMandatory).Result; var jdata = JsonConvert.DeserializeObject <ReturnData <HrpLeaveApp> >(app); var document = _context.LeaveDocument.Where(d => d.UserName.ToUpper().Equals(leave.EmpNo.ToUpper())).OrderByDescending(d => d.DateCreated).FirstOrDefault(); if (jdata.Success) { if (!string.IsNullOrEmpty(jdata.Data.Reliever)) { _context.Add(new Obstructor { LeaveRef = jdata.Data?.Ref ?? "", Requestor = leave?.EmpNo ?? "", Reliver = jdata.Data?.Reliever ?? "", Status = "Pending" }); if (document != null) { document.LeaveRef = jdata.Data?.Ref ?? ""; } _context.SaveChanges(); } } else { if (document != null) { UpdateDocument(leave); } } return(Json(jdata)); } catch (Exception) { return(Json(new ReturnData <string> { Success = false, Message = "Sorry, An error occurred" })); } }
public JsonResult ApplyLeave(HrpLeaveApp leave, bool leaveRelieverMandatory) { try { leave.LeavePeriod = _context.HrpLeavePeriod.OrderByDescending(p => p.EndDate).FirstOrDefault(p => p.StartDate.Year == DateTime.UtcNow.Year)?.Names ?? DateTime.UtcNow.Year.ToString(); leave.Reliever = leave.Reliever ?? ""; var reliverDetails = leave.Reliever.Split("-("); if (reliverDetails.Count() > 1) { leave.Reliever = reliverDetails[1] ?? ""; } leave.Reliever = leave.Reliever.Replace(")", ""); if (leave.LeaveType.ToLower().Equals("maternity leave")) { if (!CanApplyMaternityLeave(leave.EmpNo, leave.LeaveType)) { return(Json(new ReturnData <HrpLeaveApp> { Success = false, Message = "Application failed. Only Female can apply for this leave", Data = leave })); } } var leaveExists = _context.HrpLeaveApp.Any(l => l.Status.Equals("Pending") && l.LeaveType.Equals(leave.LeaveType) && l.EmpNo.Equals(leave.EmpNo)); if (leaveExists) { return(Json(new ReturnData <HrpLeaveApp> { Success = false, Message = $"You have a Pending {leave.LeaveType} Application", Data = leave })); } leave.Ref = GenerateAppNo(); //var leaveDaysDetails = _staffServices.GetEmpLeaveDays(leave.EmpNo); //if (!leaveDaysDetails.Success) // return Json(leaveDaysDetails); //var days = leaveDaysDetails.Data.Where(l => l.LeaveType.CaseInsensitiveContains(leave.LeaveType)).Sum(x => x.LeaveDays) ?? 0; var days = _staffServices.GetUserLeavesEntitled(leave.EmpNo, leave.LeaveType); var procOnlineReq = new ProcOnlineReq { ReqRef = leave.Ref, DocType = "LEAVE APPLICATION", Rdate = DateTime.UtcNow.Date, Rtime = DateTime.UtcNow.AddHours(3), Usercode = leave.EmpNo, Status = "Pending" }; var docId = _context.Wfrouting.FirstOrDefault(r => r.Document.ToUpper() == procOnlineReq.DocType.ToUpper())?.Id.ToString(); if (string.IsNullOrEmpty(docId)) { return(Json(new ReturnData <string> { Success = false, Message = "Sorry, " + procOnlineReq.DocType.ToUpper() + " Not supported at the moment. Please contact the admin" })); } if (days < 1) { return(Json(new ReturnData <HrpLeaveApp> { Success = false, Message = string.Format("Application failed. Your have {0} days for this leave", days), Data = leave })); } if (!leaveRelieverMandatory) { var user = _context.HrpEmployee.FirstOrDefault(u => u.EmpNo == leave.EmpNo); var description = $"Type of Leave: {leave.LeaveType}; Number of days requested: {leave.LeaveDays}; From: {String.Format("{0:d}", leave.Sdate)} To: {String.Format("{0:d}", leave.Edate)}"; var workFlowStatus = utils.SaveToWorkFlowCenter(procOnlineReq, user, docId, description); if (!workFlowStatus.Success) { return(Json(workFlowStatus)); } } _context.HrpLeaveApp.Add(leave); _context.SaveChanges(); return(Json(new ReturnData <HrpLeaveApp> { Success = true, Message = "Your leave application was submited succesfully", Data = leave })); } catch (Exception e) { return(Json(new ReturnData <string> { Success = false, Message = "There was a problem while trying to apply leave.", Error = new Error(e) })); } }
public Task <string> ApplyLeave(HrpLeaveApp leave, bool leaveRelieverMandatory) { var response = Post($"leave/applyLeave?leaveRelieverMandatory={leaveRelieverMandatory}", leave); return(response); }