using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; using Excel = Microsoft.Office.Interop.Excel; namespace Fuel01 { public partial class f_presta : Form { DataSet1.tb_prestaDataTable tb_presta = new DataSet1.tb_prestaDataTable(); DataSet1.tb_venteDataTable tb_vente = new DataSet1.tb_venteDataTable(); DataSet1.tb_stationDataTable tb_station = new DataSet1.tb_stationDataTable(); DataSet1.tb_stationRow current_sta = null; List lproduit = new List(); DataGridViewRow myrow = null; float oldqtt = 0; static ProdEqualityComparer prodEqC = new ProdEqualityComparer(); Dictionary lvente_tot = new Dictionary(prodEqC); Dictionary lvente_tva = new Dictionary(); string id_sta = ""; public f_presta(TextBox key_sta) { InitializeComponent(); if (File.Exists(Program.subfolder + @"\station.json")) tb_station = DbUtil.LoadFromJson(Program.subfolder + @"\station.json", tb_station); if (File.Exists(Program.folder + @"\param\produit.xml")) init_list_produit(); if (File.Exists(Program.subfolder + @"\presta_base.json")) tb_presta = DbUtil.LoadFromJson(Program.subfolder + @"\presta_base.json", tb_presta); tb_presta.AcceptChanges(); tbprestaBindingSource.DataSource = tb_presta; if (File.Exists(Program.subfolder + @"\vente.json")) tb_vente = DbUtil.LoadFromJson(Program.subfolder + @"\vente.json", tb_vente); tb_vente.AcceptChanges(); tbventeBindingSource.DataSource = tb_vente; id_sta = key_sta.Text; tbprestaBindingSource.Filter = " sta_pr='" + id_sta + "'"; tbventeBindingSource.Filter = " station_vt='" + id_sta + "'"; tbprestaBindingSource.Sort = "sta_pr,kind_pr,prod_pr"; key_sta.TextChanged += key_sta_TextChanged; key_sta_TextChanged(key_sta, null); t_facture.Enabled = Program.isExcel; } private void f_presta_Load(object sender, EventArgs e) { } void key_sta_TextChanged(object sender, EventArgs e) { TextBox mytxtb = sender as TextBox; id_sta = mytxtb.Text; tbprestaBindingSource.Filter = " sta_pr='" + id_sta + "'"; tbventeBindingSource.Filter = " station_vt='" + id_sta + "'"; current_sta = tb_station.FindBykey_sta(id_sta); } private void f_presta_FormClosing(object sender, FormClosingEventArgs e) { DbUtil.SaveToJson(Program.subfolder + @"\presta_base.json", tb_presta); DbUtil.SaveToJson(Program.subfolder + @"\presta_base.json", tb_vente); tbprestaBindingSource.Filter = ""; tbprestaBindingSource.Sort = "sta_pr,kind_pr,prod_pr"; tbprestaBindingSource.MoveFirst(); int i = 0; while (tbprestaBindingSource.Position 0) { if (dg_presta.Rows[e.RowIndex - 1].Cells[0].Value is System.DBNull) { dg_presta.Rows[e.RowIndex - 1].Cells[0].Value = id_sta; } if (dg_presta.Rows[e.RowIndex - 1].Cells[0].Value == null) { dg_presta.Rows[e.RowIndex - 1].Cells[0].Value = id_sta; } } } private void bt_ajCarbu_Click(object sender, EventArgs e) { foreach (produit p in lproduit.Where(p => p.type_prod == "Carburant")) { bool ok = false; for (int i = 0; i < dg_presta.RowCount - 1 && !ok; i++) if (dg_presta.Rows[i].Cells[1].Value.ToString() == p.key_prod) ok = true; if (!ok) { DataSet1.tb_prestaRow myNewRow = tb_presta.Newtb_prestaRow(); myNewRow["sta_pr"] = id_sta; myNewRow["prod_pr"] = p.key_prod; myNewRow["kind_pr"] = p.type_prod; myNewRow["unite_pr"] = "L"; myNewRow["pu_pr"] = 1; myNewRow["tva_pr"] = 20; myNewRow["ttc_pr"] = 1.2; tb_presta.Addtb_prestaRow(myNewRow); } } } private void bt_ajProd_Click(object sender, EventArgs e) { foreach (produit p in lproduit) { bool ok = false; for (int i = 0; i < dg_presta.RowCount - 1 && !ok; i++) if (dg_presta.Rows[i].Cells[1].Value.ToString() == p.key_prod) ok = true; if (!ok) { DataSet1.tb_prestaRow myNewRow = tb_presta.Newtb_prestaRow(); myNewRow["sta_pr"] = id_sta; myNewRow["prod_pr"] = p.key_prod; myNewRow["kind_pr"] = p.type_prod; myNewRow["unite_pr"] = p.type_prod == "Carburant" ? "L" : "U"; myNewRow["pu_pr"] = 1; myNewRow["tva_pr"] = 20; myNewRow["ttc_pr"] = 1.2; tb_presta.Addtb_prestaRow(myNewRow); } } } private void bt_calcHT_Click(object sender, EventArgs e) { foreach (DataSet1.tb_prestaRow p in tb_presta.Rows) { if (p["ttc_pr"].ToString() != "" && p["tva_pr"].ToString() != "") { Decimal ttc = (Decimal)p["ttc_pr"]; Decimal tva = 1 + (Decimal)p["tva_pr"] / 100; Decimal ht = ttc / tva; ht = Math.Round(ht, 3); p["pu_pr"] = ht; } } } private void bt_calcTTC_Click(object sender, EventArgs e) { foreach (DataSet1.tb_prestaRow p in tb_presta.Rows) { Console.WriteLine(p["sta_pr"].ToString()); Console.WriteLine(p["prod_pr"].ToString()); Console.WriteLine(p["kind_pr"].ToString()); Console.WriteLine(p["unite_pr"].ToString()); Console.WriteLine(p["pu_pr"].ToString()); Console.WriteLine(p["tva_pr"].ToString()); Console.WriteLine(p["ttc_pr"].ToString()); if (p["pu_pr"].ToString() != "" && p["tva_pr"].ToString() != "") { Decimal ht = (Decimal)p["pu_pr"]; Decimal tva = 1 + (Decimal)p["tva_pr"] / 100; Decimal ttc = ht * tva; ttc = Math.Round(ttc, 3); p["ttc_pr"] = ttc; } } } private void dg_vente_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { if (e.RowIndex > 0) { if (dg_vente.Rows[e.RowIndex - 1].Cells[0].Value is System.DBNull) { dg_vente.Rows[e.RowIndex - 1].Cells[0].Value = Program.key_ope; dg_vente.Rows[e.RowIndex - 1].Cells[1].Value = id_sta; } if (dg_vente.Rows[e.RowIndex - 1].Cells[0].Value == null) { dg_vente.Rows[e.RowIndex - 1].Cells[0].Value = Program.key_ope; dg_vente.Rows[e.RowIndex - 1].Cells[1].Value = id_sta; } } } private void dg_presta_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex != 1) return; string typeProd=""; string prod = dg_presta.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); if (lproduit.Exists(xx => xx.key_prod == prod)) typeProd = lproduit.Find(xx => xx.key_prod == prod).type_prod; dg_presta.Rows[e.RowIndex].Cells[2].Value = typeProd; } private void button1_Click(object sender, EventArgs e) { try { string filter = tbprestaBindingSource.Filter; tbprestaBindingSource.Filter = ""; tbprestaBindingSource.Sort = "sta_pr,kind_pr,prod_pr"; tbprestaBindingSource.MoveFirst(); int size = tbprestaBindingSource.Count; System.Data.DataRowView[] arr = new System.Data.DataRowView[8192]; tbprestaBindingSource.CopyTo(arr, 0); int id_sta = 9999; string kind_carbu = "####"; XElement station = null; XElement prod = null; XElement detail = null; XElement presta = new XElement("tb_prestas", null); for (int i = 0; i < tbprestaBindingSource.Count; i++) { string a = i.ToString() + "-" + arr[i].Row["sta_pr"].ToString() + "-" + arr[i].Row["kind_pr"].ToString() + "-" + arr[i].Row["prod_pr"].ToString(); Console.WriteLine(a); if (id_sta != Convert.ToInt32(arr[i].Row["sta_pr"])) { station = new XElement("tb_presta", new XElement("sta_pr", arr[i].Row["sta_pr"].ToString())); presta.Add(station); id_sta = Convert.ToInt32(arr[i].Row["sta_pr"]); kind_carbu = "####"; } if (kind_carbu != arr[i].Row["kind_pr"].ToString()) { prod = new XElement(arr[i].Row["kind_pr"].ToString() == "Carburant" ? "carburants" : "autres", null); station.Add(prod); kind_carbu = arr[i].Row["kind_pr"].ToString(); } if (lproduit.Find(xx => xx.key_prod == arr[i].Row["prod_pr"].ToString()) != null) { detail = new XElement(arr[i].Row["kind_pr"].ToString() == "Carburant" ? "carburant" : "autre", new XElement("prod_pr", arr[i].Row["prod_pr"].ToString()), new XElement("lib_prod", lproduit.Find(xx => xx.key_prod == arr[i].Row["prod_pr"].ToString()).lib_prod), new XElement("unite_pr", arr[i].Row["unite_pr"].ToString()), new XElement("pu_pr", arr[i].Row["pu_pr"].ToString()), new XElement("tva_pr", arr[i].Row["ttc_pr"].ToString()), new XElement("ttc_pr", arr[i].Row["ttc_pr"].ToString())); prod.Add(detail); } else { MessageBox.Show(arr[i].Row["prod_pr"].ToString() + " Produit supprimé !!","Attention",MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } presta.Save(Program.subfolder + @"\presta.xml"); DbUtil.SaveToJson(Program.subfolder + @"\presta_base.json", tb_presta); tbprestaBindingSource.Filter = filter; MessageBox.Show("Sauvegarde effectuée, il faut maintenant envoyer les fichiers !!","Terminé", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("Sauvegarde non effectuée!!!!"+"\r\n"+ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void t_facture_Click(object sender, EventArgs e) { f_facture ffac = new f_facture(current_sta["key_sta"].ToString()); ffac.ShowDialog(); } private void dg_vente_CellEndEdit(object sender, DataGridViewCellEventArgs e) { Console.WriteLine("EN EDIT "); bool ok = true; if (myrow != null) { Console.WriteLine("EN EDIT OK1"); float qtt = 0; float puttc = 0; float tva = 0; float ttc = 0; float ht = 0; ok = float.TryParse(myrow.Cells[5].Value.ToString(), out qtt); if (ok) ok = float.TryParse(myrow.Cells[6].Value.ToString(), out puttc); if (ok) ok = float.TryParse(myrow.Cells[7].Value.ToString(), out tva); if (ok) ok = float.TryParse(myrow.Cells[8].Value.ToString(), out ht); if (ok) ok = float.TryParse(myrow.Cells[9].Value.ToString(), out ttc); if (!ok) { MessageBox.Show("Erreur de saisie"); dg_vente.Rows[e.RowIndex].Cells[5].Value = oldqtt; return; } dg_vente.Rows[e.RowIndex].Cells[9].Value = qtt * puttc; dg_vente.Rows[e.RowIndex].Cells[8].Value = ( qtt * puttc) / ( 1 + (tva/100)); } myrow = null; oldqtt = 0; Console.WriteLine("ENd EDIT OUt"); } private void dg_vente_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { Console.WriteLine("BEGIN EDIT"); myrow = dg_vente.Rows[e.RowIndex]; if ( !float.TryParse(myrow.Cells[5].Value.ToString(), out oldqtt)) e.Cancel = true; } private void dg_vente_CellLeave(object sender, DataGridViewCellEventArgs e) { } private void dg_vente_DataError(object sender, DataGridViewDataErrorEventArgs e) { if (myrow != null) { MessageBox.Show("Appuyez sur la touche Echappe (ESC Escape) \r\n dans la grille de saisie pour Abndonner l'opération","Erreur de saisie"); dg_vente.Rows[e.RowIndex].Cells[5].Value = oldqtt; myrow = null; oldqtt = 0; } } private void chk_modif_CheckedChanged(object sender, EventArgs e) { dg_vt_qtt.ReadOnly = !chk_modif.Checked; for (int i=0; i