Levelcode-IBRCAD/IBRCAD/frm_cadclientes.cs

362 lines
12 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;
using static FFT.VerifyCep;
namespace IBRCAD
{
public partial class frm_cadclientes : Form
{
public string operacao;
private void dislplayclean()
{
this.txt_cod.Text = string.Empty;
this.txt_clinome.Text = string.Empty;
this.txt_cliraz.Text = string.Empty;
this.txt_cpf.Text = string.Empty;
this.txt_clirg.Text = string.Empty;
this.txt_clicep.Text = string.Empty;
this.txt_rua.Text = string.Empty;
this.txt_num.Text = string.Empty;
this.txt_bairro.Text = string.Empty;
this.txt_cidade.Text = string.Empty;
this.txt_estado.Text = string.Empty;
this.txt_cliemail.Text = string.Empty;
this.txt_fone.Text = string.Empty;
this.txt_celular.Text = string.Empty;
}
public void alterabotoes(int op)
{
pn_dados.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_dados.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 formatar campos
public enum Campo
{
CPF = 1,
CNPJ = 2,
CEP = 3,
}//end 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;
case Campo.CEP:
txtTexto.MaxLength = 9;
if (txtTexto.Text.Length == 5)
{
txtTexto.Text = txtTexto.Text + "-";
txtTexto.SelectionStart = txtTexto.Text.Length + 1;
}
break;
}
}//end formatar campos
public frm_cadclientes()
{
InitializeComponent();
}
private void btn_inserir_Click(object sender, EventArgs e)
{
this.operacao = "inserir";
this.alterabotoes(2);
}//end btn inserir
private void btn_localizar_Click(object sender, EventArgs e)
{
frm_consultacliente f = new frm_consultacliente();
f.ShowDialog();
if (f.codigo != 0)
{
DALLconexao cx = new DALLconexao(DadosDaConexao.StringDeConexao);
BLLCliente bll = new BLLCliente(cx);
ModeloCliente modelo = bll.CarregaModeloCliente(f.codigo);
txt_cod.Text = modelo.CliCod.ToString();
if (modelo.CliTipo == "Física")
{
rb_fisica.Checked = true;
}
else
{
rb_juridica.Checked = true;
}
txt_clinome.Text = modelo.CliNome;
txt_cliraz.Text = modelo.CliRSocial;
txt_cpf.Text = modelo.CliCpfCnpj;
txt_clirg.Text = modelo.CliRgIe;
txt_clicep.Text = modelo.CliCep;
txt_estado.Text = modelo.CliEstado;
txt_cidade.Text = modelo.CliCidade;
txt_rua.Text = modelo.CliEndereco;
txt_num.Text = modelo.CliEndNumero;
txt_bairro.Text = modelo.CliBairro;
txt_cliemail.Text = modelo.CliEmail;
txt_fone.Text = modelo.CliFone;
txt_celular.Text = modelo.CliCelular;
this.alterabotoes(3);
}
else
{
this.dislplayclean();
this.alterabotoes(1);
}
f.Dispose();
}//btn localizar
private void btn_alterar_Click(object sender, EventArgs e)
{
this.alterabotoes(2);
this.operacao = "alterar";
}//btn 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);
BLLCliente bll = new BLLCliente(cx);
bll.Excluir(Convert.ToInt32(txt_cod.Text));
this.dislplayclean();
this.alterabotoes(1);
}
}
catch
{
MessageBox.Show("Impossível excluir o registro. \n O registro esta sendo utilizado em outro local.");
this.alterabotoes(3);
}
}//btn excluir
private void btn_save_Click(object sender, EventArgs e)
{
try
{
//leitura dos dados
ModeloCliente modelo = new ModeloCliente();
modelo.CliNome = txt_clinome.Text;
modelo.CliRSocial = txt_cliraz.Text;
modelo.CliCpfCnpj = txt_cpf.Text;
modelo.CliRgIe = txt_clirg.Text;
modelo.CliCep = txt_clicep.Text;
modelo.CliCidade = txt_cidade.Text;
modelo.CliEstado = txt_estado.Text;
modelo.CliEndereco = txt_rua.Text;
modelo.CliEndNumero = txt_num.Text;
modelo.CliBairro = txt_bairro.Text;
modelo.CliEmail = txt_cliemail.Text;
modelo.CliFone = txt_fone.Text;
modelo.CliCelular = txt_celular.Text;
if (rb_fisica.Checked == true)
{
modelo.CliTipo = "Física"; // fisica
modelo.CliRSocial = "";
}
else
{
modelo.CliTipo = "Jurídica"; // juridica
}
//obj para gravar os dados no banco
DALLconexao cx = new DALLconexao(DadosDaConexao.StringDeConexao);
BLLCliente bll = new BLLCliente(cx);
if (this.operacao == "inserir")
{
//cadastrar uma categoria
bll.Incluir(modelo);
MessageBox.Show("Cadastro efetuado: Código " + modelo.CliCod.ToString());
}
else
{
//alterar uma categoria
modelo.CliCod = Convert.ToInt32(txt_cod.Text);
bll.Alterar(modelo);
MessageBox.Show("Cadastro alterado");
}
this.dislplayclean();
this.alterabotoes(1);
}
catch (Exception erro)
{
MessageBox.Show(erro.Message);
}
}//btn save
private void btn_cancel_Click(object sender, EventArgs e)
{
this.alterabotoes(1);
this.dislplayclean();
}//btn cancel
private void rb_fisica_CheckedChanged(object sender, EventArgs e)
{
if (rb_fisica.Checked == true)
{
lbRSocial.Visible = false;
txt_cliraz.Visible = false;
lbCPFCNPJ.Text = "CPF:";
lbRGIE.Text = "RG:";
}
else
{
lbRSocial.Visible = true;
txt_cliraz.Visible = true;
lbCPFCNPJ.Text = "CNPJ:";
lbRGIE.Text = "IE:";
}
}//end rb checked
private void txt_clicep_Leave(object sender, EventArgs e)
{
if (BuscaEndereco.verificaCEP(txt_clicep.Text) == false)
{
MessageBox.Show("O CEP é inválido");
txt_clicep.Clear();
txt_estado.Clear();
txt_cidade.Clear();
txt_rua.Clear();
}
else
{
if (BuscaEndereco.verificaCEP(txt_clicep.Text) == true)
{
txt_bairro.Text = BuscaEndereco.bairro;
txt_estado.Text = BuscaEndereco.estado;
txt_cidade.Text = BuscaEndereco.cidade;
txt_rua.Text = BuscaEndereco.endereco;
}
}
}//btn clicep leave
private void txt_cpf_Leave(object sender, EventArgs e)
{
lbl_cpferro.Visible = false;
if (rb_fisica.Checked == true)
{
//cpf
if (BLLVerifyCpf.IsCpf(txt_cpf.Text) == false)
{
lbl_cpferro.Visible = true;
}
}
else
{
if (BLLVerifyCpf.IsCnpj(txt_cpf.Text) == false)
{
lbl_cpferro.Visible = true;
}
}
}//btn cpf
private void txt_cpf_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != (char)8)
{
Campo edit = Campo.CPF;
if (rb_fisica.Checked == false) edit = Campo.CNPJ;
Formatar(edit, txt_cpf);
}
}//end cpf keypress
private void txt_clicep_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != (char)8)
{
Campo edit = Campo.CEP;
Formatar(edit, txt_clicep);
}
}//btn txt_clicep_KeyPress
private void frm_cadclientes_Load(object sender, EventArgs e)
{
this.rb_fisica.Checked = true;
this.rb_fisica_CheckedChanged(sender,e);
this.dislplayclean();
this.alterabotoes(1);
}// end load
private void pb_exit_Click(object sender, EventArgs e)
{
this.Close();
}//end exit
}
}