SELECT F.SFI_CODIGO AS formaPagto, F.CNPJCRED        
     , F.BANDEIRA_CARTAO, F.TIPO_INTEGRACAO          
     , coalesce(P.troco_dinheiro ,0.00) AS troco     
     , P.ITE_003 AS Valor                            
     , 999 AS admCartao                              
     , P.ITE_001 AS nPagto                           
     , P.AUTORIZACAO                                 
FROM ENCERRAVENDA EV                                 
INNER JOIN ENCERRAVENDAITEM P ON                     
   (P.ENC_001 = EV.ENC_001) AND                      
   (P.EMP_001 = EV.EMP_001)                          
INNER JOIN FORMAPGTO F ON                            
   (F.EMP_001 = P.EMP_001) AND                       
   (F.FOR_001 = P.id_formapgto)                      
INNER JOIN VENDA V ON                                
   (V.VEN_001 = EV.VEN_001) AND                      
   (V.EMP_001 = EV.EMP_001) AND                      
   CAST(V.VEN_004 AS DATE) = CAST(EV.ENC_002 AS DATE)
WHERE EV.VEN_001 = 999
  AND EV.EMP_001 = 1                         
