f_saisie2.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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_saisie2 : 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. int Retry = 0;
  28. bool hasChanged = false;
  29. bool isDiesel = false;
  30. int indiceCarb = -1;
  31. string id_vehi="";
  32. public f_saisie2()
  33. {
  34. InitializeComponent();
  35. }
  36. private void f_saisie_Load(object sender, EventArgs e)
  37. {
  38. Program.subfolder = Program.folder + @"\" + Program.key_ope;
  39. if (File.Exists(Program.subfolder + @"\vehicule.json"))
  40. tb_vehi = DbUtil.LoadFromJson<DataSet1.tb_vehiDataTable>(Program.subfolder + @"\Vehicule.json", tb_vehi);
  41. tbvehiBindingSource.DataSource = tb_vehi;
  42. if (File.Exists(Program.subfolder + @"\station.json"))
  43. tb_station = DbUtil.LoadFromJson<DataSet1.tb_stationDataTable>(Program.subfolder + @"\station.json", tb_station);
  44. tbstationBindingSource.DataSource = tb_station;
  45. myStation = tb_station.FindBykey_sta(Program.key_sta);
  46. DataRowView rowView = null;
  47. if (myStation != null)
  48. {
  49. tbstationBindingSource.MoveFirst();
  50. while (true)
  51. {
  52. rowView = tbstationBindingSource.Current as DataRowView;
  53. if (myStation != rowView.Row)
  54. if (tbstationBindingSource.Position == tbstationBindingSource.Count)
  55. break;
  56. else
  57. tbstationBindingSource.MoveNext();
  58. else
  59. break;
  60. }
  61. }
  62. if (File.Exists(Program.subfolder + @"\presta.json"))
  63. tb_presta = DbUtil.LoadFromJson<DataSet1.tb_prestaDataTable>(Program.subfolder + @"\presta.json", tb_presta);
  64. tbprestaBindingSource.DataSource = tb_presta;
  65. if (File.Exists(Program.subfolder + @"\vente.json"))
  66. tb_vente = DbUtil.LoadFromJson<DataSet1.tb_venteDataTable>(Program.subfolder + @"\vente.json", tb_vente);
  67. tb_vente.AcceptChanges();
  68. tbventeBindingSource.DataSource = tb_vente;
  69. tbprestaBindingSource.Filter = " sta_pr='" + Program.key_sta + "'";
  70. tbventeBindingSource.Filter = " station_vt='" + Program.key_sta + "'";
  71. tbprestaBindingSource.Sort = "sta_pr,kind_pr,prod_pr";
  72. tbstationBindingSource_PositionChanged(null, null);
  73. tbventetmpbindingSource.DataSource = tb_ventetmp;
  74. lblstadt.Text= myStation.date_sta.ToString("dd/MM/yy") + " " + myStation.nom_sta;
  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. old_pos = tbprestaBindingSource.Position;
  89. DataSet1.tb_prestaRow mydata = null;
  90. DataRowView myrow = tbprestaBindingSource.Current as DataRowView;
  91. if (myrow != null)
  92. {
  93. mydata = myrow.Row as DataSet1.tb_prestaRow;
  94. if (mydata["kind_pr"].ToString() == "Carburant")
  95. {
  96. if (mydata["prod_pr"].ToString().ToUpper().Contains("DIESEL"))
  97. Ldies.Add(mydata);
  98. else
  99. Lcarb.Add(mydata);
  100. }
  101. else
  102. Lprod.Add(mydata);
  103. }
  104. tbprestaBindingSource.MoveNext();
  105. }
  106. aff_zero();
  107. }
  108. private void aff_carb(string curcarb)
  109. {
  110. rdCarb1.Enabled = rdCarb2.Enabled = rdCarb3.Enabled = rdCarb4.Enabled = rdCarb5.Enabled = rdCarb6.Enabled = false;
  111. rdCarb1.Text = rdCarb2.Text = rdCarb3.Text = rdCarb4.Text = rdCarb5.Text = rdCarb6.Text = "";
  112. nuPxCarb.Value = 0;
  113. if (isDiesel)
  114. Lmycarb = Ldies;
  115. else
  116. Lmycarb = Lcarb;
  117. if (Lmycarb.Count >= 1)
  118. {
  119. rdCarb1.Enabled = true;
  120. rdCarb1.Text = Lmycarb[0].prod_pr;
  121. rdCarb1.Checked = (curcarb == rdCarb1.Text);
  122. if ( rdCarb1.Checked ) rdCarb_CheckedChanged(rdCarb1, null);
  123. }
  124. if (Lmycarb.Count >= 2)
  125. {
  126. rdCarb2.Enabled = true;
  127. rdCarb2.Text = Lmycarb[1].prod_pr;
  128. rdCarb2.Checked = (curcarb == rdCarb2.Text);
  129. if (rdCarb2.Checked) rdCarb_CheckedChanged(rdCarb2, null);
  130. }
  131. if (Lmycarb.Count >= 3)
  132. {
  133. rdCarb3.Enabled = true;
  134. rdCarb3.Text = Lmycarb[2].prod_pr;
  135. rdCarb3.Checked = (curcarb == rdCarb3.Text);
  136. if (rdCarb3.Checked) rdCarb_CheckedChanged(rdCarb3, null);
  137. }
  138. if (Lmycarb.Count >= 4)
  139. {
  140. rdCarb4.Enabled = true;
  141. rdCarb4.Text = Lmycarb[3].prod_pr;
  142. rdCarb4.Checked = (curcarb == rdCarb4.Text);
  143. if (rdCarb4.Checked) rdCarb_CheckedChanged(rdCarb4, null);
  144. }
  145. if (Lmycarb.Count >=5 )
  146. {
  147. rdCarb5.Enabled = true;
  148. rdCarb5.Text = Lmycarb[4].prod_pr;
  149. rdCarb5.Checked = (curcarb == rdCarb5.Text);
  150. if (rdCarb5.Checked) rdCarb_CheckedChanged(rdCarb5, null);
  151. }
  152. if (Lmycarb.Count >= 6)
  153. {
  154. rdCarb6.Enabled = true;
  155. rdCarb6.Text = Lmycarb[5].prod_pr;
  156. rdCarb6.Checked = (curcarb == rdCarb6.Text);
  157. if (rdCarb6.Checked) rdCarb_CheckedChanged(rdCarb6, null);
  158. }
  159. //if (Lcarb.Count > 0)
  160. //{
  161. // rdCarb1.Checked = true;
  162. // rdCarb_CheckedChanged(rdCarb1, null);
  163. //}
  164. }
  165. private void aff_prod()
  166. {
  167. chkPrFree.Enabled = true;
  168. if (Lprod.Count >= 1)
  169. {
  170. chkPr1.Enabled = true; //nuTot1.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 = true; //nuTot2.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 = true; //= nuTot3.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. if (Retry++>15) //on evite la boucle infinie
  190. {
  191. Retry = 0;
  192. return;
  193. }
  194. string from = ((NumericUpDown)sender).Name;
  195. switch (from)
  196. {
  197. case "nuPxCarb":
  198. case "nuMntCarb":
  199. if (nuPxCarb.Value > 0)
  200. {
  201. nuVolCarb.Value = nuMntCarb.Value / nuPxCarb.Value;
  202. }
  203. break;
  204. case "nuVolCarb":
  205. nuMntCarb.Value = nuVolCarb.Value * nuPxCarb.Value;
  206. break;
  207. case "nuPr1":
  208. case "nuTot1":
  209. if (nuPr1.Value > 0)
  210. {
  211. nuQtt1.Value = nuTot1.Value / nuPr1.Value;
  212. }
  213. break;
  214. case "nuQtt1":
  215. nuTot1.Value = nuQtt1.Value * nuPr1.Value;
  216. break;
  217. case "nuPr2":
  218. case "nuTot2":
  219. if (nuPr2.Value > 0)
  220. {
  221. nuQtt2.Value = nuTot2.Value / nuPr2.Value;
  222. }
  223. break;
  224. case "nuQtt2":
  225. nuTot2.Value = nuQtt2.Value * nuPr2.Value;
  226. break;
  227. case "nuPr3":
  228. case "nuTot3":
  229. if (nuPr3.Value > 0)
  230. {
  231. nuQtt3.Value = nuTot3.Value / nuPr3.Value;
  232. }
  233. break;
  234. case "nuQtt3":
  235. nuTot3.Value = nuQtt3.Value * nuPr3.Value;
  236. break;
  237. case "nuPrFree":
  238. case "nuQttFree":
  239. nuTotFree.Value = nuPrFree.Value * nuQttFree.Value;
  240. break;
  241. case "nuTotFree":
  242. nuQttFree.Value = 1;
  243. nuPrFree.Value= nuTotFree.Value / nuQttFree.Value;
  244. break;
  245. }
  246. }
  247. private void rdCarb_CheckedChanged(object sender, EventArgs e)
  248. {
  249. if (((RadioButton)sender).Checked == true)
  250. {
  251. //on récupère le nom pour l'indice du tableau
  252. indiceCarb = Convert.ToInt16(((RadioButton)sender).Name.Substring(6, 1))-1;
  253. if (indiceCarb < Lmycarb.Count && indiceCarb >= 0)
  254. nuPxCarb.Value = Lmycarb[indiceCarb].ttc_pr;
  255. else
  256. MessageBox.Show("Carburant non disponible pour ce véhicule!");
  257. }
  258. }
  259. private void bt_abort_Click(object sender, EventArgs e)
  260. {
  261. aff_zero();
  262. text_no.Focus();
  263. }
  264. private void bt_valid_Click(object sender, EventArgs e)
  265. {
  266. if (chkPrFree.Checked)
  267. {
  268. if ( nuPrFree.Value * nuQttFree.Value ==0 || txtPrFree.Text.Trim(new char[]{ ' ','a' })=="" )
  269. {
  270. MessageBox.Show("Incohérence dans les données libres (libellé ou prix ou qtt ou tva");
  271. return;
  272. }
  273. }
  274. if (nuVolCarb.Value > 0) ajpresta(Lmycarb[indiceCarb], nuPxCarb.Value, nuVolCarb.Value, nuMntCarb.Text);
  275. if (chkPr1.Checked) ajpresta(Lprod[0], nuPr1.Value, nuQtt1.Value, nuTot1.Text);
  276. if (chkPr2.Checked) ajpresta(Lprod[1], nuPr2.Value, nuQtt2.Value, nuTot2.Text);
  277. if (chkPr3.Checked) ajpresta(Lprod[2], nuPr3.Value, nuQtt3.Value, nuTot3.Text);
  278. if (chkPrFree.Checked) ajpresta(txtPrFree.Text, nuPrFree.Value, nuQttFree.Value, nuTotFree.Text,numTvaFree.Value);
  279. f_ticket ticket = new f_ticket(myStation, myVehi, tb_ventetmp);
  280. if (ticket.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  281. {
  282. foreach (DataSet1.tb_venteRow myVt in tb_ventetmp)
  283. {
  284. 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);
  285. }
  286. hasChanged = true;
  287. bt_save.Enabled = hasChanged;
  288. }
  289. tb_ventetmp.Clear();
  290. aff_zero();
  291. }
  292. /// <summary>
  293. /// Presta enregistrée
  294. /// </summary>
  295. /// <param name="prRow"></param>
  296. /// <param name="pu"></param>
  297. /// <param name="qtt"></param>
  298. /// <param name="total_str"></param>
  299. private void ajpresta(DataSet1.tb_prestaRow prRow, decimal pu, decimal qtt, string total_str)
  300. {
  301. decimal tva = 0;
  302. decimal totht = 0;
  303. decimal total = Convert.ToDecimal(total_str);
  304. string lib = prRow.prod_pr;
  305. tva = prRow.tva_pr;
  306. totht = Math.Round(total / (1 + tva / 100),2);
  307. DateTime dt = DateTime.Now;
  308. tb_ventetmp.Addtb_venteRow(Program.key_ope, Program.key_sta, id_vehi, lib, dt, qtt, pu, tva, totht, total);
  309. }
  310. /// <summary>
  311. /// Presta libre
  312. /// </summary>
  313. /// <param name="lib"></param>
  314. /// <param name="pu"></param>
  315. /// <param name="qtt"></param>
  316. /// <param name="total_str"></param>
  317. /// <param name="tva"></param>
  318. private void ajpresta(string lib, decimal pu, decimal qtt, string total_str,decimal tva)
  319. {
  320. decimal totht = 0;
  321. decimal total = Convert.ToDecimal(total_str);
  322. totht = Math.Round(total / (1 + tva / 100), 2);
  323. DateTime dt = DateTime.Now;
  324. tb_ventetmp.Addtb_venteRow(Program.key_ope, Program.key_sta, id_vehi, lib, dt, qtt, pu, tva, totht, total);
  325. }
  326. private void bt_search_Click(object sender, EventArgs e)
  327. {
  328. Retry = 0;
  329. UInt32 num;
  330. if (UInt32.TryParse(text_no.Text, out num))
  331. {
  332. myVehi = tb_vehi.FirstOrDefault(v => v.num_vehi == num && v.categ_vehi == (rdAuto.Checked ? "A" : "M"));
  333. if (myVehi != null)
  334. {
  335. lbl_pilote.Text = myVehi.cond_vehi ;
  336. isDiesel = myVehi.carbu_vehi.ToUpper().Contains("DIESEL");
  337. aff_carb(myVehi.carbu_vehi);
  338. aff_prod();
  339. id_vehi = myVehi.sort1_vehi;
  340. rdCarb1.Checked = true;
  341. }
  342. else
  343. {
  344. aff_zero();
  345. text_no.Text = num.ToString();
  346. text_no.SelectionStart = 0;
  347. text_no.SelectionLength = text_no.Text.Length;
  348. }
  349. }
  350. turnBlock(myVehi != null);
  351. }
  352. private void turnBlock(bool status)
  353. {
  354. grpCarb.Enabled = status;
  355. grpServ.Enabled = status;
  356. bt_abort.Enabled = status;
  357. bt_valid.Enabled = status;
  358. }
  359. private void aff_zero()
  360. {
  361. chkPr1.Enabled = chkPr2.Enabled = chkPr3.Enabled = chkPrFree.Enabled= false;
  362. chkPr1.Checked = chkPr2.Checked = chkPr3.Checked = chkPrFree.Checked = false;
  363. nuPr1.Enabled = nuPr2.Enabled = nuPr3.Enabled = false;
  364. nuQtt1.Enabled = nuQtt2.Enabled = nuQtt3.Enabled = false;
  365. nuTot1.Enabled = nuTot2.Enabled = nuTot3.Enabled = false;
  366. chkPr1.Text = chkPr2.Text = chkPr3.Text = txtPrFree.Text = "";
  367. nuVolCarb.Value= nuPr1.Value = nuPr2.Value = nuPr3.Value = nuPrFree.Value = 0;
  368. nuQtt1.Value = nuQtt2.Value = nuQtt3.Value = nuQttFree.Value = 0;
  369. nuTot1.Value = nuTot2.Value = nuTot3.Value = 0;
  370. nuPxCarb.Value = nuMntCarb.Value = nuPxCarb.Value = 0;
  371. text_no.Text = "";
  372. }
  373. private void text_no_KeyUp(object sender, KeyEventArgs e)
  374. {
  375. if (e.KeyValue == 13)
  376. bt_search_Click(null, null);
  377. }
  378. private void bt_save_Click(object sender, EventArgs e)
  379. {
  380. DbUtil.SaveToJson<DataSet1.tb_venteDataTable>(Program.subfolder + @"\vente.json", tb_vente);
  381. hasChanged = false;
  382. bt_save.Enabled = hasChanged;
  383. }
  384. private void f_saisie_FormClosing(object sender, FormClosingEventArgs e)
  385. {
  386. if (hasChanged)
  387. bt_save_Click(null, null);
  388. e.Cancel = false;
  389. }
  390. private void chkPrFree_CheckStateChanged(object sender, EventArgs e)
  391. {
  392. nuTotFree.Enabled = ((CheckBox)sender).Checked;
  393. if (!((CheckBox)sender).Checked)
  394. nuTotFree.Value = 1;
  395. }
  396. private void chkProduit_CheckedChanged(object sender, EventArgs e)
  397. {
  398. bool Checked= ((CheckBox)sender).Checked;
  399. var Name= ((CheckBox)sender).Name;
  400. switch (Name)
  401. {
  402. case "chkPr1":
  403. nuQtt1.Enabled = nuTot1.Enabled = Checked;
  404. nuQtt1.Value = nuTot1.Value = 0;
  405. break;
  406. case "chkPr2":
  407. nuQtt2.Enabled = nuTot2.Enabled = Checked;
  408. nuQtt2.Value = nuTot2.Value = 0;
  409. break;
  410. case "chkPr3":
  411. nuQtt3.Enabled = nuTot3.Enabled = Checked;
  412. nuQtt3.Value = nuTot3.Value = 0;
  413. break;
  414. }
  415. }
  416. }
  417. }