Example #1
0
    protected void NutValue_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            RecipeTotalNutValues nutValueItem = (RecipeTotalNutValues)e.Item.DataItem;
            HtmlTableCell        td           = (HtmlTableCell)e.Item.FindControl("tdRepeater");
            string colorCode = "";
            switch (nutValueItem.NutCategoryId)
            {
            case 1:
                colorCode = "#EE1E3E";
                break;

            case 2:
                colorCode = "#FBAB14";
                break;

            case 3:
                colorCode = "#A4CB3A";
                break;

            case 4:
                colorCode = "#1A98D5";
                break;

            case 5:
                colorCode = "#656565";
                break;

            default:
                colorCode = "#656565";
                break;
            }
            td.Style["background-color"] = colorCode;
        }
    }
Example #2
0
        internal RecipeTotalNutValues[] GetRecipeTotalNutValues(int recipeId, out bool isCompleteCalculation)
        {
            isCompleteCalculation = false;

            int ingredientsCount = DataContext.Recipes.Single(r => r.RecipeId == recipeId).Ingredients.Count;

            var measureUnitConverts = DataContext.MeasurementUnitsConverts;

            var query = from r in DataContext.Recipes.Where(r => r.RecipeId == recipeId && r.Servings > 0)
                        join i in DataContext.Ingredients on r.RecipeId equals i.RecipeId
                        join f in DataContext.Foods on i.FoodId equals f.FoodId
                        join nv in DataContext.NutValues on f.FoodId equals nv.FoodId
                        join ni in DataContext.NutItems on nv.NutItemId equals ni.NutItemId
                        orderby ni.NutCategoryId, ni.NutItemId
                select new
            {
                ni.NutItemId,
                ni.NutItemName,
                ni.DisplayUnit,
                ni.NutCategoryId,
                nv.Value,
                r.Servings,
                i.Quantity,
                f.FoodId,
                i.MeasurementUnitId,
                i.IngredientId             //added
            };


            List <RecipeTotalNutValues> list = new List <RecipeTotalNutValues>();

            decimal totalRecipeWeight = 0;
            Dictionary <int, int> NutValuesIncludedInCalaculation = new Dictionary <int, int>();

            foreach (var row in query)
            {
                bool    calculateDataFound = false;
                decimal convertRatio       = 0;

                if (row.MeasurementUnitId == AppConstants.GRAMM_UNIT_ID)
                {
                    convertRatio       = 1;
                    calculateDataFound = true;
                }
                else if (row.MeasurementUnitId == AppConstants.KILOGRAMM_UNIT_ID)
                {
                    convertRatio       = 1000;
                    calculateDataFound = true;
                }
                else
                {
                    MeasurementUnitsConvert unitConvert = measureUnitConverts.SingleOrDefault(muc => muc.FoodId == row.FoodId &&
                                                                                              muc.FromUnitId == row.MeasurementUnitId &&
                                                                                              muc.ToUnitId == AppConstants.GRAMM_UNIT_ID);
                    if (unitConvert != null)
                    {
                        if (unitConvert.FromQuantity > 0)
                        {
                            convertRatio       = unitConvert.ToQuantity / unitConvert.FromQuantity;
                            calculateDataFound = true;
                        }
                    }
                    else
                    {
                        unitConvert = measureUnitConverts.SingleOrDefault(muc => muc.FoodId == row.FoodId &&
                                                                          muc.FromUnitId == row.MeasurementUnitId &&
                                                                          muc.ToUnitId == AppConstants.KILOGRAMM_UNIT_ID);
                        if (unitConvert != null)
                        {
                            convertRatio       = unitConvert.ToQuantity / unitConvert.FromQuantity * 1000;
                            calculateDataFound = true;
                        }
                    }
                }

                if (calculateDataFound)
                {
                    RecipeTotalNutValues item = new RecipeTotalNutValues
                    {
                        NutCategoryId = row.NutCategoryId,
                        NutItemId     = row.NutItemId,
                        NutItemName   = row.NutItemName,
                        DisplayUnit   = row.DisplayUnit,
                        //TotalValue = (row.Value == null ? null : row.Quantity / row.Servings * convertRatio * row.Value / 100)
                        //calculate nutvalue for the whole recipe, not per serving as above:
                        TotalValue = (row.Value == null ? null : row.Quantity * convertRatio * (row.Value / 100))
                    };

                    //this if statement is not needed, since DisplayTotalValue should only be set after final TotalValue is determined.
                    if (item.TotalValue != null)
                    {
                        if (item.TotalValue != 0)
                        {
                            item.DisplayTotalValue = item.TotalValue.Value.ToString("F");
                        }
                        else
                        {
                            item.DisplayTotalValue = "0";
                        }
                    }

                    list.Add(item);

                    if (!NutValuesIncludedInCalaculation.Keys.Contains(row.IngredientId))
                    {
                        NutValuesIncludedInCalaculation.Add(row.IngredientId, 1);
                    }
                    else
                    {
                        NutValuesIncludedInCalaculation[row.IngredientId]++;
                    }
                }
            }
            // added code: calculate total "recipe mass":
            var ingredientsList = from ing in DataContext.Ingredients
                                  where ing.RecipeId == recipeId
                                  select ing;

            foreach (Ingredient ingredient in ingredientsList)
            {
                bool    calculateDataFound = false;
                decimal convertRatio       = 0;

                if (ingredient.MeasurementUnitId == AppConstants.GRAMM_UNIT_ID)
                {
                    convertRatio       = 1;
                    calculateDataFound = true;
                }
                else if (ingredient.MeasurementUnitId == AppConstants.KILOGRAMM_UNIT_ID)
                {
                    convertRatio       = 1000;
                    calculateDataFound = true;
                }
                else
                {
                    MeasurementUnitsConvert unitConvert = measureUnitConverts.SingleOrDefault(muc => muc.FoodId == ingredient.FoodId &&
                                                                                              muc.FromUnitId == ingredient.MeasurementUnitId &&
                                                                                              muc.ToUnitId == MyBuyList.Shared.AppConstants.GRAMM_UNIT_ID);
                    if (unitConvert != null)
                    {
                        if (unitConvert.FromQuantity > 0)
                        {
                            convertRatio       = unitConvert.ToQuantity / unitConvert.FromQuantity;
                            calculateDataFound = true;
                        }
                    }
                    else
                    {
                        unitConvert = measureUnitConverts.SingleOrDefault(muc => muc.FoodId == ingredient.FoodId &&
                                                                          muc.FromUnitId == ingredient.MeasurementUnitId &&
                                                                          muc.ToUnitId == AppConstants.KILOGRAMM_UNIT_ID);
                        if (unitConvert != null)
                        {
                            convertRatio       = unitConvert.ToQuantity / unitConvert.FromQuantity * 1000;
                            calculateDataFound = true;
                        }
                    }
                }

                if (calculateDataFound)
                {
                    totalRecipeWeight += (ingredient.Quantity * convertRatio);
                }
            }
            //end of added code.

            Dictionary <int, RecipeTotalNutValues> dict = new Dictionary <int, RecipeTotalNutValues>();

            foreach (RecipeTotalNutValues item in list)
            {
                if (!dict.ContainsKey(item.NutItemId))
                {
                    if (item.TotalValue != null)
                    {
                        dict.Add(item.NutItemId, item);
                    }
                }
                else
                {
                    dict[item.NutItemId].TotalValue += item.TotalValue;
                    //if (dict[item.NutItemId].TotalValue != null)
                    //{
                    //    if (dict[item.NutItemId].TotalValue.Value != 0)
                    //    {
                    //        dict[item.NutItemId].DisplayTotalValue = dict[item.NutItemId].TotalValue.Value.ToString("F");
                    //    }
                    //    else
                    //    {
                    //        dict[item.NutItemId].DisplayTotalValue = "0";
                    //    }
                    //}
                }
            }
            RecipeTotalNutValues[] array = dict.Values.ToArray();

            //divide each nutValue by the weight of the whole recipe in grams and then multiply by 100, to get the nutValues for a 100 grams of "recipe mass"...
            if (totalRecipeWeight != 0)
            {
                for (int i = 0; i < array.Length; i++)
                {
                    if (array[i].TotalValue.HasValue)
                    {
                        decimal x = array[i].TotalValue.Value;
                        array[i].TotalValue = (x / totalRecipeWeight) * 100;

                        if (array[i].TotalValue != 0)
                        {
                            array[i].DisplayTotalValue = array[i].TotalValue.Value.ToString("F");
                        }
                        else
                        {
                            array[i].DisplayTotalValue = "0";
                        }
                    }
                }
            }

            if (ingredientsCount == NutValuesIncludedInCalaculation.Count())
            {
                isCompleteCalculation = true;

                foreach (KeyValuePair <int, int> pair in NutValuesIncludedInCalaculation)
                {
                    if (pair.Value < 26)
                    {
                        isCompleteCalculation = false;
                    }
                }
            }

            return(array);
        }