private void BtnCadastrar_Click(object sender, EventArgs e) { if (TextNomeCliente.Text.Length == 0 || IdClienteVenda.Length == 0) { MessageBox.Show(this, "Favor selecionar um cliente !", null, MessageBoxButtons.OK, MessageBoxIcon.Error); } if (TextParcelas.Text.Length == 0) { MessageBox.Show(this, "Favor preencher a quantidade de parcelas !", null, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { var TotalDePecas = 0; var TotalCompra = new decimal(); var Existe = false; var ProdutoInexistente = new ArrayList(); for (int i = 0; i < GridProdutos.RowCount; i++) { Existe = Venda.VerificaDisponibilidade(GridProdutos.Rows[i].Cells[0].Value.ToString(), GridProdutos.Rows[i].Cells[4].Value.ToString()); TotalCompra = TotalCompra + Convert.ToDecimal(GridProdutos.Rows[i].Cells[3].Value.ToString()) * Convert.ToInt32(GridProdutos.Rows[i].Cells[4].Value); if (!Existe) { ProdutoInexistente.Add(GridProdutos.Rows[i].Cells[2].Value.ToString()); } } if (TextDescontos.Value > 0) { TotalCompra = TotalCompra - ((TotalCompra * TextDescontos.Value) / 100); } if (ProdutoInexistente.Count > 0) { for (int j = 0; j < ProdutoInexistente.Count; j++) { MessageBox.Show(this, "A quantidade em estoque do produto " + ProdutoInexistente[j] + " não é suficiente para realizar esta venda!", null, MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (TextValorVenda.DecimalValue != (decimal)TotalCompra) { MessageBox.Show(this, "O valor total da compra não é igual a soma do custo dos produtos!", null, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { var IdVenda = Venda.CadastrarVenda(IdClienteVenda, TextValorVenda.DecimalValue.ToString(), ComboFormaPagamento.SelectedValue.ToString(), TextDescontos.Text, TextParcelas.Text); for (int i = 0; i < GridProdutos.RowCount; i++) { try { for (int j = 0; j < Convert.ToInt32(GridProdutos.Rows[i].Cells[4].Value); j++) { Venda.CadastrarProdutoVenda(IdVenda, GridProdutos.Rows[i].Cells[0].Value.ToString(), GridProdutos.Rows[i].Cells[3].Value.ToString()); Venda.RemoveProdutoEstoque(GridProdutos.Rows[i].Cells[0].Value.ToString(), "1"); } } catch { } } MessageBox.Show(this, "Venda cadastrada com sucesso!", null, MessageBoxButtons.OK, MessageBoxIcon.Information); TextNomeCliente.Text = ""; IdClienteVenda = ""; TextParcelas.Text = ""; TextValorVenda.Text = "0"; TextDescontos.Text = "0"; GridProdutos.Rows.Clear(); } } }