-- Tabelas Controle de Perdas_dataset

CREATE OR REPLACE VIEW DIM_FILIAL AS


-- DIM_FILIAL
SELECT
    CODIGO   AS CODFILIAL,
    FANTASIA AS NOME_FILIAL,
    CODIGO || ' - ' || REGEXP_REPLACE(FANTASIA, '^JURUNENSE[[:space:]]*', '') AS FILIAL_COMPLETA
FROM PCFILIAL;


-- DIM_DEPARTAMENTO
SELECT
    CODEPTO,
    DESCRICAO AS NOME_DEPARTAMENTO,
    CODEPTO || ' - ' || DESCRICAO AS DEPTO_COMPLETO
FROM PCDEPTO;


-- DIM_MARCA
SELECT
    CODMARCA,
    MARCA AS NOME_MARCA,
    CODMARCA || ' - ' || MARCA AS MARCA_COMPLETA
FROM PCMARCA;


-- DIM_PRODUTO
SELECT
    P.CODPROD,
    P.DESCRICAO AS NOME_PRODUTO,
    P.CODEPTO,
    P.CODMARCA,
    P.CODPROD || ' - ' || P.DESCRICAO AS PRODUTO_COMPLETO
FROM PCPRODUT P;


-- DIM_CALENDARIO
SELECT DISTINCT
    DATA,
    EXTRACT(YEAR  FROM DATA) AS ANO,
    EXTRACT(MONTH FROM DATA) AS MES,
    EXTRACT(DAY   FROM DATA) AS DIA
FROM (
    SELECT TRUNC(DTSAIDA) AS DATA FROM VIEW_VENDAS_RESUMO_FATURAMENTO
    UNION
    SELECT TRUNC(DTENT)   AS DATA FROM VIEW_DEVOL_RESUMO_FATURAMENTO
    UNION
    SELECT TRUNC(DTMOV)   AS DATA FROM PCMOV
);

-- DIM_TIPO_BLOQUEIO

SELECT 1 AS TIPO_BLOQUEIO_ID, 'BLOQUEIO EXPOSICAO' AS TIPO_BLOQUEIO, 1 AS ORDEM, 'S' AS ATIVO FROM DUAL
UNION ALL
SELECT 2, 'BLOQUEIO MANUAL',   2, 'S' FROM DUAL
UNION ALL
SELECT 3, 'BLOQUEIO ENTRADA',  3, 'S' FROM DUAL
UNION ALL
SELECT 4, 'BLOQUEIO AVARIA',   4, 'S' FROM DUAL;

-- DIM_MOTIVO_BLOQUEIO

SELECT
  CODDEVOL   AS CODMOTIVO,
  MOTIVO,
  'S'        AS ATIVO
FROM PCTABDEV;

-- DIM_FAIXA_PRIM

SELECT 1 AS FAIXA_ID, '1 - 0 A 3 DIAS'   AS FAIXA_PRIM, 0  AS DIAS_INICIO, 3  AS DIAS_FIM, 1 AS ORDEM FROM DUAL
UNION ALL
SELECT 2, '2 - 4 A 7 DIAS',   4,  7,  2 FROM DUAL
UNION ALL
SELECT 3, '3 - 8 A 14 DIAS',  8, 14,  3 FROM DUAL
UNION ALL
SELECT 4, '4 - 15 A 30 DIAS',15, 30,  4 FROM DUAL
UNION ALL
SELECT 5, '5 - 30 A 60 DIAS',31, 60,  5 FROM DUAL
UNION ALL
SELECT 6, '6 - 60 + DIAS',   61, 9999,6 FROM DUAL;

-- FATO_FATURAMENTO-filial
SELECT
    NF.CODFILIAL,
    TRUNC(NF.DTSAIDA) AS DATA,
    NF.CODPROD,
    SUM(NF.VLVENDA)    AS FATURAMENTO_BRUTO,
    SUM(NF.VLCUSTOFIN) AS CUSTO_TOTAL
FROM VIEW_VENDAS_RESUMO_FATURAMENTO NF
WHERE NF.CONDVENDA IN (1,7)
  AND NF.DTCANCEL IS NULL
GROUP BY
    NF.CODFILIAL,
    TRUNC(NF.DTSAIDA),
    NF.CODPROD;


