public async Task <IActionResult> Create(ProductKit_ViewModel model) { var productKit = new ProductKit(); productKit.EventId = model.EventId; productKit.Name = model.Name.Replace(" ", "-"); productKit.StyleId = model.StyleId; productKit.TerritoryId = model.TerritoryId; if (ModelState.IsValid) { _context.Add(productKit); await _context.SaveChangesAsync(); productKit.Name = productKit.Name + "-" + _productKitService.AddProductKitIdentifier(model, ref productKit); _context.Update(productKit); await _context.SaveChangesAsync(); _productKitService.AddInstances(model, productKit, 0); await _context.SaveChangesAsync(); //return RedirectToAction("Details", "Events", new { id = productKit.EventId }); return(RedirectToAction(nameof(Index))); } return(View(model)); }
public void AddInstances(ProductKit_ViewModel model, ProductKit productKit, int numInstancesRemovedFromEdit) { for (int i = 0; i < model.ProductNames.Count(); i++) { var numberExistingInstancesOfthisProduct = _context.ProductInstance.Where(pi => pi.ProductId == model.ProductIds[i]).Count(); for (int j = 0; j < model.Quantities[i]; j++) { //var identifier = numberExistingInstancesOfthisProduct + j; //identifier += numInstancesRemovedFromEdit; //do something like find how many product kits with this territory have these instances and //add to that number? //maybe identifier only matters in the context of a given event, so just do this on events _context.ProductInstance.Add(new ProductInstance { Name = model.ProductNames[i] /*+ "-" + identifier*/, ProductId = model.ProductIds[i], ProductKitId = productKit.Id, CheckedOut = false }); _context.SaveChanges(); //need to save the changes and then modify the name of the instance to have the new identifier? no how do i find that instance again? var thisInstance = _context.ProductInstance.LastOrDefault();//this is kind of a sketchy way of doing this? thisInstance.Name = thisInstance.Name + "-" + AddProductInstanceIdentifier(thisInstance.ProductId, thisInstance.Id); _context.Update(thisInstance); _context.SaveChanges(); } } }
public string AddProductKitIdentifier(ProductKit_ViewModel model, ref ProductKit productKit) { //i think that this should only be existing productkits for this style, no need to make it territory exclusive because different territories get combined on an //event but their instances need exclusive names nonetheless, WAIT no that is the case for the instances but not for the proeuct kits, where is the instance identifier //creator? var existingProductKitsForThisStyleAndTerritory = _context.ProductKit.Where(pk => pk.TerritoryId == model.TerritoryId && pk.StyleId == model.StyleId).ToList(); var existingProductKitIdentifiersForThisStyleAndTerritory = _context.ProductKitIdentifier.Where(pk => pk.TerritoryId == model.TerritoryId && pk.StyleId == model.StyleId).ToList(); ProductKitIdentifier idToUse = new ProductKitIdentifier(); if (existingProductKitIdentifiersForThisStyleAndTerritory.Count == 0) { _context.ProductKitIdentifier.Add(new ProductKitIdentifier { Identifier = 0,//if you want this to be 1 later then below count under if(!thereIsAnAvailableExistinIdentifier) needs to be plus 1 IsInUse = true, StyleId = model.StyleId, TerritoryId = model.TerritoryId, ProductKitId = productKit.Id }); _context.SaveChanges(); idToUse = _context.ProductKitIdentifier.FirstOrDefault();//if there are none add it and assign it to idtouse } else { var thereIsAnAvailableExistinIdentifier = false; foreach (var identifier in existingProductKitIdentifiersForThisStyleAndTerritory) { if (identifier.IsInUse == false) { thereIsAnAvailableExistinIdentifier = true; idToUse = identifier; identifier.ProductKitId = productKit.Id; identifier.IsInUse = true; _context.Update(identifier); //i think that this is necessary... _context.SaveChanges(); break; //not sure if this continue is breaking only out of this if else? that's all i want it to do } } if (!thereIsAnAvailableExistinIdentifier) { _context.ProductKitIdentifier.Add(new ProductKitIdentifier { Identifier = existingProductKitIdentifiersForThisStyleAndTerritory.Count(), IsInUse = true, StyleId = model.StyleId, TerritoryId = model.TerritoryId, ProductKitId = productKit.Id }); _context.SaveChanges(); idToUse = _context.ProductKitIdentifier.LastOrDefault();//set to last because we just added it } } return(idToUse.Identifier.ToString()); }
public async Task <IActionResult> EventEdit(int id, ProductKitAssignEvent_ViewModel model) { if (id != model.ProductKitId) { return(NotFound()); } ProductKit productKit = _context.ProductKit.FirstOrDefault(b => b.Id == id); productKit.EventId = model.EventId; var productInstances = _context.ProductInstance.Where(i => i.ProductKitId == productKit.Id).ToList(); List <int> productInstanceIds = new List <int>(); foreach (var instance in productInstances) { productInstanceIds.Add(instance.Id); } var customer_Instances = _context.ProductInstance_Customer.Where(pic => productInstanceIds.Contains(pic.ProductInstanceId)).ToList(); //_context.ProductInstance.RemoveRange(productInstances);//don't want to clear out instances, but i do want to set them back to checkedout = 0 (false) foreach (var instance in productInstances) { instance.CheckedOut = false; } _context.ProductInstance_Customer.RemoveRange(customer_Instances); if (ModelState.IsValid) { try { _context.Update(productKit); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductKitExists(productKit.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(model)); }
public Product Setup(Product product) { product.ProductKits.Clear(); if (ConvertUtilities.ToBoolean(uxIsProductKitDrop.SelectedValue)) { foreach (string groupID in uxMultiProductKitGroup.ConvertToProductKitGroupIDs()) { ProductKit productKit = new ProductKit(CurrentCulture); productKit.ProductKitGroupID = groupID; product.ProductKits.Add(productKit); } } product.IsProductKit = ConvertUtilities.ToBoolean(uxIsProductKitDrop.SelectedValue); return(product); }
public async Task <IActionResult> Edit(int id, ProductKit_ViewModel model) { if (id != model.Id) { return(NotFound()); } ProductKit productKit = _context.ProductKit.FirstOrDefault(p => p.Id == id); productKit.EventId = model.EventId; productKit.Name = model.Name.Replace(" ", "-"); productKit.StyleId = model.StyleId; productKit.TerritoryId = model.TerritoryId; //remove all of the existing product instances and customer_productinstnace entries, maybe this could be bad in the future and loss of data? what if they try //to edit the number of instances in a product kit after they've already saved a bunch of check out and check in info var productInstances = _context.ProductInstance.Where(i => i.ProductKitId == productKit.Id).ToList(); List <int> productInstanceIds = new List <int>(); foreach (var instance in productInstances) { productInstanceIds.Add(instance.Id); } var customer_Instances = _context.ProductInstance_Customer.Where(pic => productInstanceIds.Contains(pic.ProductInstanceId)).ToList(); //clear instance identifiers var instanceIdentifiers = _context.ProductInstanceIdentifier.Where(i => productInstanceIds.Contains((int)i.InstanceId)).ToList(); //AAAAAAAAAAAAAAAAAAAAAAAA foreach (var instanceIdentifer in instanceIdentifiers) //AAAAAAAAAAA whole loop { instanceIdentifer.IsInUse = false; instanceIdentifer.InstanceId = null; } //end clear instnace identifiers var numInstancesToBeRemoved = customer_Instances.Count(); _context.ProductInstance.RemoveRange(productInstances); _context.ProductInstance_Customer.RemoveRange(customer_Instances); //editing the product kit here is actually change the number of instances entirely, so I may have to delete all instances associated with this kit and //add in altogether new ones, rather than upate the ones that remained, because the whole point of editing the product kit is the change the number of instances //because what I am changing is the quantity of instnaces it is not a quality of a given instance that is being changed, so yeah they will all have to be //deleted, meaning that their associations with customers will also have to be deleted //ADDED //END if (ModelState.IsValid) { _context.Update(productKit); await _context.SaveChangesAsync(); _productKitService.AddInstances(model, productKit, numInstancesToBeRemoved); //for (int i = 0; i < model.ProductNames.Count(); i++)//for every product //{ // var numberExistingInstancesOfthisProduct = _context.ProductInstance.Where(pi => pi.ProductId == model.ProductIds[i]).Count(); // for (int j = 0; j < model.Quantities[i]; j++) // {//change where this j starts to say make identifiers start with 1 if you want // var identifier = numberExistingInstancesOfthisProduct + j; // _context.ProductInstance.Add(new ProductInstance // { // Name = model.ProductNames[i] + "-" + identifier, // ProductId = model.ProductIds[i], // ProductKitId = productKit.Id, // CheckedOut = false // }); // } //} await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); //return RedirectToAction("Details", "Events", new { id = model.EventId }); } return(View(model)); #region old //if (ModelState.IsValid) //{ // try // { // _context.Update(model); // await _context.SaveChangesAsync(); // } // catch (DbUpdateConcurrencyException) // { // if (!ProductKitExists(model.Id)) // { // return NotFound(); // } // else // { // throw; // } // } // return RedirectToAction(nameof(Index)); //} #endregion }