internal bool SaveMeasurementUnitsConvert(MeasurementUnitsConvert convert) { using (DataContext) { try { if (!DataContext.MeasurementUnitsConverts.Contains(convert)) { convert.CreatedDate = DateTime.Now; convert.ModifiedDate = DateTime.Now; DataContext.MeasurementUnitsConverts.Add(convert); //DataContext.MeasurementUnitsConverts.InsertOnSubmit(convert); } else { MeasurementUnitsConvert itemToSave = DataContext.MeasurementUnitsConverts.Single(muc => muc.ConvertId == convert.ConvertId); itemToSave.FoodId = convert.FoodId; itemToSave.FromUnitId = convert.FromUnitId; itemToSave.FromQuantity = convert.FromQuantity; itemToSave.ToUnitId = convert.ToUnitId; itemToSave.ToQuantity = convert.ToQuantity; itemToSave.ModifiedDate = DateTime.Now; } DataContext.SaveChanges(); //DataContext.SubmitChanges(); return(true); } catch { return(false); } } }
protected void rolMeasurementUnitsConverts_ItemDataBound(object sender, ReorderListItemEventArgs e) { ReorderListItem rolItem = e.Item as ReorderListItem; LinkButton btn = rolItem.FindControl("btnUpdate") as LinkButton; Label textLine = rolItem.FindControl("lbMeasurementUnitsConvertText") as Label; MeasurementUnitsConvert MeasuresConvert = e.Item.DataItem as MeasurementUnitsConvert; if (MeasuresConvert != null) { btn.PostBackUrl = string.Format("~/Admin/MeasurementUnitsConvert.aspx?ConvertId={0}", MeasuresConvert.ConvertId); string from = MeasuresConvert.FromQuantity.ToString(); string[] fromStr = from.Split('.'); if (fromStr[1] == "00") { from = fromStr[0]; } string to = MeasuresConvert.ToQuantity.ToString(); string[] toStr = to.Split('.'); if (toStr[1] == "00") { to = toStr[0]; } Food food = BusinessFacade.Instance.GetFood(MeasuresConvert.FoodId); MeasurementUnit sourceMeasureUnit = BusinessFacade.Instance.GetMeasurementUnit(MeasuresConvert.FromUnitId); MeasurementUnit targetMeasureUnit = BusinessFacade.Instance.GetMeasurementUnit(MeasuresConvert.ToUnitId); textLine.Text = string.Format("{0} {1} {2} = {3} {4}", from, sourceMeasureUnit.UnitName, food.FoodName, to, targetMeasureUnit.UnitName); } //btn = rolItem.FindControl("btnDelete") as LinkButton; // btn.Visible = MeasuresConvert.AllowDelete; }
protected void rolMeasurementUnitsConverts_ItemDataBound(object sender, ReorderListItemEventArgs e) { ReorderListItem rolItem = e.Item as ReorderListItem; LinkButton btn = rolItem.FindControl("btnUpdate") as LinkButton; Label textLine = rolItem.FindControl("lbMeasurementUnitsConvertText") as Label; MeasurementUnitsConvert MeasuresConvert = e.Item.DataItem as MeasurementUnitsConvert; if (MeasuresConvert != null) { btn.PostBackUrl = string.Format("~/Admin/MeasurementUnitsConvert.aspx?ConvertId={0}", MeasuresConvert.ConvertId); string from = MeasuresConvert.FromQuantity.ToString(); string[] fromStr = from.Split('.'); if (fromStr[1] == "00") { from = fromStr[0]; } string to = MeasuresConvert.ToQuantity.ToString(); string[] toStr = to.Split('.'); if (toStr[1] == "00") { to = toStr[0]; } textLine.Text = string.Format("{0} {1} {2} = {3} {4}", from, MeasuresConvert.SOURCE_UNIT_NAME, MeasuresConvert.FOOD_NAME, to, MeasuresConvert.TARGET_UNIT_NAME); } //btn = rolItem.FindControl("btnDelete") as LinkButton; // btn.Visible = MeasuresConvert.AllowDelete; }
protected void btnAdd_Click(object sender, EventArgs e) { MeasurementUnitsConvert newItem = new MeasurementUnitsConvert(); newItem.SortOrder = 1; newItem.FoodId = 0; newItem.FromUnitId = 0; newItem.ToUnitId = 0; BusinessFacade.Instance.SaveMeasurementUnitsConvert(newItem); this.Rebind(); }
internal string GetIngredientInGrams(ShoppingFood food, int currUnit) { MeasurementUnitsConvert convert = BusinessFacade.Instance.GetMeasurementUnitsConvertList().SingleOrDefault(mc => mc.FoodId == food.FoodId && mc.FromUnitId == currUnit && mc.ToUnitId == (int)AppConstants.GRAMM_UNIT_ID); string result = string.Empty; if (convert != null) { decimal unitQuantity = (convert.ToQuantity / convert.FromQuantity) * food.Quantity; result = string.Format("{0} {1}", BusinessFacade.Instance.GetNumberWithFreg(unitQuantity), "גרם"); } return(result); }
protected void btnOK_Click(object sender, EventArgs e) { this.Validate(); if (this.IsValid) { Food selectedFood = BusinessFacade.Instance.GetFood(this.txtIngredientName.Text); MeasurementUnitsConvert convert = new MeasurementUnitsConvert(); convert.ConvertId = this.ConvertId; convert.FoodId = selectedFood.FoodId; convert.FromQuantity = decimal.Parse(this.txtFromQuantity.Text); convert.FromUnitId = int.Parse(this.ddlFromMeasurementUnits.SelectedValue); convert.ToQuantity = decimal.Parse(this.txtToQuantity.Text); convert.ToUnitId = int.Parse(this.ddlToMeasurementUnits.SelectedValue); if (BusinessFacade.Instance.SaveMeasurementUnitsConvert(convert)) { this.Response.Redirect("~/Admin/MeasurementUnitsConvertList.aspx"); } } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (((BasePage)Page).UserType != AppEnv.USER_ADMIN) { AppEnv.MoveToDefaultPage(); } else { ddlFromMeasurementUnits.DataSource = BusinessFacade.Instance.GetMeasurementUnitsList(); ddlFromMeasurementUnits.DataTextField = "UnitName"; ddlFromMeasurementUnits.DataValueField = "UnitId"; ddlFromMeasurementUnits.DataBind(); ddlToMeasurementUnits.DataSource = BusinessFacade.Instance.GetMeasurementUnitsList(); ddlToMeasurementUnits.DataTextField = "UnitName"; ddlToMeasurementUnits.DataValueField = "UnitId"; ddlToMeasurementUnits.DataBind(); if (!string.IsNullOrEmpty(this.Request["ConvertId"])) { ConvertId = int.Parse(this.Request["ConvertId"]); MeasurementUnitsConvert convert = BusinessFacade.Instance.GetMeasurementUnitsConvert(ConvertId); Food food = BusinessFacade.Instance.GetFood(convert.FoodId); if (convert != null) { txtIngredientName.Text = food.FoodName; txtFromQuantity.Text = convert.FromQuantity.ToString(); ddlFromMeasurementUnits.SelectedValue = convert.FromUnitId.ToString(); txtToQuantity.Text = convert.ToQuantity.ToString(); ddlToMeasurementUnits.SelectedValue = convert.ToUnitId.ToString(); } } } } }
internal MeasurementUnitsConvert GetMeasurementUnitsConvert(int id) { using (DataContext) { //DataLoadOptions dlo = new DataLoadOptions(); //dlo.LoadWith<MeasurementUnitsConvert>(m => m.Food); //dlo.LoadWith<MeasurementUnitsConvert>(m => m.FromMeasurementUnit); //dlo.LoadWith<MeasurementUnitsConvert>(m => m.ToMeasurementUnit); //DataContext.LoadOptions = dlo; try { MeasurementUnitsConvert unit = DataContext.MeasurementUnitsConverts.SingleOrDefault(mu => mu.ConvertId == id); return(unit); } catch { return(null); } } }
public bool SaveMeasurementUnitsConvert(MeasurementUnitsConvert unit) { return(new AdminDA().SaveMeasurementUnitsConvert(unit)); }
public bool SaveMeasurementUnitsConvert(MeasurementUnitsConvert newItem) { return(new AdminManager().SaveMeasurementUnitsConvert(newItem)); }
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); }
internal bool SaveMeasurementUnitsConvert(MeasurementUnitsConvert unit) { return(DataFacade.Instance.SaveMeasurementUnitsConvert(unit)); }
internal ShoppingFood[] GetMenuShoppingList(int menuId) { using (DataContext) { try { var measurmentUnits = DataContext.MeasurementUnits; var measureConverts = DataContext.MeasurementUnitsConverts; var ingredients = from i in DataContext.Ingredients join r in DataContext.Recipes on i.RecipeId equals r.RecipeId join mr in DataContext.MealRecipes on r.RecipeId equals mr.RecipeId join m in DataContext.Meals.Where(m => m.MenuId == menuId) on mr.MealId equals m.MealId select new { Ingredient = i, TotalServings = mr.Servings / 1.00M / r.Servings }; ShoppingFood[] list = (from f in DataContext.Foods where ingredients.Any(i => i.Ingredient.FoodId == f.FoodId) select new ShoppingFood(f)).ToArray(); foreach (ShoppingFood sf in list) { Dictionary <int, decimal> dictMeasureUnitsTotalQty = new Dictionary <int, decimal>(); //run on all ingredients with this food and fill dictinary of different measurement units foreach (var item in ingredients.Where(f => f.Ingredient.FoodId == sf.FoodId)) { decimal ingredientQty = item.Ingredient.Quantity * item.TotalServings; if (item.Ingredient.MeasurementUnitId == sf.CalculateUnitId) { if (!dictMeasureUnitsTotalQty.ContainsKey(sf.CalculateUnitId)) { dictMeasureUnitsTotalQty.Add(sf.CalculateUnitId, ingredientQty); } else { dictMeasureUnitsTotalQty[sf.CalculateUnitId] += ingredientQty; } } else { MeasurementUnitsConvert convert = measureConverts.SingleOrDefault(mc => mc.FoodId == sf.FoodId && mc.FromUnitId == item.Ingredient.MeasurementUnitId && mc.ToUnitId == sf.CalculateUnitId); if (convert != null) { decimal unitQuantity = (convert.ToQuantity / convert.FromQuantity) * ingredientQty; if (!dictMeasureUnitsTotalQty.ContainsKey(sf.CalculateUnitId)) { dictMeasureUnitsTotalQty.Add(sf.CalculateUnitId, unitQuantity); } else { dictMeasureUnitsTotalQty[sf.CalculateUnitId] += unitQuantity; } } else { if (!dictMeasureUnitsTotalQty.ContainsKey(item.Ingredient.MeasurementUnitId)) { dictMeasureUnitsTotalQty.Add(item.Ingredient.MeasurementUnitId, ingredientQty); } else { dictMeasureUnitsTotalQty[item.Ingredient.MeasurementUnitId] += ingredientQty; } } } } //fill DisplayQuantity of shopping food accordingly data in dictMeasureUnitsTotalQty foreach (KeyValuePair <int, decimal> item in dictMeasureUnitsTotalQty) { MeasurementUnit measurementUnitUsed = measurmentUnits.Single(mu => mu.UnitId == item.Key); sf.CalculateUnitId = measurementUnitUsed.UnitId; sf.CalculateUnitName = measurementUnitUsed.UnitName; sf.Quantity = item.Value; } } return(list); } catch { return(null); } } }