-- FATO_DEVOLUCAO-filial
SELECT
    DV.CODFILIAL,
    TRUNC(DV.DTENT) AS DATA,
    DV.CODPROD,
    SUM(DV.VLDEVOLUCAO) AS DEVOLUCAO,
    SUM(DV.VLCUSTOFIN)  AS CUSTO_DEVOLUCAO
FROM VIEW_DEVOL_RESUMO_FATURAMENTO DV
WHERE DV.CONDVENDA IN (1,7,8)
  AND DV.DTCANCEL IS NULL
GROUP BY
    DV.CODFILIAL,
    TRUNC(DV.DTENT),
    DV.CODPROD;


-- FATO_FATURAMENTO-supervmov
SELECT
    NF.CODSUPERVMOV,
    TRUNC(NF.DTSAIDA) AS DATA,
    NF.CODPROD,
    SUM(NF.VLVENDA)    AS FATURAMENTO_BRUTO,
    SUM(NF.VLCUSTOFIN) AS CUSTO_TOTAL
FROM VIEW_VENDAS_RESUMO_FATURAMENTO NF
WHERE NF.CONDVENDA IN (1,7)
  AND NF.DTCANCEL IS NULL
GROUP BY
    NF.CODSUPERVMOV,
    TRUNC(NF.DTSAIDA),
    NF.CODPROD;


-- FATO_DEVOLUCAO-supervmov
SELECT
    DV.CODSUPERVMOV,
    TRUNC(DV.DTENT) AS DATA,
    DV.CODPROD,
    SUM(DV.VLDEVOLUCAO) AS DEVOLUCAO,
    SUM(DV.VLCUSTOFIN)  AS CUSTO_DEVOLUCAO
FROM VIEW_DEVOL_RESUMO_FATURAMENTO DV
WHERE DV.CONDVENDA IN (1,7,8)
  AND DV.DTCANCEL IS NULL
GROUP BY
    DV.CODSUPERVMOV,
    TRUNC(DV.DTENT),
    DV.CODPROD;

-- FatoAjusteEstoque
SELECT
    M.CODFILIAL,
    TRUNC(M.DTMOV) AS DATA, 
    M.CODPROD,
    SUM(
        CASE 
            WHEN M.CODOPER IN ('EA','EI') THEN  M.QT * ROUND(M.PUNIT,2)
            WHEN M.CODOPER IN ('SA','SI') THEN (M.QT * -1) * ROUND(M.PUNIT,2)
            ELSE 0
        END
    ) AS VALOR_AJUSTE
FROM PCMOV M
WHERE M.CODOPER IN ('EA','EI','SA','SI')
GROUP BY
    M.CODFILIAL,
    TRUNC(M.DTMOV),
    M.CODPROD;

-- FatoAjusteDetalhe
SELECT
    M.CODFILIAL,
    TRUNC(M.DTMOV) AS DATA,
    M.CODPROD,
    COALESCE(L1.NOMEFUNC, L2.NOMEFUNC) AS RESPONSAVEL_AJUSTE,
    COALESCE(L1.HISTORICO,  L2.HISTORICO)  AS HISTORICO,
    COALESCE(L1.HISTORICO2, L2.HISTORICO2) AS HISTORICO2,
    DECODE(M.CODOPER,
        'EA','ENTRADA AJUSTE',
        'EI','ENTRADA INVENTARIO',
        'SA','SAIDA AJUSTE',
        'SI','SAIDA INVENTARIO'
select * from pcpedc    ) AS TIPO_OPERACAO,
    CASE
        WHEN M.CODOPER IN ('EA','EI') THEN  M.QT
        WHEN M.CODOPER IN ('SA','SI') THEN (M.QT * -1)
        ELSE 0
    END AS QT_AJUSTE,
    CASE 
        WHEN M.CODOPER IN ('EA','EI') THEN  M.QT * ROUND(M.PUNIT,2)
        WHEN M.CODOPER IN ('SA','SI') THEN (M.QT * -1) * ROUND(M.PUNIT,2)
        ELSE 0
    END AS VALOR_AJUSTE
FROM PCMOV M
LEFT JOIN PCLANC L1 
       ON M.CODOPER IN ('EA','EI')
      AND L1.NUMTRANSENT = M.NUMTRANSENT
LEFT JOIN PCLANC L2 
       ON M.CODOPER IN ('SA','SI')
      AND L2.NUMTRANSVENDA = M.NUMTRANSVENDA
WHERE M.CODOPER IN ('EA','EI','SA','SI');
