public ActionResult CreateForGame([Bind(Include = "ProductInGameID,ProductID, ReferencePrice, PriceInGame,GameID,Quantity,CurrencyID,DateInserted,DateUpdated,USR")] ProductInGame productInGame) { ProductInGameValidator validator = new ProductInGameValidator(); if (ModelState.IsValid) { //Deduct quantity from Product table if stock available //@Patrick 6) Depletes the product Quantity from the "Available Stock" amount on the Product record. //Available Stock must not go negative (Validator checks for this) //Get Product var product = db.Products.Where(x => x.ProductID == productInGame.ProductID).First(); //Deplete available stock on hand product.AvailableSOH -= productInGame.Quantity; //Save if (product.AvailableSOH >= 0) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); } else { ModelState.AddModelError("QuantityError", "Not enough stock on hand for this product"); ViewBag.CurrencyID = new SelectList(db.Currencies, "CurrencyID", "CurrencyCode", productInGame.CurrencyID); ViewBag.GameID = new SelectList(db.Games, "GameID", "GameCode", productInGame.GameID); ViewBag.ProductID = new SelectList(db.Products, "ProductID", "ProductSKUCode", productInGame.ProductID); return(View(productInGame)); } db.ProductInGames.Add(productInGame); db.SaveChanges(); return(RedirectToAction("EditMakeGame", "Games", new { id = productInGame.GameID })); } FluentValidation.Results.ValidationResult results = validator.Validate(productInGame); IList <ValidationFailure> failures = results.Errors; StringBuilder sb = new StringBuilder(); foreach (var e in results.Errors) { ModelState.AddModelError(e.PropertyName + "Error", e.ErrorMessage); sb.AppendLine(e.ErrorMessage); } ViewBag.CurrencyID = new SelectList(db.Currencies, "CurrencyID", "CurrencyCode", productInGame.CurrencyID); ViewBag.GameID = new SelectList(db.Games, "GameID", "GameCode", productInGame.GameID); ViewBag.ProductID = new SelectList(db.Products, "ProductID", "ProductSKUCode", productInGame.ProductID); return(View(productInGame)); }
public ActionResult Edit(ProductInGameViewModel productInGameViewModel) { ProductInGameValidator validator = new ProductInGameValidator(); if (ModelState.IsValid && validator.Validate(productInGameViewModel.ProductInGame).IsValid) { //Deduct quantity from Product table if stock available //@Patrick 6) Depletes the product Quantity from the "Available Stock" amount on the Product record. //Available Stock must not go negative (Validator checks for this) //Get Product var product = db.Products.Where(x => x.ProductID == productInGameViewModel.ProductInGame.ProductID).First(); //"return" current quantity product.AvailableSOH += productInGameViewModel.CurrentQuantity; //Deplete available stock on hand with new quantity product.AvailableSOH -= productInGameViewModel.ProductInGame.Quantity; //Save if (product.AvailableSOH >= 0 && productInGameViewModel.ProductInGame.Quantity >= 0) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); } else { if (product.AvailableSOH < 0) { ModelState.AddModelError("QuantityError", "Not enough stock on hand for this product"); } if (productInGameViewModel.ProductInGame.Quantity < 0) { ModelState.AddModelError("QuantityError", "Quantity cannot be less than 0"); } ViewBag.ProductOwnerID = new SelectList(db.Owners, "OwnerID", "OwnerCode", productInGameViewModel.ProductOwnerID); ViewBag.CurrencyID = new SelectList(db.Currencies, "CurrencyID", "CurrencyCode", productInGameViewModel.ProductInGame.CurrencyID); ViewBag.GameID = new SelectList(db.Games, "GameID", "GameCode", productInGameViewModel.ProductInGame.GameID); ViewBag.ProductID = new SelectList(db.Products, "ProductID", "ProductSKUCode", productInGameViewModel.ProductInGame.ProductID); return(View(productInGameViewModel)); } if (productInGameViewModel.ProductLocations != null) { foreach (var d in productInGameViewModel.ProductLocations) { db.Entry(d).State = EntityState.Modified; } } db.Entry(productInGameViewModel.ProductInGame).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } FluentValidation.Results.ValidationResult results = validator.Validate(productInGameViewModel.ProductInGame); IList <ValidationFailure> failures = results.Errors; StringBuilder sb = new StringBuilder(); foreach (var e in results.Errors) { ModelState.AddModelError(e.PropertyName + "Error", e.ErrorMessage); sb.AppendLine(e.ErrorMessage); } ViewBag.ProductOwnerID = new SelectList(db.Owners, "OwnerID", "OwnerCode", productInGameViewModel.ProductOwnerID); ViewBag.CurrencyID = new SelectList(db.Currencies, "CurrencyID", "CurrencyCode", productInGameViewModel.ProductInGame.CurrencyID); ViewBag.GameID = new SelectList(db.Games, "GameID", "GameCode", productInGameViewModel.ProductInGame.GameID); ViewBag.ProductID = new SelectList(db.Products, "ProductID", "ProductSKUCode", productInGameViewModel.ProductInGame.ProductID); return(View(productInGameViewModel)); }
public ActionResult EditMakeGame(GameViewModel gameVM) { StringBuilder sb = new StringBuilder(); //HORRIBLE HACK // gameVM.Game.NumberOfWinners = 1; if (ModelState.IsValid) { db.Entry(gameVM.Game).State = EntityState.Modified; db.SaveChanges(); if (gameVM.ProductInGames != null) { foreach (var d in gameVM.ProductInGames) { ProductInGameValidator validator = new ProductInGameValidator(); FluentValidation.Results.ValidationResult results = validator.Validate(d); IList <ValidationFailure> failures = results.Errors; foreach (var e in results.Errors) { ModelState.AddModelError(e.PropertyName + "Error", e.ErrorMessage); sb.AppendLine(e.ErrorMessage); } if (ModelState.IsValid) { db.Entry(d).State = EntityState.Modified; db.SaveChanges(); foreach (var l in d.ProductLocations) { db.Entry(l).State = EntityState.Modified; db.SaveChanges(); } } else { ViewBag.ErrorMessage = sb.ToString(); ViewBag.GameTypeID = new SelectList(db.GameTypes, "GameTypeID", "GameTypeCode", gameVM.Game.GameTypeID); ViewBag.GameTypeIDList = new SelectList(db.GameTypes, "GameTypeID", "GameTypeCode", gameVM.Game.GameTypeID); ViewBag.GameID = new SelectList(db.NextGames, "GameID", "USR", gameVM.Game.GameID); ViewBag.CurrencyIDList = new SelectList(db.Currencies, "CurrencyID", "CurrencyCode"); ViewBag.MemberSubscriptionType = new SelectList(db.MemberSubscriptionTypes, "MemberSubscriptionTypeID", "MemberSubscriptionTypeDescription", gameVM.Game.MemberSubscriptionType); ViewBag.Products = db.Products; ViewBag.Source = "EditMakeGame"; gameVM.ProductInGames = db.ProductInGames.Where(x => x.GameID == gameVM.Game.GameID); ViewBag.Hours = new SelectList(getHours(), "Value", "text"); ViewBag.Minutes = new SelectList(getMinutes(), "Value", "text"); ViewBag.Seconds = new SelectList(getSeconds(), "Value", "text"); return(View(gameVM)); } } } if (gameVM.GameRules != null) { //Removed this to enable Long running games // DateTime rule1DateTime = gameVM.GameRules.Single(gr => gr.GameRuleCode.ToLower() == "preparegamerule").ExcecuteTime; //DateTime rule1DateTime = db.GameRules.Include(x=>x.GameTemplate).Where(x=>x.GameTemplate.OrderInGame ==1).First().ExcecuteTime; // DateTime rulesDateTime = db.games foreach (var rule in gameVM.GameRules) { //all rules must run on same date as rule 1 WHat about TimeZones rule.ExcecuteTime = DateTime.Parse(rule.ExcecuteTime.ToString("yyyy/MM/dd") + " " + rule.ExecuteHhMmSs); db.Entry(rule).State = EntityState.Modified; db.SaveChanges(); } } return(RedirectToAction("IndexMakeGame")); } ViewBag.CurrencyIDList = new SelectList(db.Currencies, "CurrencyID", "CurrencyCode"); ViewBag.GameTypeID = new SelectList(db.GameTypes, "GameTypeID", "GameTypeCode", gameVM.Game.GameTypeID); ViewBag.GameTypeIDList = new SelectList(db.GameTypes, "GameTypeID", "GameTypeCode", gameVM.Game.GameTypeID); ViewBag.GameID = new SelectList(db.NextGames, "GameID", "USR", gameVM.Game.GameID); ViewBag.ProductID = new SelectList(db.Products, "ProductID", "ProductSKUCode"); ViewBag.MemberSubscriptionType = new SelectList(db.MemberSubscriptionTypes, "MemberSubscriptionTypeID", "MemberSubscriptionTypeDescription", gameVM.Game.MemberSubscriptionType); return(View(gameVM)); }