Ejemplo n.º 1
0
        // GET: ProductInGames/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }


            ProductInGame model = db.ProductInGames.Include(x => x.ProductLocations).Where(x => x.ProductInGameID == id).First();

            model = (ProductInGame)Helpers.TableTracker.TrackEdit(model, "USR");


            if (model == null)
            {
                return(HttpNotFound());
            }

            ProductInGameViewModel productInGameViewModel = new ProductInGameViewModel {
                ProductInGame = model, CurrentQuantity = model.Quantity, ProductLocations = model.ProductLocations
            };

            ViewBag.ProductOwnerID = new SelectList(db.Owners, "OwnerID", "OwnerCode");
            ViewBag.CurrencyID     = new SelectList(db.Currencies, "CurrencyID", "CurrencyCode", model.CurrencyID);
            ViewBag.GameID         = new SelectList(db.Games, "GameID", "GameCode", model.GameID);
            ViewBag.ProductID      = new SelectList(db.Products, "ProductID", "ProductSKUCode", model.ProductID);
            return(View(productInGameViewModel));
        }
Ejemplo n.º 2
0
        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));
        }