/// <summary> /// Crée un devis /// </summary> /// <param name="vehicule">Objet vehicule WCF</param> /// <param name="client"> Objet Client WCF</param> /// <returns></returns> public string CreateDevis(Vehicule vehicule, Client client) { //calcul du prix int prix = calculerpix(vehicule.Options); string idDevis = ""; using (MySqlConnection connection = new MySqlConnection(this.connectionString)) { string idcli = ""; connection.Open(); MySqlCommand cmd = new MySqlCommand(@"select id_client from tclient where mail='" + client.Mail + "'", connection); MySqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { idcli = dr[0].ToString(); } else { idcli = CreerClient(client); } dr.Close(); cmd.CommandText = @"call creationDevis(" + idcli + "," + prix + ",'" + DateTime.Now.ToString("u") + "')"; cmd.ExecuteNonQuery(); cmd.CommandText = "SELECT LAST_INSERT_ID() FROM tdevis"; dr = cmd.ExecuteReader(); if (dr.Read()) { idDevis = dr[0].ToString(); } } return(idDevis); }
public bool creerModelAvecFacture(Vehicule vehicule, Client client) { try { createFacture(CreerModel(vehicule, client)); return(true); } catch (Exception) { return(false); } }
/// <summary> /// Crée un vehcule dans la bdd et passe commande à l'usine /// </summary> /// <param name="vehicule">objet vehicule du WCF</param> /// <param name="client">objet client WCF</param> /// <returns></returns> public string CreerModel(Vehicule vehicule, Client client) { string idMarque = "", idModel = "", idcategorie = "", idClient = "", idDevis = ""; int version = -1; string[] idOption = new string[vehicule.Options.Length]; int i = 0; foreach (var item in vehicule.Options) { idOption[i] = AjouterOption(item); i++; } using (MySqlConnection conn = new MySqlConnection(this.connectionString)) { conn.Open(); MySqlCommand command = conn.CreateCommand(); MySqlTransaction transaction; transaction = conn.BeginTransaction(); command.Connection = conn; command.Transaction = transaction; MySqlDataReader dr; idClient = CreerClient(client); idDevis = CreateDevis(vehicule, client); try { command.CommandText = @"select ID_MARQUE from tmarque where NOM_MARQUE='" + vehicule.Marque + "';"; dr = command.ExecuteReader(); if (dr.Read()) { idMarque = dr[0].ToString(); } else { dr.Close(); command.CommandText = "call InsertMarque('" + vehicule.Marque + "')"; command.ExecuteNonQuery(); command.CommandText = "SELECT LAST_INSERT_ID() FROM tmarque"; dr = command.ExecuteReader(); if (dr.Read()) { idMarque = dr[0].ToString(); } } dr.Close(); command.CommandText = @"select ID_CATEGORIE from tcategorie where NOM_CATEGORIE='" + vehicule.Categorie + "'"; dr = command.ExecuteReader(); if (dr.Read()) { idcategorie = dr[0].ToString(); } else { dr.Close(); command.CommandText = @"call InsertCategorie('" + vehicule.Categorie + "')"; command.ExecuteNonQuery(); command.CommandText = "select LAST_INSERT_ID() from tcategorie"; dr = command.ExecuteReader(); if (dr.Read()) { idcategorie = dr[0].ToString(); } } dr.Close(); command.CommandText = @"select id_model from tmodel where nom_model='" + vehicule.Model + "';"; dr = command.ExecuteReader(); if (dr.Read()) { idModel = dr[0].ToString(); } else { dr.Close(); command.CommandText = "call insertModel(" + idMarque + ",'" + vehicule.Model + "'," + idcategorie + ")"; command.ExecuteNonQuery(); version = 0; command.CommandText = "select LAST_INSERT_ID() from tmodel"; dr = command.ExecuteReader(); if (dr.Read()) { idModel = dr[0].ToString(); } } dr.Close(); command.CommandText = @"select version from toption_has_tmodel where id_model=" + idModel + " order by version DESC"; dr = command.ExecuteReader(); if (dr.Read()) { version = Convert.ToInt32(dr[0]) + 1; } dr.Close(); //ajoue des options for (int j = 0; j < idOption.Length; j++) { if (idOption[j] != "") { command.CommandText = "call choisirOption(" + idOption[j] + "," + idModel + "," + version + ")"; } else { transaction.Rollback(); break; } command.ExecuteNonQuery(); } //recuperation de l'usine string idusine = ""; command.CommandText = "select id_client from tclient where nom_client='usine " + vehicule.Marque + "'"; dr = command.ExecuteReader(); if (dr.Read()) { idusine = dr[0].ToString(); } dr.Close(); //creation vehicule command.CommandText = "call CreationVehicule(" + idDevis + "," + idusine + "," + idModel + ")"; command.ExecuteNonQuery(); transaction.Commit(); return(idDevis); } catch (Exception e) { Console.WriteLine(e.Message); transaction.Rollback(); return(null); } finally { conn.Close(); } } }