コード例 #1
0
        public void AddFeedIngr(FeedingIngredientsView model)
        {
            using (var feedIng = new FeedingIngredientRepository())
            {
                //calculate percentage from dropdown list
                string per =
                    _percentage.GetAll()
                    .ToList()
                    .Find(x => x.FeedingPercentageId == model.FeedingPercentageId)
                    .Percentage;

                //get percentage length
                int indper = per.Length;


                //Get total mass from feeding stock
                double mass = _stock.GetAll().ToList().Find(x => x.FeedingStockId == model.FeedingStockId).TotalMass;

                //calculatye percentage mass that has been added as an ingredient
                double calcPercMass = ((Convert.ToDouble(per.Substring(0, indper - 2))) / 100) * mass;

                //Add Ingredients to database
                FeedingIngredients getList = feedIng.GetAll().ToList().Find(x => x.FeedingStockId == model.FeedingStockId);
                if (getList != null)
                {
                    getList.IngredientMass      = Convert.ToString(Convert.ToDouble(getList.IngredientMass) + calcPercMass, CultureInfo.InvariantCulture);
                    getList.FeedingPercentageId = model.FeedingPercentageId;
                    getList.FeedingStockId      = model.FeedingStockId;

                    feedIng.Update(getList);
                }
                else
                {
                    var feedIn = new FeedingIngredients
                    {
                        FeedingIngredientsId = model.FeedingIngredientsId,
                        IngredientMass       = calcPercMass.ToString(CultureInfo.InvariantCulture),
                        FeedingPercentageId  = model.FeedingPercentageId,
                        FeedingStockId       = model.FeedingStockId
                    };
                    //Call repository insert
                    feedIng.Insert(feedIn);
                }



                //Mass from stock update every time ingredient is added
                //Gets stock item that has been selected
                var stock = _stock.GetAll().ToList().Find(x => x.FeedingStockId == model.FeedingStockId);
                if (stock != null)
                {
                    //Calculate mass left
                    double massLeff = stock.TotalMass - calcPercMass;

                    //Get the number of items minused in stock
                    double massRatio = massLeff / stock.Mass;

                    //Check if number is a double
                    int index = massRatio.ToString(CultureInfo.InvariantCulture).IndexOf('.');

                    //calculates the number of items bought and replace values with left
                    int number;
                    if (index > 0)
                    {
                        number = (Convert.ToInt16(massRatio.ToString(CultureInfo.InvariantCulture).Substring(0, index)) + 1);
                        stock.NumberOfItems = number;
                    }
                    else
                    {
                        number = Convert.ToInt16(massRatio.ToString(CultureInfo.InvariantCulture));
                        stock.NumberOfItems = number;
                    }
                    //update mass stock to new value
                    stock.TotalMass = Math.Round(massLeff, 0);

                    //call update method from repository and update
                    _stock.Update(stock);
                }
            }
        }
コード例 #2
0
 public void Update(FeedingIngredients model)
 {
     _feedingPercentageRepository.Update(model);
 }
コード例 #3
0
 public void Delete(FeedingIngredients model)
 {
     _feedingPercentageRepository.Delete(model);
 }
コード例 #4
0
 public void Insert(FeedingIngredients model)
 {
     _feedingPercentageRepository.Insert(model);
 }