public ActionResult Create(ShiftPref shiftPref) { var employ = db.Employees.Find(shiftPref.EmployID); shiftPref.Name = employ.FirstName; if (ModelState.IsValid) { db.Preferences.Add(shiftPref); db.SaveChanges(); return(RedirectToAction("Index", new { Id = shiftPref.EmployID })); } return(View(shiftPref)); }
public ActionResult Create(Employees employees) { if (!(ModelState.IsValid)) { return(View(employees)); } if (employees.FirstName.Any(char.IsDigit)) { ModelState.AddModelError("FirstName", "Letters Only"); return(View(employees)); } else if (employees.LastName.Any(char.IsDigit)) { ModelState.AddModelError("LastName", "Letters Only"); return(View(employees)); } else if (employees.ID < 0) { ModelState.AddModelError("ID", "Positive Numbers Only"); return(View(employees)); } if (EmployRes.UserCounter(employees)) { ModelState.AddModelError("Email", "Employee already exist"); return(View(employees)); } if (employees.NoOfShifts < 0) { ModelState.AddModelError("NoOfShifts", "Positive Numbers Only"); return(View(employees)); } if (employees.Telephone.Any(char.IsLetter)) { ModelState.AddModelError("Telephone", "Numbers Only"); return(View(employees)); } if (ModelState.IsValid) { db.Employees.Add(employees); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(employees)); }
public void HistoryDeletion() { var count = db.History.ToList(); if (count.Count() > 476) { var counter = count.OrderBy(s => s.Dates.Date).ThenBy(s => s.OfDayType).Where(c => c.Day == "Saturday").FirstOrDefault().Dates.Date; counter = counter.AddDays(1); foreach (var shift in db.History.Where(d => d.Dates < counter).ToList()) { db.History.Remove(shift); } try { db.SaveChanges(); } catch { throw new ArgumentException("Unable to delete Previous Week History"); } } }
public ActionResult Create(ScheduleParameters scheduleParameters) { int TotalShifts = 0; int idnum = 0; if (Convert.ToInt32(scheduleParameters.DMorning) < 0 || Convert.ToInt32(scheduleParameters.DAfternoon) < 0 || Convert.ToInt32(scheduleParameters.DNight) < 0) { return(RedirectToAction("DayCreate", new { whattodo = false })); } if (scheduleParameters.Day != null && db.ScheduleParameters.Select(o => o.Day != null).Count() > 7) { return(RedirectToAction("DayCreate", new { whattodo = true })); } if (scheduleParameters.Day == null & (Convert.ToInt32(scheduleParameters.DMorning) != 0 || Convert.ToInt32(scheduleParameters.DAfternoon) != 0 || Convert.ToInt32(scheduleParameters.DNight) != 0)) { return(RedirectToAction("DayCreate", new { whattodo = true, require = true })); } if (db.ScheduleParameters.Where(d => d.Day == scheduleParameters.Day).Count() > 0) { return(RedirectToAction("DayCreate", new { dayexists = true })); } HsDelete.SpecialFixedFshiftDeletion(); if (db.ShiftsPerWeek.Any()) { idnum = db.ShiftsPerWeek.FirstOrDefault().ID; } if (scheduleParameters.Day == null) { if (idnum == 0) { ShiftsPerWeek SPW = new ShiftsPerWeek { NumOfShifts = (scheduleParameters.Morning + scheduleParameters.Afternoon + scheduleParameters.Night) * 7 }; db.ShiftsPerWeek.Add(SPW); db.SaveChanges(); } else { ShiftsPerWeek EditOldShift = db.ShiftsPerWeek.Find(idnum); EditOldShift.NumOfShifts = (scheduleParameters.Morning + scheduleParameters.Afternoon + scheduleParameters.Night) * 7; db.Entry(EditOldShift).State = EntityState.Modified; db.SaveChanges(); } } else { scheduleParameters.DMorning = scheduleParameters.DMorning == null ? 0 : scheduleParameters.DMorning; scheduleParameters.DAfternoon = scheduleParameters.DAfternoon == null ? 0 : scheduleParameters.DAfternoon; scheduleParameters.DNight = scheduleParameters.DNight == null ? 0 : scheduleParameters.DNight; TotalShifts += Convert.ToInt32(scheduleParameters.DMorning); TotalShifts += Convert.ToInt32(scheduleParameters.DAfternoon); TotalShifts += Convert.ToInt32(scheduleParameters.DNight); TotalShifts += db.ShiftsPerWeek.Select(y => y.NumOfShifts).FirstOrDefault(); ShiftsPerWeek SperW = db.ShiftsPerWeek.Find(idnum); SperW.NumOfShifts = TotalShifts; db.Entry(SperW).State = EntityState.Modified; db.SaveChanges(); } if (ModelState.IsValid) { db.ScheduleParameters.Add(scheduleParameters); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(scheduleParameters)); }
public ActionResult Fixed(FinalShift FixedShift) { if ((FixedShift.Morning == true && FixedShift.Afternoon == true) || (FixedShift.Morning == true && FixedShift.Night == true) || (FixedShift.Afternoon == true && FixedShift.Night == true) || (FixedShift.Morning == true && FixedShift.Afternoon == true && FixedShift.Night == true)) { ModelState.AddModelError("Night", "Choose one shift"); FixedShift.Employees = db.Employees.Where(x => x.Admin == false).ToList();; return(View(FixedShift)); } var employ = db.Employees.Where(x => x.Admin == false).ToList(); var totalshifts = db.ShiftsPerWeek.Select(o => o.NumOfShifts).FirstOrDefault(); DateTime NextSunday = DateTime.Now.AddDays(1); while (NextSunday.DayOfWeek != DayOfWeek.Sunday) { NextSunday = NextSunday.AddDays(1); } var listofFixed = new List <FinalShift>(); if ((db.FinalShift.Where(s => s.Dates > NextSunday.Date).Count() <= totalshifts / 2) && db.FinalShift.Count() > 0) { foreach (var shift in db.FinalShift.Where(e => e.Dates > NextSunday.Date).ToList()) { listofFixed.Add(shift); } } ViewBag.FixedList = listofFixed; var xyz = listofFixed.Where(w => w.Day == FixedShift.Day && w.Name == FixedShift.Name).Count(); if (xyz > 0) { ModelState.AddModelError("Name", "This employee has already been placed on this day"); FixedShift.Employees = employ; return(View(FixedShift)); } if (FixedShift.Morning == true) { var x = db.ScheduleParameters.Select(c => c.Morning).FirstOrDefault(); var w = db.ScheduleParameters.Where(d => d.Day == FixedShift.Day).Select(d => d.DMorning).FirstOrDefault(); if (w == null) { w = 0; } var p = db.FinalShift.Where(f => f.Day == FixedShift.Day && f.Morning == true).Count(); if (p >= (x + w)) { ModelState.AddModelError("Night", "Maximum employees per shift. Please update the settings to allow more employees"); FixedShift.Employees = employ; return(View(FixedShift)); } } else if (FixedShift.Afternoon == true) { var x = db.ScheduleParameters.Select(c => c.Afternoon).FirstOrDefault(); var w = db.ScheduleParameters.Where(d => d.Day == FixedShift.Day).Select(d => d.DAfternoon).FirstOrDefault(); if (w == null) { w = 0; } var p = db.FinalShift.Where(f => f.Day == FixedShift.Day && f.Afternoon == true).Count(); if (p >= (x + w)) { ModelState.AddModelError("Night", "Maximum employees per shift, please update the settings to allow more employees"); FixedShift.Employees = employ; return(View(FixedShift)); } } else { var x = db.ScheduleParameters.Select(c => c.Night).FirstOrDefault(); var w = db.ScheduleParameters.Where(d => d.Day == FixedShift.Day).Select(d => d.DNight).FirstOrDefault(); if (w == null) { w = 0; } var p = db.FinalShift.Where(f => f.Day == FixedShift.Day && f.Night == true).Count(); if (p >= (x + w)) { ModelState.AddModelError("Night", "Maximum employees per shift, please update the settings to allow more employees"); FixedShift.Employees = employ; return(View(FixedShift)); } } bool flag = false; int i, y; Employees Emp = db.Employees.FirstOrDefault(x => x.FirstName == FixedShift.Name); for (i = 1; FSrespo.DayOfWeek(i) != FixedShift.Day; i++) { ; } y = FixedShift.Morning == true ? 0 : FixedShift.Afternoon == true ? 2 : 3; DayOfWeek day = new System.DayOfWeek(); string dday = FSrespo.DayOfWeek(i); for (int d = 0; d < 7; d++) { if (day.ToString() != dday) { day = (DayOfWeek)((d + 1) % 7); } else { break; } } FixedShift.Dates = FSrespo.NextWeeksDates(day); if (db.FinalShift.Count() <= totalshifts) { FSrespo.OfDayHandler(true, 0, 0); } try { if (Emp != null && ModelState.IsValid) { FixedShift.EmployID = Emp.ID; flag = true; FixedShift.OfDayType = FSrespo.OrderOfDayTypeHandler(i, y); db.FinalShift.Add(FixedShift); db.SaveChanges(); return(RedirectToAction("Fixed")); } } catch { if (flag) { ModelState.AddModelError("Name", "Error Saving this shift"); FixedShift.Employees = db.Employees.ToList(); return(View(FixedShift)); } ModelState.AddModelError("Name", "Employee's ID was not found or matched"); FixedShift.Employees = db.Employees.ToList(); return(View(FixedShift)); } return(View(FixedShift)); }