public ActionResult DeleteConfirmed(CollectiveAgreementDeleteVM collectiveAgreementDeleteVM, string submitButton) { if (submitButton == "Bekräfta") { if (ModelState.IsValid) { var header = db.CollectiveAgreementHeaders.Where(c => c.Id == collectiveAgreementDeleteVM.CollAgreementHeader.Id).FirstOrDefault(); if (collectiveAgreementDeleteVM.Type == "header") { //Remove the collective agreement header db.CollectiveAgreementHeaders.Remove(header); //Remove the collective agreement info records var collAgreementInfos = db.CollectiveAgreementInfos.Where(c => c.CollectiveAgreementHeaderId == collectiveAgreementDeleteVM.CollAgreementHeader.Id).ToList(); db.CollectiveAgreementInfos.RemoveRange(collAgreementInfos); } else if (collectiveAgreementDeleteVM.Type == "info") { int infoId = collectiveAgreementDeleteVM.CollAgreementInfo[0].Id; var collAgreementInfo = db.CollectiveAgreementInfos.Where(c => c.Id == infoId).FirstOrDefault(); db.CollectiveAgreementInfos.Remove(collAgreementInfo); header.Counter = header.Counter - 1; db.Entry(header).State = EntityState.Modified; } db.SaveChanges(); } } return(RedirectToAction("Index")); }
// GET: CollectiveAgreements/Delete/5 public ActionResult Delete(string type, int?headerId, int?infoId) { // It should not be possible to delete the last "Kollektivavtal" if (db.CollectiveAgreementHeaders.Count() < 2 && type == "header") { return(RedirectToAction("Index")); } if (headerId != null) { CollectiveAgreementDeleteVM collectiveAgreementDeleteVM = new CollectiveAgreementDeleteVM(); collectiveAgreementDeleteVM.Type = type; CollAgreementHeader collAgreementHeader = new CollAgreementHeader(); collectiveAgreementDeleteVM.CollAgreementHeader = collAgreementHeader; var collectiveAgreementHeader = db.CollectiveAgreementHeaders.Where(c => c.Id == headerId).FirstOrDefault(); // It should not be possible to delete the last Time Period in a "Kollektivavtal" if (collectiveAgreementHeader.Counter < 2 && type == "info") { return(RedirectToAction("Index")); } collectiveAgreementDeleteVM.CollAgreementHeader.Id = collectiveAgreementHeader.Id; collectiveAgreementDeleteVM.CollAgreementHeader.Name = collectiveAgreementHeader.Name; collectiveAgreementDeleteVM.CollAgreementHeader.Counter = collectiveAgreementHeader.Counter; if (type == "header") { List <CollAgreementInfo> collAgreementInfos = new List <CollAgreementInfo>(); collectiveAgreementDeleteVM.CollAgreementInfo = collAgreementInfos; var collectiveAgreementInfos = db.CollectiveAgreementInfos.Where(c => c.CollectiveAgreementHeaderId == collectiveAgreementHeader.Id).ToList(); foreach (var collectiveAgreementInfo in collectiveAgreementInfos) { CollAgreementInfo collAgreementInfo = new CollAgreementInfo(); collAgreementInfo.Id = collectiveAgreementInfo.Id; collAgreementInfo.StartDate = collectiveAgreementInfo.StartDate.ToShortDateString(); collAgreementInfo.EndDate = collectiveAgreementInfo.EndDate.ToShortDateString(); collAgreementInfo.PerHourUnsocialEvening = collectiveAgreementInfo.PerHourUnsocialEvening; collAgreementInfo.PerHourUnsocialNight = collectiveAgreementInfo.PerHourUnsocialNight; collAgreementInfo.PerHourUnsocialWeekend = collectiveAgreementInfo.PerHourUnsocialWeekend; collAgreementInfo.PerHourUnsocialHoliday = collectiveAgreementInfo.PerHourUnsocialHoliday; collAgreementInfo.PerHourOnCallWeekday = collectiveAgreementInfo.PerHourOnCallWeekday; collAgreementInfo.PerHourOnCallWeekend = collectiveAgreementInfo.PerHourOnCallWeekend; collectiveAgreementDeleteVM.CollAgreementInfo.Add(collAgreementInfo); } } else if (type == "info" && infoId != null) { List <CollAgreementInfo> collAgreementInfos = new List <CollAgreementInfo>(); collectiveAgreementDeleteVM.CollAgreementInfo = collAgreementInfos; var collectiveAgreementInfos = db.CollectiveAgreementInfos.Where(c => c.Id == infoId).FirstOrDefault(); CollAgreementInfo collAgreementInfo = new CollAgreementInfo(); collAgreementInfo.Id = collectiveAgreementInfos.Id; collAgreementInfo.StartDate = collectiveAgreementInfos.StartDate.ToShortDateString(); collAgreementInfo.EndDate = collectiveAgreementInfos.EndDate.ToShortDateString(); collAgreementInfo.PerHourUnsocialEvening = collectiveAgreementInfos.PerHourUnsocialEvening; collAgreementInfo.PerHourUnsocialNight = collectiveAgreementInfos.PerHourUnsocialNight; collAgreementInfo.PerHourUnsocialWeekend = collectiveAgreementInfos.PerHourUnsocialWeekend; collAgreementInfo.PerHourUnsocialHoliday = collectiveAgreementInfos.PerHourUnsocialHoliday; collAgreementInfo.PerHourOnCallWeekday = collectiveAgreementInfos.PerHourOnCallWeekday; collAgreementInfo.PerHourOnCallWeekend = collectiveAgreementInfos.PerHourOnCallWeekend; collectiveAgreementDeleteVM.CollAgreementInfo.Add(collAgreementInfo); } return(View(collectiveAgreementDeleteVM)); } else { return(View()); } }