create or replace FUNCTION FN_CHECK_PENDENCIA (
    P_CDCLIENTE   IN   VARCHAR2,
    P_CDFILIAL    IN   VARCHAR2
) RETURN NUMBER IS
    QUANT NUMBER;
BEGIN
    SELECT
        COUNT(CO.NRLANCTONF)
    INTO QUANT
    FROM
        CONTARECEBER   CO,
        NOTAFISCAL     NF,
        BOLETRECEBER   BO,
        CLIENTE        C,
        PARAMPEDIDO    P
    WHERE
        ( NF.CDCLIENTE = P_CDCLIENTE )
        AND ( CO.CDFILIAL = NF.CDFILIAL )
        AND ( CO.NRLANCTONF = NF.NRLANCTONF )
        AND ( CO.NRLANBOLREC = BO.NRLANBOLREC )
        AND ( CO.CDFILIAL = BO.CDFILIAL )
        AND ( P.CDFILIAL = P_CDFILIAL )
        AND ( C.CDCLIENTE = P_CDCLIENTE )
        AND ( BO.DTVENBOLREC < ( SYSDATE - NVL(C.NRDIAPFINAPR, P.NRDIAPENDFIN) ) )
        AND ( CO.IDSTATREC IN (
            'A',
            'D'
        ) )
        AND ( NF.IDSTATUS IN (
            'A',
            'E'
        ) );

    IF ( QUANT > 0 ) THEN
        RETURN(1);
    ELSE
        RETURN(0);
    END IF;

END FN_CHECK_PENDENCIA;
/