select
pvfvcodrem,
'16' as "IDENTIFICADOR",--1	 
null as ID,--2
pvfvcodrem as codigo,--3
pvtccod as codFormaPagamento,--4	 
pvplpgcod as codCondicaoPagamento,--5	 
case 
	when pvsta = 'AP' then 1 
	when pvsta = 'AB' then 2 
	when pvsta = 'AA' then 3 
	when pvsta = 'MP' then 4 
	when pvsta = 'MT' then 5 
	when pvsta = 'FT' then 6 
	when pvsta = 'EX' then 7 
end  as codStatus,--6
pvempparcod as codFilial,--7	 
pvcliparcod as codCliente,--8
(select parcnpjcpf  from par where parcod = pvcliparcod) as cpfCNPJCliente,--9
(select f_livro_do_cliente(pvcliparcod, pvempparcod)) as codListaPreco,--10	 
pvrcaparcod as codVendedor,--11	 
null as codPortador,--12
pvfvcodrem as numPedido,--13	 
null as sequencialVendedor,--14
null as numOrdemCompra,--15	 
pvcadhorario as dataEmissao, --16	 
(select sum(pvivltotal) from pvi where pvipvcod = pvcod) as valorProdutosComDescAcres,--17
(select sum(pvivltab*pviqtdeatecar) from pvi where pvipvcod = pvcod) as valorProdutosSemDescAcres,--18
(select count(pviprocod) from pvi where pvipvcod = pvcod) as qtdeItens,--19	 
null as IMEI,--20
pvopnfcod as operacaoFiscal,--21
pvobsped as observacao,--22
null,--23
null,--24
null,--25
case 
	when opnftopnfcod = 'VE' then 213
	when opnftopnfcod = 'BO' then 263
	when opnftopnfcod = 'TR' then 273
	else 1
end as codTipoPedido,--26
pvdtprog as previsaoEntrega, --27	 
pvrcaparcod as codVendedorEmissor,--28
null as latitude,--29	 
null as longitude,--30	 
null as origemPedido,--31
pvvlfrete as valorFrete,--32	 
(select sum(pvivltotal+pvivlst+pvivlipi) from pvi where pvipvcod = pvcod) as valorTotalPedido,--33
null as tipoFrete,--34
(select sum(pvivlipi) from pvi where pvipvcod = pvcod) as valorIPI,--35
(select sum(pvivlst) from pvi where pvipvcod = pvcod) as valorICMS_ST,--36
case when pvsta = 'EX'then 1 else 0 end as cancelado,--37
pvobsped as observacaoCliente,--38
'R$' as tipoDesconto,--39
0 as percentualDescontoDoPedido,--40
pvvldesc as valorDescontoDoPedido,--41  
0 as percJurosCondPag,--42
null as provider,--43
null as valorTotalComissao,--44
null as codTransportadora,--45
null as codRedespacho,--46
null as uniqueidEnderecoEntrega,--47
null as codsegmento,--48
null as idERP,--49	 	 
null as usouTabelaDesconto,--50
null as codTipoDesconto,--51
null as totalTaxaAdicional,--52
null as valorAbaterBCComissao,--53
null as numPedidoOrigem,--54
null as codMixProduto,--55
null as IdCliente,--56	 
null as pesoBruto,--57
null as pesoLiquido,--58
null as cubagem,--59
null as uniqueIdEnderecoFaturamento,--60	 
null as uniqueIdEnderecoCobranca,--61	 
pvfvresposta as observacaoIntegracao,--62	 
null as percMargemContribuicao,--63	 
null as custoReposicaoNaVenda,--64	 
null as codStatusLiberacao,--65	 
pvbloqobs as motivoBloqueio,--66	 
pvbloqusuparcod as idUsuarioAvaliou,--67	 
null as motivoAvaliacao,--68	 
null as descontoDestacado,--69	 
null as percDescontoDestacado,--70
null as percDescontoSobreDesconto--71
from pv
join opnf on pvopnfcod = opnfcod
where pvfvcodrem is not null
and pvcadhorario between now()-interval '7 Days' and now()
ExecutarScriptSempre
