/// <summary> /// Get a list of all products by a shop id /// The id is Shop id /// </summary> /// <param name="id"></param> /// <returns> /// View of all Products /// </returns> public ActionResult Product(int?id) { // Create a empty list of Products to return if something is not right, else fill it var products = new List <Product>(); // Get the shop from session var shopSes = (Shop)Session["Shop"]; // Get the cart from Session var cart = (Cart)Session["ShoppingCart"]; ViewBag.Cart = cart; var proxy = new BestilNemtServiceClient(); // If the id is null check if shop from session is also null, then return the view if (id == null) { // Check if shop from session is null if (shopSes == null) { // Return a view of empty list return(View(products)); } // Set the id to the id from the shop from session id = shopSes.Id; } // if the value of id is lower than or equal 0 return the empty list if (id.Value <= 0) { return(View(products)); } // Get the shop by the id var shop = proxy.GetShop(id.Value); // If the shop is null return the empty list if (shop == null) { return(View(products)); } // Get all Warehouses with with the shop id shop.Warehouses = proxy.GetAllWarehousesByShopId(id.Value); // If the shop from session is set if (shopSes != null) { // If the ids is different reset the cart if (shopSes.Id != shop.Id) { // Clear the cart from Session Session["ShoppingCart"] = null; } } // Save the shop to session Session["Shop"] = shop; // Get all products from the warehouses products = shop.Warehouses.Select(warehouse => warehouse.Product).ToList(); // Return the view with the list of products return(View(products)); }
/// <summary> /// Find an product, and then adding a new warehouse /// </summary> public void AddProductToWarehouse() { var proxy = new BestilNemtServiceClient(); //Finds the currentuser, by using our static class var currentUser = LoginManager.User; var id = currentUser.PersonId; //finds the corret admin var admin = proxy.GetAdmin(id); var shopIdAdmin = 0; shopIdAdmin = admin.ShopId; //finds the shop through admin Shop getShop = proxy.GetShop(shopIdAdmin); if (ProductId.Text.Equals("")) { MessageBox.Show("Du har glemt at indlæse i produkt"); } else { Warehouse warehouse = new Warehouse(); var productId = int.Parse(ProductId.Text); //Getting all warehouse with a shopID, and loop through it var warehouses = proxy.GetAllWarehousesByShopId(getShop.Id); foreach (var w in warehouses) { if (w.Product.Id == productId) { warehouse = w; } } //sets value in the textboxes warehouse.MinStock = int.Parse(minStock.Text); warehouse.Stock = int.Parse(Stock.Text); warehouse.Shop = getShop; warehouse.SavingId = null; warehouse.Product = proxy.GetProduct(productId); var warehouseId = proxy.AddWarehouse(warehouse); //If the warehouse couldn't be created, it shows a message telling the user //If the warehouse is create, then the user gets a message telling it succeced if (warehouseId == 0) { MessageBox.Show("Varehus findes allerede, opret et ny produkt"); } else { MessageBox.Show("Produktet er tilføjet til dit varehus"); } } }
/// <summary> /// Updates the amount on a product in a warehouse /// </summary> public void UpdateAmount() { var proxy = new BestilNemtServiceClient(); //getting the current User var currentUser = LoginManager.User; //Finds his PersonID var id = currentUser.PersonId; //With id we find the admin, and all admin has shopID var admin = proxy.GetAdmin(id); //Checks for value input if (admin.Id == 0) { MessageBox.Show("Du er ikke admin for en bestemt shop"); } if (ProductIdWareHouse.Text.Equals("")) { MessageBox.Show("Du har glemt at indlæs et produkt"); } if (NewAmount1.Text.Equals("")) { MessageBox.Show("Du har glemt at tilføje et antal"); } if (MinAmount.Text.Equals("")) { MessageBox.Show("Du mangler tilføje et minamount"); } else { //var newamount = Int32.TryParse(NewAmount1.Text); //var minamount = Int32.TryParse(MinAmount.Text); if (Convert.ToInt32(NewAmount1.Text) < Convert.ToInt32(MinAmount.Text)) { MessageBox.Show("Nye Antal kan ikke være mindre min. Antal"); } else { var shopIdAdmin = admin.ShopId; //we find the shop for the currentuser/admin var getShop = proxy.GetShop(shopIdAdmin); //Make a new instance of warehouse var warehouse = new Warehouse(); //Get the productID var productId = int.Parse(ProductIdWareHouse.Text); //GetAllWarehousebyShopid() is a list, so we make a foreach loop, and find the corret warehouse = //with right productID var warehouses = proxy.GetAllWarehousesByShopId(getShop.Id); foreach (var w in warehouses) { if (w.Product.Id == productId) { warehouse = w; } } //sets value for textbox warehouse.Stock = int.Parse(NewAmount1.Text); warehouse.MinStock = int.Parse(MinAmount.Text); warehouse.Shop = getShop; //we update warehouse with a new amount proxy.UpdateWarehouse(warehouse); FillProductWareHouse(); MessageBox.Show("Dit Antal er nu blevet opdateret"); } } }