public void ItemSellMethod(Product lProduct, Sell lSell, ItemSell lItemSell) { try { MySqlConnection connection = new MySqlConnection(Connection.lConnection); connection.Open(); string insert = "INSERT INTO item_venda(id_prod,Qtde_prod,Valor_Venda,total_item,id_venda) values ('" + lProduct.Codigo + "','" + lItemSell.itemQuantity + "','" + lProduct.PrecoVenda + "','" + lItemSell.itemTotal + "','" + lSell.sellId + "')"; MySqlCommand command = new MySqlCommand(insert, connection); MySqlDataReader myreader; myreader = command.ExecuteReader(); } catch (Exception ex) { throw new Exception(MessageBoxResult.lErrorCommand + ex.Message); } }
private void FinishMethod() { if (this.txtProduct.Text == string.Empty | this.txtEmployee.Text == string.Empty | this.txtClient.Text == string.Empty | this.txtGrossAmount.Text == string.Empty | this.txtDiscount.Text == string.Empty | this.txtNetAmount.Text == string.Empty) { MessageBox.Show("Algumas informações importantes para a venda ficaram de fora, ou foram preenchidos errados, revise.", Config.lAlert); txtEmployee.Focus(); } else if (MessageBox.Show("Deseja finalizar venda?", Config.lAlert, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Product lProduct = new Product(); Employee lEmployee = new Employee(); Client lClient = new Client(); Sell lSell = new Sell(); ItemSell lItemSell = new ItemSell(); sellQuery lsellQuery = new sellQuery(); { lEmployee.Id = Convert.ToInt16(txtEmployee.Text); lClient.Id = Convert.ToInt16(txtClient.Text); lProduct.Codigo = Convert.ToInt16(txtProduct.Text); lSell.sellGrossAmount = txtGrossAmount.Text; lSell.sellDiscount = txtDiscount.Text; lSell.sellNetAmount = txtNetAmount.Text; lSell.sellStatus = txtStatus.Text; lSell.sellDate = txtDate.Text; lSell.sellHour = txtHour.Text; lsellQuery.SellMethod(lProduct, lEmployee, lClient, lSell); } //exibir código da venda string result; MySqlConnection cn2 = new MySqlConnection(Connection.lConnection); MySqlCommand cmd = new MySqlCommand("SELECT max(ven_cod) FROM venda", cn2); cn2.Open(); MySqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { result = reader.GetString(0); txtId.Visible = true; label8.Visible = true; txtId.Text = result; } cn2.Close(); //configurar datagrid string[,] item = new string[counter, 5]; for (int l = 0; l < counter; l++) { for (int c = 0; c < 5; c++) { DataGridViewCell cell = null; foreach (DataGridViewCell selectedCell in dataGridView1.SelectedCells) { cell = selectedCell; break; } if (cell != null) { if (c == 0) { item[l, c] = dataGridView1.Rows[l].Cells[c].Value.ToString(); lProduct.Codigo = Convert.ToInt32(item[l, c]); } else if (c == 1) { item[l, c] = dataGridView1.Rows[l].Cells[2].Value.ToString(); lItemSell.itemQuantity = Convert.ToInt32(item[l, c]); } else if (c == 2) { item[l, c] = dataGridView1.Rows[l].Cells[3].Value.ToString(); lProduct.PrecoVenda = (item[l, c]); } else if (c == 3) { item[l, c] = dataGridView1.Rows[l].Cells[5].Value.ToString(); lItemSell.itemTotal = (item[l, c]); } else if (c == 4) { lSell.sellId = Convert.ToInt32(txtId.Text); } } } lsellQuery.ItemSellMethod(lProduct, lSell, lItemSell); //Atualiza estoque MySqlConnection cn3 = new MySqlConnection(Connection.lConnection); MySqlCommand cmd3 = new MySqlCommand("SELECT pro_quantidade_no_estoque FROM cadastro_remedios WHERE pro_codigo = '" + lProduct.Codigo + "'", cn3); cn3.Open(); MySqlDataReader reader3 = cmd3.ExecuteReader(); if (reader3.Read()) { int sotockQuantity = reader3.GetInt32(0); actualStock = sotockQuantity - lItemSell.itemQuantity; } try { MySqlConnection connection = new MySqlConnection(Connection.lConnection); connection.Open(); string update = "UPDATE cadastro_remedios SET pro_quantidade_no_estoque = '" + actualStock + "' WHERE pro_codigo = '" + lProduct.Codigo + "';"; MySqlCommand command = new MySqlCommand(update, connection); MySqlDataReader myreader; myreader = command.ExecuteReader(); } catch (Exception ex) { MessageBox.Show(MessageBoxResult.lErrorCommand + ex.Message); errorQuery lerrorQuery = new errorQuery(); lerrorQuery.AddError(Principal.lUser, MessageBoxResult.lErrorCommand, ex.Message.Replace("'", ""), DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"), "Venda"); } } MessageBox.Show("Venda Finalizada com Sucesso", "Sucesso!"); //limpar a tela após finalizar venda if (Convert.ToDouble(txtNetAmount.Text) > 850.00) { try { Email = new MailMessage(); Email.To.Add(new MailAddress(Credentials.lGerenciaAdress)); Email.From = (new MailAddress(Credentials.lAdress)); Email.Subject = Config.lAlert; Email.IsBodyHtml = true; Email.Body = "FarmaLife Enterprises 2018 ®</br>A compra com o código: " + txtId.Text + " teve o valor de " + txtNetAmount.Text.Replace(",", ".") + ", que foi realizada por " + txtNameEmployee.Text + " para o cliente " + txtNameCliente.Text + ", no dia " + DateTime.Now + " o valor em questão foi acima do padrão, favor verificar. </br> <i> Não responder esse e-mail</i></br>"; SmtpClient sell2 = new SmtpClient(Credentials.lSmtpLive, Credentials.lSmtpLivePort); using (sell2) { sell2.Credentials = new System.Net.NetworkCredential(Credentials.lAdress, Credentials.lPassword); sell2.EnableSsl = true; sell2.Send(Email); } this.Close(); Venda sell = new Venda(); sell.Show(); } catch (Exception ex) { MessageBox.Show(ex.Message); errorQuery lerrorQuery = new errorQuery(); MessageBox.Show(MessageBoxResult.lEmailError, Config.lAlert); lerrorQuery.AddError(Principal.lUser, MessageBoxResult.lEmailError, ex.Message.Replace("'", ""), DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"), "Venda"); } } else { this.Close(); Venda sell = new Venda(); sell.Show(); } } }