public F_main(F_login f, string work) { InitializeComponent(); workid = work; f_login = f; f_produtos = new F_produtos(this); f_financas = new F_financas(this); f_funcionarios = new F_funcionarios(this); f_clientes = new F_clientes(this); }
private void btn_regvenda_Click(object sender, EventArgs e) { if (produtos.Rows.Count == 0) { MessageBox.Show("Não existem produtos para comprar"); } else if (tb_clienteid.Text.Equals("")) { MessageBox.Show("Insira um Cliente"); } else { DataTable dt = obterCliente(cn, tb_clienteid.Text); if (dt.Rows.Count != 1) { MessageBox.Show("Cliente inexistente"); } else { int tam = produtos.Rows.Count; //criar uma nova venda DateTime h = DateTime.Today; string[] ho = h.ToString("d").Split('/'); string hoje = ""; if (ho[0].Length == 1 && ho[1].Length == 1) { ho[0] = "0" + ho[0]; ho[1] = "0" + ho[1]; } else if (ho[0].Length == 1 && ho[1].Length == 2) { ho[0] = "0" + ho[0]; } else if (ho[0].Length == 2 && ho[1].Length == 1) { ho[1] = "0" + ho[1]; } hoje = ho[2] + ho[1] + ho[0]; string sql = "EXEC p_Create_Venda @data_venda = '" + hoje + "', @loja_code = " + loja_code + ", @work_ID = " + workid + ", @num_ID = " + tb_clienteid.Text; SqlDataAdapter da = null; dt = new DataTable(); var cmd = cn.CreateCommand(); try { cmd.CommandText = sql; da = new SqlDataAdapter(cmd.CommandText, cn); da.Fill(dt); } catch (Exception ex) { } // pegar o id dessa venda com o @@identity sql = "SELECT ID_venda FROM SPORT_SHOP.Venda WHERE ID_venda = @@IDENTITY"; da = null; dt = new DataTable(); cmd = cn.CreateCommand(); try { cmd.CommandText = sql; da = new SqlDataAdapter(cmd.CommandText, cn); da.Fill(dt); } catch (Exception ex) { } string a; int b; string venda = dt.Rows[0][0].ToString(); decimal precototal = 0; int totalprod = 0; for (int i = 0; i < tam; i++) { //adicionar os produtos ao inclui sql = "INSERT INTO SPORT_SHOP.INCLUI(ID_venda, produto_ID, quantidadeProduto) VALUES ( " + venda + ", " + produtos.Rows[i][0] + ", " + produtos.Rows[i][5] + ")"; da = null; dt = new DataTable(); cmd = cn.CreateCommand(); try { cmd.CommandText = sql; da = new SqlDataAdapter(cmd.CommandText, cn); da.Fill(dt); } catch (Exception ex) { } sql = "EXEC p_retiraStock @produto_ID = " + produtos.Rows[i][0] + ", @loja_code =" + loja_code + ", @quantidade =" + produtos.Rows[i][5]; da = null; dt = new DataTable(); cmd = cn.CreateCommand(); try { cmd.CommandText = sql; da = new SqlDataAdapter(cmd.CommandText, cn); da.Fill(dt); } catch (Exception ex) { } a = produtos.Rows[i][5].ToString(); b = Int32.Parse(a); totalprod += Int32.Parse(a); a = produtos.Rows[i][4].ToString(); precototal += Decimal.Parse(a) * b; } sql = "UPDATE SPORT_SHOP.Venda SET precoTotal = " + precototal.ToString(new CultureInfo("en-US")) + ", quantidade = " + totalprod.ToString() + "WHERE ID_venda = " + venda; da = null; dt = new DataTable(); cmd = cn.CreateCommand(); try { cmd.CommandText = sql; da = new SqlDataAdapter(cmd.CommandText, cn); da.Fill(dt); } catch (Exception ex) { } produtos.Clear(); dgv_addvenda.DataSource = produtos; tb_clienteid.Text = ""; tb_produto.Text = ""; nud_quantprod.Value = 0; dgv_addvenda.Columns[0].Width = 83; dgv_addvenda.Columns[1].Width = 106; dgv_addvenda.Columns[2].Width = 86; dgv_addvenda.Columns[3].Width = 80; dgv_addvenda.Columns[4].Width = 69; dgv_addvenda.Columns[5].Width = 70; dgv_vendas.DataSource = obterVendas(cn); dgv_vendas.Columns[0].Width = 110; dgv_vendas.Columns[1].Width = 122; dgv_vendas.Columns[2].Width = 119; dgv_vendas.Columns[3].Width = 120; dgv_vendas.Columns[4].Width = 130; dgv_vendas.Columns[5].Width = 130; MessageBox.Show("Venda efetuada com sucesso!"); f_financas = new F_financas(this); } } }