-- ============================================================
--   TABLE: LANCSALDCAIXA
-- ============================================================
CREATE TABLE LANCSALDCAIXA
( 
  NRSEQLANCSALD     NUMBER,
  CDEMPRESA         VARCHAR2(02) 					NOT NULL,
  DTLANCSALD        DATE 							NOT NULL,
  CDFILIAL          VARCHAR2(04) 					NOT NULL,
  CDCENTCUST        VARCHAR2(04) 					NOT NULL,
  VRLANCSALD        FLOAT,
  NRORG             NUMBER 			DEFAULT 	 	SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
  IDATIVO           VARCHAR2(1) 	DEFAULT 'S' 	NOT NULL ,
  DTINCLUSAO 		DATE,
  DTULTATU 			DATE,
  NRORGINCLUSAO  	NUMBER 			DEFAULT 1,
  CDOPERINCLUSAO 	VARCHAR2(12) 	DEFAULT '1',
  NRORGULTATU    	NUMBER 			DEFAULT 1,
  CDOPERULTATU   	VARCHAR2(12) 	DEFAULT '1',
    CONSTRAINT CKC_IDATIVO_LANCSALDCAIXA CHECK (IDATIVO IN ('S','N')) ,
    CONSTRAINT PK_LANCSALDCAIXA PRIMARY KEY (NRSEQLANCSALD) 
)
/

-- ============================================================
--   TABLE: TIPODESPESAS
-- ============================================================
CREATE TABLE TIPODESPESAS
( 
  CDTIPODESP        NUMBER 							NOT NULL,
  NMDESPESA         VARCHAR2(4000) 					NOT NULL,
  CDHISTPADR        VARCHAR2(04) 					NULL, 
  CDCONTCTBL        VARCHAR2(20) 					NULL,
  IDCTBLDESP        VARCHAR2(1) 	DEFAULT 'N' 	NOT NULL ,
  NRORG             NUMBER 			DEFAULT 	 	SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
  IDATIVO           VARCHAR2(1) 	DEFAULT 'S' 	NOT NULL ,
  DTINCLUSAO 		DATE,
  DTULTATU 			DATE,
  NRORGINCLUSAO  	NUMBER 			DEFAULT 1,
  CDOPERINCLUSAO 	VARCHAR2(12) 	DEFAULT '1',
  NRORGULTATU    	NUMBER 			DEFAULT 1,
  CDOPERULTATU   	VARCHAR2(12) 	DEFAULT '1',
    CONSTRAINT CKC_IDATIVO_TIPODESPESAS CHECK (IDATIVO IN ('S','N')) ,
    CONSTRAINT CKC_IDCTBLDESP_TIPODESPESAS CHECK (IDATIVO IN ('S','N')),
    CONSTRAINT PK_TIPODESPESAS PRIMARY KEY (CDTIPODESP) 
)
/

-- ============================================================
--   TABLE: LANCDESPCAIXA
-- ============================================================
CREATE TABLE LANCDESPCAIXA
( 
  NRSEQLANCDESP     NUMBER 							NOT NULL,
  CDTIPODESP        NUMBER 							NOT NULL,
  DSLANCDESP        VARCHAR2(4000),
  CDEMPRESA         VARCHAR2(20) 					NOT NULL,
  DTLANCDESP        DATE 							NOT NULL,
  CDFILIAL          VARCHAR2(04) 					NOT NULL,
  CDCENTCUST        VARCHAR2(04) 					NOT NULL,
  VRLANCDESP        FLOAT,
  NRORG             NUMBER 			DEFAULT 	 	SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
  IDATIVO           VARCHAR2(1) 	DEFAULT 'S' 	NOT NULL ,
  DTINCLUSAO        DATE,
  DTULTATU          DATE,
  NRORGINCLUSAO     NUMBER 			DEFAULT 1,
  CDOPERINCLUSAO    VARCHAR2(12) 	DEFAULT '1',
  NRORGULTATU       NUMBER 			DEFAULT 1,
  CDOPERULTATU      VARCHAR2(12) 	DEFAULT '1',
    CONSTRAINT CKC_IDATIVO_LANCDESPCAIXA CHECK (IDATIVO IN ('S','N')) ,
    CONSTRAINT PK_LANCDESPCAIXA PRIMARY KEY (NRSEQLANCDESP) 
)
/

-- ============================================================
--   TABLE: RELACSALDCAIXADESP
-- ============================================================
CREATE TABLE RELACSALDCAIXADESP
(
  NRSEQRELACLANCDESP NUMBER,
  NRSEQLANCDESP      NUMBER,
  NRSEQLANCSALD      NUMBER,
  CDEMPRESA          VARCHAR2(20) 					NOT NULL,
  DTRELACSALDDESP    DATE 							NOT NULL,
  CDFILIAL           VARCHAR2(04) 					NOT NULL,
  CDCENTCUST         VARCHAR2(04) 					NOT NULL,
  VRRELACSALDCAIX    FLOAT,
  NRORG              NUMBER 		DEFAULT 	 	SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
  IDATIVO            VARCHAR2(1) 	DEFAULT 'S' 	NOT NULL ,
  DTINCLUSAO         DATE,
  DTULTATU           DATE,
  NRORGINCLUSAO      NUMBER 		DEFAULT 1,
  CDOPERINCLUSAO     VARCHAR2(12) 	DEFAULT '1',
  NRORGULTATU        NUMBER 		DEFAULT 1,
  CDOPERULTATU       VARCHAR2(12) 	DEFAULT '1',
    CONSTRAINT CKC_IDATIVO_RELACSALDCAIXA CHECK (IDATIVO IN ('S','N')) ,
    CONSTRAINT PK_RELACSALDCAIXADESP PRIMARY KEY (NRSEQRELACLANCDESP) 
)
/

-- ============================================================
--   TABLE: LANCSALDCAIXA
-- ============================================================
ALTER TABLE LANCSALDCAIXA ADD VRLANCSALDUTIL FLOAT NULL
/

ALTER TABLE LANCSALDCAIXA ADD VRLANCSALDDISP FLOAT NULL
/

ALTER TABLE RELACSALDCAIXADESP ADD CDTIPODESP NUMBER NOT NULL
/

-- ============================================================
--   TABLE: RELACSALDCAIXADESP
-- ============================================================
alter table RELACSALDCAIXADESP
    add constraint FKFIN00398 foreign key  (NRSEQLANCDESP)
       references LANCDESPCAIXA   (NRSEQLANCDESP) on delete cascade
/

alter table RELACSALDCAIXADESP
    add constraint FKFIN00399 foreign key  (NRSEQLANCSALD)
       references LANCSALDCAIXA   (NRSEQLANCSALD) on delete cascade
/

-- ============================================================
--   TABLE: LANCSALDCAIXA
-- ============================================================
ALTER TABLE LANCSALDCAIXA ADD CDREDUCONT VARCHAR2(03) NULL
/

ALTER TABLE LANCSALDCAIXA ADD NRLANCCONT VARCHAR2(12) NULL
/

ALTER TABLE LANCDESPCAIXA ADD NRLANCCONT VARCHAR2(12) NULL
/

CREATE OR REPLACE PROCEDURE CTB_BCO_LANC(
   P_CDREDUCONT    IN CONTCORR.CDREDUCONT%TYPE,
   P_NRMOVI        IN MOVCREDBANC.NRMOVICRED%TYPE,
   P_IDTIPO        IN VARCHAR2,
   P_CDCENTCUST    FILIAL.CDCENTCUST%TYPE DEFAULT NULL,
   P_NRSEQLANCDESP IN NUMBER  DEFAULT 0,
   P_NRLANCCTB    OUT LANCCONT.NRLANCCONT%TYPE)
AS
   V_NRLANCTEMP NOVOCODIGO.NRSEQUENCIAL%TYPE;
   V_CDEMPRESA  LANCCONT.CDEMPRESA%TYPE;
   V_DSHISTLANC LANCCONT.DSHISTLANC%TYPE;
   V_CDHISTPADR HISTPADR.CDHISTPADR%TYPE;
   V_ERRO       INTEGER;
   V_CDCONTCOME LANCCONT.CDCONTCOME%TYPE;
   V_IDTIPOCOME CONTCTBL.IDTIPOCOME%TYPE;
   V_IDCONTCCUS CONTCTBL.IDCONTCCUS%TYPE;
   V_CDCENTCUST FILIAL.CDCENTCUST%TYPE;
   V_CDCENTCUSTOLD FILIAL.CDCENTCUST%TYPE;
   V_CDFILIAL   FILIAL.CDFILIAL%TYPE;
   V_COUNT      DOUBLE PRECISION;
   V_VRRATEIO   LANCCONT.VRLANCAMENT%TYPE;
   V_DTLANCTBTEMP LANCCONT.DTLANCCONT%TYPE;
   V_CREDDEB    VARCHAR2(01);
   V_RATSEQ     INTEGER;
   V_CONTRAT    VARCHAR2(01);
   V_CONTCOME   VARCHAR2(01);
   V_SEQUENCIAL VARCHAR2(05);
   V_CDCONTCTBL CONTCTBL.CDCONTCTBL%TYPE;
   V_NMCONTCORR CONTCORR.DSCONTCORR%TYPE;
   V_VRCREDITO  FLOAT;
   V_VRDIF      FLOAT;
   V_VRDEBITO   FLOAT;
   V_CONTREG    INTEGER;

   PROCEDURE BUSCA_TPCOMERCIAL(P_CDCONTCTBL IN VARCHAR2,
                               P_IDCOME     OUT VARCHAR2,
                               P_IDCCUS     OUT VARCHAR2)
   AS
      V_IDCODCOME VARCHAR2(01);
      V_IDCCUS VARCHAR2(21);
   BEGIN
      BEGIN
         SELECT NVL(IDTIPOCOME, 'N') IDTIPOCOME, IDCONTCCUS
           INTO V_IDCODCOME, V_IDCCUS
           FROM CONTCTBL
          WHERE CDCONTCTBL = P_CDCONTCTBL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_IDCODCOME := NULL;
            V_IDCCUS    := NULL;
      END;
      P_IDCOME := NVL(V_IDCODCOME,'N');
      P_IDCCUS := V_IDCCUS;
   END;

   FUNCTION BUSCA_CCUSTO(P_CDFILIAL IN VARCHAR, P_CDEMPRESA IN VARCHAR) RETURN VARCHAR
   IS
      V_CDCENTCUST VARCHAR2(04);
   BEGIN
      IF P_CDCENTCUST IS NULL THEN
      BEGIN
         SELECT CDCENTCUST
           INTO V_CDCENTCUST
           FROM FILIAL
          WHERE CDFILIAL = P_CDFILIAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDCENTCUST := NULL;
      END;
      ELSE
        V_CDCENTCUST := P_CDCENTCUST;
      END IF;
      IF V_CDCENTCUST IS NULL THEN
         BEGIN
            SELECT CDCENTCUST
              INTO V_CDCENTCUST
              FROM EMPRESA
             WHERE CDEMPRESA = P_CDEMPRESA;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_CDCENTCUST := NULL;
         END;
      END IF;
      RETURN(V_CDCENTCUST);
   END;

