Exemple #1
0
 public void DeleteAllIngredientsFromList()
 {
     using (var feedIng = new FeedingIngredientRepository())
     {
         foreach (var ingredient in feedIng.GetAll())
         {
             var ing = feedIng.GetById(ingredient.FeedingIngredientsId);
             if (ing != null)
             {
                 feedIng.Delete(ing);
             }
         }
     }
 }
        public void DeleteFeedIngr(int id)
        {
            using (var feedIng = new FeedingIngredientRepository())
            {
                var feed = feedIng.GetById(id);
                if (feed != null)
                {
                    //from ingredients
                    var ingredient = feedIng.GetById(id);

                    //Mass from stock
                    //Get selected stock
                    var stock = _stock.GetAll().ToList().Find(x => x.FeedingStockId == ingredient.FeedingStockId);
                    if (stock != null)
                    {
                        //Calculate total mass from stock add with selected ingredients
                        double totalMass = (Math.Round(stock.TotalMass, 0) + Math.Round(Convert.ToDouble(ingredient.IngredientMass), 0));

                        //Gets the average mass
                        double qtyRatio = totalMass / stock.Mass;

                        //Checks if its a double or not
                        int index = qtyRatio.ToString(CultureInfo.InvariantCulture).IndexOf('.');

                        //calculates the number of items bought and replace values with left
                        int number;
                        if (index > 0)
                        {
                            number =
                                (Convert.ToInt16(qtyRatio.ToString(CultureInfo.InvariantCulture).Substring(0, index)) +
                                 1);
                            stock.NumberOfItems = number;
                        }
                        else
                        {
                            number = Convert.ToInt16(qtyRatio.ToString(CultureInfo.InvariantCulture));
                            stock.NumberOfItems = number;
                        }
                        //equate mass with the calculated
                        stock.TotalMass = totalMass;

                        //Call update method fro the repository
                        _stock.Update(stock);
                    }
                    feedIng.Delete(feed);
                }
            }
        }