USE [Levelcode-LevelOS]
GO

/* =========================================================
   VIEW: BOLETOS ABERTOS
========================================================= */
CREATE OR ALTER VIEW VW_BoletosAbertos
AS
SELECT
    *
FROM Boletos
WHERE
    STATUS = 'ABERTO'
    AND PG = 0;
GO

/* =========================================================
   VIEW: BOLETOS VENCIDOS
========================================================= */
CREATE OR ALTER VIEW VW_BoletosVencidos
AS
SELECT
    *,
    DATEDIFF(DAY, VENCE, GETDATE()) AS DIAS_ATRASO
FROM Boletos
WHERE
    PG = 0
    AND VENCE < CAST(GETDATE() AS DATE)
    AND STATUS = 'ABERTO';
GO

/* =========================================================
   VIEW: BOLETOS PAGOS
========================================================= */
CREATE OR ALTER VIEW VW_BoletosPagos
AS
SELECT
    *,
    ISNULL(VL_PAGO, VALOR) AS VALOR_RECEBIDO
FROM Boletos
WHERE
    PG = 1
    OR STATUS = 'PAGO';
GO

/* =========================================================
   VIEW: RESUMO FINANCEIRO
========================================================= */
CREATE OR ALTER VIEW VW_BoletosResumoFinanceiro
AS
SELECT
    COUNT(*) AS TOTAL_BOLETOS,

    SUM(CASE
        WHEN STATUS = 'PAGO'
        THEN VALOR
        ELSE 0
    END) AS TOTAL_PAGO,

    SUM(CASE
        WHEN STATUS = 'ABERTO'
        THEN VALOR
        ELSE 0
    END) AS TOTAL_ABERTO,

    SUM(CASE
        WHEN STATUS = 'VENCIDO'
        THEN VALOR
        ELSE 0
    END) AS TOTAL_VENCIDO
FROM Boletos;
GO

/* =========================================================
   VIEW: DASHBOARD
========================================================= */
CREATE OR ALTER VIEW VW_BoletosDashboard
AS
SELECT
    ID_BOLETO,
    CODIGO,
    SACADO,
    STATUS,
    VALOR,
    VENCE,

    CASE
        WHEN PG = 1 THEN 'PAGO'
        WHEN VENCE < GETDATE() THEN 'VENCIDO'
        ELSE 'EM DIA'
    END AS SITUACAO,

    DATEDIFF(DAY, GETDATE(), VENCE) AS DIAS_PARA_VENCER
FROM Boletos;
GO

/* =========================================================
   VIEW: BOLETOS COM ARQUIVO
========================================================= */
CREATE OR ALTER VIEW VW_BoletosComArquivo
AS
SELECT
    *
FROM Boletos
WHERE
    ARQUIVO_CAMINHO IS NOT NULL
    OR ARQUIVO_DADOS IS NOT NULL;
GO

/* =========================================================
   VIEW: BOLETOS IMPORTADOS
========================================================= */
CREATE OR ALTER VIEW VW_BoletosImportados
AS
SELECT
    *
FROM Boletos
WHERE
    IMPORTACAO = 1;
GO

/* =========================================================
   VIEW: PRÓXIMOS VENCIMENTOS
========================================================= */
CREATE OR ALTER VIEW VW_BoletosProximosVencimentos
AS
SELECT
    *
FROM Boletos
WHERE
    PG = 0
    AND VENCE BETWEEN CAST(GETDATE() AS DATE)
    AND DATEADD(DAY, 7, CAST(GETDATE() AS DATE));
GO

/* =========================================================
   VIEW: BOLETOS DE HOJE
========================================================= */
CREATE OR ALTER VIEW VW_BoletosHoje
AS
SELECT
    *
FROM Boletos
WHERE
    CAST(VENCE AS DATE) = CAST(GETDATE() AS DATE);
GO

/* =========================================================
   VIEW: BOLETOS DA SEMANA
========================================================= */
CREATE OR ALTER VIEW VW_BoletosSemana
AS
SELECT
    *
FROM Boletos
WHERE
    VENCE BETWEEN CAST(GETDATE() AS DATE)
    AND DATEADD(DAY, 7, CAST(GETDATE() AS DATE));
GO

/* =========================================================
   VIEW: BOLETOS SEM ARQUIVO
========================================================= */
CREATE OR ALTER VIEW VW_BoletosSemArquivo
AS
SELECT
    *
FROM Boletos
WHERE
    ARQUIVO_CAMINHO IS NULL
    AND ARQUIVO_DADOS IS NULL;
GO

/* =========================================================
   VIEW: BOLETOS PENDENTES DE WEBHOOK
========================================================= */
CREATE OR ALTER VIEW VW_BoletosPendentesWebhook
AS
SELECT
    *
FROM Boletos
WHERE
    STATUS = 'PAGO'
    AND IMPORTACAO = 0;
GO