Пример #1
0
        private void Frm_venda_Load(object sender, EventArgs e)
        {
            SqlConnection conexao = new SqlConnection
            {
                ConnectionString = Properties.Settings.Default.conexao
            };

            SqlCommand comando = new SqlCommand
            {
                CommandType = CommandType.Text,
                CommandText = "SELECT * FROM Caixa WHERE id = (SELECT MAX(id) FROM Caixa)",
                Connection  = conexao
            };

            conexao.Open();

            SqlDataReader dr = comando.ExecuteReader();

            try
            {
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        situacao = Convert.ToBoolean(dr["situacao"]);
                    }
                    conexao.Close();
                    if (situacao == false)
                    {
                        Frm_Abrir_Caixa Abertura = new Frm_Abrir_Caixa();
                        Abertura.Show();
                    }
                }
                else
                {
                    Frm_Abrir_Caixa Abertura = new Frm_Abrir_Caixa();
                    Abertura.Show();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Пример #2
0
        private void Btn_finalizar_venda_Click(object sender, EventArgs e)
        {
            SqlConnection conexao = new SqlConnection
            {
                ConnectionString = Properties.Settings.Default.conexao
            };

            SqlCommand comando_caixa = new SqlCommand
            {
                CommandType = CommandType.Text,
                CommandText = "SELECT * FROM Caixa WHERE id = (SELECT MAX(id) FROM Caixa)",
                Connection  = conexao
            };

            conexao.Open();

            SqlDataReader dr = comando_caixa.ExecuteReader();

            try
            {
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        situacao = Convert.ToBoolean(dr["situacao"]);
                        id_caixa = Convert.ToInt32(dr["id"]);
                    }
                    conexao.Close();
                    if (situacao == true)
                    {
                        if (id_pagamento == 1)
                        {
                            try
                            {
                                SqlCommand acrescimo_caixa = new SqlCommand("SP_ACRESCIMO_CAIXA", conexao);
                                acrescimo_caixa.CommandType = CommandType.StoredProcedure;
                                acrescimo_caixa.Connection  = conexao;

                                acrescimo_caixa.Parameters.Add("@acrescimo", SqlDbType.Money).Value = Txt_total_pagar.Text;

                                conexao.Open();
                                acrescimo_caixa.ExecuteNonQuery();
                                conexao.Close();
                            }
                            catch (Exception ac)
                            {
                                MessageBox.Show(ac.Message);
                            }
                        }

                        SqlCommand comando = new SqlCommand("SP_LANCAR_VENDA", conexao);
                        comando.CommandType = CommandType.StoredProcedure;
                        comando.Connection  = conexao;
                        conexao.Open();
                        try
                        {
                            comando.Parameters.Add("@total_venda", SqlDbType.Money).Value = Txt_total_pagar.Text;
                            comando.Parameters.Add("@data", SqlDbType.Date).Value         = Lbl_data_lancamento.Text;
                            comando.Parameters.Add("@hora", SqlDbType.Time).Value         = Lbl_hora.Text;
                            comando.Parameters.Add("@id_pagamento", SqlDbType.Int).Value  = id_pagamento;
                            comando.Parameters.Add("@id_caixa", SqlDbType.Int).Value      = id_caixa;

                            comando.ExecuteNonQuery();
                            conexao.Close();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }

                        SqlCommand comando2 = new SqlCommand();
                        comando.CommandText = "SELECT MAX(id) FROM venda";
                        comando.CommandType = CommandType.Text;
                        comando.Connection  = conexao;
                        conexao.Open();
                        try
                        {
                            SqlDataReader DR = comando.ExecuteReader();

                            while (DR.Read())
                            {
                                id_venda = DR.GetInt32(0);
                            }
                            conexao.Close();
                            MessageBox.Show("O id da venda é: " + id_venda.ToString());
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show(ex2.Message);
                        }

                        SqlCommand comando3 = new SqlCommand("SP_LANCAR_SAIDA", conexao);
                        comando3.CommandType = CommandType.StoredProcedure;
                        comando3.Connection  = conexao;
                        conexao.Open();

                        try
                        {
                            for (int i = 0; i < Dgv_produtos_vendas.Rows.Count - 1; i++)
                            {
                                comando3.Parameters.Add("@id_produto", SqlDbType.Int).Value = Dgv_produtos_vendas.Rows[i].Cells[0].Value;
                                comando3.Parameters.Add("@quantidade", SqlDbType.Int).Value = Dgv_produtos_vendas.Rows[i].Cells[2].Value;
                                comando3.Parameters.Add("@preco", SqlDbType.Money).Value    = Dgv_produtos_vendas.Rows[i].Cells[3].Value;
                                comando3.Parameters.Add("@total", SqlDbType.Money).Value    = Dgv_produtos_vendas.Rows[i].Cells[4].Value;
                                comando3.Parameters.Add("@desconto", SqlDbType.Money).Value = Txt_desconto.Text;
                                comando3.Parameters.Add("@id_venda", SqlDbType.Int).Value   = id_venda;

                                comando3.ExecuteNonQuery();
                                comando3.Parameters.Clear();
                            }
                            conexao.Close();
                            MessageBox.Show("Venda Finalizada !");
                            Dgv_produtos_vendas.Rows.Clear();
                            Txt_total_pagar.Text        = "";
                            Txt_valor_pago.Text         = "";
                            Txt_desconto.Text           = "";
                            Txt_troco.Text              = "";
                            Cbo_pagamento.SelectedIndex = 0;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("O Caixa encontra-se na situação de fechado !");
                        Frm_Abrir_Caixa Abertura = new Frm_Abrir_Caixa();
                        Abertura.Show();
                    }
                }
                else
                {
                    Frm_Abrir_Caixa Abertura = new Frm_Abrir_Caixa();
                    Abertura.Show();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }