public ActionResult DeleteConfirmed(string id) { Employees_Other_Leave_Counts eolc = db.Employees_Other_Leave_Counts.Find(id); if (eolc != null) { db.Employees_Other_Leave_Counts.Remove(eolc); db.SaveChanges(); } var etl = db.Employees_Take_Leaves.Where(s => s.emp_code == id).ToList(); for (int i = 0; i < etl.Count(); i++) { Employees_Take_Leaves obj = db.Employees_Take_Leaves.Find(etl[i].id); db.Employees_Take_Leaves.Remove(obj); db.SaveChanges(); } Employee employee = db.Employees.Find(id); db.Employees.Remove(employee); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Index([Bind(Include = "emp_code,leave_id,date_from,date_to,financial_year_start,financial_year_end,gender")] Employees_Take_Leaves etl) { Employees_Take_Leaves employeeTakeLeave = new Employees_Take_Leaves(); employeeTakeLeave.emp_code = "NHM" + etl.emp_code; employeeTakeLeave.leave_id = etl.leave_id; employeeTakeLeave.date_from = etl.date_from; employeeTakeLeave.date_to = etl.date_to; employeeTakeLeave.financial_year_start = etl.financial_year_start; employeeTakeLeave.financial_year_end = etl.financial_year_end; employeeTakeLeave.absent_days = 0; TimeSpan difference = employeeTakeLeave.date_to - employeeTakeLeave.date_from; employeeTakeLeave.no_of_days = (int)difference.TotalDays; bool flag = false; // Additional Conditional checks for Financial Year // if((etl.financial_year_start == etl.date_from.Year) && (etl.financial_year_end == etl.date_to.Year) && (etl.date_from.Day >= 1) && (etl.date_from.Month >= 4) && (etl.date_to.Day <= 31) && (etl.date_to.Month <= 3)) // { var leaveName = db.Leaves.FirstOrDefault(s => s.id == employeeTakeLeave.leave_id).name.ToString().ToLower(); // Maternity Leave Count if (leaveName.Equals("maternity leave")) { // Select the particular row var getRow = db.Employees_Other_Leave_Counts.Where(s => s.maternity_leave_count_left >= 0).SingleOrDefault(s => s.code == employeeTakeLeave.emp_code); // Select the particular column value from the selected Row int maternityLeaveCount = getRow.maternity_leave_count_left; if (maternityLeaveCount != 0) { if (ModelState.IsValid) { db.Employees_Other_Leave_Counts.Where(s => s.code == employeeTakeLeave.emp_code).ToList().ForEach(a => a.maternity_leave_count_left = maternityLeaveCount - 1); db.SaveChanges(); flag = true; } // Marking the absent if employee applies for Maternity Leave > 180 days if (employeeTakeLeave.no_of_days > 180) { employeeTakeLeave.absent_days = employeeTakeLeave.no_of_days - 180; } } else { // leave cannot be granted TempData["message"] = "Cannot grant more leaves!!! Already 2 Maternity Leaves are granted to " + employeeTakeLeave.emp_code; return(RedirectToAction("Index")); } } // Paternity Leave Count else if (leaveName.Equals("paternity leave")) { // Select the particular row var getRow = db.Employees_Other_Leave_Counts.Where(s => s.paternity_leave_count_left >= 0).SingleOrDefault(s => s.code == employeeTakeLeave.emp_code); // Select the particular column value from the selected Row int paternityLeaveCount = getRow.paternity_leave_count_left; employeeTakeLeave.absent_days = 0; if (paternityLeaveCount != 0) { if (ModelState.IsValid) { db.Employees_Other_Leave_Counts.Where(s => s.code == employeeTakeLeave.emp_code).ToList().ForEach(a => a.paternity_leave_count_left = paternityLeaveCount - 1); db.SaveChanges(); flag = true; } // Marking the absent if employee applies for Paternity Leave > 15 days if (employeeTakeLeave.no_of_days > 15) { employeeTakeLeave.absent_days = employeeTakeLeave.no_of_days - 15; } } else { // leave cannot be granted TempData["message"] = "Cannot grant more leaves!!! Already 2 Paternity Leaves are granted to " + employeeTakeLeave.emp_code; return(RedirectToAction("Index")); } } // Child Adoption Leave Count else if (leaveName.Equals("child adoption leave")) { // Select the particular row var getRow = db.Employees_Other_Leave_Counts.Where(s => s.child_adoption_leave_count_left >= 0).SingleOrDefault(s => s.code == employeeTakeLeave.emp_code); // Select the particular column value from the selected Row int childAdoptionLeaveCount = getRow.child_adoption_leave_count_left; employeeTakeLeave.absent_days = 0; if (childAdoptionLeaveCount != 0) { if (ModelState.IsValid) { db.Employees_Other_Leave_Counts.Where(s => s.code == employeeTakeLeave.emp_code).ToList().ForEach(a => a.child_adoption_leave_count_left = childAdoptionLeaveCount - 1); db.SaveChanges(); flag = true; } // Marking the absent if employee applies for Child Adoption Leave > 40 days if (employeeTakeLeave.no_of_days > 40) { employeeTakeLeave.absent_days = employeeTakeLeave.no_of_days - 40; } } else { // leave cannot be granted TempData["message"] = "Cannot grant more leaves!!! Already 2 Child Adoption Leaves are granted to " + employeeTakeLeave.emp_code; return(RedirectToAction("Index")); } } // Medical Leave Count else if (leaveName.Equals("medical leave")) { var getMedicalLeaveOfEmployee = db.Employees_Take_Leaves.Where(s => s.emp_code == employeeTakeLeave.emp_code).Where(s => s.leave_id == employeeTakeLeave.leave_id).Where(s => s.financial_year_start == employeeTakeLeave.financial_year_start).Where(s => s.financial_year_end == employeeTakeLeave.financial_year_end).ToList(); var leavesTakenForDays = 0; for (int i = 0; i < getMedicalLeaveOfEmployee.Count(); i++) { leavesTakenForDays += getMedicalLeaveOfEmployee[i].no_of_days; } //if ((getMedicalLeaveOfEmployee == null) || (leavesTakenForDays < 15)) //{ if (employeeTakeLeave.no_of_days > 15) { employeeTakeLeave.absent_days = employeeTakeLeave.no_of_days - 15; } flag = true; } // Finally persisting the row values inside Employee Take Leave database if (flag) { if (ModelState.IsValid) { db.Employees_Take_Leaves.Add(employeeTakeLeave); db.SaveChanges(); TempData["message"] = "Leaves granted Successfully!!!. " + leaveName + "s are granted to " + employeeTakeLeave.emp_code; return(RedirectToAction("Index")); } } // } return(View(etl)); }