f_saisie.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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_saisie : Form
  15. {
  16. DataSet1.tb_stationDataTable tb_station = new DataSet1.tb_stationDataTable();
  17. DataSet1.tb_prestaDataTable tb_presta = new DataSet1.tb_prestaDataTable();
  18. DataSet1.tb_venteDataTable tb_vente = new DataSet1.tb_venteDataTable();
  19. DataSet1.tb_venteDataTable tb_ventetmp = new DataSet1.tb_venteDataTable();
  20. DataSet1.tb_vehiDataTable tb_vehi = new DataSet1.tb_vehiDataTable();
  21. List<DataSet1.tb_prestaRow> Lcarb = new List<DataSet1.tb_prestaRow>(); //Liste carbu hors diesel
  22. List<DataSet1.tb_prestaRow> Ldies = new List<DataSet1.tb_prestaRow>(); //Liste diesel
  23. List<DataSet1.tb_prestaRow> Lprod = new List<DataSet1.tb_prestaRow>(); //Liste des prestations
  24. List<DataSet1.tb_prestaRow> Lmycarb = new List<DataSet1.tb_prestaRow>(); //Liste tous les carbuants
  25. DataSet1.tb_stationRow myStation = null;
  26. DataSet1.tb_vehiRow myVehi = null;
  27. bool hasChanged = false;
  28. bool isDiesel = false;
  29. int indiceCarb = -1;
  30. string id_vehi="";
  31. public f_saisie()
  32. {
  33. InitializeComponent();
  34. }
  35. private void f_saisie_Load(object sender, EventArgs e)
  36. {
  37. Program.subfolder = Program.folder + @"\" + Program.key_ope;
  38. if (File.Exists(Program.subfolder + @"\vehicule.xml"))
  39. tb_vehi.ReadXml(Program.subfolder + @"\vehicule.xml");
  40. tbvehiBindingSource.DataSource = tb_vehi;
  41. if (File.Exists(Program.subfolder + @"\station.xml"))
  42. tb_station.ReadXml(Program.subfolder + @"\station.xml");
  43. tbstationBindingSource.DataSource = tb_station;
  44. myStation = tb_station.FindBykey_sta(Program.key_sta);
  45. DataRowView rowView = null;
  46. if (myStation != null)
  47. {
  48. tbstationBindingSource.MoveFirst();
  49. while (true)
  50. {
  51. rowView = tbstationBindingSource.Current as DataRowView;
  52. if (myStation != rowView.Row)
  53. if (tbstationBindingSource.Position == tbstationBindingSource.Count)
  54. break;
  55. else
  56. tbstationBindingSource.MoveNext();
  57. else
  58. break;
  59. }
  60. }
  61. if (File.Exists(Program.subfolder + @"\presta_base.xml"))
  62. tb_presta.ReadXml(Program.subfolder + @"\presta_base.xml");
  63. //tb_presta.AcceptChanges();
  64. tbprestaBindingSource.DataSource = tb_presta;
  65. if (File.Exists(Program.subfolder + @"\vente.xml"))
  66. tb_vente.ReadXml(Program.subfolder + @"\vente.xml");
  67. tb_vente.AcceptChanges();
  68. tbventeBindingSource.DataSource = tb_vente;
  69. //id_sta = key_sta.Text;
  70. tbprestaBindingSource.Filter = " sta_pr='" + Program.key_sta + "'";
  71. tbventeBindingSource.Filter = " station_vt='" + Program.key_sta + "'";
  72. tbprestaBindingSource.Sort = "sta_pr,kind_pr,prod_pr";
  73. tbstationBindingSource_PositionChanged(null, null);
  74. tbventetmpbindingSource.DataSource = tb_ventetmp;
  75. turnBlock(false);
  76. }
  77. private void tbstationBindingSource_PositionChanged(object sender, EventArgs e)
  78. {
  79. Lprod.Clear();
  80. Lcarb.Clear();
  81. //id_sta = key_sta.Text;
  82. tbprestaBindingSource.Filter = " sta_pr='" + Program.key_sta + "'";
  83. tbventeBindingSource.Filter = " station_vt='" + Program.key_sta + "'";
  84. tbprestaBindingSource.MoveFirst();
  85. int old_pos = -1;
  86. while (tbprestaBindingSource.Position != -1 && tbprestaBindingSource.Position < tbprestaBindingSource.Count && old_pos < tbprestaBindingSource.Position)
  87. {
  88. dg_presta.Refresh();
  89. old_pos = tbprestaBindingSource.Position;
  90. DataSet1.tb_prestaRow mydata = null;
  91. DataRowView myrow = tbprestaBindingSource.Current as DataRowView;
  92. if (myrow != null)
  93. {
  94. mydata = myrow.Row as DataSet1.tb_prestaRow;
  95. if (mydata["kind_pr"].ToString() == "Carburant")
  96. {
  97. if (mydata["prod_pr"].ToString().ToUpper().Contains("DIESEL"))
  98. Ldies.Add(mydata);
  99. else
  100. Lcarb.Add(mydata);
  101. }
  102. else
  103. Lprod.Add(mydata);
  104. }
  105. tbprestaBindingSource.MoveNext();
  106. }
  107. aff_zero();
  108. }
  109. private void aff_carb(string curcarb)
  110. {
  111. rdCarb1.Enabled = rdCarb2.Enabled = rdCarb3.Enabled = rdCarb4.Enabled = rdCarb5.Enabled = rdCarb6.Enabled = false;
  112. rdCarb1.Text = rdCarb2.Text = rdCarb3.Text = rdCarb4.Text = rdCarb5.Text = rdCarb6.Text = "";
  113. nuPxCarb.Value = 0;
  114. if (isDiesel)
  115. Lmycarb = Ldies;
  116. else
  117. Lmycarb = Lcarb;
  118. if (Lmycarb.Count >= 1)
  119. {
  120. rdCarb1.Enabled = true;
  121. rdCarb1.Text = Lmycarb[0].prod_pr;
  122. rdCarb1.Checked = (curcarb == rdCarb1.Text);
  123. if ( rdCarb1.Checked ) rdCarb_CheckedChanged(rdCarb1, null);
  124. }
  125. if (Lmycarb.Count >= 2)
  126. {
  127. rdCarb2.Enabled = true;
  128. rdCarb2.Text = Lmycarb[1].prod_pr;
  129. rdCarb2.Checked = (curcarb == rdCarb2.Text);
  130. if (rdCarb2.Checked) rdCarb_CheckedChanged(rdCarb2, null);
  131. }
  132. if (Lmycarb.Count >= 3)
  133. {
  134. rdCarb3.Enabled = true;
  135. rdCarb3.Text = Lmycarb[2].prod_pr;
  136. rdCarb3.Checked = (curcarb == rdCarb3.Text);
  137. if (rdCarb3.Checked) rdCarb_CheckedChanged(rdCarb3, null);
  138. }
  139. if (Lmycarb.Count >= 4)
  140. {
  141. rdCarb4.Enabled = true;
  142. rdCarb4.Text = Lmycarb[3].prod_pr;
  143. rdCarb4.Checked = (curcarb == rdCarb4.Text);
  144. if (rdCarb4.Checked) rdCarb_CheckedChanged(rdCarb4, null);
  145. }
  146. if (Lmycarb.Count >=5 )
  147. {
  148. rdCarb5.Enabled = true;
  149. rdCarb5.Text = Lmycarb[4].prod_pr;
  150. rdCarb5.Checked = (curcarb == rdCarb5.Text);
  151. if (rdCarb5.Checked) rdCarb_CheckedChanged(rdCarb5, null);
  152. }
  153. if (Lmycarb.Count >= 6)
  154. {
  155. rdCarb6.Enabled = true;
  156. rdCarb6.Text = Lmycarb[5].prod_pr;
  157. rdCarb6.Checked = (curcarb == rdCarb6.Text);
  158. if (rdCarb6.Checked) rdCarb_CheckedChanged(rdCarb6, null);
  159. }
  160. //if (Lcarb.Count > 0)
  161. //{
  162. // rdCarb1.Checked = true;
  163. // rdCarb_CheckedChanged(rdCarb1, null);
  164. //}
  165. }
  166. private void aff_prod()
  167. {
  168. if (Lprod.Count >= 1)
  169. {
  170. chkPr1.Enabled = nuPr1.Enabled = nuQtt1.Enabled = true;
  171. chkPr1.Text = Lprod[0].prod_pr;
  172. nuPr1.Value = Lprod[0].ttc_pr;
  173. }
  174. if (Lprod.Count >= 2)
  175. {
  176. chkPr2.Enabled = nuPr2.Enabled = nuQtt2.Enabled = true;
  177. chkPr2.Text = Lprod[1].prod_pr;
  178. nuPr2.Value = Lprod[1].ttc_pr;
  179. }
  180. if (Lprod.Count >= 3)
  181. {
  182. chkPr3.Enabled = nuPr3.Enabled = nuQtt3.Enabled = true;
  183. chkPr3.Text = Lprod[2].prod_pr;
  184. nuPr3.Value = Lprod[2].ttc_pr;
  185. }
  186. }
  187. private void nu_ValueChanged(object sender, EventArgs e)
  188. {
  189. string from = ((NumericUpDown)sender).Name;
  190. switch (from)
  191. {
  192. case "nuPxCarb":
  193. case "nuVolCarb":
  194. txtMntCarb.Text = string.Format("{0:0.00}", nuPxCarb.Value * nuVolCarb.Value);
  195. break;
  196. case "nuPr1":
  197. case "nuQtt1":
  198. txtTot1.Text = string.Format("{0:0.00}", nuPr1.Value * nuQtt1.Value);
  199. break;
  200. case "nuPr2":
  201. case "nuQtt2":
  202. txtTot2.Text = string.Format("{0:0.00}", nuPr2.Value * nuQtt2.Value);
  203. break;
  204. case "nuPr3":
  205. case "nuQtt3":
  206. txtTot3.Text = string.Format("{0:0.00}", nuPr3.Value * nuQtt3.Value);
  207. break;
  208. case "nuPrFree":
  209. case "nuQttFree":
  210. txtTotFree.Text = string.Format("{0:0.00}", nuPrFree.Value * nuQttFree.Value);
  211. break;
  212. }
  213. //txtTotGal.Text = string.Format("{0:0.00}", nuPxCarb.Value * nuVolCarb.Value + nuPr1.Value * nuQtt1.Value + nuPr2.Value * nuQtt2.Value + nuPr3.Value * nuQtt3.Value + nuPrFree.Value * nuQttFree.Value);
  214. }
  215. private void rdCarb_CheckedChanged(object sender, EventArgs e)
  216. {
  217. if (((RadioButton)sender).Checked == true)
  218. {
  219. //on récupère le nom pour l'indice du tableau
  220. indiceCarb = Convert.ToInt16(((RadioButton)sender).Name.Substring(6, 1))-1;
  221. if (indiceCarb < Lmycarb.Count && indiceCarb >= 0)
  222. nuPxCarb.Value = Lmycarb[indiceCarb].ttc_pr;
  223. else
  224. MessageBox.Show("Carburant non disponible pour ce véhicule!");
  225. }
  226. }
  227. private void bt_abort_Click(object sender, EventArgs e)
  228. {
  229. }
  230. private void bt_valid_Click(object sender, EventArgs e)
  231. {
  232. if (nuVolCarb.Value > 0) ajpresta( Lmycarb[indiceCarb],null, nuPxCarb.Value, nuVolCarb.Value, txtMntCarb.Text);
  233. if (chkPr1.Checked) ajpresta(Lprod[0], null, nuPr1.Value, nuQtt1.Value, txtTot1.Text);
  234. if (chkPr2.Checked) ajpresta(Lprod[1], null, nuPr2.Value, nuQtt2.Value, txtTot2.Text);
  235. if (chkPr3.Checked) ajpresta(Lprod[2], null, nuPr3.Value, nuQtt3.Value, txtTot3.Text);
  236. if (chkPrFree.Checked)ajpresta( null, txtPrFree.Text, nuPrFree.Value, nuQttFree.Value, txtTotFree.Text);
  237. f_ticket ticket = new f_ticket(myStation, myVehi, tb_ventetmp);
  238. if (ticket.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  239. {
  240. foreach (DataSet1.tb_venteRow myVt in tb_ventetmp)
  241. {
  242. tb_vente.Addtb_venteRow(myVt.epreuve_vt, myVt.station_vt, myVt.vehi_vt, myVt.prod_vt, myVt.date_vt, myVt.qtt_vt, myVt.pu_vt, myVt.tva_vt,myVt.ht_vt, myVt.ttc_vt);
  243. }
  244. hasChanged = true;
  245. bt_save.Enabled = hasChanged;
  246. }
  247. tb_ventetmp.Clear();
  248. aff_zero();
  249. }
  250. private void ajpresta(DataSet1.tb_prestaRow prRow, String lib, decimal pu, decimal qtt, string total_str)
  251. {
  252. //récup du libellé si presta connue
  253. decimal tva = 0;
  254. decimal totht = 0;
  255. decimal total = Convert.ToDecimal( total_str);
  256. if (prRow != null)
  257. {
  258. lib = prRow.prod_pr;
  259. tva = prRow.tva_pr;
  260. totht = Math.Round(total / (1 - tva / 100),2);
  261. }
  262. DateTime dt=DateTime.Now;
  263. tb_ventetmp.Addtb_venteRow(Program.key_ope,Program.key_sta,id_vehi,lib,dt,qtt,pu,tva,totht,total);
  264. //DataSet1.tb_venteRow newVente = DataSet1.tb_venteRow { date_vt = dt, epreuve_vt = Program.key_ope, station_vt = Program.key_sta, vehi_vt = id_vehi, prod_vt = lib, ttc_vt = Convert.ToInt32(total), qtt_vt = qtt, pu_vt = pu };
  265. }
  266. private void bt_search_Click(object sender, EventArgs e)
  267. {
  268. UInt32 num;
  269. if (UInt32.TryParse(text_no.Text, out num))
  270. {
  271. myVehi = tb_vehi.FirstOrDefault(v => v.num_vehi == num && v.categ_vehi == (rdAuto.Checked ? "A" : "M"));
  272. if (myVehi != null)
  273. {
  274. lbl_pilote.Text = myVehi.cond_vehi ;
  275. isDiesel = myVehi.carbu_vehi.ToUpper().Contains("DIESEL");
  276. aff_carb(myVehi.carbu_vehi);
  277. aff_prod();
  278. id_vehi = myVehi.sort1_vehi;
  279. }
  280. else
  281. {
  282. aff_zero();
  283. text_no.Text = num.ToString();
  284. text_no.SelectionStart = 0;
  285. text_no.SelectionLength = text_no.Text.Length;
  286. }
  287. }
  288. turnBlock(myVehi != null);
  289. }
  290. private void turnBlock(bool status)
  291. {
  292. grpCarb.Enabled = status;
  293. grpServ.Enabled = status;
  294. bt_abort.Enabled = status;
  295. bt_valid.Enabled = status;
  296. }
  297. private void aff_zero()
  298. {
  299. chkPr1.Enabled = chkPr2.Enabled = chkPr3.Enabled = false;
  300. chkPr1.Checked = chkPr2.Checked = chkPr3.Checked = chkPrFree.Checked = false;
  301. nuPr1.Enabled = nuPr2.Enabled = nuPr3.Enabled = false;
  302. nuQtt1.Enabled = nuQtt2.Enabled = nuQtt3.Enabled = false;
  303. chkPr1.Text = chkPr2.Text = chkPr3.Text = txtPrFree.Text = "";
  304. nuVolCarb.Value= nuPr1.Value = nuPr2.Value = nuPr3.Value = nuPrFree.Value = 0;
  305. nuQtt1.Value = nuQtt2.Value = nuQtt3.Value = nuQttFree.Value = 1;
  306. text_no.Text = "";
  307. }
  308. private void bt_enreg_Click(object sender, EventArgs e)
  309. {
  310. }
  311. private void text_no_KeyUp(object sender, KeyEventArgs e)
  312. {
  313. if (e.KeyValue == 13)
  314. bt_search_Click(null, null);
  315. }
  316. private void bt_save_Click(object sender, EventArgs e)
  317. {
  318. tb_vente.WriteXml(Program.subfolder + @"\vente.xml");
  319. hasChanged = false;
  320. bt_save.Enabled = hasChanged;
  321. }
  322. private void f_saisie_FormClosing(object sender, FormClosingEventArgs e)
  323. {
  324. if (hasChanged)
  325. bt_save_Click(null, null);
  326. e.Cancel = false;
  327. }
  328. }
  329. }