create or replace PROCEDURE AJUSTE_CONTAPAGAR_NF
(
   P_CDFILIAL   IN NOTAFISCAL.CDFILIAL%TYPE,
   P_NRLANCTONF IN NOTAFISCAL.NRLANCTONF%TYPE,
   P_IDSTATUS   IN NOTAFISCAL.IDSTATUS%TYPE
)
IS
   V_VRATUAAF_VRDESC NUMBER;
   V_PREV_VRATUAPAG  NUMBER;
   V_VRATUAPAGAUX    NUMBER;
   V_VALORDEVOLVIDO  NUMBER;
   V_DIFERENCA       NUMBER;
   V_NRSEQVENPAGAUX  NUMBER;
   V_VRTOTPREV       NUMBER;
   V_VRMOVFLUXO      NUMBER;
   V_NRLANCTONFAUX   NUMBER;
   V_DESCFLUXO       NUMBER;
   V_NRSEQVENPAGFLUX NUMBER;
   V_NRAUTOFORNAUX   NUMBER;
   V_DTPROGENTAFAUX  DATE;

   V_IDSTATUSAUX     VARCHAR2(1);
   V_IDGERCONTPGT    VARCHAR2(1);
   V_IDCONSORCFIN    VARCHAR2(1);
   V_IDTPORCAFINA    VARCHAR2(1);
   V_CDFILIAL        VARCHAR2(4);
   V_CDFILLANCAUX    VARCHAR2(4);
   V_CDFILDESTAUX    VARCHAR2(4);
   V_COUNT           NUMBER;
   V_CDFORNECED      VARCHAR2(21);
   V_CDFORNECEDRELAC VARCHAR2(21);

