private void bntAddOrderItem_Click(object sender, RoutedEventArgs e) { ProductRangeItem selected = cbProducts.SelectedItem as ProductRangeItem; ProductViewModel vmSelected = ProductRangeItemToViewModel(selected); if (vmSelected.Ingredients != null && vmSelected.Ingredients.MilkTypeIngredients != null && vmSelected.Ingredients.MilkTypeIngredients.Options != null) { ProductOptionViewModel defaultMilkOption = vmSelected.Ingredients.MilkTypeIngredients.Options.ElementAt(0); vmSelected.Ingredients.MilkTypeIngredients.SelectedOption = defaultMilkOption; if (!CheckStockQuantitiesForProductOption(defaultMilkOption, vmSelected.Ingredients.MilkTypeIngredients.StockUnit)) { if (vmSelected.Ingredients.MilkTypeIngredients.Options.Count() > 1) { bool foundAvailableMilkOption = false; int counter = 1; while (!foundAvailableMilkOption && counter < vmSelected.Ingredients.MilkTypeIngredients.Options.Count()) { ProductOptionViewModel milkOption = vmSelected.Ingredients.MilkTypeIngredients.Options.ElementAt(counter); if (CheckStockQuantitiesForProductOption(milkOption, vmSelected.Ingredients.MilkTypeIngredients.StockUnit)) { vmSelected.Ingredients.MilkTypeIngredients.SelectedOption = milkOption; foundAvailableMilkOption = true; MessageBox.Show("Product cannot be served with the default milk option: " + defaultMilkOption.Name); } counter++; } } } } vmSelected.Unit = 1; if (CheckStockQuantitiesForProduct(vmSelected)) { _OrderItems.Add(vmSelected); dgBeverages.ItemsSource = OrderItems; } }
private ProductViewModel ProductRangeItemToViewModel(ProductRangeItem item) { ProductViewModel vm = new ProductViewModel(); vm.Id = item.Id; vm.Name = item.Name; vm.Price = item.Price; vm.Category = item.Category; vm.Ingredients = new IngredientsViewModel(); if (item.Category.ToString().Contains("beverage")) { vm.Ingredients.ExtraIngredients = _ProductAddOns; } if (item.Ingredients != null) { if (item.Ingredients.BaseIngredients != null && item.Ingredients.BaseIngredients.Count() > 0) { List <BaseIngredientViewModel> vmBaseIngredientList = new List <BaseIngredientViewModel>(); foreach (var i in item.Ingredients.BaseIngredients) { BaseIngredientViewModel vmBaseIngredient = new BaseIngredientViewModel() { Id = i.Id, StockUnit = i.StockUnit, Name = _AllProducts.Where(p => p.Id == i.Id).Select(p => p.Name).Single(), }; vmBaseIngredientList.Add(vmBaseIngredient); } vm.Ingredients.BaseIngredients = vmBaseIngredientList; } if (item.Ingredients.MilkTypeIngredients != null) { MilkTypeIngredientsViewModel vmMilkType = new MilkTypeIngredientsViewModel() { StockUnit = item.Ingredients.MilkTypeIngredients.StockUnit, }; vm.Ingredients.MilkTypeIngredients = vmMilkType; List <ProductOptionViewModel> vmOptionList = new List <ProductOptionViewModel>(); foreach (var id in item.Ingredients.MilkTypeIngredients.Options) { ProductOptionViewModel vmOption = new ProductOptionViewModel() { Id = id, Name = _AllProducts.Where(p => p.Id == id).Select(p => p.Name).Single(), }; vmOptionList.Add(vmOption); } vm.Ingredients.MilkTypeIngredients.Options = vmOptionList; } if (item.Ingredients.ExtraIngredients != null && item.Ingredients.ExtraIngredients.Count() > 0) { List <ProductOptionViewModel> vmOptionList = new List <ProductOptionViewModel>(); foreach (var id in item.Ingredients.ExtraIngredients) { ProductOptionViewModel vmOption = new ProductOptionViewModel() { Id = id, Name = _AllProducts.Where(p => p.Id == id).Select(p => p.Name).Single(), }; vmOptionList.Add(vmOption); vmOptionList.AddRange(vm.Ingredients.ExtraIngredients); } vm.Ingredients.ExtraIngredients = vmOptionList; } } return(vm); }