public ActionResult Add() { AddOrderVM model = new AddOrderVM(); model.OrderConditionID = 2; model.Drivers = _dropdownMaker.GetDrivers(); model.Vehicles = _dropdownMaker.GetVehicles(); model.Tasks = _dropdownMaker.GetUnattachedTasks(); model.DriverStops = _dropdownMaker.GetDriverStops(); model.TaskAndDriverStopNumbers = new List <AssignTaskAndDriverStopNumbersInOrderVM>(); return(View(viewName: "Add", model: model)); }
public ActionResult AddSetTaskAndDriverStopNumbers(int[] taskIDs, int?[] driverStopIDs) { AddOrderVM model = new AddOrderVM(); model.TaskAndDriverStopNumbers = new List <AssignTaskAndDriverStopNumbersInOrderVM>(); using (TruckTransportDbContext _db = new TruckTransportDbContext()) { foreach (var taskID in taskIDs) { AssignTaskAndDriverStopNumbersInOrderVM taskAndDriverStopNumberModel = new AssignTaskAndDriverStopNumbersInOrderVM(); var task = _db.zadaci.Where(x => x.zadatak_id == taskID).AsNoTracking().FirstOrDefault(); taskAndDriverStopNumberModel.ID = task.zadatak_id; taskAndDriverStopNumberModel.Name = task.naziv; taskAndDriverStopNumberModel.IsTask = true; taskAndDriverStopNumberModel.Number = task.broj_zadatka; model.TaskAndDriverStopNumbers.Add(taskAndDriverStopNumberModel); } if (driverStopIDs != null) { foreach (var driverStopID in driverStopIDs) { if (driverStopID != null) { AssignTaskAndDriverStopNumbersInOrderVM taskAndDriverStopNumberModel = new AssignTaskAndDriverStopNumbersInOrderVM(); var driverStop = _db.stajalista.Where(x => x.stajaliste_id == driverStopID).AsNoTracking().FirstOrDefault(); taskAndDriverStopNumberModel.ID = driverStop.stajaliste_id; taskAndDriverStopNumberModel.Name = driverStop.naziv; taskAndDriverStopNumberModel.IsTask = false; taskAndDriverStopNumberModel.Number = 1; model.TaskAndDriverStopNumbers.Add(taskAndDriverStopNumberModel); } } } } return(PartialView(viewName: "_AddAssignTaskAndDriverStopNumbers", model: model)); }
public ActionResult Add(AddOrderVM model) { if (model.TaskIDs != null && model.DriverStopIDs != null) { if (model.TaskIDs.Length > 10 || model.DriverStopIDs.Length > 10) { ModelState.AddModelError("", "Broj zadataka i broj stajališta ne može biti veći od 10!"); } else if (model.TaskAndDriverStopNumbers == null) { ModelState.AddModelError("", "Redni brojevi za sve odabrane zadatke i stajališta nisu dodijeljeni!"); } else if (model.TaskAndDriverStopNumbers != null && model.TaskIDs.Length + model.DriverStopIDs.Length > model.TaskAndDriverStopNumbers.Count) { ModelState.AddModelError("", "Redni brojevi za sve odabrane zadatke i stajališta nisu dodijeljeni!"); } } if (model.TaskIDs != null && model.DriverStopIDs == null) { if (model.TaskIDs.Length > 2) { ModelState.AddModelError("", "Stajalište je obavezno ako je broj zadataka veći od 2!"); } if (model.TaskAndDriverStopNumbers == null) { ModelState.AddModelError("", "Redni brojevi za sve odabrane zadatke nisu dodijeljeni!"); } else if (model.TaskIDs.Length > model.TaskAndDriverStopNumbers.Count) { ModelState.AddModelError("", "Redni brojevi za sve odabrane zadatke nisu dodijeljeni!"); } } //check difference between numbers in list if (model.TaskAndDriverStopNumbers != null) { if (model.TaskAndDriverStopNumbers.Count != 0) { var descOrderedNumbers = model.TaskAndDriverStopNumbers.OrderByDescending(x => x.Number); if (descOrderedNumbers.Last().Number != 1) { ModelState.AddModelError("", "Najmanji redni broj mora biti 1!"); } int orderNumberErrorCounter = 0; for (int i = 0; i < descOrderedNumbers.Count(); i++) { if (descOrderedNumbers.Count() > 1 && (i + 1) < descOrderedNumbers.Count()) { int difference = descOrderedNumbers.ElementAt(i).Number - descOrderedNumbers.ElementAt(i + 1).Number; if (difference != 1 && orderNumberErrorCounter == 0) { orderNumberErrorCounter = 1; ModelState.AddModelError("", "Redni brojevi nisu ispravno raspoređeni. Razlika između dva broja mora biti 1!"); } } } } } if (ModelState.IsValid) { try { using (TruckTransportDbContext _db = new TruckTransportDbContext()) { nalozi nalogDB = new nalozi(); nalogDB.vrijeme_kreiranja = UnixTime.GetUnixTimeNow(); nalogDB.stanje_id = model.OrderConditionID; nalogDB.vozilo_id = model.VehicleID; nalogDB.vozac_id = model.DriverID; _db.nalozi.Add(nalogDB); _db.SaveChanges(); foreach (var taskID in model.TaskIDs) { zadaci taskDB = _db.zadaci.Where(x => x.zadatak_id == taskID).FirstOrDefault(); if (taskDB != null) { taskDB.nalog_id = nalogDB.nalog_id; taskDB.broj_zadatka = model.TaskAndDriverStopNumbers.Where(x => x.ID == taskDB.zadatak_id && x.IsTask).FirstOrDefault().Number; _db.Entry(taskDB).State = EntityState.Modified; } } if (model.DriverStopIDs != null) { List <stajalista_nalozi> driverStopsOrders = new List <stajalista_nalozi>(); foreach (var driverStopID in model.DriverStopIDs) { stajalista_nalozi driverStopOrderDB = new stajalista_nalozi(); driverStopOrderDB.stajaliste_id = (int)driverStopID; driverStopOrderDB.nalog_id = nalogDB.nalog_id; driverStopOrderDB.broj_stajalista = model.TaskAndDriverStopNumbers.Where(x => x.ID == driverStopOrderDB.stajaliste_id && !x.IsTask).FirstOrDefault().Number; driverStopsOrders.Add(driverStopOrderDB); } _db.stajalista_nalozi.AddRange(driverStopsOrders); } _db.SaveChanges(); return(RedirectToAction(actionName: "Index")); } } catch (Exception) { } } //delete selected values because of map init model.TaskIDs = null; model.DriverStopIDs = null; model.TaskAndDriverStopNumbers = new List <AssignTaskAndDriverStopNumbersInOrderVM>(); model.Drivers = _dropdownMaker.GetDrivers(); model.Vehicles = _dropdownMaker.GetVehicles(); model.Tasks = _dropdownMaker.GetUnattachedTasks(); model.DriverStops = _dropdownMaker.GetDriverStops(); return(View(viewName: "Add", model: model)); }