BEGIN
   FOR C_NOTAREAL IN
      (SELECT IDENTRSAIDOP, CDFORNECED
         FROM NOTAFISCAL
        WHERE CDFILIAL   = P_CDFILIAL
          AND NRLANCTONF = P_NRLANCTONF)
   LOOP
      V_VRMOVFLUXO := 0;

      IF (C_NOTAREAL.IDENTRSAIDOP = 'E') AND (C_NOTAREAL.CDFORNECED IS NOT NULL) THEN /* SE A NOTA FOR DE ENTRADA DE FORNECEDOR */
         IF (P_IDSTATUS IN ('E', 'A')) THEN

			   V_CDFORNECED := C_NOTAREAL.CDFORNECED;

            /*SELECT CDFORNERELAC
              INTO V_CDFORNECEDRELAC
              FROM FORNECEDOR
             WHERE CDFORNECED = C_NOTAREAL.CDFORNECED;

            IF(V_CDFORNECEDRELAC IS NOT NULL) THEN
               V_CDFORNECED := V_CDFORNECEDRELAC;
            END IF;

            SELECT MAX(IDGERCONTPGT) INTO V_IDGERCONTPGT
              FROM FORNECEDOR
             WHERE CDFORNECED = V_CDFORNECED;

            IF V_IDGERCONTPGT = 'S' THEN  */
               FOR C_AUTOFORNE IN /* PEGA AS AF'S RELACIONADAS À NOTA */
                  (SELECT DISTINCT CDFILDEST, CDFILLANC, NRAUTOFORN, CDPRODUTO,DTPROGENTAF
                     FROM NFRELACAF
                    WHERE CDFILIAL   = P_CDFILIAL
                      AND NRLANCTONF = P_NRLANCTONF) LOOP

                  V_CDFILLANCAUX  := C_AUTOFORNE.CDFILLANC;
                  V_CDFILDESTAUX  := C_AUTOFORNE.CDFILDEST;
                  V_NRAUTOFORNAUX := C_AUTOFORNE.NRAUTOFORN;

                  FOR C_NOTAPREVISTA IN /* PEGA A NOTA DA CONTA A PAGAR PREVISTA */
                     (SELECT DISTINCT IT.NRLANCTONF, DTPROGENTAF
                        FROM ITEMAUTF IT, NOTAFISCAL NF
                       WHERE IT.CDFILLANC  = C_AUTOFORNE.CDFILLANC
                         AND IT.CDFILDEST  = C_AUTOFORNE.CDFILDEST
                         AND IT.NRAUTOFORN = C_AUTOFORNE.NRAUTOFORN
                         AND IT.CDPRODUTO  = C_AUTOFORNE.CDPRODUTO
								 AND IT.DTPROGENTAF = C_AUTOFORNE.DTPROGENTAF
                         AND IT.CDFILLANC  = NF.CDFILIAL
                         AND IT.NRLANCTONF = NF.NRLANCTONF
                         AND NF.IDSTATUS NOT IN ('C','R')) LOOP

                     V_NRLANCTONFAUX  := C_NOTAPREVISTA.NRLANCTONF;
                     V_DTPROGENTAFAUX := C_NOTAPREVISTA.DTPROGENTAF;

                     /* PEGA OS DADOS DA NOTA QUE FOI IMPORTADA PELA AF */
                    --SELECT SUM(VRATUAPAG), MAX(NRSEQVENPAG)
		              --	INTO V_VRATUAAF_VRDESC, V_NRSEQVENPAGAUX
                    --  FROM CONTAPAGAR
                    -- WHERE CDFILIAL   = P_CDFILIAL
                    --   AND NRLANCTONF = P_NRLANCTONF;


                          SELECT SUM(VRATUAPAG)
                           INTO V_VRTOTPREV
                           FROM CONTAPAGAR
                          WHERE CDFILIAL   = C_AUTOFORNE.CDFILLANC
                            AND NRLANCTONF = C_NOTAPREVISTA.NRLANCTONF;


                     FOR C_DESCNOTA IN /* PEGA OS DADOS DAS CONTAS A PAGAR PREVISTA  */
                        (SELECT VRORIGPAG, VRATUAPAG, NRSEQVENPAG
                           FROM CONTAPAGAR
                          WHERE CDFILIAL   = C_AUTOFORNE.CDFILLANC
                            AND NRLANCTONF = C_NOTAPREVISTA.NRLANCTONF
                          ORDER BY DTORIGVENPAG)
                     LOOP

                        SELECT SUM(VRTOTAITEM)
                          INTO V_VRATUAAF_VRDESC
                         FROM  NOTAFISCAL NF,ITEMPRODUTO IT
                        WHERE NF.CDFILIAL   = P_CDFILIAL
                          AND NF.NRLANCTONF = P_NRLANCTONF
                          AND IT.CDFILIAL = NF.CDFILIAL
                          AND IT.NRLANCTONF = NF.NRLANCTONF
                          AND IT.CDPRODUTO = C_AUTOFORNE.CDPRODUTO
								  AND IT.DTPROGENTAF = C_NOTAPREVISTA.DTPROGENTAF;

                        V_PREV_VRATUAPAG := C_DESCNOTA.VRATUAPAG;

                        V_VRATUAAF_VRDESC := ROUND((C_DESCNOTA.VRATUAPAG * V_VRATUAAF_VRDESC) / V_VRTOTPREV,2);

                        IF (V_VRATUAAF_VRDESC > 0) OR (V_PREV_VRATUAPAG <= 0) THEN
                           IF V_PREV_VRATUAPAG > V_VRATUAAF_VRDESC THEN
                              V_PREV_VRATUAPAG  := V_PREV_VRATUAPAG - V_VRATUAAF_VRDESC;
                              V_VRATUAAF_VRDESC := 0;
                           ELSE
                              V_VRATUAAF_VRDESC := V_VRATUAAF_VRDESC - V_PREV_VRATUAPAG;
                              V_PREV_VRATUAPAG  := 0;
                           END IF;
                        END IF;

                        /*############################ AJUSTE NA CONTA A PAGAR ###################################*/
                        UPDATE CONTAPAGAR
                           SET VRATUAPAG   = V_PREV_VRATUAPAG
                         WHERE CDFILIAL    = C_AUTOFORNE.CDFILLANC
                           AND NRLANCTONF  = C_NOTAPREVISTA.NRLANCTONF
                           AND NRSEQVENPAG = C_DESCNOTA.NRSEQVENPAG;

                        /*############################ CANCELA A NOTA FISCAL (SE NECESSARIO) #####################*/
                        /* SE CONTAPAGAR.VRATUAPAG <= 0 ENTAO CANCELA A NF*/
                        SELECT SUM(CONTAPAGAR.VRATUAPAG)
                          INTO V_VRATUAPAGAUX
                          FROM CONTAPAGAR
                         WHERE CONTAPAGAR.CDFILIAL   = C_AUTOFORNE.CDFILLANC
                           AND CONTAPAGAR.NRLANCTONF = C_NOTAPREVISTA.NRLANCTONF;

                        V_COUNT := 0;
                          BEGIN
                            SELECT COUNT(1)
                              INTO V_COUNT
                              FROM AUTOFORNE AF, ITEMAUTF IT
                             WHERE AF.CDFILDEST = IT.CDFILDEST
                               AND AF.CDFILLANC = IT.CDFILLANC
                               AND AF.NRAUTOFORN = IT.NRAUTOFORN
                               AND IT.IDBAIXITMAF <> '2'
                               AND IT.CDFILLANC = C_AUTOFORNE.CDFILLANC
                               AND IT.NRLANCTONF = C_NOTAPREVISTA.NRLANCTONF;
                         EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                               V_COUNT := 0;
                         END;

                        V_IDSTATUSAUX := 'V';
								IF ((V_VRATUAPAGAUX <= 0.05) OR (V_COUNT = 0)) THEN
						         V_IDSTATUSAUX := 'C';
                        END IF;

                        UPDATE NOTAFISCAL
                           SET IDSTATUS   = V_IDSTATUSAUX
                         WHERE CDFILIAL   = C_AUTOFORNE.CDFILLANC
                           AND NRLANCTONF = C_NOTAPREVISTA.NRLANCTONF;

                     END LOOP;
                  END LOOP;
               END LOOP;

               /*########################### MOVIMENTO DE FLUXO #########################################*/

               FOR C_CONTAPAGAR IN
                  (SELECT CDEMPRESA,
                          DTMOVFLUXO,
                          IDMOVFLUXO,
                          CDCLASSFINA,
                          VRORIGPAG,
                          VRATUAPAG
                     FROM CONTAPAGAR
                    WHERE CDFILIAL   = V_CDFILLANCAUX
                      AND NRLANCTONF = V_NRLANCTONFAUX)
               LOOP

                  ATUALIZA_FLUXO_DE_CAIXA(C_CONTAPAGAR.CDEMPRESA,
                                          V_CDFILDESTAUX,
                                          C_CONTAPAGAR.DTMOVFLUXO,
                                          C_CONTAPAGAR.IDMOVFLUXO,
                                          C_CONTAPAGAR.CDCLASSFINA,
                                          0,
                                          - C_CONTAPAGAR.VRORIGPAG);

                  UPDATE MOVFLUXAF
                     SET VRPREVAF = VRPREVAF - C_CONTAPAGAR.VRORIGPAG
                   WHERE CDFILDEST    = V_CDFILDESTAUX
                     AND CDFILLANC    = V_CDFILLANCAUX
                     AND NRAUTOFORN   = V_NRAUTOFORNAUX
                     AND CDCLASSFINAF = C_CONTAPAGAR.CDCLASSFINA
                     AND DTENTMOVAF   = V_DTPROGENTAFAUX
                     AND DTPREVVENC   = C_CONTAPAGAR.DTMOVFLUXO;

                  /*######################## ORCAMENTO FINANCEIRO ##########################################*/

                  SELECT IDCONSORCFIN, IDTPORCAFINA
                    INTO V_IDCONSORCFIN, V_IDTPORCAFINA
                    FROM PARAMGERAL;

                  IF V_IDCONSORCFIN = 'S' THEN
                     V_CDFILIAL := NULL;
                     IF V_IDTPORCAFINA = 'F' THEN
                        V_CDFILIAL := V_CDFILDESTAUX;
                     END IF;
                     FOR C_ORCAFINA IN
                        (SELECT CDEMPRESA,
                                CDFILIAL,
                                CDCLASSFINA,
                                DTORCAFINA
                           FROM ORCAFINA
                          WHERE (CDEMPRESA   = C_CONTAPAGAR.CDEMPRESA)
                            AND (CDFILIAL    = NVL(V_CDFILIAL, CDFILIAL))
                            AND (CDCLASSFINA = C_CONTAPAGAR.CDCLASSFINA)
                            AND (DTORCAFINA BETWEEN TO_DATE(C_CONTAPAGAR.DTMOVFLUXO) - TO_CHAR(TO_DATE(C_CONTAPAGAR.DTMOVFLUXO), 'DD') + 1
                                                AND LAST_DAY(C_CONTAPAGAR.DTMOVFLUXO)))
                     LOOP

                        UPDATE ORCAFINA
                           SET VRREALORCA  = VRREALORCA - C_CONTAPAGAR.VRORIGPAG
                         WHERE CDCLASSFINA = C_ORCAFINA.CDCLASSFINA AND
                               CDEMPRESA   = C_ORCAFINA.CDEMPRESA AND
                               DTORCAFINA  = C_ORCAFINA.DTORCAFINA AND
                               CDFILIAL    = C_ORCAFINA.CDFILIAL;
                     END LOOP;
                  END IF;
               END LOOP;
            --END IF;
         ELSE
			   V_CDFORNECED := C_NOTAREAL.CDFORNECED;

            /*SELECT CDFORNERELAC
              INTO V_CDFORNECEDRELAC
              FROM FORNECEDOR
             WHERE CDFORNECED = C_NOTAREAL.CDFORNECED;

            IF(V_CDFORNECEDRELAC IS NOT NULL) THEN
               V_CDFORNECED := V_CDFORNECEDRELAC;
            END IF;

            SELECT MAX(IDGERCONTPGT) INTO V_IDGERCONTPGT
              FROM FORNECEDOR
             WHERE CDFORNECED = V_CDFORNECED;

            IF V_IDGERCONTPGT = 'S' THEN  */
               FOR C_AUTOFORNE IN /* PEGA AS AF'S RELACIONADAS À NOTA */
                  (SELECT DISTINCT CDFILDEST, CDFILLANC, NRAUTOFORN, CDPRODUTO
                     FROM NFRELACAF
                    WHERE CDFILIAL   = P_CDFILIAL
                      AND NRLANCTONF = P_NRLANCTONF)
               LOOP

                  V_CDFILLANCAUX  := C_AUTOFORNE.CDFILLANC;
                  V_CDFILDESTAUX  := C_AUTOFORNE.CDFILDEST;
                  V_NRAUTOFORNAUX := C_AUTOFORNE.NRAUTOFORN;

                  FOR C_NOTAPREVISTA IN /* PEGA O NUMERO DE LANÇAMENTO DA CONTA PREVISTA */
                     (SELECT DISTINCT NRLANCTONF, DTPROGENTAF
                        FROM ITEMAUTF
                       WHERE CDFILLANC  = C_AUTOFORNE.CDFILLANC
                         AND CDFILDEST  = C_AUTOFORNE.CDFILDEST
                         AND NRAUTOFORN = C_AUTOFORNE.NRAUTOFORN
								 AND CDPRODUTO =  C_AUTOFORNE.CDPRODUTO) LOOP

                     V_NRLANCTONFAUX  := C_NOTAPREVISTA.NRLANCTONF;
                     V_DTPROGENTAFAUX := C_NOTAPREVISTA.DTPROGENTAF;

                     /* PEGA OS DADOS DA NOTA QUE FOI IMPORTADA PELA AF */
                     SELECT SUM(VRATUAPAG)
                       INTO V_VRATUAAF_VRDESC
                       FROM CONTAPAGAR
                      WHERE CDFILIAL   = P_CDFILIAL
                        AND NRLANCTONF = P_NRLANCTONF;

                     V_VALORDEVOLVIDO := V_VRATUAAF_VRDESC;
                     V_DIFERENCA := 0;

                     FOR C_DESCPREVISTA IN
                        (SELECT VRORIGPAG, VRATUAPAG, NRSEQVENPAG
                           FROM CONTAPAGAR
                          WHERE CDFILIAL   = C_AUTOFORNE.CDFILLANC
                            AND NRLANCTONF = C_NOTAPREVISTA.NRLANCTONF
                          ORDER BY NRSEQVENPAG DESC)
                     LOOP

                        IF V_VALORDEVOLVIDO >= 0 THEN
                           IF C_DESCPREVISTA.VRATUAPAG <> C_DESCPREVISTA.VRORIGPAG THEN
                              V_DIFERENCA := C_DESCPREVISTA.VRORIGPAG - C_DESCPREVISTA.VRATUAPAG;

                              IF V_DIFERENCA > V_VALORDEVOLVIDO THEN
                                 UPDATE CONTAPAGAR
                                    SET VRATUAPAG   = C_DESCPREVISTA.VRATUAPAG + V_VALORDEVOLVIDO
                                  WHERE CDFILIAL    = C_AUTOFORNE.CDFILLANC
                                    AND NRLANCTONF  = C_NOTAPREVISTA.NRLANCTONF
                                    AND NRSEQVENPAG = C_DESCPREVISTA.NRSEQVENPAG;
                              ELSE
                                 UPDATE CONTAPAGAR
                                    SET VRATUAPAG   = C_DESCPREVISTA.VRATUAPAG + V_DIFERENCA
                                  WHERE CDFILIAL    = C_AUTOFORNE.CDFILLANC
                                    AND NRLANCTONF  = C_NOTAPREVISTA.NRLANCTONF
                                    AND NRSEQVENPAG = C_DESCPREVISTA.NRSEQVENPAG;
                              END IF;

                              UPDATE NOTAFISCAL
                                 SET IDSTATUS   = 'V'
                               WHERE CDFILIAL   = C_AUTOFORNE.CDFILLANC
                                 AND NRLANCTONF = C_NOTAPREVISTA.NRLANCTONF;

                              V_VALORDEVOLVIDO := V_VALORDEVOLVIDO - V_DIFERENCA;
                           END IF;
                        END IF;
                     END LOOP;
                  END LOOP;
               END LOOP;

               /*########################### MOVIMENTO DE FLUXO #########################################*/

               FOR C_CONTAPAGAR IN
                  (SELECT CDEMPRESA,
                          DTMOVFLUXO,
                          IDMOVFLUXO,
                          CDCLASSFINA,
                          VRORIGPAG,
                          VRATUAPAG
                     FROM CONTAPAGAR
                    WHERE CDFILIAL   = V_CDFILLANCAUX
                      AND NRLANCTONF = V_NRLANCTONFAUX)
               LOOP

                  ATUALIZA_FLUXO_DE_CAIXA(C_CONTAPAGAR.CDEMPRESA,
                                          V_CDFILDESTAUX,
                                          C_CONTAPAGAR.DTMOVFLUXO,
                                          C_CONTAPAGAR.IDMOVFLUXO,
                                          C_CONTAPAGAR.CDCLASSFINA,
                                          0,
                                          C_CONTAPAGAR.VRORIGPAG);

                  UPDATE MOVFLUXAF
                     SET VRPREVAF = VRPREVAF + C_CONTAPAGAR.VRORIGPAG
                   WHERE CDFILDEST    = V_CDFILDESTAUX
                     AND CDFILLANC    = V_CDFILLANCAUX
                     AND NRAUTOFORN   = V_NRAUTOFORNAUX
                     AND CDCLASSFINAF = C_CONTAPAGAR.CDCLASSFINA
                     AND DTENTMOVAF   = V_DTPROGENTAFAUX
                     AND DTPREVVENC   = C_CONTAPAGAR.DTMOVFLUXO;

                  /*######################## ORCAMENTO FINANCEIRO ##########################################*/

                  SELECT IDCONSORCFIN, IDTPORCAFINA
                    INTO V_IDCONSORCFIN, V_IDTPORCAFINA
                    FROM PARAMGERAL;

                  IF V_IDCONSORCFIN = 'S' THEN
                     V_CDFILIAL := NULL;
                     IF V_IDTPORCAFINA = 'F' THEN
                        V_CDFILIAL := V_CDFILDESTAUX;
                     END IF;
                     FOR C_ORCAFINA IN
                        (SELECT CDEMPRESA,
                                CDFILIAL,
                                CDCLASSFINA,
                                DTORCAFINA
                           FROM ORCAFINA
                          WHERE (CDEMPRESA   = C_CONTAPAGAR.CDEMPRESA)
                            AND (CDFILIAL    = NVL(V_CDFILIAL, CDFILIAL))
                            AND (CDCLASSFINA = C_CONTAPAGAR.CDCLASSFINA)
                            AND (DTORCAFINA BETWEEN TO_DATE(C_CONTAPAGAR.DTMOVFLUXO) - TO_CHAR(TO_DATE(C_CONTAPAGAR.DTMOVFLUXO), 'DD') + 1
                                                AND LAST_DAY(C_CONTAPAGAR.DTMOVFLUXO)))
                     LOOP

                        UPDATE ORCAFINA
                           SET VRREALORCA  = VRREALORCA + C_CONTAPAGAR.VRORIGPAG
                         WHERE CDCLASSFINA = C_ORCAFINA.CDCLASSFINA AND
                               CDEMPRESA   = C_ORCAFINA.CDEMPRESA AND
                               DTORCAFINA  = C_ORCAFINA.DTORCAFINA AND
                               CDFILIAL    = C_ORCAFINA.CDFILIAL;
                     END LOOP;
                  END IF;
               END LOOP;
            END IF;
        -- END IF;
      END IF;
   END LOOP;
