f_facture.cs 17 KB

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