526 lines
21 KiB
C#
526 lines
21 KiB
C#
using BLL;
|
|
using CustomMessageBox;
|
|
using DAL;
|
|
using MLL;
|
|
using System.Drawing;
|
|
using System.Windows.Forms;
|
|
|
|
namespace UI
|
|
{
|
|
public class EmpresaCadastroPanel : UserControl
|
|
{
|
|
private string _cx = DadosDaConexao.ObterConexao();
|
|
private readonly Color AccentBlue = Color.FromArgb(37, 99, 235);
|
|
private readonly Color TextDark = Color.FromArgb(30, 41, 59);
|
|
private readonly Color BorderColor = Color.FromArgb(226, 232, 240);
|
|
|
|
private Panel pnlToolbar = null!;
|
|
private Panel mainScroll = null!;
|
|
private Panel content = null!;
|
|
|
|
// Identificação
|
|
private RoundTextBox txtId = null!, txtNome = null!, txtCNPJ = null!;
|
|
private RoundTextBox txtTipoEmpresa = null!, txtRegimeTributario = null!, txtCNAE = null!;
|
|
|
|
// Endereço
|
|
private RoundTextBox txtCep = null!, txtEndereco = null!, txtNumero = null!, txtComplemento = null!;
|
|
private RoundTextBox txtBairro = null!, txtCidade = null!, txtUF = null!, txtPais = null!;
|
|
|
|
// Contatos
|
|
private RoundTextBox txtTelefone1 = null!, txtTelefone2 = null!, txtCelular = null!, txtWhatsapp = null!;
|
|
private RoundTextBox txtEmail = null!, txtSite = null!;
|
|
|
|
// Fiscal
|
|
private RoundTextBox txtInscricaoEstadual = null!, txtInscricaoMunicipal = null!;
|
|
|
|
// Contador
|
|
private RoundTextBox txtCNPJCPF_Contador = null!, txtNome_Contador = null!;
|
|
|
|
// Outros
|
|
private RoundTextBox txtTextoParaRecibo = null!;
|
|
private CheckBox chkAtivo = null!;
|
|
|
|
// Info (readonly)
|
|
private RoundTextBox txtCriadoEm = null!, txtAtualizadoEm = null!;
|
|
|
|
//Carregar dados da empresa atual, se houver no banco de dados
|
|
private void CarregarEmpresaAtiva()
|
|
{
|
|
var bll = new BLLEmpresa(_cx);
|
|
var empresa = bll.CarregarEmpresaAtiva();
|
|
|
|
if (empresa == null)
|
|
{
|
|
MessageBox.Show("Nenhuma empresa ativa encontrada.",
|
|
"Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
// Preenche todos os campos do formulário
|
|
txtId.Text = empresa.Id.ToString();
|
|
txtNome.Text = empresa.Nome;
|
|
txtCNPJ.Text = empresa.CNPJ;
|
|
txtTipoEmpresa.Text = empresa.TipoEmpresa;
|
|
txtRegimeTributario.Text = empresa.RegimeTributario;
|
|
txtCNAE.Text = empresa.CNAE;
|
|
txtInscricaoEstadual.Text = empresa.InscricaoEstadual;
|
|
txtInscricaoMunicipal.Text = empresa.InscricaoMunicipal;
|
|
txtCep.Text = empresa.Cep;
|
|
txtEndereco.Text = empresa.Endereco;
|
|
txtNumero.Text = empresa.Numero.ToString();
|
|
txtComplemento.Text = empresa.Complemento;
|
|
txtBairro.Text = empresa.Bairro;
|
|
txtCidade.Text = empresa.Cidade;
|
|
txtUF.Text = empresa.UF;
|
|
txtPais.Text = empresa.Pais;
|
|
txtTelefone1.Text = empresa.Telefone1;
|
|
txtTelefone2.Text = empresa.Telefone2;
|
|
txtCelular.Text = empresa.Celular;
|
|
txtWhatsapp.Text = empresa.Whatsapp;
|
|
txtEmail.Text = empresa.Email;
|
|
txtSite.Text = empresa.Site;
|
|
txtCNPJCPF_Contador.Text = empresa.CNPJCPF_Contador;
|
|
txtNome_Contador.Text = empresa.Nome_Contador;
|
|
txtTextoParaRecibo.Text = empresa.TextoParaRecibo;
|
|
chkAtivo.Checked = empresa.Ativo;
|
|
txtCriadoEm.Text = empresa.CriadoEm.ToString("dd/MM/yyyy");
|
|
txtAtualizadoEm.Text = empresa.AtualizadoEm.ToString("dd/MM/yyyy");
|
|
}
|
|
public EmpresaCadastroPanel()
|
|
{
|
|
Dock = DockStyle.Fill;
|
|
BackColor = Color.White;
|
|
DoubleBuffered = true;
|
|
InitializeLayout();
|
|
// Carregar dados da empresa atual, se houver
|
|
CarregarEmpresaAtiva();
|
|
}
|
|
|
|
private void InitializeLayout()
|
|
{
|
|
this.Controls.Clear();
|
|
|
|
// ── TOOLBAR ───────────────────────────────────────────────────────
|
|
pnlToolbar = new Panel
|
|
{
|
|
Dock = DockStyle.Top,
|
|
Height = 55,
|
|
BackColor = Color.FromArgb(248, 250, 252),
|
|
BorderStyle = BorderStyle.None
|
|
};
|
|
|
|
var flowButtons = new FlowLayoutPanel
|
|
{
|
|
Dock = DockStyle.Fill,
|
|
Padding = new Padding(12, 10, 0, 0),
|
|
BackColor = Color.Transparent
|
|
};
|
|
|
|
var btnNovo = CreateToolbarButton("Novo", Color.FromArgb(34, 197, 94));
|
|
var btnAlterar = CreateToolbarButton("Alterar", Color.FromArgb(245, 158, 11));
|
|
var btnExcluir = CreateToolbarButton("Excluir", Color.FromArgb(239, 68, 68));
|
|
var btnLocalizar = CreateToolbarButton("Localizar", AccentBlue);
|
|
var btnSalvar = CreateToolbarButton("Salvar", AccentBlue);
|
|
var btnCancelar = CreateToolbarButton("Cancelar", Color.FromArgb(148, 163, 184));
|
|
var btnCriarCert = CreateToolbarButton("Certificado", Color.FromArgb(148, 160, 184));
|
|
var btnGerarFichaPDF = CreateToolbarButton("Ficha Tecnica", Color.FromArgb(255, 0, 0));
|
|
|
|
btnNovo.Click += (s, e) => BtnNovo_Click();
|
|
btnAlterar.Click += (s, e) => BtnAlterar_Click();
|
|
btnExcluir.Click += (s, e) => BtnExcluir_Click();
|
|
btnLocalizar.Click += (s, e) => BtnLocalizar_Click();
|
|
btnSalvar.Click += (s, e) => BtnSalvar_Click();
|
|
btnCancelar.Click += (s, e) => BtnCancelar_Click();
|
|
btnCriarCert.Click += (s, e) => BtnGerarCertificado_Click();
|
|
btnGerarFichaPDF.Click += (s, e) => BtnFicha_Click();
|
|
|
|
flowButtons.Controls.AddRange(new Control[]
|
|
{ btnNovo, btnAlterar, btnExcluir, btnLocalizar, btnSalvar, btnCancelar,btnCriarCert,btnGerarFichaPDF });
|
|
pnlToolbar.Controls.Add(flowButtons);
|
|
this.Controls.Add(pnlToolbar);
|
|
|
|
// ── SCROLL + CONTENT ──────────────────────────────────────────────
|
|
mainScroll = new Panel
|
|
{
|
|
Dock = DockStyle.Fill,
|
|
AutoScroll = true,
|
|
BackColor = Color.White
|
|
};
|
|
this.Controls.Add(mainScroll);
|
|
mainScroll.BringToFront();
|
|
|
|
content = new Panel
|
|
{
|
|
Width = 1100,
|
|
Height = 900,
|
|
Location = new Point(0, 0),
|
|
BackColor = Color.White
|
|
};
|
|
mainScroll.Controls.Add(content);
|
|
|
|
const int rowH = 52;
|
|
const int secGap = 10;
|
|
const int secH = 28;
|
|
const int inputH = 28;
|
|
|
|
int y = 10;
|
|
|
|
// ── 1. IDENTIFICAÇÃO DA EMPRESA ───────────────────────────────────
|
|
content.Controls.Add(CreateSectionHeader("IDENTIFICAÇÃO DA EMPRESA", y));
|
|
y += secH + 4;
|
|
|
|
txtId = AddInput(content, "ID", 20, y, 60, inputH, readOnly: true);
|
|
txtNome = AddInput(content, "Nome / Razão Social", 90, y, 450, inputH);
|
|
txtCNPJ = AddInput(content, "CNPJ", 550, y, 180, inputH);
|
|
DocumentoHelper.Registrar(txtCNPJ);
|
|
txtTipoEmpresa = AddInput(content, "Tipo Empresa", 740, y, 200, inputH);
|
|
y += rowH;
|
|
|
|
txtRegimeTributario = AddInput(content, "Regime Tributário", 20, y, 200, inputH);
|
|
txtCNAE = AddInput(content, "CNAE", 230, y, 150, inputH);
|
|
txtInscricaoEstadual = AddInput(content, "Inscrição Estadual", 390, y, 200, inputH);
|
|
txtInscricaoMunicipal = AddInput(content, "Inscrição Municipal", 600, y, 200, inputH);
|
|
|
|
// ── 2. ENDEREÇO COMPLETO ──────────────────────────────────────────
|
|
y += rowH + secGap;
|
|
content.Controls.Add(CreateSectionHeader("ENDEREÇO COMPLETO", y));
|
|
y += secH + 4;
|
|
|
|
txtCep = AddInput(content, "CEP", 20, y, 95, inputH);
|
|
// No InitializeLayout(), após criar o txtCep
|
|
txtCep.Leave += (s, e) =>
|
|
{
|
|
string cep = txtCep.Text.Trim().Replace("-", "");
|
|
|
|
if (cep.Length != 8) return;
|
|
|
|
if (TLL.VerifyCep.verificaCEP(cep))
|
|
{
|
|
txtEndereco.Text = TLL.VerifyCep.endereco;
|
|
txtBairro.Text = TLL.VerifyCep.bairro;
|
|
txtCidade.Text = TLL.VerifyCep.cidade;
|
|
txtUF.Text = TLL.VerifyCep.estado;
|
|
txtPais.Text = "Brasil";
|
|
|
|
// Formata o CEP com hífen
|
|
txtCep.Text = $"{cep[..5]}-{cep[5..]}";
|
|
}
|
|
else
|
|
{
|
|
NT_MessageBox.Show("CEP não encontrado.", "CEP",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
}
|
|
};
|
|
txtEndereco = AddInput(content, "Logradouro", 125, y, 370, inputH);
|
|
txtNumero = AddInput(content, "Nº", 505, y, 60, inputH);
|
|
txtComplemento = AddInput(content, "Complemento", 575, y, 175, inputH);
|
|
txtBairro = AddInput(content, "Bairro", 760, y, 180, inputH);
|
|
y += rowH;
|
|
|
|
txtCidade = AddInput(content, "Cidade", 20, y, 280, inputH);
|
|
txtUF = AddInput(content, "UF", 310, y, 55, inputH);
|
|
txtPais = AddInput(content, "País", 375, y, 150, inputH);
|
|
|
|
// ── 3. CONTATOS E COMUNICAÇÃO ─────────────────────────────────────
|
|
y += rowH + secGap;
|
|
content.Controls.Add(CreateSectionHeader("CONTATOS E COMUNICAÇÃO", y));
|
|
y += secH + 4;
|
|
|
|
txtTelefone1 = AddInput(content, "Telefone 1", 20, y, 155, inputH);
|
|
txtTelefone2 = AddInput(content, "Telefone 2", 185, y, 155, inputH);
|
|
txtCelular = AddInput(content, "Celular", 350, y, 155, inputH);
|
|
txtWhatsapp = AddInput(content, "WhatsApp", 515, y, 155, inputH);
|
|
y += rowH;
|
|
|
|
txtEmail = AddInput(content, "E-mail", 20, y, 350, inputH);
|
|
txtSite = AddInput(content, "Site", 380, y, 290, inputH);
|
|
|
|
// ── 4. DADOS DO CONTADOR ──────────────────────────────────────────
|
|
y += rowH + secGap;
|
|
content.Controls.Add(CreateSectionHeader("DADOS DO CONTADOR", y));
|
|
y += secH + 4;
|
|
|
|
txtCNPJCPF_Contador = AddInput(content, "CPF/CNPJ do Contador", 20, y, 200, inputH);
|
|
DocumentoHelper.Registrar(txtCNPJCPF_Contador);
|
|
txtNome_Contador = AddInput(content, "Nome do Contador", 230, y, 400, inputH);
|
|
|
|
// ── 5. OUTROS ─────────────────────────────────────────────────────
|
|
y += rowH + secGap;
|
|
content.Controls.Add(CreateSectionHeader("OUTROS", y));
|
|
y += secH + 4;
|
|
|
|
txtTextoParaRecibo = AddInput(content, "Texto para Recibo", 20, y, 920, inputH);
|
|
y += rowH;
|
|
txtTextoParaRecibo.Multiline = true;
|
|
|
|
|
|
chkAtivo = CreateCheckBox("Ativo", 20, y);
|
|
content.Controls.Add(chkAtivo);
|
|
|
|
// ── 6. INFORMAÇÕES DO REGISTRO ────────────────────────────────────
|
|
y += 35 + secGap;
|
|
content.Controls.Add(CreateSectionHeader("INFORMAÇÕES DO REGISTRO", y));
|
|
y += secH + 4;
|
|
|
|
txtCriadoEm = AddInput(content, "Criado Em", 20, y, 175, inputH, readOnly: true);
|
|
txtAtualizadoEm = AddInput(content, "Atualizado Em", 205, y, 175, inputH, readOnly: true);
|
|
y += rowH;
|
|
|
|
content.Height = y + 10;
|
|
|
|
SetCampos(false);
|
|
}
|
|
|
|
// ── CAMPOS ────────────────────────────────────────────────────────────
|
|
|
|
private RoundTextBox[] TodosOsCampos() => new[]
|
|
{
|
|
txtNome, txtCNPJ, txtTipoEmpresa, txtRegimeTributario, txtCNAE,
|
|
txtInscricaoEstadual, txtInscricaoMunicipal,
|
|
txtCep, txtEndereco, txtNumero, txtComplemento,
|
|
txtBairro, txtCidade, txtUF, txtPais,
|
|
txtTelefone1, txtTelefone2, txtCelular, txtWhatsapp,
|
|
txtEmail, txtSite,
|
|
txtCNPJCPF_Contador, txtNome_Contador,
|
|
txtTextoParaRecibo
|
|
};
|
|
|
|
private void SetCampos(bool enabled)
|
|
{
|
|
foreach (var campo in TodosOsCampos())
|
|
{
|
|
campo.Enabled = enabled;
|
|
campo.BackColor = enabled ? Color.White : Color.FromArgb(241, 245, 249);
|
|
}
|
|
chkAtivo.Enabled = enabled;
|
|
}
|
|
|
|
// ── EVENTOS ───────────────────────────────────────────────────────────
|
|
|
|
private void BtnNovo_Click()
|
|
{
|
|
foreach (var campo in TodosOsCampos())
|
|
campo.Text = string.Empty;
|
|
|
|
chkAtivo.Checked = true;
|
|
SetCampos(true);
|
|
}
|
|
|
|
private void BtnAlterar_Click()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(txtId.Text))
|
|
{
|
|
MessageBox.Show(
|
|
"Nenhum registro selecionado para alterar.",
|
|
"Atenção",
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
SetCampos(true);
|
|
txtId.Enabled = false;
|
|
txtId.BackColor = Color.FromArgb(241, 245, 249);
|
|
}
|
|
// Evento
|
|
private void BtnFicha_Click()
|
|
{
|
|
var bll = new BLLEmpresa(_cx);
|
|
var empresa = bll.CarregarEmpresaAtiva();
|
|
|
|
if (empresa == null)
|
|
{
|
|
MessageBox.Show("Nenhuma empresa ativa encontrada.");
|
|
return;
|
|
}
|
|
|
|
var resultado = TLL.FichaEmpresaPDF.GerarEAbrir(empresa);
|
|
|
|
if (!resultado.Sucesso)
|
|
MessageBox.Show(resultado.Mensagem, "Erro",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
private void BtnExcluir_Click()
|
|
{
|
|
// Solicita confirmação e exclui o registro atual
|
|
}
|
|
|
|
private void BtnLocalizar_Click()
|
|
{
|
|
// Abre tela/diálogo de busca e preenche os campos
|
|
}
|
|
private void BtnGerarCertificado_Click()
|
|
{
|
|
ModeloEmpresa empresa = new ModeloEmpresa();
|
|
// Após salvar a empresa com sucesso...
|
|
var resultado = TLL.CertificadoHelper.Gerar(empresa, "Nike12122020*##");
|
|
|
|
if (resultado.Sucesso)
|
|
NT_MessageBox.Show(
|
|
$"Certificado gerado!\nVálido até: {resultado.ValidoAte:dd/MM/yyyy}\n" +
|
|
$"Thumbprint: {resultado.Thumbprint}",
|
|
"Certificado", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
else
|
|
NT_MessageBox.Show(resultado.Mensagem, "Erro",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
private void BtnVerificarCert_Click()
|
|
{
|
|
ModeloEmpresa empresa = new ModeloEmpresa();
|
|
var status = TLL.CertificadoHelper.VerificarStatus(empresa.CNPJ, "Nike12122020*##");
|
|
|
|
if (!status.Valido)
|
|
NT_MessageBox.Show("Certificado expirado! Gere um novo.",
|
|
"Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
}
|
|
|
|
private void BtnSalvar_Click()
|
|
{
|
|
BLLEmpresa _empresaBLL = new BLLEmpresa(_cx);
|
|
|
|
if (string.IsNullOrWhiteSpace(txtNome.Text) ||
|
|
string.IsNullOrWhiteSpace(txtCNPJ.Text) ||
|
|
string.IsNullOrWhiteSpace(txtEmail.Text))
|
|
{
|
|
NT_MessageBox.Show("Preencha ao menos Nome, CNPJ e Email.",
|
|
"Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
bool isNew = string.IsNullOrWhiteSpace(txtId.Text);
|
|
|
|
var empresa = new MLL.ModeloEmpresa(
|
|
id: isNew ? 0 : int.Parse(txtId.Text),
|
|
nome: txtNome.Text,
|
|
cNPJ: txtCNPJ.Text,
|
|
tipoEmpresa: txtTipoEmpresa.Text,
|
|
regimeTributario: txtRegimeTributario.Text,
|
|
cNAE: txtCNAE.Text,
|
|
cep: txtCep.Text,
|
|
endereco: txtEndereco.Text,
|
|
numero: string.IsNullOrWhiteSpace(txtNumero.Text) ? 0 : int.Parse(txtNumero.Text),
|
|
complemento: txtComplemento.Text,
|
|
bairro: txtBairro.Text,
|
|
cidade: txtCidade.Text,
|
|
uF: txtUF.Text,
|
|
pais: txtPais.Text,
|
|
telefone1: txtTelefone1.Text,
|
|
telefone2: txtTelefone2.Text,
|
|
celular: txtCelular.Text,
|
|
whatsapp: txtWhatsapp.Text,
|
|
email: txtEmail.Text,
|
|
site: txtSite.Text,
|
|
inscricaoEstadual: txtInscricaoEstadual.Text,
|
|
inscricaoMunicipal: txtInscricaoMunicipal.Text,
|
|
cNPJCPF_Contador: txtCNPJCPF_Contador.Text,
|
|
nome_Contador: txtNome_Contador.Text,
|
|
textoParaRecibo: txtTextoParaRecibo.Text,
|
|
ativo: chkAtivo.Checked,
|
|
criadoEm: DateTime.Now,
|
|
atualizadoEm: DateTime.Now
|
|
);
|
|
|
|
bool sucesso;
|
|
|
|
if (isNew)
|
|
sucesso = _empresaBLL.Inserir(empresa);
|
|
else
|
|
sucesso = _empresaBLL.Alterar(empresa);
|
|
|
|
if (!sucesso)
|
|
{
|
|
NT_MessageBox.Show("Erro ao salvar empresa.",
|
|
"Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
// 🔹 Atualiza UI
|
|
txtAtualizadoEm.Text = DateTime.Now.ToString("dd/MM/yyyy");
|
|
SetCampos(false);
|
|
|
|
NT_MessageBox.Show("Empresa salva com sucesso!", "Sucesso",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
|
|
private void BtnCancelar_Click()
|
|
{
|
|
foreach (var campo in TodosOsCampos())
|
|
campo.Text = string.Empty;
|
|
|
|
chkAtivo.Checked = false;
|
|
SetCampos(false);
|
|
}
|
|
|
|
// ── HELPERS ───────────────────────────────────────────────────────────
|
|
|
|
private Panel CreateSectionHeader(string title, int y)
|
|
{
|
|
var pnl = new Panel { Location = new Point(20, y), Width = 1000, Height = 26 };
|
|
var lbl = new Label
|
|
{
|
|
Text = title,
|
|
Font = new Font("Segoe UI", 8.5f, FontStyle.Bold),
|
|
ForeColor = AccentBlue,
|
|
AutoSize = true,
|
|
Location = new Point(0, 0)
|
|
};
|
|
var line = new Panel
|
|
{
|
|
BackColor = BorderColor,
|
|
Height = 1,
|
|
Width = 980,
|
|
Location = new Point(0, 20)
|
|
};
|
|
pnl.Controls.Add(lbl);
|
|
pnl.Controls.Add(line);
|
|
return pnl;
|
|
}
|
|
|
|
private RoundTextBox AddInput(Control parent, string label,
|
|
int x, int y, int width, int height,
|
|
bool readOnly = false)
|
|
{
|
|
var lbl = new Label
|
|
{
|
|
Text = label,
|
|
Location = new Point(x, y),
|
|
Font = new Font("Segoe UI", 7.5f, FontStyle.Bold),
|
|
ForeColor = readOnly ? Color.Gray : TextDark,
|
|
AutoSize = true
|
|
};
|
|
var txt = new RoundTextBox
|
|
{
|
|
Location = new Point(x, y + 16),
|
|
Size = new Size(width, height),
|
|
Radius = 4,
|
|
BorderColor = readOnly ? Color.FromArgb(203, 213, 225) : BorderColor,
|
|
FocusColor = AccentBlue,
|
|
ReadOnly = readOnly,
|
|
BackColor = readOnly ? Color.FromArgb(241, 245, 249) : Color.White
|
|
};
|
|
parent.Controls.Add(lbl);
|
|
parent.Controls.Add(txt);
|
|
return txt;
|
|
}
|
|
|
|
private CheckBox CreateCheckBox(string text, int x, int y) => new CheckBox
|
|
{
|
|
Text = text,
|
|
Location = new Point(x, y),
|
|
Font = new Font("Segoe UI", 8.5f, FontStyle.Bold),
|
|
ForeColor = TextDark,
|
|
AutoSize = true
|
|
};
|
|
|
|
private RoundButton CreateToolbarButton(string text, Color color) => new RoundButton
|
|
{
|
|
Text = text,
|
|
Size = new Size(95, 32),
|
|
BackColor = color,
|
|
ForeColor = Color.White,
|
|
Font = new Font("Segoe UI Semibold", 8.5f),
|
|
Margin = new Padding(0, 0, 6, 0),
|
|
Cursor = Cursors.Hand
|
|
};
|
|
}
|
|
} |