private bool Modified(TblTempTimecards timecard, TblTimecards original) { return(original.TimeDate != timecard.TimeDate || original.ProjectId != timecard.ProjectId || original.SpecId != timecard.SpecId || original.HourType != timecard.HourType || original.HourTime != timecard.HourTime); }
public IActionResult Save(EditViewModel editModel) { // Get the timecard from the edit VM TblTimecards timecard = editModel.TimeCard; // Check if the original entry is in the temp table - if so, grab the last updated one var tempcard = context.TblTempTimecards.Where(x => x.TimeId == timecard.TimeId).GroupBy(x => x.TimeId).Select(x => x.LastOrDefault()).ToList(); bool modified = false; // If tempcard is empty, then the original entry is not in the temp table but in the live table if (tempcard.Count == 0) { var tmcard = context.TblTimecards.Find(timecard.TimeId); modified = Modified(tmcard, timecard); } else { // Should probably do something here if the entry for the timecard is marked for delete if (tempcard[0].Delete) { return(View("Index")); } modified = Modified(tempcard[0], timecard); } // If there have been changes from what's in the database, update it and add to the temp table if (modified) { TblTempTimecards editToTemp = new TblTempTimecards { EmployeeId = 77, TimeId = timecard.TimeId, TimePeriodId = (int)timecard.TimePeriodId, TimeDate = timecard.TimeDate, ProjectId = timecard.ProjectId, SpecId = timecard.SpecId, HourTime = timecard.HourTime, HourType = timecard.HourType, OnRoad = false, EditedDate = DateTime.UtcNow, Edit = true }; context.TblTempTimecards.Add(editToTemp); context.SaveChanges(); return(RedirectToAction("Index", new { actionDone = "edit" })); } return(RedirectToAction("Index")); }
public IActionResult AddTimeCard() { var newTimecard = new TblTimecards(); EditViewModel editModel = new EditViewModel(); // Convert the timecard to an editVM editModel.TimeCard = newTimecard; // Get the start and end dates var payPeriodDates = context.TblTimecardHeader.Where(x => x.Validated == false).Select(x => new { x.PayPeriodStartDate, x.PayPeriodEndDate }).ToList(); editModel.DateRange = editModel.PopulateDatesInPayPeriod(payPeriodDates[0].PayPeriodStartDate, payPeriodDates[0].PayPeriodEndDate); // Populate the select list items with the projects, stations, and labor codes from the database and assign to VM var projects = context.TblProjects.Select(x => new SelectListItem { Text = $@"{ x.ProjectId.ToString() } - { x.Pdescription.Substring(0, Math.Min(x.Pdescription.ToString().Length, 50)) }", Value = x.ProjectId.ToString() }).ToList(); //projects.Insert(0, new SelectListItem { Text = "-- Select a Project --", Value = "" , Disabled = true}); var stations = context.TblSpec.Where(x => x.ProjectId == editModel.TimeCard.ProjectId).Select(x => new SelectListItem { Text = $@"{ x.SpecId.ToString() } - { x.Sdescription }", Value = x.SpecId.ToString() }).ToList(); //stations.Insert(0, new SelectListItem { Text = "-- Select a Station --", Value = "", Selected = true }); var hours = context.TlkpHourTypes.Select(x => new SelectListItem { Text = $@"{ x.HourType.ToString() } - { x.HourDescription }", Value = x.HourType.ToString() }).ToList(); //hours.Insert(0, new SelectListItem { Text = "-- Select a Labor Code --", Value = "" , Disabled = true}); editModel.Projects = projects; editModel.Stations = stations; editModel.LaborCodes = hours; return(PartialView("AddTimeCard", editModel)); }
public IActionResult EditTimeCard(int id) { if (id == 0) { return(View("Index")); } // if the TimeId exists in the temp table bool inTempTable = context.TblTempTimecards.Any(x => x.TimeId == id); var timecard = new TblTimecards(); if (inTempTable) { // if TimeId is in the table, grab the most recent one and assign that info to the timecard var tempcard = context.TblTempTimecards.Include(x => x.Spec).Include(x => x.Project).Include(x => x.HourTypeNavigation).Where(x => x.TimeId == id).GroupBy(x => x.TimeId).Select(x => x.LastOrDefault()).FirstOrDefault(); timecard.TimeId = tempcard.TimeId; timecard.TimeDate = tempcard.TimeDate; timecard.EmployeeId = tempcard.EmployeeId; timecard.ProjectId = tempcard.ProjectId; timecard.SpecId = tempcard.SpecId; timecard.HourType = tempcard.HourType; timecard.HourTime = tempcard.HourTime; timecard.TimePeriodId = tempcard.TimePeriodId; timecard.TblSpec = tempcard.Spec; timecard.TblProjects = tempcard.Project; timecard.HourTypeNavigation = tempcard.HourTypeNavigation; } else { // if TimeId is not in the temp table, grab it from the live table and assign to timecard timecard = context.TblTimecards.Include(tc => tc.TblSpec) .Include(tc => tc.TblProjects) .Include(tc => tc.HourTypeNavigation) .Where(x => x.TimeId == id).FirstOrDefault(); } // Convert the timecard to an editVM that has all the necessary stuff to view the edit page EditViewModel editModel = new EditViewModel(); editModel.TimeCard = timecard; // Get the start and end dates for the pay period to then populate all the dates for the pay period var payPeriodDates = context.TblTimecardHeader.Where(x => x.Validated == false).Select(x => new { x.PayPeriodStartDate, x.PayPeriodEndDate }).ToList(); editModel.DateRange = editModel.PopulateDatesInPayPeriod(payPeriodDates[0].PayPeriodStartDate, payPeriodDates[0].PayPeriodEndDate); // Populate the select list items with the projects, stations, and labor codes from the database and assign to VM var projects = context.TblProjects.Select(x => new SelectListItem { Text = $@"{ x.ProjectId.ToString() } - { x.Pdescription.Substring(0, Math.Min(x.Pdescription.ToString().Length, 50)) }", Value = x.ProjectId.ToString() }).ToList(); var stations = context.TblSpec.Where(x => x.ProjectId == editModel.TimeCard.ProjectId).Select(x => new SelectListItem { Text = $@"{ x.SpecId.ToString() } - { x.Sdescription }", Value = x.SpecId.ToString() }).ToList(); var hours = context.TlkpHourTypes.Select(x => new SelectListItem { Text = $@"{ x.HourType.ToString() } - { x.HourDescription }", Value = x.HourType.ToString() }).ToList(); editModel.Projects = projects; editModel.Stations = stations; editModel.LaborCodes = hours; return(PartialView("EditTimeCard", editModel)); }