public ActionResult Delete(int?id) { if (id == null) { //return new HttpStatusCodeResult(HttpStatusCode.BadRequest); return(RedirectToAction("Index", "Home")); } CareCompany careCompany = db.CareCompanies.Find(id); if (careCompany == null) { return(HttpNotFound()); } // It should not be possible to delete passive Companies if (careCompany.IsActive == false) { return(RedirectToAction("Index")); } CareCompanyDeleteVM deleteVM = new CareCompanyDeleteVM(); deleteVM.CareCompanyId = careCompany.Id; deleteVM.CompanyName = careCompany.CompanyName; deleteVM.OrganisationNumber = careCompany.OrganisationNumber; deleteVM.StreetAddress = careCompany.StreetAddress; deleteVM.Postcode = careCompany.Postcode; deleteVM.City = careCompany.City; deleteVM.AccountNumber = careCompany.AccountNumber; deleteVM.CompanyPhoneNumber = careCompany.CompanyPhoneNumber; CollectiveAgreementHeader collectiveAgreement = db.CollectiveAgreementHeaders.Where(c => c.Id == careCompany.SelectedCollectiveAgreementId).SingleOrDefault(); if (collectiveAgreement != null) { deleteVM.CollectiveAgreementName = collectiveAgreement.Name; } deleteVM.CollectiveAgreementSpecName = careCompany.CollectiveAgreementSpecName; return(View(deleteVM)); }
//public ActionResult Create([Bind(Include = "Id,Name,StartDate,EndDate,PerHourUnsocialEvening,PerHourUnsocialNight,PerHourUnsocialWeekend,PerHourUnsocialHoliday,PerHourOnCallWeekday,PerHourOnCallWeekend,NewAgreement")] CollectiveAgreementCreateVM collectiveAgreement, string submitButton) public ActionResult Create(CollectiveAgreementCreateVM collectiveAgreement, string submitButton) { //Check if the time period overlaps with an existing time period for the same collective agreement if (!collectiveAgreement.NewAgreement) { var collectiveAgreementInfos = db.CollectiveAgreementInfos.Where(c => c.CollectiveAgreementHeaderId == collectiveAgreement.HeaderId).ToList(); bool noOverlapFound = true; int idx = 0; do { if (collectiveAgreement.StartDate >= collectiveAgreementInfos[idx].StartDate && collectiveAgreement.StartDate <= collectiveAgreementInfos[idx].EndDate) { ModelState.AddModelError("StartDate", "Datumet överlappar med en existerande period."); noOverlapFound = false; } idx++; } while (noOverlapFound && idx < collectiveAgreementInfos.Count()); noOverlapFound = true; idx = 0; do { if (collectiveAgreement.EndDate >= collectiveAgreementInfos[idx].StartDate && collectiveAgreement.EndDate <= collectiveAgreementInfos[idx].EndDate) { ModelState.AddModelError("EndDate", "Datumet överlappar med en existerande period."); noOverlapFound = false; } idx++; } while (noOverlapFound && idx < collectiveAgreementInfos.Count()); } //Check if a collective agreement with the same name already exists if (collectiveAgreement.NewAgreement) { if (db.CollectiveAgreementHeaders.Where(c => c.Name == collectiveAgreement.Name).FirstOrDefault() != null) { ModelState.AddModelError("Name", "Det finns redan ett avtal med samma namn."); } } //Check that the last date of the agreement is equal to or greater than the first date if (collectiveAgreement.StartDate.Date > collectiveAgreement.EndDate.Date) { ModelState.AddModelError("EndDate", "Kollektivavtalets slutdatum får inte vara tidigare än avtalets startdatum."); } if (ModelState.IsValid) { if (submitButton == "Spara") { int latestHeaderId = 1; if (collectiveAgreement.NewAgreement == true) { CollectiveAgreementHeader collectiveAgreementHeader = new CollectiveAgreementHeader(); collectiveAgreementHeader.Name = collectiveAgreement.Name; collectiveAgreementHeader.Counter = 1; db.CollectiveAgreementHeaders.Add(collectiveAgreementHeader); db.SaveChanges(); latestHeaderId = db.CollectiveAgreementHeaders.ToList().Last().Id; } else { var header = db.CollectiveAgreementHeaders.Where(c => c.Id == collectiveAgreement.HeaderId).FirstOrDefault(); header.Counter++; db.Entry(header).State = EntityState.Modified; } CollectiveAgreementInfo collectiveAgreementInfo = new CollectiveAgreementInfo(); collectiveAgreementInfo.StartDate = collectiveAgreement.StartDate; collectiveAgreementInfo.EndDate = collectiveAgreement.EndDate; collectiveAgreementInfo.PerHourUnsocialEvening = collectiveAgreement.PerHourUnsocialEvening; collectiveAgreementInfo.PerHourUnsocialNight = collectiveAgreement.PerHourUnsocialNight; collectiveAgreementInfo.PerHourUnsocialWeekend = collectiveAgreement.PerHourUnsocialWeekend; collectiveAgreementInfo.PerHourUnsocialHoliday = collectiveAgreement.PerHourUnsocialHoliday; collectiveAgreementInfo.PerHourOnCallWeekday = collectiveAgreement.PerHourOnCallWeekday; collectiveAgreementInfo.PerHourOnCallWeekend = collectiveAgreement.PerHourOnCallWeekend; if (collectiveAgreement.NewAgreement == true) { collectiveAgreementInfo.CollectiveAgreementHeaderId = latestHeaderId; } else { collectiveAgreementInfo.CollectiveAgreementHeaderId = collectiveAgreement.HeaderId; } db.CollectiveAgreementInfos.Add(collectiveAgreementInfo); db.SaveChanges(); } } else { return(View(collectiveAgreement)); } return(RedirectToAction("Index")); }