497 lines
17 KiB
C#
497 lines
17 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using MLL;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
|
|
namespace DAL
|
|
{
|
|
public class DALClientes
|
|
{
|
|
private readonly string _conexao;
|
|
|
|
public DALClientes(string conexao)
|
|
{
|
|
_conexao = conexao;
|
|
}
|
|
|
|
#region INSERT
|
|
|
|
public bool Inserir(ModeloCliente c)
|
|
{
|
|
try
|
|
{
|
|
using SqlConnection conn = new(_conexao);
|
|
|
|
string sql = @"
|
|
INSERT INTO Clientes
|
|
(
|
|
EmpresaId,
|
|
Nome,
|
|
NomeFantasia,
|
|
TipoPessoa,
|
|
Documento,
|
|
RG,
|
|
InscricaoMunicipal,
|
|
DataNascimento,
|
|
Contato,
|
|
Telefone1,
|
|
Telefone2,
|
|
Celular,
|
|
Whatsapp,
|
|
Email,
|
|
EmailNFe,
|
|
Site,
|
|
Grupo,
|
|
Cep,
|
|
Endereco,
|
|
Numero,
|
|
Complemento,
|
|
Bairro,
|
|
Cidade,
|
|
UF,
|
|
Pais,
|
|
LimiteCredito,
|
|
Bloqueado,
|
|
ObservacoesCobranca,
|
|
VendedorPadraoId,
|
|
TipoConsumidor,
|
|
Observacoes,
|
|
CampoExtra1,
|
|
CampoExtra2,
|
|
CampoExtra3,
|
|
Bitcoin,
|
|
Ethereum,
|
|
Litecoin,
|
|
Ativo,
|
|
UltimaCompra
|
|
)
|
|
VALUES
|
|
(
|
|
@EmpresaId,
|
|
@Nome,
|
|
@NomeFantasia,
|
|
@TipoPessoa,
|
|
@Documento,
|
|
@RG,
|
|
@InscricaoMunicipal,
|
|
@DataNascimento,
|
|
@Contato,
|
|
@Telefone1,
|
|
@Telefone2,
|
|
@Celular,
|
|
@Whatsapp,
|
|
@Email,
|
|
@EmailNFe,
|
|
@Site,
|
|
@Grupo,
|
|
@Cep,
|
|
@Endereco,
|
|
@Numero,
|
|
@Complemento,
|
|
@Bairro,
|
|
@Cidade,
|
|
@UF,
|
|
@Pais,
|
|
@LimiteCredito,
|
|
@Bloqueado,
|
|
@ObservacoesCobranca,
|
|
@VendedorPadraoId,
|
|
@TipoConsumidor,
|
|
@Observacoes,
|
|
@CampoExtra1,
|
|
@CampoExtra2,
|
|
@CampoExtra3,
|
|
@Bitcoin,
|
|
@Ethereum,
|
|
@Litecoin,
|
|
@Ativo,
|
|
@UltimaCompra
|
|
)";
|
|
|
|
using SqlCommand cmd = new(sql, conn);
|
|
|
|
PreencherParametros(cmd, c);
|
|
|
|
conn.Open();
|
|
|
|
return cmd.ExecuteNonQuery() > 0;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region UPDATE
|
|
|
|
public bool Alterar(ModeloCliente c)
|
|
{
|
|
try
|
|
{
|
|
using SqlConnection conn = new(_conexao);
|
|
|
|
string sql = @"
|
|
UPDATE Clientes SET
|
|
|
|
EmpresaId = @EmpresaId,
|
|
Nome = @Nome,
|
|
NomeFantasia = @NomeFantasia,
|
|
TipoPessoa = @TipoPessoa,
|
|
Documento = @Documento,
|
|
RG = @RG,
|
|
InscricaoMunicipal = @InscricaoMunicipal,
|
|
DataNascimento = @DataNascimento,
|
|
Contato = @Contato,
|
|
Telefone1 = @Telefone1,
|
|
Telefone2 = @Telefone2,
|
|
Celular = @Celular,
|
|
Whatsapp = @Whatsapp,
|
|
Email = @Email,
|
|
EmailNFe = @EmailNFe,
|
|
Site = @Site,
|
|
Grupo = @Grupo,
|
|
Cep = @Cep,
|
|
Endereco = @Endereco,
|
|
Numero = @Numero,
|
|
Complemento = @Complemento,
|
|
Bairro = @Bairro,
|
|
Cidade = @Cidade,
|
|
UF = @UF,
|
|
Pais = @Pais,
|
|
LimiteCredito = @LimiteCredito,
|
|
Bloqueado = @Bloqueado,
|
|
ObservacoesCobranca = @ObservacoesCobranca,
|
|
VendedorPadraoId = @VendedorPadraoId,
|
|
TipoConsumidor = @TipoConsumidor,
|
|
Observacoes = @Observacoes,
|
|
CampoExtra1 = @CampoExtra1,
|
|
CampoExtra2 = @CampoExtra2,
|
|
CampoExtra3 = @CampoExtra3,
|
|
Bitcoin = @Bitcoin,
|
|
Ethereum = @Ethereum,
|
|
Litecoin = @Litecoin,
|
|
Ativo = @Ativo,
|
|
UltimaCompra = @UltimaCompra,
|
|
AtualizadoEm = GETDATE()
|
|
|
|
WHERE Id = @Id";
|
|
|
|
using SqlCommand cmd = new(sql, conn);
|
|
|
|
PreencherParametros(cmd, c);
|
|
|
|
cmd.Parameters.AddWithValue("@Id", c.Id);
|
|
|
|
conn.Open();
|
|
|
|
return cmd.ExecuteNonQuery() > 0;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region DELETE
|
|
|
|
public bool Excluir(int id)
|
|
{
|
|
try
|
|
{
|
|
using SqlConnection conn = new(_conexao);
|
|
|
|
string sql = "DELETE FROM Clientes WHERE Id = @Id";
|
|
|
|
using SqlCommand cmd = new(sql, conn);
|
|
|
|
cmd.Parameters.AddWithValue("@Id", id);
|
|
|
|
conn.Open();
|
|
|
|
return cmd.ExecuteNonQuery() > 0;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region SELECT
|
|
|
|
public ModeloCliente? Carregar(int id)
|
|
{
|
|
using SqlConnection conn = new(_conexao);
|
|
|
|
string sql = "SELECT * FROM Clientes WHERE Id = @Id";
|
|
|
|
using SqlCommand cmd = new(sql, conn);
|
|
|
|
cmd.Parameters.AddWithValue("@Id", id);
|
|
|
|
conn.Open();
|
|
|
|
using SqlDataReader dr = cmd.ExecuteReader();
|
|
|
|
if (dr.Read())
|
|
{
|
|
return PreencherModelo(dr);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public List<ModeloCliente> Listar()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM Clientes ORDER BY Nome");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region VIEWS
|
|
|
|
public List<ModeloCliente> ListarTodos()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_Clientes");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarAtivos()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesAtivos");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarInativos()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesInativos");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarBloqueados()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesBloqueados");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarComLimite()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesComLimite");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarSemLimite()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesSemLimite");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarPF()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesPF");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarPJ()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesPJ");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarWhatsapp()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesWhatsapp");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarEmail()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesEmail");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarEmailNFe()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesEmailNFe");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarCripto()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesCripto");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarUltimaCompra()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesUltimaCompra");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarSemCompra()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesSemCompra");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarRecentes()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesRecentes");
|
|
}
|
|
|
|
public List<ModeloCliente> ListarCobranca()
|
|
{
|
|
return ExecutarViewLista("SELECT * FROM VW_ClientesCobranca");
|
|
}
|
|
|
|
public DataTable ObterClientesPorCidade()
|
|
{
|
|
return ExecutarViewDataTable("SELECT * FROM VW_ClientesCidade");
|
|
}
|
|
|
|
public DataTable ObterClientesPorGrupo()
|
|
{
|
|
return ExecutarViewDataTable("SELECT * FROM VW_ClientesGrupo");
|
|
}
|
|
|
|
public DataTable ObterDashboard()
|
|
{
|
|
return ExecutarViewDataTable("SELECT * FROM VW_ClientesDashboard");
|
|
}
|
|
|
|
public DataTable ObterClientesCompleto()
|
|
{
|
|
return ExecutarViewDataTable("SELECT * FROM VW_ClientesCompleto");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region HELPERS
|
|
|
|
private List<ModeloCliente> ExecutarViewLista(string query)
|
|
{
|
|
List<ModeloCliente> lista = [];
|
|
|
|
using SqlConnection conn = new(_conexao);
|
|
|
|
using SqlCommand cmd = new(query, conn);
|
|
|
|
conn.Open();
|
|
|
|
using SqlDataReader dr = cmd.ExecuteReader();
|
|
|
|
while (dr.Read())
|
|
{
|
|
lista.Add(PreencherModelo(dr));
|
|
}
|
|
|
|
return lista;
|
|
}
|
|
|
|
private DataTable ExecutarViewDataTable(string query)
|
|
{
|
|
DataTable dt = new();
|
|
|
|
using SqlConnection conn = new(_conexao);
|
|
|
|
using SqlDataAdapter da = new(query, conn);
|
|
|
|
da.Fill(dt);
|
|
|
|
return dt;
|
|
}
|
|
|
|
private static ModeloCliente PreencherModelo(SqlDataReader dr)
|
|
{
|
|
return new ModeloCliente
|
|
{
|
|
Id = Convert.ToInt32(dr["Id"]),
|
|
EmpresaId = Convert.ToInt32(dr["EmpresaId"]),
|
|
|
|
Nome = Convert.ToString(dr["Nome"]) ?? string.Empty,
|
|
NomeFantasia = Convert.ToString(dr["NomeFantasia"]) ?? string.Empty,
|
|
TipoPessoa = Convert.ToString(dr["TipoPessoa"]) ?? string.Empty,
|
|
Documento = Convert.ToString(dr["Documento"]) ?? string.Empty,
|
|
RG = Convert.ToString(dr["RG"]) ?? string.Empty,
|
|
InscricaoMunicipal = Convert.ToString(dr["InscricaoMunicipal"]) ?? string.Empty,
|
|
Contato = Convert.ToString(dr["Contato"]) ?? string.Empty,
|
|
Telefone1 = Convert.ToString(dr["Telefone1"]) ?? string.Empty,
|
|
Telefone2 = Convert.ToString(dr["Telefone2"]) ?? string.Empty,
|
|
Celular = Convert.ToString(dr["Celular"]) ?? string.Empty,
|
|
Whatsapp = Convert.ToString(dr["Whatsapp"]) ?? string.Empty,
|
|
Email = Convert.ToString(dr["Email"]) ?? string.Empty,
|
|
EmailNFe = Convert.ToString(dr["EmailNFe"]) ?? string.Empty,
|
|
Site = Convert.ToString(dr["Site"]) ?? string.Empty,
|
|
Grupo = Convert.ToString(dr["Grupo"]) ?? string.Empty,
|
|
Cep = Convert.ToString(dr["Cep"]) ?? string.Empty,
|
|
Endereco = Convert.ToString(dr["Endereco"]) ?? string.Empty,
|
|
Complemento = Convert.ToString(dr["Complemento"]) ?? string.Empty,
|
|
Bairro = Convert.ToString(dr["Bairro"]) ?? string.Empty,
|
|
Cidade = Convert.ToString(dr["Cidade"]) ?? string.Empty,
|
|
UF = Convert.ToString(dr["UF"]) ?? string.Empty,
|
|
Pais = Convert.ToString(dr["Pais"]) ?? string.Empty,
|
|
Observacoes = Convert.ToString(dr["Observacoes"]) ?? string.Empty,
|
|
TipoConsumidor = Convert.ToString(dr["TipoConsumidor"]) ?? string.Empty,
|
|
|
|
Numero = dr["Numero"] == DBNull.Value
|
|
? (int?)null
|
|
: Convert.ToInt32(dr["Numero"]),
|
|
|
|
DataNascimento = dr["DataNascimento"] == DBNull.Value
|
|
? (DateTime?)null
|
|
: Convert.ToDateTime(dr["DataNascimento"]),
|
|
|
|
LimiteCredito = dr["LimiteCredito"] == DBNull.Value
|
|
? 0
|
|
: Convert.ToDecimal(dr["LimiteCredito"]),
|
|
|
|
Bloqueado = dr["Bloqueado"] != DBNull.Value &&
|
|
Convert.ToBoolean(dr["Bloqueado"]),
|
|
|
|
Ativo = dr["Ativo"] != DBNull.Value &&
|
|
Convert.ToBoolean(dr["Ativo"]),
|
|
|
|
UltimaCompra = dr["UltimaCompra"] == DBNull.Value
|
|
? (DateTime?)null
|
|
: Convert.ToDateTime(dr["UltimaCompra"])
|
|
};
|
|
}
|
|
|
|
private static void PreencherParametros(SqlCommand cmd, ModeloCliente c)
|
|
{
|
|
cmd.Parameters.AddWithValue("@EmpresaId", c.EmpresaId);
|
|
cmd.Parameters.AddWithValue("@Nome", c.Nome);
|
|
cmd.Parameters.AddWithValue("@NomeFantasia", (object?)c.NomeFantasia ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@TipoPessoa", c.TipoPessoa);
|
|
cmd.Parameters.AddWithValue("@Documento", c.Documento);
|
|
cmd.Parameters.AddWithValue("@RG", (object?)c.RG ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@InscricaoMunicipal", (object?)c.InscricaoMunicipal ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@DataNascimento", c.DataNascimento ?? (object)DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Contato", (object?)c.Contato ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Telefone1", (object?)c.Telefone1 ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Telefone2", (object?)c.Telefone2 ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Celular", (object?)c.Celular ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Whatsapp", (object?)c.Whatsapp ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Email", (object?)c.Email ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@EmailNFe", (object?)c.EmailNFe ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Site", (object?)c.Site ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Grupo", (object?)c.Grupo ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Cep", (object?)c.Cep ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Endereco", (object?)c.Endereco ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Numero", c.Numero ?? (object)DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Complemento", (object?)c.Complemento ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Bairro", (object?)c.Bairro ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Cidade", (object?)c.Cidade ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@UF", (object?)c.UF ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Pais", (object?)c.Pais ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@LimiteCredito", c.LimiteCredito);
|
|
cmd.Parameters.AddWithValue("@Bloqueado", c.Bloqueado);
|
|
cmd.Parameters.AddWithValue("@ObservacoesCobranca", (object?)c.ObservacoesCobranca ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@VendedorPadraoId", c.VendedorPadraoId ?? (object)DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@TipoConsumidor", (object?)c.TipoConsumidor ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Observacoes", (object?)c.Observacoes ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@CampoExtra1", (object?)c.CampoExtra1 ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@CampoExtra2", (object?)c.CampoExtra2 ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@CampoExtra3", (object?)c.CampoExtra3 ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Bitcoin", (object?)c.Bitcoin ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Ethereum", (object?)c.Ethereum ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Litecoin", (object?)c.Litecoin ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@Ativo", c.Ativo);
|
|
cmd.Parameters.AddWithValue("@UltimaCompra", c.UltimaCompra ?? (object)DBNull.Value);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |