Esempio n. 1
0
        private void btnEditRecipe_Click(object sender, EventArgs e)
        {
            if (btnEditRecipe.Text == "Edit Recipe")
            {
                //Switches the form into a editable state
                txtName.Enabled              = true;
                txtPriceSold.Enabled         = true;
                txtPrepTime.Enabled          = true;
                txtCookTime.Enabled          = true;
                txtDirections.Enabled        = true;
                txtDirections.ReadOnly       = false;
                txtTags.Enabled              = true;
                txtServingSize.Enabled       = true;
                cboIng.Enabled               = true;
                cboUnit.Enabled              = true;
                txtQty.Enabled               = true;
                btnAddIng.Enabled            = true;
                btnEditIng.Enabled           = true;
                btnDeleteIng.Enabled         = true;
                btnAddToTagList.Enabled      = true;
                btnRemoveSelectedTag.Enabled = true;
                btnEditRecipe.Text           = "Save Updates";
                lsvIngredients.Enabled       = true;
                lbxTags.Enabled              = true;
                btnPrint.Visible             = true;
            }
            else
            {
                //Save Data...Hunter's version of what dustin did on the Entry page
                DialogResult button = MessageBox.Show("Are you sure you want to overwrite the previous data?", "Save Edited Recipe", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (button == DialogResult.Yes)
                {
                    //Basically, create brand new foodstuff from all the parameters here, getting the old FSID
                    //then remove the old foodstuff's recipematerial entries that don't match the new one's
                    string strID = fstoUpdate.ID;

                    string strName = fstoUpdate.Name;
                    if (txtName.Text != strName)
                    {
                        strName = txtName.Text;
                    }

                    int intPrepTime = fstoUpdate.PrepTime;
                    if (txtPrepTime.Text == "")
                    {
                        txtPrepTime.Text = "-1";
                    }
                    else if (txtPrepTime.Text != intPrepTime.ToString())
                    {
                        if (!int.TryParse(txtPrepTime.Text, out intPrepTime))
                        {
                            MessageBox.Show("Error converting new prep time value.  Did you enter it correctly?");
                            return;
                        }
                    }

                    int intCookTime = fstoUpdate.CookTime;
                    if (txtCookTime.Text == "")
                    {
                        txtCookTime.Text = "-1";
                    }
                    else if (txtCookTime.Text != intCookTime.ToString())
                    {
                        if (!int.TryParse(txtCookTime.Text, out intCookTime))
                        {
                            MessageBox.Show("Error converting new cook time value.  Did you enter it correctly?");
                            return;
                        }
                    }

                    int intServings = fstoUpdate.Servings;
                    if (txtServingSize.Text == "")
                    {
                        txtServingSize.Text = "-1";
                    }
                    else if (txtServingSize.Text != intServings.ToString())
                    {
                        if (!int.TryParse(txtServingSize.Text, out intServings))
                        {
                            MessageBox.Show("Error converting new serving size value.  Did you enter it correctly?");
                            return;
                        }
                    }

                    double dblCost = fstoUpdate.Cost;
                    if (txtPriceSold.Text == "")
                    {
                        txtPriceSold.Text = "-1.0";
                    }
                    else if (txtPriceSold.Text != dblCost.ToString())
                    {
                        if (!Double.TryParse(txtPriceSold.Text, out dblCost))
                        {
                            MessageBox.Show("Error converting new price value.  Did you enter it correctly?");
                            return;
                        }
                    }

                    string strDirections = fstoUpdate.Directions;
                    if (txtDirections.Text == "")
                    {
                        strDirections = txtDirections.Text;
                    }
                    else if (txtDirections.Text != strDirections)
                    {
                        strDirections = txtDirections.Text;
                    }

                    List <string> newTags = new List <string>();
                    if (lbxTags.Items.Count > 0)
                    {
                        for (int i = 0; i < lbxTags.Items.Count; i++)
                        {
                            newTags.Add(lbxTags.Items[i].ToString());
                        }
                    }

                    //

                    //if item in list but not fs's list, add it, then add as its own foodstuff
                    //if item matches, check unit and quantity
                    //if different, update from ingredient list
                    //if item in fs's list is not in ingredient list, remove it
                    List <Recipe> newIngrList = new List <Recipe>();


                    if (lsvIngredients.Items.Count > 0)
                    {
                        foreach (ListViewItem lvi in lsvIngredients.Items)
                        {
                            //check to see whether the item exists or not by matching name, which is the third column of the listview.
                            if (DataConnection.FindFoodstuffsNamed(lvi.SubItems[2].Text).Count > 0)
                            {
                                //this is supposed to return true if a name match is found
                                //and add it to the new food's ingredient list
                                newIngrList.Add(new Recipe(strID, (DataConnection.FindFoodstuffsNamed(lvi.SubItems[2].Text)[0].ID),
                                                           lvi.SubItems[0].Text, lvi.SubItems[1].Text));
                            }
                            else
                            {
                                //Did not find a match...
                                //making dummy entries

                                //grab the first up to 4 characters of the name
                                string newPHID = lvi.SubItems[2].Text.Substring(0, Math.Min(lvi.SubItems[2].Text.Split(' ')[0].Length, 4));

                                newPHID += (DataConnection.NumFoodstuffs() + 1).ToString("0000#");

                                //new placeholder ID, name from listview's name column
                                FoodStuff fsPlaceholder = new FoodStuff(newPHID, lvi.SubItems[2].Text);
                                //should automatically generate the self-referencing RecipeMaterial stub...
                                DataConnection.AddFoodStuff(fsPlaceholder);
                                //then put this in the list for the actual food we're going to add
                                newIngrList.Add(new Recipe(strID, newPHID, lvi.SubItems[0].Text, lvi.SubItems[1].Text));
                            }
                        }
                    }
                    //now update it!
                    DataConnection.UpdateFoodstuff(new FoodStuff(strID, strName, strDirections, intPrepTime, intCookTime, dblCost, intServings, newTags, newIngrList));
                }


                //List<Recipe> newItemIngredients = new List<Recipe>();
                //if (lsvIngredients.Items.Count == 0)
                //{
                //    //Not sure if this will need to change for updating...We only need the new ingredients
                //    newItemIngredients.Add(new Recipe(newFS.ID, fstoUpdate.ID));
                //}
                //else
                //{
                //   foreach(ListViewItem lvi in lsvIngredients.Items)
                //    if(DataConnection.FindFoodstuffsNamed(lvi.SubItems[0].Text).Count > 0 )
                //    {
                //        newItemIngredients.Add(new Recipe(fstoUpdate.ID, (DataConnection.FindFoodstuffsNamed(lvi.SubItems[0].Text)[0].ID),
                //                                   lvi.SubItems[1].Text, lvi.SubItems[2].Text));
                //    }
                //    else
                //    {
                //         //Did not find a match...Dustin Dummy entries

                //         string newPHID = lvi.SubItems[0].Text.Substring(0, Math.Min(lvi.SubItems[0].Text.Split(' ')[0].Length, 4));//grab the first up to 4 characters of the name
                //         newPHID += (DataConnection.NumFoodstuffs()+1).ToString("0000#");
                //         FoodStuff fsPlaceholder = new FoodStuff(newPHID, lvi.SubItems[0].Text);
                //         DataConnection.AddFoodStuff(fsPlaceholder, new Recipe(newPHID, newPHID));

                //         newItemIngredients.Add(new Recipe(newFS.ID, newPHID, lvi.SubItems[1].Text, lvi.SubItems[2].Text));
                //   }

                //}

                //then reset the form back into a readonly state
                btnEditRecipe.Text           = "Edit Recipe";
                txtName.Enabled              = false;
                txtPriceSold.Enabled         = false;
                txtPrepTime.Enabled          = false;
                txtCookTime.Enabled          = false;
                txtDirections.Enabled        = false;
                txtTags.Enabled              = false;
                txtServingSize.Enabled       = false;
                cboIng.Enabled               = false;
                cboUnit.Enabled              = false;
                txtQty.Enabled               = false;
                btnAddIng.Enabled            = false;
                btnEditIng.Enabled           = false;
                btnDeleteIng.Enabled         = false;
                btnAddToTagList.Enabled      = false;
                btnRemoveSelectedTag.Enabled = false;
                txtDirections.ReadOnly       = true;
                lblMessage.Text              = "Data edit saved";
                timer1.Start();
                lsvIngredients.Enabled = false;
                lbxTags.Enabled        = false;
            }
        }