/// <summary> /// Supprime l'élement sélectionné du bon tableau /// </summary> /// <param name="NodeName"></param> /// <param name="SelectedItem"></param> private void DeleteItemListView(String NodeName, ListViewItem SelectedItem) { switch (NodeName) { case "Articles": ArticleDAO.DeleteArticle(SelectedItem.SubItems[2].Text); UpdateListView("Articles"); break; case "Marques": BrandDAO.DeleteBrand(int.Parse(SelectedItem.SubItems[1].Text)); UpdateListView("Marques"); break; case "Familles": FamilyDAO.DeleteFamily(int.Parse(SelectedItem.SubItems[1].Text)); UpdateListView("Familles"); break; case "Sous familles": SubFamilyDAO.DeleteSubFamily(int.Parse(SelectedItem.SubItems[1].Text)); UpdateListView("Sous familles"); break; default: break; } }
/// <summary> /// Créer la sous famille lorsque l'on clique sur le bouton valider /// </summary> /// <param name="Sender"></param> /// <param name="Event"></param> private void OkButton_Click(object Sender, EventArgs Event) { int IntRef; // vérifie si le contenue du champ reference est bien un nombre if (int.TryParse(RefTextBox.Text, out IntRef)) { // vérifie que le champ nom soit remplie if (NameTextBox.Text != "" && FamilyComboBox.Text != "") { if (SubFamilyDAO.GetSubFamilyById(IntRef) == null) { SubFamily NewSubFamily = new SubFamily(IntRef, NameTextBox.Text, (Family)FamilyComboBox.SelectedItem); SubFamilyDAO.AddSubFamily(NewSubFamily); this.Close(); } else { MessageBox.Show("Ref existe déjà", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Les champs doivent etre remplient", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Référence doit etre un chiffre", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Si les modifications sont valides change la sous famille concernée à l'appuie sur le bouton valider /// </summary> /// <param name="Sender"></param> /// <param name="Event"></param> private void OkButton_Click(object Sender, EventArgs Event) { // vérifie que les champs soient remplies if (NameTextBox.Text != "") { SubFamilyDAO.EditSubFamily(int.Parse(SubFamilyNameLabel.Text), NameTextBox.Text, (Family)FamilyComboBox.SelectedItem); this.Close(); } else { MessageBox.Show("Les champs doivent etre remplient", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Créer une sous famille à partir de son nom et de sa famille /// </summary> /// <param name="NameSubFamilyToSet"></param> /// <param name="FamilyToSet"></param> public SubFamily(String NameSubFamilyToSet, Family FamilyToSet) { NameSubFamily = NameSubFamilyToSet; RefFamily = FamilyToSet; RefSubFamily = -1; int TestRef = -1; // Recherche d'un id inutilisé while (RefSubFamily == -1) { TestRef++; if (SubFamilyDAO.GetSubFamilyById(TestRef) == null) { RefSubFamily = TestRef; } } }
/// <summary> /// Recréer les tableaux en fonctions des données de la bdd pour le mettre à jour /// </summary> /// <param name="NodeName"></param> private void UpdateListView(String NodeName) { int ColumnsWidth = 0; // reinitialise le trie MainListView.ListViewItemSorter = null; // choisi quel tableau mettre à jour ou créer switch (NodeName) { case "Articles": Article[] Articles = ArticleDAO.GetAllArticles(); MainListView.Columns.Clear(); MainListView.Items.Clear(); MainListView.Groups.Clear(); ColumnsWidth = MainListView.Width / 7; MainListView.Columns.Add("Quantité", ColumnsWidth); MainListView.Columns.Add("Description", ColumnsWidth); MainListView.Columns.Add("Référence", ColumnsWidth); MainListView.Columns.Add("Marque", ColumnsWidth); MainListView.Columns.Add("Famille", ColumnsWidth); MainListView.Columns.Add("Sous-Famille", ColumnsWidth); MainListView.Columns.Add("Prix H.T.", ColumnsWidth); foreach (Article A in Articles) { string[] ArticleToAdd = new string[7]; ListViewItem ArticleItem; // ajoute les items a la ListView ArticleToAdd[0] = A.Quantity.ToString(); ArticleToAdd[1] = A.Description; ArticleToAdd[2] = A.RefArticle; ArticleToAdd[3] = A.RefBrand.ToString(); ArticleToAdd[4] = A.RefSubFamily.RefFamily.ToString(); ArticleToAdd[5] = A.RefSubFamily.ToString(); ArticleToAdd[6] = A.PriceHT.ToString(); ArticleItem = new ListViewItem(ArticleToAdd); MainListView.Items.Add(ArticleItem); } break; case "Marques": Brand[] Brands = BrandDAO.GetAllBrands(); MainListView.Columns.Clear(); MainListView.Items.Clear(); MainListView.Groups.Clear(); ColumnsWidth = MainListView.Width / 2; MainListView.Columns.Add("Nom", ColumnsWidth); MainListView.Columns.Add("Référence", ColumnsWidth); foreach (Brand B in Brands) { string[] BrandToAdd = new string[2]; ListViewItem BrandItem; // ajoute les items a la ListView BrandToAdd[0] = B.NameBrand; BrandToAdd[1] = B.RefBrand.ToString(); BrandItem = new ListViewItem(BrandToAdd); MainListView.Items.Add(BrandItem); } break; case "Familles": Family[] Families = FamilyDAO.GetAllFamilies(); MainListView.Columns.Clear(); MainListView.Items.Clear(); MainListView.Groups.Clear(); ColumnsWidth = MainListView.Width / 2; MainListView.Columns.Add("Nom", ColumnsWidth); MainListView.Columns.Add("Référence", ColumnsWidth); foreach (Family F in Families) { string[] FamilyToAdd = new string[2]; ListViewItem FamilyItem; // ajoute les items a la ListView FamilyToAdd[0] = F.NameFamily; FamilyToAdd[1] = F.RefFamily.ToString(); FamilyItem = new ListViewItem(FamilyToAdd); MainListView.Items.Add(FamilyItem); } break; case "Sous familles": SubFamily[] SubFamilies = SubFamilyDAO.GetAllSubFamilies(); MainListView.Columns.Clear(); MainListView.Items.Clear(); MainListView.Groups.Clear(); ColumnsWidth = MainListView.Width / 3; MainListView.Columns.Add("Nom", ColumnsWidth); MainListView.Columns.Add("Référence", ColumnsWidth); MainListView.Columns.Add("Familles", ColumnsWidth); foreach (SubFamily SF in SubFamilies) { string[] SubFamilyToAdd = new string[3]; ListViewItem SubFamilyItem; // ajoute les items a la ListView SubFamilyToAdd[0] = SF.NameSubFamily; SubFamilyToAdd[1] = SF.RefSubFamily.ToString(); SubFamilyToAdd[2] = SF.RefFamily.ToString(); SubFamilyItem = new ListViewItem(SubFamilyToAdd); MainListView.Items.Add(SubFamilyItem); } break; default: break; } }
/// <summary> /// Lance l'import au clic sur "importer et Ecraser" /// </summary> /// <param name="Sender"></param> /// <param name="Event"></param> private void OverwriteButton_Click(object Sender, EventArgs Event) { //Nombre de ligne du fichier (-1 pour éviter la colonne titre) int NbLine = -1; // test les erreurs avec le fichier CSV try { // compte le nombre de ligne du fichier using (var reader = new StreamReader(@CSVNameTextBox.Text)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(';'); NbLine++; } } // met le maximun de la progress bar au nombre de ligne ImportProgressBar.Maximum = NbLine; using (var reader = new StreamReader(@CSVNameTextBox.Text)) { // nettoie la bd try { using (var con = new SQLiteConnection("Data Source = Bacchus.SQLite ;Version=3;New=False;Compress=True;")) { // supprime le contenu de la table articles using (var Command = new SQLiteCommand("DELETE FROM Articles;")) { //execute query Command.Connection = con; Command.Connection.Open(); Command.ExecuteNonQuery(); con.Close(); } // supprime le contenu de la table marques using (var Command = new SQLiteCommand("DELETE FROM Marques;")) { //execute query Command.Connection = con; Command.Connection.Open(); Command.ExecuteNonQuery(); con.Close(); } // supprime le contenu de la table sousfamilles using (var Command = new SQLiteCommand("DELETE FROM SousFamilles;")) { //execute query Command.Connection = con; Command.Connection.Open(); Command.ExecuteNonQuery(); con.Close(); } // supprime le contenu de la table familles using (var Command = new SQLiteCommand("DELETE FROM Familles;")) { //execute query Command.Connection = con; Command.Connection.Open(); Command.ExecuteNonQuery(); con.Close(); } } } catch (Exception ExceptionCaught) { MessageBox.Show("Error while clearing db : " + ExceptionCaught.Message, ExceptionCaught.GetType().ToString()); } bool TitleLine = true; while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(';'); // verification qui permet d'éviter la ligne titre if (TitleLine == false) { // verifie si la reference est valide if (ArticleDAO.VerifArticleRef(values[1]) == true) { // cherche la quantité, si elle existe, dans la premiere colonne var firstSpaceIndex = values[0].IndexOf(" "); var Quantity = values[0].Substring(0, firstSpaceIndex); // la quantite si elle existe var Description = values[0].Substring(firstSpaceIndex + 1); // la description si il y a un quantite int IntQuantity; if (int.TryParse(Quantity, out IntQuantity) == false) { Description = values[0]; IntQuantity = 1; } // si la famille existe la trouve sinon la creer Family FamilyToAdd = FamilyDAO.GetFamilyByName(values[3]); if (FamilyToAdd == null) { FamilyToAdd = new Family(values[3]); FamilyDAO.AddFamily(FamilyToAdd); } // si la sous famille existe la trouve sinon la creer SubFamily SubFamilyToAdd = SubFamilyDAO.GetSubFamilyByName(values[4], FamilyToAdd); if (SubFamilyToAdd == null) { SubFamilyToAdd = new SubFamily(values[4], FamilyToAdd); SubFamilyDAO.AddSubFamily(SubFamilyToAdd); } // si la marque existe la trouve sinon la creer Brand BrandToAdd = BrandDAO.GetBrandByName(values[2]); if (BrandToAdd == null) { BrandToAdd = new Brand(values[2]); BrandDAO.AddBrand(BrandToAdd); } // vérifie si le prix est bien un double float FloatPrice; if (float.TryParse(values[5], out FloatPrice)) { // si la reference de l'article existe la trouve sinon creer l'article Article ArticleToAdd = ArticleDAO.GetArticleById(values[1]); if (ArticleToAdd == null) { //créer l'article et le rajoute à la bd ArticleToAdd = new Article(values[1], Description, SubFamilyToAdd, BrandToAdd, FloatPrice, IntQuantity); ArticleDAO.AddArticle(ArticleToAdd); // une ligne rajoutée, on avance donc d'une étape dans la progress bar ImportProgressBar.PerformStep(); } } } } else { TitleLine = false; } } this.Close(); } } catch (Exception ExceptionCaught) { MessageBox.Show(ExceptionCaught.Message, "Erreur : " + ExceptionCaught.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error); } }