CREATE OR REPLACE
FUNCTION LANCCTB_VALIDO(
   P_CDEMPRESA VARCHAR2,
   P_NRLANCCONT VARCHAR2) RETURN VARCHAR2
AS
   V_COUNT NUMBER;
BEGIN
   V_COUNT := 0;

   BEGIN
      SELECT COUNT(*)
        INTO V_COUNT
        FROM (SELECT CDEMPRESA, NRLANCCONT
                FROM (SELECT CDEMPRESA, NRLANCCONT
                        FROM (SELECT CDEMPRESA, NRLANCCONT
                                FROM (SELECT CDEMPRESA, NRLANCCONT
                                        FROM (SELECT CDEMPRESA, NRLANCCONT
                                                FROM (SELECT DISTINCT CDEMPRESA, NRLANCCONT
                                                        FROM LANCCONT L
                                                       WHERE L.IDLANCINTE = 'I'
                                                         AND L.NRLANCCONT = P_NRLANCCONT
                                                         AND L.CDEMPRESA = P_CDEMPRESA
                                                         AND NOT EXISTS (SELECT 1 FROM BAIXAPAGA B WHERE CDEMPRESA = L.CDEMPRESA AND B.NRLCTBBPAG = L.NRLANCCONT)) L
                                               WHERE NOT EXISTS (SELECT 1 FROM BAIXARECEBIDA B WHERE B.CDEMPRESA = L.CDEMPRESA AND B.NRLCTBBREC = L.NRLANCCONT)) L
                                       WHERE NOT EXISTS (SELECT 1 FROM NOTAFISCAL NF WHERE NF.CDEMPRESA = L.CDEMPRESA AND NF.NRLANCTBDOC = L.NRLANCCONT)) L
                                WHERE  NOT EXISTS (SELECT 1 FROM AJCTPAGAR AJ WHERE NVL(CDEMPRESA, NVL(CDECTBAJPAG, AJ.CDEMMFAJCNTP)) = L.CDEMPRESA AND AJ.NRLCTBAJPAG = L.NRLANCCONT)) L
                       WHERE NOT EXISTS (SELECT 1 FROM AJCTRECEBER AJ WHERE NVL(CDEMPRESA, NVL(CDECTBAJREC, AJ.CDEMMFAJCNTR)) = L.CDEMPRESA AND AJ.NRLCTBAJREC = L.NRLANCCONT)) L
               WHERE NOT EXISTS (SELECT 1 FROM MOVCREDBANC M WHERE NVL(M.CDEMPRESA, CDECTBMCRED) = L.CDEMPRESA AND M.NRLCTBMCRED = L.NRLANCCONT)) L
       WHERE NOT EXISTS (SELECT 1 FROM MOVDEBBANC M WHERE NVL(M.CDEMPRESA, CDECTBMDEB) = L.CDEMPRESA AND M.NRLCTBMDEB = L.NRLANCCONT)
         AND NOT EXISTS (SELECT 1 FROM TRANSFBANC T WHERE T.CDEMPRESA = L.CDEMPRESA AND T.NRLCTBTRAN = L.NRLANCCONT)
         AND NOT EXISTS (SELECT 1 FROM LANCLOTE LC WHERE LC.CDEMPRESA = L.CDEMPRESA AND LC.NRLANCDEFI = L.NRLANCCONT);
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_COUNT := 0;
   END;
   IF V_COUNT <> 0 THEN
      RETURN('N');
   ELSE
      RETURN('S');
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LANCCTB_VALIDO','5.85.xxx'); END;
/
