149 lines
3.9 KiB
C#
149 lines
3.9 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Collections.Generic;
|
|
using DALL;
|
|
using MLL;
|
|
|
|
namespace BLL
|
|
{
|
|
public class BLLClientes
|
|
{
|
|
private DALClientes dal;
|
|
|
|
public BLLClientes(string conexao)
|
|
{
|
|
dal = new DALClientes(conexao);
|
|
}
|
|
|
|
#region INSERT
|
|
public bool Inserir(ModeloCliente c)
|
|
{
|
|
string erro = Validar(c);
|
|
|
|
if (!string.IsNullOrEmpty(erro))
|
|
throw new Exception(erro);
|
|
|
|
// 🔥 evita duplicidade (Documento + Empresa)
|
|
var existente = CarregarPorDocumento(c.EmpresaId, c.Documento);
|
|
|
|
if (existente != null)
|
|
throw new Exception("Já existe um cliente com esse documento.");
|
|
|
|
return dal.Inserir(c);
|
|
}//Inserir
|
|
#endregion
|
|
|
|
#region UPDATE
|
|
public bool Alterar(ModeloCliente c)
|
|
{
|
|
if (c.Id <= 0)
|
|
throw new Exception("Cliente inválido.");
|
|
|
|
string erro = Validar(c);
|
|
|
|
if (!string.IsNullOrEmpty(erro))
|
|
throw new Exception(erro);
|
|
|
|
var existente = CarregarPorDocumento(c.EmpresaId, c.Documento);
|
|
|
|
if (existente != null && existente.Id != c.Id)
|
|
throw new Exception("Já existe outro cliente com esse documento.");
|
|
|
|
return dal.Alterar(c);
|
|
}//Alterar
|
|
#endregion
|
|
|
|
#region DELETE
|
|
public bool Excluir(int id)
|
|
{
|
|
if (id <= 0)
|
|
throw new Exception("ID inválido.");
|
|
|
|
return dal.Excluir(id);
|
|
}//Excluir
|
|
#endregion
|
|
|
|
#region SELECT
|
|
public ModeloCliente Carregar(int id)
|
|
{
|
|
if (id <= 0)
|
|
return null;
|
|
|
|
return dal.Carregar(id);
|
|
}//Carregar
|
|
|
|
public List<ModeloCliente> Listar()
|
|
{
|
|
return dal.Listar();
|
|
}//Listar
|
|
#endregion
|
|
|
|
#region BUSCA POR DOCUMENTO
|
|
public ModeloCliente CarregarPorDocumento(int empresaId, string documento)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(documento))
|
|
return null;
|
|
|
|
documento = LimparNumeros(documento);
|
|
|
|
return dal.Listar()
|
|
.FirstOrDefault(x =>
|
|
x.EmpresaId == empresaId &&
|
|
LimparNumeros(x.Documento) == documento);
|
|
}
|
|
#endregion
|
|
|
|
#region VALIDAÇÃO
|
|
private string Validar(ModeloCliente c)
|
|
{
|
|
if (c == null)
|
|
return "Objeto cliente inválido.";
|
|
|
|
if (c.EmpresaId <= 0)
|
|
return "Empresa inválida.";
|
|
|
|
if (string.IsNullOrWhiteSpace(c.Nome))
|
|
return "Nome é obrigatório.";
|
|
|
|
if (string.IsNullOrWhiteSpace(c.TipoPessoa))
|
|
return "Tipo de pessoa é obrigatório.";
|
|
|
|
if (string.IsNullOrWhiteSpace(c.Documento))
|
|
return "Documento é obrigatório.";
|
|
|
|
// 🔥 limpa CPF/CNPJ
|
|
c.Documento = LimparNumeros(c.Documento);
|
|
|
|
if (c.TipoPessoa == "PF" && c.Documento.Length != 11)
|
|
return "CPF inválido.";
|
|
|
|
if (c.TipoPessoa == "PJ" && c.Documento.Length != 14)
|
|
return "CNPJ inválido.";
|
|
|
|
if (!string.IsNullOrEmpty(c.Email))
|
|
{
|
|
if (!c.Email.Contains("@"))
|
|
return "Email inválido.";
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(c.UF))
|
|
{
|
|
if (c.UF.Length != 2)
|
|
return "UF inválida.";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
#endregion
|
|
|
|
#region AUX
|
|
private string LimparNumeros(string texto)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(texto))
|
|
return texto;
|
|
|
|
return new string(texto.Where(char.IsDigit).ToArray());
|
|
}
|
|
#endregion
|
|
}
|
|
} |