public void CalcIngredientCost(PercentageView model)
        {
            //to get percentage for the ingredient added
            double percRate = model.Percentage/100;

            using (_fStockRepo)
            {
                var stockItem = _fStockRepo.GetById(model.FeedingStockId);

                if (stockItem != null)
                {
            //to get total mass of the ingredient in stock
                    double totalMass = stockItem.NumberOfItems*stockItem.Mass;

            //to get amount of mass to remove from stock item according to percentage inputted
                    double massUsed = totalMass*percRate;

            //to get number of items that should be left in stock for the ingredient chosen
                    int numItemsLeft = (int) (totalMass - massUsed)/stockItem.Mass;

                    stockItem.NumberOfItems = numItemsLeft;

                    _fStockRepo.Update(stockItem);
            //to get how much the ingredient to be used costs
                    double ingCost = (massUsed/stockItem.Mass)*stockItem.ItemPrice;

                    var feedingScheme = _fSchemeRepo.GetById(model.FeedingSchemeId);
                    feedingScheme.TotalFeedingCost += ingCost;

                    _fSchemeRepo.Update(feedingScheme);
                }
            }
        }
        public void DeletePercent(PercentageView model)
        {
            using (var percRepo = new PercentageRepository())
            {
                Percentage feed = percRepo.GetById(model.FeedingSchemeId);

                if (feed != null)
                {
                    percRepo.Delete(feed);
                }
            }
        }
 public void AddPercent(PercentageView model)
 {
     using (var percRepo = new PercentageRepository())
     {
         var ingPerc = new Percentage()
         {
             PercentageId = model.PercentageId,
             FeedingStockId = _fStockRepo.GetAll().Find(x=>x.FeedingStockId == model.FeedingStockId).FeedingStockId,
             PercentageRate = model.Percentage,
             FeedingSchemeId = _fSchemeRepo.GetAll().Find(p=>p.FeedingSchemeId==model.FeedingSchemeId).FeedingSchemeId
         };
        percRepo.Insert(ingPerc);
        CalcIngredientCost(model);
     }
 }
        public ActionResult AddPercentage(PercentageView model)
        {
            if (ModelState.IsValid)
            {
                FeedingStockBussiness fStock = new FeedingStockBussiness();
                ViewBag.FeedingStockId = new SelectList(fStock.GetAllFeedingStock(), "FeedingStockId", "ItemName");
                FeedingSchemeBl fScheme = new FeedingSchemeBl();
                ViewBag.FeedingSchemeId = new SelectList(fScheme.GetAllFeedSchemeViews(), "FeedingSchemeId", "SchemeCode");

                PercentageBl percBl=new PercentageBl();

                if (percBl.IsAvailable(model))
                {
                    percBl.AddPercent(model);
                    return RedirectToAction("GetAllPercentage");
                }

                return RedirectToAction("ValidateAvailable");
            }

            return View();
        }
        //Method to validate if stock items exist from which a percenatge can be obtained from
        public bool IsAvailable(PercentageView model)
        {
            var available = false;

            FeedingStockBussiness stockItem= new FeedingStockBussiness();

            FeedingStockView stockView = stockItem.GetFeedingStockById(model.FeedingStockId);

            if (stockView != null)
                if (stockView.NumberOfItems >= 1)
                           available = true;

            return available;
        }