public ActionResult Create([Bind(Include = "LivestockID,CategoryId,Weight,BatchID,DateOfBirth,Sex,CostPrice")] LivesStock livesStock, HttpPostedFileBase img_upload) { livesStock.sold = false; livesStock.addedtocart = false; byte[] data = null; data = new byte[img_upload.ContentLength]; img_upload.InputStream.Read(data, 0, img_upload.ContentLength); livesStock.Picture = data; if (ModelState.IsValid) { Category category = db.Categories.Find(livesStock.CategoryId); db.LivesStocks.Add(livesStock); if (livesStock.BatchID != null) { //Decrement the number of uncreated livestock for that batch db.Batches.Find(livesStock.BatchID).UncreatedQuantity--; } db.SaveChanges(); livesStock.Code = livesStock.GenerateCode(category); db.Entry(livesStock).State = EntityState.Modified; LiveStockWeight liveStockWeight = new LiveStockWeight(); liveStockWeight.LivestockID = livesStock.LivestockID; liveStockWeight.Date = liveStockWeight.EntryDate(); liveStockWeight.Weight = livesStock.Weight; db.LiveStockWeights.Add(liveStockWeight); db.SaveChanges(); // livesStock.Weight = liveStockWeight.Weight; CategoryCost categoryCost = new CategoryCost(); livesStock.SellingPrice = livesStock.calcSellingPrice(categoryCost.ValidategoryCost(db.CategoryCosts.ToList(), livesStock.CategoryId)); db.Entry(livesStock).State = EntityState.Modified; db.SaveChanges(); if (livesStock.BatchID != null) { //check whether there are still any ucreated livestock for that batch if (db.Batches.Find(livesStock.BatchID).UncreatedQuantity > 0) { //if there are, return to the livestock create screen return(RedirectToAction("Create", db.Batches.Find(livesStock.BatchID))); } } TempData["Message"] = "<script>alert('Livestock added!');</script>"; return(RedirectToAction("Index")); } Batch batch = new Batch(); List <Batch> batches = db.Batches.ToList <Batch>(); ViewBag.BatchID = new SelectList(batch.ValidBatch(batches), "BatchID", "BatchCode"); ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "CategoryType", livesStock.CategoryId); return(View(livesStock)); }
public ActionResult Create([Bind(Include = "LiveStockWeightID,LivestockID,Weight,Date")] LiveStockWeight liveStockWeight) { if (ModelState.IsValid) { liveStockWeight.Date = liveStockWeight.EntryDate(); db.LiveStockWeights.Add(liveStockWeight); db.SaveChanges(); LivesStock livesStock = db.LivesStocks.Find(liveStockWeight.LivestockID); livesStock.Weight = liveStockWeight.Weight; CategoryCost categoryCost = new CategoryCost(); livesStock.SellingPrice = livesStock.calcSellingPrice(categoryCost.ValidategoryCost(db.CategoryCosts.ToList(), livesStock.CategoryId)); db.Entry(livesStock).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.LivestockID = new SelectList(db.LivesStocks, "LivestockID", "Code", liveStockWeight.LivestockID); return(View(liveStockWeight)); }
public ActionResult Edit([Bind(Include = "C_ID,CategoryId,BasicCost,CostPerKG")] CategoryCost categoryCost) { if (ModelState.IsValid) { db.Entry(categoryCost).State = EntityState.Modified; db.SaveChanges(); LivesStock livesStock = new LivesStock(); List <LivesStock> listLivestocks = livesStock.LivestocksInCategory(db.LivesStocks.ToList(), categoryCost); for (int i = 0; i < listLivestocks.Count; i++) { livesStock = listLivestocks[i]; livesStock.SellingPrice = livesStock.calcSellingPrice(categoryCost); db.Entry(livesStock).State = EntityState.Modified; } db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "CategoryType", categoryCost.CategoryId); return(View(categoryCost)); }