/// <summary> /// Verifies in the database if the given Recette is in use in an already started lot /// </summary> /// <param name="recette"></param> /// <returns></returns> public bool IsRecetteInUse(Recette recette) { bool isInUse = false; OpenConnection(); string SQLString = "SELECT Count(*) FROM `lot` WHERE Stu_ID = 1 AND Rct_Numero = @Rct_Numero " + "OR Stu_ID = 2 AND Rct_Numero = @Rct_Numero " + "OR Stu_ID = 3 AND Rct_Numero = @Rct_Numero"; MySqlCommand cmd = Conn.CreateCommand(); cmd.CommandText = SQLString; cmd.Parameters.AddWithValue("@Rct_Numero", recette.ID); cmd.Prepare(); MySqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { string test = reader["Count(*)"].ToString(); isInUse = reader["Count(*)"].ToString() != "0"; } return(isInUse); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DeleteRecette_Click(object sender, EventArgs e) { if (dataGridView1.SelectedCells.Count == 1) { int selectedRowIndex = dataGridView1.SelectedCells[0].RowIndex; Recette recette = ListRecette[selectedRowIndex]; string message = string.Format("Voulez-vous supprimer la recette \"{0}\"?", recette.Nom); DialogResult result = MessageBox.Show(message, "Suppression recette", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { if (Program.manager.IsRecetteInUse(recette)) { MessageBox.Show("Cette recette est en utilisation et ne peux pas être supprimée.", "Recette déjà utilisé", MessageBoxButtons.OK); } else { Program.manager.DeleteRecette(recette); } } } else { MessageBox.Show("Veuillez séléctionner uniquement une cellule dans la liste des recettes"); } SetupDataGridView(); }
/// <summary> /// Updates the datagrid with the given filters /// </summary> /// <param name="filterParameters"></param> private void UpdateDataGrid(LotFilterParameters filterParameters) { List <Lot> lots = Program.manager.GetFilteredLots(filterParameters); lotDataGrid.Rows.Clear(); Recette recette = new Recette(); for (int i = 0; i < lots.Count(); i++) { recette = Program.manager.GetRecetteByID(lots[i].RecetteID); string status = ""; switch (lots[i].StatusID) { case 1: status = "Terminé"; break; case 2: status = "En Production"; break; case 3: status = "En attente"; break; case 4: status = "Ouvert"; break; } string[] row = { lots[i].Nom, lots[i].DateCreation.ToString(), lots[i].DateButoir.ToString(), lots[i].Quantite.ToString(), lots[i].QuantiteAtteinte.ToString(), recette.Nom, status }; lotDataGrid.Rows.Add(row); } }
/// <summary> /// Adds a new Recette /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void NewRecette_Click(object sender, EventArgs e) { // Create a new recette to use it later Recette recette = new Recette(); recette.Nom = "Nouvelle recette"; // Adds the recette to the database and the recette list Program.manager.CreateRecette(recette); // Realoading the datagrid SetupDataGridView(); // Changes the active recette to this one ChangeActiveRecette(ListRecette.Last().ID); }
/// <summary> /// Creates a new recette in the database /// </summary> /// <param name="recette">Recette to add</param> public void CreateRecette(Recette recette) { OpenConnection(); // Preparing the statement string SQLString = "INSERT INTO recette (Rct_Nom) VALUES (@Rct_Nom)"; MySqlCommand cmd = Conn.CreateCommand(); cmd.CommandText = SQLString; cmd.Parameters.AddWithValue("@Rct_Nom", recette.Nom); cmd.Prepare(); cmd.ExecuteNonQuery(); CloseConnection(); }
/// <summary> /// Updates the name of the recette /// </summary> /// <param name="recette">The recette to change</param> /// <param name="name">New name for the recette</param> public void UpdateRecetteName(Recette recette, string name) { OpenConnection(); // Preparing the statement string SQLString = "UPDATE recette SET Rct_Nom = @val1 WHERE Rct_Numero = @val2"; MySqlCommand cmd = Conn.CreateCommand(); cmd.CommandText = SQLString; cmd.Parameters.AddWithValue("@val1", name); cmd.Parameters.AddWithValue("@val2", recette.ID); cmd.Prepare(); cmd.ExecuteReader(); CloseConnection(); }
/// <summary> /// Deletes the recette with all its operations /// </summary> /// <param name="recette"></param> public void DeleteRecette(Recette recette) { // First deleting all oeration of this recette List <Operation> operations = GetAllOperByRecID(recette.ID); for (int i = 0; i < operations.Count(); i++) { DeleteOperation(operations[i]); } // Deleting the recette OpenConnection(); string SQLString = "DELETE FROM recette WHERE Rct_Numero = @rct_numero"; MySqlCommand cmd = Conn.CreateCommand(); cmd.CommandText = SQLString; cmd.Parameters.AddWithValue("@rct_numero", recette.ID); cmd.ExecuteNonQuery(); CloseConnection(); }
/// <summary> /// Gets and returns the Recette by the given id from the database /// </summary> /// <param name="ID"></param> /// <returns></returns> public Recette GetRecetteByID(int ID) { string SQLString = string.Format("SELECT * FROM Recette WHERE Rct_Numero = {0}", ID); OpenConnection(); MySqlCommand cmd = Conn.CreateCommand(); cmd.CommandText = SQLString; MySqlDataReader reader = cmd.ExecuteReader(); reader.Read(); Recette recette = new Recette(); recette.ID = int.Parse(reader["Rct_Numero"].ToString()); recette.Nom = reader["Rct_Nom"].ToString(); CloseConnection(); return(recette); }
/// <summary> /// Click of the "Charger" button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { // Verify that only one cell is selected if (dataGridView1.SelectedCells.Count > 1) { string message = "Veuillez selectionner seulement une case"; string caption = "Multiples cellules selectionnées"; MessageBoxButtons buttons = MessageBoxButtons.OK; MessageBox.Show(message, caption, buttons); } else { int rowIndex = dataGridView1.SelectedCells[0].RowIndex; int id = ListRecette[rowIndex].ID; bool save = false; bool cancel = false; // if unsaved changes exist, asks the user if he wants to save them if (UnSavedChanges) { string message = "Il existe des changements non sauvegardés, voulez-vous les sauvegarder?"; string caption = "Sauvegarder?"; MessageBoxButtons buttons = MessageBoxButtons.YesNoCancel; DialogResult result = MessageBox.Show(message, caption, buttons); save = result == DialogResult.Yes; cancel = result == DialogResult.Cancel; } // When saving is needed if (save) { SaveChanges(); } // Charging everything if (!cancel) { ListOperation = Program.manager.GetAllOperByRecID(id); Operation[] operations = ListOperation.ToArray(); dataGridView2.Rows.Clear(); for (int i = 0; i < operations.Length; i++) { string description = ""; if (operations[i].Description != null) { description = operations[i].Description.ToString(); } string[] row = { operations[i].Numero.ToString(), operations[i].Position.ToString(), operations[i].Temps.ToString(), description, operations[i].Quittance ? "Avec" : "Sans", }; dataGridView2.Rows.Add(row); } ChangeActiveRecette(id); } // If the recette is in use, block all modifications Recette recette = new Recette(); recette.ID = id; dataGridView2.ReadOnly = Program.manager.IsRecetteInUse(recette); } }