public List <QueryLineItem> GetQueryLineItemsData(IUnitOfWork unitOfWork, DateTime fromDate, DateTime toDate, bool includeIngredients) { List <QueryLineItem> result = new List <QueryLineItem>(); DateTime startDate = fromDate.Date; DateTime endDate = toDate.Date.AddDays(1); //get data var query = from li in unitOfWork.LineItemRepository.Get(x => x.Order.Date >= startDate && x.Order.Date < endDate) //where li.Order.Date >= startDate && li.Order.Date < endDate select li; //group li by li.Product; //UnitMeasure unitUM = context.UnitMeasures.Single(x => x.Name == "U"); foreach (var li in query) { QueryLineItem copy = new QueryLineItem(); copy.Quantity = li.Quantity; copy.UnitOfMeasure = li.UnitMeasure;; copy.Product = li.Product; copy.Amount = li.Amount; result.Add(copy); //add ingredients if (includeIngredients && IsRecipe(copy.Product)) { AddIngredients(copy.Product, copy.Quantity, copy.UnitOfMeasure, result); } }//foreach return(result); }
private void AddIngredients(Product recipeProduct, double qtty, UnitMeasure um, List <QueryLineItem> result) { double proportion = qtty * um.ToBaseConversion / recipeProduct.RecipeQuantity / recipeProduct.RecipeUnitMeasure.ToBaseConversion; foreach (var ingredient in recipeProduct.Ingredients) { QueryLineItem ing_op = new QueryLineItem(); ing_op.Quantity = proportion * ingredient.Quantity; ing_op.UnitOfMeasure = ingredient.UnitMeasure; ing_op.Product = ingredient.IngredientProduct; result.Add(ing_op); //add ingredients recursively if (IsRecipe(ingredient.IngredientProduct)) { AddIngredients(ing_op.Product, ing_op.Quantity, ing_op.UnitOfMeasure, result); } } }