BEGIN
   V_ERRO := 0;

   BEGIN
      SELECT CDEMPRESA, CDCONTCTBL, DSCONTCORR
        INTO V_CDEMPRESA, V_CDCONTCTBL, V_NMCONTCORR
        FROM CONTCORR
       WHERE CDREDUCONT = P_CDREDUCONT;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDEMPRESA  := NULL;
         V_CDCONTCTBL := NULL;
         V_NMCONTCORR := NULL;
   END;

   BEGIN
      SELECT IDCONTGERE
        INTO V_CONTRAT
        FROM CONTOPECTBL
       WHERE CDEMPRESA = V_CDEMPRESA;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CONTRAT := NULL;
   END;
   IF P_IDTIPO = 'X' THEN
      BEGIN
         SELECT CDEMPRESA , DTLANCDESP
           INTO V_CDEMPRESA , V_DTLANCTBTEMP
           FROM LANCDESPCAIXA
          WHERE NRSEQLANCDESP = P_NRSEQLANCDESP;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDEMPRESA    := NULL;
            V_DTLANCTBTEMP := NULL;
      END;

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

      BEGIN
         SELECT COUNT(*)
           INTO V_COUNT
           FROM PARAMCTBL
          WHERE V_DTLANCTBTEMP >= DTINTCTBBCO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_COUNT := 0;
      END;

      IF V_NRLANCTEMP IS NULL THEN
       /* PEGA UM NOVO NÚMERO DE LANÇAMENTO CONTÁBIL, CASO O LANÇAMENTO NO BCO SEJA NOVO */
          NOVO_CODIGO_NT('LANCCONTABIL' || V_CDEMPRESA || TO_CHAR(V_DTLANCTBTEMP,'YYYYMM'), V_NRLANCTEMP);
          V_NRLANCTEMP := TO_CHAR(V_DTLANCTBTEMP,'YYYYMM')||SUBSTR(V_NRLANCTEMP, 15, 6);
      END IF;
      /* DELETA TODOS OS REGISTROS TEMPORÁRIOS PARA ESSE LANÇAMENTO */
      DELETE
        FROM LANCCTBTEMP
       WHERE NRLANCCTBTEMP = V_NRLANCTEMP
         AND CDEMPRESA     = V_CDEMPRESA;

      DELETE FROM RATECTBTEMP
       WHERE NRLANCCTBTEMP = V_NRLANCTEMP
         AND CDEMPRESA     = V_CDEMPRESA;

         FOR C_MOVI IN (SELECT FI.CDFILIAL, FI.CDCONTCTBL AS CONTA_CRED,
                               L.CDCENTCUST, TP.CDCONTCTBL AS CONTA_DEB, TP.CDHISTPADR, L.VRLANCDESP AS VRMOVICRED,
                               L.DSLANCDESP, L.CDEMPRESA , TP.NMDESPESA, L.DTLANCDESP  AS DTMOVICRED,
                               LPAD(TO_CHAR(NRSEQLANCDESP),8,'0') AS NRDOCMBANC
                          FROM LANCDESPCAIXA L, TIPODESPESAS TP, FILIAL FI
                         WHERE NRSEQLANCDESP   = P_NRSEQLANCDESP
                           AND TP.CDTIPODESP   = L.CDTIPODESP
                           AND L.CDFILIAL      = FI.CDFILIAL )LOOP

             V_DSHISTLANC := C_MOVI.NMDESPESA||'-'||C_MOVI.DSLANCDESP ;

             BUSCA_TPCOMERCIAL(C_MOVI.CONTA_DEB, V_IDTIPOCOME, V_IDCONTCCUS);

             V_CDCONTCOME    := NULL;
             V_CDCENTCUSTOLD := C_MOVI.CDCENTCUST;
             
             BEGIN
               SELECT COUNT(*)
                 INTO V_COUNT
                 FROM LANCCTBTEMP
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    = C_MOVI.CONTA_DEB;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_COUNT := 0;
            END;

            IF V_COUNT = 0 THEN
               INSERT INTO LANCCTBTEMP
                  (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                   DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
               VALUES
                  (V_CDEMPRESA, V_NRLANCTEMP, C_MOVI.NRDOCMBANC, '00001', C_MOVI.CONTA_DEB, C_MOVI.CDHISTPADR,
                   V_DSHISTLANC, C_MOVI.DTMOVICRED, C_MOVI.VRMOVICRED * -1, V_IDTIPOCOME, V_CDCONTCOME, 'BCO');
            ELSE
               UPDATE LANCCTBTEMP
                  SET VRLANCCTBTEMP = VRLANCCTBTEMP + (C_MOVI.VRMOVICRED * -1)
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    = C_MOVI.CONTA_DEB;
            END IF;

           IF V_CONTRAT = 'S' THEN
               IF V_IDCONTCCUS <> 'O' THEN
               IF V_CDCENTCUSTOLD IS NULL THEN
                  V_CDCENTCUST := BUSCA_CCUSTO(C_MOVI.CDFILIAL, V_CDEMPRESA);
               ELSE
                 V_CDCENTCUST := V_CDCENTCUSTOLD;
               END IF;
                  BEGIN
                     SELECT COUNT(*)
                       INTO V_COUNT
                       FROM RATECTBTEMP
                      WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                        AND CDEMPRESA     = V_CDEMPRESA
                        AND CDFILIAL      = C_MOVI.CDFILIAL
                        AND CDCENTCUST    = V_CDCENTCUST
                        AND NRSEQCTBTEMP  = '00001';
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_COUNT := 0;
                  END;
                  
                  IF V_COUNT = 0 THEN
                     INSERT INTO RATECTBTEMP
                        (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                     VALUES
                        (V_CDEMPRESA, V_NRLANCTEMP, '00001', C_MOVI.CDFILIAL, V_CDCENTCUST, C_MOVI.VRMOVICRED * -1, C_MOVI.DTMOVICRED, '00001');
                  ELSE
                     UPDATE RATECTBTEMP
                        SET VRRATECTBTEMP = VRRATECTBTEMP + (C_MOVI.VRMOVICRED * -1)
                      WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                        AND CDEMPRESA     = V_CDEMPRESA
                        AND CDFILIAL      = C_MOVI.CDFILIAL
                        AND CDCENTCUST    = V_CDCENTCUST
                        AND NRSEQCTBTEMP  = '00001';
                  END IF;
               END IF;
            END IF;

            /*************************************************************************************
                                  PARTIDA DE CRÉDITO
             ************************************************************************************/
            V_IDTIPOCOME := NULL;
            V_IDCONTCCUS := NULL;
            V_VRRATEIO   := 0;
            V_COUNT      := 0;
             BUSCA_TPCOMERCIAL(C_MOVI.CONTA_CRED, V_IDTIPOCOME, V_IDCONTCCUS);
            IF V_IDTIPOCOME = 'B' THEN
               V_CDCONTCOME := P_CDREDUCONT;
               
            ELSE
              V_CDCONTCOME := NULL;
            END IF;
             
             V_CDCENTCUSTOLD := C_MOVI.CDCENTCUST;
             BEGIN
               SELECT COUNT(*)
                 INTO V_COUNT
                 FROM LANCCTBTEMP
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    =  C_MOVI.CONTA_CRED;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_COUNT := 0;
            END;

            IF V_COUNT = 0 THEN
               INSERT INTO LANCCTBTEMP
                  (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                   DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
               VALUES
                  (V_CDEMPRESA, V_NRLANCTEMP, C_MOVI.NRDOCMBANC, '00002', C_MOVI.CONTA_CRED, C_MOVI.CDHISTPADR,
                   V_DSHISTLANC, C_MOVI.DTMOVICRED, C_MOVI.VRMOVICRED , V_IDTIPOCOME, V_CDCONTCOME, 'BCO');
            ELSE
               UPDATE LANCCTBTEMP
                  SET VRLANCCTBTEMP = VRLANCCTBTEMP + (C_MOVI.VRMOVICRED)
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    = C_MOVI.CONTA_CRED;
            END IF;

           IF V_CONTRAT = 'S' THEN
               IF V_IDCONTCCUS <> 'O' THEN
               IF V_CDCENTCUSTOLD IS NULL THEN
                  V_CDCENTCUST := BUSCA_CCUSTO(C_MOVI.CDFILIAL, V_CDEMPRESA);
               ELSE
                 V_CDCENTCUST := V_CDCENTCUSTOLD;
               END IF;
                  BEGIN
                     SELECT COUNT(*)
                       INTO V_COUNT
                       FROM RATECTBTEMP
                      WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                        AND CDEMPRESA     = V_CDEMPRESA
                        AND CDFILIAL      = C_MOVI.CDFILIAL
                        AND CDCENTCUST    = V_CDCENTCUST
                        AND NRSEQCTBTEMP  = '00002';
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_COUNT := 0;
                  END;

                  IF V_COUNT = 0 THEN
                     INSERT INTO RATECTBTEMP
                        (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                     VALUES
                        (V_CDEMPRESA, V_NRLANCTEMP, '00002', C_MOVI.CDFILIAL, V_CDCENTCUST, C_MOVI.VRMOVICRED, C_MOVI.DTMOVICRED, '00001');
                  ELSE
                     UPDATE RATECTBTEMP
                        SET VRRATECTBTEMP = VRRATECTBTEMP + (C_MOVI.VRMOVICRED)
                      WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                        AND CDEMPRESA     = V_CDEMPRESA
                        AND CDFILIAL      = C_MOVI.CDFILIAL
                        AND CDCENTCUST    = V_CDCENTCUST
                        AND NRSEQCTBTEMP  = '00002';
                  END IF;
               END IF;
            END IF;

         END LOOP;

         VERIFICA_ERRO_CTB(V_NRLANCTEMP, V_CDEMPRESA);

         BEGIN
            SELECT COUNT(*)
              INTO V_COUNT
              FROM ERROCTBTEMP
             WHERE NRLANCCTBTEMP = V_NRLANCTEMP
               AND CDEMPRESA = V_CDEMPRESA;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_COUNT := 0;
         END;
      IF V_COUNT = 0 THEN
         /* GRAVA O LANÇAMENTO TEMPORÁRIO SE NÃO CONTER ERRO NA LANCCONT E LANCCUSTO, APÓS GRAVAR IRÁ APAGAR O LANCAMENTO TEMPORÁRIO */
         UPDATE LANCDESPCAIXA
            SET NRLANCCONT = V_NRLANCTEMP
          WHERE NRSEQLANCDESP = P_NRSEQLANCDESP;
         GRAVA_LANC_CTB(V_NRLANCTEMP, V_CDEMPRESA);
      END IF;
      
      P_NRLANCCTB := V_NRLANCTEMP;
   ELSIF P_IDTIPO = 'S' THEN
     BEGIN
         SELECT CDEMPRESA , DTLANCSALD
           INTO V_CDEMPRESA , V_DTLANCTBTEMP
           FROM LANCSALDCAIXA
          WHERE NRSEQLANCSALD = P_NRSEQLANCDESP;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDEMPRESA    := NULL;
            V_DTLANCTBTEMP := NULL;
      END;

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

      BEGIN
         SELECT COUNT(*)
           INTO V_COUNT
           FROM PARAMCTBL
          WHERE V_DTLANCTBTEMP >= DTINTCTBBCO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_COUNT := 0;
      END;

      IF V_NRLANCTEMP IS NULL THEN
       /* PEGA UM NOVO NÚMERO DE LANÇAMENTO CONTÁBIL, CASO O LANÇAMENTO NO BCO SEJA NOVO */
          NOVO_CODIGO_NT('LANCCONTABIL' || V_CDEMPRESA || TO_CHAR(V_DTLANCTBTEMP,'YYYYMM'), V_NRLANCTEMP);
          V_NRLANCTEMP := TO_CHAR(V_DTLANCTBTEMP,'YYYYMM')||SUBSTR(V_NRLANCTEMP, 15, 6);
      END IF;
      /* DELETA TODOS OS REGISTROS TEMPORÁRIOS PARA ESSE LANÇAMENTO */
      DELETE
        FROM LANCCTBTEMP
       WHERE NRLANCCTBTEMP = V_NRLANCTEMP
         AND CDEMPRESA     = V_CDEMPRESA;

      DELETE FROM RATECTBTEMP
       WHERE NRLANCCTBTEMP = V_NRLANCTEMP
         AND CDEMPRESA     = V_CDEMPRESA;
         BEGIN
             SELECT CDHISTPADR
               INTO V_CDHISTPADR
               FROM HISTINTEGRA
              WHERE CDOPERACAO = 'TRANSF';
         EXCEPTION
                WHEN NO_DATA_FOUND  THEN
            V_CDHISTPADR := NULL;
         END;
         FOR C_MOVI IN (SELECT FI.CDFILIAL, FI.CDCONTCTBL AS CONTA_DEB, 
                               L.CDCENTCUST, V_CDCONTCTBL AS CONTA_CRED, L.VRLANCSALD AS VRMOVICRED,
                               V_NMCONTCORR AS DSLANCDESP, L.CDEMPRESA ,  L.DTLANCSALD  AS DTMOVICRED,
                               LPAD(TO_CHAR(NRSEQLANCSALD),8,'0') AS NRDOCMBANC
                          FROM LANCSALDCAIXA L, FILIAL FI
                         WHERE NRSEQLANCSALD   = P_NRSEQLANCDESP
                           AND L.CDFILIAL      = FI.CDFILIAL )LOOP

             V_DSHISTLANC := 'Entrada'||' '||C_MOVI.DSLANCDESP ;

             BUSCA_TPCOMERCIAL(C_MOVI.CONTA_DEB, V_IDTIPOCOME, V_IDCONTCCUS);

             V_CDCONTCOME    := NULL;
             V_CDCENTCUSTOLD := C_MOVI.CDCENTCUST;
             
             BEGIN
               SELECT COUNT(*)
                 INTO V_COUNT
                 FROM LANCCTBTEMP
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    = C_MOVI.CONTA_DEB;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_COUNT := 0;
            END;

            IF V_COUNT = 0 THEN
               INSERT INTO LANCCTBTEMP
                  (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                   DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
               VALUES
                  (V_CDEMPRESA, V_NRLANCTEMP, C_MOVI.NRDOCMBANC, '00001', C_MOVI.CONTA_DEB, V_CDHISTPADR,
                   V_DSHISTLANC, C_MOVI.DTMOVICRED, C_MOVI.VRMOVICRED * -1, V_IDTIPOCOME, V_CDCONTCOME, 'BCO');
            ELSE
               UPDATE LANCCTBTEMP
                  SET VRLANCCTBTEMP = VRLANCCTBTEMP + (C_MOVI.VRMOVICRED * -1)
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    = C_MOVI.CONTA_DEB;
            END IF;

           IF V_CONTRAT = 'S' THEN
               IF V_IDCONTCCUS <> 'O' THEN
               IF V_CDCENTCUSTOLD IS NULL THEN
                  V_CDCENTCUST := BUSCA_CCUSTO(C_MOVI.CDFILIAL, V_CDEMPRESA);
               ELSE
                 V_CDCENTCUST := V_CDCENTCUSTOLD;
               END IF;
                  BEGIN
                     SELECT COUNT(*)
                       INTO V_COUNT
                       FROM RATECTBTEMP
                      WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                        AND CDEMPRESA     = V_CDEMPRESA
                        AND CDFILIAL      = C_MOVI.CDFILIAL
                        AND CDCENTCUST    = V_CDCENTCUST
                        AND NRSEQCTBTEMP  = '00001';
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_COUNT := 0;
                  END;
                  
                  IF V_COUNT = 0 THEN
                     INSERT INTO RATECTBTEMP
                        (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                     VALUES
                        (V_CDEMPRESA, V_NRLANCTEMP, '00001', C_MOVI.CDFILIAL, V_CDCENTCUST, C_MOVI.VRMOVICRED * -1, C_MOVI.DTMOVICRED, '00001');
                  ELSE
                     UPDATE RATECTBTEMP
                        SET VRRATECTBTEMP = VRRATECTBTEMP + (C_MOVI.VRMOVICRED * -1)
                      WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                        AND CDEMPRESA     = V_CDEMPRESA
                        AND CDFILIAL      = C_MOVI.CDFILIAL
                        AND CDCENTCUST    = V_CDCENTCUST
                        AND NRSEQCTBTEMP  = '00001';
                  END IF;
               END IF;
            END IF;

            /*************************************************************************************
                                  PARTIDA DE CRÉDITO
             ************************************************************************************/
            V_IDTIPOCOME := NULL;
            V_IDCONTCCUS := NULL;
            V_VRRATEIO   := 0;
            V_COUNT      := 0;
             BUSCA_TPCOMERCIAL(C_MOVI.CONTA_CRED, V_IDTIPOCOME, V_IDCONTCCUS);

             
               IF V_IDTIPOCOME = 'B' THEN
                 V_CDCONTCOME := P_CDREDUCONT;
               
               ELSE
                V_CDCONTCOME := NULL;
               END IF;
             
             V_CDCENTCUSTOLD := C_MOVI.CDCENTCUST;
             BEGIN
               SELECT COUNT(*)
                 INTO V_COUNT
                 FROM LANCCTBTEMP
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    =  C_MOVI.CONTA_CRED;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_COUNT := 0;
            END;

            IF V_COUNT = 0 THEN
               INSERT INTO LANCCTBTEMP
                  (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                   DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
               VALUES
                  (V_CDEMPRESA, V_NRLANCTEMP, C_MOVI.NRDOCMBANC, '00002', C_MOVI.CONTA_CRED, V_CDHISTPADR,
                   V_DSHISTLANC, C_MOVI.DTMOVICRED, C_MOVI.VRMOVICRED , V_IDTIPOCOME, V_CDCONTCOME, 'BCO');
            ELSE
               UPDATE LANCCTBTEMP
                  SET VRLANCCTBTEMP = VRLANCCTBTEMP + (C_MOVI.VRMOVICRED)
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    = C_MOVI.CONTA_CRED;
            END IF;

           IF V_CONTRAT = 'S' THEN
               IF V_IDCONTCCUS <> 'O' THEN
               IF V_CDCENTCUSTOLD IS NULL THEN
                  V_CDCENTCUST := BUSCA_CCUSTO(C_MOVI.CDFILIAL, V_CDEMPRESA);
               ELSE
                 V_CDCENTCUST := V_CDCENTCUSTOLD;
               END IF;
                  BEGIN
                     SELECT COUNT(*)
                       INTO V_COUNT
                       FROM RATECTBTEMP
                      WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                        AND CDEMPRESA     = V_CDEMPRESA
                        AND CDFILIAL      = C_MOVI.CDFILIAL
                        AND CDCENTCUST    = V_CDCENTCUST
                        AND NRSEQCTBTEMP  = '00002';
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_COUNT := 0;
                  END;

                  IF V_COUNT = 0 THEN
                     INSERT INTO RATECTBTEMP
                        (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                     VALUES
                        (V_CDEMPRESA, V_NRLANCTEMP, '00002', C_MOVI.CDFILIAL, V_CDCENTCUST, C_MOVI.VRMOVICRED, C_MOVI.DTMOVICRED, '00001');
                  ELSE
                     UPDATE RATECTBTEMP
                        SET VRRATECTBTEMP = VRRATECTBTEMP + (C_MOVI.VRMOVICRED)
                      WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                        AND CDEMPRESA     = V_CDEMPRESA
                        AND CDFILIAL      = C_MOVI.CDFILIAL
                        AND CDCENTCUST    = V_CDCENTCUST
                        AND NRSEQCTBTEMP  = '00002';
                  END IF;
               END IF;
            END IF;

         END LOOP;

         VERIFICA_ERRO_CTB(V_NRLANCTEMP, V_CDEMPRESA);

         BEGIN
            SELECT COUNT(*)
              INTO V_COUNT
              FROM ERROCTBTEMP
             WHERE NRLANCCTBTEMP = V_NRLANCTEMP
               AND CDEMPRESA = V_CDEMPRESA;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_COUNT := 0;
         END;
      IF V_COUNT = 0 THEN
         /* GRAVA O LANÇAMENTO TEMPORÁRIO SE NÃO CONTER ERRO NA LANCCONT E LANCCUSTO, APÓS GRAVAR IRÁ APAGAR O LANCAMENTO TEMPORÁRIO */
         UPDATE LANCSALDCAIXA
            SET NRLANCCONT = V_NRLANCTEMP
          WHERE NRSEQLANCSALD = P_NRSEQLANCDESP;
         GRAVA_LANC_CTB(V_NRLANCTEMP, V_CDEMPRESA);
      END IF;
      
      P_NRLANCCTB := V_NRLANCTEMP;
    
   ELSIF P_IDTIPO = 'C' THEN
      /* PEGA A DATA DE MOVIMENTAÇÃO, É A DATA INFORMADA NA TELA BCO11100 */

      BEGIN
         SELECT MAX(CDCENTCUST)
           INTO V_CDCENTCUSTOLD
           FROM LANCCCUSTO
           WHERE NRLANCCONT = V_NRLANCTEMP
            AND CDEMPRESA  = V_CDEMPRESA;
      EXCEPTION
          WHEN NO_DATA_FOUND THEN
           V_CDCENTCUSTOLD := NULL;
      END;

      BEGIN
         SELECT DTMOVICRED, NRLCTBMCRED
           INTO V_DTLANCTBTEMP, V_NRLANCTEMP
           FROM MOVCREDBANC
          WHERE CDREDUCONT = P_CDREDUCONT
            AND NRMOVICRED = P_NRMOVI;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_DTLANCTBTEMP := NULL;
            V_NRLANCTEMP := NULL;
      END;
      IF V_NRLANCTEMP IS NOT NULL THEN
         /* DELETE RESPONSÁVEL POR DELETAR OS LANÇAMENTOS NA LANCCONT COM O MESMO SEQUENCIAL DO LANÇAMENTO NO BCO, POSTERIORMENTE IRÁ RECONTABILIZAR O MESMO */
         DELETE
           FROM LANCCCUSTO
          WHERE NRLANCCONT = V_NRLANCTEMP
            AND CDEMPRESA  = V_CDEMPRESA;

         DELETE
           FROM LANCCONT
          WHERE NRLANCCONT = V_NRLANCTEMP
            AND CDEMPRESA  = V_CDEMPRESA;

         DELETE
           FROM RATECTBTEMP
          WHERE NRLANCCTBTEMP = V_NRLANCTEMP
            AND CDEMPRESA = V_CDEMPRESA;

         DELETE
           FROM LANCCTBTEMP
          WHERE NRLANCCTBTEMP = V_NRLANCTEMP
            AND CDEMPRESA = V_CDEMPRESA;
      END IF;

      BEGIN
         SELECT COUNT(*)
           INTO V_COUNT
           FROM PARAMCTBL
          WHERE V_DTLANCTBTEMP >= DTINTCTBBCO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_COUNT := 0;
      END;

      IF V_COUNT > 0 THEN
         IF V_NRLANCTEMP IS NULL THEN
         /* PEGA UM NOVO NÚMERO DE LANÇAMENTO CONTÁBIL, CASO O LANÇAMENTO NO BCO SEJA NOVO */
            NOVO_CODIGO_NT('LANCCONTABIL' || V_CDEMPRESA || TO_CHAR(V_DTLANCTBTEMP,'YYYYMM'), V_NRLANCTEMP);
            V_NRLANCTEMP := TO_CHAR(V_DTLANCTBTEMP,'YYYYMM')||SUBSTR(V_NRLANCTEMP, 15, 6);
         END IF;

         /* DELETA TODOS OS REGISTROS TEMPORÁRIOS PARA ESSE LANÇAMENTO */
         DELETE
           FROM LANCCTBTEMP
          WHERE NRLANCCTBTEMP = V_NRLANCTEMP
            AND CDEMPRESA     = V_CDEMPRESA;

         DELETE FROM RATECTBTEMP
          WHERE NRLANCCTBTEMP = V_NRLANCTEMP
            AND CDEMPRESA     = V_CDEMPRESA;

         FOR C_MOVI IN (
            SELECT M.CDREDUCONT, M.NRMOVICRED, T.CDHISTPADR, C.CDCONTCTBL CONTA_DEB, DTMOVICRED,
                   M.VRMOVICRED, M.CDCLIENTE, M.CDFORNECED, M.CDFILIAL, T.CDCONTCTBL CONTA_CRE,
                   M.CDTPCREDBANC, B.NRDOCMBANC
              FROM MOVCREDBANC M, TPCREDBANC T, CONTCORR C, MOVBANCARIO B
             WHERE M.CDTPCREDBANC = T.CDTPCREDBANC
               AND M.CDREDUCONT   = C.CDREDUCONT
               AND M.NRMOVBANC    = B.NRMOVBANC
               AND M.CDREDUCONT   = B.CDREDUCONT
               AND M.CDREDUCONT = P_CDREDUCONT
               AND M.NRMOVICRED = P_NRMOVI) LOOP

            -- Formata a descrição do histórico --
            V_DSHISTLANC := CTB_HIST_BCO(C_MOVI.CDREDUCONT, C_MOVI.NRMOVICRED, C_MOVI.CDHISTPADR, '', 'C');

            /*************************************************************************************
                                  PARTIDA DE DÉBITO
             ************************************************************************************/
            -- Verifica se a conta contábil é do tipo comercial (banco, fornecedor ou cliente) --
            BUSCA_TPCOMERCIAL(C_MOVI.CONTA_DEB, V_IDTIPOCOME, V_IDCONTCCUS);

            IF V_IDTIPOCOME = 'B' THEN
               V_CDCONTCOME := C_MOVI.CDREDUCONT;

            ELSIF V_IDTIPOCOME = 'C' THEN
               V_CDCONTCOME := C_MOVI.CDCLIENTE;

            ELSIF V_IDTIPOCOME = 'F' THEN
               V_CDCONTCOME := C_MOVI.CDFORNECED;

            ELSE
               V_CDCONTCOME := NULL;
            END IF;

            BEGIN
               SELECT COUNT(*)
                 INTO V_COUNT
                 FROM LANCCTBTEMP
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    = C_MOVI.CONTA_DEB;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_COUNT := 0;
            END;

            IF V_COUNT = 0 THEN
               INSERT INTO LANCCTBTEMP
                  (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                   DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
               VALUES
                  (V_CDEMPRESA, V_NRLANCTEMP, C_MOVI.NRDOCMBANC, '00001', C_MOVI.CONTA_DEB, C_MOVI.CDHISTPADR,
                   V_DSHISTLANC, C_MOVI.DTMOVICRED, C_MOVI.VRMOVICRED * -1, V_IDTIPOCOME, V_CDCONTCOME, 'BCO');
            ELSE
               UPDATE LANCCTBTEMP
                  SET VRLANCCTBTEMP = VRLANCCTBTEMP + (C_MOVI.VRMOVICRED * -1)
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    = C_MOVI.CONTA_DEB;
            END IF;

            -- Realiza o rateio caso a conta contábil não seja "Só Oficial" --
            IF V_CONTRAT = 'S' THEN
               IF V_IDCONTCCUS <> 'O' THEN
               IF V_CDCENTCUSTOLD IS NULL THEN
                  V_CDCENTCUST := BUSCA_CCUSTO(C_MOVI.CDFILIAL, V_CDEMPRESA);
               ELSE
                 V_CDCENTCUST := V_CDCENTCUSTOLD;
               END IF;
                  BEGIN
                     SELECT COUNT(*)
                       INTO V_COUNT
                       FROM RATECTBTEMP
                      WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                        AND CDEMPRESA     = V_CDEMPRESA
                        AND CDFILIAL      = C_MOVI.CDFILIAL
                        AND CDCENTCUST    = V_CDCENTCUST
                        AND NRSEQCTBTEMP  = '00001';
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_COUNT := 0;
                  END;

                  IF V_COUNT = 0 THEN
                     INSERT INTO RATECTBTEMP
                        (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                     VALUES
                        (V_CDEMPRESA, V_NRLANCTEMP, '00001', C_MOVI.CDFILIAL, V_CDCENTCUST, C_MOVI.VRMOVICRED * -1, C_MOVI.DTMOVICRED, '00001');
                  ELSE
                     UPDATE RATECTBTEMP
                        SET VRRATECTBTEMP = VRRATECTBTEMP + (C_MOVI.VRMOVICRED * -1)
                      WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                        AND CDEMPRESA     = V_CDEMPRESA
                        AND CDFILIAL      = C_MOVI.CDFILIAL
                        AND CDCENTCUST    = V_CDCENTCUST
                        AND NRSEQCTBTEMP  = '00001';
                  END IF;
               END IF;
            END IF;

            /*************************************************************************************
                                  PARTIDA DE CRÉDITO
             ************************************************************************************/
            V_IDTIPOCOME := NULL;
            V_IDCONTCCUS := NULL;
            V_VRRATEIO   := 0;
            V_COUNT      := 0;


            -- Verifica se a conta contábil é do tipo comercial (banco, fornecedor ou cliente) --
            BUSCA_TPCOMERCIAL(C_MOVI.CONTA_CRE, V_IDTIPOCOME, V_IDCONTCCUS);

            IF V_IDTIPOCOME = 'B' THEN
               V_CDCONTCOME := C_MOVI.CDREDUCONT;

            ELSIF V_IDTIPOCOME = 'C' THEN
               V_CDCONTCOME := C_MOVI.CDCLIENTE;

            ELSIF V_IDTIPOCOME = 'F' THEN
               V_CDCONTCOME := C_MOVI.CDFORNECED;

            ELSE
               V_CDCONTCOME := NULL;
            END IF;

            BEGIN
               SELECT COUNT(*)
                 INTO V_COUNT
                 FROM LANCCTBTEMP
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    = C_MOVI.CONTA_CRE;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_COUNT := 0;
            END;

            IF V_COUNT = 0 THEN
               INSERT INTO LANCCTBTEMP
                  (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                   DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
               VALUES
                  (V_CDEMPRESA, V_NRLANCTEMP, C_MOVI.NRDOCMBANC, '00002', C_MOVI.CONTA_CRE, C_MOVI.CDHISTPADR,
                   V_DSHISTLANC, C_MOVI.DTMOVICRED, C_MOVI.VRMOVICRED, V_IDTIPOCOME, V_CDCONTCOME, 'BCO');
            ELSE
               UPDATE LANCCTBTEMP
                  SET VRLANCCTBTEMP = VRLANCCTBTEMP + C_MOVI.VRMOVICRED
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    = C_MOVI.CONTA_CRE;
            END IF;

            IF V_CONTRAT = 'S' THEN
               IF V_IDCONTCCUS <> 'O' THEN
                  BEGIN
                     SELECT COUNT(*)
                       INTO V_COUNT
                       FROM RATTPCRED
                      WHERE CDTPCREDBANC = C_MOVI.CDTPCREDBANC;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_COUNT := 0;
                  END;

                   IF V_COUNT > 0 THEN
                      V_RATSEQ := 1;
                      FOR RAT IN(
                         SELECT CDCENTCUST, CDFILIAL, VRPERATTPCRE
                           FROM RATTPCRED
                          WHERE CDTPCREDBANC = C_MOVI.CDTPCREDBANC) LOOP

                         BEGIN
                            SELECT COUNT(*)
                              INTO V_COUNT
                              FROM RATECTBTEMP
                             WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                               AND CDEMPRESA     = V_CDEMPRESA
                               AND NRSEQCTBTEMP  = '00002'
                               AND CDFILIAL      = NVL(RAT.CDFILIAL, C_MOVI.CDFILIAL)
                               AND CDCENTCUST    = RAT.CDCENTCUST;
                         EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                               V_COUNT := 0;
                         END;

                         IF V_COUNT = 0 THEN
                             INSERT INTO RATECTBTEMP
                                (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                             VALUES
                                (V_CDEMPRESA, V_NRLANCTEMP, '00002', NVL(RAT.CDFILIAL, C_MOVI.CDFILIAL), RAT.CDCENTCUST,
                                 ROUND((C_MOVI.VRMOVICRED * RAT.VRPERATTPCRE)/100, 2), C_MOVI.DTMOVICRED, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                         ELSE
                            UPDATE RATECTBTEMP
                               SET VRRATECTBTEMP = VRRATECTBTEMP +  ROUND((C_MOVI.VRMOVICRED * RAT.VRPERATTPCRE)/100, 2)
                             WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                               AND CDEMPRESA     = V_CDEMPRESA
                               AND NRSEQCTBTEMP  = '00002'
                               AND CDFILIAL      = NVL(RAT.CDFILIAL, C_MOVI.CDFILIAL)
                               AND CDCENTCUST    = RAT.CDCENTCUST;
                         END IF;

                         V_VRRATEIO := V_VRRATEIO + ROUND((C_MOVI.VRMOVICRED * RAT.VRPERATTPCRE)/100, 2);

                         V_RATSEQ := V_RATSEQ + 1;

                         /* FILIAL E CENTRO DE CUSTO USADOS PARA ARREDONDAR ULTIMO RATEIO, CASO DÊ DIFERENÇA */
                         V_CDFILIAL   := NVL(RAT.CDFILIAL, C_MOVI.CDFILIAL);
                         V_CDCENTCUST := RAT.CDCENTCUST;
                      END LOOP;
                      V_RATSEQ := 1;
                      IF V_VRRATEIO <> C_MOVI.VRMOVICRED THEN
                         V_VRRATEIO := C_MOVI.VRMOVICRED - V_VRRATEIO;
                         UPDATE RATECTBTEMP
                            SET VRRATECTBTEMP = VRRATECTBTEMP + V_VRRATEIO
                          WHERE CDEMPRESA     = V_CDEMPRESA
                            AND NRLANCCTBTEMP = V_NRLANCTEMP
                            AND NRSEQCTBTEMP  = '00002'
                            AND CDFILIAL      = V_CDFILIAL
                            AND CDCENTCUST    = V_CDCENTCUST;
                      END IF;
                   ELSE
                   IF V_CDCENTCUSTOLD IS NULL THEN
                      V_CDCENTCUST := BUSCA_CCUSTO(C_MOVI.CDFILIAL, V_CDEMPRESA);
                   ELSE
                     V_CDCENTCUST := V_CDCENTCUSTOLD;
                   END IF;

                      BEGIN
                         SELECT COUNT(*), R.NRSEQCTBTEMP
                           INTO V_COUNT, V_SEQUENCIAL
                           FROM RATECTBTEMP R, LANCCTBTEMP L
                          WHERE R.CDEMPRESA     = V_CDEMPRESA
                            AND R.NRLANCCTBTEMP = V_NRLANCTEMP
                            AND CDFILIAL        = NVL(V_CDFILIAL, C_MOVI.CDFILIAL)
                            AND CDCENTCUST      = V_CDCENTCUST
                            AND L.NRLANCCTBTEMP = R.NRLANCCTBTEMP
                            AND L.CDEMPRESA     = R.CDEMPRESA
                            AND L.NRSEQCTBTEMP  = R.NRSEQCTBTEMP
                            AND L.CDCONTCTBL    = C_MOVI.CONTA_CRE
                          GROUP BY R.NRSEQCTBTEMP;
                      EXCEPTION
                         WHEN NO_DATA_FOUND THEN
                            V_COUNT := 0;
                            V_SEQUENCIAL := NULL;
                      END;

                      IF V_COUNT = 0 THEN
                         INSERT INTO RATECTBTEMP
                            (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                         VALUES
                            (V_CDEMPRESA, V_NRLANCTEMP, '00002', NVL(V_CDFILIAL, C_MOVI.CDFILIAL), V_CDCENTCUST, C_MOVI.VRMOVICRED, C_MOVI.DTMOVICRED, '00001');
                      ELSE
                         UPDATE RATECTBTEMP
                            SET VRRATECTBTEMP = VRRATECTBTEMP + C_MOVI.VRMOVICRED
                          WHERE CDEMPRESA     = V_CDEMPRESA
                            AND NRLANCCTBTEMP = V_NRLANCTEMP
                            AND CDFILIAL      = NVL(V_CDFILIAL, C_MOVI.CDFILIAL)
                            AND CDCENTCUST    = V_CDCENTCUST
                            AND NRSEQCTBTEMP  = V_SEQUENCIAL;
                      END IF;
                   END IF;
               END IF;
            END IF;
         END LOOP;
         /* TÉRMINO DAS PARTIDAS DO LANCAMENTO CONTABIL*/

         /*VERIFICACAO DE ERROS NAS PARTIDAS E NO LANCAMENTO CONTABIL*/
         VERIFICA_ERRO_CTB(V_NRLANCTEMP, V_CDEMPRESA);

         BEGIN
            SELECT COUNT(*)
              INTO V_COUNT
              FROM ERROCTBTEMP
             WHERE NRLANCCTBTEMP = V_NRLANCTEMP
               AND CDEMPRESA = V_CDEMPRESA;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_COUNT := 0;
         END;

         IF V_COUNT = 0 THEN
            /*SALVA NA TABELA DE MOVIMENTAÇÃO DE CRÉDITO BANCÁRIO, O NÚMERO DE LANÇAMENTO CONTÁBIL*/
            UPDATE MOVCREDBANC
               SET NRLCTBMCRED = V_NRLANCTEMP
             WHERE CDREDUCONT = P_CDREDUCONT
               AND NRMOVICRED = P_NRMOVI;
         END IF;

         /* GRAVA O LANÇAMENTO TEMPORÁRIO SE NÃO CONTER ERRO NA LANCCONT E LANCCUSTO, APÓS GRAVAR IRÁ APAGAR O LANCAMENTO TEMPORÁRIO */
         GRAVA_LANC_CTB(V_NRLANCTEMP, V_CDEMPRESA);
         P_NRLANCCTB := V_NRLANCTEMP;
      END IF;
   ELSIF P_IDTIPO = 'B' THEN

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

      BEGIN
         SELECT COUNT(*)
           INTO V_COUNT
           FROM PARAMCTBL
          WHERE V_DTLANCTBTEMP >= DTINTCTBBCO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_COUNT := 0;
      END;
      BEGIN
         SELECT DTMOVICRED, NRLCTBMCRED
           INTO V_DTLANCTBTEMP, V_NRLANCTEMP
           FROM MOVCREDBANC
          WHERE CDREDUCONT = P_CDREDUCONT
            AND NRMOVICRED = P_NRMOVI;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_DTLANCTBTEMP := NULL;
            V_NRLANCTEMP := NULL;
      END;

      IF V_NRLANCTEMP IS NULL THEN
       /* PEGA UM NOVO NÚMERO DE LANÇAMENTO CONTÁBIL, CASO O LANÇAMENTO NO BCO SEJA NOVO */
          NOVO_CODIGO_NT('LANCCONTABIL' || V_CDEMPRESA || TO_CHAR(V_DTLANCTBTEMP,'YYYYMM'), V_NRLANCTEMP);
          V_NRLANCTEMP := TO_CHAR(V_DTLANCTBTEMP,'YYYYMM')||SUBSTR(V_NRLANCTEMP, 15, 6);
      END IF;

      /* DELETA TODOS OS REGISTROS TEMPORÁRIOS PARA ESSE LANÇAMENTO */
      DELETE
        FROM LANCCTBTEMP
       WHERE NRLANCCTBTEMP = V_NRLANCTEMP
         AND CDEMPRESA     = V_CDEMPRESA;

      DELETE FROM RATECTBTEMP
       WHERE NRLANCCTBTEMP = V_NRLANCTEMP
         AND CDEMPRESA     = V_CDEMPRESA;
      V_CONTREG := 1;
      V_VRCREDITO  := 0;
      V_VRDIF      := 0;
      V_VRDEBITO   := 0;
--RAISE_APPLICATION_ERROR(-20501,P_CDREDUCONT||'-'||P_NRMOVI,TRUE);
       FOR C_MOVI_B IN (SELECT DISTINCT
                                BO.CDFILIAL,
                                BO.NRLANBOLREC,
                                BO.CDCLIENTE,
                                CL.NMRAZSOCCLIE,
                                BO.VRFINALBREC,
                                BO.CDREDUCONT,
                                BO.CDEMPRESA,
                                CC.CDCONTCTBL AS CONTA_DEB,
                                CC.CDCCTBCAUCIO AS CONTA_DESC,
                                BO.NRMOVICRED,
                                ''  AS CDFORNECED ,
                                TP.CDHISTPADR,
                                TP.CDCONTCTBL AS CONTA_CRE,
                                MV.DTMOVICRED,
                                MO.NRDOCMBANC,
                                MV.VRMOVICRED,
                                TP.CDTPCREDBANC,
                                ROWNUM
                             FROM
                                CLIENTE      CL,
                                BOLETRECEBER BO,
                                CONTCORR     CC,
                                MOVCREDBANC  MV,
                                TPCREDBANC   TP,
                                MOVBANCARIO  MO
                             WHERE
                                (BO.CDREDUCONT = P_CDREDUCONT) AND
                                (BO.CDREDUCONT = CC.CDREDUCONT) AND
                                (BO.NRMOVICRED = P_NRMOVI) AND
                                (BO.CDCLIENTE  = CL.CDCLIENTE ) AND
                                (MV.CDREDUCONT  = BO.CDREDUCONT)   AND
                                (MV.NRMOVICRED  = BO.NRMOVICRED) AND
                                (TP.CDTPCREDBANC = MV.CDTPCREDBANC) AND
                                (MO.CDREDUCONT   = MV.CDREDUCONT)  AND
                                (MO.NRMOVBANC    = MV.NRMOVBANC)
                                ORDER BY ROWNUM) LOOP

          -- Formata a descrição do histórico --
          V_DSHISTLANC := CTB_HIST_BCO(C_MOVI_B.CDREDUCONT, C_MOVI_B.NRMOVICRED, C_MOVI_B.CDHISTPADR, '', 'C');

          /*************************************************************************************
                                PARTIDA DE DÉBITO
           ************************************************************************************/
          -- Verifica se a conta contábil é do tipo comercial (banco, fornecedor ou cliente) --
          BUSCA_TPCOMERCIAL(C_MOVI_B.CONTA_DEB, V_IDTIPOCOME, V_IDCONTCCUS);

          IF V_IDTIPOCOME = 'B' THEN
             V_CDCONTCOME := C_MOVI_B.CDREDUCONT;

          ELSIF V_IDTIPOCOME = 'C' THEN
             V_CDCONTCOME := C_MOVI_B.CDCLIENTE;

          ELSIF V_IDTIPOCOME = 'F' THEN
             V_CDCONTCOME := C_MOVI_B.CDFORNECED;

          ELSE
             V_CDCONTCOME := NULL;
          END IF;

          BEGIN
             SELECT COUNT(*)
               INTO V_COUNT
               FROM LANCCTBTEMP
              WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                AND CDEMPRESA     = V_CDEMPRESA
                AND CDCONTCTBL    = C_MOVI_B.CONTA_DEB;
          EXCEPTION
             WHEN NO_DATA_FOUND THEN
                V_COUNT := 0;
          END;

          IF V_COUNT = 0 THEN
             INSERT INTO LANCCTBTEMP
                (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                 DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
             VALUES
                (V_CDEMPRESA, V_NRLANCTEMP, C_MOVI_B.NRDOCMBANC, '00001', C_MOVI_B.CONTA_DEB, C_MOVI_B.CDHISTPADR,
                 V_DSHISTLANC, C_MOVI_B.DTMOVICRED, C_MOVI_B.VRMOVICRED * -1, V_IDTIPOCOME, V_CDCONTCOME, 'BCO');

          END IF;
          
          -- Realiza o rateio caso a conta contábil não seja "Só Oficial" --
          IF V_CONTRAT = 'S' THEN
             IF V_IDCONTCCUS <> 'O' THEN
             IF V_CDCENTCUSTOLD IS NULL THEN
                V_CDCENTCUST := BUSCA_CCUSTO(C_MOVI_B.CDFILIAL, V_CDEMPRESA);
             ELSE
               V_CDCENTCUST := V_CDCENTCUSTOLD;
             END IF;
                BEGIN
                   SELECT COUNT(*)
                     INTO V_COUNT
                     FROM RATECTBTEMP
                    WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                      AND CDEMPRESA     = V_CDEMPRESA
                      AND CDFILIAL      = C_MOVI_B.CDFILIAL
                      AND CDCENTCUST    = V_CDCENTCUST
                      AND NRSEQCTBTEMP  = '00001';
                EXCEPTION
                   WHEN NO_DATA_FOUND THEN
                      V_COUNT := 0;
                END;

                IF V_COUNT = 0 THEN
                   INSERT INTO RATECTBTEMP
                      (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                   VALUES
                      (V_CDEMPRESA, V_NRLANCTEMP, '00001', C_MOVI_B.CDFILIAL, V_CDCENTCUST, C_MOVI_B.VRMOVICRED * -1, C_MOVI_B.DTMOVICRED, '00001');
                END IF;
             END IF;
          END IF;

          /*************************************************************************************
                                PARTIDA DE CRÉDITO
           ************************************************************************************/
          V_IDTIPOCOME := NULL;
          V_IDCONTCCUS := NULL;
          V_VRRATEIO   := 0;
          V_COUNT      := 0;
          
          IF V_VRDEBITO = 0 THEN
             V_VRDEBITO := C_MOVI_B.VRMOVICRED;
          END IF;
            V_VRCREDITO  := C_MOVI_B.VRFINALBREC;
          
          
          -- Verifica se a con0ta contábil é do tipo comercial (banco, fornecedor ou cliente) --
          BUSCA_TPCOMERCIAL(C_MOVI_B.CONTA_CRE, V_IDTIPOCOME, V_IDCONTCCUS);

          IF V_IDTIPOCOME = 'B' THEN
             V_CDCONTCOME := C_MOVI_B.CDREDUCONT;

          ELSIF V_IDTIPOCOME = 'C' THEN
             V_CDCONTCOME := C_MOVI_B.CDCLIENTE;

          ELSIF V_IDTIPOCOME = 'F' THEN
             V_CDCONTCOME := C_MOVI_B.CDFORNECED;

          ELSE
             V_CDCONTCOME := NULL;
          END IF;

          BEGIN
             SELECT COUNT(*)
               INTO V_COUNT
               FROM LANCCTBTEMP
              WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                AND CDEMPRESA     = V_CDEMPRESA
                AND CDCONTCTBL    = C_MOVI_B.CONTA_CRE;
          EXCEPTION
             WHEN NO_DATA_FOUND THEN
                V_COUNT := 0;
          END;

          IF V_COUNT = 0 THEN
             INSERT INTO LANCCTBTEMP
                (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                 DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
             VALUES
                (V_CDEMPRESA, V_NRLANCTEMP, C_MOVI_B.NRDOCMBANC, '00002', C_MOVI_B.CONTA_CRE, C_MOVI_B.CDHISTPADR,
                 V_DSHISTLANC, C_MOVI_B.DTMOVICRED, V_VRCREDITO, V_IDTIPOCOME, V_CDCONTCOME, 'BCO');
          ELSE
             UPDATE LANCCTBTEMP
                SET VRLANCCTBTEMP = VRLANCCTBTEMP + V_VRCREDITO
              WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                AND CDEMPRESA     = V_CDEMPRESA
                AND CDCONTCTBL    = C_MOVI_B.CONTA_CRE;
          END IF;

          IF V_CONTRAT = 'S' THEN
             IF V_IDCONTCCUS <> 'O' THEN
                BEGIN
                   SELECT COUNT(*)
                     INTO V_COUNT
                     FROM RATTPCRED
                    WHERE CDTPCREDBANC = C_MOVI_B.CDTPCREDBANC;
                EXCEPTION
                   WHEN NO_DATA_FOUND THEN
                      V_COUNT := 0;
                END;

                 IF V_COUNT > 0 THEN
                    V_RATSEQ := 1;
                    FOR RAT IN(
                       SELECT CDCENTCUST, CDFILIAL, VRPERATTPCRE
                         FROM RATTPCRED
                        WHERE CDTPCREDBANC = C_MOVI_B.CDTPCREDBANC) LOOP

                       BEGIN
                          SELECT COUNT(*)
                            INTO V_COUNT
                            FROM RATECTBTEMP
                           WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                             AND CDEMPRESA     = V_CDEMPRESA
                             AND NRSEQCTBTEMP  = '00002'
                             AND CDFILIAL      = NVL(RAT.CDFILIAL, C_MOVI_B.CDFILIAL)
                             AND CDCENTCUST    = RAT.CDCENTCUST;
                       EXCEPTION
                          WHEN NO_DATA_FOUND THEN
                             V_COUNT := 0;
                       END;

                       IF V_COUNT = 0 THEN
                           INSERT INTO RATECTBTEMP
                              (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                           VALUES
                              (V_CDEMPRESA, V_NRLANCTEMP, '00002', NVL(RAT.CDFILIAL, C_MOVI_B.CDFILIAL), RAT.CDCENTCUST,
                               ROUND((V_VRCREDITO * RAT.VRPERATTPCRE)/100, 2), C_MOVI_B.DTMOVICRED, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                       ELSE
                          UPDATE RATECTBTEMP
                             SET VRRATECTBTEMP = VRRATECTBTEMP +  ROUND((V_VRCREDITO * RAT.VRPERATTPCRE)/100, 2)
                           WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                             AND CDEMPRESA     = V_CDEMPRESA
                             AND NRSEQCTBTEMP  = '00002'
                             AND CDFILIAL      = NVL(RAT.CDFILIAL, C_MOVI_B.CDFILIAL)
                             AND CDCENTCUST    = RAT.CDCENTCUST;
                       END IF;

                       V_VRRATEIO := V_VRRATEIO + ROUND((V_VRCREDITO * RAT.VRPERATTPCRE)/100, 2);

                       V_RATSEQ := V_RATSEQ + 1;

                       /* FILIAL E CENTRO DE CUSTO USADOS PARA ARREDONDAR ULTIMO RATEIO, CASO DÊ DIFERENÇA */
                       V_CDFILIAL   := NVL(RAT.CDFILIAL, C_MOVI_B.CDFILIAL);
                       V_CDCENTCUST := RAT.CDCENTCUST;
                    END LOOP;
                    V_RATSEQ := 1;
                    IF V_VRRATEIO <> V_VRCREDITO THEN
                       V_VRRATEIO := V_VRCREDITO - V_VRRATEIO;
                       UPDATE RATECTBTEMP
                          SET VRRATECTBTEMP = VRRATECTBTEMP + V_VRRATEIO
                        WHERE CDEMPRESA     = V_CDEMPRESA
                          AND NRLANCCTBTEMP = V_NRLANCTEMP
                          AND NRSEQCTBTEMP  = '00002'
                          AND CDFILIAL      = V_CDFILIAL
                          AND CDCENTCUST    = V_CDCENTCUST;
                    END IF;
                 ELSE
                 IF V_CDCENTCUSTOLD IS NULL THEN
                    V_CDCENTCUST := BUSCA_CCUSTO(C_MOVI_B.CDFILIAL, V_CDEMPRESA);
                 ELSE
                   V_CDCENTCUST := V_CDCENTCUSTOLD;
                 END IF;

                    BEGIN
                       SELECT COUNT(*), R.NRSEQCTBTEMP
                         INTO V_COUNT, V_SEQUENCIAL
                         FROM RATECTBTEMP R, LANCCTBTEMP L
                        WHERE R.CDEMPRESA     = V_CDEMPRESA
                          AND R.NRLANCCTBTEMP = V_NRLANCTEMP
                          AND CDFILIAL        = NVL(V_CDFILIAL, C_MOVI_B.CDFILIAL)
                          AND CDCENTCUST      = V_CDCENTCUST
                          AND L.NRLANCCTBTEMP = R.NRLANCCTBTEMP
                          AND L.CDEMPRESA     = R.CDEMPRESA
                          AND L.NRSEQCTBTEMP  = R.NRSEQCTBTEMP
                          AND L.CDCONTCTBL    = C_MOVI_B.CONTA_CRE
                        GROUP BY R.NRSEQCTBTEMP;
                    EXCEPTION
                       WHEN NO_DATA_FOUND THEN
                          V_COUNT := 0;
                          V_SEQUENCIAL := NULL;
                    END;

                    IF V_COUNT = 0 THEN
                       INSERT INTO RATECTBTEMP
                          (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                       VALUES
                          (V_CDEMPRESA, V_NRLANCTEMP, '00002', NVL(V_CDFILIAL, C_MOVI_B.CDFILIAL), V_CDCENTCUST, V_VRCREDITO, C_MOVI_B.DTMOVICRED, '00001');
                    ELSE
                       UPDATE RATECTBTEMP
                          SET VRRATECTBTEMP = VRRATECTBTEMP + V_VRCREDITO
                        WHERE CDEMPRESA     = V_CDEMPRESA
                          AND NRLANCCTBTEMP = V_NRLANCTEMP
                          AND CDFILIAL      = NVL(V_CDFILIAL, C_MOVI_B.CDFILIAL)
                          AND CDCENTCUST    = V_CDCENTCUST
                          AND NRSEQCTBTEMP  = V_SEQUENCIAL;
                    END IF;
                 END IF;
             END IF;
          END IF;
            IF V_VRDIF = 0 THEN
               V_VRDIF :=  (V_VRDEBITO-V_VRCREDITO);
            ELSE
               V_VRDIF := V_VRCREDITO*-1;
            END IF; 
            
            --RAISE_APPLICATION_ERROR(-20501,V_VRDIF, TRUE); 
            IF V_VRDIF <> 0 THEN 
                -- Verifica se a con0ta contábil é do tipo comercial (banco, fornecedor ou cliente) --
                BUSCA_TPCOMERCIAL(C_MOVI_B.CONTA_DESC, V_IDTIPOCOME, V_IDCONTCCUS);
      
                IF V_IDTIPOCOME = 'B' THEN
                   V_CDCONTCOME := C_MOVI_B.CDREDUCONT;
      
                ELSIF V_IDTIPOCOME = 'C' THEN
                   V_CDCONTCOME := C_MOVI_B.CDCLIENTE;
      
                ELSIF V_IDTIPOCOME = 'F' THEN
                   V_CDCONTCOME := C_MOVI_B.CDFORNECED;
      
                ELSE
                   V_CDCONTCOME := NULL;
                END IF;
      
                BEGIN
                   SELECT COUNT(*)
                     INTO V_COUNT
                     FROM LANCCTBTEMP
                    WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                      AND CDEMPRESA     = V_CDEMPRESA
                      AND CDCONTCTBL    = C_MOVI_B.CONTA_DESC;
                EXCEPTION
                   WHEN NO_DATA_FOUND THEN
                      V_COUNT := 0;
                END;
      
                IF V_COUNT = 0 THEN
                   INSERT INTO LANCCTBTEMP
                      (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                       DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
                   VALUES
                      (V_CDEMPRESA, V_NRLANCTEMP, C_MOVI_B.NRDOCMBANC, '00003', C_MOVI_B.CONTA_DESC, C_MOVI_B.CDHISTPADR,
                       V_DSHISTLANC, C_MOVI_B.DTMOVICRED, V_VRDIF, V_IDTIPOCOME, V_CDCONTCOME, 'BCO');
                ELSE
                   UPDATE LANCCTBTEMP
                      SET VRLANCCTBTEMP = VRLANCCTBTEMP + V_VRDIF
                    WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                      AND CDEMPRESA     = V_CDEMPRESA
                      AND CDCONTCTBL    = C_MOVI_B.CONTA_DESC;
                END IF;
      
                IF V_CONTRAT = 'S' THEN
                   IF V_IDCONTCCUS <> 'O' THEN
                      BEGIN
                         SELECT COUNT(*)
                           INTO V_COUNT
                           FROM RATTPCRED
                          WHERE CDTPCREDBANC = C_MOVI_B.CDTPCREDBANC;
                      EXCEPTION
                         WHEN NO_DATA_FOUND THEN
                            V_COUNT := 0;
                      END;
      
                       IF V_COUNT > 0 THEN
                          V_RATSEQ := 1;
                          FOR RAT IN(
                             SELECT CDCENTCUST, CDFILIAL, VRPERATTPCRE
                               FROM RATTPCRED
                              WHERE CDTPCREDBANC = C_MOVI_B.CDTPCREDBANC) LOOP
      
                             BEGIN
                                SELECT COUNT(*)
                                  INTO V_COUNT
                                  FROM RATECTBTEMP
                                 WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                                   AND CDEMPRESA     = V_CDEMPRESA
                                   AND NRSEQCTBTEMP  = '00003'
                                   AND CDFILIAL      = NVL(RAT.CDFILIAL, C_MOVI_B.CDFILIAL)
                                   AND CDCENTCUST    = RAT.CDCENTCUST;
                             EXCEPTION
                                WHEN NO_DATA_FOUND THEN
                                   V_COUNT := 0;
                             END;
      
                             IF V_COUNT = 0 THEN
                                 INSERT INTO RATECTBTEMP
                                    (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                 VALUES
                                    (V_CDEMPRESA, V_NRLANCTEMP, '00003', NVL(RAT.CDFILIAL, C_MOVI_B.CDFILIAL), RAT.CDCENTCUST,
                                     ROUND((V_VRDIF * RAT.VRPERATTPCRE)/100, 2), C_MOVI_B.DTMOVICRED, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                             ELSE
                                UPDATE RATECTBTEMP
                                   SET VRRATECTBTEMP = VRRATECTBTEMP +  ROUND((V_VRDIF * RAT.VRPERATTPCRE)/100, 2)
                                 WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                                   AND CDEMPRESA     = V_CDEMPRESA
                                   AND NRSEQCTBTEMP  = '00003'
                                   AND CDFILIAL      = NVL(RAT.CDFILIAL, C_MOVI_B.CDFILIAL)
                                   AND CDCENTCUST    = RAT.CDCENTCUST;
                             END IF;
      
                             V_VRRATEIO := V_VRRATEIO + ROUND((V_VRDIF * RAT.VRPERATTPCRE)/100, 2);
      
                             V_RATSEQ := V_RATSEQ + 1;
      
                             /* FILIAL E CENTRO DE CUSTO USADOS PARA ARREDONDAR ULTIMO RATEIO, CASO DÊ DIFERENÇA */
                             V_CDFILIAL   := NVL(RAT.CDFILIAL, C_MOVI_B.CDFILIAL);
                             V_CDCENTCUST := RAT.CDCENTCUST;
                          END LOOP;
                          V_RATSEQ := 1;
                          IF V_VRRATEIO <> V_VRDIF THEN
                             V_VRRATEIO := V_VRDIF - V_VRRATEIO;
                             UPDATE RATECTBTEMP
                                SET VRRATECTBTEMP = VRRATECTBTEMP + V_VRRATEIO
                              WHERE CDEMPRESA     = V_CDEMPRESA
                                AND NRLANCCTBTEMP = V_NRLANCTEMP
                                AND NRSEQCTBTEMP  = '00003'
                                AND CDFILIAL      = V_CDFILIAL
                                AND CDCENTCUST    = V_CDCENTCUST;
                          END IF;
                       ELSE
                       IF V_CDCENTCUSTOLD IS NULL THEN
                          V_CDCENTCUST := BUSCA_CCUSTO(C_MOVI_B.CDFILIAL, V_CDEMPRESA);
                       ELSE
                         V_CDCENTCUST := V_CDCENTCUSTOLD;
                       END IF;
      
                          BEGIN
                             SELECT COUNT(*), R.NRSEQCTBTEMP
                               INTO V_COUNT, V_SEQUENCIAL
                               FROM RATECTBTEMP R, LANCCTBTEMP L
                              WHERE R.CDEMPRESA     = V_CDEMPRESA
                                AND R.NRLANCCTBTEMP = V_NRLANCTEMP
                                AND CDFILIAL        = NVL(V_CDFILIAL, C_MOVI_B.CDFILIAL)
                                AND CDCENTCUST      = V_CDCENTCUST
                                AND L.NRLANCCTBTEMP = R.NRLANCCTBTEMP
                                AND L.CDEMPRESA     = R.CDEMPRESA
                                AND L.NRSEQCTBTEMP  = R.NRSEQCTBTEMP
                                AND L.CDCONTCTBL    = C_MOVI_B.CONTA_DESC
                              GROUP BY R.NRSEQCTBTEMP;
                          EXCEPTION
                             WHEN NO_DATA_FOUND THEN
                                V_COUNT := 0;
                                V_SEQUENCIAL := NULL;
                          END;
      
                          IF V_COUNT = 0 THEN
                             INSERT INTO RATECTBTEMP
                                (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                             VALUES
                                (V_CDEMPRESA, V_NRLANCTEMP, '00003', NVL(V_CDFILIAL, C_MOVI_B.CDFILIAL), V_CDCENTCUST, C_MOVI_B.VRMOVICRED, C_MOVI_B.DTMOVICRED, '00001');
                          ELSE
                             UPDATE RATECTBTEMP
                                SET VRRATECTBTEMP = VRRATECTBTEMP + C_MOVI_B.VRMOVICRED
                              WHERE CDEMPRESA     = V_CDEMPRESA
                                AND NRLANCCTBTEMP = V_NRLANCTEMP
                                AND CDFILIAL      = NVL(V_CDFILIAL, C_MOVI_B.CDFILIAL)
                                AND CDCENTCUST    = V_CDCENTCUST
                                AND NRSEQCTBTEMP  = V_SEQUENCIAL;
                          END IF;
                       END IF;
                   END IF;
                END IF;
            END IF;
         -- END IF
       END LOOP;

         /* TÉRMINO DAS PARTIDAS DO LANCAMENTO CONTABIL*/

         /*VERIFICACAO DE ERROS NAS PARTIDAS E NO LANCAMENTO CONTABIL*/
         VERIFICA_ERRO_CTB(V_NRLANCTEMP, V_CDEMPRESA);

         BEGIN
            SELECT COUNT(*)
              INTO V_COUNT
              FROM ERROCTBTEMP
             WHERE NRLANCCTBTEMP = V_NRLANCTEMP
               AND CDEMPRESA = V_CDEMPRESA;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_COUNT := 0;
         END;

         IF V_COUNT = 0 THEN
            /*SALVA NA TABELA DE MOVIMENTAÇÃO DE CRÉDITO BANCÁRIO, O NÚMERO DE LANÇAMENTO CONTÁBIL*/
            UPDATE MOVCREDBANC
               SET NRLCTBMCRED = V_NRLANCTEMP
             WHERE CDREDUCONT = P_CDREDUCONT
               AND NRMOVICRED = P_NRMOVI;
         END IF;

         /* GRAVA O LANÇAMENTO TEMPORÁRIO SE NÃO CONTER ERRO NA LANCCONT E LANCCUSTO, APÓS GRAVAR IRÁ APAGAR O LANCAMENTO TEMPORÁRIO */
         GRAVA_LANC_CTB(V_NRLANCTEMP, V_CDEMPRESA);
         P_NRLANCCTB := V_NRLANCTEMP;
   ELSE
      BEGIN
         SELECT DTMOVIDEBI, NRLCTBMDEB
           INTO V_DTLANCTBTEMP, V_NRLANCTEMP
           FROM MOVDEBBANC
          WHERE CDREDUCONT = P_CDREDUCONT
            AND NRMOVIDEBI = P_NRMOVI;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_DTLANCTBTEMP := NULL;
            V_NRLANCTEMP := NULL;
      END;

      BEGIN
         SELECT COUNT(*)
           INTO V_COUNT
          FROM PARAMCTBL
         WHERE V_DTLANCTBTEMP >= DTINTCTBBCO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_COUNT := 0;
      END;

      IF V_COUNT > 0 THEN
         IF V_NRLANCTEMP IS NOT NULL THEN
            DELETE
              FROM LANCCCUSTO
             WHERE NRLANCCONT = V_NRLANCTEMP
               AND CDEMPRESA = V_CDEMPRESA;

            DELETE
              FROM LANCCONT
             WHERE NRLANCCONT = V_NRLANCTEMP
               AND CDEMPRESA = V_CDEMPRESA;

            DELETE
              FROM RATECTBTEMP
             WHERE NRLANCCTBTEMP = V_NRLANCTEMP
               AND CDEMPRESA     = V_CDEMPRESA;

            DELETE
              FROM LANCCTBTEMP
             WHERE NRLANCCTBTEMP = V_NRLANCTEMP
               AND CDEMPRESA     = V_CDEMPRESA;
         END IF;

         IF V_NRLANCTEMP IS NULL THEN
            NOVO_CODIGO_NT('LANCCONTABIL' || V_CDEMPRESA || TO_CHAR(V_DTLANCTBTEMP,'YYYYMM'), V_NRLANCTEMP);
            V_NRLANCTEMP := TO_CHAR(V_DTLANCTBTEMP,'YYYYMM')||SUBSTR(V_NRLANCTEMP, 15, 6);
         END IF;

         FOR C_MOVI IN (
            SELECT M.CDREDUCONT, M.NRMOVIDEBI, T.CDHISTPADR, C.CDCONTCTBL CONTA_CRE, DTMOVIDEBI,
                   M.VRMOVIDEBI, M.CDCLIENTE, M.CDFORNECED, M.CDFILIAL, T.CDCONTCTBL CONTA_DEB,
                   M.CDTPDEBIBANC, B.NRDOCMBANC
              FROM MOVDEBBANC M, TPDEBIBANC T, CONTCORR C, MOVBANCARIO B
             WHERE M.CDTPDEBIBANC = T.CDTPDEBIBANC
               AND M.CDREDUCONT   = C.CDREDUCONT
               AND M.NRMOVBANC    = B.NRMOVBANC
               AND M.CDREDUCONT   = B.CDREDUCONT
               AND M.CDREDUCONT = P_CDREDUCONT
               AND M.NRMOVIDEBI = P_NRMOVI) LOOP

            -- Formata a descrição do histórico --
            V_DSHISTLANC := CTB_HIST_BCO(C_MOVI.CDREDUCONT, C_MOVI.NRMOVIDEBI, C_MOVI.CDHISTPADR, '', 'D');

            /*************************************************************************************
                                PARTIDA DE CRÉDITO - CONTA CONTÁBIL DA CONTA CORRENTE
             ************************************************************************************/
            -- Verifica se a conta contábil é do tipo comercial (banco, fornecedor ou cliente) --
            BUSCA_TPCOMERCIAL(C_MOVI.CONTA_CRE, V_IDTIPOCOME, V_IDCONTCCUS);

            IF V_IDTIPOCOME = 'B' THEN
               V_CDCONTCOME := C_MOVI.CDREDUCONT;

            ELSIF V_IDTIPOCOME = 'C' THEN
               V_CDCONTCOME := C_MOVI.CDCLIENTE;

            ELSIF V_IDTIPOCOME = 'F' THEN
               V_CDCONTCOME := C_MOVI.CDFORNECED;

            ELSE
               V_CDCONTCOME := NULL;
            END IF;

            BEGIN
               SELECT COUNT(*)
                 INTO V_COUNT
                 FROM LANCCTBTEMP
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    = C_MOVI.CONTA_CRE;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_COUNT := 0;
            END;

            IF V_COUNT = 0 THEN
               INSERT INTO LANCCTBTEMP
                  (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR, CDDOCUTEMP,
                  DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
               VALUES
                  (V_CDEMPRESA, V_NRLANCTEMP, '00001', C_MOVI.CONTA_CRE, C_MOVI.CDHISTPADR, C_MOVI.NRDOCMBANC,
                  V_DSHISTLANC, C_MOVI.DTMOVIDEBI, C_MOVI.VRMOVIDEBI, V_IDTIPOCOME, V_CDCONTCOME, 'BCO');
            ELSE
               UPDATE LANCCTBTEMP
                  SET VRLANCCTBTEMP = VRLANCCTBTEMP + C_MOVI.VRMOVIDEBI
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    = C_MOVI.CONTA_CRE;
            END IF;


            -- Realiza o rateio caso a conta contábil não seja "Só Oficial" --
            IF V_CONTRAT = 'S' THEN
               IF V_IDCONTCCUS <> 'O' THEN

               IF V_CDCENTCUSTOLD IS NULL THEN
                  V_CDCENTCUST := BUSCA_CCUSTO(C_MOVI.CDFILIAL, V_CDEMPRESA);
               ELSE
                 V_CDCENTCUST := V_CDCENTCUSTOLD;
               END IF;

                  BEGIN
                     SELECT COUNT(*)
                       INTO V_COUNT
                       FROM RATECTBTEMP
                      WHERE CDEMPRESA     = V_CDEMPRESA
                        AND NRLANCCTBTEMP = V_NRLANCTEMP
                        AND CDFILIAL      = C_MOVI.CDFILIAL
                        AND CDCENTCUST    = V_CDCENTCUST
                        AND NRSEQCTBTEMP  = '00001';
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_COUNT := 0;
                  END;

                  IF V_COUNT = 0 THEN
                     INSERT INTO RATECTBTEMP
                        (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                     VALUES
                        (V_CDEMPRESA, V_NRLANCTEMP, '00001',  C_MOVI.CDFILIAL, V_CDCENTCUST, C_MOVI.VRMOVIDEBI, C_MOVI.DTMOVIDEBI, '00001');
                  ELSE
                     UPDATE RATECTBTEMP
                        SET VRRATECTBTEMP = VRRATECTBTEMP + C_MOVI.VRMOVIDEBI
                      WHERE CDEMPRESA     = V_CDEMPRESA
                        AND NRLANCCTBTEMP = V_NRLANCTEMP
                        AND CDFILIAL      = C_MOVI.CDFILIAL
                        AND CDCENTCUST    = V_CDCENTCUST
                        AND NRSEQCTBTEMP  = '00001';
                  END IF;
               END IF;
            END IF;


            /*************************************************************************************
                                  PARTIDA DE DÉBITO - CONTA CONTÁBIL TIPO DE DÉBITO
             ************************************************************************************/
            V_IDTIPOCOME := NULL;
            V_IDCONTCCUS := NULL;
            V_VRRATEIO   := 0;
            V_COUNT      := 0;


            -- Verifica se a conta contábil é do tipo comercial (banco, fornecedor ou cliente) --
            BUSCA_TPCOMERCIAL(C_MOVI.CONTA_DEB, V_IDTIPOCOME, V_IDCONTCCUS);

            IF V_IDTIPOCOME = 'B' THEN
               V_CDCONTCOME := C_MOVI.CDREDUCONT;

            ELSIF V_IDTIPOCOME = 'C' THEN
               V_CDCONTCOME := C_MOVI.CDCLIENTE;

            ELSIF V_IDTIPOCOME = 'F' THEN
               V_CDCONTCOME := C_MOVI.CDFORNECED;

            ELSE
               V_CDCONTCOME := NULL;
            END IF;

            BEGIN
               SELECT COUNT(*)
                 INTO V_COUNT
                 FROM LANCCTBTEMP
                WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                  AND CDEMPRESA  = V_CDEMPRESA
                  AND CDCONTCTBL = C_MOVI.CONTA_DEB;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_COUNT := 0;
            END;

            IF V_COUNT = 0 THEN
               INSERT INTO LANCCTBTEMP
                  (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR, CDDOCUTEMP,
                   DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
               VALUES
                  (V_CDEMPRESA, V_NRLANCTEMP, '00002', C_MOVI.CONTA_DEB, C_MOVI.CDHISTPADR, C_MOVI.NRDOCMBANC,
                   V_DSHISTLANC, C_MOVI.DTMOVIDEBI, C_MOVI.VRMOVIDEBI * -1, V_IDTIPOCOME, V_CDCONTCOME, 'BCO');
            ELSE
               UPDATE LANCCTBTEMP
                  SET VRLANCCTBTEMP = VRLANCCTBTEMP + (C_MOVI.VRMOVIDEBI * -1)
                WHERE NRLANCCTBTEMP  = V_NRLANCTEMP
                  AND CDEMPRESA     = V_CDEMPRESA
                  AND CDCONTCTBL    = C_MOVI.CONTA_DEB;
            END IF;

            IF V_CONTRAT = 'S' THEN
               IF V_IDCONTCCUS <> 'O' THEN
                  BEGIN
                     SELECT COUNT(*)
                       INTO V_COUNT
                       FROM RATTPDEBI
                      WHERE CDTPDEBIBANC = C_MOVI.CDTPDEBIBANC;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_COUNT := 0;
                  END;

                   IF V_COUNT > 0 THEN
                      V_RATSEQ := 1;
                      FOR RAT IN(
                         SELECT CDCENTCUST, CDFILIAL,  VRPERATTPDEB
                           FROM RATTPDEBI
                          WHERE CDTPDEBIBANC = C_MOVI.CDTPDEBIBANC) LOOP

                         BEGIN
                            SELECT COUNT(*)
                              INTO V_COUNT
                              FROM RATECTBTEMP  R
                             WHERE CDFILIAL     = NVL(RAT.CDFILIAL, C_MOVI.CDFILIAL)
                               AND CDCENTCUST   = RAT.CDCENTCUST
                               AND R.CDEMPRESA  = V_CDEMPRESA
                               AND R.NRLANCCTBTEMP = V_NRLANCTEMP
                               AND R.NRSEQCTBTEMP  = '00002';
                         EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                               V_COUNT := 0;
                         END;

                         IF V_COUNT = 0 THEN
                            INSERT INTO RATECTBTEMP
                               (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                            VALUES
                               (V_CDEMPRESA, V_NRLANCTEMP, '00002', NVL(RAT.CDFILIAL, C_MOVI.CDFILIAL), RAT.CDCENTCUST,
                                ROUND(((C_MOVI.VRMOVIDEBI * RAT.VRPERATTPDEB)/100) * -1, 2), C_MOVI.DTMOVIDEBI, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                         ELSE
                            UPDATE RATECTBTEMP R
                               SET VRRATECTBTEMP = VRRATECTBTEMP +  ROUND(((C_MOVI.VRMOVIDEBI * RAT.VRPERATTPDEB)/100) * -1, 2)
                             WHERE CDFILIAL     = NVL(RAT.CDFILIAL, C_MOVI.CDFILIAL)
                               AND CDCENTCUST   = RAT.CDCENTCUST
                               AND R.CDEMPRESA  = V_CDEMPRESA
                               AND R.NRLANCCTBTEMP = V_NRLANCTEMP
                               AND R.NRSEQCTBTEMP = '00002';
                         END IF;


                         V_VRRATEIO := V_VRRATEIO + ROUND((C_MOVI.VRMOVIDEBI * RAT.VRPERATTPDEB)/100, 2);
                         V_RATSEQ := V_RATSEQ + 1;

                         /* FILIAL E CENTRO DE CUSTO USADOS PARA ARREDONDAR ULTIMO RATEIO, CASO DÊ DIFERENÇA */
                         V_CDFILIAL   := NVL(RAT.CDFILIAL, C_MOVI.CDFILIAL);
                         V_CDCENTCUST := RAT.CDCENTCUST;
                      END LOOP;
                      V_RATSEQ := 1;
                      IF V_VRRATEIO <> C_MOVI.VRMOVIDEBI THEN
                         IF V_VRRATEIO < C_MOVI.VRMOVIDEBI THEN
                            V_VRRATEIO := (V_VRRATEIO - C_MOVI.VRMOVIDEBI);
                         ELSE
                            V_VRRATEIO := - (V_VRRATEIO - C_MOVI.VRMOVIDEBI);
                         END IF;
                         UPDATE RATECTBTEMP
                            SET VRRATECTBTEMP = VRRATECTBTEMP + V_VRRATEIO
                          WHERE CDEMPRESA     = V_CDEMPRESA
                            AND NRLANCCTBTEMP = V_NRLANCTEMP
                            AND NRSEQCTBTEMP  = '00002'
                            AND CDFILIAL      = V_CDFILIAL
                            AND CDCENTCUST    = V_CDCENTCUST;
                      END IF;
                   ELSE
                      V_CDCENTCUST := BUSCA_CCUSTO(C_MOVI.CDFILIAL, V_CDEMPRESA);
                      V_RATSEQ := 1;

                      BEGIN
                         SELECT COUNT(*), L.NRSEQCTBTEMP
                           INTO V_COUNT, V_SEQUENCIAL
                           FROM RATECTBTEMP  R, LANCCTBTEMP L
                          WHERE CDFILIAL     = NVL(V_CDFILIAL, C_MOVI.CDFILIAL)
                            AND CDCENTCUST   = V_CDCENTCUST
                            AND R.CDEMPRESA  = V_CDEMPRESA
                            AND R.NRLANCCTBTEMP = V_NRLANCTEMP
                            AND R.CDEMPRESA     = L.CDEMPRESA
                            AND R.NRLANCCTBTEMP = L.CDEMPRESA
                            AND L.CDCONTCTBL    = C_MOVI.CONTA_CRE
                          GROUP BY L.NRSEQCTBTEMP;
                      EXCEPTION
                         WHEN NO_DATA_FOUND THEN
                            V_COUNT := 0;
                            V_SEQUENCIAL := NULL;
                      END;

                      IF V_COUNT = 0 THEN
                         INSERT INTO RATECTBTEMP
                            (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                         VALUES
                            (V_CDEMPRESA, V_NRLANCTEMP, '00002', NVL(V_CDFILIAL, C_MOVI.CDFILIAL), V_CDCENTCUST,
                             C_MOVI.VRMOVIDEBI * -1, C_MOVI.DTMOVIDEBI, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                      ELSE
                         UPDATE RATECTBTEMP
                            SET VRRATECTBTEMP = VRRATECTBTEMP +  ROUND(C_MOVI.VRMOVIDEBI * -1, 2)
                          WHERE CDFILIAL      = NVL(V_CDFILIAL, C_MOVI.CDFILIAL)
                            AND CDCENTCUST    = V_CDCENTCUST
                            AND CDEMPRESA     = V_CDEMPRESA
                            AND NRLANCCTBTEMP = V_NRLANCTEMP
                            AND NRSEQCTBTEMP  = V_SEQUENCIAL;
                      END IF;
                   END IF;
               END IF;
            END IF;
         END LOOP;
         /* TÉRMINO DAS PARTIDAS DO LANCAMENTO CONTABIL*/

         /*VERIFICACAO DE ERROS NAS PARTIDAS E NO LANCAMENTO CONTABIL*/
         VERIFICA_ERRO_CTB(V_NRLANCTEMP, V_CDEMPRESA);

         BEGIN
            SELECT COUNT(*)
              INTO V_COUNT
              FROM ERROCTBTEMP
             WHERE NRLANCCTBTEMP = V_NRLANCTEMP
               AND CDEMPRESA = V_CDEMPRESA;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_COUNT := 0;
         END;

         IF V_COUNT = 0 THEN
            UPDATE MOVDEBBANC
               SET NRLCTBMDEB = V_NRLANCTEMP
             WHERE CDREDUCONT = P_CDREDUCONT
               AND NRMOVIDEBI = P_NRMOVI;
         END IF;
         /* GRAVA O LANÇAMENTO TEMPORÁRIO SE NÃO CONTER ERRO NA LANCCONT E LANCCUSTO, APÓS GRAVAR IRÁ APAGAR O LANCAMENTO TEMPORÁRIO */
         GRAVA_LANC_CTB(V_NRLANCTEMP, V_CDEMPRESA);
         P_NRLANCCTB := V_NRLANCTEMP;
      END IF;
   END IF;
END;  