public string SmartQty(string id, double qty, string unit, double mass, List <FoodQty> foodQty, string lang) { string resp = null; string resp_ = null; Foods food = new Foods(); double baseQty = 0; string unit_ = food.GetUnit(qty, unit); FoodQty fq = foodQty.Where(a => a.id == id).FirstOrDefault(); if (fq != null) { baseQty = fq.qty; unit = fq.unit; } if (baseQty > 0) { if (unit == "l") { //***** Only for liquids (milk, oil, beverages...) ***** // if (mass / baseQty > 0.5) { qty = Math.Round(mass / baseQty, 1); unit_ = food.GetUnit(qty, t.Tran(unit, lang)); resp_ = string.Format(" (1{0}={1}{2})", t.Tran(unit, lang), baseQty, t.Tran("g", lang)); } } else { if (mass > baseQty) { qty = Math.Round(mass / baseQty, 1); unit_ = food.GetUnit(qty, t.Tran(unit, lang)); resp_ = string.Format(" (1{0}={1}{2})", t.Tran(unit, lang), baseQty, t.Tran("g", lang)); } } } resp = string.Format("{0} {1}{2}" , qty.ToString() , unit_ , !string.IsNullOrWhiteSpace(resp_) ? resp_ : null); return(resp); }
public object CreateShoppingList(List <Foods.NewFood> x, int consumers, string lang) { List <FoodQty> fq = LoadFoodQty(); object res = new object(); List <Foods.NewFood> list = new List <Foods.NewFood>(); Foods f = new Foods(); List <Foods.NewFood> foods = new List <Foods.NewFood>(); if (consumers >= 1) { foods = f.MultipleConsumers(x, consumers); } else { foods = x; } var group = foods.GroupBy(a => a.food).Select(a => new { id = a.Select(i => i.id).FirstOrDefault(), food = a.Key, qty = a.Sum(q => q.quantity), unit = f.GetUnit(a.Sum(q => q.quantity), a.Select(u => u.unit).FirstOrDefault()), mass = Math.Round(a.Sum(m => m.mass), 0), smartQty = SmartQty(a.Select(i => i.id).FirstOrDefault() , a.Sum(q => q.quantity) , f.GetUnit(a.Sum(q => q.quantity), a.Select(u => u.unit).FirstOrDefault()) , Math.Round(a.Sum(m => m.mass), 0) , fq , lang), smartMass = SmartMass(Math.Round(a.Sum(m => m.mass), 0), lang), price = Math.Round(a.Sum(p => p.price.value), 2), currency = a.Select(u => u.price.currency).FirstOrDefault() }).ToList(); var totalPrice = Math.Round(foods.Sum(a => a.price.value), 2); var currency = foods.Select(a => a.price.currency).FirstOrDefault(); res = new { foods = group, total = new { price = totalPrice, currency = currency } }; return(res); }
public string SmartQty(string id, double qty, string unit, double mass, List <FoodQty> foodQty, string lang) { Foods food = new Foods(); double baseQty = 0; string unit_ = food.GetUnit(qty, unit); FoodQty fq = foodQty.Where(a => a.id == id).FirstOrDefault(); if (fq != null) { baseQty = fq.qty; unit = fq.unit; } if (baseQty > 0 && mass > baseQty) { qty = Math.Round(mass / baseQty, 1); unit_ = food.GetUnit(qty, t.Tran(unit, lang)); } return(string.Format("{0} {1}{2}" , qty.ToString() , unit_ , baseQty > 0 && mass > baseQty ? string.Format(" (1 {0} = {1} {2})", t.Tran(unit, lang), baseQty, t.Tran("g", lang)) : "")); }
public NewShoppingList CreateShoppingList(List <Foods.NewFood> x, int consumers, string userId, string lang) { NewShoppingList shoppingList = new NewShoppingList(); shoppingList.response = new Global.Response(); //Stopwatch stopwatch = new Stopwatch(); //stopwatch.Start(); //shoppingList.time = 0; try { List <FoodQty> fq = LoadFoodQty(); List <Foods.NewFood> list = new List <Foods.NewFood>(); Foods f = new Foods(); List <Foods.NewFood> foods = new List <Foods.NewFood>(); if (consumers >= 1) { foods = f.MultipleConsumers(x, consumers); } else { foods = x; } Prices P = new Prices(); var prices = P.LoadData(userId, null); foreach (var g in foods) { var basicF = GetBasicFoodData(g.id, lang); basicF.price = prices.Find(a => a.food.title == basicF.basicFood); if (!string.IsNullOrWhiteSpace(basicF.basicFood)) { g.food = basicF.basicFood; } if (basicF.price != null) { g.price.currency = basicF.price.netPrice.currency; g.price.value = basicF.price.netPrice.value * g.mass / basicF.price.mass.value; } } var group = foods.GroupBy(a => a.food).Select(a => new GroupedFood { id = a.Select(i => i.id).FirstOrDefault(), food = a.Key, qty = a.Sum(q => q.quantity), unit = f.GetUnit(a.Sum(q => q.quantity), a.Select(u => u.unit).FirstOrDefault()), mass = Math.Round(a.Sum(m => m.mass), 0), smartQty = SmartQty(a.Select(i => i.id).FirstOrDefault() , a.Sum(q => q.quantity) , f.GetUnit(a.Sum(q => q.quantity), a.Select(u => u.unit).FirstOrDefault()) , Math.Round(a.Sum(m => m.mass), 0) , fq , lang), smartMass = SmartMass(Math.Round(a.Sum(m => m.mass), 0), lang), price = Math.Round(a.Sum(p => p.price.value), 2), currency = a.Select(u => u.price.currency).FirstOrDefault(), foodGroup = a.Select(i => i.foodGroup).FirstOrDefault() }).OrderBy(a => a.food).ToList(); var totalPrice = Math.Round(foods.Sum(a => a.price.value), 2); string currency = null; if (foods.Any(a => a.price.currency != null)) { currency = foods.FirstOrDefault(a => a.price.currency != null).price.currency; } List <Foods.FoodGroup> foodGroups = new List <Foods.FoodGroup>(); Foods F = new Foods(); using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + Server.MapPath("~/App_Data/" + dataBase))) { connection.Open(); foodGroups = F.GetFoodGroups(connection); } shoppingList.groupedFoodsByFoodGroup = new List <GroupedFoodsByFoodGroup>(); foreach (var fg in foodGroups) { GroupedFoodsByFoodGroup groupedFoodsByFoodGroup = new GroupedFoodsByFoodGroup(); groupedFoodsByFoodGroup.foodGroup = GetMainFoodGroupName(fg, foodGroups, lang); groupedFoodsByFoodGroup.foods = new List <GroupedFood>(); foreach (var g in group) { if (g.foodGroup.parent != "A") { if (g.foodGroup.parent == fg.group.code) { groupedFoodsByFoodGroup.foods.Add(g); } } else { if (g.foodGroup.code == fg.group.code) { groupedFoodsByFoodGroup.foods.Add(g); } } } if (groupedFoodsByFoodGroup.foods.Count > 0) { shoppingList.groupedFoodsByFoodGroup.Add(groupedFoodsByFoodGroup); } } shoppingList.total = new Total(); shoppingList.total.price = totalPrice; shoppingList.total.currency = currency; shoppingList.response.isSuccess = true; shoppingList.response.msg = "ok"; //shoppingList.time = stopwatch.Elapsed.TotalSeconds; return(shoppingList); } catch (Exception e) { L.SendErrorLog(e, x[0].id, null, "ShoppingList", "CreateShoppingList"); shoppingList.response.isSuccess = false; shoppingList.response.msg = e.Message; return(shoppingList); } }