예제 #1
0
 public InventoryModel()
 {
     AllItems = GroceryItem.GetAllItemsAsList(ConnString);
     CurrentUser = new User(ConnString);
     NewItem = new GroceryItem(ConnString);
     ItemGrid = new Grid<GroceryItem>(AllItems);
     CurrentOrder = new Order(ConnString);
     OrderGrid = new Grid<IOrderableItem>(CurrentOrder.OrderItems);
     OrderHistoryGrid = new Grid<Order>(Order.GetOrderHistory(ConnString));
 }
예제 #2
0
        public ActionResult Inventory(InventoryModel model, string inventoryAction)
        {
            if (Session["UID"] != null)
            {
                if (model.CurrentUser == null)
                {
                    model.CurrentUser = new User(InventoryModel.ConnString);
                }

                if (!model.CurrentUser.LoadUserDetails((int)Session["UID"]))
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }

            if (TempData["InventoryModel"] != null)
            {
                model = TempData["InventoryModel"] as InventoryModel;
            }

            if (!String.IsNullOrEmpty(inventoryAction))
            {
                switch (inventoryAction)
                {
                    case "AddItem":
                        try
                        {
                            model.NewItem = new GroceryItem(Request.Form["NewItem.SKU"], Request.Form["NewItem.Brand"], Request.Form["NewItem.Description"],
                                                       int.Parse(Request.Form["NewItem.CurrentQty"]), int.Parse(Request.Form["NewItem.OrderThreshold"]), int.Parse(Request.Form["NewItem.MaxQty"]),
                                                       double.Parse(Request.Form["NewItem.UnitCost"]), double.Parse(Request.Form["NewItem.RetailPrice"]), model.CurrentUser.UserID,
                                                       model.CurrentUser.UserID, InventoryModel.ConnString);

                            if (model.CurrentUser.LoadUserDetails((int)Session["UID"]))
                            {
                                if (model.NewItem.Save(true, out model.Message))
                                {
                                    model.IsSuccess = true;
                                    model.AllItems = GroceryItem.GetAllItemsAsList(InventoryModel.ConnString);
                                    return View(model);
                                }
                                else
                                {
                                    model.IsError = true;
                                    model.ModalType = "newItemModal";
                                    model.ShowErrorModal = false;
                                    return View(model);
                                }
                            }
                            else
                            {
                                Session["UID"] = null;
                                UserModel newModel = new UserModel();
                                newModel.Message = "SESSION HAS EXPIRED.<BR />PLEASE LOG IN AGAIN";
                                TempData["Model"] = newModel;
                                return RedirectToAction("Index");
                            }
                        }
                        catch (Exception ex)
                        {
                            model.IsError = true;
                            model.ModalType = "newItemModal";
                            model.Message = ex.Message;
                            model.ShowErrorModal = false;
                            return View(model);
                        }

                    case "EditItem":
                        try
                        {
                            if (model.CurrentUser.LoadUserDetails((int)Session["UID"]))
                            {
                                GroceryItem item = new GroceryItem(InventoryModel.ConnString, int.Parse(Request.Form["editItemID"]), model.CurrentUser.UserID);
                                item.SKU = Request.Form["editSKU"];
                                item.Brand = Request.Form["editBrand"];
                                item.Description = Request.Form["editDescription"];
                                item.CurrentQty = int.Parse(Request.Form["editCurrentQty"]);
                                item.MaxQty = int.Parse(Request.Form["editMaxQty"]);
                                item.OrderThreshold = int.Parse(Request.Form["editOrderThreshold"]);
                                item.UnitCost = double.Parse(Request.Form["editUnitCost"]);
                                item.RetailPrice = double.Parse(Request.Form["editRetailPrice"]);

                                if (item.Save(false, out model.Message))
                                {
                                    model.IsSuccess = true;
                                    model.AllItems = GroceryItem.GetAllItemsAsList(InventoryModel.ConnString);
                                    model.ItemGrid = new Grid<GroceryItem>(model.AllItems);
                                    return View(model);
                                }
                                else
                                {
                                    model.IsError = true;
                                    model.ModalType = "editModal";
                                    model.ShowErrorModal = false;
                                    return View(model);
                                }
                            }
                            else
                            {
                                Session["UID"] = null;
                                UserModel newModel = new UserModel();
                                newModel.Message = "SESSION HAS EXPIRED.<BR />PLEASE LOG IN AGAIN";
                                TempData["Model"] = newModel;
                                return RedirectToAction("Index");
                            }
                        }
                        catch (Exception ex)
                        {
                            model.IsError = true;
                            model.ModalType = "editModal";
                            model.Message = ex.Message;
                            model.ShowErrorModal = false;
                            return View(model);
                        }

                    case "DeleteItem":

                        try
                        {
                            if (model.CurrentUser.LoadUserDetails((int)Session["UID"]))
                            {
                                GroceryItem deleteItem = new GroceryItem(InventoryModel.ConnString, int.Parse(Request.Form["deleteItemID"]), model.CurrentUser.UserID);

                                if (deleteItem.DeleteItem(out model.Message))
                                {
                                    model.IsSuccess = true;
                                    model.AllItems = GroceryItem.GetAllItemsAsList(InventoryModel.ConnString);
                                    return View(model);
                                }
                                else
                                {
                                    model.IsError = true;
                                    model.ModalType = "deleteModal";
                                    model.ShowErrorModal = false;
                                    return View(model);
                                }
                            }
                            else
                            {
                                Session["UID"] = null;
                                UserModel newModel = new UserModel();
                                newModel.Message = "SESSION HAS EXPIRED.<BR />PLEASE LOG IN AGAIN";
                                TempData["Model"] = newModel;
                                return RedirectToAction("Index");
                            }
                        }
                        catch (Exception ex)
                        {
                            model.IsError = true;
                            model.ModalType = "deleteModal";
                            model.Message = ex.Message;
                            model.ShowErrorModal = false;
                            return View(model);
                        }
                }
            }

            model.ItemGrid = new Grid<GroceryItem>(model.AllItems);

            return View(model);
        }