Last active
July 21, 2024 21:44
-
-
Save lucassmacedo/d91ec03273101eaa6f8adabef6178027 to your computer and use it in GitHub Desktop.
V_INTEGRACAO_PEDIDOS.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with PRODUTOS as (SELECT NUMPED, | |
json_arrayagg( | |
JSON_OBJECT( | |
'codigoSistemaGestao' VALUE CODPROD, | |
'codigoSistemaGestao' VALUE CAST(ABS(QT) AS DECIMAL(18, 2)), | |
'codigoSistemaGestao' VALUE 'N', | |
'codigoSistemaGestaoGrupoProduto' VALUE 'DISDAL' | |
) | |
RETURNING CLOB | |
) AS DADOS | |
FROM PCPEDI | |
group by NUMPED) | |
SELECT PCPEDC.NUMPED AS CODIGO, | |
NULL AS TOKEN, | |
TO_DATE(DATA, 'YYYY/MM/DD') AS DATA, | |
JSON_OBJECT( | |
'tipoCliente' VALUE PCCLIENT.TIPOFJ, | |
'documentoCliente' VALUE REGEXP_REPLACE(PCCLIENT.CGCENT, '[^0-9]', ''), | |
'nomeOrigem' VALUE PCCLIENT.FANTASIA, | |
'codigoOriginal' VALUE PCPEDC.NUMPED, | |
'codigoPedido' VALUE PCPEDC.NUMPED, | |
'dataEmissao' VALUE PCPEDC.DATA, | |
'dataPrimeiroVencimento' VALUE PCPEDC.DTENTREGA, | |
'valorBruto' VALUE CAST(ABS(PCPEDC.VLTOTAL) AS DECIMAL(18, 2)), | |
'valorEntrada' VALUE 0, | |
'valorLiquido' VALUE CAST(ABS(PCPEDC.VLATEND) AS DECIMAL(18, 2)), | |
'valorSaldo' VALUE CAST(CASE WHEN PCPEDC.POSICAO = 'F' THEN 0 ELSE ABS(PCPEDC.VLATEND) END AS DECIMAL(18, 2)), | |
'tipoRepresentante' VALUE PCUSUARI.TIPOVEND, | |
'documentoRepresentante' VALUE REGEXP_REPLACE(DECODE(PCUSUARI.tipovend, | |
'J', PCUSUARI.CGC, | |
'F', PCUSUARI.CPF, | |
'E', PCUSUARI.CODUSUR | |
), '[^0-9]', ''), | |
'tipoPagamento' VALUE CASE | |
WHEN PCPEDC.CODCOB NOT IN ('DH', 'DEP', 'CH') THEN 0 | |
WHEN PCPEDC.CODCOB IN ('DH', 'CH') THEN 1 | |
WHEN PCPEDC.CODCOB IN ('DEP') THEN 2 | |
END, | |
'dataAtualizaSituacao' VALUE NULL, | |
'dataFaturamento' VALUE PCPEDC.DTFAT, | |
'diasEntreParcelas' VALUE PCPEDC.PRAZOMEDIO, | |
'Prazo' VALUE (PCPEDC.PRAZO1) || DECODE(PCPEDC.PRAZO2, NULL, '', '/') || | |
(PCPEDC.PRAZO2) || DECODE(PCPEDC.PRAZO3, NULL, '', '/') || | |
(PCPEDC.PRAZO3) || DECODE(PCPEDC.PRAZO4, NULL, '', '/') || | |
(PCPEDC.PRAZO4) || DECODE(PCPEDC.PRAZO5, NULL, '', '/') || | |
(PCPEDC.PRAZO5) || DECODE(PCPEDC.PRAZO6, NULL, '', '/') || | |
(PCPEDC.PRAZO6) || DECODE(PCPEDC.PRAZO7, NULL, '', '/') || | |
(PCPEDC.PRAZO7) || DECODE(PCPEDC.PRAZO8, NULL, '', '/') || | |
(PCPEDC.PRAZO8) || DECODE(PCPEDC.PRAZO9, NULL, '', '/') || | |
(PCPEDC.PRAZO9) || DECODE(PCPEDC.PRAZO10, NULL, '', '/') || | |
(PCPEDC.PRAZO10) || DECODE(PCPEDC.PRAZO11, NULL, '', '/') || | |
(PCPEDC.PRAZO11) || DECODE(PCPEDC.PRAZO12, NULL, '', '/') || | |
(PCPEDC.PRAZO12), | |
'quantidadeParcelas' VALUE DECODE(PCPEDC.PRAZO1, NULL, 0, 1) + | |
DECODE(PCPEDC.PRAZO2, NULL, 0, 1) + | |
DECODE(PCPEDC.PRAZO3, NULL, 0, 1) + | |
DECODE(PCPEDC.PRAZO4, NULL, 0, 1) + | |
DECODE(PCPEDC.PRAZO5, NULL, 0, 1) + | |
DECODE(PCPEDC.PRAZO6, NULL, 0, 1) + | |
DECODE(PCPEDC.PRAZO7, NULL, 0, 1) + | |
DECODE(PCPEDC.PRAZO8, NULL, 0, 1) + | |
DECODE(PCPEDC.PRAZO9, NULL, 0, 1) + | |
DECODE(PCPEDC.PRAZO10, NULL, 0, 1) + | |
DECODE(PCPEDC.PRAZO11, NULL, 0, 1) + | |
DECODE(PCPEDC.PRAZO12, NULL, 0, 1), | |
'codigoMoeda' VALUE 'BRL', | |
'solicitante' VALUE NULL | |
) AS pedido, | |
JSON_ARRAY( | |
JSON_OBJECT( | |
'desassociar' VALUE NULL, | |
'documento' VALUE NULL, | |
'origem' VALUE NULL, | |
'tipoCliente' VALUE NULL, | |
'tipoRelacao' VALUE NULL | |
) | |
) as clientesRelacionados, | |
JSON_OBJECT( | |
'codigoSistemaGestao' VALUE PCPEDC.POSICAO | |
) as situacao, | |
JSON_OBJECT( | |
'cidade' value PCCLIENT.MUNICENT, | |
'uf' value DECODE(PCUSUARI.tipovend, 'E', 'EX', PCCLIENT.ESTENT), | |
'rua' value PCCLIENT.ENDERENT, | |
'bairro' value PCCLIENT.BAIRROENT, | |
'cep' value PCCLIENT.CEPENT, | |
'numero' value PCCLIENT.NUMEROENT, | |
'complemento' value PCCLIENT.COMPLEMENTOENT, | |
'tipoEndereco' value 5, | |
'confirmado' value null, | |
'apelido' value null | |
) as endereco, | |
JSON_ARRAY( | |
JSON_OBJECT( | |
'desassociar' VALUE NULL, | |
'codigoSistemaGestao' VALUE NULL, | |
'valor' VALUE NULL | |
) | |
) as divisorLimite, | |
JSON_ARRAY( | |
JSON_OBJECT( | |
'desassociar' VALUE NULL, | |
'codigoSistemaGestao' VALUE 'FIL-' || RTRIM(pcpedc.codfilial), | |
'nome' VALUE 'FIL-' || RTRIM(PCPEDC.codfilial), | |
'nomeCategoria' VALUE 'FILIAL' | |
), | |
JSON_OBJECT( | |
'desassociar' VALUE NULL, | |
'codigoSistemaGestao' VALUE 'TDV-' || RTRIM(pcpedc.CONDVENDA), | |
'nome' VALUE 'TDV-' || RTRIM(PCPEDC.CONDVENDA), | |
'nomeCategoria' VALUE 'TIPO DE VENDA' | |
), | |
JSON_OBJECT( | |
'desassociar' VALUE NULL, | |
'codigoSistemaGestao' VALUE 'COB-' || RTRIM(pcpedc.CODCOB), | |
'nome' VALUE PCPEDC.CODCOB, | |
'nomeCategoria' VALUE 'COBRANCA' | |
) | |
) as parametrizacoesCategorias, | |
JSON_ARRAY( | |
JSON_OBJECT( | |
'complemento' VALUE NULL, | |
'dataCadastro' VALUE NULL, | |
'login' VALUE NULL, | |
'excluir' VALUE NULL | |
) | |
) as informacoesComplementares, | |
JSON_ARRAY( | |
JSON_OBJECT( | |
'desassociar' VALUE NULL, | |
'codigoSistemaGestao' VALUE NULL | |
) | |
) as motivos, | |
JSON_ARRAY( | |
JSON_OBJECT( | |
'excluir' VALUE NULL, | |
'complemento' VALUE NULL, | |
'login' VALUE NULL, | |
'dataCadastro' VALUE NULL | |
) | |
) as informacoesComplementares, | |
JSON_ARRAY( | |
JSON_OBJECT( | |
'desassociar' VALUE NULL, | |
'formaPagamento' VALUE NULL, | |
'documento' VALUE NULL, | |
'statusOperacao' VALUE NULL, | |
'observacao' VALUE NULL, | |
'parcelas' VALUE JSON_ARRAY( | |
JSON_OBJECT( | |
'dataVencimento' VALUE NULL, | |
'valor' VALUE NULL, | |
'observacao' VALUE NULL | |
) | |
) | |
) | |
) as condicoesPagamento | |
FROM PCPEDC | |
JOIN PCCLIENT ON PCCLIENT.CODCLI = PCPEDC.CODCLI | |
JOIN PCUSUARI ON PCUSUARI.CODUSUR = PCCLIENT.CODUSUR1 | |
JOIN PRODUTOS on PRODUTOS.NUMPED = PCPEDC.NUMPED | |
JOIN PCPLPAG ON PCPLPAG.CODPLPAG = PCPEDC.CODPLPAG | |
LEFT JOIN PCCOB ON PCCOB.CODCOB = PCPEDC.CODCOB | |
AND ROWNUM < 10; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment