fenetre du vente

mail@pastecode.io avatar
unknown
apex
2 years ago
19 kB
1
Indexable
Never
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace GestionDeSctock1.Forms
{
    public partial class addvente : MetroFramework.Forms.MetroForm
    {
        public addvente(string message)
        {
            InitializeComponent();
            client.Text = message;
        }

        SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=gestion2;Integrated Security=True;");
        SqlCommand cmd;
        SqlCommand cmd1;
        SqlCommand cmd2;
        SqlCommand cmd4;
        SqlCommand cmd3;
        SqlCommand cmd5;
        SqlCommand cmd6;


        SqlDataAdapter da;
        DataTable dt = new DataTable();


        private void addvente_Load(object sender, EventArgs e)
        {
            select();
        }

        //select product 
        public void select()
        {
            dt.Clear();
            da = new SqlDataAdapter(@"SELECT [id]
                                              ,[Désignation]
                                              ,[Qté_initiale] as [Qté initiale]
                                              ,[Poids_dunité] as [Poids d'unité]
                                              ,[Prix_de_production] as [Prix de production]
                                              ,[Prix_Vente_Détail] as [Prix vente détail]
                                              ,[Prix_Vente_Gros] as [Prix vente gros]
                                          FROM [dbo].[produitFinal]", cn);
            da.Fill(dt);
            this.metroGrid1.DataSource = dt;
        }

        // calculattion
        public void calcul()
        {
            if (remise.Text != "" && puv.Text == "" && Qte.Text != "")
            {
                int qte = int.Parse(Qte.Text);
                int puuv = 0;
                int remis = int.Parse(remise.Text);
                int stock = int.Parse(prixprodstock.Text);
                int vers = int.Parse(versement.Text);

                int res;
                res = (qte * puuv) - remis;

                int rend;
                rend = res - vers;

                

                int bef = res - (stock * qte);
                int berf = (bef * 100) / res;

                resultat.Text = Convert.ToString(res);
                benefis.Text = Convert.ToString(bef);
                benpercent.Text = Convert.ToString(berf);
                rendu.Text = Convert.ToString(rend);

            }
            if (remise.Text == "" && puv.Text != "" && Qte.Text != "")
            {
                int qte = int.Parse(Qte.Text);
                int puuv = int.Parse(puv.Text);
                int remis = 0;
                int stock = int.Parse(prixprodstock.Text);

                if (versement.Text == "")
                {
                    int res;
                    res = (qte * puuv) - remis;
                    resultat.Text = Convert.ToString(res);
                    
                }
                else
                {
                    int res;
                    res = (qte * puuv) - remis;

                    int vers = int.Parse(versement.Text);

                    int rend;
                    rend = res - vers;

                    int bef = res - (stock * qte);
                    int berf = (bef * 100) / res;

                    benefis.Text = Convert.ToString(bef);
                    benpercent.Text = Convert.ToString(berf);
                    rendu.Text = Convert.ToString(rend);
                    resultat.Text = Convert.ToString(res);

                }

                

            }
            if (remise.Text != "" && puv.Text == "" && Qte.Text == "")
            {
                int qte = 0;
                int puuv = int.Parse(puv.Text);
                int remis = int.Parse(remise.Text);
                int stock = int.Parse(prixprodstock.Text);

                if (versement.Text == "")
                {
                    int res;
                    res = (qte * puuv) - remis;
                    resultat.Text = Convert.ToString(res);

                }
                else
                {
                    int res;
                    res = (qte * puuv) - remis;

                    int vers = int.Parse(versement.Text);

                    int rend;
                    rend = res - vers;

                    int bef = res - (stock * qte);
                    int berf = (bef * 100) / res;

                    benefis.Text = Convert.ToString(bef);
                    benpercent.Text = Convert.ToString(berf);
                    rendu.Text = Convert.ToString(rend);
                    resultat.Text = Convert.ToString(res);

                }


            }
            if (remise.Text != "" && puv.Text != "" && Qte.Text != "")
            {
                int qte = int.Parse(Qte.Text);
                int puuv = int.Parse(puv.Text);
                int remis = int.Parse(remise.Text);
                int stock = int.Parse(prixprodstock.Text);

                if (versement.Text == "")
                {
                    int res;
                    res = (qte * puuv) - remis;
                    resultat.Text = Convert.ToString(res);

                }
                else
                {
                    int res;
                    res = (qte * puuv) - remis;

                    int vers = int.Parse(versement.Text);

                    int rend;
                    rend = res - vers;

                    int bef = res - (stock * qte);
                    int berf = (bef * 100) / res;

                    benefis.Text = Convert.ToString(bef);
                    benpercent.Text = Convert.ToString(berf);
                    rendu.Text = Convert.ToString(rend);
                    resultat.Text = Convert.ToString(res);

                }

            }
        }

        public void add()
        {
            if (designation.Text == "" && puv.Text == "" && Qte.Text == "" && client.Text == "" && mode.Text == "")
            {
                MessageBox.Show("Les entrées ne doivent pas être vides ! ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (heure.Text == "")
            {
                calcul();

                DateTime now = DateTime.Now;

                heure.Text = now.ToString("t");

                try
                {
                    // get client id
                    cmd1 = new SqlCommand(@"SELECT id FROM client WHERE Nom_client = '" + client.Text + "' ", cn);
                    cn.Open();
                    SqlDataReader sdr = cmd1.ExecuteReader();
                    while (sdr.Read())
                    {
                        textBox1.Text = sdr["id"].ToString();
                    }
                    cn.Close();
                }
                catch
                {
                    MessageBox.Show("S'il vous plaît ,premierement ajouter un fournisseur ! ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                try
                {
                    // get product id
                    cmd2 = new SqlCommand(@"SELECT id FROM produitFinal WHERE Désignation = '" + designation.Text + "' ", cn);
                    cn.Open();
                    SqlDataReader sdr3 = cmd2.ExecuteReader();
                    while (sdr3.Read())
                    {
                        textBox3.Text = sdr3["id"].ToString();
                    }
                    cn.Close();
                }
                catch
                {
                    MessageBox.Show("S'il vous plaît ,premierement ajouter un produit ! ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }


                // get the last factur id
                cmd3 = new SqlCommand("SELECT MAX(id) FROM facturevente", cn);
                cn.Open();
                try
                {

                    double result = (Convert.ToDouble(cmd3.ExecuteScalar()));
                    textBox2.Text = result.ToString();
                }
                catch
                {
                    textBox2.Text = "1";
                }
                finally
                {
                    cn.Close();
                }

                
                    // insert operation
                    cmd = new SqlCommand(@"INSERT INTO [dbo].[facturevente]
                                                   ([Date_facture]
                                                   ,[Versement]
                                                   ,[Remise]
                                                   ,[idclient])
                                             VALUES ('" + dtp.Value.Date.ToString("MM/dd /yyyy") + "' , '" + versement.Text + "' , '" + remise.Text + "' , '" + textBox1.Text + "'  )", cn);

                    cmd4 = new SqlCommand(@"INSERT INTO [dbo].[factureventeDe]
                                                       ([Mode_Vente]
                                                       ,[Date]
                                                       ,[Heure]
                                                       ,[Qte]
                                                       ,[prix_vente]
                                                       ,[Bénifis]
                                                       ,[Bénifis_percent]
                                                       ,[idproduit]
                                                       ,[idfacturevente])
                                                 VALUES('" + mode.Text + "' , '" + dtp.Value.Date.ToString("MM/dd /yyyy") + "' , '" + heure.Text + "' , '" + Qte.Text + "' ,'"+resultat.Text+"', '" + benefis.Text + "' , '" + benpercent.Text + "' , '" + textBox3.Text + "' , '" + textBox2.Text + "' )", cn);

                    cmd5 = new SqlCommand(@"INSERT INTO [dbo].[vente_countoire]
                                                           ([mode_vente]
                                                           ,[Désigniation]
                                                           ,[Quantité]
                                                           ,[Date]
                                                           ,[Heure]
                                                           ,[prix_vente]
                                                           ,[Remise]
                                                           ,[Versement]
                                                           ,[Client])
                                                     VALUES ('" + mode.Text + "' , '" + designation.Text + "' , '" + Qte.Text + "' , '" + dtp.Value.Date.ToString("MM/dd /yyyy") + "' , '" + heure.Text + "' , '"+resultat.Text+"','" + remise.Text + "' , '" + versement.Text + "' , '" + benefis.Text + "' )", cn);

                    cmd6 = new SqlCommand(@"UPDATE [dbo].[produitFinal] SET [Qté_initiale] = [Qté_initiale] - '" + Qte.Text + "' WHERE id = '" + textBox4.Text + "' ", cn);


                    cn.Open();
                    cmd.ExecuteNonQuery();
                    cmd1.ExecuteNonQuery();
                    cmd2.ExecuteNonQuery();
                    cmd3.ExecuteNonQuery();
                    cmd4.ExecuteNonQuery();
                    cmd5.ExecuteNonQuery();
                    cmd6.ExecuteNonQuery();
                    cn.Close();

                    var resul = MessageBox.Show("Votre Donné été ajouté correctement ! ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (resul == DialogResult.OK)
                    {
                        this.Close();
                    }
                    select();
                

                

            }
            else
            {
                calcul();

                try
                {
                    // get client id
                    cmd1 = new SqlCommand(@"SELECT id FROM client WHERE Nom_client = '" + client.Text + "' ", cn);
                    cn.Open();
                    SqlDataReader sdr = cmd1.ExecuteReader();
                    while (sdr.Read())
                    {
                        textBox1.Text = sdr["id"].ToString();
                    }
                    cn.Close();
                }
                catch
                {
                    MessageBox.Show("S'il vous plaît ,premierement ajouter un client ! ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                try
                {
                    // get product id
                    cmd2 = new SqlCommand(@"SELECT id FROM produitFinal WHERE Désignation = '" + designation.Text + "' ", cn);
                    cn.Open();
                    SqlDataReader sdr3 = cmd2.ExecuteReader();
                    while (sdr3.Read())
                    {
                        textBox3.Text = sdr3["id"].ToString();
                    }
                    cn.Close();
                }
                catch
                {
                    MessageBox.Show("S'il vous plaît ,premierement ajouter un produit ! ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }


                // get the last factur id
                cmd3 = new SqlCommand("SELECT MAX(id) FROM facturevente", cn);
                try
                {
                    cn.Open();
                    double result = (Convert.ToDouble(cmd3.ExecuteScalar()));
                    textBox2.Text = result.ToString();
                }
                catch
                {
                    textBox2.Text = "1";
                }
                finally
                {
                    cn.Close();
                }

                try
                {
                    // insert operation
                    cmd = new SqlCommand(@"INSERT INTO [dbo].[facturevente]
                                                   ([Date_facture]
                                                   ,[Versement]
                                                   ,[Remise]
                                                   ,[idclient])
                                             VALUES ('" + dtp.Value.Date.ToString("MM/dd /yyyy") + "' , '" + versement.Text + "' , '" + remise.Text + "' , '" + textBox1.Text + "'  )", cn);

                    cmd4 = new SqlCommand(@"INSERT INTO [dbo].[factureventeDe]
                                                       ([Mode_Vente]
                                                       ,[Date]
                                                       ,[Heure]
                                                       ,[Qte]
                                                       ,[prix_vente]
                                                       ,[Bénifis]
                                                       ,[Bénifis_percent]
                                                       ,[idproduit] 
                                                       ,[idfacturevente])
                                                 VALUES('" + mode.Text + "' , '" + dtp.Value.Date.ToString("MM/dd /yyyy") + "' , '" + heure.Text + "' , '" + Qte.Text + "' ,'" + resultat.Text + "', '" + benefis.Text + "' , '" + benpercent.Text + "' , '" + textBox3.Text + "' , '" + textBox2.Text + "' )", cn);

                    cmd5 = new SqlCommand(@"INSERT INTO [dbo].[vente_countoire]
                                                           ([mode_vente]
                                                           ,[Désigniation]
                                                           ,[Quantité]
                                                           ,[Date]
                                                           ,[Heure]
                                                           ,[prix_vente]
                                                           ,[Remise]
                                                           ,[Versement]
                                                           ,[Bénifis]
                                                           ,[Bénifis_percent]
                                                           ,[Client])
                                                     VALUES ('" + mode.Text + "' , '" + designation.Text + "' , '" + Qte.Text + "' , '" + dtp.Value.Date.ToString("MM/dd /yyyy") + "' , '" + heure.Text + "' , '" + resultat.Text + "','" + remise.Text + "' , '" + versement.Text + "' , '" + benefis.Text + "' , '" + benpercent.Text + "' , '" + client.Text + "')", cn);

                    cmd6 = new SqlCommand(@"UPDATE [dbo].[produitFinal] SET [Qté_initiale] = [Qté_initiale] - '" + Qte.Text + "' WHERE id = '" + textBox4.Text + "' ", cn);


                    cn.Open();
                    cmd.ExecuteNonQuery();
                    cmd1.ExecuteNonQuery();
                    cmd2.ExecuteNonQuery();
                    cmd3.ExecuteNonQuery();
                    cmd4.ExecuteNonQuery();
                    cmd5.ExecuteNonQuery();
                    cmd6.ExecuteNonQuery();
                    cn.Close();

                    var resul = MessageBox.Show("Votre Donné été ajouté correctement ! ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (resul == DialogResult.OK)
                    {
                        this.Close();
                    }
                    select();
                }

                catch
                {
                    MessageBox.Show("Les entrées ne doivent pas être vides ! ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }   
        // get data from dgv in textbox
        private void metroGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow row = metroGrid1.Rows[e.RowIndex];
                textBox4.Text = row.Cells[0].Value.ToString();
                designation.Text = row.Cells[1].Value.ToString();
                Qte.Text = row.Cells[2].Value.ToString();
                puv.Text = row.Cells[6].Value.ToString();
                prixprodstock.Text = row.Cells[4].Value.ToString();
            }

        }

        private void addbtn_Click(object sender, EventArgs e)
        {
            add();
        }

        private void cancelbtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void calculer_Click(object sender, EventArgs e)
        {
            calcul();
        }

        private void client_DoubleClick(object sender, EventArgs e)
        {
            Forms.choseclient ch = new choseclient();
            ch.ShowDialog();
        }
    }
}