// GET: CollectiveAgreements/Create public ActionResult Create(int?id) { CollectiveAgreementCreateVM collectiveAgreementCreateVM = new CollectiveAgreementCreateVM(); if (id == null) //This is a new collective agreement { collectiveAgreementCreateVM.NewAgreement = true; } else //This is an existing collective agreement { collectiveAgreementCreateVM.NewAgreement = false; var collectiveAgreementHeader = db.CollectiveAgreementHeaders.Where(c => c.Id == id).FirstOrDefault(); collectiveAgreementCreateVM.HeaderId = collectiveAgreementHeader.Id; collectiveAgreementCreateVM.Name = collectiveAgreementHeader.Name; } collectiveAgreementCreateVM.StartDate = DateTime.Now.AddDays(1); collectiveAgreementCreateVM.EndDate = DateTime.Now.AddDays(1); return(View(collectiveAgreementCreateVM)); }
//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")); }