USE [Levelcode-LevelOS]
GO

CREATE OR ALTER VIEW VW_Chegues
AS
SELECT *
FROM Chegues;
GO

CREATE OR ALTER VIEW VW_CheguesCompensados
AS
SELECT *
FROM Chegues
WHERE OK = 'SIM';
GO

CREATE OR ALTER VIEW VW_CheguesPendentes
AS
SELECT *
FROM Chegues
WHERE
    OK IS NULL
    OR OK = ''
    OR OK = 'NAO'
    OR OK = 'NÃO';
GO

CREATE OR ALTER VIEW VW_CheguesClientes
AS
SELECT *
FROM Chegues
WHERE
    CLIENTE IS NOT NULL
    AND CLIENTE <> '';
GO

CREATE OR ALTER VIEW VW_CheguesFornecedores
AS
SELECT *
FROM Chegues
WHERE
    FORNECEDOR IS NOT NULL
    AND FORNECEDOR <> '';
GO

CREATE OR ALTER VIEW VW_CheguesReceber
AS
SELECT *
FROM Chegues
WHERE TIPO = 'RECEBER';
GO

CREATE OR ALTER VIEW VW_CheguesPagar
AS
SELECT *
FROM Chegues
WHERE TIPO = 'PAGAR';
GO

CREATE OR ALTER VIEW VW_CheguesHoje
AS
SELECT *
FROM Chegues
WHERE COMPENSAR = CONVERT(VARCHAR, GETDATE(), 103);
GO

CREATE OR ALTER VIEW VW_CheguesDashboard
AS
SELECT
    COUNT(*) AS TOTAL,

    SUM(CASE
        WHEN OK = 'SIM'
        THEN 1
        ELSE 0
    END) AS COMPENSADOS,

    SUM(CASE
        WHEN OK IS NULL
        OR OK = ''
        OR OK = 'NAO'
        OR OK = 'NÃO'
        THEN 1
        ELSE 0
    END) AS PENDENTES
FROM Chegues;
GO