CREATE OR REPLACE PROCEDURE FECHA_CONTABILIDADE(
    P_CDEMPRESA       IN  OPERACAOBEM.CDEMPRESA%TYPE,
    P_NRSEQFECHABEM   IN  FECHAMENTOBEM.NRSEQFECHABEM%TYPE,
    P_NRSEQCAPAOPEBEM IN  CAPAOPERABEM.NRSEQCAPAOPEBEM%TYPE,
    P_IDSIMULACAO     IN  OPERACAOBEM.IDSIMULOPERACAO%TYPE,
    P_CDOPERADOR      IN  OPERADOR.CDOPERADOR%TYPE,
    P_CARGAFECHA      IN  OPERACAOBEM.IDSIMULOPERACAO%TYPE,
    P_IDOPERACAO      IN  VARCHAR2,
    P_RETORNO         OUT VARCHAR2
    )
AS
   V_CDCONTCTBLBEM CTBLBEMATIVO.CDCONTCTBLBEM%TYPE;
   V_VALORCTBL        NUMBER;
   V_NRSEQCTBLITCAPA  NUMBER;
   V_HISTPADRAO       VARCHAR2(4);
   V_EXISTE_OPER      VARCHAR2(1);
   V_EXISTE_CTBL      INTEGER;
   V_DTINICLOTE       DATE;
   V_DTFINALOTE       DATE;
   V_CDLOTE           CAPALOTE.CDLOTE%TYPE;
   V_NRLANCLOTE       VARCHAR2(30);
   V_NRLANCL          LANCLOTE.NRLANCLOTE%TYPE;
   V_NRSEQLANLOTE     NUMBER;
   V_LANCLOTE         NUMBER;
   V_CDEMPRESA        VARCHAR2(2);
   V_CDBEMATIVO       VARCHAR2(10);
   V_CDGRUPOBEM       VARCHAR2(6);
   V_VRINIDEP         FLOAT;
   V_VRJADEP          FLOAT;
   V_VRFALDEP         FLOAT;
   V_VROPERA          FLOAT;
   V_NRSEQOPERABEM    VARCHAR2(10);
   V_CDTIPOOPERBEM    VARCHAR2(16);
   V_DTOPERA          DATE;
   V_VRAQUIBEM        FLOAT;
   V_NRSEQCAPAOPEBEM  VARCHAR2(10);
   V_NRSEQITCAPAOPE   VARCHAR2(10);
   V_IDSIMULOPERABEM  VARCHAR2(1);
   V_CDCONTCTBL       VARCHAR2(20);
   V_IDTIPOOPERCONT   VARCHAR2(1);
   V_DTINCCTBLITC     DATE;
   V_VRCTBL           FLOAT;
   V_CONTADOR         VARCHAR(20);
   V_IDCONTCCUS       VARCHAR2(1);
   V_CDFILIAL         VARCHAR2(4);
   V_CDCENTCUST       VARCHAR2(4);
   V_DSHISTPADR       VARCHAR2(1000);
   V_TPOPER           VARCHAR2(4);
   V_EXISRATE         BOOLEAN;
   V_ROWSAFFECT       VARCHAR2(1);
   V_VALORCTBLRATE    FLOAT;
   V_IDAGRUPCONTATIV  VARCHAR2(2);
   V_NMFORNECED       VARCHAR2(1000);
   V_NMBEM            VARCHAR2(1000);
   V_CDPATRIBEM       VARCHAR2(30);
   V_CDBEM            VARCHAR2(10);
   V_NRNOTAFISC       VARCHAR2(1000);
   V_CDFILIANT        VARCHAR2(4);
   V_TIPOOPERIMOB     VARCHAR2(16);
   V_EXISTE_IMOB      INTEGER;
   V_TIPOOPERDEPR     VARCHAR2(16);
   V_EXISTE_DEPR      INTEGER;
   V_NRSEQFECHACO     VARCHAR2(10);
   V_NRSEQOPERACO     VARCHAR2(10);
   V_NRSEQOPERABEMCO  VARCHAR2(10);
   V_NRSEQFECHATC     VARCHAR2(10);
   V_NRSEQCTBLIT      VARCHAR2(3);
   V_NRSEQCTBLITRAT   VARCHAR2(3);
   V_VRCTBLTC         FLOAT;
   V_NRNOTAFIOPERA    VARCHAR2(10);
   V_NMCOMPRZSCLI     VARCHAR2(1000);
   V_NRINSESTCLIE     VARCHAR2(25);
   V_CDFORNECED       VARCHAR(21);
   V_TPAPROP          VARCHAR2(02);
   V_VROPERPIS        FLOAT;
   V_VROPERCOF        FLOAT;
   V_VRCTBLDEPR       FLOAT;
   V_CDCONTPIS        VARCHAR2(20);
   V_CDCONTCOF        VARCHAR2(20);
   V_IDORIGNOTA       VARCHAR2(1);
   V_COUNTIMP         NUMBER;
   CURSOR C_OPERFECHA IS
      SELECT OPERACAOBEM.CDEMPRESA,         OPERACAOBEM.CDBEMATIVO,
             OPERACAOBEM.CDGRUPOBEM,        OPERACAOBEM.VRINIDEPROPEBEM,
             OPERACAOBEM.VRJADEPROPEBEM,    OPERACAOBEM.VRFALDEPROPEBEM+NVL(BEMATIVO.VRAQUIBEMATIVO-OPERACAOBEM.VRINIDEPROPEBEM,0),
             OPERACAOBEM.VROPERABEM,        OPERACAOBEM.NRSEQOPERABEM,
             OPERACAOBEM.CDTIPOOPERBEM,     OPERACAOBEM.DTOPERABEM,
             BEMATIVO.VRAQUIBEMATIVO,       ITCAPAOPERABEM.NRSEQCAPAOPEBEM,
             ITCAPAOPERABEM.NRSEQITCAPAOPE, ITCAPAOPERABEM.IDSIMULOPERABEM, BEMATIVO.CDFORNECED,
             BEMATIVO.IDTPAPROPPISCOF, DECODE(BEMATIVO.NRLANCTONF, NULL, 'N', 'S') IDORIGNOTA
        FROM OPERACAOBEM, BEMATIVO, ITCAPAOPERABEM, CAPAOPERABEM
       WHERE OPERACAOBEM.CDEMPRESA        = P_CDEMPRESA
         AND OPERACAOBEM.IDSITOPERABEM    = '1'
         AND OPERACAOBEM.IDSIMULOPERACAO  = P_IDSIMULACAO
         AND OPERACAOBEM.CDEMPRESA        = BEMATIVO.CDEMPRESA
         AND OPERACAOBEM.CDBEMATIVO       = BEMATIVO.CDBEMATIVO
         AND OPERACAOBEM.CDEMPRESA        = ITCAPAOPERABEM.CDEMPRESA
         AND OPERACAOBEM.CDBEMATIVO       = ITCAPAOPERABEM.CDBEMATIVO
         AND OPERACAOBEM.IDSIMULOPERACAO  = ITCAPAOPERABEM.IDSIMULOPERABEM
         AND OPERACAOBEM.NRSEQOPERABEM    = ITCAPAOPERABEM.NRSEQOPERABEM
         AND OPERACAOBEM.CDEMPRESA        = CAPAOPERABEM.CDEMPRESA
         AND P_NRSEQFECHABEM              = CAPAOPERABEM.NRSEQFECHABEM
         AND CAPAOPERABEM.CDEMPRESA       = ITCAPAOPERABEM.CDEMPRESA
         AND CAPAOPERABEM.NRSEQCAPAOPEBEM = ITCAPAOPERABEM.NRSEQCAPAOPEBEM
         AND CAPAOPERABEM.IDSIMULOPERABEM = ITCAPAOPERABEM.IDSIMULOPERABEM
       ORDER BY OPERACAOBEM.CDGRUPOBEM;
   CURSOR C_CONTAGRUP IS
      SELECT CTBLITCAPAOPER.CDCONTCTBL, CTBLITCAPAOPER.IDTIPOOPERCONT, CTBLITCAPAOPER.DTINCCTBLITC,
             CTBLITCAPAOPER.NRSEQCAPAOPEBEM, CTBLITCAPAOPER.CDEMPRESA,
             sum(CTBLITCAPAOPER.VRCONTCTBLITC) VRCTBL, ITCAPAOPERABEM.IDSIMULOPERABEM,
             ITCAPAOPERABEM.NRSEQOPERABEM, CTBLITCAPAOPER.CDHISTPADR, HISTPADR.DSHISTPADR, CTBLITCAPAOPER.CDFILIAL, CTBLITCAPAOPER.CDCENTCUST, B.CDFORNECED
             FROM CTBLITCAPAOPER, ITCAPAOPERABEM,HISTPADR, BEMATIVO B
             WHERE EXISTS (SELECT CAPAOPERABEM.CDEMPRESA,ITCAPAOPERABEM.IDSIMULOPERABEM,
                                  CAPAOPERABEM.NRSEQCAPAOPEBEM, ITCAPAOPERABEM.NRSEQITCAPAOPE
                             FROM CAPAOPERABEM, ITCAPAOPERABEM
                            WHERE P_CDEMPRESA                    = CAPAOPERABEM.CDEMPRESA
                              AND P_NRSEQFECHABEM                = CAPAOPERABEM.NRSEQFECHABEM
                              AND CAPAOPERABEM.CDEMPRESA         = ITCAPAOPERABEM.CDEMPRESA
                              AND CAPAOPERABEM.NRSEQCAPAOPEBEM   = ITCAPAOPERABEM.NRSEQCAPAOPEBEM
                              AND ITCAPAOPERABEM.CDEMPRESA       = CTBLITCAPAOPER.CDEMPRESA
                              AND ITCAPAOPERABEM.NRSEQCAPAOPEBEM = CTBLITCAPAOPER.NRSEQCAPAOPEBEM
                              AND ITCAPAOPERABEM.NRSEQITCAPAOPE  = CTBLITCAPAOPER.NRSEQITCAPAOPE)
               AND ITCAPAOPERABEM.CDEMPRESA       = CTBLITCAPAOPER.CDEMPRESA
               AND ITCAPAOPERABEM.IDSIMULOPERABEM = CTBLITCAPAOPER.IDSIMULOPERABEM
               AND ITCAPAOPERABEM.NRSEQCAPAOPEBEM = CTBLITCAPAOPER.NRSEQCAPAOPEBEM
               AND ITCAPAOPERABEM.NRSEQITCAPAOPE  = CTBLITCAPAOPER.NRSEQITCAPAOPE
               AND CTBLITCAPAOPER.CDHISTPADR      = HISTPADR.CDHISTPADR
               AND ITCAPAOPERABEM.CDEMPRESA       = B.CDEMPRESA
               AND ITCAPAOPERABEM.CDBEMATIVO      = B.CDBEMATIVO
             GROUP BY CDCONTCTBL, IDTIPOOPERCONT, DTINCCTBLITC, CTBLITCAPAOPER.NRSEQCAPAOPEBEM, CTBLITCAPAOPER.CDEMPRESA,
                      ITCAPAOPERABEM.IDSIMULOPERABEM,ITCAPAOPERABEM.NRSEQOPERABEM,CTBLITCAPAOPER.CDHISTPADR,HISTPADR.DSHISTPADR,CTBLITCAPAOPER.CDFILIAL, CTBLITCAPAOPER.CDCENTCUST, B.CDFORNECED
             ORDER BY CDCONTCTBL,IDTIPOOPERCONT DESC;
   CURSOR C_OPERTRANS IS
      SELECT OPERACAOBEM.CDEMPRESA,         OPERACAOBEM.CDBEMATIVO,
             OPERACAOBEM.CDGRUPOBEM,        OPERACAOBEM.VRINIDEPROPEBEM,
             OPERACAOBEM.VRJADEPROPEBEM,
             OPERACAOBEM.VRFALDEPROPEBEM+NVL(BEMATIVO.VRAQUIBEMATIVO-OPERACAOBEM.VRINIDEPROPEBEM,0),
             OPERACAOBEM.VROPERABEM,        OPERACAOBEM.NRSEQOPERABEM,
             OPERACAOBEM.CDTIPOOPERBEM,     OPERACAOBEM.DTOPERABEM,
             BEMATIVO.VRAQUIBEMATIVO,       ITCAPAOPERABEM.NRSEQCAPAOPEBEM,
             ITCAPAOPERABEM.NRSEQITCAPAOPE, ITCAPAOPERABEM.IDSIMULOPERABEM, BEMATIVO.CDFORNECED
        FROM OPERACAOBEM, BEMATIVO, ITCAPAOPERABEM, CAPAOPERABEM
       WHERE OPERACAOBEM.CDEMPRESA        = P_CDEMPRESA
         AND OPERACAOBEM.IDSITOPERABEM    = '1'
         AND OPERACAOBEM.IDSIMULOPERACAO  = P_IDSIMULACAO
         AND CAPAOPERABEM.NRSEQCAPAOPEBEM = P_NRSEQCAPAOPEBEM
         AND OPERACAOBEM.CDEMPRESA        = BEMATIVO.CDEMPRESA
         AND OPERACAOBEM.CDBEMATIVO       = BEMATIVO.CDBEMATIVO
         AND OPERACAOBEM.CDEMPRESA        = ITCAPAOPERABEM.CDEMPRESA
         AND OPERACAOBEM.CDBEMATIVO       = ITCAPAOPERABEM.CDBEMATIVO
         AND OPERACAOBEM.IDSIMULOPERACAO  = ITCAPAOPERABEM.IDSIMULOPERABEM
         AND OPERACAOBEM.NRSEQOPERABEM    = ITCAPAOPERABEM.NRSEQOPERABEM
         AND OPERACAOBEM.CDEMPRESA        = CAPAOPERABEM.CDEMPRESA
         AND CAPAOPERABEM.CDEMPRESA       = ITCAPAOPERABEM.CDEMPRESA
         AND CAPAOPERABEM.NRSEQCAPAOPEBEM = ITCAPAOPERABEM.NRSEQCAPAOPEBEM
         AND CAPAOPERABEM.IDSIMULOPERABEM = ITCAPAOPERABEM.IDSIMULOPERABEM
       ORDER BY OPERACAOBEM.CDGRUPOBEM;
   CURSOR C_CONTFECHA IS
     SELECT CTBLITCAPAOPER.CDCONTCTBL, CTBLITCAPAOPER.IDTIPOOPERCONT, CTBLITCAPAOPER.DTINCCTBLITC,
             CTBLITCAPAOPER.NRSEQCAPAOPEBEM, CTBLITCAPAOPER.CDEMPRESA,
             SUM(CTBLITCAPAOPER.VRCONTCTBLITC) VRCTBL, ITCAPAOPERABEM.CDBEMATIVO, ITCAPAOPERABEM.IDSIMULOPERABEM,
             ITCAPAOPERABEM.NRSEQOPERABEM, CTBLITCAPAOPER.CDHISTPADR, HISTPADR.DSHISTPADR, CTBLITCAPAOPER.CDFILIAL, CTBLITCAPAOPER.CDCENTCUST,FORNECEDOR.NMRAZSOCFORN,BEMATIVO.NMBEMATIVO,BEMATIVO.CDPATRIBEMATIVO,BEMATIVO.CDBEMATIVO,BEMATIVO.NRNOTAFISCBEM, BEMATIVO.CDFORNECED
        FROM CTBLITCAPAOPER, ITCAPAOPERABEM,HISTPADR,BEMATIVO,FORNECEDOR
       WHERE EXISTS (SELECT CAPAOPERABEM.CDEMPRESA,       ITCAPAOPERABEM.IDSIMULOPERABEM,
                            CAPAOPERABEM.NRSEQCAPAOPEBEM, ITCAPAOPERABEM.NRSEQITCAPAOPE
                       FROM CAPAOPERABEM, ITCAPAOPERABEM
                      WHERE P_CDEMPRESA                    = CAPAOPERABEM.CDEMPRESA
                        AND P_NRSEQFECHABEM                = CAPAOPERABEM.NRSEQFECHABEM
                        AND CAPAOPERABEM.CDEMPRESA         = ITCAPAOPERABEM.CDEMPRESA
                        AND CAPAOPERABEM.NRSEQCAPAOPEBEM   = ITCAPAOPERABEM.NRSEQCAPAOPEBEM
                        AND ITCAPAOPERABEM.CDEMPRESA       = CTBLITCAPAOPER.CDEMPRESA
                        AND ITCAPAOPERABEM.NRSEQCAPAOPEBEM = CTBLITCAPAOPER.NRSEQCAPAOPEBEM
                        AND ITCAPAOPERABEM.NRSEQITCAPAOPE  = CTBLITCAPAOPER.NRSEQITCAPAOPE)
         AND ITCAPAOPERABEM.CDEMPRESA       = CTBLITCAPAOPER.CDEMPRESA
         AND ITCAPAOPERABEM.IDSIMULOPERABEM = CTBLITCAPAOPER.IDSIMULOPERABEM
         AND ITCAPAOPERABEM.NRSEQCAPAOPEBEM = CTBLITCAPAOPER.NRSEQCAPAOPEBEM
         AND ITCAPAOPERABEM.NRSEQITCAPAOPE  = CTBLITCAPAOPER.NRSEQITCAPAOPE
         AND CTBLITCAPAOPER.CDHISTPADR      = HISTPADR.CDHISTPADR
         AND ITCAPAOPERABEM.CDBEMATIVO      = BEMATIVO.CDBEMATIVO
         AND BEMATIVO.CDFORNECED          = FORNECEDOR.CDFORNECED(+)
       GROUP BY CTBLITCAPAOPER.CDCONTCTBL, IDTIPOOPERCONT, DTINCCTBLITC, CTBLITCAPAOPER.NRSEQCAPAOPEBEM,
                CTBLITCAPAOPER.CDEMPRESA, ITCAPAOPERABEM.CDBEMATIVO, ITCAPAOPERABEM.IDSIMULOPERABEM,
                ITCAPAOPERABEM.NRSEQOPERABEM, CTBLITCAPAOPER.CDHISTPADR,HISTPADR.DSHISTPADR, CTBLITCAPAOPER.CDFILIAL, CTBLITCAPAOPER.CDCENTCUST,FORNECEDOR.NMRAZSOCFORN,BEMATIVO.NMBEMATIVO,BEMATIVO.CDPATRIBEMATIVO,BEMATIVO.CDBEMATIVO,BEMATIVO.NRNOTAFISCBEM, BEMATIVO.CDFORNECED
       ORDER BY ITCAPAOPERABEM.CDBEMATIVO,CDCONTCTBL,IDTIPOOPERCONT DESC;
   CURSOR C_CONTTRANS IS
      SELECT CTBLITCAPAOPER.CDCONTCTBL, CTBLITCAPAOPER.IDTIPOOPERCONT, CTBLITCAPAOPER.DTINCCTBLITC,
             CTBLITCAPAOPER.NRSEQCAPAOPEBEM, CTBLITCAPAOPER.CDEMPRESA,
             SUM(CTBLITCAPAOPER.VRCONTCTBLITC) VRCTBL, ITCAPAOPERABEM.CDBEMATIVO, ITCAPAOPERABEM.IDSIMULOPERABEM,
             ITCAPAOPERABEM.NRSEQOPERABEM, CTBLITCAPAOPER.CDFILIAL,CTBLITCAPAOPER.CDCENTCUST,CTBLITCAPAOPER.CDHISTPADR,HISTPADR.DSHISTPADR,
             BEMATIVO.CDPATRIBEMATIVO,BEMATIVO.NMBEMATIVO,ITCAPAOPERABEM.NRNOTAFIITCAPA,CLIENTE.NMRAZSOCCLIE,CLIENTE.NRINSESTCLIE, BEMATIVO.CDFORNECED
        FROM CTBLITCAPAOPER, ITCAPAOPERABEM,HISTPADR,BEMATIVO, CLIENTE
       WHERE EXISTS (SELECT CAPAOPERABEM.CDEMPRESA,       ITCAPAOPERABEM.IDSIMULOPERABEM,
                            CAPAOPERABEM.NRSEQCAPAOPEBEM, ITCAPAOPERABEM.NRSEQITCAPAOPE
                       FROM CAPAOPERABEM, ITCAPAOPERABEM
                      WHERE P_CDEMPRESA                    = CAPAOPERABEM.CDEMPRESA
                        AND P_NRSEQCAPAOPEBEM              = CAPAOPERABEM.NRSEQCAPAOPEBEM
                        AND CAPAOPERABEM.CDEMPRESA         = ITCAPAOPERABEM.CDEMPRESA
                        AND CAPAOPERABEM.NRSEQCAPAOPEBEM   = ITCAPAOPERABEM.NRSEQCAPAOPEBEM
                        AND ITCAPAOPERABEM.CDEMPRESA       = CTBLITCAPAOPER.CDEMPRESA
                        AND ITCAPAOPERABEM.NRSEQCAPAOPEBEM = CTBLITCAPAOPER.NRSEQCAPAOPEBEM
                        AND ITCAPAOPERABEM.NRSEQITCAPAOPE  = CTBLITCAPAOPER.NRSEQITCAPAOPE)
         AND ITCAPAOPERABEM.CDCLIENTE          = CLIENTE.CDCLIENTE(+)
         AND ITCAPAOPERABEM.CDEMPRESA       = CTBLITCAPAOPER.CDEMPRESA
         AND ITCAPAOPERABEM.IDSIMULOPERABEM = CTBLITCAPAOPER.IDSIMULOPERABEM
         AND ITCAPAOPERABEM.NRSEQCAPAOPEBEM = CTBLITCAPAOPER.NRSEQCAPAOPEBEM
         AND ITCAPAOPERABEM.NRSEQITCAPAOPE  = CTBLITCAPAOPER.NRSEQITCAPAOPE
         AND CTBLITCAPAOPER.CDHISTPADR      = HISTPADR.CDHISTPADR
         AND ITCAPAOPERABEM.CDBEMATIVO      = BEMATIVO.CDBEMATIVO
         AND ITCAPAOPERABEM.CDEMPRESA = BEMATIVO.CDEMPRESA
       GROUP BY CTBLITCAPAOPER.CDCONTCTBL, CTBLITCAPAOPER.IDTIPOOPERCONT, CTBLITCAPAOPER.DTINCCTBLITC, CTBLITCAPAOPER.NRSEQCAPAOPEBEM,
                CTBLITCAPAOPER.CDEMPRESA, ITCAPAOPERABEM.CDBEMATIVO, ITCAPAOPERABEM.IDSIMULOPERABEM,
                ITCAPAOPERABEM.NRSEQOPERABEM,CTBLITCAPAOPER.CDFILIAL,CTBLITCAPAOPER.CDCENTCUST,CTBLITCAPAOPER.CDHISTPADR,HISTPADR.DSHISTPADR,
                BEMATIVO.CDPATRIBEMATIVO,BEMATIVO.NMBEMATIVO,ITCAPAOPERABEM.NRNOTAFIITCAPA,CLIENTE.NMRAZSOCCLIE,CLIENTE.NRINSESTCLIE, BEMATIVO.CDFORNECED
       ORDER BY CDCONTCTBL,IDTIPOOPERCONT DESC;
   CURSOR C_CONTAGRUPTC IS
      SELECT CTBLITCAPAOPER.CDCONTCTBL, CTBLITCAPAOPER.IDTIPOOPERCONT, CTBLITCAPAOPER.DTINCCTBLITC,
             CTBLITCAPAOPER.NRSEQCAPAOPEBEM, CTBLITCAPAOPER.CDEMPRESA,
             sum(CTBLITCAPAOPER.VRCONTCTBLITC) VRCTBL, ITCAPAOPERABEM.IDSIMULOPERABEM,
             ITCAPAOPERABEM.NRSEQOPERABEM, CTBLITCAPAOPER.CDHISTPADR, HISTPADR.DSHISTPADR, CTBLITCAPAOPER.CDFILIAL, CTBLITCAPAOPER.CDCENTCUST, B.CDFORNECED
        FROM CTBLITCAPAOPER, ITCAPAOPERABEM,HISTPADR, BEMATIVO B
       WHERE EXISTS (SELECT CAPAOPERABEM.CDEMPRESA,ITCAPAOPERABEM.IDSIMULOPERABEM,
                            CAPAOPERABEM.NRSEQCAPAOPEBEM, ITCAPAOPERABEM.NRSEQITCAPAOPE
                       FROM CAPAOPERABEM, ITCAPAOPERABEM
                      WHERE P_CDEMPRESA                    = CAPAOPERABEM.CDEMPRESA
                        AND V_NRSEQFECHATC                 = CAPAOPERABEM.NRSEQFECHABEM
                        AND CAPAOPERABEM.CDEMPRESA         = ITCAPAOPERABEM.CDEMPRESA
                        AND CAPAOPERABEM.NRSEQCAPAOPEBEM   = ITCAPAOPERABEM.NRSEQCAPAOPEBEM
                        AND ITCAPAOPERABEM.CDEMPRESA       = CTBLITCAPAOPER.CDEMPRESA
                        AND ITCAPAOPERABEM.NRSEQCAPAOPEBEM = CTBLITCAPAOPER.NRSEQCAPAOPEBEM
                        AND ITCAPAOPERABEM.NRSEQITCAPAOPE  = CTBLITCAPAOPER.NRSEQITCAPAOPE)
         AND ITCAPAOPERABEM.CDEMPRESA       = CTBLITCAPAOPER.CDEMPRESA
         AND ITCAPAOPERABEM.IDSIMULOPERABEM = CTBLITCAPAOPER.IDSIMULOPERABEM
         AND ITCAPAOPERABEM.NRSEQCAPAOPEBEM = CTBLITCAPAOPER.NRSEQCAPAOPEBEM
         AND ITCAPAOPERABEM.NRSEQITCAPAOPE  = CTBLITCAPAOPER.NRSEQITCAPAOPE
         AND CTBLITCAPAOPER.CDHISTPADR      = HISTPADR.CDHISTPADR
         AND ITCAPAOPERABEM.CDEMPRESA       = B.CDEMPRESA
         AND ITCAPAOPERABEM.CDBEMATIVO      = B.CDBEMATIVO
      GROUP BY CDCONTCTBL, IDTIPOOPERCONT, DTINCCTBLITC, CTBLITCAPAOPER.NRSEQCAPAOPEBEM, CTBLITCAPAOPER.CDEMPRESA,
               ITCAPAOPERABEM.IDSIMULOPERABEM,ITCAPAOPERABEM.NRSEQOPERABEM,CTBLITCAPAOPER.CDHISTPADR,HISTPADR.DSHISTPADR,CTBLITCAPAOPER.CDFILIAL, CTBLITCAPAOPER.CDCENTCUST, B.CDFORNECED
      ORDER BY CDCONTCTBL,IDTIPOOPERCONT DESC;
   CURSOR C_CONTTRNSCO IS
      SELECT CTBLITCAPAOPER.CDCONTCTBL, CTBLITCAPAOPER.IDTIPOOPERCONT, CTBLITCAPAOPER.DTINCCTBLITC,
             CTBLITCAPAOPER.NRSEQCAPAOPEBEM, CTBLITCAPAOPER.CDEMPRESA,
             SUM(CTBLITCAPAOPER.VRCONTCTBLITC) VRCTBL, ITCAPAOPERABEM.CDBEMATIVO, ITCAPAOPERABEM.IDSIMULOPERABEM,
             ITCAPAOPERABEM.NRSEQOPERABEM, CTBLITCAPAOPER.CDHISTPADR, HISTPADR.DSHISTPADR, CTBLITCAPAOPER.CDFILIAL, CTBLITCAPAOPER.CDCENTCUST,FORNECEDOR.NMRAZSOCFORN,BEMATIVO.NMBEMATIVO,BEMATIVO.CDPATRIBEMATIVO,BEMATIVO.CDBEMATIVO,BEMATIVO.NRNOTAFISCBEM,BEMATIVO.CDFORNECED
        FROM CTBLITCAPAOPER, ITCAPAOPERABEM,HISTPADR,BEMATIVO,FORNECEDOR
       WHERE EXISTS (SELECT CAPAOPERABEM.CDEMPRESA,       ITCAPAOPERABEM.IDSIMULOPERABEM,
                            CAPAOPERABEM.NRSEQCAPAOPEBEM, ITCAPAOPERABEM.NRSEQITCAPAOPE
                       FROM CAPAOPERABEM, ITCAPAOPERABEM
                      WHERE P_CDEMPRESA                    = CAPAOPERABEM.CDEMPRESA
                        AND V_NRSEQFECHATC                 = CAPAOPERABEM.NRSEQFECHABEM
                        AND CAPAOPERABEM.CDEMPRESA         = ITCAPAOPERABEM.CDEMPRESA
                        AND CAPAOPERABEM.NRSEQCAPAOPEBEM   = ITCAPAOPERABEM.NRSEQCAPAOPEBEM
                        AND ITCAPAOPERABEM.CDEMPRESA       = CTBLITCAPAOPER.CDEMPRESA
                        AND ITCAPAOPERABEM.NRSEQCAPAOPEBEM = CTBLITCAPAOPER.NRSEQCAPAOPEBEM
                        AND ITCAPAOPERABEM.NRSEQITCAPAOPE  = CTBLITCAPAOPER.NRSEQITCAPAOPE)
         AND ITCAPAOPERABEM.CDEMPRESA       = CTBLITCAPAOPER.CDEMPRESA
         AND ITCAPAOPERABEM.IDSIMULOPERABEM = CTBLITCAPAOPER.IDSIMULOPERABEM
         AND ITCAPAOPERABEM.NRSEQCAPAOPEBEM = CTBLITCAPAOPER.NRSEQCAPAOPEBEM
         AND ITCAPAOPERABEM.NRSEQITCAPAOPE  = CTBLITCAPAOPER.NRSEQITCAPAOPE
         AND CTBLITCAPAOPER.CDHISTPADR      = HISTPADR.CDHISTPADR
         AND ITCAPAOPERABEM.CDBEMATIVO      = BEMATIVO.CDBEMATIVO
         AND BEMATIVO.CDFORNECED          = FORNECEDOR.CDFORNECED(+)
       GROUP BY CTBLITCAPAOPER.CDCONTCTBL, IDTIPOOPERCONT, DTINCCTBLITC, CTBLITCAPAOPER.NRSEQCAPAOPEBEM,
                CTBLITCAPAOPER.CDEMPRESA, ITCAPAOPERABEM.CDBEMATIVO, ITCAPAOPERABEM.IDSIMULOPERABEM,
                ITCAPAOPERABEM.NRSEQOPERABEM, CTBLITCAPAOPER.CDHISTPADR,HISTPADR.DSHISTPADR, CTBLITCAPAOPER.CDFILIAL, CTBLITCAPAOPER.CDCENTCUST,FORNECEDOR.NMRAZSOCFORN,BEMATIVO.NMBEMATIVO,BEMATIVO.CDPATRIBEMATIVO,BEMATIVO.CDBEMATIVO,BEMATIVO.NRNOTAFISCBEM,BEMATIVO.CDFORNECED
       ORDER BY ITCAPAOPERABEM.CDBEMATIVO,CDCONTCTBL,IDTIPOOPERCONT DESC;
BEGIN
   P_RETORNO := '';
   V_EXISTE_OPER := 'N';
   V_EXISTE_CTBL := 0;
   IF P_CARGAFECHA = 'C' THEN
         IF P_IDOPERACAO = 'F' THEN
            DELETE
                  FROM CTBLITCAPAOPER
             WHERE CDEMPRESA||IDSIMULOPERABEM||NRSEQCAPAOPEBEM IN (SELECT CDEMPRESA||IDSIMULOPERABEM||NRSEQCAPAOPEBEM
                                                                     FROM CAPAOPERABEM
                                                                    WHERE P_CDEMPRESA     = CAPAOPERABEM.CDEMPRESA
                                                                      AND P_NRSEQFECHABEM = CAPAOPERABEM.NRSEQFECHABEM);
            OPEN C_OPERFECHA;
         ELSE
            OPEN C_OPERTRANS;
         END IF;
         LOOP
         IF P_IDOPERACAO = 'F'  THEN
            FETCH C_OPERFECHA INTO V_CDEMPRESA,      V_CDBEMATIVO,
                                   V_CDGRUPOBEM,     V_VRINIDEP,
                                   V_VRJADEP,        V_VRFALDEP,
                                   V_VROPERA,        V_NRSEQOPERABEM,
                                   V_CDTIPOOPERBEM,  V_DTOPERA,
                                   V_VRAQUIBEM,      V_NRSEQCAPAOPEBEM,
                                   V_NRSEQITCAPAOPE, V_IDSIMULOPERABEM,V_CDFORNECED, V_TPAPROP,V_IDORIGNOTA;
            EXIT WHEN C_OPERFECHA%NOTFOUND;
         ELSE
            FETCH C_OPERTRANS INTO V_CDEMPRESA,      V_CDBEMATIVO,
                                   V_CDGRUPOBEM,     V_VRINIDEP,
                                   V_VRJADEP,        V_VRFALDEP,
                                   V_VROPERA,        V_NRSEQOPERABEM,
                                   V_CDTIPOOPERBEM,  V_DTOPERA,
                                   V_VRAQUIBEM,      V_NRSEQCAPAOPEBEM,
                                   V_NRSEQITCAPAOPE, V_IDSIMULOPERABEM,V_CDFORNECED;
            EXIT WHEN C_OPERTRANS%NOTFOUND;
         END IF;
         V_EXISTE_OPER := 'S';
         BEGIN
            SELECT COUNT(CDEMPRESA)
              INTO V_EXISTE_CTBL
              FROM TPCTTIPOOPERBEM
             WHERE CDEMPRESA     = P_CDEMPRESA
               AND CDTIPOOPERBEM    = V_CDTIPOOPERBEM;
         EXCEPTION
           WHEN NO_DATA_FOUND THEN
              V_EXISTE_CTBL := NULL;
         END;
         BEGIN
            SELECT IDTIPOOPERBEM
              INTO V_TPOPER
              FROM TIPOOPERBEM
             WHERE CDEMPRESA     = P_CDEMPRESA
               AND CDTIPOOPERBEM = V_CDTIPOOPERBEM;
         EXCEPTION
           WHEN NO_DATA_FOUND THEN
              V_TPOPER := NULL;
         END;
         IF NOT (V_TPOPER = '01' AND V_IDORIGNOTA = 'S') THEN /*A CONTABILIZACAO IMOBILIZACAO E FEITA NO LANCAMENTO DA NOTA FISCAL, POR ISSO A ROTINA NAO IRA CONTABILIZAR.*/
            IF V_TPOPER <> '09' THEN /* A CONTABLIZACAO DA TRANSFERENCIA CONTABIL SERA FEITA SEPARADAMENTE LOGO ABAIXO NA PROCEDURE.*/
               IF V_EXISTE_CTBL > 0 THEN
                  /*Rotina especifica para a contabilizacao dos bens que possuem apropriacao de credito por Depreciacao ou amortizacao*/
                  IF ((V_TPOPER = '02') AND (V_TPAPROP = '09' OR V_TPAPROP = '11')) THEN                 
                    FOR C_CTBLDPR IN (SELECT CDTIPOCONTABEM, NRSEQCTBLOP, IDDEBCREDCTBLOP, IDVALORCTBLOP
                                        FROM TPCTTIPOOPERBEM
                                       WHERE CDEMPRESA     = P_CDEMPRESA
                                         AND CDTIPOOPERBEM = V_CDTIPOOPERBEM
                                      ORDER BY NRSEQCTBLOP) LOOP
                      BEGIN
                         SELECT CDCONTCTBLBEM,   CDHISTPADR
                           INTO V_CDCONTCTBLBEM, V_HISTPADRAO
                           FROM CTBLBEMATIVO
                          WHERE CDEMPRESA      = P_CDEMPRESA
                            AND CDBEMATIVO     = V_CDBEMATIVO
                            AND CDTIPOCONTABEM = C_CTBLDPR.CDTIPOCONTABEM;
                      EXCEPTION
                         WHEN NO_DATA_FOUND THEN
                            V_CDCONTCTBLBEM := NULL;
                      END;
                      IF V_CDCONTCTBLBEM IS NULL THEN
                         BEGIN
                            SELECT CDCONTCTBLGRBEM, CDHISTPADR
                              INTO V_CDCONTCTBLBEM, V_HISTPADRAO
                              FROM CTBLGRUPOBEM
                             WHERE CDEMPRESA      = P_CDEMPRESA
                               AND CDGRUPOBEM     = V_CDGRUPOBEM
                               AND CDTIPOCONTABEM = C_CTBLDPR.CDTIPOCONTABEM;
                         EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                               V_CDCONTCTBLBEM := NULL;
                         END;
                      END IF;
                      BEGIN
                         SELECT COUNT(*)
                           INTO V_COUNTIMP
                           FROM IMPOSTOBEM IB, IMPOSTO IM
                          WHERE IB.CDEMPRESA = P_CDEMPRESA
                            AND IB.CDBEMATIVO = V_CDBEMATIVO
                            AND IM.CDIMPOSTO  = IB.CDIMPOSTO
                            AND IM.CDIMPOSTONFE = '50';
                      EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_COUNTIMP := 0;
                      END;
                      V_VALORCTBL := V_VROPERA;
                      IF V_VALORCTBL > 0 THEN
                         /* Recebe os valores do PIS, do Cofins, e da depreciacao ja retirados os impostos, no caso do lancamento ser Debito.*/
                         IF ((C_CTBLDPR.IDDEBCREDCTBLOP = 'D') AND (V_COUNTIMP <> 0)) THEN
                            V_VROPERPIS  := (V_VALORCTBL*1.65)/100;
                            V_VROPERCOF  := (V_VALORCTBL*7.65)/100;
                            V_VRCTBLDEPR := V_VALORCTBL - (V_VROPERPIS + V_VROPERCOF);
                            V_VALORCTBL  := V_VRCTBLDEPR; 
                         END IF;        
                         /* Realiza os lancamento da depreciacao com rateio (C_RATEIOBEM com dados) ou sem rateio (C_RATEIOBEM sem dados)*/
                         V_EXISRATE := FALSE;
                         FOR C_RATEIOBEM IN (
                            SELECT CDFILICCUST, CDCENTCUSBEM, VRRATEBEM
                              FROM RATEIOBEM R
                             WHERE R.CDEMPRESA   = P_CDEMPRESA
                               AND R.CDBEMATIVO  = V_CDBEMATIVO) LOOP
                            V_VALORCTBLRATE := ROUND(((V_VALORCTBL*C_RATEIOBEM.VRRATEBEM)/100),2);
                            SELECT NVL(MAX(TO_NUMBER(NRSEQCTBLITCAPA)),0)+1 INTO V_NRSEQCTBLITCAPA
                              FROM CTBLITCAPAOPER
                             WHERE CDEMPRESA = P_CDEMPRESA
                                AND IDSIMULOPERABEM = V_IDSIMULOPERABEM
                               AND NRSEQCAPAOPEBEM = V_NRSEQCAPAOPEBEM
                               AND NRSEQITCAPAOPE  = V_NRSEQITCAPAOPE;
                            INSERT INTO CTBLITCAPAOPER
                                   (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                    CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                    DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                    CDHISTPADR, CDFILIAL, CDCENTCUST)
                            VALUES
                                   (P_CDEMPRESA,     V_IDSIMULOPERABEM, V_NRSEQCAPAOPEBEM,     V_NRSEQITCAPAOPE,
                                     V_CDCONTCTBLBEM, C_CTBLDPR.IDDEBCREDCTBLOP, LPAD(TO_CHAR(V_NRSEQCTBLITCAPA),3,'0'), V_VALORCTBLRATE,
                                    SYSDATE,         P_CDOPERADOR,           SYSDATE,                    P_CDOPERADOR,
                                    V_HISTPADRAO,C_RATEIOBEM.CDFILICCUST,C_RATEIOBEM.CDCENTCUSBEM);
                            V_EXISRATE := TRUE;
                         END LOOP;
                         IF V_EXISRATE = FALSE THEN
                            BEGIN
                               SELECT BEMATIVO.CDFILIAL, BEMATIVO.CDCENTCUST
                                 INTO V_CDFILIAL, V_CDCENTCUST
                                 FROM BEMATIVO
                                WHERE BEMATIVO.CDBEMATIVO = V_CDBEMATIVO
                                  AND BEMATIVO.CDEMPRESA = P_CDEMPRESA;
                            EXCEPTION
                               WHEN NO_DATA_FOUND THEN
                                 V_CDFILIAL   := NULL;
                                 V_CDCENTCUST := NULL;
                            END;
                            SELECT NVL(MAX(TO_NUMBER(NRSEQCTBLITCAPA)),0)+1 INTO V_NRSEQCTBLITCAPA
                              FROM CTBLITCAPAOPER
                             WHERE CDEMPRESA = P_CDEMPRESA
                               AND IDSIMULOPERABEM = V_IDSIMULOPERABEM
                               AND NRSEQCAPAOPEBEM = V_NRSEQCAPAOPEBEM
                               AND NRSEQITCAPAOPE  = V_NRSEQITCAPAOPE;
                            INSERT INTO CTBLITCAPAOPER
                                   (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                    CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                    DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                    CDHISTPADR, CDFILIAL, CDCENTCUST)
                            VALUES
                                   (P_CDEMPRESA,     V_IDSIMULOPERABEM, V_NRSEQCAPAOPEBEM,     V_NRSEQITCAPAOPE,
                                    V_CDCONTCTBLBEM, C_CTBLDPR.IDDEBCREDCTBLOP, LPAD(TO_CHAR(V_NRSEQCTBLITCAPA),3,'0'), V_VALORCTBL,
                                    SYSDATE,         P_CDOPERADOR,           SYSDATE,                    P_CDOPERADOR,
                                    V_HISTPADRAO,V_CDFILIAL,V_CDCENTCUST);
                         END IF;
                         /*Realiza o lancamento dos impostos*/
                         IF C_CTBLDPR.IDDEBCREDCTBLOP = 'D' THEN
                         /*Recebe a Filial, Centro de Custo e Conta Contabil dos impostos*/
                            BEGIN
                               SELECT BEMATIVO.CDFILIAL, BEMATIVO.CDCENTCUST
                                 INTO V_CDFILIAL, V_CDCENTCUST
                                 FROM BEMATIVO
                                WHERE BEMATIVO.CDBEMATIVO = V_CDBEMATIVO
                                  AND BEMATIVO.CDEMPRESA = P_CDEMPRESA;
                            EXCEPTION
                               WHEN NO_DATA_FOUND THEN
                                 V_CDFILIAL   := NULL;
                                 V_CDCENTCUST := NULL;
                            END;
                           
                            BEGIN 
                               SELECT CONTCTBL_IMPOSTO(IB.CDIMPOSTO, 'A', '', '', '') AS CDCONTPIS 
                                 INTO V_CDCONTPIS 
                                 FROM IMPOSTOBEM IB, IMPOSTO IM
                                WHERE IB.CDEMPRESA = P_CDEMPRESA 
                                  AND IB.CDBEMATIVO = V_CDBEMATIVO 
                                  AND IM.CDIMPOSTO  = IB.CDIMPOSTO
                                  AND IM.CDIMPOSTONFE = '50';
                            EXCEPTION
                              WHEN NO_DATA_FOUND THEN
                                 V_CDCONTPIS := NULL;
                            END;
                            
                            BEGIN 
                               SELECT CONTCTBL_IMPOSTO(IB.CDIMPOSTO, 'A', '', '', '') AS CDCONTCOF 
                                 INTO V_CDCONTCOF
                                 FROM IMPOSTOBEM IB, IMPOSTO IM
                                WHERE IB.CDEMPRESA = P_CDEMPRESA 
                                  AND IB.CDBEMATIVO = V_CDBEMATIVO 
                                  AND IM.CDIMPOSTO  = IB.CDIMPOSTO
                                  AND IM.CDIMPOSTONFE = '60';
                            EXCEPTION
                              WHEN NO_DATA_FOUND THEN
                                 V_CDCONTCOF := NULL;
                            END;
                            
                            /* 1 - PIS*/
                            IF (V_VROPERPIS > 0) AND (V_CDCONTPIS IS NOT NULL) THEN     
                               SELECT NVL(MAX(TO_NUMBER(NRSEQCTBLITCAPA)),0)+1 INTO V_NRSEQCTBLITCAPA
                                 FROM CTBLITCAPAOPER
                                WHERE CDEMPRESA = P_CDEMPRESA
                                  AND IDSIMULOPERABEM = V_IDSIMULOPERABEM
                                  AND NRSEQCAPAOPEBEM = V_NRSEQCAPAOPEBEM
                                  AND NRSEQITCAPAOPE  = V_NRSEQITCAPAOPE;
                               INSERT INTO CTBLITCAPAOPER
                                      (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                       CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                       DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                       CDHISTPADR, CDFILIAL, CDCENTCUST)
                               VALUES
                                      (P_CDEMPRESA,     V_IDSIMULOPERABEM, V_NRSEQCAPAOPEBEM,     V_NRSEQITCAPAOPE,
                                       V_CDCONTPIS, 'D', LPAD(TO_CHAR(V_NRSEQCTBLITCAPA),3,'0'), V_VROPERPIS,
                                       SYSDATE,         P_CDOPERADOR,           SYSDATE,   P_CDOPERADOR,
                                       V_HISTPADRAO,V_CDFILIAL,V_CDCENTCUST);   
                               END IF;
                            /* 2 - COFINS*/
                            IF (V_VROPERCOF > 0) AND (V_CDCONTCOF IS NOT NULL) THEN    
                               SELECT NVL(MAX(TO_NUMBER(NRSEQCTBLITCAPA)),0)+1 INTO V_NRSEQCTBLITCAPA
                                 FROM CTBLITCAPAOPER
                                WHERE CDEMPRESA = P_CDEMPRESA
                                  AND IDSIMULOPERABEM = V_IDSIMULOPERABEM
                                  AND NRSEQCAPAOPEBEM = V_NRSEQCAPAOPEBEM
                                  AND NRSEQITCAPAOPE  = V_NRSEQITCAPAOPE;
                               INSERT INTO CTBLITCAPAOPER
                                      (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                       CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                       DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                       CDHISTPADR, CDFILIAL, CDCENTCUST)
                               VALUES
                                      (P_CDEMPRESA,     V_IDSIMULOPERABEM, V_NRSEQCAPAOPEBEM, V_NRSEQITCAPAOPE,
                                       V_CDCONTCOF, 'D', LPAD(TO_CHAR(V_NRSEQCTBLITCAPA),3,'0'), V_VROPERCOF,
                                       SYSDATE, P_CDOPERADOR, SYSDATE, P_CDOPERADOR,
                                       V_HISTPADRAO, V_CDFILIAL, V_CDCENTCUST);   
                             END IF;
                         END IF;
                      END IF;
                    END LOOP;
                  ELSE   
                     FOR C_CTBL IN (SELECT CDTIPOCONTABEM, NRSEQCTBLOP, IDDEBCREDCTBLOP, IDVALORCTBLOP
                                      FROM TPCTTIPOOPERBEM
                                     WHERE CDEMPRESA     = P_CDEMPRESA
                                       AND CDTIPOOPERBEM = V_CDTIPOOPERBEM
                                     ORDER BY NRSEQCTBLOP) LOOP
                         BEGIN
                            SELECT CDCONTCTBLBEM,   CDHISTPADR
                              INTO V_CDCONTCTBLBEM, V_HISTPADRAO
                              FROM CTBLBEMATIVO
                             WHERE CDEMPRESA      = P_CDEMPRESA
                               AND CDBEMATIVO     = V_CDBEMATIVO
                               AND CDTIPOCONTABEM = C_CTBL.CDTIPOCONTABEM;
                         EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                               V_CDCONTCTBLBEM := NULL;
                         END;
                         IF V_CDCONTCTBLBEM IS NULL THEN
                            BEGIN
                               SELECT CDCONTCTBLGRBEM, CDHISTPADR
                                 INTO V_CDCONTCTBLBEM, V_HISTPADRAO
                                 FROM CTBLGRUPOBEM
                                WHERE CDEMPRESA      = P_CDEMPRESA
                                  AND CDGRUPOBEM     = V_CDGRUPOBEM
                                  AND CDTIPOCONTABEM = C_CTBL.CDTIPOCONTABEM;
                            EXCEPTION
                               WHEN NO_DATA_FOUND THEN
                                  V_CDCONTCTBLBEM := NULL;
                            END;
                         END IF;
                         IF V_CDCONTCTBLBEM IS NOT NULL THEN
                            IF C_CTBL.IDVALORCTBLOP = '01' THEN
                               V_VALORCTBL := V_VROPERA;
                            END IF;
                            IF C_CTBL.IDVALORCTBLOP = '02' THEN
                               V_VALORCTBL := V_VRINIDEP;
                            END IF;
                            IF C_CTBL.IDVALORCTBLOP = '03' THEN
                               V_VALORCTBL := V_VROPERA;
                            END IF;
                            IF C_CTBL.IDVALORCTBLOP = '04' THEN
                               V_VALORCTBL := V_VRFALDEP;
                            END IF;
                            IF C_CTBL.IDVALORCTBLOP = '05' THEN
                               V_VALORCTBL := V_VRJADEP;
                            END IF;
                            IF (C_CTBL.IDVALORCTBLOP = '06') OR (C_CTBL.IDVALORCTBLOP = '07') THEN
                               V_VALORCTBL := V_VROPERA - V_VRFALDEP; 
                               IF ((C_CTBL.IDVALORCTBLOP = '06') AND (V_VALORCTBL > 0)) OR ((C_CTBL.IDVALORCTBLOP = '07') AND (V_VALORCTBL < 0)) THEN
                                  V_VALORCTBL := 0;
                              ELSE
                                  IF V_VALORCTBL < 0 THEN
                                      V_VALORCTBL := V_VALORCTBL * (-1);
                                  END IF;
                                 
                               END IF;
                   
                               /*
                               IF V_VALORCTBL < 0 THEN 
                                  V_VALORCTBL := V_VALORCTBL * (-1); 
                               END IF; */
                            END IF; 
                            IF C_CTBL.IDVALORCTBLOP = '08' THEN 
                               V_VALORCTBL := V_VROPERA; 
                            END IF; 
                            IF C_CTBL.IDVALORCTBLOP = '09' THEN 
                               V_VALORCTBL := V_VROPERA; 
                            END IF; 
                            
                            V_VALORCTBL := ROUND(V_VALORCTBL,2);
                            IF V_VALORCTBL > 0 THEN
                               V_EXISRATE := FALSE;
                               FOR C_RATEIOBEM IN (
                                   SELECT CDFILICCUST, CDCENTCUSBEM, VRRATEBEM
                                     FROM RATEIOBEM R
                                    WHERE R.CDEMPRESA   = P_CDEMPRESA
                                      AND R.CDBEMATIVO  = V_CDBEMATIVO) LOOP
                                       V_VALORCTBLRATE := ROUND(((V_VALORCTBL*C_RATEIOBEM.VRRATEBEM)/100),2);
                                       SELECT NVL(MAX(TO_NUMBER(NRSEQCTBLITCAPA)),0)+1 INTO V_NRSEQCTBLITCAPA
                                         FROM CTBLITCAPAOPER
                                        WHERE CDEMPRESA = P_CDEMPRESA
                                          AND IDSIMULOPERABEM = V_IDSIMULOPERABEM
                                          AND NRSEQCAPAOPEBEM = V_NRSEQCAPAOPEBEM
                                          AND NRSEQITCAPAOPE  = V_NRSEQITCAPAOPE;
                                       INSERT INTO CTBLITCAPAOPER
                                              (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                               CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                               DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                               CDHISTPADR, CDFILIAL, CDCENTCUST)
                                       VALUES
                                              (P_CDEMPRESA,     V_IDSIMULOPERABEM, V_NRSEQCAPAOPEBEM,     V_NRSEQITCAPAOPE,
                                               V_CDCONTCTBLBEM, C_CTBL.IDDEBCREDCTBLOP, LPAD(TO_CHAR(V_NRSEQCTBLITCAPA),3,'0'), V_VALORCTBLRATE,
                                               SYSDATE,         P_CDOPERADOR,           SYSDATE,                    P_CDOPERADOR,
                                               V_HISTPADRAO,C_RATEIOBEM.CDFILICCUST,C_RATEIOBEM.CDCENTCUSBEM);
                                    V_EXISRATE := TRUE;
                               END LOOP;
                               IF V_EXISRATE = FALSE THEN
                                  BEGIN
                                    SELECT BEMATIVO.CDFILIAL, BEMATIVO.CDCENTCUST
                                      INTO V_CDFILIAL, V_CDCENTCUST
                                      FROM BEMATIVO
                                     WHERE BEMATIVO.CDBEMATIVO = V_CDBEMATIVO
                                       AND BEMATIVO.CDEMPRESA = P_CDEMPRESA;
                                  EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                      V_CDFILIAL   := NULL;
                                      V_CDCENTCUST := NULL;
                                  END;
                                  SELECT NVL(MAX(TO_NUMBER(NRSEQCTBLITCAPA)),0)+1 INTO V_NRSEQCTBLITCAPA
                                    FROM CTBLITCAPAOPER
                                   WHERE CDEMPRESA = P_CDEMPRESA
                                     AND IDSIMULOPERABEM = V_IDSIMULOPERABEM
                                     AND NRSEQCAPAOPEBEM = V_NRSEQCAPAOPEBEM
                                     AND NRSEQITCAPAOPE  = V_NRSEQITCAPAOPE;
                                  INSERT INTO CTBLITCAPAOPER
                                         (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                          CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                          DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                          CDHISTPADR, CDFILIAL, CDCENTCUST)
                                  VALUES
                                         (P_CDEMPRESA,     V_IDSIMULOPERABEM, V_NRSEQCAPAOPEBEM,     V_NRSEQITCAPAOPE,
                                          V_CDCONTCTBLBEM, C_CTBL.IDDEBCREDCTBLOP, LPAD(TO_CHAR(V_NRSEQCTBLITCAPA),3,'0'), V_VALORCTBL,
                                          SYSDATE,         P_CDOPERADOR,           SYSDATE,                    P_CDOPERADOR,
                                          V_HISTPADRAO,V_CDFILIAL,V_CDCENTCUST);
                               END IF;
                            END IF;
                         END IF;  
                      END LOOP;
                   END IF;
               ELSE
                  P_RETORNO := '03'||V_CDTIPOOPERBEM;
                  EXIT;
               END IF;
            ELSE
               SELECT MIN(CDTIPOOPERBEM) AS CDTIPOOPERBEM
                 INTO V_TIPOOPERIMOB
                 FROM TIPOOPERBEM
                 WHERE CDEMPRESA   = P_CDEMPRESA
                 AND IDTIPOOPERBEM = '01';
               BEGIN
                 SELECT COUNT(CDEMPRESA)
                   INTO V_EXISTE_IMOB
                   FROM TPCTTIPOOPERBEM
                WHERE CDEMPRESA     = P_CDEMPRESA
                  AND CDTIPOOPERBEM = V_TIPOOPERIMOB;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                    V_EXISTE_IMOB := 0;
               END;
               SELECT MIN(CDTIPOOPERBEM) AS CDTIPOOPERBEM
                 INTO V_TIPOOPERDEPR
                 FROM TIPOOPERBEM
                 WHERE CDEMPRESA   = P_CDEMPRESA
                 AND IDTIPOOPERBEM = '02';
               BEGIN
                 SELECT COUNT(CDEMPRESA)
                   INTO V_EXISTE_DEPR
                   FROM TPCTTIPOOPERBEM
                WHERE CDEMPRESA     = P_CDEMPRESA
                  AND CDTIPOOPERBEM = V_TIPOOPERDEPR;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                    V_EXISTE_DEPR := 0;
               END;
               IF V_EXISTE_IMOB > 0 AND  V_EXISTE_DEPR > 0 THEN
                  SELECT DISTINCT  FB.NRSEQFECHABEM , CO.NRSEQCAPAOPEBEM,  IT.NRSEQOPERABEM
                    INTO V_NRSEQFECHACO, V_NRSEQOPERACO,  V_NRSEQOPERABEMCO
                    FROM FECHAMENTOBEM FB, ITCAPAOPERABEM IT, CAPAOPERABEM CO, OPERACAOBEM OP,
                         TIPOOPERBEM  TB
                   WHERE FB.CDEMPRESA  = P_CDEMPRESA
                     AND IT.CDEMPRESA  = FB.CDEMPRESA
                     AND CO.CDEMPRESA  = FB.CDEMPRESA
                     AND OP.CDEMPRESA  = FB.CDEMPRESA
                     AND TRUNC(FB.DTFECHABEM) = V_DTOPERA
                     AND IT.CDBEMATIVO = V_CDBEMATIVO
                     AND OP.CDBEMATIVO = IT.CDBEMATIVO
                     AND CO.NRSEQFECHABEM = FB.NRSEQFECHABEM
                     AND OP.NRSEQOPERABEM = IT.NRSEQOPERABEM
                     AND CO.CDTIPOOPERBEM = TB.CDTIPOOPERBEM
                     AND TB.IDTIPOOPERBEM = '09'
                     AND OP.IDTIPOFECHAOPER = 'C'
                     AND TRUNC(OP.DTOPERABEM) = TRUNC(FB.DTFECHABEM)
                     AND IT.NRSEQCAPAOPEBEM = CO.NRSEQCAPAOPEBEM;
                     V_NRSEQCTBLITRAT := NULL;
                     V_NRSEQCTBLIT    := NULL;
                  FOR IMOB_CTBL_ANT IN (SELECT T.CDTIPOCONTABEM, T.NRSEQCTBLOP, T.IDDEBCREDCTBLOP, T.IDVALORCTBLOP, CT.CDHISTPADR, BA.CDFILIAL, BA.CDCENTCUST,
                                               CT.CDCONTCTBLGRBEM, DECODE(T.IDDEBCREDCTBLOP, 'D', BA.VRAQUIBEMATIVO, BA.VRAQUIBEMATIVO*(-1)) AS VALORCTBL,
                                               NVL(DECODE(T.IDDEBCREDCTBLOP, 'D', 'C','D'),'') AS TIPOCTBL, BA.CDBEMATIVO, IT.NRSEQITCAPAOPE
                                          FROM TPCTTIPOOPERBEM T, CTBLGRUPOBEM CT, BEMATIVO BA, (SELECT CDGRUPOITCANTE AS CDGRUPOBEM, CDEMPRESA, CDBEMATIVO,
                                                                                                        NRSEQITCAPAOPE
                                                                                                   FROM ITCAPAOPERABEM
                                                                                                  WHERE NRSEQCAPAOPEBEM = V_NRSEQOPERACO
                                                                                                    AND DSOBSITCAPAOPER = 'IMOBILIZACAO'
                                                                                                    AND VRITCAPAOPERA < 0
                                                                                                    AND CDEMPRESA       = P_CDEMPRESA) IT
                                         WHERE T.CDEMPRESA   = P_CDEMPRESA
                                           AND CT.CDEMPRESA  = T.CDEMPRESA
                                           AND IT.CDEMPRESA  = T.CDEMPRESA
                                           AND T.CDTIPOOPERBEM  = V_TIPOOPERIMOB
                                           AND CT.CDTIPOCONTABEM = T.CDTIPOCONTABEM
                                           AND CT.CDGRUPOBEM = IT.CDGRUPOBEM
                                           AND BA.CDEMPRESA  = T.CDEMPRESA
                                           AND BA.CDBEMATIVO = IT.CDBEMATIVO
                                           ORDER BY NRSEQCTBLOP) LOOP
                      V_EXISRATE := FALSE;
                      FOR C_RATEIOBEM IN (
                          SELECT CDFILICCUST, CDCENTCUSBEM, VRRATEBEM
                            FROM RATEIOBEM R
                           WHERE R.CDEMPRESA   = P_CDEMPRESA
                             AND R.CDBEMATIVO  = IMOB_CTBL_ANT.CDBEMATIVO) LOOP
                          V_VALORCTBLRATE := ROUND(((IMOB_CTBL_ANT.VALORCTBL *C_RATEIOBEM.VRRATEBEM)/100),2);
                          IF V_NRSEQCTBLITRAT IS NULL THEN
                             V_NRSEQCTBLITRAT := '001';
                          ELSE
                             V_NRSEQCTBLITRAT := LPAD(TO_CHAR(TO_NUMBER(V_NRSEQCTBLITRAT)+1),3,'0');
                          END IF;
                          INSERT INTO CTBLITCAPAOPER
                                     (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                      CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                      DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                      CDHISTPADR, CDFILIAL, CDCENTCUST)
                          VALUES
                                     (P_CDEMPRESA,     V_IDSIMULOPERABEM, V_NRSEQOPERACO,   IMOB_CTBL_ANT.NRSEQITCAPAOPE,
                                     IMOB_CTBL_ANT.CDCONTCTBLGRBEM, IMOB_CTBL_ANT.TIPOCTBL, V_NRSEQCTBLITRAT, V_VALORCTBLRATE,
                                     V_DTOPERA ,         P_CDOPERADOR,           V_DTOPERA, P_CDOPERADOR,
                                     IMOB_CTBL_ANT.CDHISTPADR,C_RATEIOBEM.CDFILICCUST,C_RATEIOBEM.CDCENTCUSBEM);
                           V_EXISRATE := TRUE;
                      END LOOP;
                      IF V_EXISRATE = FALSE THEN
                         BEGIN
                           SELECT BEMATIVO.CDFILIAL, BEMATIVO.CDCENTCUST
                             INTO V_CDFILIAL, V_CDCENTCUST
                             FROM BEMATIVO
                            WHERE BEMATIVO.CDBEMATIVO = IMOB_CTBL_ANT.CDBEMATIVO
                              AND BEMATIVO.CDEMPRESA = P_CDEMPRESA;
                         EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                             V_CDFILIAL   := NULL;
                             V_CDCENTCUST := NULL;
                         END;
                         IF V_NRSEQCTBLIT IS NULL THEN
                            V_NRSEQCTBLIT := '001';
                         ELSE
                            V_NRSEQCTBLIT := LPAD(TO_CHAR(TO_NUMBER(V_NRSEQCTBLIT)+1),03,'0');
                         END IF;
                         INSERT INTO CTBLITCAPAOPER
                                (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                 CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                 DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                 CDHISTPADR, CDFILIAL, CDCENTCUST)
                         VALUES
                                (P_CDEMPRESA,     V_IDSIMULOPERABEM, V_NRSEQOPERACO,    IMOB_CTBL_ANT.NRSEQITCAPAOPE,
                                 IMOB_CTBL_ANT.CDCONTCTBLGRBEM, IMOB_CTBL_ANT.TIPOCTBL, V_NRSEQCTBLIT, IMOB_CTBL_ANT.VALORCTBL,
                                 V_DTOPERA, P_CDOPERADOR,   V_DTOPERA,  P_CDOPERADOR,
                                 IMOB_CTBL_ANT.CDHISTPADR,V_CDFILIAL,V_CDCENTCUST);
                      END IF;
                  END LOOP;
                  FOR IMOB_CTBL_NEW IN (SELECT T.CDTIPOCONTABEM, T.NRSEQCTBLOP, T.IDDEBCREDCTBLOP, T.IDVALORCTBLOP, CT.CDHISTPADR, BA.CDFILIAL, BA.CDCENTCUST,
                                               CT.CDCONTCTBLGRBEM, DECODE(T.IDDEBCREDCTBLOP, 'C', BA.VRAQUIBEMATIVO, BA.VRAQUIBEMATIVO*(-1)) AS VALORCTBL, BA.CDBEMATIVO,
                                               IT.NRSEQITCAPAOPE
                                         FROM TPCTTIPOOPERBEM T, CTBLGRUPOBEM CT, BEMATIVO BA, (SELECT CDGRUPOITCNOVO AS CDGRUPOBEM, CDEMPRESA, CDBEMATIVO,
                                                                                                       NRSEQITCAPAOPE
                                                                                                  FROM ITCAPAOPERABEM
                                                                                                 WHERE NRSEQCAPAOPEBEM = V_NRSEQOPERACO
                                                                                                   AND DSOBSITCAPAOPER = 'IMOBILIZACAO'
                                                                                                   AND VRITCAPAOPERA > 0
                                                                                                   AND CDEMPRESA       = P_CDEMPRESA) IT
                                        WHERE T.CDEMPRESA   = P_CDEMPRESA
                                          AND CT.CDEMPRESA  = T.CDEMPRESA
                                          AND IT.CDEMPRESA  = T.CDEMPRESA
                                          AND T.CDTIPOOPERBEM  = V_TIPOOPERIMOB
                                          AND CT.CDTIPOCONTABEM = T.CDTIPOCONTABEM
                                          AND CT.CDGRUPOBEM = IT.CDGRUPOBEM
                                          AND BA.CDEMPRESA  = T.CDEMPRESA
                                          AND BA.CDBEMATIVO = IT.CDBEMATIVO
                                          ORDER BY NRSEQCTBLOP) LOOP
                      V_EXISRATE := FALSE;
                      FOR C_RATEIOBEM IN (
                          SELECT CDFILICCUST, CDCENTCUSBEM, VRRATEBEM
                            FROM RATEIOBEM R
                           WHERE R.CDEMPRESA   = P_CDEMPRESA
                             AND R.CDBEMATIVO  = IMOB_CTBL_NEW.CDBEMATIVO) LOOP
                           V_VALORCTBLRATE := ROUND(((IMOB_CTBL_NEW.VALORCTBL *C_RATEIOBEM.VRRATEBEM)/100),2);
                          IF V_NRSEQCTBLITRAT IS NULL THEN
                             V_NRSEQCTBLITRAT := '001';
                          ELSE
                             V_NRSEQCTBLITRAT := LPAD(TO_CHAR(TO_NUMBER(V_NRSEQCTBLITRAT)+1),3,'0');
                          END IF;
                          INSERT INTO CTBLITCAPAOPER
                                     (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                      CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                      DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                      CDHISTPADR, CDFILIAL, CDCENTCUST)
                          VALUES
                                     (P_CDEMPRESA,     V_IDSIMULOPERABEM,  V_NRSEQOPERACO, IMOB_CTBL_NEW.NRSEQITCAPAOPE,
                                     IMOB_CTBL_NEW.CDCONTCTBLGRBEM, IMOB_CTBL_NEW.IDDEBCREDCTBLOP, V_NRSEQCTBLITRAT, V_VALORCTBLRATE,
                                     V_DTOPERA, P_CDOPERADOR, V_DTOPERA, P_CDOPERADOR,
                                     IMOB_CTBL_NEW.CDHISTPADR,C_RATEIOBEM.CDFILICCUST,C_RATEIOBEM.CDCENTCUSBEM);
                           V_EXISRATE := TRUE;
                      END LOOP;
                      IF V_EXISRATE = FALSE THEN
                         BEGIN
                           SELECT BEMATIVO.CDFILIAL, BEMATIVO.CDCENTCUST
                             INTO V_CDFILIAL, V_CDCENTCUST
                             FROM BEMATIVO
                            WHERE BEMATIVO.CDBEMATIVO = IMOB_CTBL_NEW.CDBEMATIVO
                              AND BEMATIVO.CDEMPRESA = P_CDEMPRESA;
                         EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                             V_CDFILIAL   := NULL;
                             V_CDCENTCUST := NULL;
                         END;
                         IF V_NRSEQCTBLIT IS NULL THEN
                            V_NRSEQCTBLIT := '001';
                         ELSE
                            V_NRSEQCTBLIT := LPAD(TO_CHAR(TO_NUMBER(V_NRSEQCTBLIT)+1),03,'0');
                         END IF;
                         INSERT INTO CTBLITCAPAOPER
                                (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                 CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                 DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                 CDHISTPADR, CDFILIAL, CDCENTCUST)
                         VALUES
                                (P_CDEMPRESA,     V_IDSIMULOPERABEM,  V_NRSEQOPERACO , IMOB_CTBL_NEW.NRSEQITCAPAOPE,
                                 IMOB_CTBL_NEW.CDCONTCTBLGRBEM, IMOB_CTBL_NEW.IDDEBCREDCTBLOP, V_NRSEQCTBLIT, IMOB_CTBL_NEW.VALORCTBL,
                                 V_DTOPERA, P_CDOPERADOR, V_DTOPERA, P_CDOPERADOR,
                                 IMOB_CTBL_NEW.CDHISTPADR,V_CDFILIAL,V_CDCENTCUST);
                      END IF;
                  END LOOP;
                  FOR DEPR_CTBL_ANT IN (SELECT T.CDTIPOCONTABEM, T.NRSEQCTBLOP, T.IDDEBCREDCTBLOP, T.IDVALORCTBLOP, CT.CDHISTPADR,
                                               BA.CDFILIAL, BA.CDCENTCUST, CT.CDCONTCTBLGRBEM,
                                               DECODE(T.IDDEBCREDCTBLOP, 'C', IT.VRITCAPAOPERA, IT.VRITCAPAOPERA*(-1)) AS VALORCTBL,
                                               NVL(DECODE(T.IDDEBCREDCTBLOP, 'D', 'C','D'),'') AS TIPOCTBL, BA.CDBEMATIVO, IT.NRSEQITCAPAOPE
                                          FROM TPCTTIPOOPERBEM T, CTBLGRUPOBEM CT, BEMATIVO BA, (SELECT CDGRUPOITCANTE AS CDGRUPOBEM, CDEMPRESA, CDBEMATIVO,
                                                                                                        QTMESDEPITCANTE AS QTMESDEP, VRITCAPAOPERA, NRSEQITCAPAOPE
                                                                                                   FROM ITCAPAOPERABEM
                                                                                                  WHERE NRSEQCAPAOPEBEM = V_NRSEQOPERACO
                                                                                                    AND DSOBSITCAPAOPER = 'DEPRECIACAO'
                                                                                                    AND VRITCAPAOPERA < 0
                                                                                                    AND CDEMPRESA       = P_CDEMPRESA) IT
                                         WHERE T.CDEMPRESA   = P_CDEMPRESA
                                           AND CT.CDEMPRESA  = T.CDEMPRESA
                                           AND IT.CDEMPRESA  = T.CDEMPRESA
                                           AND T.CDTIPOOPERBEM  = V_TIPOOPERDEPR
                                           AND CT.CDTIPOCONTABEM = T.CDTIPOCONTABEM
                                           AND CT.CDGRUPOBEM = IT.CDGRUPOBEM
                                           AND BA.CDEMPRESA  = T.CDEMPRESA
                                           AND BA.CDBEMATIVO = IT.CDBEMATIVO
                                        ORDER BY NRSEQCTBLOP) LOOP
                      V_EXISRATE := FALSE;
                      FOR C_RATEIOBEM IN (
                          SELECT CDFILICCUST, CDCENTCUSBEM, VRRATEBEM
                            FROM RATEIOBEM R
                           WHERE R.CDEMPRESA   = P_CDEMPRESA
                             AND R.CDBEMATIVO  = DEPR_CTBL_ANT.CDBEMATIVO) LOOP
                           V_VALORCTBLRATE := ROUND(((DEPR_CTBL_ANT.VALORCTBL *C_RATEIOBEM.VRRATEBEM)/100),2);
                          IF V_NRSEQCTBLITRAT IS NULL THEN
                             V_NRSEQCTBLITRAT := '001';
                          ELSE
                             V_NRSEQCTBLITRAT := LPAD(TO_CHAR(TO_NUMBER(V_NRSEQCTBLITRAT)+1),3,'0');
                          END IF;
                          INSERT INTO CTBLITCAPAOPER
                                     (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                      CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                      DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                      CDHISTPADR, CDFILIAL, CDCENTCUST)
                          VALUES
                                     (P_CDEMPRESA,     V_IDSIMULOPERABEM, V_NRSEQOPERACO,  DEPR_CTBL_ANT.NRSEQITCAPAOPE,
                                     DEPR_CTBL_ANT.CDCONTCTBLGRBEM, DEPR_CTBL_ANT.TIPOCTBL, V_NRSEQCTBLITRAT, V_VALORCTBLRATE,
                                     V_DTOPERA, P_CDOPERADOR, V_DTOPERA, P_CDOPERADOR,
                                     DEPR_CTBL_ANT.CDHISTPADR,C_RATEIOBEM.CDFILICCUST,C_RATEIOBEM.CDCENTCUSBEM);
                           V_EXISRATE := TRUE;
                      END LOOP;
                      IF V_EXISRATE = FALSE THEN
                         BEGIN
                           SELECT BEMATIVO.CDFILIAL, BEMATIVO.CDCENTCUST
                             INTO V_CDFILIAL, V_CDCENTCUST
                             FROM BEMATIVO
                            WHERE BEMATIVO.CDBEMATIVO = DEPR_CTBL_ANT.CDBEMATIVO
                              AND BEMATIVO.CDEMPRESA = P_CDEMPRESA;
                         EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                             V_CDFILIAL   := NULL;
                             V_CDCENTCUST := NULL;
                         END;
                         IF V_NRSEQCTBLIT IS NULL THEN
                            V_NRSEQCTBLIT := '001';
                         ELSE
                            V_NRSEQCTBLIT := LPAD(TO_CHAR(TO_NUMBER(V_NRSEQCTBLIT)+1),03,'0');
                         END IF;
                         INSERT INTO CTBLITCAPAOPER
                                (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                 CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                 DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                 CDHISTPADR, CDFILIAL, CDCENTCUST)
                         VALUES
                                (P_CDEMPRESA,     V_IDSIMULOPERABEM, V_NRSEQOPERACO,  DEPR_CTBL_ANT.NRSEQITCAPAOPE,
                                 DEPR_CTBL_ANT.CDCONTCTBLGRBEM, DEPR_CTBL_ANT.TIPOCTBL, V_NRSEQCTBLIT, DEPR_CTBL_ANT.VALORCTBL,
                                 V_DTOPERA, P_CDOPERADOR, V_DTOPERA, P_CDOPERADOR,
                                 DEPR_CTBL_ANT.CDHISTPADR,V_CDFILIAL,V_CDCENTCUST);
                      END IF;
               END LOOP;
               FOR DEPR_CTBL_NEW IN (SELECT T.CDTIPOCONTABEM, T.NRSEQCTBLOP, T.IDDEBCREDCTBLOP, T.IDVALORCTBLOP, CT.CDHISTPADR,
                                           BA.CDFILIAL, BA.CDCENTCUST, CT.CDCONTCTBLGRBEM,
                                           DECODE(T.IDDEBCREDCTBLOP, 'C', IT.VRITCAPAOPERA , VRITCAPAOPERA*(-1)) AS VALORCTBL,
                                           T.IDDEBCREDCTBLOP AS TIPOCTBL, BA.CDBEMATIVO, IT.NRSEQITCAPAOPE
                                      FROM TPCTTIPOOPERBEM T, CTBLGRUPOBEM CT, BEMATIVO BA, (SELECT CDGRUPOITCNOVO AS CDGRUPOBEM, CDEMPRESA, CDBEMATIVO,
                                                                                                    QTMESDEPITCNOVO AS QTMESDEP, VRITCAPAOPERA, NRSEQITCAPAOPE
                                                                                               FROM ITCAPAOPERABEM
                                                                                              WHERE NRSEQCAPAOPEBEM = V_NRSEQOPERACO
                                                                                                AND DSOBSITCAPAOPER = 'DEPRECIACAO'
                                                                                                AND VRITCAPAOPERA > 0
                                                                                                AND CDEMPRESA       = P_CDEMPRESA) IT
                                     WHERE T.CDEMPRESA   = P_CDEMPRESA
                                       AND CT.CDEMPRESA  = T.CDEMPRESA
                                       AND IT.CDEMPRESA  = T.CDEMPRESA
                                       AND T.CDTIPOOPERBEM  = V_TIPOOPERDEPR
                                       AND CT.CDTIPOCONTABEM = T.CDTIPOCONTABEM
                                       AND CT.CDGRUPOBEM = IT.CDGRUPOBEM
                                       AND BA.CDEMPRESA  = T.CDEMPRESA
                                       AND BA.CDBEMATIVO = IT.CDBEMATIVO
                                    ORDER BY NRSEQCTBLOP) LOOP
                      V_EXISRATE := FALSE;
                      FOR C_RATEIOBEM IN (
                          SELECT CDFILICCUST, CDCENTCUSBEM, VRRATEBEM
                            FROM RATEIOBEM R
                           WHERE R.CDEMPRESA   = P_CDEMPRESA
                             AND R.CDBEMATIVO  = DEPR_CTBL_NEW.CDBEMATIVO) LOOP
                           V_VALORCTBLRATE := ROUND(((DEPR_CTBL_NEW.VALORCTBL *C_RATEIOBEM.VRRATEBEM)/100),2);
                          IF V_NRSEQCTBLITRAT IS NULL THEN
                             V_NRSEQCTBLITRAT := '001';
                          ELSE
                             V_NRSEQCTBLITRAT := LPAD(TO_CHAR(TO_NUMBER(V_NRSEQCTBLITRAT)+1),3,'0');
                          END IF;
                          INSERT INTO CTBLITCAPAOPER
                                 (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                  CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                  DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                  CDHISTPADR, CDFILIAL, CDCENTCUST)
                          VALUES
                                 (P_CDEMPRESA,     V_IDSIMULOPERABEM, V_NRSEQOPERACO ,   DEPR_CTBL_NEW.NRSEQITCAPAOPE,
                                  DEPR_CTBL_NEW.CDCONTCTBLGRBEM, DEPR_CTBL_NEW.TIPOCTBL, V_NRSEQCTBLITRAT, V_VALORCTBLRATE,
                                  V_DTOPERA, P_CDOPERADOR, V_DTOPERA, P_CDOPERADOR,
                                  DEPR_CTBL_NEW.CDHISTPADR,C_RATEIOBEM.CDFILICCUST,C_RATEIOBEM.CDCENTCUSBEM);
                           V_EXISRATE := TRUE;
                      END LOOP;
                      IF V_EXISRATE = FALSE THEN
                         BEGIN
                           SELECT BEMATIVO.CDFILIAL, BEMATIVO.CDCENTCUST
                             INTO V_CDFILIAL, V_CDCENTCUST
                             FROM BEMATIVO
                            WHERE BEMATIVO.CDBEMATIVO = DEPR_CTBL_NEW.CDBEMATIVO
                              AND BEMATIVO.CDEMPRESA = P_CDEMPRESA;
                         EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                             V_CDFILIAL   := NULL;
                             V_CDCENTCUST := NULL;
                         END;
                         IF V_NRSEQCTBLIT IS NULL THEN
                            V_NRSEQCTBLIT := '001';
                         ELSE
                            V_NRSEQCTBLIT := LPAD(TO_CHAR(TO_NUMBER(V_NRSEQCTBLIT)+1),03,'0');
                         END IF;
                         INSERT INTO CTBLITCAPAOPER
                                (CDEMPRESA,    IDSIMULOPERABEM, NRSEQCAPAOPEBEM, NRSEQITCAPAOPE,
                                 CDCONTCTBL,   IDTIPOOPERCONT,  NRSEQCTBLITCAPA, VRCONTCTBLITC,
                                 DTINCCTBLITC, CDOPEINCCTBLITC, DTATUOPECTBLITC, CDOPEATUCTBLITC,
                                 CDHISTPADR, CDFILIAL, CDCENTCUST)
                         VALUES
                                (P_CDEMPRESA,     V_IDSIMULOPERABEM, V_NRSEQOPERACO ,   DEPR_CTBL_NEW.NRSEQITCAPAOPE,
                                 DEPR_CTBL_NEW.CDCONTCTBLGRBEM, DEPR_CTBL_NEW.TIPOCTBL, V_NRSEQCTBLIT, DEPR_CTBL_NEW.VALORCTBL,
                                 V_DTOPERA, P_CDOPERADOR, V_DTOPERA, P_CDOPERADOR,
                                 DEPR_CTBL_NEW.CDHISTPADR,V_CDFILIAL,V_CDCENTCUST);
                      END IF;
                  END LOOP;
                  UPDATE CAPAOPERABEM
                     SET NRSEQFECHABEM = V_NRSEQFECHACO
                   WHERE NRSEQCAPAOPEBEM = V_NRSEQOPERACO
                     AND P_CDEMPRESA     = CDEMPRESA
                     AND P_IDSIMULACAO   = IDSIMULOPERABEM;
                  V_NRSEQFECHATC := V_NRSEQFECHACO;
                  BEGIN
                    SELECT MIN(DTCAPAOPEBEM)
                      INTO V_DTINICLOTE
                      FROM CAPAOPERABEM
                     WHERE P_CDEMPRESA = CAPAOPERABEM.CDEMPRESA
                       AND ((V_NRSEQFECHACO = CAPAOPERABEM.NRSEQFECHABEM) OR (V_NRSEQOPERACO = CAPAOPERABEM.NRSEQCAPAOPEBEM));
                  EXCEPTION
                   WHEN NO_DATA_FOUND THEN
                     V_DTINICLOTE := NULL;
                  END;
                  NOVO_CODIGO('GPA' || TO_CHAR(V_DTINICLOTE,  'MMYYYY'), V_CONTADOR);
                  V_CDLOTE := ('GPA' || TO_CHAR(V_DTINICLOTE,  'MMYYYY') || SUBSTR(V_CONTADOR, 18, 3));
                  V_DTINICLOTE := TO_DATE('01'|| TO_CHAR(V_DTINICLOTE,  'MMYYYY'));
                  V_DTFINALOTE := LAST_DAY(V_DTINICLOTE);
                  INSERT INTO CAPALOTE
                         (CDEMPRESA,CDLOTE,DTINICLOTE,DTFINALOTE,IDLOTE,QTERROLOTE,
                          VRTOTAINFO,QTLANCINFO,VRTOTADIGI,QTLANCDIGI,VRTOTDBDIGI,
                          VRTOTCRDIGI,VRDIFTOTAL,VRDIFDEBCRE,DSOBSELOTE,DTINICDIFE)
                  VALUES
                         (P_CDEMPRESA, V_CDLOTE, V_DTOPERA, V_DTFINALOTE, 'D', NULL,
                          NULL, NULL, NULL, NULL, NULL,
                          NULL, NULL, NULL, SUBSTR('CONTAB. GESTAO DE ATIVOS', 1, 30), NULL);
                  V_NRSEQLANLOTE := 1;
                  NOVO_CODIGO('LANCLOTE'||P_CDEMPRESA||V_CDLOTE, V_NRLANCLOTE , 1);
                  V_NRLANCL := SUBSTR(V_NRLANCLOTE, 15, 6);
                  BEGIN
                    SELECT IDAGRUPCONTATIV
                      INTO V_IDAGRUPCONTATIV
                      FROM PARAMEMPRESA
                     WHERE CDEMPRESA = P_CDEMPRESA;
                  EXCEPTION
                   WHEN NO_DATA_FOUND THEN
                     V_IDAGRUPCONTATIV := NULL;
                  END;
