CREATE OR REPLACE PROCEDURE DEMONSTRATIVO_CTBO(
   P_CDEMPRESA IN VARCHAR2, -- CODIGO DA EMPRESA
   P_CDTIPODEMO IN TIPODEMO.CDTIPODEMO%TYPE, -- CODIGO DO DEMONSTRATIVO
   P_IDSALDO IN VARCHAR, -- TIPO DO SALDO, ACUMULADO OU PERIODO
   P_IDDESCENCER IN VARCHAR, -- DESCONSIDERA ENCERRAMENTO DE EXERC
   P_DATAA IN DATE, -- DATA 1 QUANDO ACUMULADO
   P_DATAB IN DATE, -- DATA 2 QUANDO ACUMULADO
   P_DATAAINTERA IN DATE, -- DATA INICIAL DO PRIMEIRO INTERVALO QUANDO PERIODO
   P_DATABINTERA IN DATE, -- DATA FINAL DO PRIMEIRO INTERVALO  QUANDO PERIODO
   P_DATAAINTERB IN DATE, -- DATA INICIAL DO SEGUNDO INTERVALO QUANDO PERIODO
   P_DATABINTERB IN DATE, -- DATA FINAL DO SEGUNDO INTERVALO QUANDO PERIODO
   P_NRDEMO IN OUT NUMBER)
AS
   V_TEMP VARCHAR2(20);
   V_NRDEMOCTBTEMP INT;
   V_COUNT NUMBER;
   V_VRTEMP FLOAT;
   V_VRTEMP2 FLOAT;
   V_CDEMPRESA VARCHAR2(02);

   V_DATASALDOA DATE;
   V_DATASALDOB DATE;

   V_DATAINIA DATE;
   V_DATAFINA DATE;

   V_DATAINIB DATE;
   V_DATAFINB DATE;

   V_VRPORC1 FLOAT;
   V_VRPORC2 FLOAT;
