public IActionResult DistributeProduct(int productID) { var product = _Iproduct.GetProductByID(productID); var model = new DistributeProductVM { productID = product.ProductID, productName = product.ProductName, price = product.UnitPrice, manufacturer = product.Manufacturer.ManufacturerName, unitsInStock = product.UnitsInStock, category = product.SubCategory.Category.CategoryName, subcategory = product.SubCategory.SubCategoryName, _list = _database.branch.Select(e => new rows { branchID = e.BranchID, cityname = e.City.CityName, quntityPerBranch = _database.branchproduct.Where(a => a.BranchID == e.BranchID).Select(a => a.UnitsInBranch).FirstOrDefault() ?? 0 }).ToList(), _stock = _database.stock.Select(e => new rows2 { stockID = e.StockID, stockName = e.StockName, stockQuanttity = _database.stockproduct.Where(a => a.StockID == e.StockID && product.ProductID == a.ProductID).Select(a => a.Quantity).FirstOrDefault() }).ToList() }; ViewData["err"] = TempData["error"]; return(View(model)); }
public IActionResult SaveBranchProduct(DistributeProductVM model) { var product = _Iproduct.GetProductByID(model.productID); var sum = 0; foreach (var i in model._list) { var bp = _database.branchproduct.Where(e => e.BranchID == i.branchID && e.ProductID == product.ProductID).FirstOrDefault(); // ide u tabelu jel u toj prodavnici taj proizvod vec postoji if (bp != null && model.productID == bp.ProductID) { bp.UnitsInBranch += i.quntityPerBranch; // ako je u pitanju isti proizvod doda se kolicina samo } else { var testing = new BranchProduct // pravi se novi zapis te poslovnice tog proizvoda te kolicine { BranchID = i.branchID, ProductID = product.ProductID, UnitsInBranch = i.quntityPerBranch, }; _database.Add(testing); } sum += i.quntityPerBranch; // sabiraju se kolicine po prodavnicama } var stock = _database.stockproduct.Where(e => e.ProductID == model.productID).FirstOrDefault(); // ode na skladiste uzme koliko ga ima tamo if (stock.Quantity >= sum) { stock.Quantity = stock.Quantity - sum; // od skladista oduzmi UKUPNU KOLICINU ZA SVE POSLOVNICE _database.Add(new AdminActivity { ActivityID = 7, AdminID = Int32.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)), DateOfActivity = DateTime.Now }); _database.SaveChanges(); } else { TempData["error"] = "Unijeta količina nije dostupna, pokušajte ponovni unos"; return(Redirect("/Product/DistributeProduct?productID=" + product.ProductID)); } _database.SaveChanges(); return(Redirect("/Product/ShowStock")); }
public IActionResult SaveBranchProduct(DistributeProductVM model) { var product = _Iproduct.GetProductByID(model.productID); var sum = 0; foreach (var i in model._list) { var bp = _database.branchproduct.Where(e => e.BranchID == i.branchID && e.ProductID == product.ProductID).FirstOrDefault(); // ide u tabelu jel u toj prodavnici taj proizvod vec postoji if (bp != null && model.productID == bp.ProductID) { bp.UnitsInBranch += i.quntityPerBranch; } else { var testing = new BranchProduct { BranchID = i.branchID, ProductID = product.ProductID, UnitsInBranch = i.quntityPerBranch, }; _database.Add(testing); } sum += i.quntityPerBranch; } var stock = _database.stockproduct.Where(e => e.ProductID == model.productID).FirstOrDefault(); if (stock.Quantity >= sum) { stock.Quantity = stock.Quantity - sum; _database.Add(new AdminActivity { ActivityID = 7, AdminID = Int32.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)), DateOfActivity = DateTime.Now }); _database.SaveChanges(); } else { TempData["error"] = "Unijeta količina nije dostupna, pokušajte ponovni unos"; return(Redirect("/Product/DistributeProduct?productID=" + product.ProductID)); } _database.SaveChanges(); return(Redirect("/Product/ShowStock")); }