--                  IF V_IDAGRUPCONTATIV = 'S' THEN
    --                 OPEN C_CONTAGRUPTC;
  --                ELSE
                     OPEN C_CONTTRNSCO;
            --      END IF;
                  LOOP
                 /*    IF V_IDAGRUPCONTATIV = 'S' THEN
                        FETCH C_CONTAGRUPTC INTO V_CDCONTCTBL,   V_IDTIPOOPERCONT,
                                                 V_DTINCCTBLITC, V_NRSEQCAPAOPEBEM,
                                                 V_CDEMPRESA,    V_VRCTBL,
                                                 V_IDSIMULOPERABEM,
                                                 V_NRSEQOPERABEM,V_HISTPADRAO,V_DSHISTPADR,
                                                 V_CDFILIAL,V_CDCENTCUST;
                        EXIT WHEN C_CONTAGRUPTC%NOTFOUND;
                     ELSE    */
                        FETCH C_CONTTRNSCO INTO V_CDCONTCTBL,   V_IDTIPOOPERCONT,
                                                V_DTINCCTBLITC, V_NRSEQCAPAOPEBEM,
                                                V_CDEMPRESA,    V_VRCTBL,
                                                V_CDBEMATIVO,   V_IDSIMULOPERABEM,
                                                V_NRSEQOPERABEM,V_HISTPADRAO,V_DSHISTPADR,
                                                V_CDFILIAL, V_CDCENTCUST,V_NMFORNECED,V_NMBEM,V_CDPATRIBEM,V_CDBEM,V_NRNOTAFISC, V_CDFORNECED;
                        EXIT WHEN C_CONTTRNSCO%NOTFOUND;
                        V_DSHISTPADR := REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(V_DSHISTPADR,'[FIL]',V_CDFILIAL),'[DFEM]',TO_CHAR(V_DTFINALOTE,'MM/YYYY')),'[RSF]',V_NMFORNECED),'[NBEM]',V_NMBEM),'[PATR]',V_CDPATRIBEM),'[BEM]',V_CDBEM),'[DOC]',V_NRNOTAFISC);
                   --  END IF;
                    /* IF V_IDAGRUPCONTATIV = 'S' THEN
                        IF V_CDFILIANT <> V_CDFILIAL THEN
                            NOVA_PARTIDA(P_CDEMPRESA,V_CDLOTE,V_DTFINALOTE,V_NRLANCL, V_CDCONTCTBL, V_HISTPADRAO,
                                        V_DSHISTPADR, ROUND(V_VRCTBL,2), '', '','', V_CDLOTE, 'N');
                        ELSE
                           NOVA_PARTIDA(P_CDEMPRESA,V_CDLOTE,V_DTFINALOTE,V_NRLANCL, V_CDCONTCTBL, V_HISTPADRAO,
                                        V_DSHISTPADR, ROUND(V_VRCTBL,2), '', '','', V_CDLOTE, 'S');
                        END IF;
                     ELSE    */
                           NOVA_PARTIDA(P_CDEMPRESA,V_CDLOTE,V_DTFINALOTE,V_NRLANCL, V_CDCONTCTBL, V_HISTPADRAO,
                                        V_DSHISTPADR, ROUND(V_VRCTBL,2), 'F', V_CDFORNECED,'', V_CDLOTE, 'N');
                           SELECT IDCONTCCUS INTO V_IDCONTCCUS
                             FROM CONTCTBL
                            WHERE CDCONTCTBL = V_CDCONTCTBL;
                           IF V_IDCONTCCUS <> 'O' THEN
                              RATEIO_PARTIDA(P_CDEMPRESA,V_CDLOTE,V_DTFINALOTE,V_NRLANCL,V_CDFILIAL,
                                             V_CDCENTCUST, ROUND(V_VRCTBL, 2));
                           END IF;
                   --  END IF;
                  END LOOP;
                /*  IF V_IDAGRUPCONTATIV = 'S' THEN
                     CLOSE C_CONTAGRUPTC;
                  ELSE*/
                     CLOSE C_CONTTRNSCO;
               --   END IF;
                  V_NRSEQFECHATC := '';
                  UPDATE CAPAOPERABEM
                     SET IDSITCAPAOPER = '4'
                   WHERE V_NRSEQFECHACO = NRSEQFECHABEM
                     AND P_CDEMPRESA     = CDEMPRESA
                     AND P_IDSIMULACAO   = IDSIMULOPERABEM;
                  UPDATE FECHAMENTOBEM
                     SET IDSITFECHABEM = '4',
                         DTCTBFECHABEM = V_DTOPERA,
                         CDOPCTBFECHABEM = P_CDOPERADOR
                   WHERE V_NRSEQFECHACO = NRSEQFECHABEM
                     AND P_CDEMPRESA = CDEMPRESA
                     AND P_IDSIMULACAO = IDSIMULFECHABEM;
                  UPDATE OPERACAOBEM
                     SET CDLOTE = V_CDLOTE,
                         IDSITOPERABEM = '2'
                   WHERE P_CDEMPRESA        = CDEMPRESA
                     AND V_CDBEMATIVO       = CDBEMATIVO
                     AND P_IDSIMULACAO      = IDSIMULOPERACAO
                     AND V_NRSEQOPERABEMCO  = NRSEQOPERABEM;
                  UPDATE CAPAOPERABEM
                     SET IDSITCAPAOPER = '4'
                   WHERE P_NRSEQCAPAOPEBEM = NRSEQCAPAOPEBEM
                     AND P_CDEMPRESA     = CDEMPRESA
                     AND P_IDSIMULACAO   = IDSIMULOPERABEM;
               ELSE
                  IF V_EXISTE_IMOB = 0 THEN
                     P_RETORNO := '03'||V_TIPOOPERIMOB;
                     EXIT;
                  END IF;
                  IF V_EXISTE_DEPR = 0 THEN
                     P_RETORNO := '03'||V_TIPOOPERDEPR;
                     EXIT;
                  END IF;
               END IF;
            END IF;
         END IF;
      END LOOP;
      IF V_EXISTE_OPER = 'N' THEN
         P_RETORNO := '02';
      END IF;
   END IF;
   IF P_CARGAFECHA = 'P' THEN
      BEGIN
        SELECT MIN(DTCAPAOPEBEM)
          INTO V_DTINICLOTE
          FROM CAPAOPERABEM
         WHERE P_CDEMPRESA = CAPAOPERABEM.CDEMPRESA
           AND ((P_NRSEQFECHABEM = CAPAOPERABEM.NRSEQFECHABEM) OR (P_NRSEQCAPAOPEBEM = CAPAOPERABEM.NRSEQCAPAOPEBEM));
      EXCEPTION
       WHEN NO_DATA_FOUND THEN
         V_DTINICLOTE := NULL;
      END;
      NOVO_CODIGO('GPA' || TO_CHAR(V_DTINICLOTE,  'MMYYYY'), V_CONTADOR);
      V_CDLOTE := ('GPA' || TO_CHAR(V_DTINICLOTE,  'MMYYYY') || SUBSTR(V_CONTADOR, 18, 3));
      V_DTINICLOTE := TO_DATE('01'|| TO_CHAR(V_DTINICLOTE,  'MMYYYY'));
      V_DTFINALOTE := LAST_DAY(V_DTINICLOTE);
      INSERT INTO CAPALOTE
             (CDEMPRESA,CDLOTE,DTINICLOTE,DTFINALOTE,IDLOTE,QTERROLOTE,
              VRTOTAINFO,QTLANCINFO,VRTOTADIGI,QTLANCDIGI,VRTOTDBDIGI,
              VRTOTCRDIGI,VRDIFTOTAL,VRDIFDEBCRE,DSOBSELOTE,DTINICDIFE)
      VALUES
             (P_CDEMPRESA, V_CDLOTE, V_DTINICLOTE, V_DTFINALOTE, 'D', NULL,
              NULL, NULL, NULL, NULL, NULL,
              NULL, NULL, NULL, SUBSTR('CONTAB. GESTAO DE ATIVOS', 1, 30), NULL);
      V_NRSEQLANLOTE := 1;
      NOVO_CODIGO('LANCLOTE'||P_CDEMPRESA||V_CDLOTE, V_NRLANCLOTE , 1);
      V_NRLANCL := SUBSTR(V_NRLANCLOTE, 15, 6);
      BEGIN
        SELECT IDAGRUPCONTATIV
          INTO V_IDAGRUPCONTATIV
          FROM PARAMEMPRESA
         WHERE CDEMPRESA = P_CDEMPRESA;
      EXCEPTION
       WHEN NO_DATA_FOUND THEN
         V_IDAGRUPCONTATIV := NULL;
      END;
      IF V_IDAGRUPCONTATIV = 'S' THEN
         IF P_IDOPERACAO = 'F'  THEN
            OPEN C_CONTAGRUP;
         ELSE
            OPEN C_CONTTRANS;
         END IF;
      ELSE
         IF P_IDOPERACAO = 'F' THEN
            OPEN C_CONTFECHA;
         ELSE
            OPEN C_CONTTRANS;
         END IF;
      END IF;
      LOOP
         IF V_IDAGRUPCONTATIV = 'S' THEN
            IF P_IDOPERACAO = 'F'  THEN
               FETCH C_CONTAGRUP INTO V_CDCONTCTBL,   V_IDTIPOOPERCONT,
                                      V_DTINCCTBLITC, V_NRSEQCAPAOPEBEM,
                                      V_CDEMPRESA,    V_VRCTBL,
                                      V_IDSIMULOPERABEM,
                                      V_NRSEQOPERABEM,V_HISTPADRAO,V_DSHISTPADR,
                                      V_CDFILIAL,V_CDCENTCUST,V_CDFORNECED;
               EXIT WHEN C_CONTAGRUP%NOTFOUND;
               V_DSHISTPADR := REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(V_DSHISTPADR,'[FIL]',V_CDFILIAL),'[DFEM]',TO_CHAR(V_DTFINALOTE,'MM/YYYY')),'[RSF]',V_NMFORNECED),'[NBEM]',V_NMBEM),'[PATR]',V_CDPATRIBEM),'[BEM]',V_CDBEM),'[DOC]',NVL(V_NRNOTAFISC,'[DOC]'));
            ELSE
               FETCH C_CONTTRANS INTO V_CDCONTCTBL,   V_IDTIPOOPERCONT,
                                      V_DTINCCTBLITC, V_NRSEQCAPAOPEBEM,
                                      V_CDEMPRESA,    V_VRCTBL,
                                      V_CDBEMATIVO,   V_IDSIMULOPERABEM,
                                      V_NRSEQOPERABEM,V_CDFILIAL,V_CDCENTCUST,V_HISTPADRAO,V_DSHISTPADR,V_CDPATRIBEM,V_NMBEM,V_NRNOTAFIOPERA,V_NMCOMPRZSCLI,V_NRINSESTCLIE,V_CDFORNECED;
               EXIT WHEN C_CONTTRANS%NOTFOUND;
               V_DSHISTPADR := REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(V_DSHISTPADR,'[FIL]',V_CDFILIAL),'[DFEM]',TO_CHAR(V_DTFINALOTE,'MM/YYYY')),'[RSF]',V_NMFORNECED),'[NBEM]',V_NMBEM),'[PATR]',V_CDPATRIBEM),'[BEM]',V_CDBEM),'[DOC]',V_NRNOTAFIOPERA),'[CLI]',V_NRINSESTCLIE),'[RSC]',V_NMCOMPRZSCLI);
            END IF;
         ELSE
            IF P_IDOPERACAO = 'F'  THEN
               FETCH C_CONTFECHA INTO V_CDCONTCTBL,   V_IDTIPOOPERCONT,
                                      V_DTINCCTBLITC, V_NRSEQCAPAOPEBEM,
                                      V_CDEMPRESA,    V_VRCTBL,
                                      V_CDBEMATIVO,   V_IDSIMULOPERABEM,
                                      V_NRSEQOPERABEM,V_HISTPADRAO,V_DSHISTPADR,
                                      V_CDFILIAL, V_CDCENTCUST,V_NMFORNECED,V_NMBEM,V_CDPATRIBEM,V_CDBEM,V_NRNOTAFISC,V_CDFORNECED;
               EXIT WHEN C_CONTFECHA%NOTFOUND;
               V_DSHISTPADR := REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(V_DSHISTPADR,'[FIL]',V_CDFILIAL),'[DFEM]',TO_CHAR(V_DTFINALOTE,'MM/YYYY')),'[RSF]',V_NMFORNECED),'[NBEM]',V_NMBEM),'[PATR]',V_CDPATRIBEM),'[BEM]',V_CDBEM),'[DOC]',NVL(V_NRNOTAFISC,'[DOC]'));
            ELSE
               FETCH C_CONTTRANS INTO V_CDCONTCTBL,   V_IDTIPOOPERCONT,
                                      V_DTINCCTBLITC, V_NRSEQCAPAOPEBEM,
                                      V_CDEMPRESA,    V_VRCTBL,
                                      V_CDBEMATIVO,   V_IDSIMULOPERABEM,
                                      V_NRSEQOPERABEM,V_CDFILIAL,V_CDCENTCUST,V_HISTPADRAO,V_DSHISTPADR,V_CDPATRIBEM,V_NMBEM,V_NRNOTAFIOPERA,V_NMCOMPRZSCLI,V_NRINSESTCLIE,V_CDFORNECED;
               EXIT WHEN C_CONTTRANS%NOTFOUND;
               V_DSHISTPADR := REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(V_DSHISTPADR,'[FIL]',V_CDFILIAL),'[DFEM]',TO_CHAR(V_DTFINALOTE,'MM/YYYY')),'[RSF]',V_NMFORNECED),'[NBEM]',V_NMBEM),'[PATR]',V_CDPATRIBEM),'[BEM]',V_CDBEM),'[DOC]',V_NRNOTAFIOPERA),'[CLI]',V_NRINSESTCLIE),'[RSC]',V_NMCOMPRZSCLI);
            END IF;
         END IF;
         IF V_IDTIPOOPERCONT = 'D' THEN
            V_VRCTBL := -V_VRCTBL;
         END IF;
          IF V_IDAGRUPCONTATIV = 'S' THEN
            IF V_CDFILIANT <> V_CDFILIAL THEN
                NOVA_PARTIDA(P_CDEMPRESA,V_CDLOTE,V_DTFINALOTE,V_NRLANCL, V_CDCONTCTBL, V_HISTPADRAO,
                            V_DSHISTPADR, ROUND(V_VRCTBL,2), 'F', V_CDFORNECED,'', V_CDLOTE, 'N');
            ELSE
               NOVA_PARTIDA(P_CDEMPRESA,V_CDLOTE,V_DTFINALOTE,V_NRLANCL, V_CDCONTCTBL, V_HISTPADRAO,
                            V_DSHISTPADR, ROUND(V_VRCTBL,2), 'F', V_CDFORNECED,'', V_CDLOTE, 'S');
        END IF;
     ELSE
               NOVA_PARTIDA(P_CDEMPRESA,V_CDLOTE,V_DTFINALOTE,V_NRLANCL, V_CDCONTCTBL, V_HISTPADRAO,
                            V_DSHISTPADR, ROUND(V_VRCTBL,2), 'F', V_CDFORNECED,'', V_CDLOTE, 'N');
     END IF;
         SELECT IDCONTCCUS INTO V_IDCONTCCUS
           FROM CONTCTBL
          WHERE CDCONTCTBL = V_CDCONTCTBL;
         IF V_IDCONTCCUS <> 'O' THEN
            RATEIO_PARTIDA(P_CDEMPRESA,V_CDLOTE,V_DTFINALOTE,V_NRLANCL,V_CDFILIAL,
                           V_CDCENTCUST, ROUND(V_VRCTBL, 2));
         END IF;
         V_NRSEQLANLOTE := V_NRSEQLANLOTE + 1;
         IF V_IDAGRUPCONTATIV = 'S' THEN
            IF P_IDOPERACAO = 'F'  THEN
               FOR C_BEMATIVO IN(
                   SELECT DISTINCT ITCAPAOPERABEM.CDBEMATIVO,NRSEQOPERABEM
                          FROM CTBLITCAPAOPER, ITCAPAOPERABEM,HISTPADR, BEMATIVO B
                          WHERE EXISTS (SELECT CAPAOPERABEM.CDEMPRESA,ITCAPAOPERABEM.IDSIMULOPERABEM,
                                               CAPAOPERABEM.NRSEQCAPAOPEBEM, ITCAPAOPERABEM.NRSEQITCAPAOPE
                                          FROM CAPAOPERABEM, ITCAPAOPERABEM
                                         WHERE P_CDEMPRESA                    = CAPAOPERABEM.CDEMPRESA
                                           AND P_NRSEQFECHABEM                = CAPAOPERABEM.NRSEQFECHABEM
                                           AND CAPAOPERABEM.CDEMPRESA         = ITCAPAOPERABEM.CDEMPRESA
                                           AND CAPAOPERABEM.NRSEQCAPAOPEBEM   = ITCAPAOPERABEM.NRSEQCAPAOPEBEM
                                           AND ITCAPAOPERABEM.CDEMPRESA       = CTBLITCAPAOPER.CDEMPRESA
                                           AND ITCAPAOPERABEM.NRSEQCAPAOPEBEM = CTBLITCAPAOPER.NRSEQCAPAOPEBEM
                                           AND ITCAPAOPERABEM.NRSEQITCAPAOPE  = CTBLITCAPAOPER.NRSEQITCAPAOPE)
                            AND ITCAPAOPERABEM.CDEMPRESA       = CTBLITCAPAOPER.CDEMPRESA
                            AND ITCAPAOPERABEM.IDSIMULOPERABEM = CTBLITCAPAOPER.IDSIMULOPERABEM
                            AND ITCAPAOPERABEM.NRSEQCAPAOPEBEM = CTBLITCAPAOPER.NRSEQCAPAOPEBEM
                            AND ITCAPAOPERABEM.NRSEQITCAPAOPE  = CTBLITCAPAOPER.NRSEQITCAPAOPE
                            AND CTBLITCAPAOPER.CDHISTPADR      = HISTPADR.CDHISTPADR
                            AND ITCAPAOPERABEM.CDEMPRESA       = B.CDEMPRESA
                            AND ITCAPAOPERABEM.CDBEMATIVO      = B.CDBEMATIVO) LOOP
                   UPDATE OPERACAOBEM
                      SET CDLOTE = V_CDLOTE,
                          IDSITOPERABEM = '2'
                    WHERE V_CDEMPRESA              = CDEMPRESA
                      AND C_BEMATIVO.CDBEMATIVO    = CDBEMATIVO
                      AND V_IDSIMULOPERABEM        = IDSIMULOPERACAO
                      AND C_BEMATIVO.NRSEQOPERABEM = NRSEQOPERABEM;
               END LOOP;
            ELSE
               UPDATE OPERACAOBEM
                  SET CDLOTE = V_CDLOTE,
                      IDSITOPERABEM = '2'
                WHERE V_CDEMPRESA            = CDEMPRESA
                  AND V_CDBEMATIVO           = CDBEMATIVO
                  AND V_IDSIMULOPERABEM      = IDSIMULOPERACAO
                  AND V_NRSEQOPERABEM        = NRSEQOPERABEM;
            END IF;
         ELSE
            UPDATE OPERACAOBEM
               SET CDLOTE = V_CDLOTE,
                   IDSITOPERABEM = '2'
             WHERE V_CDEMPRESA            = CDEMPRESA
               AND V_CDBEMATIVO           = CDBEMATIVO
               AND V_IDSIMULOPERABEM      = IDSIMULOPERACAO
               AND V_NRSEQOPERABEM        = NRSEQOPERABEM;
         END IF;
         V_CDFILIANT := V_CDFILIAL;
      END LOOP;
      SELECT COUNT(*) INTO V_LANCLOTE
        FROM LANCLOTE
       WHERE P_CDEMPRESA = CDEMPRESA
         AND V_CDLOTE = CDLOTE;
      IF V_LANCLOTE > 0 THEN
         IF P_IDOPERACAO = 'F' THEN
            UPDATE CAPAOPERABEM
               SET IDSITCAPAOPER = '4'
             WHERE P_NRSEQFECHABEM = NRSEQFECHABEM
               AND P_CDEMPRESA     = CDEMPRESA
               AND P_IDSIMULACAO   = IDSIMULOPERABEM;
            UPDATE FECHAMENTOBEM
               SET IDSITFECHABEM = '4',
                   DTCTBFECHABEM = SYSDATE,
                   CDOPCTBFECHABEM = P_CDOPERADOR
             WHERE P_NRSEQFECHABEM = NRSEQFECHABEM
               AND P_CDEMPRESA = CDEMPRESA
               AND P_IDSIMULACAO = IDSIMULFECHABEM;
          ELSE
            UPDATE CAPAOPERABEM
               SET IDSITCAPAOPER = '4'
             WHERE P_NRSEQCAPAOPEBEM = NRSEQCAPAOPEBEM
               AND P_CDEMPRESA     = CDEMPRESA
               AND P_IDSIMULACAO   = IDSIMULOPERABEM;
          END IF;
      ELSE
         DELETE FROM CAPALOTE
          WHERE CDLOTE = V_CDLOTE
            AND CDEMPRESA = P_CDEMPRESA;
      END IF;
   END IF;
END;
/