private bool CheckRatioSums(int combID)
        {
            int mealRatioSum  = 0;
            int totalRatioSum = 0;

            for (int i = 0; i < newMealRatiosList.Count; i++)
            {
                MealRatio ratio = newMealRatiosList[i];
                mealRatioSum = ratio.FatPortion + ratio.CarbPortion + ratio.ProteinPortion;
                if (mealRatioSum != 100)
                {
                    lblError.Text = $"Omjer namirnica za obrok {ratio.Meal.Name} nije pravilan";
                    SQLProcedures.DeleteCombination(combID);
                    return(false);
                }
                totalRatioSum += ratio.TotalPortion;
            }

            if (totalRatioSum != 100)
            {
                lblError.Text = $"Omjer obroka za kombinaciju nije pravilan";
                SQLProcedures.DeleteCombination(combID);
                return(false);
            }

            return(true);
        }
Example #2
0
        //shows menu after date and meal number selection
        private ActionResult GenerateMenuTwo()
        {
            ViewBag.ShowMessage = false;

            int mealNumber = int.Parse(Session["mealNumber"].ToString());
            List <Combination> combinationsList = SQLProcedures.GetSavedMealCombinations();

            combinationsList.Sort((x, y) => x.NumberOfMeals.CompareTo(y.NumberOfMeals));

            ViewBag.Combinations = new SelectList(combinationsList, "NumberOfMeals", "NumberOfMeals", mealNumber);

            string selectedCombDate = Session["selectedCombDate"].ToString();

            ViewBag.CombinationDate = selectedCombDate;

            double totalKcal = CalculateTotalKcal();

            //sorting food by type

            List <Food> foodList    = SQLProcedures.GetFood();
            List <Food> carbsList   = new List <Food>();
            List <Food> fatList     = new List <Food>();
            List <Food> proteinList = new List <Food>();

            foreach (Food food in foodList)
            {
                switch (food.Type)
                {
                case "Ugljikohidrat":
                    carbsList.Add(food);
                    break;

                case "Mast":
                    fatList.Add(food);
                    break;

                case "Bjelancevina":
                    proteinList.Add(food);
                    break;

                default:
                    break;
                }
            }

            List <MealRatio>    mealRatioList = SQLProcedures.GetMealRatioOfComboForNumberOfMeals(mealNumber);
            List <MenuMealData> menuMealsList = new List <MenuMealData>();
            Random rnd = new Random();

            for (int i = 0; i < mealNumber; i++)
            {
                MealRatio ratio            = mealRatioList.ElementAt(i);
                double    totalMealPortion = Math.Round(totalKcal * ratio.TotalPortion / 100, 2);
                double    fatPortion       = Math.Round(totalMealPortion * ratio.FatPortion / 100, 2);
                double    carbPortion      = Math.Round(totalMealPortion * ratio.CarbPortion / 100, 2);
                double    proteinPortion   = Math.Round(totalMealPortion * ratio.ProteinPortion / 100, 2);

                int    index    = rnd.Next(fatList.Count);
                Food   fat      = fatList.ElementAt(index);
                string fatUnits = CalculateUnitAmount(fat.IDFood, fatPortion);

                index = rnd.Next(carbsList.Count);
                Food   carbs     = carbsList.ElementAt(index);
                string carbUnits = CalculateUnitAmount(carbs.IDFood, carbPortion);

                index = rnd.Next(proteinList.Count);
                Food   protein      = proteinList.ElementAt(index);
                string proteinUnits = CalculateUnitAmount(protein.IDFood, proteinPortion);

                menuMealsList.Add(new MenuMealData
                {
                    MealName     = mealRatioList.ElementAt(i).Meal.Name,
                    CarbName     = carbs.Name,
                    CarbUnits    = carbUnits,
                    FatName      = fat.Name,
                    FatUnits     = fatUnits,
                    ProteinName  = protein.Name,
                    ProteinUnits = proteinUnits
                });
            }

            Session["menuMealsList"] = menuMealsList;
            return(View("GenerateMenuTwo", menuMealsList));
        }
        protected void ddlBrojObroka_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            if (ddlBrojObroka.SelectedIndex != 0)
            {
                lblStartDate.Visible = true;
                txtStartDate.Visible = true;
                lblEndDate.Visible   = true;
                txtEndDate.Visible   = true;

                btnSpremi.Visible    = true;
                btnOdustani.Visible  = true;
                btnOnemoguci.Visible = false;

                if (mealRatioList == null)
                {
                    mealRatioList = new List <MealRatio>();
                }

                int selectedNumberOfMeals = int.Parse(ddlBrojObroka.SelectedValue);
                isActive = SQLProcedures.CheckForSavedCombination(selectedNumberOfMeals);
                if (isActive)
                {
                    btnSpremi.Visible    = false;
                    btnOdustani.Visible  = false;
                    btnOnemoguci.Visible = true;

                    mealCombination   = SQLProcedures.GetSavedCombination(selectedNumberOfMeals);
                    mealRatioList     = SQLProcedures.GetMealRatios(mealCombination.IDCombination);
                    txtStartDate.Text = mealCombination.StartDate.ToString(DATE_FORMAT);
                    if (mealCombination.EndDate != DateTime.MinValue)
                    {
                        txtEndDate.Text = mealCombination.EndDate.ToString(DATE_FORMAT);
                    }
                    gvCombinations.DataSource = mealRatioList;
                    gvCombinations.DataBind();
                }

                else
                {
                    txtStartDate.Text = string.Empty;
                    txtEndDate.Text   = string.Empty;
                    for (int i = 0; i < selectedNumberOfMeals; i++)
                    {
                        MealRatio mr = new MealRatio
                        {
                            Meal = new Meal {
                                IDMeal = 0, Name = string.Empty
                            },
                            FatPortion     = 0,
                            CarbPortion    = 0,
                            ProteinPortion = 0,
                            TotalPortion   = 0
                        };
                        mealRatioList.Add(mr);
                    }
                    gvCombinations.DataSource = mealRatioList;
                    gvCombinations.DataBind();
                }
            }
            else
            {
                lblStartDate.Visible      = false;
                txtStartDate.Visible      = false;
                lblEndDate.Visible        = false;
                txtEndDate.Visible        = false;
                btnSpremi.Visible         = false;
                btnOdustani.Visible       = false;
                btnOnemoguci.Visible      = false;
                gvCombinations.DataSource = null;
                gvCombinations.DataBind();
            }
            FillObrociDDL();
        }
        protected void btnSpremi_Click(object sender, EventArgs e)
        {
            int numberOfMeals = int.Parse(ddlBrojObroka.SelectedValue);
            int combID;

            if (!string.IsNullOrEmpty(txtStartDate.Text))
            {
                lblError.Text = string.Empty;
                DateTime startDate = DateTime.Parse(txtStartDate.Text);
                DateTime endDate;

                if (!string.IsNullOrEmpty(txtEndDate.Text))
                {
                    endDate = DateTime.Parse(txtEndDate.Text);
                    if (endDate < DateTime.Now)
                    {
                        lblError.Text = "Uneseni datum isteka je već prošao";
                        return;
                    }
                    combID = SQLProcedures.InsertCombination(numberOfMeals, startDate, endDate);
                }
                else
                {
                    combID = SQLProcedures.InsertCombination(numberOfMeals, startDate, null);
                }

                mealsList = SQLProcedures.GetMeals();
                for (int i = 0; i < gvCombinations.Rows.Count; i++)
                {
                    MealRatio    mealRatio = new MealRatio();
                    DropDownList ddl       = gvCombinations.Rows[i].FindControl("ddlObroci") as DropDownList;
                    if (mealsList.Find(m => m.Name == ddl.SelectedItem.Text).IsValid)
                    {
                        mealRatio.Meal = mealsList.Find(m => m.Name == ddl.SelectedItem.Text);
                    }
                    else
                    {
                        lblError.Text = $"Odabran obrok u {i + 1}. redu nije dozvoljen - pogledati tablicu Obroci";
                        SQLProcedures.DeleteCombination(combID);
                        return;
                    }

                    var txtFat   = gvCombinations.Rows[i].FindControl("txtMasti") as TextBox;
                    var txtCarb  = gvCombinations.Rows[i].FindControl("txtUgljikohidrati") as TextBox;
                    var txtProt  = gvCombinations.Rows[i].FindControl("txtBjelancevine") as TextBox;
                    var txtTotal = gvCombinations.Rows[i].FindControl("txtUkupno") as TextBox;

                    if (int.TryParse(txtFat.Text, out int fat))
                    {
                        mealRatio.FatPortion = fat;
                    }
                    else
                    {
                        lblError.Text = $"Unesena vrijednost masti za obrok {mealRatio.Meal.Name} nije valjana";
                        SQLProcedures.DeleteCombination(combID);
                        return;
                    }

                    if (int.TryParse(txtCarb.Text, out int carb))
                    {
                        mealRatio.CarbPortion = carb;
                    }
                    else
                    {
                        lblError.Text = $"Unesena vrijednost ugljikohidrata za obrok {mealRatio.Meal.Name} nije valjana";
                        SQLProcedures.DeleteCombination(combID);
                        return;
                    }

                    if (int.TryParse(txtProt.Text, out int prot))
                    {
                        mealRatio.ProteinPortion = prot;
                    }
                    else
                    {
                        lblError.Text = $"Unesena vrijednost bjelancevina za obrok {mealRatio.Meal.Name} nije valjana";
                        SQLProcedures.DeleteCombination(combID);
                        return;
                    }

                    if (int.TryParse(txtTotal.Text, out int total))
                    {
                        mealRatio.TotalPortion = total;
                    }
                    else
                    {
                        lblError.Text = $"Unesena vrijednost ukupnog udjela za obrok {mealRatio.Meal.Name} nije valjana";
                        SQLProcedures.DeleteCombination(combID);
                        return;
                    }

                    newMealRatiosList.Add(mealRatio);
                }

                AddNewMealRatios(combID);
            }

            else
            {
                lblError.Text = "Početni datum nije definiran";
            }
        }