public HttpResponseMessage ApplyLeave(Leave leave) { HttpResponseMessage response = null; try { if (leave.leavetype_id != 0 && leave.from_date != DateTime.MinValue && leave.to_date != DateTime.MinValue) { string leave_type = LeaveRepo.GetLeaveTypeById(leave.leavetype_id); leave.from_date = leave.from_date.Date; leave.to_date = leave.to_date.Date; DateTime timeNow = DateTime.Now.Date; if (leave.from_date.Year > DateTime.Now.Year || leave.to_date.Year > DateTime.Now.Year && leave_type != Constants.LEAVE_TYPE_ML) { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_502", "Leave should be applied in CURRENT YEAR", "Leave should be applied in CURRENT YEAR")); return(response); } if (leave_type == Constants.LEAVE_TYPE_ML && leave.from_date >= timeNow) { leave.to_date = leave.from_date.AddDays(182); } if (leave.from_date < timeNow || leave.to_date < timeNow || leave.from_date > leave.to_date) { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_502", "Select valid date ", "Select valid date")); return(response); } List <Leave> leavelist = LeaveRepo.GetActiveLeaveListByEmployeeId(leave.employee_id); // to get the pending and approved leave list foreach (Leave leaveinstance in leavelist) //to check if leave is already applied for the given days { if (leave.from_date <= leaveinstance.from_date && leave.to_date >= leaveinstance.to_date) { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_516", "Already leaves has been applied inbetween selected days", "Already leaves has been applied inbetween selected days")); return(response); } else if ((leaveinstance.from_date <= leave.from_date && leave.from_date <= leaveinstance.to_date) || (leaveinstance.from_date <= leave.to_date && leave.to_date <= leaveinstance.to_date)) { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_516", "Already leaves has been applied inbetween selected days", "Already leaves has been applied inbetween selected days")); return(response); } } Employee employee_instance = EmployeeRepo.GetEmployeeById(leave.employee_id); string gender = employee_instance.gender; #region ML_Leave if (gender.ToLower() == "female" && leave_type == Constants.LEAVE_TYPE_ML) { Leavebalance_sheet leave_balance_instance = LeaveRepo.LeaveBalanceById(leave.employee_id, leave.leavetype_id); if (leave_balance_instance.no_of_days != 0 && leave_balance_instance.no_of_days == Constants.ML_LEAVE_BALANCE) { leave.no_of_days = Constants.ML_LEAVE_BALANCE; leave_balance_instance.no_of_days = leave_balance_instance.no_of_days - leave.no_of_days; LeaveRepo.UpdateLeaveBalanceSheet(leave_balance_instance); LeaveRepo.AddLeaveHistory(leave); ReportingTo reporting_to = EmployeeRepo.GetReportingtoByEmpId(leave.employee_id); MailHandler.LeaveMailing(leave.from_date, leave.to_date, employee_instance.first_name, Constants.LEAVE_STATUS_PENDING, employee_instance.email, reporting_to.mailid, null, reporting_to.emp_name); response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_001", "Leave Applied successfully", "Leave Applied successfully")); } else { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_503", "Insufficient ML leave balance", "Insufficient ML leave balance")); return(response); } } else if (gender.ToLower() == "male" && leave_type == Constants.LEAVE_TYPE_ML) { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_504", "Invalid leave type", "Invalid leave type")); return(response); } #endregion ML_Leave else { Leavebalance_sheet leave_balance_instance = LeaveRepo.LeaveBalanceById(leave.employee_id, leave.leavetype_id); List <DateTime> holiday = LeaveRepo.GetDateFromHoliday(); decimal noofdays = (decimal)Utils.DaysLeft(leave.from_date, leave.to_date, true, holiday); if (noofdays == 0) { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_505", "Selected date falls on holiday", "Selected date falls on holiday")); return(response); } #region CL_Leave else if (leave_type == Constants.LEAVE_TYPE_CL) { //int leave_type_id = LeaveRepo.GetLeavetypeIdByLeavetype(leave_type1); if (noofdays > 3) { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_506", "CL should be within three days", "CL should be within three days")); return(response); } else if (noofdays <= 3) { if (leave_balance_instance.no_of_days < noofdays) { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_507", "Insufficient CL balance, Please select different leave type", "Insufficient CL balance, Please select different leave type")); return(response); } else if (leave_balance_instance.no_of_days >= noofdays) { leave.no_of_days = (int)noofdays; leave_balance_instance.no_of_days = leave_balance_instance.no_of_days - leave.no_of_days; response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_001", "Leave Applied successfully", "Leave Applied successfully")); } } } #endregion CL_Leave #region EL_Leave else if (leave_type == Constants.LEAVE_TYPE_EL) { //int LOP_leave_type_id = LeaveRepo.GetLeavetypeIdByLeavetype(Constants.LEAVE_TYPE_LOP); //decimal Lop_leave_balance = LeaveRepo.GetNoofDaysById(LOP_leave_type_id, leave.employee_id); //decimal El_leave_balance = LeaveRepo.GetNoofDaysById(leave.leavetype_id, leave.employee_id); if (leave_balance_instance.no_of_days < noofdays) { //El_leave_balance = noofdays - El_leave_balance; //Lop_leave_balance = Math.Abs(Lop_leave_balance + (decimal)El_leave_balance); //El_leave_balance = 0; //leave.no_of_days = (int)noofdays; //Leavebalance_sheet EL_balance_instance = LeaveRepo.LeaveBalanceById(leave.employee_id, leave.leavetype_id); //leave.EL_flag = (decimal)EL_balance_instance.no_of_days; //EL_balance_instance.no_of_days = El_leave_balance; //LeaveRepo.UpdateLeaveBalanceSheet(EL_balance_instance); //Leavebalance_sheet lop_balance_instance = LeaveRepo.LeaveBalanceById(leave.employee_id, LOP_leave_type_id); //lop_balance_instance.no_of_days = Lop_leave_balance; //LeaveRepo.UpdateLeaveBalanceSheet(lop_balance_instance); //LeaveRepo.AddLeaveHistory(leave); //ReportingTo reporting_to = EmployeeRepo.GetReportingtoByEmpId(leave.employee_id); //MailHandler.LeaveMailing(leave.from_date, leave.to_date, employee_instance.first_name, Constants.LEAVE_STATUS_PENDING, employee_instance.email, reporting_to.mailid, null, reporting_to.emp_name); //response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_001", "Leave Applied successfully", "Leave Applied successfully")); response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_002", "Insufficient EL blanace, Please select different leave type", "Insufficient EL blanace, Please select different leave type")); return(response); } else if (leave_balance_instance.no_of_days >= noofdays) { //El_leave_balance = El_leave_balance - noofdays; leave.no_of_days = (int)noofdays; leave_balance_instance.no_of_days = leave_balance_instance.no_of_days - leave.no_of_days; response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_001", "Leave Applied successfully", "Leave Applied successfully")); } } #endregion EL_Leave #region LOP else if (leave_type == Constants.LEAVE_TYPE_LOP) { decimal Lop_leave_balance = LeaveRepo.GetNoofDaysById(leave.leavetype_id, leave.employee_id); int Cl_leave_id = LeaveRepo.GetLeavetypeIdByLeavetype(Constants.LEAVE_TYPE_CL); int El_leave_id = LeaveRepo.GetLeavetypeIdByLeavetype(Constants.LEAVE_TYPE_EL); decimal Cl_leave_balance = LeaveRepo.GetNoofDaysById(Cl_leave_id, leave.employee_id); decimal El_leave_balance = LeaveRepo.GetNoofDaysById(El_leave_id, leave.employee_id); if (Cl_leave_balance == 0 && El_leave_balance == 0) { Lop_leave_balance = Lop_leave_balance + noofdays; leave.no_of_days = (int)noofdays; leave_balance_instance.no_of_days = leave_balance_instance.no_of_days + leave.no_of_days; response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_001", "Leave Applied successfully", "Leave Applied successfully")); } else if (Cl_leave_balance == 0 && El_leave_balance > 0) { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_508", "You have EL leave balance. So you can apply from that", "You have EL leave balance. So you can apply from that")); return(response); } else if (El_leave_balance == 0 && Cl_leave_balance > 0) { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_509", "You have CL leave balance. So you can apply from that", "You have CL leave balance. So you can apply from that")); return(response); } else if (Cl_leave_balance > 0 && El_leave_balance > 0) { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_510", "You have CL, EL leave balance", "You have CL, EL leave balance")); return(response); } } #endregion LOP #region Wrok_From _Home else if (leave_type == Constants.LEAVE_TYPE_WFH) { decimal?WFH_leave_balance = LeaveRepo.GetNoofDaysById(leave.leavetype_id, leave.employee_id); if (noofdays <= 2) { leave.no_of_days = (int)noofdays; leave_balance_instance.no_of_days = leave_balance_instance.no_of_days + leave.no_of_days; response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_001", "Leave Applied successfully", "Leave Applied successfully")); } else if (noofdays > 2) { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_511", "Work from home should be within two days", "Work from home should be within two days")); return(response); } } #endregion LeaveRepo.UpdateLeaveBalanceSheet(leave_balance_instance); LeaveRepo.AddLeaveHistory(leave); ReportingTo reporting_to = EmployeeRepo.GetReportingtoByEmpId(leave.employee_id); MailHandler.LeaveMailing(leave.from_date, leave.to_date, employee_instance.first_name, Constants.LEAVE_STATUS_PENDING, employee_instance.email, reporting_to.mailid, null, reporting_to.emp_name); } } else { response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_512", "Mandatory fields are missing", "Mandatory fields are missing")); } } catch (Exception exception) { Debug.WriteLine(exception.Message); Debug.WriteLine(exception.GetBaseException()); response = Request.CreateResponse(HttpStatusCode.OK, new EMSResponseMessage("EMS_101", "Application Error", exception.Message)); } return(response); }