f_facture.cs 17 KB

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