IF EXISTS (SELECT * FROM SYSOBJECTS O, SYSUSERS U
           WHERE O.NAME = 'CONSULTA_FORMPGTO' AND
                 TYPE = 'FN' 		       AND
	              U.UID = O.UID            AND
	              U.NAME = USER)
DROP FUNCTION CONSULTA_FORMPGTO
GO


CREATE  FUNCTION CONSULTA_FORMPGTO
( @P_CDFILIAL     VARCHAR,
  @P_NRLANCTONF   VARCHAR,
  @P_IDORIGDESTNF VARCHAR) RETURNS INT
AS   
BEGIN
   DECLARE @V_FORMAPAGAMENTO  INT;

   IF @P_IDORIGDESTNF = 'F' BEGIN
      SELECT @V_FORMAPAGAMENTO = CASE FO.IDTPFORPGTFO WHEN NULL THEN 0 ELSE 1 END  
        FROM NOTAFISCAL NF, FORNECEDOR FO
       WHERE NF.CDFORNECED = FO.CDFORNECED
         AND NF.CDFILIAL = @P_CDFILIAL
         AND NF.NRLANCTONF = @P_NRLANCTONF;
   END
   ELSE BEGIN
      SELECT @V_FORMAPAGAMENTO = CASE CL.IDTPFORPGTCL WHEN NULL THEN 0 ELSE 1 END  
        FROM NOTAFISCAL NF, CLIENTE CL
       WHERE NF.CDCLIENTE = CL.CDCLIENTE
         AND NF.CDFILIAL = @P_CDFILIAL
         AND NF.NRLANCTONF = @P_NRLANCTONF;
   END;

   RETURN(@V_FORMAPAGAMENTO);
END
GO

-- VERSAO --
EXEC ATUVEROBJ 'CONSULTA_FORMPGTO','5.05.007';
GO

