CREATE OR REPLACE PROCEDURE GRAVA_LANC_CTB(
   P_NRLANCCONT  IN LANCCONT.NRLANCCONT%TYPE,
   P_CDEMPRESA   IN CONTCORR.CDREDUCONT%TYPE)
AS
   V_CONTCOME   VARCHAR2(01);
   V_CREDDEB    VARCHAR2(01);
   V_CONTRAT    VARCHAR2(01);
   V_COUNT      INTEGER;   
BEGIN


   BEGIN
      SELECT IDCONTGERE
         INTO V_CONTRAT
         FROM CONTOPECTBL
        WHERE CDEMPRESA = P_CDEMPRESA;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CONTRAT := NULL;
   END;

    SELECT COUNT(*)
     INTO V_COUNT
     FROM ERROCTBTEMP
    WHERE CDEMPRESA = P_CDEMPRESA
      AND NRLANCCTBTEMP = P_NRLANCCONT;
   IF V_COUNT = 0 THEN
      FOR LANCCONT IN
         (SELECT L.CDEMPRESA,  L.NRLANCCTBTEMP, L.DTLANCCTBTEMP, L.NRSEQCTBTEMP, L.CDCONTCTBL, C.NMCONTCTBL,
                L.CDHISTPADR, L.DSHISTCTB, L.VRLANCCTBTEMP, DECODE(L.IDLANCCTBCOME, 'N', NULL, IDLANCCTBCOME) IDLANCCTBCOME,
                L.CDLANCCTBCOME, L.CDDOCUTEMP CDDOCUCONT, IDSISTEMA
           FROM LANCCTBTEMP L, CONTCTBL C, EMPRESA E
          WHERE L.CDCONTCTBL  = C.CDCONTCTBL
            AND L.CDEMPRESA   = E.CDEMPRESA
            AND L.CDEMPRESA   = P_CDEMPRESA
            AND NRLANCCTBTEMP = P_NRLANCCONT
          ORDER BY NRSEQCTBTEMP) LOOP

         IF LANCCONT.VRLANCCTBTEMP > 0 THEN
            V_CREDDEB := 'C';
         ELSE
            V_CREDDEB := 'D';
         END IF;

         INSERT INTO LANCCONT
            (CDEMPRESA, NRLANCCONT, NRSEQLANC, DTLANCCONT, CDCONTCTBL,
             CDHISTPADR, DSHISTLANC, IDTIPOLANC, VRLANCAMENT,
             IDLANCSALDO, IDLANCINTE, DTULTATULAN, IDLANCCOME, CDCONTCOME, CDDOCUCONT, IDSISTEMA)
         VALUES
            (LANCCONT.CDEMPRESA, LANCCONT.NRLANCCTBTEMP, LANCCONT.NRSEQCTBTEMP, LANCCONT.DTLANCCTBTEMP, LANCCONT.CDCONTCTBL,
             LANCCONT.CDHISTPADR, LANCCONT.DSHISTCTB, V_CREDDEB, LANCCONT.VRLANCCTBTEMP,
             'N', 'I', SYSDATE, LANCCONT.IDLANCCTBCOME, LANCCONT.CDLANCCTBCOME, LANCCONT.CDDOCUCONT, LANCCONT.IDSISTEMA);

         IF V_CONTRAT = 'S' THEN
            FOR RAT IN
               (SELECT R.CDFILIAL, F.NMFILIAL, R.CDCENTCUST, C.NMCENTCUST, TO_DATE(R.DTCOMPCTBTEMP) DTCOMPCTBTEMP,
                      R.VRRATECTBTEMP VRLANCCUST, R.NRSEQCTBTEMP, R.NRLANCCTBTEMP, R.CDEMPRESA
                 FROM RATECTBTEMP R, FILIAL F, CENTCUST C
                WHERE R.NRLANCCTBTEMP = LANCCONT.NRLANCCTBTEMP
                  AND R.CDEMPRESA     = LANCCONT.CDEMPRESA
                  AND R.CDFILIAL      = F.CDFILIAL
                  AND R.CDCENTCUST    = C.CDCENTCUST
                  AND R.NRSEQCTBTEMP = LANCCONT.NRSEQCTBTEMP
                ORDER BY R.CDFILIAL, R.CDCENTCUST)LOOP

                INSERT INTO LANCCCUSTO
                   (CDEMPRESA, NRLANCCONT, NRSEQLANC, CDFILIAL, CDCENTCUST, VRLANCCUST, DTCOMPCUSTO, CDCONTCTBL)
                VALUES
                   (RAT.CDEMPRESA, RAT.NRLANCCTBTEMP, RAT.NRSEQCTBTEMP, RAT.CDFILIAL, RAT.CDCENTCUST, RAT.VRLANCCUST, RAT.DTCOMPCTBTEMP, LANCCONT.CDCONTCTBL);
            END LOOP;
         END IF;
      END LOOP;
      DELETE FROM LANCCTBTEMP
       WHERE NRLANCCTBTEMP = P_NRLANCCONT
         AND CDEMPRESA     = P_CDEMPRESA;

      DELETE FROM RATECTBTEMP
       WHERE NRLANCCTBTEMP = P_NRLANCCONT
         AND CDEMPRESA     = P_CDEMPRESA;
   END IF;
END;
/