public ActionResult Create(BundlesViewModel vm) { if (Session["isAdmin"] == null || !(bool)Session["isAdmin"]) return RedirectToAction("Index", new { controller = "Home", action = "Index" }); if (vm!=null || vm.SchoolsCheckboxes!=null) { if (vm.BundleName == null || vm.BundleName.Trim().Equals("")) { TempData["error"] = "Bundle Must Have a name"; return RedirectToAction("Index"); } IList<int> checkedSchools = new List<int>(); var labels = db.Labels.ToList(); int numSelected = vm.SchoolsCheckboxes.Where(x => x).Count(); int index = 0; foreach (bool isChecked in vm.SchoolsCheckboxes) { if (isChecked) checkedSchools.Add(vm.Schools[index].SchoolId); index++; } vm.SelectedSchoolIds = checkedSchools; TempData["BundlesViewModel"] = vm; return RedirectToAction("ItemTypesSelect"); } return RedirectToAction("Index"); }
// GET: Bundles/Create public ActionResult Create() { if (Session["isAdmin"] == null || !(bool)Session["isAdmin"]) return RedirectToAction("Index", new { controller = "Home", action = "Index" }); //Checkbox for every school IList<bool> schools = new List<bool>(); foreach (var school in db.Schools) { schools.Add(true); //Default to true (checked) } BundlesViewModel vm = new BundlesViewModel { Schools = db.Schools.ToList(), SchoolsCheckboxes = schools }; return View(vm); }
public ActionResult ItemTypesSubmit(BundlesViewModel vm) { if (Session["isAdmin"] == null || !(bool)Session["isAdmin"]) return RedirectToAction("Index", new { controller = "Home", action = "Index" }); if (vm!=null || vm.ItemTypesCheckboxes != null) { int numSelected = vm.ItemTypesCheckboxes.Where(x => x).ToList().Count; if (numSelected == 0) { ModelState.AddModelError("", "At least one Item Type must be selected"); return View(vm); } List<ItemTypes> selectedItemTypes = new List<ItemTypes>(); int index = 0; foreach (bool selectedItem in vm.ItemTypesCheckboxes) { if (selectedItem) { selectedItemTypes.Add(vm.ItemTypes[index]); } index++; } vm.SelectedItemTypes = selectedItemTypes; TempData["BundlesViewModel"] = vm; return RedirectToAction("ItemQuantitySelect"); } else return RedirectToAction("Index"); }
public ActionResult ItemQuantitySubmit(BundlesViewModel vm) { if (Session["isAdmin"] == null || !(bool)Session["isAdmin"]) return RedirectToAction("Index", new { controller = "Home", action = "Index" }); if (vm == null) return RedirectToAction("Index"); var itemTypes = db.ItemTypes.ToList(); var labels = db.Labels.ToList(); List<Bundles> tempBundle = new List<Bundles>(); //Temporarily hold new bundles IList<Items> tempItems = new List<Items>(); //Temporarily hold new items int numSchools = vm.SelectedSchoolIds.Count; string itemTypeName = ""; //Create the Bundles //For Every Selected School foreach (int schoolId in vm.SelectedSchoolIds) { bool canCreate = true; //For Every Selected Item Type for (int i = 0; i < vm.SelectedItemTypes.Count; i++) { var itemType = db.ItemTypes.Find(vm.SelectedItemTypes[i].ItemTypeId); if (itemType == null) { TempData["error"] = "An Item Type has been removed"; return RedirectToAction("Index"); } var available = itemType.Item.Where(x => x.CheckedInById == null); //Can't be checked in available = available.Where(x => x.CheckedOutById == null); //checked out available = available.Where(x => x.BundleId == null); //or assigned to a bundle if(itemType.HasLabel) available = available.Where(x => x.LabelId == schoolId); var availableItems = available.Where(item => tempItems.IndexOf(item) < 0).ToList(); int numAvailable = availableItems.Count(); int desiredQuantity = vm.ItemQuantityFields[i]; if (desiredQuantity < 1) { TempData["error"] = "Quantity must be at least 1"; return RedirectToAction("Index"); } if(numAvailable >= desiredQuantity) { for (int j = 0; j < desiredQuantity; j++) { tempItems.Add(availableItems[j]); } } //Stop The Process else { itemTypeName = itemType.ItemName; canCreate = false; //Not enough items to match demand break; } } //Create Temporary Bundle if (canCreate) { var school = db.Schools.Find(schoolId); var newBundle = new Bundles { BundleName = vm.BundleName + " (" + school.SchoolName +")", SchoolId = schoolId }; tempBundle.Add(newBundle); } else { TempData["error"] = "Not enough " + itemTypeName +"s available to meet demand"; return RedirectToAction("Index"); } } //Add all the temp bundles to the database for (int i = 0; i < tempBundle.Count; i++) { db.Bundles.Add(tempBundle[i]); db.SaveChanges(); } //Associate Temporary Items with bundle id and save int currItemIndex = 0; for (int i = 0; i < tempBundle.Count; i++) //For every bundle { for(int j=0; j<vm.SelectedItemTypes.Count; j++) //For every selected item type { for (int k = 0; k < vm.ItemQuantityFields[j]; k++) //For every Selected Item type quantity { var dbItem = db.Items.Find(tempItems[currItemIndex].ItemId); if (dbItem == null || dbItem.BundleId != null) { for (int m = 0; m < tempBundle.Count; m++) DeleteBundle(tempBundle[m].BundleId); TempData["error"] = "Item has been assigned to another bundle or it was removed"; return RedirectToAction("Index"); } dbItem.BundleId = tempBundle[i].BundleId; db.Entry(dbItem).State = EntityState.Modified; db.SaveChanges(); currItemIndex++; } } } return RedirectToAction("Index"); }