f_station.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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;
  12. using System.Xml.Linq;
  13. using Excel=Microsoft.Office.Interop.Excel;
  14. namespace Fuel01
  15. {
  16. public partial class f_station : Form
  17. {
  18. DataSet1.tb_stationDataTable tb_station = new DataSet1.tb_stationDataTable();
  19. string keysta;
  20. f_presta fpresta;
  21. List<pays> lpays = new List<pays>();
  22. public f_station(DataSet1.tb_stationDataTable tb_station,string keysta)
  23. {
  24. InitializeComponent();
  25. this.tb_station = tb_station;
  26. this.keysta = keysta;
  27. }
  28. private void f_station_Load(object sender, EventArgs e)
  29. {
  30. tb_station.AcceptChanges();
  31. tbstationBindingSource.DataSource = tb_station;
  32. TurnTxt(false);
  33. TurnBt(false);
  34. if (Program.key_ope != null)
  35. this.Text = "Stations " + Program.nom_ope;
  36. if (File.Exists(Program.folder + @"\param\pays.xml"))
  37. InitListPays();
  38. tbstationBindingSource.MoveFirst();
  39. DataRowView myRow = tbstationBindingSource.Current as DataRowView;
  40. if (myRow != null)
  41. while (myRow.Row["key_sta"].ToString() != keysta)
  42. {
  43. int i = tbstationBindingSource.Position;
  44. tbstationBindingSource.MoveNext();
  45. myRow = tbstationBindingSource.Current as DataRowView;
  46. if (i == tbstationBindingSource.Position)
  47. break;
  48. }
  49. }
  50. private void f_station_FormClosing(object sender, FormClosingEventArgs e)
  51. {
  52. if (fpresta != null)
  53. fpresta.Close();
  54. DataSet1.tb_stationDataTable tb_station_tmp = new DataSet1.tb_stationDataTable();
  55. foreach (DataRow dr in tb_station.OrderBy(p => p.date_sta))
  56. tb_station_tmp.ImportRow(dr);
  57. DbUtil.SaveToJson<DataSet1.tb_stationDataTable>(Program.subfolder + @"\station.json", tb_station_tmp);
  58. tb_station_tmp.Dispose();
  59. e.Cancel = false;
  60. }
  61. #region affichage
  62. private void TurnTxt(bool act)
  63. {
  64. text_etp_sta.Enabled = act;
  65. dt_date_sta.Enabled = act;
  66. nn_num_sta.Enabled = act;
  67. text_contact_sta.Enabled = act;
  68. text_nom_sta.Enabled = act;
  69. text_adr1_sta.Enabled = act;
  70. text_adr2_sta.Enabled = act;
  71. combo_pays_sta.Enabled = act;
  72. text_cp_sta.Enabled = act;
  73. text_ville_sta.Enabled = act;
  74. text_tel_sta.Enabled = act;
  75. text_tva_sta.Enabled = act;
  76. text_divers_sta.Enabled = act;
  77. }
  78. private void TurnBt(bool act)
  79. {
  80. Bt_Abort.Enabled = act;
  81. bt_Valid.Enabled = act;
  82. bt_Mod.Enabled = !act;
  83. bt_Aj.Enabled = !act;
  84. bt_Sup.Enabled = !act;
  85. dg_station.Enabled = !act;
  86. }
  87. #endregion
  88. private void InitListPays()
  89. {
  90. XDocument xRoot = XDocument.Load(Program.folder + @"\param\pays.xml");
  91. var data = from item in xRoot.Descendants("tb_pays")
  92. orderby item.Element("order_pays").Value
  93. select new pays()
  94. {
  95. key_pays = item.Element("key_pays").Value,
  96. nom_pays = item.Element("nom_pays").Value,
  97. code_pays = item.Element("code_pays").Value,
  98. concat_pays = item.Element("code_pays").Value + "-" + item.Element("nom_pays").Value
  99. };
  100. lpays = data.ToList();
  101. combo_pays_sta.Items.Clear();
  102. combo_pays_sta.DisplayMember = "key_pays";
  103. foreach (pays p in lpays)
  104. combo_pays_sta.Items.Add(p);
  105. }
  106. #region Boutons
  107. private void bt_Mod_Click(object sender, EventArgs e)
  108. {
  109. TurnBt(true);
  110. TurnTxt(true);
  111. }
  112. private void bt_Aj_Click(object sender, EventArgs e)
  113. {
  114. TurnTxt(true);
  115. TurnBt(true);
  116. string key = findNewkey();
  117. DataSet1.tb_stationRow myNewRow = tb_station.Newtb_stationRow();
  118. myNewRow["key_sta"] = key;
  119. myNewRow["epr_sta"] = Program.key_ope;
  120. tb_station.Addtb_stationRow(myNewRow);
  121. tbstationBindingSource.MoveFirst();
  122. DataRowView myRow = tbstationBindingSource.Current as DataRowView;
  123. while (myRow.Row["key_sta"].ToString() != key)
  124. {
  125. tbstationBindingSource.MoveNext();
  126. myRow = tbstationBindingSource.Current as DataRowView;
  127. }
  128. }
  129. private void bt_Sup_Click(object sender, EventArgs e)
  130. {
  131. DataRowView myRow = tbstationBindingSource.Current as DataRowView;
  132. if (myRow == null) return;
  133. string msg = string.Format("Station {0}\n Etape {1} du {2} \n Etes-vous certain de vouloir la supprimer", myRow.Row["nom_sta"].ToString(), myRow.Row["etp_sta"].ToString(), myRow.Row["date_sta"].ToString());
  134. string caption = "Suppression définitive";
  135. if (MessageBox.Show(msg, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
  136. {
  137. DataSet1.tb_stationRow stationRow = tb_station.FindBykey_sta(myRow.Row["key_sta"].ToString());
  138. if (stationRow != null)
  139. tb_station.Removetb_stationRow(stationRow);
  140. }
  141. }
  142. private void bt_Valid_Click(object sender, EventArgs e)
  143. {
  144. tb_station.AcceptChanges();
  145. TurnTxt(false);
  146. TurnBt(false);
  147. }
  148. private void Bt_Abort_Click(object sender, EventArgs e)
  149. {
  150. tb_station.RejectChanges();
  151. TurnTxt(false);
  152. TurnBt(false);
  153. }
  154. #endregion
  155. #region fonctions
  156. private string findNewkey()
  157. {
  158. Random rand1 = new Random();
  159. string key = rand1.Next(99999).ToString();
  160. while (tb_station.FindBykey_sta(key) != null)
  161. {
  162. MessageBox.Show(key);
  163. key = rand1.Next(99999).ToString();
  164. }
  165. return key;
  166. }
  167. #endregion
  168. private void combo_pays_sta_SelectedIndexChanged(object sender, EventArgs e)
  169. {
  170. pays cur_Pays=lpays.Find(p => p.key_pays == combo_pays_sta.Text);
  171. if ( cur_Pays!=null)
  172. StatusLabel1.Text = cur_Pays.nom_pays;
  173. else
  174. StatusLabel1.Text = "...";
  175. }
  176. private void bt_Presta_Click(object sender, EventArgs e)
  177. {
  178. if ( fpresta==null)
  179. fpresta = new f_presta(text_key_sta);
  180. try
  181. {
  182. fpresta.Show();
  183. }
  184. catch (System.ObjectDisposedException )
  185. {
  186. fpresta = new f_presta(text_key_sta);
  187. fpresta.Show();
  188. }
  189. }
  190. private void label11_Click(object sender, EventArgs e)
  191. {
  192. }
  193. private void t_facture_Click(object sender, EventArgs e)
  194. {
  195. }
  196. //if (!File.Exists(Program.subfolder + @"\Facture_Carbu.xlsx"))
  197. // return;
  198. //string fileName=Program.subfolder+@"\Facture_"+text_key_sta.Text+".xlsx";
  199. //File.Copy(Program.subfolder + @"\Facture_Carbu.xlsx", fileName,true);
  200. //Excel.Application xlApp;
  201. //Excel.Workbook xlWorkBook;
  202. //Excel.Worksheet xlWorkSheet;
  203. //object misValue = System.Reflection.Missing.Value;
  204. //xlApp = new Excel.Application();
  205. //xlWorkBook = xlApp.Workbooks.Add(misValue);
  206. //try
  207. //{
  208. // xlApp = new Excel.Application();
  209. // xlWorkBook = xlApp.Workbooks.Open(fileName, misValue);
  210. // xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
  211. //}
  212. //catch (Exception ex)
  213. //{
  214. // MessageBox.Show("Vérifier que le fichier n'est pas ouvert et est un fichier Excel correct", "Ouverture de " + fileName + " impossible", MessageBoxButtons.OK, MessageBoxIcon.Error);
  215. // return;
  216. //}
  217. //xlWorkSheet.Cells[2, 2].Value = text_nom_sta.Text;
  218. //xlWorkSheet.Cells[3, 2].Value = text_adr1_sta.Text;
  219. //xlWorkSheet.Cells[4, 2].Value = text_adr2_sta.Text;
  220. //xlWorkSheet.Cells[5, 2].Value = text_cp_sta.Text+"-"+text_ville_sta.Text;
  221. //xlWorkSheet.Cells[6, 2].Value = combo_pays_sta.Text;
  222. //xlWorkSheet.Cells[8, 2].Value = text_tel_sta.Text;
  223. //xlWorkSheet.Cells[3, 8].Value = DateTime.Now.ToString("Le : dd/MM/yyyy");
  224. //xlWorkSheet.Cells[14, 2].Value = "Ref : Ravitaillement "+Program.nom_ope+" du "+dt_date_sta.Value.ToString("dd/MM/yy");
  225. //xlWorkBook.Save();
  226. //xlApp.Quit();
  227. }
  228. }