313 lines
7.5 KiB
C#
313 lines
7.5 KiB
C#
using DAL;
|
|
using DALL;
|
|
using MLL;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
|
|
namespace BLL
|
|
{
|
|
public class BLLClientes
|
|
{
|
|
private readonly DALClientes dal;
|
|
|
|
public BLLClientes(string conexao)
|
|
{
|
|
dal = new DALClientes(conexao);
|
|
}
|
|
|
|
#region CRUD
|
|
|
|
public bool Inserir(ModeloCliente c)
|
|
{
|
|
string erro = Validar(c);
|
|
|
|
if (!string.IsNullOrWhiteSpace(erro))
|
|
throw new Exception(erro);
|
|
|
|
ModeloCliente? existente =
|
|
CarregarPorDocumento(c.EmpresaId, c.Documento);
|
|
|
|
if (existente != null)
|
|
throw new Exception("Já existe um cliente com esse documento.");
|
|
|
|
return dal.Inserir(c);
|
|
}
|
|
|
|
public bool Alterar(ModeloCliente c)
|
|
{
|
|
if (c.Id <= 0)
|
|
throw new Exception("Cliente inválido.");
|
|
|
|
string erro = Validar(c);
|
|
|
|
if (!string.IsNullOrWhiteSpace(erro))
|
|
throw new Exception(erro);
|
|
|
|
ModeloCliente? 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);
|
|
}
|
|
|
|
public bool Excluir(int id)
|
|
{
|
|
if (id <= 0)
|
|
throw new Exception("ID inválido.");
|
|
|
|
return dal.Excluir(id);
|
|
}
|
|
|
|
public ModeloCliente? Carregar(int id)
|
|
{
|
|
if (id <= 0)
|
|
return null;
|
|
|
|
return dal.Carregar(id);
|
|
}
|
|
|
|
public List<ModeloCliente> Listar()
|
|
{
|
|
return dal.Listar();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region BUSCAS
|
|
|
|
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);
|
|
}
|
|
|
|
public List<ModeloCliente> BuscarPorNome(string nome)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(nome))
|
|
return [];
|
|
|
|
return dal.Listar()
|
|
.Where(x =>
|
|
x.Nome.Contains(
|
|
nome,
|
|
StringComparison.OrdinalIgnoreCase))
|
|
.ToList();
|
|
}
|
|
|
|
public List<ModeloCliente> BuscarPorCidade(string cidade)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(cidade))
|
|
return [];
|
|
|
|
return dal.Listar()
|
|
.Where(x =>
|
|
!string.IsNullOrWhiteSpace(x.Cidade) &&
|
|
x.Cidade.Contains(
|
|
cidade,
|
|
StringComparison.OrdinalIgnoreCase))
|
|
.ToList();
|
|
}
|
|
|
|
public List<ModeloCliente> BuscarPorTelefone(string telefone)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(telefone))
|
|
return [];
|
|
|
|
telefone = LimparNumeros(telefone);
|
|
|
|
return dal.Listar()
|
|
.Where(x =>
|
|
LimparNumeros(x.Telefone1).Contains(telefone) ||
|
|
LimparNumeros(x.Telefone2).Contains(telefone) ||
|
|
LimparNumeros(x.Celular).Contains(telefone) ||
|
|
LimparNumeros(x.Whatsapp).Contains(telefone))
|
|
.ToList();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region VIEWS
|
|
|
|
public List<ModeloCliente> ListarTodos()
|
|
{
|
|
return dal.ListarTodos();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarAtivos()
|
|
{
|
|
return dal.ListarAtivos();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarInativos()
|
|
{
|
|
return dal.ListarInativos();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarBloqueados()
|
|
{
|
|
return dal.ListarBloqueados();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarComLimite()
|
|
{
|
|
return dal.ListarComLimite();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarSemLimite()
|
|
{
|
|
return dal.ListarSemLimite();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarPF()
|
|
{
|
|
return dal.ListarPF();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarPJ()
|
|
{
|
|
return dal.ListarPJ();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarWhatsapp()
|
|
{
|
|
return dal.ListarWhatsapp();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarEmail()
|
|
{
|
|
return dal.ListarEmail();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarEmailNFe()
|
|
{
|
|
return dal.ListarEmailNFe();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarCripto()
|
|
{
|
|
return dal.ListarCripto();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarUltimaCompra()
|
|
{
|
|
return dal.ListarUltimaCompra();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarSemCompra()
|
|
{
|
|
return dal.ListarSemCompra();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarRecentes()
|
|
{
|
|
return dal.ListarRecentes();
|
|
}
|
|
|
|
public List<ModeloCliente> ListarCobranca()
|
|
{
|
|
return dal.ListarCobranca();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region DASHBOARD
|
|
|
|
public DataTable ObterClientesPorCidade()
|
|
{
|
|
return dal.ObterClientesPorCidade();
|
|
}
|
|
|
|
public DataTable ObterClientesPorGrupo()
|
|
{
|
|
return dal.ObterClientesPorGrupo();
|
|
}
|
|
|
|
public DataTable ObterDashboard()
|
|
{
|
|
return dal.ObterDashboard();
|
|
}
|
|
|
|
public DataTable ObterClientesCompleto()
|
|
{
|
|
return dal.ObterClientesCompleto();
|
|
}
|
|
|
|
#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.";
|
|
|
|
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.IsNullOrWhiteSpace(c.Email))
|
|
{
|
|
if (!c.Email.Contains("@"))
|
|
return "Email inválido.";
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(c.UF))
|
|
{
|
|
if (c.UF.Length != 2)
|
|
return "UF inválida.";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region AUXILIARES
|
|
|
|
private string LimparNumeros(string? texto)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(texto))
|
|
return string.Empty;
|
|
|
|
return new string(
|
|
texto.Where(char.IsDigit).ToArray());
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |