/// <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);
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        ///Kevin Broskow
        /// Created: 2019/01/21
        ///
        /// Method to handle the event of the primary button being clicked.
        /// </summary>
        /// <remarks>
        /// Jared Greenfield
        /// Updated: 2019/04/04
        /// Updated to remove Item and add in Item
        /// Added Price Added Offering Support
        /// </remarks>
        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            bool offerCreated = false;

            if (!ValidateInput())
            {
                return;
            }
            if (((String)this.btnSubmit.Content) == "Submit")
            {
                try
                {
                    int OfferingID;
                    if (cbPurchasable.IsChecked == true)
                    {
                        Decimal outVal;
                        Decimal.TryParse(txtPrice.Text, out outVal);
                        Offering newOffering = new Offering();
                        newOffering.Price          = outVal;
                        newOffering.Description    = txtDescription.Text;
                        newOffering.EmployeeID     = 100000;
                        newOffering.OfferingTypeID = "Item";
                        OfferingID         = _offeringManager.CreateOffering(newOffering);
                        oldItem.OfferingID = OfferingID;
                        offerCreated       = true;
                    }

                    oldItem.Name                = this.txtName.Text;
                    oldItem.OnHandQty           = Int32.Parse(this.txtOnHand.Text);
                    oldItem.ReorderQty          = Int32.Parse(this.txtReorder.Text);
                    oldItem.ItemType            = this.cboItemType.SelectedItem.ToString();
                    oldItem.Description         = this.txtDescription.Text;
                    oldItem.DateActive          = DateTime.Now;
                    oldItem.CustomerPurchasable = (bool)cbPurchasable.IsChecked;
                    oldItem.ItemID              = _itemManager.CreateItem(oldItem);
                    if ((bool)this.cbPurchasable.IsChecked)
                    {
                        oldItem.CustomerPurchasable = true;
                    }
                    else
                    {
                        oldItem.CustomerPurchasable = false;
                    }
                    MessageBox.Show("Add worked ");
                }
                catch (Exception ex)
                {
                    if (offerCreated)
                    {
                        try
                        {
                            _offeringManager.DeleteOfferingByID((int)oldItem.OfferingID);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Blank offering Created. Please Delete.");
                        }
                    }
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
            else if (((String)this.btnSubmit.Content) == "Save")
            {
                try
                {
                    int      offeringID = 0;
                    Offering newOffering;
                    if (_offering != null)
                    {
                        newOffering = new Offering()
                        {
                            OfferingID     = _offering.OfferingID,
                            OfferingTypeID = _offering.OfferingTypeID,
                            Active         = _offering.Active,
                            Description    = _offering.Description,
                            EmployeeID     = 100000,
                            Price          = (Decimal)1.0
                        };
                    }
                    else
                    {
                        newOffering = new Offering()
                        {
                            OfferingTypeID = "Item",
                            Description    = txtDescription.Text,
                            EmployeeID     = 100000,
                            Price          = (Decimal)1.0
                        };
                    }
                    Decimal outVal;
                    Decimal.TryParse(txtPrice.Text, out outVal);
                    newOffering.Price = outVal;
                    if (cbPurchasable.IsChecked == true && oldItem.OfferingID != null)
                    {
                        //Update the existing Offering
                        _offeringManager.UpdateOffering(_offering, newOffering);
                    }
                    else if (cbPurchasable.IsChecked == true && oldItem.OfferingID == null)
                    {
                        // Create an offering because one doesn't exist
                        newOffering        = new Offering("Item", 100000, txtDescription.Text, outVal);
                        offeringID         = _offeringManager.CreateOffering(newOffering);
                        newItem.OfferingID = offeringID;
                    }
                    else if (oldItem.OfferingID == null)
                    {
                        // Do nothing with offerings because the other two choices are already taken care of.
                    }
                    newItem.Name        = this.txtName.Text;
                    newItem.OnHandQty   = Int32.Parse(this.txtOnHand.Text);
                    newItem.ReorderQty  = Int32.Parse(this.txtReorder.Text);
                    newItem.ItemType    = this.cboItemType.SelectedItem.ToString();
                    newItem.Description = this.txtDescription.Text;
                    newItem.ItemID      = oldItem.ItemID;
                    if (newItem.OfferingID == null)
                    {
                        newItem.OfferingID = oldItem.OfferingID;
                    }
                    newItem.DateActive = (DateTime)this.dpDateActive.SelectedDate;
                    if (this.txtPrice.Text != "" && this.txtPrice.Text != null)
                    {
                        newItem.RecipeID = oldItem.RecipeID;
                    }
                    else
                    {
                        newItem.RecipeID = null;
                    }
                    if ((bool)this.cbPurchasable.IsChecked)
                    {
                        newItem.CustomerPurchasable = true;
                    }
                    else
                    {
                        oldItem.CustomerPurchasable = false;
                    }
                    if (cbActive.IsChecked == true)
                    {
                        newItem.Active = true;
                    }
                    else
                    {
                        newItem.Active = false;
                    }
                    _itemManager.UpdateItem(oldItem, newItem);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
            this.Close();
        }