Levelcode-IBRCAD/IBRCAD/frm_cadfornecedores.cs

285 lines
9.7 KiB
C#

using BLL;
using DAL;
using FFT;
using Modelo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IBRCAD
{
public partial class frm_cadfornecedores : Form
{
private string operacao = "";
public void alterabotoes(int op)
{
pn_data.Enabled = false;
btn_inserir.Enabled = false;
btn_localizar.Enabled = false;
btn_alterar.Enabled = false;
btn_excluir.Enabled = false;
btn_cancel.Enabled = false;
btn_save.Enabled = false;
if (op == 1)
{
btn_inserir.Enabled = true;
btn_localizar.Enabled = true;
}
if (op == 2)
{
pn_data.Enabled = true;
btn_save.Enabled = true;
btn_cancel.Enabled = true;
}
if (op == 3)
{
btn_alterar.Enabled = true;
btn_excluir.Enabled = true;
btn_cancel.Enabled = true;
}
}//end alterabotões
private void cleanDisplay()
{
this.pn_data.Enabled = false;
this.txt_cod.Text = string.Empty;
this.txt_forname.Text = string.Empty;
this.txt_raz.Text = string.Empty;
this.txt_cnpj.Text = string.Empty;
this.lbl_errocnpj.Visible = false;
this.txt_ie.Text = string.Empty;
this.txt_cep.Text = string.Empty;
this.txt_rua.Text = string.Empty;
this.txt_numero.Text = string.Empty;
this.txt_cidade.Text = string.Empty;
this.txt_estado.Text = string.Empty;
this.txt_bairro.Text = string.Empty;
this.txt_email.Text = string.Empty;
this.txt_fone.Text = string.Empty;
this.txt_celular.Text = string.Empty;
}//end cleandisplay
public enum Campo
{
CPF = 1,
CNPJ = 2,
}//end enum campo
public void Formatar(Campo Valor, TextBox txtTexto)
{
switch (Valor)
{
case Campo.CPF:
txtTexto.MaxLength = 14;
if (txtTexto.Text.Length == 3)
{
txtTexto.Text = txtTexto.Text + ".";
txtTexto.SelectionStart = txtTexto.Text.Length + 1;
}
else if (txtTexto.Text.Length == 7)
{
txtTexto.Text = txtTexto.Text + ".";
txtTexto.SelectionStart = txtTexto.Text.Length + 1;
}
else if (txtTexto.Text.Length == 11)
{
txtTexto.Text = txtTexto.Text + "-";
txtTexto.SelectionStart = txtTexto.Text.Length + 1;
}
break;
case Campo.CNPJ:
txtTexto.MaxLength = 18;
if (txtTexto.Text.Length == 2 || txtTexto.Text.Length == 6)
{
txtTexto.Text = txtTexto.Text + ".";
txtTexto.SelectionStart = txtTexto.Text.Length + 1;
}
else if (txtTexto.Text.Length == 10)
{
txtTexto.Text = txtTexto.Text + "/";
txtTexto.SelectionStart = txtTexto.Text.Length + 1;
}
else if (txtTexto.Text.Length == 15)
{
txtTexto.Text = txtTexto.Text + "-";
txtTexto.SelectionStart = txtTexto.Text.Length + 1;
}
break;
}
}//end formatar campo
public frm_cadfornecedores()
{
InitializeComponent();
}
private void btn_inserir_Click(object sender, EventArgs e)
{
this.operacao = "inserir";
this.alterabotoes(2);
}//btn inserir
private void btn_localizar_Click(object sender, EventArgs e)
{
frm_consultafornecedor f = new frm_consultafornecedor();
f.ShowDialog();
if (f.codigo != 0)
{
DALLconexao cx = new DALLconexao(DadosDaConexao.StringDeConexao);
BLLFornecedor bll = new BLLFornecedor(cx);
ModeloFornecedor modelo = bll.CarregaModeloFornecedor(f.codigo);
txt_cod.Text = modelo.ForCod.ToString();
txt_forname.Text = modelo.ForNome;
txt_raz.Text = modelo.ForRSocial;
txt_cnpj.Text = modelo.ForCnpj;
txt_ie.Text = modelo.ForIe;
txt_cep.Text = modelo.ForCep;
txt_estado.Text = modelo.ForEstado;
txt_cidade.Text = modelo.ForCidade;
txt_rua.Text = modelo.ForEndereco;
txt_numero.Text = modelo.ForEndNumero;
txt_bairro.Text = modelo.ForBairro;
txt_email.Text = modelo.ForEmail;
txt_fone.Text = modelo.ForFone;
txt_celular.Text = modelo.ForCelular;
this.alterabotoes(3);
}
else
{
this.cleanDisplay();
this.alterabotoes(1);
}
f.Dispose();
}//end localizar
private void btn_alterar_Click(object sender, EventArgs e)
{
this.alterabotoes(2);
this.operacao = "alterar";
}//end alterar
private void btn_excluir_Click(object sender, EventArgs e)
{
try
{
DialogResult d = MessageBox.Show("Deseja excluir o registro?", "Aviso", MessageBoxButtons.YesNo);
if (d.ToString() == "Yes")
{
DALLconexao cx = new DALLconexao(DadosDaConexao.StringDeConexao);
BLLFornecedor bll = new BLLFornecedor(cx);
bll.Excluir(Convert.ToInt32(txt_cod.Text));
this.cleanDisplay();
this.alterabotoes(1);
}
}
catch
{
MessageBox.Show("Impossível excluir o registro. \n O registro esta sendo utilizado em outro local.");
this.alterabotoes(3);
}
}//end excluir
private void btn_save_Click(object sender, EventArgs e)
{
try
{
//leitura dos dados
ModeloFornecedor modelo = new ModeloFornecedor();
modelo.ForNome = txt_forname.Text;
modelo.ForRSocial = txt_raz.Text;
modelo.ForCnpj = txt_cnpj.Text;
modelo.ForIe = txt_ie.Text;
modelo.ForCep = txt_cep.Text;
modelo.ForCidade = txt_cidade.Text;
modelo.ForEstado = txt_estado.Text;
modelo.ForEndereco = txt_rua.Text;
modelo.ForEndNumero = txt_numero.Text;
modelo.ForBairro = txt_bairro.Text;
modelo.ForEmail = txt_email.Text;
modelo.ForFone = txt_fone.Text;
modelo.ForCelular = txt_celular.Text;
//obj para gravar os dados no banco
DALLconexao cx = new DALLconexao(DadosDaConexao.StringDeConexao);
BLLFornecedor bll = new BLLFornecedor(cx);
if (this.operacao == "inserir")
{
//cadastrar uma categoria
bll.Incluir(modelo);
MessageBox.Show("Cadastro efetuado: Código " + modelo.ForCod.ToString());
}
else
{
//alterar uma categoria
modelo.ForCod = Convert.ToInt32(txt_cod.Text);
bll.Alterar(modelo);
MessageBox.Show("Cadastro alterado");
}
this.cleanDisplay();
this.alterabotoes(1);
}
catch (Exception erro)
{
MessageBox.Show(erro.Message);
}
}//end save
private void btn_cancel_Click(object sender, EventArgs e)
{
this.alterabotoes(1);
this.cleanDisplay();
}//end btn cancel
private void txt_cnpj_Leave(object sender, EventArgs e)
{
lbl_errocnpj.Visible = false;
if (VerifyCpf.IsCnpj(txt_cnpj.Text) == false)
{
lbl_errocnpj.Visible = true;
}
}//end txt_cnpj_Leave
private void txt_cnpj_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != (char)8)
{
Campo edit = Campo.CNPJ;
Formatar(edit, txt_cnpj);
}
}// end txt_cnpj_KeyPress
private void txt_cep_Leave(object sender, EventArgs e)
{
if (BLLVerifyCep.verificaCEP(txt_cep.Text) == true)
{
txt_bairro.Text = BLLVerifyCep.bairro;
txt_estado.Text = BLLVerifyCep.estado;
txt_cidade.Text = BLLVerifyCep.cidade;
txt_rua.Text = BLLVerifyCep.endereco;
}
}//end txt_cep_Leave
private void pb_exit_Click(object sender, EventArgs e)
{
this.Close();
}//end close
private void frm_cadfornecedores_Load(object sender, EventArgs e)
{
this.alterabotoes(1);
this.cleanDisplay();
}//end load form
}
}