/// <summary> /// Find an article with the reference /// </summary> /// <param name="Ref"></param> /// <returns></returns> public Article FindByRef(string Ref) { OpenConnection(); var Result = ExecuteSelect("SELECT * FROM " + TableName + " WHERE " + RefName + " = '" + Ref + "'"); Article Arti; SousFamilleControl SFCont = new SousFamilleControl(); MarqueControl MCont = new MarqueControl(); if (Result.Read()) { Arti = new Article( Result.GetString(0), Result.GetString(1), Result.GetFloat(4), Result.GetInt16(5), MCont.FindByRef(Result.GetInt16(3)), SFCont.FindByRef(Result.GetInt16(2))); } else { Arti = null; } CloseConnection(); return(Arti); }
/// <summary> /// Delete a Famille Row in Database /// </summary> /// <param name="Objet"></param> /// <returns></returns> public override bool Delete(Famille Objet) // TO-DO { // Init SousFamilleControl SCont = new SousFamilleControl(); // SSFamille Database Access HashSet <SousFamille> ListeSousFamilles; if (Objet == null) { return(false); } /* * remove all "SousFamilles" and "Articles" linked to this "Famille" in the database */ // Step 1 : get all "SousFamilles" Linked to this "Famille" ListeSousFamilles = SCont.FindByFamily(Objet); // Step 2 : Delete All foreach (SousFamille SousFamille in ListeSousFamilles) { SCont.Delete(SousFamille); } return(ExecuteUpdate("DELETE FROM " + TableName + " WHERE " + RefName + " = " + Objet.RefFamille)); }
/// <summary> /// Check Article param except Reference, return true if its ok /// </summary> /// <param name="Arti"></param> /// <returns></returns> public bool CheckParam(Article Arti) { if (Arti == null || Arti.SousFamille == null || Arti.PrixHT < 0 || Arti.Quantite < 0 || Arti.Marque == null) { return(false); } MarqueControl MCont = new MarqueControl(); SousFamilleControl SFCont = new SousFamilleControl(); if (MCont.FindByRef(Arti.Marque.RefMarque) == null || SFCont.FindByRef(Arti.SousFamille.RefSousFamille) == null) { return(false); } return(true); }
/// <summary> /// Get all existant article /// </summary> /// <returns></returns> public override HashSet <Article> GetAll() { OpenConnection(); HashSet <Article> Liste = new HashSet <Article>(); var Result = ExecuteSelect("SELECT * FROM " + TableName); SousFamilleControl SFCont = new SousFamilleControl(); MarqueControl MCont = new MarqueControl(); while (Result.Read()) { Article Arti = new Article( Result.GetString(0), Result.GetString(1), Result.GetFloat(4), Result.GetInt16(5), MCont.FindByRef(Result.GetInt16(3)), SFCont.FindByRef(Result.GetInt16(2))); Liste.Add(Arti); } CloseConnection(); return(Liste); }
/// <summary> /// Import the file in the database /// </summary> /// <param name="Path"></param> /// <returns></returns> public static bool ImportFile(String Path, ProgressBar Progress) { try { using (var reader = new StreamReader(Path, Encoding.Default)) { Console.WriteLine("We are importing this file " + Path); ArticleControl ACont = new ArticleControl(); FamilleControl FCont = new FamilleControl(); SousFamilleControl SFCont = new SousFamilleControl(); MarqueControl MCont = new MarqueControl(); Progress.Maximum = reader.ReadLine().Count() - 1;// remove the first line while (!reader.EndOfStream) { Progress.PerformStep(); Model.Marque Mark = new Model.Marque(); Model.Article Artic = new Model.Article(); Model.SousFamille SousFam = new Model.SousFamille(); Model.Famille Fam = new Model.Famille(); /* * Parser * */ var Line = reader.ReadLine(); //Console.WriteLine(line); string[] values = Line.Split(';'); /* * Create a "Marque" in the Database and get the Id */ Mark.Nom = values[MARQUE]; // the "Marque" object is not in the Database if (!MCont.Exist(Mark)) { // Create one MCont.Insert(Mark); } // get it Mark = MCont.GetByName(Mark); /* * Create a "Famille" in the Database and get the ID */ Fam.Nom = values[FAMILLE]; if (!FCont.Exist(Fam)) { FCont.Insert(Fam); } Fam = FCont.GetByName(Fam); /* * Create a "SousFamille" in the Database and get the ID */ SousFam.Nom = values[SOUSFAMILLE]; SousFam.Famille = Fam; if (!SFCont.Exist(SousFam)) { SFCont.Insert(SousFam); } SousFam = SFCont.GetByName(SousFam); /* * Create the "Article" and stock it in the database */ Artic.RefArticle = values[REF]; Artic.PrixHT = float.Parse(values[PRIXHT].Replace('.', ',')); //Console.WriteLine(Artic.PrixHT); Artic.Description = values[DESCRIPTION]; Artic.Marque = Mark; Artic.SousFamille = SousFam; // the Article object need to be update in the Database if (ACont.Exist(Artic)) { //Console.WriteLine("Update"); ACont.Update(Artic); } else { //Console.WriteLine("Insert"); ACont.Insert(Artic); } } Progress.Value = Progress.Maximum; } return(true); } catch (Exception e) { Console.WriteLine(e.StackTrace); return(false); } }