BEGIN
 --  NOVO_CODIGO_NT('DEMOCTBOFICIAL', V_TEMP);
   V_NRDEMOCTBTEMP := TO_NUMBER(P_NRDEMO);

   /* FILTRA A EMPRESA */
   IF LENGTH(P_CDEMPRESA) > 2 THEN
      V_CDEMPRESA := 'XX';
   ELSE
      IF P_CDEMPRESA = 'T' THEN
         V_CDEMPRESA := 'T';
      ELSE
         V_CDEMPRESA := P_CDEMPRESA;
      END IF;
   END IF;

   IF P_IDDESCENCER = 'N' THEN
      IF P_IDSALDO = 'A' THEN
         V_DATASALDOA := LAST_DAY(P_DATAA);
         V_DATASALDOB := LAST_DAY(P_DATAB);
         FOR C_AUX IN (
            SELECT I.CDTIPODEMO, I.CDITEMSTRU, I.IDORIGVALOR
              FROM ITEMDEMOF I
             WHERE I.CDTIPODEMO = P_CDTIPODEMO
            ORDER BY I.CDTIPODEMO, I.CDITEMSTRU) LOOP
               V_VRTEMP := NULL;
               V_VRTEMP2 := NULL;
               IF C_AUX.IDORIGVALOR <> 'I' THEN
                  SELECT NVL(SUM(VRSALDOITEM), 0)
                    INTO V_VRTEMP
                    FROM (SELECT NVL(SUM(ROUND(S.VRSALDEMPR, 2)),0) AS VRSALDOITEM
                            FROM SALDEMPR S, ITEMAPUCTB I
                           WHERE ((INSTR(P_CDEMPRESA, S.CDEMPRESA) > 0) OR ('T' = P_CDEMPRESA))
                             AND S.DTSALDEMPR = V_DATASALDOA
                             AND S.CDCONTCTBL = I.CDORIGDEMO
                             AND I.CDTIPODEMO = C_AUX.CDTIPODEMO
                             AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                             AND I.IDTIPOORIG = 'S'
                           UNION ALL
                          SELECT NVL(SUM(ROUND(D.VRITEMSTRU1, 2)), 0) AS VRSALDOITEM
                            FROM DEMONSTCTBTMP D, ITEMAPUCTB I
                           WHERE I.CDTIPODEMO = C_AUX.CDTIPODEMO
                             AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                             AND D.NRDEMONSTCTBTMP = V_NRDEMOCTBTEMP
                             AND D.CDTIPODEMO = SUBSTR(I.CDORIGDEMO, 1, 2)
                             AND D.CDITEMSTRU = SUBSTR(I.CDORIGDEMO, 3, 4)
                             AND I.IDTIPOORIG = 'V');
                  IF P_DATAB IS NOT NULL THEN
                     SELECT NVL(SUM(VRSALDOITEM), 0)
                       INTO V_VRTEMP2
                       FROM (SELECT NVL(SUM(ROUND(S.VRSALDEMPR, 2)),0) AS VRSALDOITEM
                               FROM SALDEMPR S, ITEMAPUCTB I
                              WHERE ((INSTR(P_CDEMPRESA, S.CDEMPRESA) > 0) OR ('T' = P_CDEMPRESA))
                                AND S.DTSALDEMPR = V_DATASALDOB
                                AND S.CDCONTCTBL = I.CDORIGDEMO
                                AND I.CDTIPODEMO = C_AUX.CDTIPODEMO
                                AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                                AND I.IDTIPOORIG = 'S'
                              UNION ALL
                             SELECT NVL(SUM(ROUND(D.VRITEMSTRU2, 2)), 0) AS VRSALDOITEM
                               FROM DEMONSTCTBTMP D, ITEMAPUCTB I
                              WHERE I.CDTIPODEMO = C_AUX.CDTIPODEMO
                                AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                                AND D.NRDEMONSTCTBTMP = V_NRDEMOCTBTEMP
                                AND D.CDTIPODEMO = SUBSTR(I.CDORIGDEMO, 1, 2)
                                AND D.CDITEMSTRU = SUBSTR(I.CDORIGDEMO, 3, 4)
                                AND I.IDTIPOORIG = 'V');
                  END IF;
                  INSERT INTO DEMONSTCTBTMP
                     (NRDEMONSTCTBTMP, CDEMPRESA, CDTIPODEMO, CDITEMSTRU, IDTIPOITEM,
                      VRITEMSTRU1, VRPORCSOBRE1, VRITEMSTRU2, VRPORCSOBRE2, VRVARIACAO, VRPORCVARIACAO)
                  VALUES
                     (V_NRDEMOCTBTEMP, V_CDEMPRESA, P_CDTIPODEMO, C_AUX.CDITEMSTRU, C_AUX.IDORIGVALOR,
                      V_VRTEMP, 0, V_VRTEMP2, 0, NULL, NULL);
               END IF;
         END LOOP;
      ELSE
         V_DATAINIA := TO_DATE('01/'||TO_CHAR(P_DATAAINTERA, 'MM/YYYY'));
         V_DATAFINA := TRUNC(LAST_DAY(P_DATABINTERA));
         FOR C_AUX IN (
            SELECT I.CDTIPODEMO, I.CDITEMSTRU, I.IDORIGVALOR
              FROM ITEMDEMOF I
             WHERE I.CDTIPODEMO = P_CDTIPODEMO
            ORDER BY I.CDTIPODEMO, I.CDITEMSTRU) LOOP
               V_VRTEMP := NULL;
               V_VRTEMP2 := NULL;
               IF C_AUX.IDORIGVALOR <> 'I' THEN
                  SELECT NVL(SUM(VRSALDOITEM), 0)
                    INTO V_VRTEMP
                    FROM (SELECT NVL(SUM(ROUND(S.VRSALDEMPR, 2)),0) AS VRSALDOITEM
                            FROM (SELECT NVL(ROUND(SUM(VRLANCAMENT), 2), 0) VRSALDEMPR
                                    FROM LANCCONT L, (SELECT CDCONTCTBL
                                                        FROM CONTCTBL C, ITEMAPUCTB I
                                                       WHERE C.CDCONTCTBL LIKE I.CDORIGDEMO || '%'
                                                         AND C.IDTIPOCONT = 'A'
                                                         AND C.IDCONTCCUS <> 'R'
                                                         AND I.CDTIPODEMO = C_AUX.CDTIPODEMO
                                                         AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                                                         AND I.IDTIPOORIG = 'S') C
                                   WHERE DTLANCCONT BETWEEN V_DATAINIA AND V_DATAFINA
                                     AND ((INSTR(P_CDEMPRESA, CDEMPRESA) > 0) OR ('T' = P_CDEMPRESA))
                                     AND L.CDCONTCTBL = C.CDCONTCTBL) S
                           UNION ALL
                          SELECT NVL(SUM(ROUND(D.VRITEMSTRU1, 2)), 0) AS VRSALDOITEM
                            FROM DEMONSTCTBTMP D, ITEMAPUCTB I
                           WHERE I.CDTIPODEMO = C_AUX.CDTIPODEMO
                             AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                             AND D.NRDEMONSTCTBTMP = V_NRDEMOCTBTEMP
                             AND D.CDTIPODEMO = SUBSTR(I.CDORIGDEMO, 1, 2)
                             AND D.CDITEMSTRU = SUBSTR(I.CDORIGDEMO, 3, 4)
                             AND I.IDTIPOORIG = 'V');
                  IF P_DATAAINTERB IS NOT NULL THEN
                     V_DATAINIB := TO_DATE('01/'||TO_CHAR(P_DATAAINTERB, 'MM/YYYY'));
                     V_DATAFINB := TRUNC(LAST_DAY(P_DATABINTERB));
                     SELECT NVL(SUM(VRSALDOITEM), 0)
                     INTO V_VRTEMP2
                     FROM (SELECT NVL(SUM(ROUND(S.VRSALDEMPR, 2)),0) AS VRSALDOITEM
                             FROM (SELECT NVL(ROUND(SUM(VRLANCAMENT), 2), 0) VRSALDEMPR
                                     FROM LANCCONT L, (SELECT CDCONTCTBL
                                                         FROM CONTCTBL C, ITEMAPUCTB I
                                                        WHERE C.CDCONTCTBL LIKE I.CDORIGDEMO || '%'
                                                          AND C.IDTIPOCONT = 'A'
                                                          AND C.IDCONTCCUS <> 'R'
                                                          AND I.CDTIPODEMO = C_AUX.CDTIPODEMO
                                                          AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                                                          AND I.IDTIPOORIG = 'S') C
                                    WHERE DTLANCCONT BETWEEN V_DATAINIB AND V_DATAFINB
                                      AND ((INSTR(P_CDEMPRESA, CDEMPRESA) > 0) OR ('T' = P_CDEMPRESA))
                                      AND L.CDCONTCTBL = C.CDCONTCTBL) S
                            UNION ALL
                           SELECT NVL(SUM(ROUND(D.VRITEMSTRU2, 2)), 0) AS VRSALDOITEM
                             FROM DEMONSTCTBTMP D, ITEMAPUCTB I
                            WHERE I.CDTIPODEMO = C_AUX.CDTIPODEMO
                              AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                              AND D.NRDEMONSTCTBTMP = V_NRDEMOCTBTEMP
                              AND D.CDTIPODEMO = SUBSTR(I.CDORIGDEMO, 1, 2)
                              AND D.CDITEMSTRU = SUBSTR(I.CDORIGDEMO, 3, 4)
                              AND I.IDTIPOORIG = 'V');
                  END IF;
                  INSERT INTO DEMONSTCTBTMP
                     (NRDEMONSTCTBTMP, CDEMPRESA, CDTIPODEMO, CDITEMSTRU, IDTIPOITEM,
                      VRITEMSTRU1, VRPORCSOBRE1, VRITEMSTRU2, VRPORCSOBRE2, VRVARIACAO, VRPORCVARIACAO)
                  VALUES
                     (V_NRDEMOCTBTEMP, V_CDEMPRESA, P_CDTIPODEMO, C_AUX.CDITEMSTRU, C_AUX.IDORIGVALOR,
                      V_VRTEMP, 0, V_VRTEMP2, 0, NULL, NULL);
               END IF;
         END LOOP;
      END IF;
   ELSE
      IF P_IDSALDO = 'A' THEN
         V_DATASALDOA := LAST_DAY(P_DATAA);
         V_DATASALDOB := LAST_DAY(P_DATAB);
         FOR C_AUX IN (
            SELECT I.CDTIPODEMO, I.CDITEMSTRU, I.IDORIGVALOR
              FROM ITEMDEMOF I
             WHERE I.CDTIPODEMO = P_CDTIPODEMO
            ORDER BY I.CDTIPODEMO, I.CDITEMSTRU) LOOP
               V_VRTEMP := 0;
               V_VRTEMP2 := NULL;
               IF C_AUX.IDORIGVALOR <> 'I' THEN
                  SELECT NVL(SUM(VRSALDOITEM), 0)
                    INTO V_VRTEMP
                    FROM (SELECT NVL(SUM(ROUND(S.VRSALDOTRANSF, 2)),0) AS VRSALDOITEM
                            FROM SALDOTRANSF S, ITEMAPUCTB I
                           WHERE ((INSTR(P_CDEMPRESA, S.CDEMPRESA) > 0) OR ('T' = P_CDEMPRESA))
                             AND S.DTSALDOTRANSF = V_DATASALDOA
                             AND S.CDCONTCTBL = I.CDORIGDEMO
                             AND I.CDTIPODEMO = C_AUX.CDTIPODEMO
                             AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                             AND I.IDTIPOORIG = 'S'
                           UNION ALL
                          SELECT NVL(SUM(ROUND(D.VRITEMSTRU1, 2)), 0) AS VRSALDOITEM
                            FROM DEMONSTCTBTMP D, ITEMAPUCTB I
                           WHERE I.CDTIPODEMO = C_AUX.CDTIPODEMO
                             AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                             AND D.NRDEMONSTCTBTMP = V_NRDEMOCTBTEMP
                             AND D.CDTIPODEMO = SUBSTR(I.CDORIGDEMO, 1, 2)
                             AND D.CDITEMSTRU = SUBSTR(I.CDORIGDEMO, 3, 4)
                             AND I.IDTIPOORIG = 'V');
                  IF P_DATABINTERA IS NOT NULL THEN
                     SELECT NVL(SUM(VRSALDOITEM), 0)
                       INTO V_VRTEMP2
                       FROM (SELECT NVL(SUM(ROUND(S.VRSALDOTRANSF, 2)),0) AS VRSALDOITEM
                               FROM SALDOTRANSF S, ITEMAPUCTB I
                              WHERE ((INSTR(P_CDEMPRESA, S.CDEMPRESA) > 0) OR ('T' = P_CDEMPRESA))
                                AND S.DTSALDOTRANSF = V_DATASALDOB
                                AND S.CDCONTCTBL = I.CDORIGDEMO
                                AND I.CDTIPODEMO = C_AUX.CDTIPODEMO
                                AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                                AND I.IDTIPOORIG = 'S'
                              UNION ALL
                             SELECT NVL(SUM(ROUND(D.VRITEMSTRU2, 2)), 0) AS VRSALDOITEM
                               FROM DEMONSTCTBTMP D, ITEMAPUCTB I
                              WHERE I.CDTIPODEMO = C_AUX.CDTIPODEMO
                                AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                                AND D.NRDEMONSTCTBTMP = V_NRDEMOCTBTEMP
                                AND D.CDTIPODEMO = SUBSTR(I.CDORIGDEMO, 1, 2)
                                AND D.CDITEMSTRU = SUBSTR(I.CDORIGDEMO, 3, 4)
                                AND I.IDTIPOORIG = 'V');
                  END IF;
                  INSERT INTO DEMONSTCTBTMP
                     (NRDEMONSTCTBTMP, CDEMPRESA, CDTIPODEMO, CDITEMSTRU, IDTIPOITEM,
                      VRITEMSTRU1, VRPORCSOBRE1, VRITEMSTRU2, VRPORCSOBRE2, VRVARIACAO, VRPORCVARIACAO)
                  VALUES
                     (V_NRDEMOCTBTEMP, V_CDEMPRESA, P_CDTIPODEMO, C_AUX.CDITEMSTRU, C_AUX.IDORIGVALOR,
                      V_VRTEMP, 0, V_VRTEMP2, 0, NULL, NULL);
               END IF;
         END LOOP;
      ELSE
         V_DATAINIA := TO_DATE('01/'||TO_CHAR(P_DATAAINTERA, 'MM/YYYY'));
         V_DATAFINA := TRUNC(LAST_DAY(P_DATABINTERA));
         FOR C_AUX IN (
            SELECT I.CDTIPODEMO, I.CDITEMSTRU, I.IDORIGVALOR
              FROM ITEMDEMOF I
             WHERE I.CDTIPODEMO = P_CDTIPODEMO
            ORDER BY I.CDTIPODEMO, I.CDITEMSTRU) LOOP
               V_VRTEMP := NULL;
               V_VRTEMP2 := NULL;
               IF C_AUX.IDORIGVALOR <> 'I' THEN
                  SELECT NVL(SUM(VRSALDOITEM), 0)
                    INTO V_VRTEMP
                    FROM (SELECT NVL(SUM(ROUND(S.VRSALDEMPR, 2)),0) AS VRSALDOITEM
                            FROM (SELECT NVL(ROUND(SUM(VRLANCAMENT), 2), 0) VRSALDEMPR
                                    FROM LANCCONT L, (SELECT CDCONTCTBL
                                                        FROM CONTCTBL C, ITEMAPUCTB I
                                                       WHERE C.CDCONTCTBL LIKE I.CDORIGDEMO || '%'
                                                         AND C.IDTIPOCONT = 'A'
                                                         AND C.IDCONTCCUS <> 'R'
                                                         AND I.CDTIPODEMO = C_AUX.CDTIPODEMO
                                                         AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                                                         AND I.IDTIPOORIG = 'S') C
                                   WHERE DTLANCCONT BETWEEN V_DATAINIA AND V_DATAFINA
                                     AND ((INSTR(P_CDEMPRESA, CDEMPRESA) > 0) OR ('T' = P_CDEMPRESA))
                                     AND L.CDCONTCTBL = C.CDCONTCTBL
                                     AND L.IDLANCINTE <> 'T') S
                           UNION ALL
                          SELECT NVL(SUM(ROUND(D.VRITEMSTRU1, 2)), 0) AS VRSALDOITEM
                            FROM DEMONSTCTBTMP D, ITEMAPUCTB I
                           WHERE I.CDTIPODEMO = C_AUX.CDTIPODEMO
                             AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                             AND D.NRDEMONSTCTBTMP = V_NRDEMOCTBTEMP
                             AND D.CDTIPODEMO = SUBSTR(I.CDORIGDEMO, 1, 2)
                             AND D.CDITEMSTRU = SUBSTR(I.CDORIGDEMO, 3, 4)
                             AND I.IDTIPOORIG = 'V');
                  IF P_DATAAINTERB IS NOT NULL THEN
                     V_DATAINIB := TO_DATE('01/'||TO_CHAR(P_DATAAINTERB, 'MM/YYYY'));
                     V_DATAFINB := TRUNC(LAST_DAY(P_DATABINTERB));
                     SELECT NVL(SUM(VRSALDOITEM), 0)
                     INTO V_VRTEMP2
                     FROM (SELECT NVL(SUM(ROUND(S.VRSALDEMPR, 2)),0) AS VRSALDOITEM
                             FROM (SELECT NVL(ROUND(SUM(VRLANCAMENT), 2), 0) VRSALDEMPR
                                     FROM LANCCONT L, (SELECT CDCONTCTBL
                                                         FROM CONTCTBL C, ITEMAPUCTB I
                                                        WHERE C.CDCONTCTBL LIKE I.CDORIGDEMO || '%'
                                                          AND C.IDTIPOCONT = 'A'
                                                          AND C.IDCONTCCUS <> 'R'
                                                          AND I.CDTIPODEMO = C_AUX.CDTIPODEMO
                                                          AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                                                          AND I.IDTIPOORIG = 'S') C
                                    WHERE DTLANCCONT BETWEEN V_DATAINIB AND V_DATAFINB
                                      AND ((INSTR(P_CDEMPRESA, CDEMPRESA) > 0) OR ('T' = P_CDEMPRESA))
                                      AND L.CDCONTCTBL = C.CDCONTCTBL
                                      AND L.IDLANCINTE <> 'T') S
                            UNION ALL
                           SELECT NVL(SUM(ROUND(D.VRITEMSTRU2, 2)), 0) AS VRSALDOITEM
                             FROM DEMONSTCTBTMP D, ITEMAPUCTB I
                            WHERE I.CDTIPODEMO = C_AUX.CDTIPODEMO
                              AND I.CDITEMSTRU = C_AUX.CDITEMSTRU
                              AND D.NRDEMONSTCTBTMP = V_NRDEMOCTBTEMP
                              AND D.CDTIPODEMO = SUBSTR(I.CDORIGDEMO, 1, 2)
                              AND D.CDITEMSTRU = SUBSTR(I.CDORIGDEMO, 3, 4)
                              AND I.IDTIPOORIG = 'V');
                  END IF;
                  INSERT INTO DEMONSTCTBTMP
                     (NRDEMONSTCTBTMP, CDEMPRESA, CDTIPODEMO, CDITEMSTRU, IDTIPOITEM,
                      VRITEMSTRU1, VRPORCSOBRE1, VRITEMSTRU2, VRPORCSOBRE2, VRVARIACAO, VRPORCVARIACAO)
                  VALUES
                     (V_NRDEMOCTBTEMP, V_CDEMPRESA, P_CDTIPODEMO, C_AUX.CDITEMSTRU, C_AUX.IDORIGVALOR,
                      V_VRTEMP, 0, V_VRTEMP2, 0, NULL, NULL);
               END IF;
         END LOOP;
      END IF;
   END IF;

   FOR C_PORC_SOBRE in
      (SELECT CDITEMSTRU, CDITEMSTRUPERC CDITEMPORC
         FROM ITEMDEMOF
        WHERE CDTIPODEMO = P_CDTIPODEMO
          AND CDITEMSTRUPERC IS NOT NULL) LOOP
      V_VRTEMP := 0;
      V_VRTEMP2 := 0;
      V_VRPORC1 := 0;
      BEGIN
         SELECT VRITEMSTRU1, VRITEMSTRU2
           INTO V_VRPORC1, V_VRPORC2
           FROM DEMONSTCTBTMP
          WHERE NRDEMONSTCTBTMP = V_NRDEMOCTBTEMP
            AND CDITEMSTRU = C_PORC_SOBRE.CDITEMSTRU;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_VRPORC1 := 0;
            V_VRPORC2 := 0;
      END;
      IF ((V_VRPORC1 <> 0) OR (V_VRPORC2 <> 0)) THEN
         BEGIN
            SELECT VRITEMSTRU1, VRITEMSTRU2
              INTO V_VRTEMP, V_VRTEMP2
              FROM DEMONSTCTBTMP
             WHERE NRDEMONSTCTBTMP = V_NRDEMOCTBTEMP
               AND CDITEMSTRU = C_PORC_SOBRE.CDITEMPORC;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_VRTEMP := 0;
               V_VRTEMP2 := 0;
         END;
         IF V_VRTEMP <> 0 THEN
            V_VRPORC1 := ROUND((V_VRPORC1 * 100) / V_VRTEMP, 2);
         ELSE
            V_VRPORC1 := 0;
         END IF;
         IF V_VRTEMP2 <> 0 THEN
            V_VRPORC2 := ROUND((V_VRPORC2 * 100) / V_VRTEMP2, 2);
         ELSE
            V_VRPORC2 := 0;
         END IF;
         UPDATE DEMONSTCTBTMP
            SET VRPORCSOBRE1 = V_VRPORC1,
                VRPORCSOBRE2 = V_VRPORC2
          WHERE CDITEMSTRU = C_PORC_SOBRE.CDITEMSTRU
            AND NRDEMONSTCTBTMP = V_NRDEMOCTBTEMP;
      END IF;
   END LOOP;

   FOR C_VAR IN
      (SELECT CDITEMSTRU, VRITEMSTRU1, VRITEMSTRU2
         FROM DEMONSTCTBTMP
        WHERE NRDEMONSTCTBTMP = V_NRDEMOCTBTEMP
          AND NVL(VRITEMSTRU2, 0) <> 0
          AND NVL(VRITEMSTRU1, 0) <> 0) LOOP

      IF C_VAR.VRITEMSTRU1 <> C_VAR.VRITEMSTRU2 THEN
         V_VRTEMP := ROUND(C_VAR.VRITEMSTRU1 - C_VAR.VRITEMSTRU2, 2);
         V_VRTEMP2 := ROUND((C_VAR.VRITEMSTRU2 * 100) / C_VAR.VRITEMSTRU1, 2) - 100;
      ELSE
         V_VRTEMP := NULL;
         V_VRTEMP2 := NULL;
      END IF;
      
      UPDATE DEMONSTCTBTMP
         SET VRVARIACAO = V_VRTEMP,
             VRPORCVARIACAO = V_VRTEMP2
       WHERE NRDEMONSTCTBTMP = V_NRDEMOCTBTEMP
         AND CDITEMSTRU = C_VAR.CDITEMSTRU;
   END LOOP;

   P_NRDEMO := V_NRDEMOCTBTEMP;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('DEMONSTRATIVO_CTBO','5.75.xxx'); END;
/