END;
/

CREATE OR REPLACE PROCEDURE ESTORNA_LANC_CONTABIL(
   P_CDEMPRESA  IN EMPRESA.CDEMPRESA%TYPE,
   P_NRLANCCONT IN LANCCONT.NRLANCCONT%TYPE)

AS
   V_DSHISTLANC       LANCCONT.DSHISTLANC%TYPE;
   V_DSHISTLANCEST    LANCCONT.DSHISTLANC%TYPE;
   V_FATOR            NUMBER;
   V_NRLANCCONT       LANCCONT.NRLANCCONT%TYPE;
   V_NRLANCCONTEST    LANCCONT.NRLANCCONT%TYPE;
   V_NRSEQLANCCONT    VARCHAR2(20);
   V_NRSEQLANCCONTEST VARCHAR2(20);
   V_IDGERESTLANCONT  VARCHAR2(01);
BEGIN
   BEGIN
      SELECT NVL(IDGERESTLANCONT,'N')
        INTO V_IDGERESTLANCONT
        FROM CONTOPECTBL
       WHERE CDEMPRESA  = P_CDEMPRESA;
   EXCEPTION
        WHEN NO_DATA_FOUND THEN
        V_IDGERESTLANCONT := NULL;
   END;
   V_FATOR := -1;
   IF NVL(V_IDGERESTLANCONT,'N') = 'S' THEN
      FOR C_LANC IN (SELECT CDEMPRESA, NRLANCCONT, NRSEQLANC, CDCONTCTBL, CDHISTPADR,
                            DTLANCCONT, CDDOCUCONT, IDTIPOLANC, DSHISTLANC, VRLANCAMENT,
                            IDLANCCOME, CDCONTCOME, IDLANCSALDO, IDLANCINTE,
                            IDLANCMANI, CDOPERINS, IDSISTEMA
                      FROM LANCCONT
                     WHERE CDEMPRESA  = P_CDEMPRESA
                       AND NRLANCCONT = P_NRLANCCONT
                       AND IDLANCINTE = 'I')LOOP
          IF V_NRLANCCONT IS NULL THEN
             NOVO_CODIGO_NT('LANCCONTABIL' || C_LANC.CDEMPRESA || TO_CHAR(C_LANC.DTLANCCONT,'YYYYMM'),V_NRSEQLANCCONT,1);
             V_NRLANCCONT := TO_CHAR(C_LANC.DTLANCCONT,'YYYYMM')||SUBSTR(V_NRSEQLANCCONT,15,6);
          END IF;

          IF V_NRLANCCONTEST IS NULL THEN
             NOVO_CODIGO_NT('LANCCONTABIL' || C_LANC.CDEMPRESA || TO_CHAR(C_LANC.DTLANCCONT,'YYYYMM'),V_NRSEQLANCCONTEST,1);
             V_NRLANCCONTEST := TO_CHAR(C_LANC.DTLANCCONT,'YYYYMM')||SUBSTR(V_NRSEQLANCCONTEST,15,6);
          END IF;
          /*ESTORNA LANÇAMENTO QUE SERÁ EXCLUÍDO*/
          V_DSHISTLANC    := SUBSTR('ESTORNO REF. LANC:'||' - '||V_NRLANCCONTEST||' - '||C_LANC.DSHISTLANC,1,200);

          V_DSHISTLANCEST := SUBSTR('LANÇAMENTO ESTORNADO PELO LANÇAMENTO - '||V_NRLANCCONT||' - '||C_LANC.DSHISTLANC,1,200);

         INSERT INTO LANCCONT
               (CDEMPRESA, NRLANCCONT, NRSEQLANC, CDCONTCTBL, CDHISTPADR,
                DTLANCCONT, CDDOCUCONT, IDTIPOLANC, DSHISTLANC, VRLANCAMENT,
                IDLANCCOME, CDCONTCOME, IDLANCSALDO, IDLANCINTE, IDLANCMANI,
                CDOPERINS, DTULTATULAN, IDSALDLANCO, IDSALDLANGE, IDSISTEMA)
         VALUES
               (C_LANC.CDEMPRESA, V_NRLANCCONT, C_LANC.NRSEQLANC, C_LANC.CDCONTCTBL, C_LANC.CDHISTPADR,
                C_LANC.DTLANCCONT, C_LANC.CDDOCUCONT, 'C', V_DSHISTLANC, C_LANC.VRLANCAMENT * V_FATOR,
                C_LANC.IDLANCCOME, C_LANC.CDCONTCOME, 'N', C_LANC.IDLANCINTE,'N',
                C_LANC.CDOPERINS, SYSDATE, 'N', 'N', C_LANC.IDSISTEMA);

        INSERT INTO LANCCONT
               (CDEMPRESA, NRLANCCONT, NRSEQLANC, CDCONTCTBL, CDHISTPADR,
                DTLANCCONT, CDDOCUCONT, IDTIPOLANC, DSHISTLANC, VRLANCAMENT,
                IDLANCCOME, CDCONTCOME, IDLANCSALDO, IDLANCINTE, IDLANCMANI,
                CDOPERINS, DTULTATULAN, IDSALDLANCO, IDSALDLANGE, IDSISTEMA)
         VALUES
               (C_LANC.CDEMPRESA, V_NRLANCCONTEST, C_LANC.NRSEQLANC, C_LANC.CDCONTCTBL, C_LANC.CDHISTPADR,
                C_LANC.DTLANCCONT, C_LANC.CDDOCUCONT, 'C', V_DSHISTLANCEST, C_LANC.VRLANCAMENT,
                C_LANC.IDLANCCOME, C_LANC.CDCONTCOME, 'N', C_LANC.IDLANCINTE,'N',
                C_LANC.CDOPERINS, SYSDATE, 'N', 'N', C_LANC.IDSISTEMA);

         FOR C_LANCCUST IN (SELECT CDEMPRESA, NRLANCCONT, NRSEQLANC, CDFILIAL,
                                   CDCENTCUST, VRLANCCUST, DTCOMPCUSTO, CDCONTCTBL
                              FROM LANCCCUSTO
                             WHERE CDEMPRESA  = C_LANC.CDEMPRESA
                               AND NRLANCCONT = C_LANC.NRLANCCONT
                               AND NRSEQLANC  = C_LANC.NRSEQLANC
                               AND CDCONTCTBL = C_LANC.CDCONTCTBL)LOOP
           INSERT INTO LANCCCUSTO
               (CDEMPRESA, NRLANCCONT, NRSEQLANC, CDFILIAL,
                CDCENTCUST, VRLANCCUST, DTCOMPCUSTO)
           VALUES
               (C_LANCCUST.CDEMPRESA, V_NRLANCCONT, C_LANCCUST.NRSEQLANC, C_LANCCUST.CDFILIAL,
                C_LANCCUST.CDCENTCUST, C_LANCCUST.VRLANCCUST * V_FATOR, C_LANCCUST.DTCOMPCUSTO);
           INSERT INTO LANCCCUSTO
               (CDEMPRESA, NRLANCCONT, NRSEQLANC, CDFILIAL,
                CDCENTCUST, VRLANCCUST, DTCOMPCUSTO)
           VALUES
               (C_LANCCUST.CDEMPRESA, V_NRLANCCONTEST, C_LANCCUST.NRSEQLANC, C_LANCCUST.CDFILIAL,
                C_LANCCUST.CDCENTCUST, C_LANCCUST.VRLANCCUST, C_LANCCUST.DTCOMPCUSTO);
         END LOOP;
      END LOOP;
   END IF;
END;