public ActionResult AddExpense(input newExpense)
 {
     if (ModelState.IsValid)
     {
         db.inputs.Add(newExpense);
         db.SaveChanges();
         RedirectToAction("Expenses");
     }
     ViewBag.product = loadProducts();
     ViewBag.userID  = loadUsers();
     ViewBag.status  = loadFieldStates();
     ViewBag.pigID   = loadPigs();
     return(View(newExpense));
 }
        public ActionResult AddPigs(pigModel pigToAdd)
        {
            pig             newPig             = new pig();
            pigRelationship newPigRelationship = new pigRelationship();
            pig             checkTagNumber     = db.pigs.Where(u => u.tagNumber == pigToAdd.tagNumber).FirstOrDefault();

            if (checkTagNumber != null)
            {
                loadPigDropDownList();
                ViewBag.Message = "Tag number " + pigToAdd.tagNumber.ToString() + " already exist, please assign a unique tag";
                return(View(pigToAdd));
            }
            if (pigToAdd.source == 1)
            {
                newPig        = AddPigDetails(pigToAdd, newPig);
                newPig.source = pigToAdd.source;
                if (pigToAdd.fatherID != "Unknown")
                {
                    newPigRelationship.malePig = Convert.ToInt32(pigToAdd.fatherID);
                }
                if (pigToAdd.motherID != "Unknown")
                {
                    newPigRelationship.femalePig = Convert.ToInt32(pigToAdd.motherID);
                }

                db.pigs.Add(newPig);
                //db.SaveChanges();
                //pig newlyAddedPig = db.pigs.Single(u => u.tagNumber == newPig.tagNumber);
                newPigRelationship.siblings = Convert.ToInt32(newPig.pigNumber);
                newPigRelationship.status   = 1;
                db.pigRelationships.Add(newPigRelationship);
                db.SaveChanges();
                return(RedirectToAction("AllPigs"));
            }
            else if (pigToAdd.source == 2)
            {
                newPig        = AddPigDetails(pigToAdd, newPig);
                newPig.source = pigToAdd.source;
                db.pigs.Add(newPig);


                //pig newlyAddedPig = db.pigs.Single(u => u.tagNumber == newPig.tagNumber);
                input boughtPig = new input();
                boughtPig.product          = 1;
                boughtPig.costPerProduct   = pigToAdd.buyingPrice;
                boughtPig.numberOfProducts = 1;
                boughtPig.total            = pigToAdd.buyingPrice;
                boughtPig.date             = Convert.ToDateTime(pigToAdd.dateOfAcquisition.ToShortDateString());
                boughtPig.userID           = 1;
                boughtPig.status           = 1;
                boughtPig.pigID            = newPig.pigNumber;
                db.inputs.Add(boughtPig);

                supplier newSupplier = new supplier();

                if (pigToAdd.supplierID == "Add Supplier")
                {
                    newSupplier.supplierName        = pigToAdd.supplierName;
                    newSupplier.supplierEmail       = pigToAdd.supplierEmail;
                    newSupplier.supplierPhoneNumber = pigToAdd.supplierPhoneNumber;
                    newSupplier.supplierState       = 1;
                    newSupplier.outSourcedPigs.Add(new outSourcedPig()
                    {
                        pigNumber = newPig.pigNumber, status = 1
                    });
                    db.suppliers.Add(newSupplier);
                }
                else
                {
                    db.outSourcedPigs.Add(new outSourcedPig()
                    {
                        supplierID = Convert.ToInt32(pigToAdd.supplierID), pigNumber = newPig.pigNumber, status = 1
                    });
                }
                if (pigToAdd.fatherID != "Unknown")
                {
                    newPigRelationship.malePig = Convert.ToInt32(pigToAdd.fatherID);
                }
                if (pigToAdd.motherID != "Unknown")
                {
                    newPigRelationship.femalePig = Convert.ToInt32(pigToAdd.motherID);
                }
                newPigRelationship.siblings = Convert.ToInt32(newPig.pigNumber);
                newPigRelationship.status   = 1;
                db.pigRelationships.Add(newPigRelationship);
                db.SaveChanges();
                return(RedirectToAction("AllPigs"));
            }
            return(View());
        }