USE [Levelcode-LevelOS]
GO

/* =========================================================
   VIEW: TODOS OS CLIENTES
========================================================= */
CREATE OR ALTER VIEW VW_Clientes
AS
SELECT
    *
FROM Clientes;
GO

/* =========================================================
   VIEW: CLIENTES ATIVOS
========================================================= */
CREATE OR ALTER VIEW VW_ClientesAtivos
AS
SELECT
    *
FROM Clientes
WHERE Ativo = 1;
GO

/* =========================================================
   VIEW: CLIENTES INATIVOS
========================================================= */
CREATE OR ALTER VIEW VW_ClientesInativos
AS
SELECT
    *
FROM Clientes
WHERE Ativo = 0;
GO

/* =========================================================
   VIEW: CLIENTES BLOQUEADOS
========================================================= */
CREATE OR ALTER VIEW VW_ClientesBloqueados
AS
SELECT
    *
FROM Clientes
WHERE Bloqueado = 1;
GO

/* =========================================================
   VIEW: CLIENTES COM LIMITE
========================================================= */
CREATE OR ALTER VIEW VW_ClientesComLimite
AS
SELECT
    *
FROM Clientes
WHERE LimiteCredito > 0;
GO

/* =========================================================
   VIEW: CLIENTES SEM LIMITE
========================================================= */
CREATE OR ALTER VIEW VW_ClientesSemLimite
AS
SELECT
    *
FROM Clientes
WHERE
    LimiteCredito IS NULL
    OR LimiteCredito = 0;
GO

/* =========================================================
   VIEW: CLIENTES PF
========================================================= */
CREATE OR ALTER VIEW VW_ClientesPF
AS
SELECT
    *
FROM Clientes
WHERE TipoPessoa = 'PF';
GO

/* =========================================================
   VIEW: CLIENTES PJ
========================================================= */
CREATE OR ALTER VIEW VW_ClientesPJ
AS
SELECT
    *
FROM Clientes
WHERE TipoPessoa = 'PJ';
GO

/* =========================================================
   VIEW: CLIENTES COM WHATSAPP
========================================================= */
CREATE OR ALTER VIEW VW_ClientesWhatsapp
AS
SELECT
    *
FROM Clientes
WHERE
    Whatsapp IS NOT NULL
    AND Whatsapp <> '';
GO

/* =========================================================
   VIEW: CLIENTES COM EMAIL
========================================================= */
CREATE OR ALTER VIEW VW_ClientesEmail
AS
SELECT
    *
FROM Clientes
WHERE
    Email IS NOT NULL
    AND Email <> '';
GO

/* =========================================================
   VIEW: CLIENTES COM EMAIL NFE
========================================================= */
CREATE OR ALTER VIEW VW_ClientesEmailNFe
AS
SELECT
    *
FROM Clientes
WHERE
    EmailNFe IS NOT NULL
    AND EmailNFe <> '';
GO

/* =========================================================
   VIEW: CLIENTES COM CRIPTO
========================================================= */
CREATE OR ALTER VIEW VW_ClientesCripto
AS
SELECT
    *
FROM Clientes
WHERE
    Bitcoin IS NOT NULL
    OR Ethereum IS NOT NULL
    OR Litecoin IS NOT NULL;
GO

/* =========================================================
   VIEW: CLIENTES POR CIDADE
========================================================= */
CREATE OR ALTER VIEW VW_ClientesCidade
AS
SELECT
    Cidade,
    UF,
    COUNT(*) AS TOTAL
FROM Clientes
GROUP BY Cidade, UF;
GO

/* =========================================================
   VIEW: CLIENTES POR GRUPO
========================================================= */
CREATE OR ALTER VIEW VW_ClientesGrupo
AS
SELECT
    Grupo,
    COUNT(*) AS TOTAL
FROM Clientes
GROUP BY Grupo;
GO

/* =========================================================
   VIEW: CLIENTES COM ÚLTIMA COMPRA
========================================================= */
CREATE OR ALTER VIEW VW_ClientesUltimaCompra
AS
SELECT
    *
FROM Clientes
WHERE UltimaCompra IS NOT NULL;
GO

/* =========================================================
   VIEW: CLIENTES SEM COMPRA
========================================================= */
CREATE OR ALTER VIEW VW_ClientesSemCompra
AS
SELECT
    *
FROM Clientes
WHERE UltimaCompra IS NULL;
GO

/* =========================================================
   VIEW: CLIENTES RECENTES
========================================================= */
CREATE OR ALTER VIEW VW_ClientesRecentes
AS
SELECT
    *
FROM Clientes
WHERE CriadoEm >= DATEADD(DAY, -30, GETDATE());
GO

/* =========================================================
   VIEW: CLIENTES PARA COBRANÇA
========================================================= */
CREATE OR ALTER VIEW VW_ClientesCobranca
AS
SELECT
    Id,
    Nome,
    Documento,
    Telefone1,
    Celular,
    Whatsapp,
    Email,
    ObservacoesCobranca
FROM Clientes
WHERE Ativo = 1;
GO

/* =========================================================
   VIEW: DASHBOARD CLIENTES
========================================================= */
CREATE OR ALTER VIEW VW_ClientesDashboard
AS
SELECT
    COUNT(*) AS TOTAL_CLIENTES,

    SUM(CASE
        WHEN Ativo = 1
        THEN 1
        ELSE 0
    END) AS CLIENTES_ATIVOS,

    SUM(CASE
        WHEN Ativo = 0
        THEN 1
        ELSE 0
    END) AS CLIENTES_INATIVOS,

    SUM(CASE
        WHEN Bloqueado = 1
        THEN 1
        ELSE 0
    END) AS CLIENTES_BLOQUEADOS,

    SUM(CASE
        WHEN TipoPessoa = 'PF'
        THEN 1
        ELSE 0
    END) AS TOTAL_PF,

    SUM(CASE
        WHEN TipoPessoa = 'PJ'
        THEN 1
        ELSE 0
    END) AS TOTAL_PJ
FROM Clientes;
GO

USE [Levelcode-LevelOS]
GO

USE [Levelcode-LevelOS]
GO

/* =========================================================
   VIEW: CLIENTES COMPLETO
========================================================= */
CREATE OR ALTER VIEW VW_ClientesCompleto
AS
SELECT
    -- CLIENTE
    C.Id,
    C.EmpresaId,
    C.Nome,
    C.NomeFantasia,
    C.TipoPessoa,
    C.Documento,
    C.RG,
    C.InscricaoMunicipal,
    C.DataNascimento,
    C.Contato,
    C.Telefone1,
    C.Telefone2,
    C.Celular,
    C.Whatsapp,
    C.Email,
    C.EmailNFe,
    C.Site,
    C.Grupo,
    C.Cep,
    C.Endereco,
    C.Numero,
    C.Complemento,
    C.Bairro,
    C.Cidade,
    C.UF,
    C.Pais,
    C.LimiteCredito,
    C.Bloqueado,
    C.ObservacoesCobranca,
    C.VendedorPadraoId,
    C.TipoConsumidor,
    C.Observacoes,
    C.CampoExtra1,
    C.CampoExtra2,
    C.CampoExtra3,
    C.Bitcoin,
    C.Ethereum,
    C.Litecoin,
    C.Ativo,
    C.UltimaCompra,
    C.CriadoEm,
    C.AtualizadoEm,

    -- EMPRESA
    E.IdEmpresa,
    E.Nome AS EmpresaNome

FROM Clientes C
INNER JOIN Empresa E
    ON C.EmpresaId = E.IdEmpresa;
GO