//INSERIR public static void Inserir(MTaxaEmbarque item) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString; try { conexao.Open(); } catch { throw new Exception("O banco de dados não esta conectado, para utilizar o sistema primeiro inicie o gerenciador de Banco de Dados!! conexão com o SGBD"); } SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = "INSERT INTO TBTaxaDeEmbarque (Data, " + "Quantidade, ValorTarifa, ValorTotal, CODEmpresaID) VALUES(@Data, " + "@Quantidade, @ValorTarifa, @ValorTotal, @CODEmpresaID)"; SqlParameter param = new SqlParameter("@Data", SqlDbType.VarChar); param.Value = item.Data; comando.Parameters.Add(param); param = new SqlParameter("@Quantidade", SqlDbType.Int); param.Value = item.Quantidade; comando.Parameters.Add(param); param = new SqlParameter("@ValorTarifa", SqlDbType.Decimal); param.Value = item.ValorTarifa; comando.Parameters.Add(param); param = new SqlParameter("@ValorTotal", SqlDbType.Decimal); param.Value = item.ValorTotal; comando.Parameters.Add(param); param = new SqlParameter("@CODEmpresaID", SqlDbType.Int); param.Value = item.EmpresaID; comando.Parameters.Add(param); try { comando.ExecuteNonQuery(); } catch { throw new Exception("Erro no Cadastro no Banco de Dados!"); } finally { conexao.Close(); } }
//BOTÃO LIMPAR private void btnLimparPesquisaTaxa_Click(object sender, EventArgs e) { lbValorTotal.Text = string.Empty; MTaxaEmbarque taxa = new MTaxaEmbarque(); cbSelecionarEmpresaPesquisar.SelectedIndex = 0; txtData.Text = (""); dtPesquisarTaxa.DataSource = null; }
//INSERIR --- CONCLUIR VERIFICAÇÕES public static void Inserir(MTaxaEmbarque item) { //REGEX PARA VALIDAR NUMEROS Regex validarNumero = new Regex(@"^\d+$"); //Regex validarData = new Regex(@"(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/((1[2-9]|[2-9][0-9])[0-9]{2})"); //Regex validarData = new Regex(@"(\d{2}\/\d{2}\/\d{4} \d{2}:\d{2})"); if (item == null) { throw new Exception("Taxa de Embarque esta incorreta"); } if (item.EmpresaID == 0) { throw new Exception("É necessário selecionar uma empresa"); } if (item.Data == "00/00/0000" || item.Data == "/ /") { throw new Exception("A data esta incorreta"); } if (item.Quantidade <= 0) { throw new Exception("Campo Quantidade não pode ser negativo ou igual a zero"); } if (item.ValorTarifa <= 0) { throw new Exception("Campo Valor Tarifa não pode ser negativo ou igual a zero"); } //VALIDAR DATA //if (validarData.IsMatch(item.Data)) //{ // throw new Exception("A data esta incorreta"); //} //corrigir aqui - esta invalidando tudo - alterar o regex //if (validarData.IsMatch(item.Data)) //{ // throw new Exception("A data esta incorreta"); //} try { DTaxaEmbarque.Inserir(item); } catch { throw; } }
//OBTER public static MTaxaEmbarque Obter(MTaxaEmbarque item) { MTaxaEmbarque retorno = null; if (item != null) { retorno = DTaxaEmbarque.Obter(item); } return(retorno); }
//OBTER public static MTaxaEmbarque Obter(MTaxaEmbarque item) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString; try { conexao.Open(); } catch { throw new Exception("O banco de dados não esta conectado, para utilizar o sistema primeiro inicie o gerenciador de Banco de Dados!! conexão com o SGBD"); } SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = " SELECT id, Data, Quantidade, " + "ValorTarifa, ValorTotal, CODEmpresaID " + "FROM TBTaxaDeEmbarque WHERE id = @id"; SqlParameter param = new SqlParameter("@id", SqlDbType.Int); param.Value = item.id; comando.Parameters.Add(param); SqlDataReader reader = comando.ExecuteReader(); MTaxaEmbarque retorno = null; if (reader.Read()) { retorno = new MTaxaEmbarque(); retorno.id = int.Parse(reader["id"].ToString()); retorno.Data = reader["data"].ToString(); retorno.Quantidade = int.Parse(reader["Quantidade"].ToString()); retorno.ValorTarifa = Decimal.Parse(reader["ValorTarifa"].ToString()); retorno.ValorTotal = Decimal.Parse(reader["ValorTotal"].ToString()); retorno.EmpresaID = int.Parse(reader["CODEmpresaID"].ToString()); } reader.Close(); conexao.Close(); return(retorno); }
//EXCLUIR public static void Excluir(MTaxaEmbarque item) { if (item.id == 0) { throw new Exception("Nome da Empresa Inválida"); } try { DTaxaEmbarque.Excluir(item); } catch { throw; } }
//PESQUISAR public static List <MTaxaEmbarque> Pesquisar(MTaxaEmbarque item) { List <MTaxaEmbarque> retorno = null; if (item.id == 0) { retorno = DTaxaEmbarque.Pesquisar(item); //RETORNO SE NÃO HOUVER NENHUM CADASTRO if (retorno == null) { throw new Exception("A Pesquisa não retornou nenhum cadastro!"); } } return(retorno); }
//BOTÃO EDITAR private void btnEditar_Click(object sender, EventArgs e) { if (dtPesquisarTaxa.SelectedRows != null && dtPesquisarTaxa.SelectedRows.Count > 0) { MTaxaEmbarque taxa = new MTaxaEmbarque(); taxa.id = int.Parse(dtPesquisarTaxa.SelectedRows[0]. Cells["idDataGridViewTextBoxColumn"].Value.ToString()); Form v = new VTaxaCadastrar(taxa); v.ShowDialog(); btnPesquisarTaxa_Click(null, null); } }
//BOTÃO SALVAR private void btnSalvar_Click(object sender, EventArgs e) { bool dadosValidos = true; if (dadosValidos) { MTaxaEmbarque taxa = new MTaxaEmbarque(); taxa.EmpresaID = int.Parse(cbSelecionarEmpresa.SelectedValue.ToString()); taxa.Data = txtData.Text.Trim(); try { taxa.Quantidade = int.Parse(txtQuantidade.Text.Trim()); taxa.ValorTarifa = Decimal.Parse(txtValorTarifa.Text.Trim()); taxa.ValorTotal = Decimal.Parse(txtValorTotal.Text.Trim()); } catch { MessageBox.Show("Existem campos obrigatórios sem preenchimento!"); } try { if (atual != null) { taxa.id = atual.id; CTaxaEmbarque.Atualizar(taxa); MessageBox.Show("Campo alterado com sucesso.", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { CTaxaEmbarque.Inserir(taxa); MessageBox.Show("Campo salvo com sucesso.", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } this.Close(); } catch (Exception Ex) { MessageBox.Show(Ex.Message); } } }
//BOTÃO EXCLUIR private void btnExcluir_Click(object sender, EventArgs e) { if (dtPesquisarTaxa.SelectedRows != null && dtPesquisarTaxa.SelectedRows.Count > 0) { DialogResult excluirTaxa = MessageBox.Show("Deseja Excluir esta Empresa?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (excluirTaxa == DialogResult.Yes) { MTaxaEmbarque taxa = (MTaxaEmbarque)dtPesquisarTaxa.SelectedRows[0]. DataBoundItem; bool sucesso = false; try { CTaxaEmbarque.Excluir(taxa); sucesso = true; } catch { MessageBox.Show("Empresa já possui taxa de Embarque, cadastrada, sera necessário exclui-las", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (sucesso) { MessageBox.Show("Taxa de Embarque Excluida com sucesso", "", MessageBoxButtons.OK, MessageBoxIcon.Information); List <MTaxaEmbarque> lista = (List <MTaxaEmbarque>)dtPesquisarTaxa.DataSource; lista.Remove(taxa); dtPesquisarTaxa.DataSource = null; dtPesquisarTaxa.DataSource = lista; } } } }
//BOTÃO PESQUISAR private void btnPesquisarTaxa_Click(object sender, EventArgs e) { lbValorTotal.Text = string.Empty; MTaxaEmbarque taxa = new MTaxaEmbarque(); taxa.EmpresaID = int.Parse(cbSelecionarEmpresaPesquisar.SelectedValue.ToString()); taxa.Data = txtData.Text; dtPesquisarTaxa.DataSource = null; try { dtPesquisarTaxa.DataSource = CTaxaEmbarque.Pesquisar(taxa); } catch { MessageBox.Show("Não foi encontrados dados de acordo com a pesquisa realizada", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//EXCLUIR public static void Excluir(MTaxaEmbarque item) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString; try { conexao.Open(); } catch { throw new Exception("O banco de dados não esta conectado, para utilizar o sistema primeiro inicie o gerenciador de Banco de Dados!! conexão com o SGBD"); } SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = "DELETE FROM TBTaxaDeEmbarque where id = @id"; SqlParameter param = new SqlParameter("@id", SqlDbType.Int); param.Value = item.id; comando.Parameters.Add(param); try { comando.ExecuteNonQuery(); } catch { throw; } finally { conexao.Close(); } }
//ATUALIZAR public static void Atualizar(MTaxaEmbarque item) { if (item == null) { throw new Exception("Objeto PESSOA inválido"); } if (item.EmpresaID == 0) { throw new Exception("É necessário selecionar uma empresa"); } if (item.Data == "00/00/0000" || item.Data == "/ /") { throw new Exception("A data esta incorreta"); } if (item.Quantidade <= 0) { throw new Exception("Campo Quantidade não pode ser negativo ou igual a zero"); } if (item.ValorTarifa <= 0) { throw new Exception("Campo Valor Tarifa não pode ser negativo ou igual a zero"); } try { DTaxaEmbarque.Atualizar(item); } catch { throw; } }
//PESQUISAR public static List <MTaxaEmbarque> Pesquisar(MTaxaEmbarque item) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString; try { conexao.Open(); } catch { throw new Exception("O banco de dados não esta conectado, para utilizar o sistema primeiro inicie o gerenciador de Banco de Dados!! conexão com o SGBD"); } SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = " SELECT TE.id, TE.Data, " + " TE.Quantidade, TE.ValorTarifa, EM.NomeEmpresa, TE.ValorTotal, TE.CODEmpresaID FROM " + "TBTaxaDeEmbarque AS TE JOIN TBEmpresa AS EM ON TE.CODEmpresaID = EM.id WHERE 1 = 1 "; if (item.EmpresaID != 0) { comando.CommandText += " AND TE.CODEmpresaID = @CODEmpresaID "; SqlParameter param = new SqlParameter("@CODEmpresaID", SqlDbType.Int); param.Value = item.EmpresaID; comando.Parameters.Add(param); } if (item.Data != " / /") //melhorar esta pesquisa aqui posteriormente { comando.CommandText += " AND TE.Data = @Data "; SqlParameter param = new SqlParameter("@Data", SqlDbType.VarChar); param.Value = item.Data; comando.Parameters.Add(param); } SqlDataReader reader = comando.ExecuteReader(); List <MTaxaEmbarque> retorno = null; while (reader.Read()) { if (retorno == null) { retorno = new List <MTaxaEmbarque>(); } MTaxaEmbarque empresa = new MTaxaEmbarque(); empresa.id = int.Parse(reader["id"].ToString()); empresa.Data = reader["data"].ToString(); empresa.Quantidade = int.Parse(reader["Quantidade"].ToString()); empresa.ValorTarifa = Decimal.Parse(reader["ValorTarifa"].ToString()); empresa.ValorTotal = Decimal.Parse(reader["ValorTotal"].ToString()); empresa.NomeDaEmpresa = reader["NomeEmpresa"].ToString(); empresa.EmpresaID = int.Parse(reader["CODEmpresaID"].ToString()); retorno.Add(empresa); } reader.Close(); conexao.Close(); return(retorno); }
//ATUALIZAR public static void Atualizar(MTaxaEmbarque item) { SqlConnection conexao = new SqlConnection(); conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString; try { conexao.Open(); } catch { throw new Exception("O banco de dados não esta conectado, para utilizar o sistema primeiro inicie o gerenciador de Banco de Dados!! conexão com o SGBD"); } SqlCommand comando = new SqlCommand(); comando.Connection = conexao; comando.CommandText = "UPDATE TBTaxaDeEmbarque SET data = @data, " + "Quantidade = @Quantidade, ValorTarifa = @ValorTarifa, " + "ValorTotal = @ValorTotal, CODEmpresaID = @CODEmpresaID " + " WHERE id = @id "; SqlParameter param = new SqlParameter("@id", SqlDbType.Int); param.Value = item.id; comando.Parameters.Add(param); param = new SqlParameter("@data", SqlDbType.VarChar); param.Value = item.Data; comando.Parameters.Add(param); param = new SqlParameter("@Quantidade", SqlDbType.Int); param.Value = item.Quantidade; comando.Parameters.Add(param); param = new SqlParameter("@ValorTarifa", SqlDbType.Decimal); param.Value = item.ValorTarifa; comando.Parameters.Add(param); param = new SqlParameter("@ValorTotal", SqlDbType.Decimal); param.Value = item.ValorTotal; comando.Parameters.Add(param); param = new SqlParameter("@CODEmpresaID", SqlDbType.Int); param.Value = item.EmpresaID; comando.Parameters.Add(param); try { comando.ExecuteNonQuery(); } catch { throw new Exception("O comando não pode ser executado"); } finally { conexao.Close(); } }
public VTaxaCadastrar(MTaxaEmbarque item) { InitializeComponent(); item = CTaxaEmbarque.Obter(item); atual = item; }