f_saisie2.cs 18 KB

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