f_vehi.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Xml.Linq;
  12. namespace Fuel01
  13. {
  14. public partial class f_vehi : Form
  15. {
  16. DataSet1.tb_vehiDataTable tb_vehi = new DataSet1.tb_vehiDataTable();
  17. private bool hasChanged = false;
  18. List<Produit> lproduit = new List<Produit>();
  19. List<Produit> lcarbu = new List<Produit>();
  20. List<Famille> lfamille = new List<Famille>();
  21. public f_vehi()
  22. {
  23. InitializeComponent();
  24. dg_vehi.AutoGenerateColumns = false;
  25. dg_vehi.DataSource = tbvehiculeBindingSource;
  26. }
  27. private void vehi_Load(object sender, EventArgs e)
  28. {
  29. Program.subfolder = Program.folder + @"\" + Program.key_ope;
  30. if (!Directory.Exists(Program.subfolder))
  31. Directory.CreateDirectory(Program.subfolder);
  32. else
  33. if ( File.Exists(Program.subfolder + @"\vehicule.json"))
  34. tb_vehi = DbUtil.LoadFromJson<DataSet1.tb_vehiDataTable>(Program.subfolder + @"\vehicule.json", tb_vehi);
  35. tb_vehi.AcceptChanges();
  36. tbvehiculeBindingSource.DataSource = tb_vehi;
  37. turn_txt(false);
  38. turn_bt(false);
  39. if (Program.key_ope != null)
  40. this.Text = "Véhicules " + Program.nom_ope;
  41. if (File.Exists(Program.folder + @"\param\famille.xml"))
  42. init_list_famille();
  43. if (File.Exists(Program.folder + @"\param\produit.xml"))
  44. init_list_produit();
  45. }
  46. private void init_list_famille()
  47. {
  48. XDocument xRoot = XDocument.Load(Program.folder + @"\param\famille.xml");
  49. var data = from item in xRoot.Descendants("tb_famille")
  50. orderby item.Element("key_fam").Value
  51. select new Famille()
  52. {
  53. key_fam = item.Element("key_fam").Value
  54. };
  55. lfamille = data.ToList();
  56. combo_famille.Items.Clear();
  57. combo_famille.DisplayMember = "key_fam";
  58. foreach (Famille f in lfamille)
  59. combo_famille.Items.Add(f);
  60. }
  61. private void init_list_produit()
  62. {
  63. XDocument xRoot = XDocument.Load(Program.folder + @"\param\produit.xml");
  64. var data = from item in xRoot.Descendants("tb_prod")
  65. orderby item.Element("type_prod").Value, item.Element("lib_prod").Value
  66. select new Produit()
  67. {
  68. key_prod = item.Element("key_prod").Value,
  69. lib_prod = item.Element("lib_prod").Value,
  70. type_prod = item.Element("type_prod").Value
  71. };
  72. lproduit = data.ToList();
  73. lcarbu=lproduit.Where(p => p.type_prod == "Carburant").ToList();
  74. combo_energy.Items.Clear();
  75. combo_energy.DisplayMember = "key_prod";
  76. foreach (Produit p in lcarbu)
  77. combo_energy.Items.Add(p);
  78. }
  79. #region affichage
  80. private void turn_txt(bool act)
  81. {
  82. text_immat_vehi.Enabled = act;
  83. text_num_vehi.Enabled = act;
  84. text_cmpnum_vehi.Enabled = act;
  85. combo_categ_vehi.Enabled = act;
  86. text_type_vehi.Enabled = act;
  87. text_kmdep_vehi.Enabled = act;
  88. text_kmarr_vehi.Enabled = act;
  89. text_cond_vehi.Enabled = act;
  90. combo_energy.Enabled = act;
  91. combo_famille.Enabled = act;
  92. }
  93. private void turn_bt(bool act)
  94. {
  95. Bt_Abort.Enabled = act;
  96. bt_Valid.Enabled = act;
  97. bt_Mod.Enabled = !act;
  98. bt_Aj.Enabled = !act;
  99. bt_Sup.Enabled = !act;
  100. dg_vehi.Enabled = !act;
  101. }
  102. #endregion
  103. #region Boutons
  104. private void bt_Mod_Click(object sender, EventArgs e)
  105. {
  106. maj_sort_vehi();
  107. turn_bt(true);
  108. turn_txt(true);
  109. }
  110. private void bt_Aj_Click(object sender, EventArgs e)
  111. {
  112. turn_txt(true);
  113. turn_bt(true);
  114. string key = findNewkey();
  115. DataSet1.tb_vehiRow myNewRow=tb_vehi.Newtb_vehiRow();
  116. myNewRow["key_vehi"] = key;
  117. myNewRow["ope_vehi"] = Program.key_ope;
  118. tb_vehi.Addtb_vehiRow(myNewRow);
  119. tbvehiculeBindingSource.MoveFirst();
  120. DataRowView myRow = tbvehiculeBindingSource.Current as DataRowView;
  121. while (myRow.Row["key_vehi"].ToString() != key)
  122. {
  123. tbvehiculeBindingSource.MoveNext();
  124. myRow = tbvehiculeBindingSource.Current as DataRowView;
  125. }
  126. }
  127. private void bt_Sup_Click(object sender, EventArgs e)
  128. {
  129. DataRowView myRow = tbvehiculeBindingSource.Current as DataRowView;
  130. if (myRow == null) return;
  131. string msg = string.Format("Vehicule {0}\nEtes-vous certain de vouloir supprimer \n{1}", myRow.Row["num_vehi"].ToString(), myRow.Row["immat_vehi"].ToString());
  132. string caption = "Suppression définitive";
  133. if (MessageBox.Show(msg, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
  134. {
  135. DataSet1.tb_vehiRow vehiRow = tb_vehi.FindBykey_vehi(myRow.Row["key_vehi"].ToString());
  136. if (vehiRow != null)
  137. tb_vehi.Removetb_vehiRow(vehiRow);
  138. hasChanged = true;
  139. }
  140. }
  141. private void bt_Valid_Click(object sender, EventArgs e)
  142. {
  143. maj_sort_vehi();
  144. tb_vehi.AcceptChanges();
  145. turn_txt(false);
  146. turn_bt(false);
  147. hasChanged = true;
  148. }
  149. private void Bt_Abort_Click(object sender, EventArgs e)
  150. {
  151. tb_vehi.RejectChanges();
  152. turn_txt(false);
  153. turn_bt(false);
  154. }
  155. #endregion
  156. #region fonctions
  157. private string findNewkey()
  158. {
  159. Random rand1 = new Random();
  160. string key = rand1.Next(99999).ToString();
  161. while (tb_vehi.FindBykey_vehi(key) != null)
  162. {
  163. MessageBox.Show(key);
  164. key = rand1.Next(99999).ToString();
  165. }
  166. return key;
  167. }
  168. #endregion
  169. private void f_vehi_FormClosing(object sender, FormClosingEventArgs e)
  170. {
  171. if (hasChanged) saveData();
  172. DataTable tb_vehi_change = tb_vehi.GetChanges();
  173. tb_vehi_change = null;
  174. if (tb_vehi_change != null) MessageBox.Show("Veuillez Valider ou Abandonner", "Modifications en cours");
  175. e.Cancel = (tb_vehi_change != null);
  176. }
  177. private void saveData()
  178. {
  179. if (File.Exists(Program.subfolder + @"\vehicule.json"))
  180. File.Copy(Program.subfolder + @"\vehicule.json", Program.subfolder + @"\vehicule.back.json",true);
  181. DbUtil.SaveToJson<DataSet1.tb_vehiDataTable>(Program.subfolder + @"\vehicule.json", tb_vehi);
  182. }
  183. private void combo_energy_TextUpdate(object sender, EventArgs e)
  184. {
  185. Produit cur_Prod = lproduit.Find(p => p.key_prod == combo_energy.Text);
  186. if (cur_Prod != null)
  187. StatusLabel1.Text = cur_Prod.lib_prod;
  188. else
  189. StatusLabel1.Text = "...";
  190. }
  191. private void maj_sort_vehi()
  192. {
  193. DataRowView myRow = tbvehiculeBindingSource.Current as DataRowView;
  194. if ( myRow!=null)
  195. myRow["sort1_vehi"]= string.Format("{0}{1:00000}{2}", myRow["categ_vehi"], myRow["num_vehi"], myRow["cmpnum_vehi"]);
  196. }
  197. private void text_num_vehi_Leave(object sender, EventArgs e)
  198. {
  199. maj_sort_vehi();
  200. }
  201. private void label2_Click(object sender, EventArgs e)
  202. {
  203. }
  204. private void text_immat_vehi_TextChanged(object sender, EventArgs e)
  205. {
  206. }
  207. }
  208. }