f_facture.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using Excel = Microsoft.Office.Interop.Excel;
  12. using System.Diagnostics;
  13. using System.Xml.Linq;
  14. namespace Fuel01
  15. {
  16. public partial class f_facture : Form
  17. {
  18. static ProdEqualityComparer prodEqC = new ProdEqualityComparer();
  19. Dictionary<IdProd, VenteTot> lvente_tot = new Dictionary<IdProd, VenteTot>(prodEqC);
  20. Dictionary<decimal, VenteTva> lvente_tva = new Dictionary<decimal, VenteTva>();
  21. DataSet1.tb_venteDataTable tb_vente = new DataSet1.tb_venteDataTable();
  22. DataSet1.tb_stationDataTable tb_station = new DataSet1.tb_stationDataTable();
  23. DataSet1.tb_stationRow current_sta = null;
  24. string Id_sta { get; set; }
  25. List<Produit> lproduit = new List<Produit>();
  26. public f_facture(string _id_sta)
  27. {
  28. Id_sta = _id_sta;
  29. InitializeComponent();
  30. if (File.Exists(Program.subfolder + @"\station.json"))
  31. tb_station = DbUtil.LoadFromJson<DataSet1.tb_stationDataTable>(Program.subfolder + @"\station.json", tb_station);
  32. if (File.Exists(Program.subfolder + @"\vente.json"))
  33. tb_vente = DbUtil.LoadFromJson<DataSet1.tb_venteDataTable>(Program.subfolder + @"\vente.json", tb_vente);
  34. tb_vente.AcceptChanges();
  35. }
  36. private void f_facture_Load(object sender, EventArgs e)
  37. {
  38. tbventeBindingSource.DataSource = tb_vente;
  39. tbventeBindingSource.Filter = " station_vt='" + Id_sta + "'";
  40. current_sta = tb_station.FindBykey_sta(Id_sta);
  41. init_list_produit();
  42. _ = TestFacture();
  43. }
  44. private static bool TestFacture()
  45. {
  46. if (!File.Exists(Program.subfolder + @"\Facture_Carbu.xlsx"))
  47. {
  48. if (File.Exists(Program.folder + @"\param\Facture_Carbu.xlsx"))
  49. {
  50. File.Copy(Program.folder + @"\param\facture_carbu.xlsx", Program.subfolder + @"\facture_carbu.xlsx");
  51. }
  52. else
  53. {
  54. if (File.Exists(Program.folder + @"\Facture_Carbu.xlsx"))
  55. {
  56. File.Copy(Program.folder + @"\facture_carbu.xlsx", Program.subfolder + @"\facture_carbu.xlsx");
  57. }
  58. }
  59. }
  60. if (!File.Exists(Program.subfolder + @"\Facture_Carbu.xlsx"))
  61. {
  62. MessageBox.Show("Fichier de base " + Program.subfolder + @"\Facture_Carbu.xlsx non trouvé!!!", "Erreur");
  63. return false;
  64. }
  65. return true;
  66. }
  67. private void bt_fact_Click(object sender, EventArgs e)
  68. {
  69. this.Cursor = Cursors.WaitCursor;
  70. makeChange();
  71. lvente_tot.Clear();
  72. lvente_tva.Clear();
  73. string fileName = "";
  74. Excel.Application xlApp = null;
  75. try
  76. {
  77. if (!File.Exists(Program.subfolder + @"\Facture_Carbu.xlsx"))
  78. {
  79. if (!File.Exists(Program.folder + @"\Facture_Carbu.xlsx"))
  80. {
  81. if (!File.Exists(Program.folder + @"\param\Facture_Carbu.xlsx"))
  82. {
  83. MessageBox.Show("Fichier de base " + Program.subfolder + @"\Facture_Carbu.xlsx non trouvé!!!", "Erreur");
  84. return;
  85. }
  86. else
  87. {
  88. File.Copy(Program.folder + @"\param\facture_carbu.xlsx", Program.subfolder + @"\facture_carbu.xlsx");
  89. }
  90. File.Copy(Program.folder + @"\facture_carbu.xlsx", Program.subfolder + @"\facture_carbu.xlsx");
  91. }
  92. else
  93. {
  94. File.Copy(Program.folder + @"\param\facture_carbu.xlsx", Program.subfolder + @"\facture_carbu.xlsx");
  95. }
  96. }
  97. if (!File.Exists(Program.subfolder + @"\Facture_Carbu.xlsx"))
  98. {
  99. MessageBox.Show("Fichier de base " + Program.subfolder + @"\Facture_Carbu.xlsx non trouvé!!!", "Erreur");
  100. return;
  101. }
  102. fileName = Program.subfolder + @"\Facture_" + Id_sta + ".xlsx";
  103. File.Copy(Program.subfolder + @"\Facture_Carbu.xlsx", fileName, true);
  104. Excel.Workbook xlWorkBook;
  105. Excel.Worksheet xlWorkSheet;
  106. object misValue = System.Reflection.Missing.Value;
  107. try
  108. {
  109. xlApp = new Excel.Application();
  110. xlWorkBook = xlApp.Workbooks.Open(fileName, misValue);
  111. xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
  112. }
  113. catch (Exception ex)
  114. {
  115. MessageBox.Show($"Vérifier que le fichier n'est pas ouvert et est un fichier Excel correct \r\n+ {ex.Message} ", "Ouverture de " + fileName + " impossible", MessageBoxButtons.OK, MessageBoxIcon.Error);
  116. if (xlApp != null)
  117. xlApp.Quit();
  118. this.Cursor = Cursors.Default;
  119. return;
  120. }
  121. xlWorkSheet.Cells[2, 2].Value = makeItCLean(xlWorkSheet.Cells[2, 2].Value, current_sta["nom_sta"].ToString());
  122. xlWorkSheet.Cells[3, 2].Value = makeItCLean(xlWorkSheet.Cells[3, 2].Value, current_sta["adr1_sta"].ToString());
  123. xlWorkSheet.Cells[4, 2].Value = makeItCLean(xlWorkSheet.Cells[4, 2].Value, current_sta["adr2_sta"].ToString());
  124. xlWorkSheet.Cells[5, 2].Value = makeItCLean(xlWorkSheet.Cells[5, 2].Value, current_sta["cp_sta"].ToString() + "-" + current_sta["ville_sta"].ToString());
  125. xlWorkSheet.Cells[6, 2].Value = makeItCLean(xlWorkSheet.Cells[6, 2].Value, current_sta["pays_sta"].ToString());
  126. xlWorkSheet.Cells[8, 2].Value = makeItCLean(xlWorkSheet.Cells[8, 2].Value, current_sta["tel_sta"].ToString());
  127. xlWorkSheet.Cells[9, 2].Value = makeItCLean(xlWorkSheet.Cells[9, 2].Value, current_sta["tva_sta"].ToString());
  128. xlWorkSheet.Cells[10, 4].Value = makeItCLean(xlWorkSheet.Cells[10, 4].Value, current_sta["ville_sta"].ToString());
  129. xlWorkSheet.Cells[11, 4].Value = makeItCLean(xlWorkSheet.Cells[11, 4].Value, DateTime.Now.ToString("Le : dd/MM/yyyy"));
  130. xlWorkSheet.Cells[14, 2].Value = makeItCLean(xlWorkSheet.Cells[14, 2].Value, Program.nom_ope + " du " + current_sta.date_sta.ToString("dd/MM/yy"));
  131. xlWorkSheet.Cells[48, 2].Value = makeItCLean(xlWorkSheet.Cells[48, 2].Value, current_sta["divers_sta"].ToString());
  132. Dictionary<string, VenteTot> lvente_fact = new Dictionary<string, VenteTot>();
  133. lvente_fact.Add("A - DIESEL", new VenteTot { vente = null, total_ttc = 0, total_qtt = 0, total_ht = 0 });
  134. lvente_fact.Add("B - AUTRES CARBURANTS", new VenteTot { vente = null, total_ttc = 0, total_qtt = 0, total_ht = 0 });
  135. lvente_tot.Clear();
  136. try
  137. {
  138. foreach (DataSet1.tb_venteRow myrow in tb_vente.Where(p => p.station_vt == Id_sta))
  139. {
  140. VenteTot myFact = null;
  141. Produit pdt = lproduit.Find(xx => xx.key_prod == myrow.prod_vt);
  142. string sort = "Z";
  143. if (pdt != null) // Produit référencé
  144. {
  145. var isDIesel = myrow.prod_vt.ToUpper().Contains("DIESEL");
  146. if (isDIesel)
  147. {
  148. sort = pdt.type_prod == "Carburant" ? ("A - DIESEL") : "C - PRODUITS DIVERS";
  149. }
  150. else
  151. {
  152. sort = pdt.type_prod == "Carburant" ? ("B - AUTRES CARBURANTS") : "C - PRODUITS DIVERS";
  153. }
  154. }
  155. else
  156. {
  157. sort = "C - PRODUITS DIVERS";
  158. }
  159. if (!lvente_fact.ContainsKey(sort))
  160. lvente_fact.Add(sort, new VenteTot { vente = myrow, total_ttc = 0, total_qtt = 0, total_ht = 0 });
  161. myFact = lvente_fact[sort];
  162. myFact.total_qtt += myrow.qtt_vt;
  163. myFact.total_ht += myrow.ht_vt;
  164. myFact.total_ttc += myrow.ttc_vt;
  165. }
  166. int lig = 21;
  167. decimal totgal = 0;
  168. foreach (KeyValuePair<string, VenteTot> myVente in lvente_fact)
  169. {
  170. Console.WriteLine(string.Format(" Type :{0} ", myVente.Key));
  171. if (myVente.Value.total_ht > 0)
  172. {
  173. xlWorkSheet.Cells[lig, 2].Value = myVente.Key.Replace("A - ", "").Replace("B - ", "").Replace("C - ", "").Replace("D - ", "");
  174. xlWorkSheet.Cells[lig, 6].Value = myVente.Value.total_ttc;
  175. totgal += myVente.Value.total_ttc;
  176. lig += 1;
  177. }
  178. }
  179. xlWorkSheet.Cells[41, 6].Value = totgal;
  180. xlWorkBook.Save();
  181. xlWorkBook.Close();
  182. }
  183. catch (Exception ex)
  184. {
  185. MessageBox.Show(ex.Message, "Erreur à la création de " + fileName);
  186. }
  187. if (xlApp != null)
  188. xlApp.Quit();
  189. ProcessStartInfo notepadStartInfo = new ProcessStartInfo(fileName);
  190. Process.Start(notepadStartInfo);
  191. }
  192. catch (Exception ex)
  193. {
  194. MessageBox.Show("Abandon !!!! \r\n " + ex.Message);
  195. }
  196. this.Cursor = Cursors.Default;
  197. }
  198. private static string makeItCLean(string strExcel, string data)
  199. {
  200. string ret = data;
  201. if (strExcel.Length > 0 && strExcel.Substring(0, 1) == "#")
  202. {
  203. ret = strExcel.Substring(1) + data;
  204. }
  205. return ret;
  206. }
  207. private void f_facture_FormClosing(object sender, FormClosingEventArgs e)
  208. {
  209. e.Cancel = makeChange();
  210. }
  211. private void init_list_produit()
  212. {
  213. XDocument xRoot = XDocument.Load(Program.folder + @"\param\produit.xml");
  214. var data = from item in xRoot.Descendants("tb_prod")
  215. orderby item.Element("type_prod").Value, item.Element("lib_prod").Value
  216. select new Produit()
  217. {
  218. key_prod = item.Element("key_prod").Value,
  219. lib_prod = item.Element("lib_prod").Value,
  220. type_prod = item.Element("type_prod").Value
  221. };
  222. lproduit = data.ToList();
  223. }
  224. private void bt_simul_Click(object sender, EventArgs e)
  225. {
  226. Dictionary<string, VenteTot> lvente_fact = new Dictionary<string, VenteTot>();
  227. lvente_fact.Add("A - DIESEL", new VenteTot { vente = null, total_ttc = 0, total_qtt = 0, total_ht = 0 });
  228. lvente_fact.Add("B - AUTRES CARBURANTS", new VenteTot { vente = null, total_ttc = 0, total_qtt = 0, total_ht = 0 });
  229. List<Simul> lsimul = new List<Simul>();
  230. lvente_tot.Clear();
  231. lvente_tva.Clear();
  232. try
  233. {
  234. foreach (DataSet1.tb_venteRow myrow in tb_vente.Where(p => p.station_vt == Id_sta))
  235. {
  236. VenteTot myTot = null;
  237. VenteTot myFact = null;
  238. Produit pdt = lproduit.Find(xx => xx.key_prod == myrow.prod_vt);
  239. string lib_produit = "";
  240. string sort;
  241. if (pdt != null) // Produit référencé
  242. {
  243. var isDIesel = myrow.prod_vt.ToUpper().Contains("DIESEL");
  244. if (isDIesel)
  245. {
  246. sort = pdt.type_prod == "Carburant" ? ("A - DIESEL") : "C - PRODUITS DIVERS";
  247. }
  248. else
  249. {
  250. sort = pdt.type_prod == "Carburant" ? ("B - AUTRES CARBURANTS") : "C - PRODUITS DIVERS";
  251. }
  252. lib_produit = pdt.lib_prod;
  253. }
  254. else
  255. sort = "C - PRODUITS DIVERS";
  256. IdProd my_id = new IdProd { code_pro = myrow.prod_vt, ttc_pro = myrow.pu_vt, tva_pro = myrow.tva_vt, lib_pro = lib_produit, type_pro = sort };
  257. if (!lvente_fact.ContainsKey(sort))
  258. lvente_fact.Add(sort, new VenteTot { vente = null, total_ttc = 0, total_qtt = 0, total_ht = 0 });
  259. if (!lvente_tot.ContainsKey(my_id))
  260. lvente_tot.Add(my_id, new VenteTot { vente = null, total_ttc = 0, total_qtt = 0, total_ht = 0 });
  261. myTot = lvente_tot[my_id];
  262. myTot.total_qtt += myrow.qtt_vt;
  263. myTot.total_ht += myrow.ht_vt;
  264. myTot.total_ttc += myrow.ttc_vt;
  265. myFact = lvente_fact[sort];
  266. myFact.total_qtt += myrow.qtt_vt;
  267. myFact.total_ht += myrow.ht_vt;
  268. myFact.total_ttc += myrow.ttc_vt;
  269. VenteTva myTva = null;
  270. if (myrow.tva_vt != 0)
  271. {
  272. if (!lvente_tva.ContainsKey(myrow.tva_vt))
  273. lvente_tva.Add(myrow.tva_vt, new VenteTva { taux_tva = myrow.tva_vt, base_tva = 0, mnt_ttc = 0, mnt_tva = 0 });
  274. myTva = lvente_tva[myrow.tva_vt];
  275. myTva.base_tva += myrow.ht_vt;
  276. myTva.mnt_ttc += myrow.ttc_vt;
  277. myTva.mnt_tva = myTva.mnt_ttc - myTva.base_tva;
  278. Console.WriteLine(string.Format(" taux:{0} base:{1} tva:{2} ttc:{3} ", myrow.tva_vt, myTva.base_tva, myTva.mnt_tva, myTva.mnt_ttc));
  279. }
  280. }
  281. int nbprod = 0;
  282. decimal totgal = 0;
  283. foreach (KeyValuePair<string, VenteTot> myVente in lvente_fact)
  284. {
  285. Console.WriteLine(string.Format(" Type :{0} ", myVente.Key));
  286. Simul sm = new Simul();
  287. sm.libel = myVente.Key;
  288. sm.qtt = 0;
  289. sm.pu = 0;
  290. sm.prix = myVente.Value.total_ttc;
  291. totgal += sm.prix;
  292. nbprod += 1;
  293. lsimul.Add(sm);
  294. }
  295. lsimul.Add(new Simul { libel = "Total", prix = totgal, qtt = null, pu = null });
  296. dg_simul.DataSource = lsimul;
  297. dg_simul.Columns[0].Width = 300;
  298. DataGridViewCellStyle N2 = new DataGridViewCellStyle();
  299. N2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
  300. N2.Format = "N2";
  301. N2.NullValue = "";
  302. DataGridViewCellStyle N3 = new DataGridViewCellStyle();
  303. N3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
  304. N3.Format = "N3";
  305. N3.NullValue = "";
  306. dg_simul.Columns[1].DefaultCellStyle = N2;
  307. dg_simul.Columns[2].DefaultCellStyle = N3;
  308. dg_simul.Columns[3].DefaultCellStyle = N2;
  309. }
  310. catch (Exception ex)
  311. {
  312. MessageBox.Show(ex.Message, "Erreur au calcul de la facture");
  313. }
  314. }
  315. private bool makeChange()
  316. {
  317. this.vt_qtt.DataBindings["Text"].BindingManagerBase.EndCurrentEdit();
  318. bool ret = false;
  319. DataTable tb_vente_change = tb_vente.GetChanges();
  320. while (tb_vente_change != null)
  321. {
  322. if (MessageBox.Show("Voulez-vous Valider Oui ou Non les modifications apportées ?", "Modifications en cours!!!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation) == DialogResult.Yes)
  323. {
  324. tb_vente.AcceptChanges();
  325. tb_vente.WriteXml(Program.subfolder + @"\vente.xml", XmlWriteMode.WriteSchema);
  326. tb_vente_change = tb_vente.GetChanges();
  327. DbUtil.SaveToJson<DataSet1.tb_venteDataTable>(Program.subfolder + @"\vente.json", tb_vente);
  328. }
  329. else
  330. {
  331. tb_vente.RejectChanges();
  332. tb_vente_change = tb_vente.GetChanges();
  333. }
  334. }
  335. ret = (tb_vente_change != null);
  336. return ret;
  337. }
  338. private void vt_qtt_TextChanged(object sender, EventArgs e)
  339. {
  340. //this.vt_ttc.DataBindings["Text"].BindingManagerBase.EndCurrentEdit();
  341. if (vt_ttc.Text == "")
  342. {
  343. vt_ttc.Text = "0";
  344. vt_qtt.SelectAll();
  345. }
  346. float ttc = 0;
  347. try
  348. {
  349. ttc = (float)Convert.ToDouble(this.vt_ttc.Text);
  350. }
  351. catch (Exception ex)
  352. {
  353. return;
  354. }
  355. float qtt = 0;
  356. float puttc = (float)Convert.ToDouble(this.vt_pu.Text);
  357. float tva = 0;
  358. float ht = 0;
  359. qtt = ttc / puttc;
  360. ht = (qtt * puttc) / (1 + (tva / 100));
  361. vt_qtt.Text = String.Format("{0:0.00}", qtt);
  362. vt_ht.Text = String.Format("{0:0.00}", ht);
  363. vt_ttc.Refresh();
  364. vt_ht.Refresh();
  365. //this.vt_qtt.DataBindings["Text"].BindingManagerBase.EndCurrentEdit();
  366. //this.vt_ht.DataBindings["Text"].BindingManagerBase.EndCurrentEdit();
  367. }
  368. private void vt_qtt_Enter(object sender, EventArgs e)
  369. {
  370. this.vt_ttc.TextChanged += new System.EventHandler(this.vt_qtt_TextChanged);
  371. }
  372. private void vt_qtt_Leave(object sender, EventArgs e)
  373. {
  374. if (vt_qtt.Text == "") vt_qtt.Text = "0";
  375. this.vt_ttc.TextChanged -= new System.EventHandler(this.vt_qtt_TextChanged);
  376. }
  377. }
  378. public class Simul
  379. {
  380. public string libel { get; set; }
  381. public decimal? qtt { get; set; }
  382. public decimal? pu { get; set; }
  383. public decimal prix { get; set; }
  384. }
  385. }