/// <summary> /// Jared Greenfield /// Created: 2019/01/30 /// /// <remarks> /// Jared Greenfield /// Updated: 2019/02/06 /// Modified to allow creation of internal items and external Offerings. /// Ex. A recipe for a roux might be saved, but you wouldn't serve just a roux as /// a menu item. /// </remarks> /// <remarks> /// Jared Greenfield /// Updated: 2019/03/05 /// Added Update capability for offerings and items to the edit feature. /// </remarks> /// Submits the recipe after validation to be stored in the database. /// </summary> private void BtnSubmit_Click(object sender, RoutedEventArgs e) { if (btnSubmit.Content.ToString() == "Submit New Recipe") { if (validateForm()) { try { int recipeID = 0; Offering offering = null; Item newItem = null; // If the item will be a publicly available dish, it will be created as an Offering if (chkPurchasable.IsChecked == true) { // Remove the 100000 when moving to production offering = new Offering("Item", _user.EmployeeID, txtRecipeDescription.Text, Decimal.Parse(txtPrice.Text)); } newItem = new Item(null, (bool)chkPurchasable.IsChecked, recipeID, cboType.SelectedItem.ToString(), txtRecipeDescription.Text, 0, txtRecipeName.Text, 0); newItem.DateActive = DateTime.Now; Recipe recipe = new Recipe(txtRecipeName.Text, txtRecipeDescription.Text, DateTime.Now); List <RecipeItemLineVM> lines = new List <RecipeItemLineVM>(); foreach (var item in dgIngredientList.Items) { lines.Add((RecipeItemLineVM)item); } recipe.RecipeLines = lines; // Add the recipe and the lines recipeID = _recipeManager.CreateRecipe(recipe, newItem, offering); this.DialogResult = true; } catch (Exception ex) { MessageBox.Show("There was an error adding the recipe. " + ex.Message); } } } else if (btnSubmit.Content.ToString() == "Edit Recipe") { SetupEditPage(); } else if (btnSubmit.Content.ToString() == "Save Changes") { if (validateForm()) { try { Recipe newRecipe = new Recipe(_oldRecipe.RecipeID, txtRecipeName.Text, txtRecipeDescription.Text, _oldRecipe.DateAdded, (bool)chkActive.IsChecked); List <RecipeItemLineVM> lines = new List <RecipeItemLineVM>(); foreach (var item in _recipeManager.RecipeLines) { lines.Add((RecipeItemLineVM)item); } newRecipe.RecipeLines = lines; bool isRecipeUpdateSuccessful = _recipeManager.UpdateRecipe(_oldRecipe, newRecipe); int offeringID; // If the recipe is purchasable by the public if (chkPurchasable.IsChecked == true) { //If the recipe was previously an offering if (_offering != null) { offeringID = _offering.OfferingID; //If there was a change to offering if (_isOfferingChanged) { Offering newOffering = new Offering(_offering.OfferingID, "Item", _offering.EmployeeID, txtRecipeDescription.Text, Decimal.Parse(txtPrice.Text), (bool)chkActive.IsChecked); _offeringManager.UpdateOffering(_offering, newOffering); } else { // Do nothing as nothing in offering was changed } } else // Happens when a recipe is newly promoted to an Offering { Offering offering = new Offering("Item", _user.EmployeeID, txtRecipeDescription.Text, Decimal.Parse(txtPrice.Text)); offeringID = _offeringManager.CreateOffering(offering); } if (_isItemChanged) { Item newItem = new Item(_item.ItemID, (int)offeringID, (bool)chkPurchasable.IsChecked, _oldRecipe.RecipeID, cboType.SelectedItem.ToString(), txtRecipeDescription.Text, _item.OnHandQty, txtRecipeName.Text, _item.ReorderQty, _item.DateActive, (bool)chkActive.IsChecked); _itemManager.UpdateItem(_item, newItem); } } else { // Was previously an offering if (_offering != null) { Offering newOffering = new Offering(_offering.OfferingID, "Item", _offering.EmployeeID, txtRecipeDescription.Text, Decimal.Parse(txtPrice.Text), (bool)chkPurchasable.IsChecked); _offeringManager.UpdateOffering(_offering, newOffering); if (_isItemChanged) { Item newItem = new Item(_item.ItemID, _offering.OfferingID, (bool)chkPurchasable.IsChecked, _oldRecipe.RecipeID, cboType.SelectedItem.ToString(), txtRecipeDescription.Text, _item.OnHandQty, txtRecipeName.Text, _item.ReorderQty, _item.DateActive, (bool)chkActive.IsChecked); _itemManager.UpdateItem(_item, newItem); } } else { if (_isItemChanged) { Item newItem = new Item(_item.ItemID, null, (bool)chkPurchasable.IsChecked, _oldRecipe.RecipeID, cboType.SelectedItem.ToString(), txtRecipeDescription.Text, _item.OnHandQty, txtRecipeName.Text, _item.ReorderQty, _item.DateActive, (bool)chkActive.IsChecked); _itemManager.UpdateItem(_item, newItem); } } } if (isRecipeUpdateSuccessful) { MessageBox.Show("Update successful."); this.DialogResult = true; } else { throw new Exception("Recipe update unsuccessful."); } _isItemChanged = false; _isOfferingChanged = false; } catch (Exception ex) { MessageBox.Show("There was an error updating the record: " + ex.Message); } } } }