Exemple #1
0
        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));
        }
Exemple #2
0
        // GET: LivesStocks/Create
        //[Authorize(Roles = "Stock Controller")]
        public ActionResult AutoCreate(Batch batch)
        {
            //loop this create process as many times as there are livestock in the batch
            for (int i = 0; i < batch.Quantity; i++)
            {
                Category category = db.Categories.Find(batch.CategoryID);
                //use the constructor to create default livestock
                //using correct batch number and correct category
                LivesStock livestock = new LivesStock(batch, category);
                livestock.DateOfBirth = DateTime.Now;
                db.LivesStocks.Add(livestock);
                db.SaveChanges();
                livestock.Code            = livestock.GenerateCode(category);
                db.Entry(livestock).State = EntityState.Modified;
                db.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }