DROP FUNCTION RESUMO_ROP
/

DROP TYPE RESUMO_ROP_TABLE
/

DROP TYPE RESUMO_ROP_TYPE
/

CREATE TYPE RESUMO_ROP_TYPE AS
OBJECT (NRRESUMO_ROP        NUMBER,
        DSFILIAL            VARCHAR2(40),
        DSMODELO            VARCHAR2(03),
        NRTOPICO            FLOAT,
        DSDESCRICAOTOP      VARCHAR2(50),
        VRREAL              FLOAT(126),
        VRPERREAL           FLOAT(126),
        VRPROJECAO          FLOAT(126),
        VRPERPROJECAO       FLOAT(126),
        VRPROJMANUAL        FLOAT(126),
        VRPERPROJMANUAL     FLOAT(126),
        VRORCADO            FLOAT(126),
        VRPERORCADO         FLOAT(126),
        NRORDEM             NUMBER,
        NRORG               NUMBER,
        NRPROJECAOMANUALROP NUMBER)
/

CREATE TYPE RESUMO_ROP_TABLE AS
TABLE OF RESUMO_ROP_TYPE
/

CREATE OR REPLACE FUNCTION RESUMO_ROP
 (P_CDFILIAL     IN VARCHAR2,
  P_DATAINI      IN DATE,
  P_DATAFIN      IN DATE,
  P_RESULINDIV   IN VARCHAR2,
  P_IDRELAT      IN VARCHAR2,
  P_IDMODELO     IN VARCHAR2 DEFAULT 'P',
  P_IDPERCEN     IN VARCHAR2 DEFAULT 'L') RETURN RESUMO_ROP_TABLE PIPELINED
IS
  V_CDFILIAL         VARCHAR2(04);
  V_IDTPLANCROP      VARCHAR2(02);
  V_IDFILAGRROP      VARCHAR2(01);
  V_CDFILIALNFITROP  VARCHAR2(04);
  V_NMSERVICO        VARCHAR2(40);
  V_IDSERVPRINC      VARCHAR2(01);
  V_QTITEMLANCROP    FLOAT(126);
  V_VRITEMLANCROP    FLOAT(126);
  V_VRIMPOSTOREAL    FLOAT(126);
  V_NRROPFILIAL      VARCHAR2(10);
  V_NRLANCROP        VARCHAR2(10);
  V_VRFATFOSREAL     FLOAT(126);
  V_VRFATLIQUIREAL   FLOAT(126);
  V_VRDEPRBENSREAL   FLOAT(126);
  V_VRINVEST         FLOAT(126);
  V_VROUTRDESPREAL   FLOAT(126);
  V_VRTOTALDESP      FLOAT(126);
  V_NRDIA_UTIL       NUMBER;
  V_NRDIA_REALIZADO  NUMBER;
  V_VRMAOOBRAREAL    FLOAT(126);
  V_VRTOTALMO        FLOAT(126);
  V_SUBGRUPRODANT    VARCHAR2(10);

  V_NRRESUMO_ROP     NUMBER;
  V_DSFILIAL         VARCHAR2(40);
  V_DSMODELO         VARCHAR2(03);
  V_NRTOPICO         FLOAT;
  V_DSDESCRICAOTOP   VARCHAR2(50);
  V_VRPERREAL        FLOAT(126);
  V_VRPROJECAO       FLOAT(126);
  V_VRPERPROJECAO    FLOAT(126);
  V_VRPROJMANUAL     FLOAT(126);
  V_VRPERPROJMANUAL  FLOAT(126);
  V_VRORCADO         FLOAT(126);
  V_VRPERORCADO      FLOAT(126);
  V_CALC_PROJ        VARCHAR2(01);

  V_VRORCDEPREC      FLOAT(126);
  V_VRORCINV         FLOAT(126);
  V_VRORCIMPSOBLUC   FLOAT(126);
  V_VRORCIMPRECU     FLOAT(126);
  V_VRORCDM          FLOAT(126);
  V_VRORCDE          FLOAT(126);
  V_VRORCDD          FLOAT(126);
  V_VRORCTX          FLOAT(126);
  V_VRORCMO          FLOAT(126);
  V_VRORCCON         FLOAT(126);
  V_VRORCLIQ         FLOAT(126);
  V_VRORCIMP         FLOAT(126);
  V_VRORCFAT         FLOAT(126);
  V_TEMPPROJECAO     FLOAT(126);

  V_VRITEMLANCROP_P  FLOAT(126);
  V_VRIMPOSTOREAL_P  FLOAT(126);
  V_TEMPPROJECAO_P   FLOAT(126);
  V_VRFATLIQUIREAL_P FLOAT(126);
  V_VROUTRDESPREAL_P FLOAT(126);
  V_VRMAOOBRAREAL_P  FLOAT(126);
  V_VRDEPRBENSREAL_P FLOAT(126);
  V_VRCREDIMPREAL_P  FLOAT(126);
  V_RESULTADOREAL_P  FLOAT(126);
  V_VRINVEST_P       FLOAT(126);
  V_RESULTOTAL_P     FLOAT(126);

  V_RESULTADO_O      FLOAT(126);
  V_RESULTOTAL_O     FLOAT(126);

  V_VALORBON         FLOAT(126);
  V_VRTOTRELITROP    FLOAT(126);
  V_DT_NMTIPOOPER    VARCHAR2(40);
  V_ENTRADASCOMPRA   FLOAT(126);
  V_OUTRASENTRADAS   FLOAT(126);
  V_VRCREDIMPREAL    FLOAT(126);
  V_CONTRANSP        FLOAT(126);
  V_VRIMPCONTRANS    FLOAT(126);
  V_ESTOQUEINICIAL   FLOAT(126);
  V_VRTOTINVFINAL    FLOAT(126);
  V_VRRETIRADA       FLOAT(126);
  V_SAIDASDEV        FLOAT(126);
  V_VRTOTRETIPLA     FLOAT(126);
  V_OUTRASSAIDAS     FLOAT(126);
  V_VRTOTRETIEST     FLOAT(126);
  V_VRTOTAJUST       FLOAT(126);
  V_TXADM            FLOAT(126);
  V_VRTOTIMPLA       FLOAT(126);
  V_RESULTADOREAL    FLOAT(126);
  V_VRTOTBAIXAPLA    FLOAT(126);
  V_RESULTOTAL       FLOAT(126);
  
  V_NRORDEM          NUMBER; 
  V_ENTRADA          FLOAT(126);
  V_SAIDA            FLOAT(126);
  V_DEBCRED          FLOAT(126);
  V_AUX              FLOAT(126);
  V_AUX_P            FLOAT(126);

  VP_VRFATBRUTMAN    FLOAT(126);
  VP_VRFATFOSMAN     FLOAT(126);
  VP_VRCONSUMOMAN    FLOAT(126);
  VP_VROUTRDESPMAN   FLOAT(126);
  VP_VRMAOOBRAMAN    FLOAT(126);
  VP_VRDEPRBENSMAN   FLOAT(126);
  VP_VRCREDIMPMAN    FLOAT(126);
  VP_VRTAXAADMMAN    FLOAT(126);
  VP_VRINVESTMAN     FLOAT(126);
  VP_NRPROJECAOMANUALROP NUMBER;

  VP_VRFATNFMAN        FLOAT(126);
  VP_VRFATDIVMAN       FLOAT(126);
  VP_VRFATRECEXTRAMAN  FLOAT(126);
  VP_VRIMPOSTOMAN      FLOAT(126);
  VP_VRDEDUDEVMAN      FLOAT(126);
  VP_VRDESCSOBVENDAMAN FLOAT(126);
  VP_VRENCARGOMAN      FLOAT(126);
  VP_VRAJUSTEMAN       FLOAT(126);
  VP_VRDEBCREMAN       FLOAT(126);

  VP_RESULTADO         FLOAT(126);

  V_NRDIAINIORCA       VARCHAR2(02);
  V_SINAL              FLOAT(126);

  V_BRUTO              FLOAT(126);
  V_BRUTO_P            FLOAT(126);
  V_BRUTO_O            FLOAT(126);
  V_FATEFETIVO         FLOAT(126);
  V_FATEFETIVO_P       FLOAT(126);
  V_FATEFETIVO_O       FLOAT(126);
  V_FATNOTAFISC        FLOAT(126);
  V_FATNOTAFISC_P      FLOAT(126);
  V_FATNOTAFISC_O      FLOAT(126);
  V_FATFORSALE         FLOAT(126);
  V_FATFORSALE_P       FLOAT(126);
  V_FATFORSALE_O       FLOAT(126);
  V_FATDIVERSO         FLOAT(126);
  V_FATDIVERSO_P       FLOAT(126);
  V_FATDIVERSO_O       FLOAT(126);
  V_FATRECEXTRA        FLOAT(126);
  V_FATRECEXTRA_P      FLOAT(126);
  V_FATRECEXTRA_O      FLOAT(126);
  V_VRDEDURECEITA      FLOAT(126);
  V_VRDEDURECEITA_P    FLOAT(126);
  V_VRDEDURECEITA_O    FLOAT(126);
  V_DEDUIMPOFOS        FLOAT(126);
  V_DEDUIMPOFOS_P      FLOAT(126);
  V_DEDUIMPOFOS_O      FLOAT(126);
  V_DEDUIMPO           FLOAT(126);
  V_DEDUIMPO_P         FLOAT(126);
  V_DEDUIMPO_O         FLOAT(126);
  V_CREDIMPO           FLOAT(126);
  V_CREDIMPO_P         FLOAT(126);
  V_CREDIMPO_O         FLOAT(126);
  V_FATDEVCANC         FLOAT(126);
  V_FATDEVCANC_P       FLOAT(126);
  V_FATDEVCANC_O       FLOAT(126);
  V_DESCSOBVENDA       FLOAT(126);
  V_DESCSOBVENDA_P     FLOAT(126);
  V_DESCSOBVENDA_O     FLOAT(126);
  V_ENCARGOS           FLOAT(126);
  V_ENCARGOS_P         FLOAT(126);
  V_ENCARGOS_O         FLOAT(126);
  V_LIQUIDO            FLOAT(126);
  V_LIQUIDO_P          FLOAT(126);
  V_LIQUIDO_O          FLOAT(126);
  V_VRTAXAADMIN        FLOAT(126);
  V_VRTAXAADMIN_P      FLOAT(126);
  V_VRTAXAADMIN_O      FLOAT(126);
  V_VRMARGEMCONTR      FLOAT(126);
  V_VRMARGEMCONTR_P    FLOAT(126);
  V_VRMARGEMCONTR_O    FLOAT(126);
  V_VRDESPESATOT       FLOAT(126);
  V_VRDESPESATOT_P     FLOAT(126);
  V_VRDESPESATOT_O     FLOAT(126);
  V_DESPESA            FLOAT(126);
  V_DESPESA_P          FLOAT(126);
  V_DESPESA_O          FLOAT(126);
  V_AJUSTE             FLOAT(126);
  V_AJUSTE_P           FLOAT(126);
  V_AJUSTE_O           FLOAT(126);
  V_DEBCREDBCO         FLOAT(126);
  V_DEBCREDBCO_P       FLOAT(126);
  V_DEBCREDBCO_O       FLOAT(126);
  V_MAOOBRA            FLOAT(126);
  V_MAOOBRA_P          FLOAT(126);
  V_MAOOBRA_O          FLOAT(126);
  V_VRRESULTPARC       FLOAT(126);
  V_VRRESULTPARC_P     FLOAT(126);
  V_VRRESULTPARC_O     FLOAT(126);
  V_DEPREC             FLOAT(126);
  V_DEPREC_P           FLOAT(126);
  V_DEPREC_O           FLOAT(126);
  V_INVESTI            FLOAT(126);
  V_INVESTI_P          FLOAT(126);
  V_INVESTI_O          FLOAT(126);
  V_VRRESULTFINAL      FLOAT(126);
  V_VRRESULTFINAL_P    FLOAT(126);
  V_VRRESULTFINAL_O    FLOAT(126);
  V_VRCONSUMOREAL      FLOAT(126);
  V_VRCONSUMOREAL_P    FLOAT(126);
  V_VRCONSUMOREAL_O    FLOAT(126);
  V_TEMP               VARCHAR2(01);
  V_VRIMPFORSALEREAL   FLOAT(126);
  V_VRIMPFORSALEREAL_p FLOAT(126);
  V_VRIMPFORSALEREAL_O FLOAT(126);

  V_NRORG              NUMBER;

  OUT_RECORD RESUMO_ROP_TYPE := RESUMO_ROP_TYPE(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                                                NULL, NULL, NULL, NULL, NULL, NULL);
BEGIN
   V_SINAL := (-1);
   V_TEMP := 'V';

   V_DSMODELO := 'ROP';
   V_NRTOPICO := 0;
   V_DSDESCRICAOTOP := '';
   V_NRORDEM := 0;

   V_NRRESUMO_ROP    := 1;
   V_VRPERREAL       := 0;
   V_VRPROJECAO      := 0;
   V_VRPERPROJECAO   := 0;
   V_VRPROJMANUAL    := 0;
   V_VRPERPROJMANUAL := 0;
   V_VRORCADO        := 0;
   V_VRPERORCADO     := 0;

   V_ENTRADASCOMPRA  := 0;
   V_OUTRASENTRADAS  := 0;

   V_CONTRANSP := 0;

   IF P_RESULINDIV = 'S' THEN
      V_CDFILIAL := P_CDFILIAL;

      BEGIN
         SELECT CDFILIAL || ' - ' || NMFILIAL
           INTO V_DSFILIAL
           FROM FILIAL
          WHERE CDFILIAL = V_CDFILIAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
           V_DSFILIAL := NULL;
      END;
      
      FOR C_RESUMO_PAR IN (SELECT P.CDFILIAL, IDFATUROP, IDDETALHEFATUR, IDFILIALAGRROP, IDCALCIMPFAT,
                                  P.IDEXIBEDESPESAMOBRA, P.IDPERCFIXIMPREC, P.IDCALCPERCENTFAT, P.IDRETIRADAROP,
                                  P.CDGRUPPROD, P.VRPERIMPRECUROP, P.IDTIPOCALCTAXAADM, P.IDDTAPURFAT, G.NRORG,
                                  P.IDPROJECMANU, P.IDVOLUNEGOCIO, P.IDCONSFATUDIV, P.IDGERROPFOS, NVL(P.IDLANCIMPPAG,'N') IDLANCIMPPAG,
                                  NVL(P.IDAPUINVFILROP,'N') IDAPUINVFILROP, NVL(P.IDAPURAINVCTPAGROP,'N') IDAPURAINVCTPAGROP,
                                  NVL(P.IDAPURDEPRECROP,'N') IDAPURDEPRECROP,
                                  NVL(P.IDDESPMOBROP, 'N') IDDESPMOBROP, NVL(P.IDAPURDESPENTRMO,'N') IDAPURDESPENTRMO,
                                  NVL(P.IDDESPMOBROPCTB, 'N') IDDESPMOBROPCTB, NVL(P.IDAPUMOCTPGROP, 'N') IDAPUMOCTPGROP,
                                  NVL(P.IDCONSCREDBCO, 'N') IDCONSCREDBCO, NVL(P.IDCONSDEBBCO, 'N') IDCONSDEBBCO,
                                  NVL(P.IDCONSLANCAJUSPAG, 'N') IDCONSLANCAJUSPAG, NVL(P.IDCONSLANCAJUSREC, 'N') IDCONSLANCAJUSREC,
                                  NVL(P.IDPERFIXIMPDRF, 'N') IDPERFIXIMPDRF
                             FROM PARFILIALROP P, PARAMGERAL G
                            WHERE P.CDFILIAL = TIPOPARAM_DRO(V_CDFILIAL)) LOOP

         IF P_IDRELAT IN ('02','03') THEN
            V_IDFILAGRROP := C_RESUMO_PAR.IDFILIALAGRROP;
         ELSE
            V_IDFILAGRROP := 'N';
         END IF;

         IF P_IDMODELO = 'P' THEN
            V_IDTPLANCROP := CASE WHEN C_RESUMO_PAR.IDFATUROP = 'AB'
                                  THEN CASE WHEN C_RESUMO_PAR.IDDETALHEFATUR = 'M'
                                            THEN 'FE'
                                            ELSE 'FS'
                                       END
                                  ELSE CASE WHEN C_RESUMO_PAR.IDFATUROP = 'FE'
                                            THEN 'FE'
                                            ELSE 'FS'
                                       END
                             END;
         ELSE
            V_IDTPLANCROP := C_RESUMO_PAR.IDFATUROP;
         END IF;

         V_VRIMPOSTOREAL   := 0;
         V_VRITEMLANCROP   := 0;
         V_VRFATFOSREAL    := 0;
         V_VRFATLIQUIREAL  := 0;
         V_VRCONSUMOREAL   := 0;
         V_VRDEPRBENSREAL  := 0;
         V_VRINVEST        := 0;
         V_VROUTRDESPREAL  := 0;
         V_VRTOTALDESP     := 0;
         V_SUBGRUPRODANT   := NULL;
         V_VRCREDIMPREAL   := 0;
         V_VRCREDIMPREAL_P := 0;

         IF P_IDMODELO = 'P' THEN
            IF V_IDTPLANCROP = 'FE' THEN
               FOR C_RESUMO_ROP IN (SELECT IT.CDFILIALNFITROP, S.NMSERVICO, S.IDSERVPRINC, NVL(SUM(IT.QTITEMLANCROP),0) QTITEMLANCROP,
                                           NVL(SUM(IT.VRITEMLANCROP),0) VRITEMLANCROP, L.CDFILIAL, L.NRROPFILIAL, L.NRLANCROP, IT.NRITLANCROP
                                      FROM LANCROPFILIAL L INNER JOIN ITLANCROPFILIAL IT ON L.CDFILIAL = IT.CDFILIAL
                                                                                        AND L.NRROPFILIAL = IT.NRROPFILIAL
                                                                                        AND L.NRLANCROP = IT.NRLANCROP
                                                           INNER JOIN SERVICO S ON S.CDFILIAL = IT.CDFILIAL
                                                                                        AND S.CDSERVFILI = IT.CDITEMLANCROP
                                     WHERE L.CDFILIAL = V_CDFILIAL
                                       AND L.IDTPLANCROP = V_IDTPLANCROP
                                       AND TRUNC(L.DTLANCROP) BETWEEN P_DATAINI AND P_DATAFIN
                                       AND (((S.IDTIPOSERV = 'M') AND (V_IDFILAGRROP = 'S'))
                                         OR ((S.IDTIPOSERV IN ('N','F')) AND (V_IDFILAGRROP = 'N')))
                                     GROUP BY L.CDFILIAL, IT.CDFILIALNFITROP, S.NMSERVICO, S.IDSERVPRINC,
                                              L.NRROPFILIAL, L.NRLANCROP, IT.NRITLANCROP, L.CDFILIAL
                                     ORDER BY IT.CDFILIALNFITROP, S.NMSERVICO, S.IDSERVPRINC) LOOP

                  V_VRITEMLANCROP := V_VRITEMLANCROP + C_RESUMO_ROP.VRITEMLANCROP;

                  SELECT ROUND(NVL(SUM(NVL(DECODE(I.IDTRATIMPROP,'D', IR.VRITRELITROP *(-1),DECODE(I.IDTRATIMPROP,'V',0,IR.VRITRELITROP)),0)),0) + V_VRIMPOSTOREAL,2)
                    INTO V_VRIMPOSTOREAL
                    FROM ITRELACITROP IR INNER JOIN IMPOSTOROP I ON IR.CDITRELITROP  = I.CDIMPOSTO
                   WHERE IR.CDFILIAL      = C_RESUMO_ROP.CDFILIAL
                     AND IR.NRROPFILIAL   = C_RESUMO_ROP.NRROPFILIAL
                     AND IR.NRLANCROP     = C_RESUMO_ROP.NRLANCROP
                     AND IR.NRITLANCROP   = C_RESUMO_ROP.NRITLANCROP
                     AND I.CDFILIAL       = C_RESUMO_PAR.CDFILIAL
                     AND I.IDTPIMPOSTOROP = 'FE';
               END LOOP;
            ELSE
               FOR C_RESUMO_ROP IN (SELECT IT.CDFILIALNFITROP, NVL(SUM(IT.QTITEMLANCROP),0) QTITEMLANCROP,
                                           NVL(SUM(IT.VRITEMLANCROP),0) VRITEMLANCROP, L.CDFILIAL, L.NRROPFILIAL, L.NRLANCROP, IT.NRITLANCROP
                                      FROM LANCROPFILIAL L INNER JOIN ITLANCROPFILIAL IT ON L.CDFILIAL = IT.CDFILIAL
                                                                                        AND L.NRROPFILIAL = IT.NRROPFILIAL
                                                                                        AND L.NRLANCROP = IT.NRLANCROP
                                                           INNER JOIN TIPOOPERAC T ON L.CDTIPOOPERROP = T.CDTIPOOPER
                                     WHERE ((L.CDFILIAL = V_CDFILIAL) OR ((CDFILCENTROP = V_CDFILIAL) AND (V_IDFILAGRROP = 'S')))
                                       AND L.IDTPLANCROP = 'FS'
                                       AND ((('S' = C_RESUMO_PAR.IDDTAPURFAT) AND (L.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN)) OR
                                            (('R' = C_RESUMO_PAR.IDDTAPURFAT) AND (L.DTFINREFEROP BETWEEN P_DATAINI AND P_DATAFIN)))
                                       AND T.IDENTRSAIDOP = 'S'
                                     GROUP BY IT.CDFILIALNFITROP, L.CDFILIAL, L.NRROPFILIAL, L.NRLANCROP, IT.NRITLANCROP) LOOP

                  V_VRITEMLANCROP := V_VRITEMLANCROP + C_RESUMO_ROP.VRITEMLANCROP;

                  SELECT ROUND(NVL(SUM(DECODE(IDTRATIMPROP,'D',(-1) * VRITRELITROP ,DECODE(IDTRATIMPROP,'V',0,VRITRELITROP))),0) + V_VRIMPOSTOREAL,2)
                    INTO V_VRIMPOSTOREAL
                    FROM LANCROPFILIAL L, ITRELACITROP IR, IMPOSTO IP, IMPOSTOROP I
                   WHERE ((L.CDFILIAL = V_CDFILIAL) OR ((CDFILCENTROP = V_CDFILIAL) AND (V_IDFILAGRROP = 'S')))
                     AND L.IDTPLANCROP = 'FS'
                     AND ((('S' = C_RESUMO_PAR.IDDTAPURFAT) AND (L.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN)) OR
                         (('R' = C_RESUMO_PAR.IDDTAPURFAT) AND (L.DTFINREFEROP BETWEEN P_DATAINI AND P_DATAFIN)))
                     AND L.CDFILIAL = IR.CDFILIAL
                     AND L.NRROPFILIAL = IR.NRROPFILIAL
                     AND L.NRLANCROP = IR.NRLANCROP
                     AND IP.CDIMPOSTO = IR.CDITRELITROP(+)
                     AND IP.CDIMPOSTO = I.CDIMPOSTO
                     AND I.CDFILIAL = C_RESUMO_PAR.CDFILIAL
                     AND IDTPIMPOSTOROP = 'FS'
                     AND IR.CDFILIAL = C_RESUMO_ROP.CDFILIAL
                     AND IR.NRROPFILIAL = C_RESUMO_ROP.NRROPFILIAL
                     AND IR.NRLANCROP = C_RESUMO_ROP.NRLANCROP
                     AND IR.NRITLANCROP = C_RESUMO_ROP.NRITLANCROP;
               END LOOP;
            END IF;

            SELECT NVL(SUM(VRITEMLANCROP),0) AS VRTOTALVENDA
              INTO V_VRFATFOSREAL
              FROM ITLANCROPFILIAL I, LANCROPFILIAL LA
             WHERE LA.CDFILIAL = I.CDFILIAL
               AND LA.NRROPFILIAL = I.NRROPFILIAL
               AND LA.NRLANCROP = I.NRLANCROP
               AND ((LA.CDFILIAL = V_CDFILIAL) OR ((CDFILCENTROP = V_CDFILIAL) AND (V_IDFILAGRROP = 'S')))
               AND LA.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN
               AND LA.IDTPLANCROP = 'FF';

            V_VRFATLIQUIREAL := CASE WHEN C_RESUMO_PAR.IDCALCIMPFAT = 'FATBRUTO'
                                     THEN NVL(V_VRITEMLANCROP,0) + NVL(V_VRIMPOSTOREAL,0) + NVL(V_VRFATFOSREAL,0)
                                     ELSE NVL(V_VRITEMLANCROP,0) + NVL(V_VRFATFOSREAL,0)
                                END;

            SELECT NVL(SUM(VRITEMLANCROP),0) VRDEPRECIACAO
              INTO V_VRDEPRBENSREAL
              FROM ITLANCROPFILIAL IT, LANCROPFILIAL L
             WHERE ((L.CDFILIAL = V_CDFILIAL) OR ((CDFILCENTROP  = V_CDFILIAL) AND (V_IDFILAGRROP = 'S')))
               AND L.IDTPLANCROP = 'DB'
               AND L.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN
               AND L.CDFILIAL = IT.CDFILIAL
               AND L.NRROPFILIAL = IT.NRROPFILIAL
               AND L.NRLANCROP = IT.NRLANCROP;

            BEGIN
               SELECT (VRDESPDIV+VRDESPENT+VRDESPPAG) VRTOTALDESP
                 INTO V_VRTOTALDESP
                 FROM (SELECT NVL(SUM(VRDESPDIV),0) VRDESPDIV, NVL(SUM(VRDESPENT),0) VRDESPENT, NVL(SUM(VRDESPPAG),0) VRDESPPAG
                         FROM (SELECT DECODE(IDTPLANCROP,'DD',VRITEMLANCROP,0) VRDESPDIV,
                                      DECODE(IDTPLANCROP,'DE',VRITEMLANCROP,0)VRDESPENT,
                                      DECODE(IDTPLANCROP,'DC',VRITEMLANCROP,0) VRDESPPAG
                                 FROM ITLANCROPFILIAL IT, LANCROPFILIAL L
                                WHERE L.CDFILIAL    = V_CDFILIAL
                                  AND L.IDTPLANCROP IN ('DD','DE','DC')
                                  AND L.DTLANCROP   BETWEEN '01/'||TO_CHAR(TO_DATE(P_DATAINI),'MM/YYYY') AND LAST_DAY(P_DATAFIN)
                                  AND L.CDFILIAL    = IT.CDFILIAL
                                  AND L.NRROPFILIAL = IT.NRROPFILIAL
                                  AND L.NRLANCROP   = IT.NRLANCROP));
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_VRTOTALDESP := 0;
            END;

            BEGIN
               SELECT NRDIA_UTIL, CASE WHEN NRDIA_REALIZADO = 0 THEN 1 ELSE NRDIA_REALIZADO END NRDIA_REALIZADO, 
                      DECODE(DTINICOMP, DTINI, DECODE(DTFINALCOMP, DTFINAL,'N','S'),'S') CALC_PROJ
                 INTO V_NRDIA_UTIL, V_NRDIA_REALIZADO, V_CALC_PROJ
                 FROM (SELECT RETORNA_DIA_UTIL(V_CDFILIAL,P_DATAINI,LAST_DAY(P_DATAFIN)) AS NRDIA_UTIL,
                              RETORNA_DIAS_REALIZADOS(V_CDFILIAL,P_DATAINI,P_DATAFIN) AS NRDIA_REALIZADO,
                              P_DATAFIN AS DTFINAL, P_DATAINI DTINI, LAST_DAY(P_DATAFIN) DTFINALCOMP,
                              FIRST_DAY(P_DATAINI) DTINICOMP
                         FROM DUAL);
            EXCEPTION
              WHEN NO_DATA_FOUND THEN BEGIN
                 V_NRDIA_UTIL  := NULL;
                 V_NRDIA_REALIZADO := NULL;
                 V_CALC_PROJ := NULL;
              END;
            END;

            V_VROUTRDESPREAL := CASE WHEN C_RESUMO_PAR.IDEXIBEDESPESAMOBRA = 'TOTAL_PERIODO'
                                     THEN V_VRTOTALDESP
                                     ELSE (V_VRTOTALDESP / V_NRDIA_UTIL) * V_NRDIA_REALIZADO
                                END;

            V_DT_NMTIPOOPER := NULL;
            FOR C_COMPRASMES IN (                                                                     
               /* Order by alterado */
               SELECT A1.CDGRUPPROD, A1.CDSUBGRPROD, A2.DTLANCROP, A2.CDTIPOOPERROP,
                      NVL((SELECT 'A'
                             FROM TIPOOPERACROP T
                            WHERE A2.CDTIPOOPERROP = T.CDTIPOOPER
                              AND CDFILIAL = C_RESUMO_PAR.CDFILIAL
                              AND IDENTRSAIDOP = 'E'
                              AND IDTPOPERACROP = 'CC'),'B') IDTIPOENTRADA,
                      A2.NMTIPOOPER, A2.CDFILAGREROP,
                      A2.QTTOTCONSENTR, NVL(A2.VRTOTCONSENTR,0) VRTOTCONSENTR,
                      ROUND(A2.VRCONTRANS,2) AS VRCONTRANS,
                      A2.VRCUSTOLOG, A2.VRCUSTSEGPROD, NUMEROREROP
                 FROM (SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD
                         FROM SUBGRUPPROD S
                        WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD) A1 LEFT JOIN
                       (SELECT DTLANCROP, CDGRUPPRODROP AS CDGRUPPROD, CDSUBGRPRODROP AS CDSUBGRPROD,
                               CDTIPOOPERROP, NMTIPOOPER, CDFILCENTROP AS CDFILAGREROP,
                               SUM(QTITEMLANCROP) QTTOTCONSENTR,SUM(VRITEMLANCROP) VRTOTCONSENTR,
                               SUM(VRCONTRANS) AS VRCONTRANS, SUM(NVL(VRCUSTOLOGITROP,0)) VRCUSTOLOG,
                               SUM(NVL(VRCUSTSEGPROD,0)) VRCUSTSEGPROD, NUMEROREROP
                          FROM (SELECT DTLANCROP, CDGRUPPRODROP, CDSUBGRPRODROP,
                                       CDTIPOOPERROP, NMTIPOOPER, CDFILCENTROP, QTITEMLANCROP,
                                       VRITEMLANCROP, RATEIO_CONHECIMENTO_TRANSPORTE(L.CDFILIALNFROP,
                                          L.NRLANCTONFROP,CDGRUPPRODROP,CDSUBGRPRODROP) AS VRCONTRANS,
                                       VRCUSTOLOGITROP, VRCUSTSEGPROD, NUMEROREROP
                                  FROM LANCROPFILIAL L INNER JOIN ITLANCROPFILIAL IT ON L.NRROPFILIAL = IT.NRROPFILIAL
                                                                                    AND L.NRLANCROP = IT.NRLANCROP
                                                                                    AND L.IDTPLANCROP = 'CC'
                                                                                    AND L.CDFILIAL = P_CDFILIAL
                                                                                    AND L.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN
                                                                                    AND IT.CDGRUPPRODROP = C_RESUMO_PAR.CDGRUPPROD
                                                                                    AND L.CDFILIAL = IT.CDFILIAL
                                                       INNER JOIN TIPOOPERAC T ON L.CDTIPOOPERROP =  T.CDTIPOOPER
                                                                              AND T.IDENTRSAIDOP  = 'E'
                                 UNION ALL
                                SELECT DTLANCROP, CDGRUPPRODROP, CDSUBGRPRODROP, CDTIPOOPERROP, NMTIPOOPER,
                                       CDFILCENTROP, QTITEMLANCROP, VRITEMLANCROP,
                                       RATEIO_CONHECIMENTO_TRANSPORTE(L.CDFILIALNFROP,L.NRLANCTONFROP,CDGRUPPRODROP,CDSUBGRPRODROP) AS VRCONTRANS,
                                       VRCUSTOLOGITROP, VRCUSTSEGPROD, NUMEROREROP
                                  FROM LANCROPFILIAL L INNER JOIN ITLANCROPFILIAL IT ON L.NRROPFILIAL = IT.NRROPFILIAL
                                                                                    AND L.NRLANCROP = IT.NRLANCROP
                                                                                    AND L.IDTPLANCROP = 'CC'
                                                                                    AND L.CDFILCENTROP = P_CDFILIAL
                                                                                    AND V_IDFILAGRROP = 'S'
                                                                                    AND L.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN
                                                                                    AND IT.CDGRUPPRODROP = C_RESUMO_PAR.CDGRUPPROD
                                                                                    AND L.CDFILIAL = IT.CDFILIAL
                                                       INNER JOIN TIPOOPERAC T ON L.CDTIPOOPERROP =  T.CDTIPOOPER
                                                                              AND T.IDENTRSAIDOP  = 'E')
                         GROUP BY CDFILCENTROP,CDTIPOOPERROP,NMTIPOOPER,DTLANCROP,CDGRUPPRODROP, CDSUBGRPRODROP, NUMEROREROP) A2
                                                                    ON A1.CDGRUPPROD = A2.CDGRUPPROD
                                                                   AND A1.CDSUBGRPROD = A2.CDSUBGRPROD
                ORDER BY A2.DTLANCROP, A2.NMTIPOOPER, A1.CDSUBGRPROD, IDTIPOENTRADA DESC, A2.CDFILAGREROP DESC, A1.CDGRUPPROD, A2.CDTIPOOPERROP) LOOP

               V_VALORBON := CASE WHEN C_COMPRASMES.CDTIPOOPERROP = '06'
                                  THEN 0
                                  ELSE NVL(C_COMPRASMES.VRTOTCONSENTR,0)
                             END;

               V_VRTOTRELITROP := 0;
               IF (V_DT_NMTIPOOPER IS NULL) OR (V_DT_NMTIPOOPER <> TO_CHAR(C_COMPRASMES.DTLANCROP,'DDMMYYYY')||C_COMPRASMES.NMTIPOOPER) THEN
                  V_DT_NMTIPOOPER := TO_CHAR(C_COMPRASMES.DTLANCROP,'DDMMYYYY')||C_COMPRASMES.NMTIPOOPER;

                  /* assImpCompMes  - modificada */
                  SELECT SUM(CASE IDTRATIMPROP WHEN 'A'
                                               THEN NVL(VRTOTRELITROP,0)
                                               WHEN 'D'
                                               THEN NVL(VRTOTRELITROP,0)*(-1)
                                               ELSE 0
                             END)
                    INTO V_VRTOTRELITROP
                    FROM (SELECT IDTRATIMPROP, R.CDIMPOSTO, SGIMPOSTO
                            FROM IMPOSTO I INNER JOIN IMPOSTOROP R ON I.CDIMPOSTO = R.CDIMPOSTO
                           WHERE CDFILIAL = C_RESUMO_PAR.CDFILIAL
                             AND IDTPIMPOSTOROP = 'CC') A1 LEFT JOIN (SELECT CDITRELITROP, L.DTLANCROP, NMTIPOOPER, CDFILCENTROP AS CDFILAGREROP,
                                                                             SUM(NVL(QTITRELITROP,0)) QTITRELITROP,
                                                                             SUM(NVL(VRITRELITROP,0)) VRTOTRELITROP
                                                                        FROM LANCROPFILIAL L INNER JOIN ITLANCROPFILIAL IT
                                                                                                     ON L.CDFILIAL = IT.CDFILIAL
                                                                                                    AND L.NRROPFILIAL = IT.NRROPFILIAL
                                                                                                    AND L.NRLANCROP = IT.NRLANCROP
                                                                                             INNER JOIN ITRELACITROP IR
                                                                                                     ON IT.CDFILIAL = IR.CDFILIAL
                                                                                                    AND IT.NRROPFILIAL = IR.NRROPFILIAL
                                                                                                    AND IT.NRLANCROP = IR.NRLANCROP
                                                                                                    AND IT.NRITLANCROP = IR.NRITLANCROP
                                                                                             INNER JOIN TIPOOPERAC T
                                                                                                     ON L.CDTIPOOPERROP = T.CDTIPOOPER
                                                                       WHERE ((L.CDFILIAL = V_CDFILIAL)
                                                                           OR ((CDFILCENTROP = V_CDFILIAL)
                                                                          AND (V_IDFILAGRROP = 'S')))
                                                                         AND L.IDTPLANCROP = 'CC'
                                                                         AND L.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN
                                                                         AND T.IDENTRSAIDOP = 'E'
                                                                         AND TRUNC(L.DTLANCROP) = TRUNC(C_COMPRASMES.DTLANCROP)
                                                                         AND NMTIPOOPER = C_COMPRASMES.NMTIPOOPER
                                                                       GROUP BY CDFILCENTROP, L.DTLANCROP,
                                                                                CDITRELITROP, NMTIPOOPER,
                                                                                NRNOTAFISCROP, CDSERIENFROP
                                                                       ORDER BY CDFILCENTROP, L.DTLANCROP, NMTIPOOPER, NRNOTAFISCROP) A2
                                                                  ON A1.CDIMPOSTO = A2.CDITRELITROP;
               END IF;
            
               IF C_COMPRASMES.IDTIPOENTRADA = 'A' THEN
                  V_ENTRADASCOMPRA := NVL(V_ENTRADASCOMPRA,0) + NVL(V_VALORBON,0) + NVL(V_VRTOTRELITROP,0);
               ELSE
                  V_OUTRASENTRADAS := NVL(V_OUTRASENTRADAS,0) + NVL(V_VALORBON,0) + NVL(V_VRTOTRELITROP,0);
               END IF;

               IF C_RESUMO_PAR.IDPERCFIXIMPREC <> 'S' THEN
                  V_VRCREDIMPREAL := NVL(V_VRCREDIMPREAL,0) + NVL(V_VRTOTRELITROP,0);
               END IF;
               V_VRCREDIMPREAL := NVL(V_VRCREDIMPREAL,0) + NVL(V_VALORBON,0) + NVL(V_VRTOTRELITROP,0);

               V_CONTRANSP := NVL(V_CONTRANSP,0) + NVL(C_COMPRASMES.VRCONTRANS,0);
            END LOOP;

            IF C_RESUMO_PAR.IDPERCFIXIMPREC = 'S' THEN
               V_VRCREDIMPREAL := (NVL(V_VRCREDIMPREAL,0) * NVL(C_RESUMO_PAR.VRPERIMPRECUROP,0))/ 100;
            END IF;         

            SELECT SUM(NVL(DECODE(A1.IDTRATIMPROP,'D', IM.VRIMPOSTOIT *(-1),DECODE(A1.IDTRATIMPROP,'V',0,IM.VRIMPOSTOIT)),0)) AS VRIMPCONTRANS
              INTO V_VRIMPCONTRANS
              FROM (SELECT DISTINCT NF.CDFILIAL, NF.NRLANCTONF
                      FROM ITLANCROPFILIAL ITR INNER JOIN ITEMNOTAFISC ITN ON ITR.NRLANCTONFITROP = ITN.NRLANCTONF
                                                                          AND ITN.CDFILIAL = ITR.CDFILIALNFITROP
                                               INNER JOIN NOTAFISCAL NF ON ITN.NRLANCTONFIT = NF.NRLANCTONF
                                                                       AND NF.CDFILIAL = ITN.CDFILIAL
                                                                       AND NF.IDENTRSAIDOP = 'E'
                                               INNER JOIN LANCROPFILIAL L ON ((L.CDFILIAL = V_CDFILIAL)
                                                                          OR ((CDFILCENTROP = V_CDFILIAL)
                                                                         AND (V_IDFILAGRROP = 'S')))
                                                                         AND L.IDTPLANCROP = 'CC'
                                                                         AND L.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN
                                                                         AND CDGRUPPRODROP = C_RESUMO_PAR.CDGRUPPROD
                                                                         AND L.CDFILIAL = ITR.CDFILIAL
                                                                         AND L.NRROPFILIAL = ITR.NRROPFILIAL
                                                                         AND L.NRLANCROP = ITR.NRLANCROP
                                               INNER JOIN TIPOOPERAC T ON L.CDTIPOOPERROP = T.CDTIPOOPER
                                                                      AND T.IDENTRSAIDOP = 'E'
                                                                      AND NVL(T.IDOPERDEVONF ,'N') = 'N'
                                                                      AND T.CDTIPOOPER <> '03') A
                     INNER JOIN ITEMPROIMPOS IM ON A.CDFILIAL = IM.CDFILIAL
                                               AND A.NRLANCTONF = IM.NRLANCTONF
                     INNER JOIN (SELECT IDTRATIMPROP, R.CDIMPOSTO, SGIMPOSTO
                                   FROM IMPOSTOROP R INNER JOIN IMPOSTO I ON R.CDIMPOSTO = I.CDIMPOSTO
                                  WHERE CDFILIAL = TIPOPARAM_DRO(V_CDFILIAL)
                                    AND IDTPIMPOSTOROP = 'CC') A1 ON A1.CDIMPOSTO = IM.CDIMPOSTO
             WHERE NRITPROIMPOS = '1';                   
         
            V_CONTRANSP := NVL(V_CONTRANSP,0) + NVL(V_VRIMPCONTRANS,0);

            /* adqResumoApu - group by alterado */
            SELECT SUM(VRTOTINVINIC) VRTOTINVINIC, SUM(VRTOTINVFINAL) VRTOTINVFINAL, SUM(VRRETIRADA) VRRETIRADA,
                   SUM(VRTOTRETIPLA) VRTOTRETIPLA, SUM(VRTOTRETIEST) VRTOTRETIEST, SUM(VRTOTAJUST) VRTOTAJUST,
                   SUM(VRTOTIMPLA) VRTOTIMPLA, SUM(VRTOTBAIXAPLA) VRTOTBAIXAPLA
              INTO V_ESTOQUEINICIAL, V_VRTOTINVFINAL, V_VRRETIRADA, V_VRTOTRETIPLA,
                   V_VRTOTRETIEST, V_VRTOTAJUST, V_VRTOTIMPLA, V_VRTOTBAIXAPLA
              FROM (
            SELECT SUM(NVL(A2.VRTOTINVINIC,0)) VRTOTINVINIC, SUM(NVL(A2.VRTOTINVFINAL,0)) VRTOTINVFINAL,
                   SUM(NVL(VRTOTRETIP,0) + NVL(VRTOTRETIE,0)) VRRETIRADA, SUM(NVL(VRTOTRETIP,0)) VRTOTRETIPLA, SUM(NVL(VRTOTRETIE,0)) VRTOTRETIEST,
                   SUM(NVL(VRTOTAJUST,0))VRTOTAJUST, SUM(NVL(VRTOTIMPLA,0)) VRTOTIMPLA,SUM(NVL(VRTOTBAIXAPLA,0)) VRTOTBAIXAPLA
              FROM (SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'II' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD
                     UNION ALL
                    SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'IF' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD
                     UNION ALL
                    SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'RP' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD
                     UNION ALL
                    SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'RE' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD
                     UNION ALL
                    SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'BP' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD
                     UNION ALL
                    SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'AJ' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD
                     UNION ALL
                    SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'IM' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD) A1 LEFT JOIN

                   (SELECT CDGRUPPROD, CDSUBGRPROD, IDTPLANCROP, CDFILAGREROP,
                           NVL(VRTOTINVINIC,0) VRTOTINVINIC,
                           NVL(VRTOTINVFINAL,0) VRTOTINVFINAL,
                           NVL(VRTOTRETIP,0) VRTOTRETIP, NVL(VRTOTRETIE,0) VRTOTRETIE,
                           NVL(VRTOTBAIXAPLA,0) VRTOTBAIXAPLA, NVL(VRTOTAJUST,0) VRTOTAJUST,
                           NVL(VRTOTIMPLA,0) VRTOTIMPLA
                      FROM (SELECT CDGRUPPRODROP AS CDGRUPPROD, CDSUBGRPRODROP AS CDSUBGRPROD,
                                   IDTPLANCROP,  '' AS CDFILAGREROP,
                                   CASE WHEN IDTPLANCROP = 'II' THEN VRITEMLANCROP ELSE 0 END VRTOTINVINIC,
                                   CASE WHEN IDTPLANCROP = 'IF' THEN VRITEMLANCROP ELSE 0 END VRTOTINVFINAL,
                                   (0) VRTOTRETIP, (0) VRTOTRETIE,(0)VRTOTBAIXAPLA,
                                   (0) VRTOTAJUST, (0) VRTOTIMPLA
                              FROM LANCROPFILIAL L INNER JOIN ITLANCROPFILIAL IT ON L.CDFILIAL = IT.CDFILIAL
                                                                                AND L.NRROPFILIAL = IT.NRROPFILIAL
                                                                                AND L.NRLANCROP = IT.NRLANCROP
                             WHERE L.CDFILIAL = V_CDFILIAL
                               AND ((L.DTLANCROP = P_DATAINI AND IDTPLANCROP = 'II')
                                OR (L.DTLANCROP = P_DATAFIN AND IDTPLANCROP = 'IF'))
                               AND CDGRUPPRODROP = C_RESUMO_PAR.CDGRUPPROD
                             UNION ALL
                            SELECT CDGRUPPRODROP AS CDGRUPPROD,  CDSUBGRPRODROP AS CDSUBGRPROD, IDTPLANCROP,
                                   '' AS CDFILAGREROP,
                                   (0) VRTOTINVINIC, (0) VRTOTINVFINAL,
                                   CASE WHEN IDTPLANCROP = 'RP' THEN VRITEMLANCROP ELSE 0 END VRTOTRETIP,
                                   CASE WHEN IDTPLANCROP = 'RE' THEN VRITEMLANCROP ELSE 0 END VRTOTRETIE,
                                   CASE WHEN IDTPLANCROP = 'BP' THEN VRITEMLANCROP ELSE 0 END VRTOTBAIXAPLA,
                                   CASE WHEN IDTPLANCROP = 'AJ' THEN VRITEMLANCROP ELSE 0 END VRTOTAJUST,
                                   CASE WHEN IDTPLANCROP = 'IM' THEN VRITEMLANCROP ELSE 0 END VRTOTIMPLA
                              FROM ITLANCROPFILIAL IT INNER JOIN LANCROPFILIAL L ON L.CDFILIAL = IT.CDFILIAL
                                                                                AND L.NRROPFILIAL = IT.NRROPFILIAL
                                                                                AND L.NRLANCROP = IT.NRLANCROP
                             WHERE L.CDFILIAL = V_CDFILIAL
                               AND L.IDTPLANCROP IN ('RE','RP','AJ','IM','BP')
                               AND L.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN)) A2 ON A1.CDGRUPPROD  = A2.CDGRUPPROD
                                                                                   AND A1.CDSUBGRPROD = A2.CDSUBGRPROD
                                                                                   AND A1.IDTPLANCROP = A2.IDTPLANCROP
             WHERE V_IDFILAGRROP  = 'N'
             GROUP BY A1.CDSUBGRPROD,  A2.CDFILAGREROP

             UNION ALL

            SELECT SUM(NVL(A2.VRTOTINVINIC,0)) VRTOTINVINIC, SUM(NVL(A2.VRTOTINVFINAL,0)) VRTOTINVFINAL,
                   SUM(VRTOTRETIP + VRTOTRETIE) VRRETIRADA, SUM(NVL(VRTOTRETIP,0)) VRTOTRETIPLA, SUM(NVL(VRTOTRETIE,0)) VRTOTRETIEST,
                   SUM(NVL(VRTOTBAIXAPLA,0)) VRTOTBAIXAPLA, SUM(NVL(VRTOTAJUST,0)) VRTOTAJUST, SUM(NVL(VRTOTIMPLA,0)) VRTOTIMPLA
              FROM (SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'II' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD
                     UNION ALL
                    SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'IF' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD
                     UNION ALL
                    SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'RP' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD
                     UNION ALL
                    SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'RE' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD
                     UNION ALL
                    SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'BP' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD
                     UNION ALL
                    SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'AJ' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD
                     UNION ALL
                    SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD, 'IM' IDTPLANCROP
                      FROM SUBGRUPPROD S
                     WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD) A1 LEFT JOIN

                   (SELECT CDGRUPPROD,  CDSUBGRPROD, IDTPLANCROP, CDFILAGREROP,
                           NVL(VRTOTINVINIC,0) VRTOTINVINIC,
                           NVL(VRTOTINVFINAL,0) VRTOTINVFINAL,
                           NVL(VRTOTRETIP,0) VRTOTRETIP, NVL(VRTOTRETIE,0) VRTOTRETIE,
                           NVL(VRTOTBAIXAPLA,0) VRTOTBAIXAPLA,NVL(VRTOTAJUST,0) VRTOTAJUST,
                           NVL(VRTOTIMPLA,0) VRTOTIMPLA
                      FROM (SELECT GR.CDGRUPPROD,  SG.CDSUBGRPROD, IDTPLANCROP,  CDFILCENTROP AS CDFILAGREROP,
                                   CASE WHEN IDTPLANCROP = 'II' THEN VRITEMLANCROP ELSE 0 END VRTOTINVINIC,
                                   CASE WHEN IDTPLANCROP = 'IF' THEN VRITEMLANCROP ELSE 0 END VRTOTINVFINAL,
                                   (0) VRTOTRETIP, (0) VRTOTRETIE,(0)VRTOTBAIXAPLA,(0) VRTOTAJUST, (0) VRTOTIMPLA
                              FROM LANCROPFILIAL L INNER JOIN ITLANCROPFILIAL IT ON L.CDFILIAL = IT.CDFILIAL
                                                                                AND L.NRROPFILIAL = IT.NRROPFILIAL
                                                                                AND L.NRLANCROP = IT.NRLANCROP
                                                   INNER JOIN SUBGRUPPROD SG ON IT.CDITEMLANCROP = SG.CDSUBGRPROD
                                                   INNER JOIN GRUPPROD GR ON GR.CDGRUPPROD = SG.CDGRUPPROD
                             WHERE ((L.CDFILIAL = V_CDFILIAL) OR (CDFILCENTROP = V_CDFILIAL))
                               AND ((L.DTLANCROP = P_DATAINI AND IDTPLANCROP = 'II') OR (L.DTLANCROP = P_DATAFIN AND IDTPLANCROP = 'IF'))
                               AND GR.CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD
                             UNION ALL
                            SELECT CDGRUPPRODROP AS CDGRUPPROD, CDSUBGRPRODROP AS CDSUBGRPROD, IDTPLANCROP,
                                  CDFILCENTROP AS CDFILAGREROP,
                                  (0) VRTOTINVINIC, (0) VRTOTINVFINAL,
                                  DECODE(IDTPLANCROP,'RP',VRITEMLANCROP,0) AS  VRTOTRETIP,
                                  DECODE(IDTPLANCROP,'RE',VRITEMLANCROP,0) AS  VRTOTRETIE,
                                  DECODE(IDTPLANCROP,'BP',VRITEMLANCROP,0) AS  VRTOTBAIXAPLA,
                                  DECODE(IDTPLANCROP,'AJ',VRITEMLANCROP,0) AS  VRTOTAJUST,
                                  DECODE(IDTPLANCROP,'IM',VRITEMLANCROP,0) AS  VRTOTIMPLA
                             FROM ITLANCROPFILIAL IT, LANCROPFILIAL L
                            WHERE ((L.CDFILIAL = V_CDFILIAL) OR (CDFILCENTROP = V_CDFILIAL))
                              AND L.IDTPLANCROP IN ('RE','RP','AJ','IM','BP')
                              AND L.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN
                              AND L.CDFILIAL = IT.CDFILIAL
                              AND L.NRROPFILIAL = IT.NRROPFILIAL
                              AND L.NRLANCROP = IT.NRLANCROP)) A2 ON A1.CDGRUPPROD = A2.CDGRUPPROD
                                                                 AND A1.CDSUBGRPROD = A2.CDSUBGRPROD
                                                                 AND A1.IDTPLANCROP = A2.IDTPLANCROP
             WHERE V_IDFILAGRROP = 'S') X;

            V_DT_NMTIPOOPER := NULL;
         
            FOR C_SAIDDEV IN (SELECT A1.CDGRUPPROD, A1.CDSUBGRPROD, A2.DTLANCROP, A2.CDTIPOOPERROP,
                                     A2.NMTIPOOPER, A2.CDFILAGREROP, NVL(A2.QTTOTCONSSAIDA,0) QTTOTCONSSAIDA,
                                     NVL(A2.VRTOTCONSSAID,0) VRTOTCONSSAID,
                                     NVL((SELECT 'A'
                                            FROM TIPOOPERACROP T
                                           WHERE A2.CDTIPOOPERROP = T.CDTIPOOPER
                                             AND CDFILIAL = C_RESUMO_PAR.CDFILIAL
                                             AND IDENTRSAIDOP = 'S'
                                             AND IDTPOPERACROP = 'CS'),'B') IDTIPOSAIDA, NUMEROREROP
                                FROM (SELECT CDGRUPPROD,S.CDSUBGRPROD, S.NMSUBGRPROD
                                       FROM SUBGRUPPROD S
                                      WHERE CDGRUPPROD = C_RESUMO_PAR.CDGRUPPROD) A1 LEFT JOIN
                              (SELECT DTLANCROP, CDGRUPPRODROP AS CDGRUPPROD,  CDSUBGRPRODROP AS CDSUBGRPROD,
                                      CDTIPOOPERROP, NMTIPOOPER, CDFILCENTROP AS CDFILAGREROP,
                                      SUM(QTITEMLANCROP) QTTOTCONSSAIDA,
                                      SUM(VRITEMLANCROP) VRTOTCONSSAID, NUMEROREROP
                                 FROM LANCROPFILIAL L INNER JOIN ITLANCROPFILIAL IT ON L.CDFILIAL = IT.CDFILIAL
                                                                                   AND L.NRROPFILIAL = IT.NRROPFILIAL
                                                                                   AND L.NRLANCROP = IT.NRLANCROP
                                                      INNER JOIN TIPOOPERAC T ON T.CDTIPOOPER = L.CDTIPOOPERROP
                                WHERE ((L.CDFILIAL = V_CDFILIAL) OR ((CDFILCENTROP = V_CDFILIAL) AND (V_IDFILAGRROP = 'S')))
                                  AND L.IDTPLANCROP = 'CS'
                                  AND L.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN
                                  AND CDGRUPPRODROP = C_RESUMO_PAR.CDGRUPPROD
                                  AND T.IDENTRSAIDOP  = 'S'
                                GROUP BY CDFILCENTROP, CDTIPOOPERROP, NMTIPOOPER, DTLANCROP,
                                      CDGRUPPRODROP, CDSUBGRPRODROP, NUMEROREROP) A2 ON A1.CDGRUPPROD = A2.CDGRUPPROD
                                                                                    AND A1.CDSUBGRPROD = A2.CDSUBGRPROD
                               ORDER BY A1.CDSUBGRPROD, IDTIPOSAIDA DESC, A2.DTLANCROP, A2.CDFILAGREROP DESC,
                                        A1.CDGRUPPROD,A2.CDTIPOOPERROP, A2.NMTIPOOPER) LOOP

               V_VRTOTRELITROP := 0;
               IF (V_DT_NMTIPOOPER IS NULL) OR (V_DT_NMTIPOOPER <> TO_CHAR(C_SAIDDEV.DTLANCROP,'DDMMYYYY')||C_SAIDDEV.NMTIPOOPER) THEN
                  V_DT_NMTIPOOPER := TO_CHAR(C_SAIDDEV.DTLANCROP,'DDMMYYYY')||C_SAIDDEV.NMTIPOOPER;

                  SELECT SUM(NVL(CASE IDTRATIMPROP WHEN 'D'
                                                   THEN VRTOTRELITROP *(-1)
                                                   WHEN 'A'
                                                   THEN VRTOTRELITROP
                                                   ELSE 0
                                 END, 0))
                    INTO V_VRTOTRELITROP
                    FROM (SELECT IDTRATIMPROP, R.CDIMPOSTO, SGIMPOSTO
                            FROM IMPOSTO I INNER JOIN IMPOSTOROP R ON R.CDIMPOSTO = I.CDIMPOSTO
                           WHERE CDFILIAL = C_RESUMO_PAR.CDFILIAL
                             AND IDTPIMPOSTOROP = 'CS') A1 LEFT JOIN (SELECT CDITRELITROP, L.DTLANCROP, NMTIPOOPER, CDFILCENTROP AS CDFILAGREROP,
                                                                             SUM(NVL(QTITRELITROP,0)) QTITRELITROP,
                                                                             SUM(NVL(VRITRELITROP,0)) VRTOTRELITROP
                                                                        FROM LANCROPFILIAL L INNER JOIN ITLANCROPFILIAL IT
                                                                                                     ON L.CDFILIAL = IT.CDFILIAL
                                                                                                    AND L.NRROPFILIAL = IT.NRROPFILIAL
                                                                                                    AND L.NRLANCROP = IT.NRLANCROP
                                                                                             INNER JOIN TIPOOPERAC T
                                                                                                     ON T.CDTIPOOPER = L.CDTIPOOPERROP
                                                                                             INNER JOIN ITRELACITROP IR
                                                                                                     ON IT.CDFILIAL = IR.CDFILIAL
                                                                                                    AND IT.NRROPFILIAL = IR.NRROPFILIAL
                                                                                                    AND IT.NRLANCROP = IR.NRLANCROP
                                                                                                    AND IT.NRITLANCROP = IR.NRITLANCROP
                                                                       WHERE ((L.CDFILIAL = V_CDFILIAL) OR ((CDFILCENTROP = V_CDFILIAL)
                                                                          AND (V_IDFILAGRROP = 'S')))
                                                                         AND L.IDTPLANCROP = 'CS'
                                                                         AND TRUNC(L.DTLANCROP) = TRUNC(C_SAIDDEV.DTLANCROP)
                                                                         AND T.NMTIPOOPER = C_SAIDDEV.NMTIPOOPER
                                                                         AND T.IDENTRSAIDOP = 'S'
                                                                       GROUP BY CDFILCENTROP, L.DTLANCROP,CDITRELITROP, NMTIPOOPER) A2
                                                                  ON A1.CDIMPOSTO = A2.CDITRELITROP;
               END IF;

               IF C_SAIDDEV.IDTIPOSAIDA = 'A' THEN
                  V_SAIDASDEV := NVL(V_SAIDASDEV,0) + NVL(C_SAIDDEV.VRTOTCONSSAID,0) + NVL(V_VRTOTRELITROP,0);
               ELSE
                  V_OUTRASSAIDAS := NVL(V_OUTRASSAIDAS,0) + NVL(C_SAIDDEV.VRTOTCONSSAID,0) + NVL(V_VRTOTRELITROP,0);
               END IF;
            END LOOP;

            V_VRCONSUMOREAL := CASE C_RESUMO_PAR.IDRETIRADAROP WHEN 'C'
                                                               THEN NVL(V_ENTRADASCOMPRA,0) + NVL(V_OUTRASENTRADAS,0) + NVL(V_CONTRANSP,0)
                                                               WHEN 'R'
                                                               THEN NVL(V_ESTOQUEINICIAL,0) + NVL(V_ENTRADASCOMPRA,0) + NVL(V_OUTRASENTRADAS,0) +
                                                                    NVL(V_CONTRANSP,0) - NVL(V_SAIDASDEV,0) - NVL(V_OUTRASSAIDAS,0) - NVL(V_VRTOTINVFINAL,0)
                                                               WHEN 'E'
                                                               THEN NVL(V_VRTOTRETIEST,0)
                                                               WHEN 'P'
                                                               THEN NVL(V_VRTOTRETIPLA,0)
                                                               ELSE NVL(V_VRTOTRETIEST,0) + NVL(V_VRTOTRETIPLA,0)
                               END;

            BEGIN
               SELECT (VRMANUAL + VRCTB + VRCTPAGAR) VRTOTALMO
                 INTO V_VRTOTALMO
                 FROM (SELECT SUM(VRMANUAL) VRMANUAL, SUM(VRCTB) VRCTB, SUM(VRCTPAGAR) VRCTPAGAR
                         FROM (SELECT TIPO,VRMANUAL,VRCTB,VRCTPAGAR
                                 FROM (SELECT 'MANUAL' AS TIPO,NVL(SUM(VRITEMLANCROP),0) AS VRMANUAL,0 VRCTB,0 VRCTPAGAR
                                         FROM ITLANCROPFILIAL IT, LANCROPFILIAL L, DESPADMFATUR D
                                        WHERE L.CDFILIAL         = V_CDFILIAL
                                          AND L.IDTPLANCROP      = 'DM'
                                          AND IT.NRLANCTONFITROP IS NULL
                                          AND L.DTLANCROP        BETWEEN '01/'||TO_CHAR(TO_DATE(P_DATAINI),'MM/YYYY') AND LAST_DAY(P_DATAFIN)
                                          AND L.CDFILIAL         = IT.CDFILIAL
                                          AND L.NRROPFILIAL      = IT.NRROPFILIAL
                                          AND L.NRLANCROP        = IT.NRLANCROP
                                          AND IT.CDITEMLANCROP   = D.CDDESPADMFAT
                                        GROUP BY DTLANCROP, DSDESPADMFAT
                                        UNION ALL
                                        SELECT 'CTPAGAR' AS TIPO,0 AS VRMANUAL,0 AS VRCTB,NVL(SUM(VRITEMLANCROP),0) AS VRCTPAGAR
                                          FROM ITLANCROPFILIAL IT, LANCROPFILIAL L,TIPOCTPAGAR T
                                         WHERE L.CDFILIAL       = V_CDFILIAL
                                           AND L.IDTPLANCROP    = 'DM'
                                           AND L.DTLANCROP      BETWEEN '01/'||TO_CHAR(TO_DATE(P_DATAINI),'MM/YYYY') AND LAST_DAY(P_DATAFIN)
                                           AND L.CDFILIAL       = IT.CDFILIAL
                                           AND L.NRROPFILIAL    = IT.NRROPFILIAL
                                           AND L.NRLANCROP      = IT.NRLANCROP
                                           AND IT.CDITEMLANCROP = T.CDTPCTPAGAR
                                         GROUP BY DTLANCROP,DSTPCTPAGAR
                                         UNION ALL
                                        SELECT DISTINCT 'CTB' AS TIPO,0 AS VRMANUAL,NVL(VRITEMLANCROP,0) AS VRCTB,0 AS VRCTPAGAR
                                          FROM ITLANCROPFILIAL IT, LANCROPFILIAL L,DESPORCMOCCONT D,DESPADMFATUR DF
                                         WHERE L.CDFILIAL       = V_CDFILIAL
                                           AND L.IDTPLANCROP    = 'DM'
                                           AND IT.NRLANCTONFITROP IS NOT NULL
                                           AND L.DTLANCROP      BETWEEN '01/'||TO_CHAR(TO_DATE(P_DATAINI),'MM/YYYY') AND LAST_DAY(P_DATAFIN)
                                           AND L.CDFILIAL       = IT.CDFILIAL
                                           AND L.NRROPFILIAL    = IT.NRROPFILIAL
                                           AND L.NRLANCROP      = IT.NRLANCROP
                                           AND IT.CDITEMLANCROP = D.CDDESPADMFAT
                                           AND D.CDDESPADMFAT   = DF.CDDESPADMFAT)));
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_VRTOTALMO := 0;
            END;

            V_VRMAOOBRAREAL := CASE WHEN C_RESUMO_PAR.IDEXIBEDESPESAMOBRA = 'TOTAL_PERIODO'
                                    THEN ABS(NVL(V_VRTOTALMO,0))
                                    ELSE (ABS(NVL(V_VRTOTALMO,0)) / V_NRDIA_UTIL) * V_NRDIA_REALIZADO
                               END;

            SELECT NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'F'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCFAT,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'I'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCIMP,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'F'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) -
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'I'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCLIQ,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'C'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCCON,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'O'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCMO,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'T'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCTX,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'D'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCDD,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'E'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCDE,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'M'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCDM,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'R'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCIMPRECU,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'S'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCIMPSOBLUC,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'N'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCINV,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'P'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCDEPREC
              INTO V_VRORCFAT, V_VRORCIMP, V_VRORCLIQ, V_VRORCCON, V_VRORCMO, V_VRORCTX,
                   V_VRORCDD, V_VRORCDE, V_VRORCDM, V_VRORCIMPRECU, V_VRORCIMPSOBLUC,
                   V_VRORCINV, V_VRORCDEPREC
              FROM FILIAL F INNER JOIN ITEMORCAOP I ON I.CDFILIAL = F.CDFILIAL
             WHERE I.NRANOORCAOP = TO_CHAR(P_DATAINI,'YYYY')
               AND I.CDFILIAL = P_CDFILIAL
               AND I.CDVERSORCAOP = (SELECT MAX(CDVERSORCAOP)
                                       FROM ITEMORCAOP V
                                      WHERE V.CDFILIAL = P_CDFILIAL
                                        AND V.NRANOORCAOP = TO_CHAR(P_DATAINI,'YYYY'))
               AND I.IDTIPOORCAOP IN ('F','L','C','O','T','D','M','E', 'I', 'R', 'S', 'N', 'P')
               AND I.IDITEMORCAMENTO = 'A';

            BEGIN
               SELECT NVL(VRFATBRUTMAN,0), NVL(VRFATFOSMAN,0), NVL(VRCONSUMOMAN,0), NVL(VROUTRDESPMAN,0), NVL(VRMAOOBRAMAN,0),
                      NVL(VRDEPRBENSMAN,0), NVL(VRCREDIMPMAN,0), NVL(VRTAXAADMMAN,0), NVL(VRINVESTMAN,0), NRPROJECAOMANUALROP
                 INTO VP_VRFATBRUTMAN, VP_VRFATFOSMAN, VP_VRCONSUMOMAN, VP_VROUTRDESPMAN, VP_VRMAOOBRAMAN,
                      VP_VRDEPRBENSMAN, VP_VRCREDIMPMAN, VP_VRTAXAADMMAN, VP_VRINVESTMAN, VP_NRPROJECAOMANUALROP
                 FROM PROJECAOMANUALROP
                WHERE CDFILIAL = V_CDFILIAL
                  AND DTINICIAL = FIRST_DAY(P_DATAINI)
                  AND DTFINAL = LAST_DAY(P_DATAFIN);
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  VP_VRFATBRUTMAN  := NULL; VP_VRFATFOSMAN  := NULL; VP_VRCONSUMOMAN := NULL;
                  VP_VROUTRDESPMAN := NULL; VP_VRMAOOBRAMAN := NULL; VP_VRINVESTMAN  := NULL;
                  VP_VRDEPRBENSMAN := NULL; VP_VRCREDIMPMAN := NULL; VP_VRTAXAADMMAN := NULL;
                  VP_NRPROJECAOMANUALROP := NULL;
               END;
            END;

            IF V_DSFILIAL IS NOT NULL THEN
               V_VRITEMLANCROP_P := CASE WHEN V_CALC_PROJ = 'S'
                                         THEN (V_VRITEMLANCROP/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                                         ELSE V_VRITEMLANCROP
                                    END;

               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := 1;
               OUT_RECORD.DSDESCRICAOTOP  := 'Faturamento Bruto';
               OUT_RECORD.VRREAL          := V_VRITEMLANCROP;
               OUT_RECORD.VRPERREAL       := CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                  THEN 100
                                                  ELSE NULL
                                             END;
               OUT_RECORD.VRPROJECAO      := V_VRITEMLANCROP_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN V_VRITEMLANCROP_P > 0
                                                  THEN CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                            THEN 100
                                                            ELSE NULL
                                                       END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_VRFATBRUTMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_VRORCFAT;
               OUT_RECORD.VRPERORCADO     := CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                  THEN CASE WHEN V_VRORCFAT = 0
                                                            THEN 0
                                                            ELSE 100
                                                       END
                                                  ELSE NULL
                                             END;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               V_NRRESUMO_ROP := 2;

               V_VRIMPOSTOREAL_P := CASE WHEN V_CALC_PROJ = 'S'
                                         THEN (V_VRIMPOSTOREAL/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                                         ELSE V_VRIMPOSTOREAL
                                    END;

               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               OUT_RECORD.DSDESCRICAOTOP  := 'Imposto (+/-)'; /*IMPOSTO*/
               OUT_RECORD.VRREAL          := V_VRIMPOSTOREAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN V_VRITEMLANCROP <> 0
                                                  THEN (V_VRIMPOSTOREAL*100)/V_VRITEMLANCROP
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_VRIMPOSTOREAL_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN V_VRITEMLANCROP_P > 0
                                                  THEN CASE WHEN V_VRIMPOSTOREAL_P <> 0
                                                            THEN (V_VRIMPOSTOREAL_P*100)/V_VRITEMLANCROP_P
                                                            ELSE 0
                                                       END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := NULL;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_VRORCIMP;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCFAT = 0
                                                  THEN 0
                                                  ELSE (V_VRORCIMP*100)/V_VRORCFAT
                                             END;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               V_NRRESUMO_ROP := 3;

               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               OUT_RECORD.DSDESCRICAOTOP  := 'Faturamento ForSale (+)'; /*FATFORSALE*/
               OUT_RECORD.VRREAL          := V_VRFATFOSREAL;
               OUT_RECORD.VRPERREAL       := NULL;
               OUT_RECORD.VRPROJECAO      := CASE WHEN V_CALC_PROJ = 'S'
                                                  THEN (V_VRFATFOSREAL/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                                                  ELSE V_VRFATFOSREAL
                                             END;
               OUT_RECORD.VRPERPROJECAO   := NULL;
               OUT_RECORD.VRPROJMANUAL    := VP_VRFATFOSMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := NULL;
               OUT_RECORD.VRPERORCADO     := NULL;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;

               PIPE ROW (OUT_RECORD);

               IF C_RESUMO_PAR.IDPERFIXIMPDRF = 'S' THEN
                  SELECT SUM(DECODE(R.IDTRATIMPROP, 'A', 1, DECODE(R.IDTRATIMPROP, 'D', -1, 0)) * I.VRITRELITROP) AS VRIMPOSTO
                    INTO V_VRIMPFORSALEREAL
                    FROM LANCROPFILIAL L, ITRELACITROP I, IMPOSTOROP R
                   WHERE L.CDFILIAL = V_CDFILIAL
                     AND L.CDFILIAL = I.CDFILIAL
                     AND L.NRROPFILIAL = I.NRROPFILIAL
                     AND L.NRLANCROP = I.NRLANCROP
                     AND L.IDTPLANCROP = 'FF'
                     AND L.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN
                     AND R.CDFILIAL = TIPOPARAM_DRO(V_CDFILIAL)
                     AND R.CDIMPOSTO = I.CDITRELITROP
                     AND R.IDTPIMPOSTOROP = 'FN';
               ELSE       
                  SELECT SUM(I.VRIMPITLANCROP * (-1)) AS VRIMPOSTO
                    INTO V_VRIMPFORSALEREAL
                    FROM LANCROPFILIAL L, ITLANCROPFILIAL I
                   WHERE L.CDFILIAL = V_CDFILIAL
                     AND L.CDFILIAL = I.CDFILIAL
                     AND L.NRROPFILIAL = I.NRROPFILIAL
                     AND L.NRLANCROP = I.NRLANCROP
                     AND L.IDTPLANCROP = 'FF'
                     AND L.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN;
               END IF;

               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               OUT_RECORD.DSDESCRICAOTOP  := 'Imposto ForSale (+/-)'; /*IMPOSTO FORSALE*/
               OUT_RECORD.VRREAL          := V_VRIMPFORSALEREAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN V_VRITEMLANCROP <> 0
                                                  THEN (V_VRIMPFORSALEREAL*100)/V_VRITEMLANCROP
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_VRIMPFORSALEREAL_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN V_VRITEMLANCROP_P > 0
                                                  THEN CASE WHEN V_VRIMPFORSALEREAL_P <> 0
                                                            THEN (V_VRIMPFORSALEREAL_P*100)/V_VRITEMLANCROP_P
                                                            ELSE 0
                                                       END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := NULL;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := 0;
               OUT_RECORD.VRPERORCADO     := NULL;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               V_NRRESUMO_ROP := 4;

               V_VRFATLIQUIREAL_P := CASE WHEN V_CALC_PROJ = 'S'
                                          THEN (V_VRFATLIQUIREAL/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                                          ELSE V_VRFATLIQUIREAL
                                     END;

               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               OUT_RECORD.DSDESCRICAOTOP  := 'Faturamento Liquido'; /*FATLIQU*/
               OUT_RECORD.VRREAL          := V_VRFATLIQUIREAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO') AND (V_VRFATLIQUIREAL > 0)
                                                  THEN (V_VRFATLIQUIREAL*100)/V_VRITEMLANCROP
                                                  WHEN (NVL(C_RESUMO_PAR.IDCALCPERCENTFAT, 'X') <> 'FATURAMENTO_BRUTO') AND (V_VRFATLIQUIREAL > 0)
                                                  THEN 100
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_VRFATLIQUIREAL_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_VRITEMLANCROP_P > 0) AND (V_VRFATLIQUIREAL_P > 0)
                                                  THEN CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                            THEN (V_VRFATLIQUIREAL_P*100)/V_VRITEMLANCROP_P
                                                            ELSE 100
                                                       END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := NULL;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_VRORCLIQ;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCFAT = 0
                                                  THEN 0
                                                  ELSE CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                            THEN (V_VRORCLIQ*100)/V_VRORCFAT
                                                            ELSE 100
                                                       END
                                             END;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               V_NRRESUMO_ROP := 5;

               V_VRCONSUMOREAL_P := CASE WHEN V_CALC_PROJ = 'S'
                                         THEN (V_VRCONSUMOREAL/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                                         ELSE V_VRCONSUMOREAL
                                    END;

               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := 2;
               OUT_RECORD.DSDESCRICAOTOP  := 'Consumo (-)'; /*CONSUM*/
               OUT_RECORD.VRREAL          := V_VRCONSUMOREAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_VRITEMLANCROP > 0) AND (V_VRFATLIQUIREAL > 0)
                                                  THEN (V_VRCONSUMOREAL*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                   THEN V_VRITEMLANCROP
                                                                                   ELSE V_VRFATLIQUIREAL
                                                                              END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_VRCONSUMOREAL_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_VRITEMLANCROP_P > 0) AND (V_VRCONSUMOREAL_P > 0)
                                                  THEN (V_VRCONSUMOREAL_P*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                     THEN V_VRITEMLANCROP_P
                                                                                     ELSE V_VRFATLIQUIREAL_P
                                                                                END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_VRCONSUMOMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_VRORCCON;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                                  THEN 0
                                                  ELSE (V_VRORCCON*100)/
                                                      CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                           THEN V_VRORCFAT
                                                           ELSE V_VRORCLIQ
                                                      END
                                             END;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               V_VROUTRDESPREAL_P := CASE WHEN V_CALC_PROJ = 'S'
                                          THEN (V_VROUTRDESPREAL/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                                          ELSE V_VROUTRDESPREAL
                                     END;

               V_NRRESUMO_ROP := 6;

               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := 3;
               OUT_RECORD.DSDESCRICAOTOP  := 'Outras Despesas (-)'; /*OUTRDESP*/
               OUT_RECORD.VRREAL          := V_VROUTRDESPREAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_VROUTRDESPREAL > 0) AND (V_VRFATLIQUIREAL > 0)
                                                  THEN (V_VROUTRDESPREAL*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                    THEN V_VRITEMLANCROP
                                                                                    ELSE V_VRFATLIQUIREAL
                                                                               END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_VROUTRDESPREAL_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN V_VRITEMLANCROP_P > 0
                                                  THEN (V_VROUTRDESPREAL_P*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                      THEN V_VRITEMLANCROP_P
                                                                                      ELSE V_VRFATLIQUIREAL_P
                                                                                 END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_VROUTRDESPMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_VRORCDE + V_VRORCDM + V_VRORCDD;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                                  THEN 0
                                                  ELSE ((V_VRORCDE + V_VRORCDM + V_VRORCDD)*100)/
                                                      CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                           THEN V_VRORCFAT
                                                           ELSE V_VRORCLIQ
                                                      END
                                             END;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               V_NRRESUMO_ROP := 7;

               V_VRMAOOBRAREAL_P := CASE WHEN V_CALC_PROJ = 'S'
                                         THEN (V_VRMAOOBRAREAL/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                                         ELSE V_VRMAOOBRAREAL
                                    END;

               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := 4;
               OUT_RECORD.DSDESCRICAOTOP  := 'Mao de Obra (-)'; /*MAOOBRA*/
               OUT_RECORD.VRREAL          := V_VRMAOOBRAREAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN V_VRFATLIQUIREAL > 0
                                                  THEN (V_VRMAOOBRAREAL*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                   THEN V_VRITEMLANCROP
                                                                                   ELSE V_VRFATLIQUIREAL
                                                                              END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_VRMAOOBRAREAL_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN V_VRFATLIQUIREAL_P > 0
                                                  THEN (V_VRMAOOBRAREAL_P*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                  THEN V_VRITEMLANCROP_P
                                                                                  ELSE V_VRFATLIQUIREAL_P
                                                                             END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_VRMAOOBRAMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_VRORCMO;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                                  THEN 0
                                                  ELSE (V_VRORCMO*100)/
                                                      CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                           THEN V_VRORCFAT
                                                           ELSE V_VRORCLIQ
                                                      END
                                             END;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               V_NRRESUMO_ROP := 8;

               V_VRDEPRBENSREAL_P := CASE WHEN V_CALC_PROJ = 'S'
                                          THEN (V_VRDEPRBENSREAL/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                                          ELSE V_VRDEPRBENSREAL
                                     END;

               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               OUT_RECORD.DSDESCRICAOTOP  := 'Depreciacao de Bens (-)'; /*DEPREBENS*/
               OUT_RECORD.VRREAL          := V_VRDEPRBENSREAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_VRDEPRBENSREAL > 0) AND (V_VRFATLIQUIREAL > 0)
                                                  THEN (V_VRDEPRBENSREAL*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                    THEN V_VRITEMLANCROP
                                                                                    ELSE V_VRFATLIQUIREAL
                                                                               END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_VRDEPRBENSREAL_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN V_VRFATLIQUIREAL_P > 0
                                                  THEN (V_VRDEPRBENSREAL_P*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                      THEN V_VRITEMLANCROP_P
                                                                                      ELSE V_VRFATLIQUIREAL_P
                                                                                 END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_VRDEPRBENSMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_VRORCDEPREC;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                                  THEN 0
                                                  ELSE (V_VRORCDEPREC*100)/
                                                      CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                           THEN V_VRORCFAT
                                                           ELSE V_VRORCLIQ
                                                      END
                                             END;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               V_VRCREDIMPREAL_P := CASE WHEN V_CALC_PROJ = 'S'
                                         THEN (V_VRCREDIMPREAL/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                                         ELSE V_VRCREDIMPREAL
                                    END;

               V_NRRESUMO_ROP := 9;

               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               OUT_RECORD.DSDESCRICAOTOP  := 'Credito de Imposto (+)'; /*CREDIMP*/
               OUT_RECORD.VRREAL          := V_VRCREDIMPREAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_VRCREDIMPREAL > 0) AND (V_VRFATLIQUIREAL > 0)
                                                  THEN (V_VRCREDIMPREAL*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                   THEN V_VRITEMLANCROP
                                                                                   ELSE V_VRFATLIQUIREAL
                                                                              END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_VRCREDIMPREAL_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN V_VRFATLIQUIREAL_P > 0
                                                  THEN (V_VRCREDIMPREAL_P*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                     THEN V_VRITEMLANCROP_P
                                                                                     ELSE V_VRFATLIQUIREAL_P
                                                                                END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_VRCREDIMPMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_VRORCIMPRECU;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                                  THEN 0
                                                  ELSE (V_VRORCIMPRECU*100)/
                                                      CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                           THEN V_VRORCFAT
                                                           ELSE V_VRORCLIQ
                                                      END
                                             END;
                                          
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               V_NRRESUMO_ROP := 10;
            
               SELECT DISTINCT NVL(A.TAXAADMINREG,0)+NVL(B.TAXAADMINMATRIZ,0) as TXADM
                 INTO V_TXADM
                 FROM (SELECT 'A' CDCODIGO,F.CDFILIAL, AR.TAXAADMINREG TAXAADMINREG
                         FROM FILIAL F LEFT JOIN AREAGERE AR ON F.CDAREAGERE = AR.CDAREAGERE
                        WHERE F.CDFILIAL = V_CDFILIAL) A LEFT JOIN (SELECT 'A' CDCODIGO,NVL(P.TAXAADMINMATRIZ,0) TAXAADMINMATRIZ
                                                                      FROM PARAMGESTAO P) B ON A.CDCODIGO = B.CDCODIGO;

               V_VRTAXAADMIN := ROUND(CASE WHEN (V_VRFATLIQUIREAL > 0) AND (V_TXADM > 0)
                                     THEN CASE WHEN C_RESUMO_PAR.IDTIPOCALCTAXAADM = 'FATURAMENTO_BRUTO'
                                               THEN V_VRITEMLANCROP
                                               ELSE V_VRFATLIQUIREAL
                                          END / 100 * V_TXADM
                                     ELSE 0
                                END,2);

               V_VRTAXAADMIN_P := ROUND(CASE WHEN V_CALC_PROJ = 'S'
                                       THEN (V_VRTAXAADMIN/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                                       ELSE V_VRTAXAADMIN
                                  END,2);
                                                       
               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               OUT_RECORD.DSDESCRICAOTOP  := 'Taxa Administrativa (-)'; /*TAXAADM*/
               OUT_RECORD.VRREAL          := V_VRTAXAADMIN;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_VRFATLIQUIREAL > 0) AND (V_VRTAXAADMIN > 0)
                                                  THEN (V_VRTAXAADMIN*100)/ CASE WHEN C_RESUMO_PAR.IDTIPOCALCTAXAADM = 'FATURAMENTO_BRUTO'
                                                                                 THEN V_VRITEMLANCROP
                                                                                 ELSE V_VRFATLIQUIREAL
                                                                            END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_VRTAXAADMIN_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN V_VRFATLIQUIREAL_P > 0
                                                  THEN (V_VRTAXAADMIN_P*100)/ CASE WHEN C_RESUMO_PAR.IDTIPOCALCTAXAADM = 'FATURAMENTO_BRUTO'
                                                                                   THEN 1--1NVL(V_VRITEMLANCROP_P,1)
                                                                                   ELSE 1--NVL(V_VRFATLIQUIREAL_P,1)
                                                                              END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_VRTAXAADMMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_VRORCTX;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCTX = 0
                                                  THEN 0
                                                  ELSE (V_VRORCTX*100)/
                                                      CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                           THEN V_VRORCFAT
                                                           ELSE V_VRORCLIQ
                                                      END
                                             END;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               V_NRRESUMO_ROP := 11;

               V_RESULTADOREAL := ROUND(NVL(V_VRFATLIQUIREAL,0) - NVL(V_VRCONSUMOREAL,0) - NVL(V_VROUTRDESPREAL,0) + (NVL(V_VRMAOOBRAREAL,0)*(-1)) -
                                  NVL(V_VRTAXAADMIN,0) + NVL(V_VRCREDIMPREAL,0) - NVL(V_VRDEPRBENSREAL,0),2);
                               
               V_RESULTADOREAL_P := ROUND(NVL(V_VRFATLIQUIREAL_P,0) - NVL(V_VRCONSUMOREAL_P,0) - NVL(V_VROUTRDESPREAL_P,0) + (NVL(V_VRMAOOBRAREAL_P,0)*(-1)) -
                                    NVL(V_VRTAXAADMIN_P,0) + NVL(V_VRCREDIMPREAL_P,0) - NVL(V_VRDEPRBENSREAL_P,0),2);

               V_RESULTADO_O := ROUND(V_VRORCLIQ - V_VRORCCON - V_VRORCDM - V_VRORCDE - V_VRORCDD -
                                V_VRORCMO + V_VRORCIMPRECU - V_VRORCDEPREC,2);
                                        
               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               OUT_RECORD.DSDESCRICAOTOP  := 'Resultado  (=)'; /*RESULT*/
               OUT_RECORD.VRREAL          := V_RESULTADOREAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_VRFATLIQUIREAL > 0) AND (V_VRITEMLANCROP > 0) AND (V_RESULTADOREAL > 0)
                                                  THEN (V_RESULTADOREAL*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                   THEN V_VRITEMLANCROP
                                                                                   ELSE V_VRFATLIQUIREAL
                                                                              END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_RESULTADOREAL_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_VRFATLIQUIREAL_P > 0) AND (V_RESULTADOREAL_P > 0)
                                                  THEN (V_RESULTADOREAL_P*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                     THEN V_VRITEMLANCROP_P
                                                                                     ELSE V_VRFATLIQUIREAL_P
                                                                                END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := NULL;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_RESULTADO_O;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                                  THEN 0
                                                  ELSE (V_RESULTADO_O*100)/
                                                      CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                           THEN V_VRORCFAT
                                                           ELSE V_VRORCLIQ
                                                      END
                                             END;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               V_NRRESUMO_ROP := 12;

               SELECT TRUNC(NVL(SUM(VRITEMLANCROP),0),2) VRINVEST
                 INTO V_VRINVEST
                 FROM LANCROPFILIAL L INNER JOIN ITLANCROPFILIAL IT ON L.CDFILIAL = IT.CDFILIAL
                                                                   AND L.NRROPFILIAL = IT.NRROPFILIAL
                                                                   AND L.NRLANCROP = IT.NRLANCROP
                WHERE ((L.CDFILIAL  = V_CDFILIAL) OR ((CDFILCENTROP  = V_CDFILIAL)AND (V_IDFILAGRROP = 'S')))
                  AND L.IDTPLANCROP = 'IV'
                  AND L.DTLANCROP BETWEEN P_DATAINI AND P_DATAFIN;

               V_VRINVEST_P := TRUNC(CASE WHEN V_CALC_PROJ = 'S'
                                          THEN (V_VRINVEST/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                                          ELSE V_VRINVEST
                                     END,2);

               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := 5;
               OUT_RECORD.DSDESCRICAOTOP  := 'Investimentos (-)'; /*INVEST*/
               OUT_RECORD.VRREAL          := V_VRINVEST;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_VRFATLIQUIREAL > 0) AND (V_VRITEMLANCROP > 0) AND (V_VRINVEST > 0)
                                                  THEN (V_VRINVEST*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                              THEN V_VRITEMLANCROP
                                                                              ELSE V_VRFATLIQUIREAL
                                                                         END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_VRINVEST_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_VRFATLIQUIREAL_P > 0) AND (V_VRINVEST_P > 0)
                                                  THEN (V_VRINVEST_P*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                THEN V_VRITEMLANCROP_P
                                                                                ELSE V_VRFATLIQUIREAL_P
                                                                           END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_VRINVESTMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_VRORCINV;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                                  THEN 0
                                                  ELSE (V_VRORCINV*100)/
                                                      CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                           THEN V_VRORCFAT
                                                           ELSE V_VRORCLIQ
                                                      END
                                             END;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               V_NRRESUMO_ROP := 13;

               V_RESULTOTAL := CASE WHEN C_RESUMO_PAR.IDCALCIMPFAT = 'RESULTTOTAL'
                                    THEN NVL(V_RESULTADOREAL,0) - NVL(V_VRINVEST,0) + NVL(V_VRIMPOSTOREAL,0) + NVL(V_VRIMPFORSALEREAL,0)
                                    ELSE NVL(V_RESULTADOREAL,0) - NVL(V_VRINVEST,0)
                               END;

               V_RESULTOTAL_P := CASE WHEN C_RESUMO_PAR.IDCALCIMPFAT = 'RESULTTOTAL'
                                      THEN NVL(V_RESULTADOREAL_P,0) - NVL(V_VRINVEST_P,0) + NVL(V_VRIMPOSTOREAL_P,0) + NVL(V_VRIMPFORSALEREAL_P,0)
                                      ELSE NVL(V_RESULTADOREAL_P,0) - NVL(V_VRINVEST_P,0)
                                 END;

               V_RESULTOTAL_O := V_VRORCLIQ - V_VRORCCON - V_VRORCDM - V_VRORCDE - V_VRORCDD -
                                 V_VRORCMO + V_VRORCIMPRECU - V_VRORCDEPREC - V_VRORCINV;

               V_NRORDEM := V_NRORDEM + 1;

               OUT_RECORD.NRORDEM         := V_NRORDEM;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               OUT_RECORD.DSDESCRICAOTOP  := 'Resultado Total (=)'; /*RESULTOT*/
               OUT_RECORD.VRREAL          := V_RESULTOTAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_VRFATLIQUIREAL > 0) AND (V_VRITEMLANCROP > 0) --AND (V_RESULTOTAL > 0)
                                                  THEN (V_RESULTOTAL*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                THEN V_VRITEMLANCROP
                                                                                ELSE V_VRFATLIQUIREAL
                                                                           END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_RESULTOTAL_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_VRITEMLANCROP_P > 0) AND (V_RESULTOTAL_P > 0)
                                                  THEN (V_RESULTOTAL_P*100)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                  THEN V_VRITEMLANCROP_P
                                                                                  ELSE V_VRFATLIQUIREAL_P
                                                                             END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := NULL;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_RESULTOTAL_O;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                                  THEN 0
                                                  ELSE (V_RESULTOTAL_O*100)/
                                                      CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                           THEN V_VRORCFAT
                                                           ELSE V_VRORCLIQ
                                                      END
                                             END;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               V_NRRESUMO_ROP := V_NRRESUMO_ROP+1;
            END IF;
         ELSE
            BEGIN
               SELECT NRDIA_UTIL, CASE WHEN NRDIA_REALIZADO = 0 THEN 1 ELSE NRDIA_REALIZADO END NRDIA_REALIZADO,
                      DECODE(DTINICOMP, DTINI, DECODE(DTFINALCOMP, DTFINAL,'N','S'),'S') CALC_PROJ
                 INTO V_NRDIA_UTIL, V_NRDIA_REALIZADO, V_CALC_PROJ
                 FROM (SELECT RETORNA_DIA_UTIL(V_CDFILIAL,P_DATAINI,LAST_DAY(P_DATAFIN)) AS NRDIA_UTIL,
                              RETORNA_DIAS_REALIZADOS(V_CDFILIAL,P_DATAINI,P_DATAFIN) AS NRDIA_REALIZADO,
                              P_DATAFIN AS DTFINAL, P_DATAINI DTINI, LAST_DAY(P_DATAFIN) DTFINALCOMP,
                              FIRST_DAY(P_DATAINI) DTINICOMP
                         FROM DUAL);
            EXCEPTION
              WHEN NO_DATA_FOUND THEN BEGIN
                 V_NRDIA_UTIL  := NULL;
                 V_NRDIA_REALIZADO := NULL;
                 V_CALC_PROJ := NULL;
              END;
            END;

            SELECT NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'F'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCFAT,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'I'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCIMP,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'F'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) -
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'I'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCLIQ,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'C'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCCON,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'O'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCMO,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'T'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCTX,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'D'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCDD,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'E'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCDE,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'M'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCDM,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'R'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCIMPRECU,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'S'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCIMPSOBLUC,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'N'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCINV,
                   NVL(SUM(CASE I.IDTIPOORCAOP WHEN 'P'
                                               THEN CASE TO_CHAR(P_DATAINI, 'MM') WHEN '01' THEN VRORCAMENTOJAN
                                                                                  WHEN '02' THEN VRORCAMENTOFEV
                                                                                  WHEN '03' THEN VRORCAMENTOMAR
                                                                                  WHEN '04' THEN VRORCAMENTOABR
                                                                                  WHEN '05' THEN VRORCAMENTOMAI
                                                                                  WHEN '06' THEN VRORCAMENTOJUN
                                                                                  WHEN '07' THEN VRORCAMENTOJUL
                                                                                  WHEN '08' THEN VRORCAMENTOAGO
                                                                                  WHEN '09' THEN VRORCAMENTOSET
                                                                                  WHEN '10' THEN VRORCAMENTOOUT
                                                                                  WHEN '11' THEN VRORCAMENTONOV
                                                                                  ELSE VRORCAMENTODEZ
                                                    END
                                               ELSE 0
                           END),0) AS VRORCDEPREC
              INTO V_VRORCFAT, V_VRORCIMP, V_VRORCLIQ, V_VRORCCON, V_VRORCMO, V_VRORCTX,
                   V_VRORCDD, V_VRORCDE, V_VRORCDM, V_VRORCIMPRECU, V_VRORCIMPSOBLUC,
                   V_VRORCINV, V_VRORCDEPREC
              FROM FILIAL F INNER JOIN ITEMORCAOP I ON I.CDFILIAL = F.CDFILIAL
             WHERE I.NRANOORCAOP = TO_CHAR(P_DATAINI,'YYYY')
               AND I.CDFILIAL = P_CDFILIAL
               AND I.CDVERSORCAOP = (SELECT MAX(CDVERSORCAOP)
                                       FROM ITEMORCAOP V
                                      WHERE V.CDFILIAL = P_CDFILIAL
                                        AND V.NRANOORCAOP = TO_CHAR(P_DATAINI,'YYYY'))
               AND I.IDTIPOORCAOP IN ('F','L','C','O','T','D','M','E', 'I', 'R', 'S', 'N', 'P')
               AND I.IDITEMORCAMENTO = 'A';

            /* Faturamento */
            V_FATEFETIVO := 0;
            IF (V_IDTPLANCROP = 'FE') OR (V_IDTPLANCROP = 'AB') THEN
               SELECT NVL(SUM(F.VRFATURAMENTO),0)
                 INTO V_FATEFETIVO
                 FROM FATURAMENTOROP F INNER JOIN SERVICO S ON F.CDFILIAL = S.CDFILIAL
                                                           AND F.CDSERVFILI = S.CDSERVFILI
                WHERE F.IDTPLANCROP = 'FAT_EFETIVO'
                  AND F.CDFILIAL = V_CDFILIAL
                  AND F.DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN;
            END IF;

            V_FATNOTAFISC := 0;
            IF V_IDTPLANCROP = 'FS' THEN
               SELECT NVL(SUM(F.VRFATURAMENTO),0)
                 INTO V_FATNOTAFISC
                 FROM FATURAMENTOROP F
                WHERE F.IDTPLANCROP = 'FAT_NOTAFISCAL'
                  AND F.CDFILIAL = V_CDFILIAL                  
                  AND ((('S' = C_RESUMO_PAR.IDDTAPURFAT) AND (F.DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN))
                    OR (('R' = C_RESUMO_PAR.IDDTAPURFAT) AND (F.DTREFERENC BETWEEN P_DATAINI AND P_DATAFIN)));
            END IF;

            IF V_IDTPLANCROP = 'AB' THEN
               SELECT NVL(SUM(F.VRFATURAMENTO),0)
                 INTO V_FATNOTAFISC
                 FROM FATURAMENTOROP F
                WHERE F.IDTPLANCROP = 'FAT_NOTAFISCAL'
                  AND F.CDFILIAL = V_CDFILIAL
                  AND ((('S' = C_RESUMO_PAR.IDDTAPURFAT) AND (F.DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN))
                    OR (('R' = C_RESUMO_PAR.IDDTAPURFAT) AND (F.DTREFERENC BETWEEN P_DATAINI AND P_DATAFIN)))
                  AND F.NRAPURACFAT IS NULL;
            END IF;

            IF C_RESUMO_PAR.IDVOLUNEGOCIO = 'S' THEN
               SELECT NVL(SUM(F.VRFATURAMENTO),0)
                 INTO V_FATRECEXTRA
                 FROM FATURAMENTOROP F
                WHERE F.IDTPLANCROP = 'RECEITA_EXTRA'
                  AND F.CDFILIAL = V_CDFILIAL
                  AND F.DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN;
            ELSE
               V_FATRECEXTRA := 0;
            END IF;

            IF C_RESUMO_PAR.IDCONSFATUDIV = 'S' THEN
               SELECT NVL(SUM(F.VRFATURAMENTO),0)
                 INTO V_FATDIVERSO
                 FROM FATURAMENTOROP F
                WHERE F.IDTPLANCROP = 'FAT_DIVERSOS'
                  AND F.CDFILIAL = V_CDFILIAL
                  AND F.DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN;
            ELSE
               V_FATDIVERSO := 0;
            END IF;

            IF C_RESUMO_PAR.IDGERROPFOS = 'S' THEN
               SELECT NVL(SUM(F.VRFATURAMENTO),0) - NVL(SUM(FD.VRFATURAMENTO),0)
                 INTO V_FATFORSALE
                 FROM FATURAMENTOROP F LEFT JOIN FATURAMENTOROP FD ON F.IDTPLANCROP = FD.IDTPLANCROP
                                                                  AND F.CDFILIAL = FD.CDFILIAL
                                                                  AND F.DTENTRSAID = FD.DTENTRSAID
                                                                  AND FD.IDSTATUS = 'V'
                WHERE F.IDTPLANCROP = 'FAT_FORSALE'
                  AND F.IDSTATUS = 'B'
                  AND F.CDFILIAL = V_CDFILIAL
                  AND F.DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN;
            ELSE
               V_FATFORSALE := 0;
            END IF;

            V_FATDEVCANC := 0;
            IF (V_IDTPLANCROP = 'FS') OR (V_IDTPLANCROP = 'AB') THEN
               SELECT NVL(SUM(F.VRFATDEVCANC),0)
                 INTO V_FATDEVCANC
                 FROM FATURAMENTOROP F
                WHERE F.IDTPLANCROP = 'FAT_NOTAFISCAL'
                  AND F.CDFILIAL = V_CDFILIAL
                  AND F.DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN;
            END IF;

            BEGIN
               SELECT NVL(VRFATBRUTMAN,0), NVL(VRFATFOSMAN,0), NVL(VRCONSUMOMAN,0), NVL(VROUTRDESPMAN,0), NVL(VRMAOOBRAMAN,0),
                      NVL(VRDEPRBENSMAN,0), NVL(VRCREDIMPMAN,0), NVL(VRTAXAADMMAN,0), NVL(VRINVESTMAN,0), NRPROJECAOMANUALROP,
                      NVL(VRFATNFMAN,0), NVL(VRFATDIVMAN,0), NVL(VRFATRECEXTRAMAN,0), NVL(VRIMPOSTOMAN,0),
                      NVL(VRDEDUDEVMAN,0), NVL(VRDESCSOBVENDAMAN,0), NVL(VRENCARGOMAN,0), NVL(VRAJUSTEMAN,0),
                      NVL(VRDEBCREMAN,0) 
                 INTO VP_VRFATBRUTMAN, VP_VRFATFOSMAN, VP_VRCONSUMOMAN, VP_VROUTRDESPMAN, VP_VRMAOOBRAMAN,
                      VP_VRDEPRBENSMAN, VP_VRCREDIMPMAN, VP_VRTAXAADMMAN, VP_VRINVESTMAN, VP_NRPROJECAOMANUALROP,
                      VP_VRFATNFMAN, VP_VRFATDIVMAN, VP_VRFATRECEXTRAMAN, VP_VRIMPOSTOMAN, VP_VRDEDUDEVMAN,
                      VP_VRDESCSOBVENDAMAN, VP_VRENCARGOMAN, VP_VRAJUSTEMAN, VP_VRDEBCREMAN
                 FROM PROJECAOMANUALROP
                WHERE CDFILIAL = V_CDFILIAL
                  AND DTINICIAL = FIRST_DAY(P_DATAINI)
                  AND DTFINAL = LAST_DAY(P_DATAFIN);
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  VP_VRFATBRUTMAN  := 0; VP_VRFATFOSMAN  := 0; VP_VRCONSUMOMAN := 0;
                  VP_VROUTRDESPMAN := 0; VP_VRMAOOBRAMAN := 0; VP_VRINVESTMAN  := 0;
                  VP_VRDEPRBENSMAN := 0; VP_VRCREDIMPMAN := 0; VP_VRTAXAADMMAN := 0;
                  VP_NRPROJECAOMANUALROP := NULL;
               END;
            END;

            SELECT NVL(SUM(CASE R.IDTRATIMPROP WHEN 'D' THEN NVL(D.VRDEDUCAO,0)*(-1)
                                               WHEN 'V' THEN 0
                                               ELSE D.VRDEDUCAO
                           END),0)
              INTO V_DEDUIMPO
              FROM DEDUCAOROP D INNER JOIN IMPOSTOROP R ON D.CDIMPOSTO = R.CDIMPOSTO
                                                       AND R.CDFILIAL = TIPOPARAM_DRO(D.CDFILIAL)
                                INNER JOIN PARFILIALROP P ON P.CDFILIAL = TIPOPARAM_DRO(D.CDFILIAL)
             WHERE D.CDFILIAL = V_CDFILIAL
               AND D.DTREFERENC BETWEEN P_DATAINI AND P_DATAFIN
               AND ((D.IDTPLANCROP = 'FAT_NOTAFISCAL' AND R.IDTPIMPOSTOROP IN ('FS')) OR
                    (D.IDTPLANCROP = 'FAT_EFETIVO' AND R.IDTPIMPOSTOROP IN ('FE')))
               AND (R.IDTPIMPOSTOROP = P.IDFATUROP OR P.IDFATUROP = 'AB');

            IF C_RESUMO_PAR.IDLANCIMPPAG = 'S' THEN
               SELECT NVL(SUM(D.VRDEDUCAO),0)+V_DEDUIMPO
                 INTO V_DEDUIMPO
                 FROM DEDUCAOROP D
                WHERE D.CDFILIAL = V_CDFILIAL
                  AND D.DTREFERENC BETWEEN P_DATAINI AND P_DATAFIN
                  AND D.IDTPLANCROP = 'CONTA_PAGAR';
            END IF;

            IF C_RESUMO_PAR.IDPERFIXIMPDRF = 'S' THEN
               SELECT CASE R.IDTRATIMPROP WHEN 'A' THEN 1
                                          WHEN 'D' THEN -1
                                          ELSE 0
                      END * D.VRDEDUCAO
                 INTO V_DEDUIMPOFOS
                 FROM DEDUCAOROP D, IMPOSTOROP R
                WHERE D.CDFILIAL = V_CDFILIAL
                  AND D.IDTPLANCROP = 'FAT_FORSALE'
                  AND D.DTREFERENC BETWEEN P_DATAINI AND P_DATAFIN
                  AND R.CDFILIAL = TIPOPARAM_DRO(V_CDFILIAL)
                  AND R.CDIMPOSTO = D.CDIMPOSTO
                  AND R.IDTPIMPOSTOROP = 'FN';
            ELSE
               SELECT SUM(D.VRDEDUCAO)
                 INTO V_DEDUIMPOFOS
                 FROM DEDUCAOROP D
                WHERE D.CDFILIAL = V_CDFILIAL
                  AND D.IDTPLANCROP = 'FAT_FORSALE'
                  AND D.DTREFERENC BETWEEN P_DATAINI AND P_DATAFIN;
            END IF;

            IF C_RESUMO_PAR.IDPERCFIXIMPREC <> 'S' THEN
               SELECT NVL(SUM(CASE R.IDTRATIMPROP WHEN 'D' THEN NVL(D.VRDEDUCAO,0)*(-1)
                                                           ELSE D.VRDEDUCAO
                              END),0)
                 INTO V_CREDIMPO
                 FROM DEDUCAOROP D INNER JOIN IMPOSTOROP R ON D.CDIMPOSTO = R.CDIMPOSTO
                                                          AND R.CDFILIAL = TIPOPARAM_DRO(D.CDFILIAL)
                                   INNER JOIN IMPOSTO I ON D.CDIMPOSTO = I.CDIMPOSTO,
                      PARFILIALROP P
                WHERE D.CDFILIAL = V_CDFILIAL
                  AND D.DTREFERENC BETWEEN P_DATAINI AND P_DATAFIN
                  AND D.IDTPLANCROP = 'CONSUMO_ENTRADA'
                  AND R.IDTPIMPOSTOROP IN ('CC')
                  AND P.CDFILIAL = TIPOPARAM_DRO(D.CDFILIAL)
                  AND P.IDPERCFIXIMPREC <> 'S';
            ELSE
               SELECT (NVL(SUM(C.VRITEMLANCROP),0)*NVL(MAX(P.VRPERIMPRECUROP),0))/100
                 INTO V_CREDIMPO
                 FROM CONSUMOROP C INNER JOIN FILIALROP F ON C.NRFILIALROP = F.NRFILIALROP
                                   INNER JOIN FORNECEDOR FO ON FO.CDFORNECED = C.CDCLIENTEROP,
                      PARFILIALROP P
                WHERE C.IDTPLANCROP = 'CONSUMO_ENTRADA'
                  AND F.CDFILIAL = V_CDFILIAL
                  AND F.DTFILIALROP = C.DTCONSUMROP
                  AND F.DTFILIALROP BETWEEN P_DATAINI AND P_DATAFIN
                  AND C.CDTIPOOPERROP <> '06'
                  AND P.CDFILIAL = TIPOPARAM_DRO(F.CDFILIAL)
                  AND P.IDPERCFIXIMPREC = 'S';
            END IF;

            IF C_RESUMO_PAR.IDGERROPFOS = 'S' THEN
               SELECT NVL(SUM(F.VRFATURAMENTO),0)
                 INTO V_DESCSOBVENDA
                 FROM FATURAMENTOROP F
                WHERE F.IDTPLANCROP = 'FAT_FORSALE'
                  AND F.IDSTATUS = 'D'
                  AND F.CDFILIAL = V_CDFILIAL
                  AND F.DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN;
            ELSE
               V_DESCSOBVENDA := 0;
            END IF;

            SELECT V_DESCSOBVENDA + NVL(SUM(I.VRITEMENCA*(CASE E.IDENCARGO WHEN 'A' THEN (-1)
                                                                           WHEN 'V' THEN 0
                                                                           ELSE 1
                                                          END)),0)
              INTO V_DESCSOBVENDA
              FROM FATURAMENTOROP F INNER JOIN ITEMENCARGO I ON F.CDFILIALLANC = I.CDFILIAL
                                                            AND F.NRLANCTONF = I.NRLANCTONF
                                    INNER JOIN DEDURECEITAROP D ON D.CDFILIAL = C_RESUMO_PAR.CDFILIAL
                                                               AND D.CDENCARGO = I.CDENCARGO    
                                    INNER JOIN ENCARGO E ON I.CDENCARGO = E.CDENCARGO
             WHERE F.IDTPLANCROP = 'FAT_NOTAFISCAL'
               AND F.CDFILIAL = V_CDFILIAL
               AND ((('S' = C_RESUMO_PAR.IDDTAPURFAT) AND (F.DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN))
                 OR (('R' = C_RESUMO_PAR.IDDTAPURFAT) AND (F.DTREFERENC BETWEEN P_DATAINI AND P_DATAFIN)));

            SELECT NVL(SUM(I.VRITEMENCA*(CASE E.IDENCARGO WHEN 'A' THEN (-1)
                                                          WHEN 'V' THEN 0
                                                          ELSE 1
                                         END)),0)
              INTO V_ENCARGOS
              FROM FATURAMENTOROP F INNER JOIN ITEMENCARGO I ON F.CDFILIALLANC = I.CDFILIAL
                                                            AND F.NRLANCTONF = I.NRLANCTONF
                                    INNER JOIN ENCARGO E ON I.CDENCARGO = E.CDENCARGO
             WHERE F.IDTPLANCROP = 'FAT_NOTAFISCAL'
               AND F.CDFILIAL = V_CDFILIAL
               AND ((('S' = C_RESUMO_PAR.IDDTAPURFAT) AND (F.DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN))
                 OR (('R' = C_RESUMO_PAR.IDDTAPURFAT) AND (F.DTREFERENC BETWEEN P_DATAINI AND P_DATAFIN)))
               AND NOT EXISTS (SELECT 1
                                 FROM DEDURECEITAROP D
                                WHERE D.CDFILIAL = C_RESUMO_PAR.CDFILIAL
                                  AND D.CDENCARGO = I.CDENCARGO);

            V_BRUTO := V_FATEFETIVO + V_FATNOTAFISC + V_FATRECEXTRA + V_FATFORSALE + V_FATDIVERSO;

            V_LIQUIDO := CASE WHEN C_RESUMO_PAR.IDCALCIMPFAT = 'FATBRUTO'
                              THEN V_BRUTO - (NVL(V_DEDUIMPO,0) + NVL(V_DEDUIMPOFOS,0) + NVL(V_CREDIMPO,0) + NVL(V_DESCSOBVENDA,0) + NVL(V_ENCARGOS,0) + NVL(V_FATDEVCANC,0))
                              ELSE V_BRUTO - (NVL(V_CREDIMPO,0) + NVL(V_DESCSOBVENDA,0) + NVL(V_ENCARGOS,0) + NVL(V_FATDEVCANC,0))
                         END;

            V_AUX := NVL(V_FATEFETIVO,0) + NVL(V_FATNOTAFISC,0) + NVL(V_FATRECEXTRA,0) + NVL(V_FATFORSALE,0) +
                     NVL(V_FATDIVERSO,0);
            V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                            THEN (V_AUX/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                            ELSE V_AUX
                       END;

            OUT_RECORD.NRRESUMO_ROP    := 0;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := 1;
            OUT_RECORD.DSDESCRICAOTOP  := 'FAT';
            /*OUT_RECORD.DSDESCRICAOTOP  := 'Faturamento (=)';*/
            OUT_RECORD.VRREAL          := V_AUX;
            OUT_RECORD.VRPERREAL       := CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                               THEN 1
                                               ELSE NULL
                                          END;
            OUT_RECORD.VRPROJECAO      := V_AUX_P;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN V_AUX_P > 0
                                               THEN CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                         THEN 1
                                                         ELSE NULL
                                                    END
                                               ELSE 0
                                          END;
            OUT_RECORD.VRPROJMANUAL    := VP_VRFATBRUTMAN + VP_VRFATFOSMAN + VP_VRFATNFMAN + VP_VRFATDIVMAN + VP_VRFATRECEXTRAMAN;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_VRORCFAT;
            OUT_RECORD.VRPERORCADO     := CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                               THEN CASE WHEN V_VRORCFAT = 0
                                                         THEN 0
                                                         ELSE 1
                                                    END
                                               ELSE NULL
                                          END;
            OUT_RECORD.NRORDEM         := 1;
            OUT_RECORD.NRORG           := C_RESUMO_PAR.NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
            PIPE ROW (OUT_RECORD);

            VP_RESULTADO := NVL(VP_VRFATBRUTMAN,0) + NVL(VP_VRFATFOSMAN,0) + NVL(VP_VRFATNFMAN,0) + NVL(VP_VRFATDIVMAN,0) + NVL(VP_VRFATRECEXTRAMAN,0);

            IF (V_IDTPLANCROP = 'FE') OR (V_IDTPLANCROP = 'AB') THEN
               V_NRRESUMO_ROP := 1;

               V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                               THEN (V_FATEFETIVO/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                               ELSE V_FATEFETIVO
                          END;

               OUT_RECORD.NRORDEM         := 2;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Faturamento Efetivos (+)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'FATEFET';
               OUT_RECORD.VRREAL          := NVL(V_FATEFETIVO,0);
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0) AND (V_FATEFETIVO > 0)
                                                  THEN V_FATEFETIVO/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                          THEN V_BRUTO
                                                                          ELSE V_LIQUIDO
                                                                     END
                                                  ELSE 0
                                              END;
               OUT_RECORD.VRPROJECAO      := V_AUX_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                     THEN V_BRUTO
                                                                     ELSE V_LIQUIDO
                                                                END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_VRFATBRUTMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := 0;
               OUT_RECORD.VRPERORCADO     := 0;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);
            END IF;

            IF (V_IDTPLANCROP = 'FS') OR (V_IDTPLANCROP = 'AB') THEN
               V_NRRESUMO_ROP := 0;
               
               V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                               THEN (V_FATNOTAFISC/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                               ELSE V_FATNOTAFISC
                          END;

               OUT_RECORD.NRORDEM         := 3;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               OUT_RECORD.DSDESCRICAOTOP  := 'FATNF';
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Faturamento NF'||CASE WHEN V_IDTPLANCROP = 'AB' THEN '*' ELSE '' END||' (+)';*/
               OUT_RECORD.VRREAL          := NVL(V_FATNOTAFISC,0);
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0) AND (V_FATNOTAFISC > 0)
                                                  THEN V_FATNOTAFISC/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                           THEN V_BRUTO
                                                                           ELSE V_LIQUIDO
                                                                      END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_AUX_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                     THEN V_BRUTO
                                                                     ELSE V_LIQUIDO
                                                                END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_VRFATNFMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := 0;
               OUT_RECORD.VRPERORCADO     := 0;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);
            END IF;

            IF C_RESUMO_PAR.IDGERROPFOS = 'S' THEN
               V_NRRESUMO_ROP := 3;

               V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                               THEN (V_FATFORSALE/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                               ELSE V_FATFORSALE
                          END;

               OUT_RECORD.NRORDEM         := 4;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Faturamento ForSale (+)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'FATFOR';
               OUT_RECORD.VRREAL          := NVL(V_FATFORSALE,0);
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0) AND (V_FATFORSALE > 0)
                                                  THEN V_FATFORSALE/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                          THEN V_BRUTO
                                                                          ELSE V_LIQUIDO
                                                                     END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_AUX_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                     THEN V_BRUTO
                                                                     ELSE V_LIQUIDO
                                                                END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_VRFATFOSMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := 0;
               OUT_RECORD.VRPERORCADO     := 0;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);
            END IF;

            IF C_RESUMO_PAR.IDCONSFATUDIV = 'S' THEN
               V_NRRESUMO_ROP := 0;

               V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                               THEN (V_FATDIVERSO/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                               ELSE V_FATDIVERSO
                          END;

               OUT_RECORD.NRORDEM         := 5;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Faturamento Diversos (+)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'FATDIV';
               OUT_RECORD.VRREAL          := NVL(V_FATDIVERSO,0);
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0) AND (V_FATRECEXTRA > 0)
                                                  THEN V_FATDIVERSO/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                          THEN V_BRUTO
                                                                          ELSE V_LIQUIDO
                                                                     END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_AUX_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                     THEN V_BRUTO
                                                                     ELSE V_LIQUIDO
                                                                END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_VRFATDIVMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := 0;
               OUT_RECORD.VRPERORCADO     := 0;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);
            END IF;

            IF C_RESUMO_PAR.IDVOLUNEGOCIO = 'S' THEN
               V_NRRESUMO_ROP := 0;

               V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                               THEN (V_FATRECEXTRA/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                               ELSE V_FATRECEXTRA
                          END;

               OUT_RECORD.NRORDEM         := 5;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Faturamento Diversos (+)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'FATRECEXTRA';
               OUT_RECORD.VRREAL          := NVL(V_FATRECEXTRA,0);
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0) AND (V_FATRECEXTRA > 0)
                                                  THEN V_FATRECEXTRA/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                           THEN V_BRUTO
                                                                           ELSE V_LIQUIDO
                                                                      END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_AUX_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                     THEN V_BRUTO
                                                                     ELSE V_LIQUIDO
                                                                END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_VRFATRECEXTRAMAN;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := 0;
               OUT_RECORD.VRPERORCADO     := 0;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);
            END IF;

            V_NRRESUMO_ROP := 0;

            V_AUX := CASE WHEN C_RESUMO_PAR.IDCALCIMPFAT = 'FATBRUTO'
                          THEN NVL(V_DEDUIMPO,0) + NVL(V_DEDUIMPOFOS,0) + NVL(V_CREDIMPO,0) + NVL(V_DESCSOBVENDA,0) + NVL(V_ENCARGOS,0) + NVL(V_FATDEVCANC,0)
                          ELSE NVL(V_CREDIMPO,0) + NVL(V_DESCSOBVENDA,0) + NVL(V_ENCARGOS,0) + NVL(V_FATDEVCANC,0)
                     END;
            V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                            THEN (V_AUX/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                            ELSE V_AUX
                       END;

            OUT_RECORD.NRORDEM         := 6;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := 2;
            /*OUT_RECORD.DSDESCRICAOTOP  := 'Deducoes da Receita (=)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'DEDUREC';
            OUT_RECORD.VRREAL          := V_AUX * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN V_AUX/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                THEN V_BRUTO
                                                                ELSE V_LIQUIDO
                                                           END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                  THEN V_BRUTO
                                                                  ELSE V_LIQUIDO
                                                             END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := VP_VRIMPOSTOMAN + VP_VRCREDIMPMAN + VP_VRDEDUDEVMAN + VP_VRDESCSOBVENDAMAN + VP_VRENCARGOMAN;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := (V_VRORCIMP + V_VRORCIMPRECU) * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                               THEN 0
                                               ELSE ((V_VRORCIMP + V_VRORCIMPRECU)*100)/
                                                   CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                        THEN V_VRORCFAT
                                                        ELSE V_VRORCLIQ
                                                   END
                                          END * V_SINAL;
            OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
            PIPE ROW (OUT_RECORD);

            VP_RESULTADO := VP_RESULTADO - NVL(VP_VRIMPOSTOMAN,0) + NVL(VP_VRCREDIMPMAN,0) + NVL(VP_VRDEDUDEVMAN,0) + NVL(VP_VRDESCSOBVENDAMAN,0) + NVL(VP_VRENCARGOMAN,0);

            V_NRRESUMO_ROP := 2;

            V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                            THEN (NVL(V_DEDUIMPO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                            ELSE NVL(V_DEDUIMPO,0)
                       END;

            OUT_RECORD.NRORDEM         := 7;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := '    Imposto (+/-)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'DEDUIM';
            OUT_RECORD.VRREAL          := NVL(V_DEDUIMPO,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_DEDUIMPO,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                            THEN V_BRUTO
                                                                            ELSE V_LIQUIDO
                                                                       END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                  THEN V_BRUTO
                                                                  ELSE V_LIQUIDO
                                                             END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := VP_VRIMPOSTOMAN;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_VRORCIMP * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                               THEN 0
                                               ELSE (V_VRORCIMP*100)/
                                                   CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                        THEN V_VRORCFAT
                                                        ELSE V_VRORCLIQ
                                                   END
                                          END * V_SINAL;
            OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
            PIPE ROW (OUT_RECORD);            

            V_NRRESUMO_ROP := 2;

            V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                            THEN (NVL(V_DEDUIMPOFOS,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                            ELSE NVL(V_DEDUIMPOFOS,0)
                       END;

            OUT_RECORD.NRORDEM         := 7;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := '    Imposto ForSale (+/-)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'DEDUIMFOS';
            OUT_RECORD.VRREAL          := NVL(V_DEDUIMPOFOS,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_DEDUIMPOFOS,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                            THEN V_BRUTO
                                                                            ELSE V_LIQUIDO
                                                                       END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                  THEN V_BRUTO
                                                                  ELSE V_LIQUIDO
                                                             END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := VP_VRIMPOSTOMAN * V_SINAL;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_VRORCIMP * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                               THEN 0
                                               ELSE (V_VRORCIMP*100)/
                                                   CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                        THEN V_VRORCFAT
                                                        ELSE V_VRORCLIQ
                                                   END
                                          END * V_SINAL;
            OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
            PIPE ROW (OUT_RECORD);

            V_NRRESUMO_ROP := 9;

            V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                            THEN (NVL(V_CREDIMPO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                            ELSE NVL(V_CREDIMPO,0)
                       END;


            OUT_RECORD.NRORDEM         := 8;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := '    Credito de Imposto (+)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'DEDUCR';
            OUT_RECORD.VRREAL          := NVL(V_CREDIMPO,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_CREDIMPO,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                            THEN V_BRUTO
                                                                            ELSE V_LIQUIDO
                                                                       END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                  THEN V_BRUTO
                                                                  ELSE V_LIQUIDO
                                                             END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := VP_VRCREDIMPMAN * V_SINAL;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_VRORCIMPRECU * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                               THEN 0
                                               ELSE (V_VRORCIMPRECU*100)/
                                                   CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                        THEN V_VRORCFAT
                                                        ELSE V_VRORCLIQ
                                                   END
                                          END * V_SINAL;
            OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
            PIPE ROW (OUT_RECORD);

            IF (V_IDTPLANCROP = 'FS') OR (V_IDTPLANCROP = 'AB') THEN
               V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                               THEN (V_FATDEVCANC/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                               ELSE V_FATDEVCANC
                          END;

               V_NRRESUMO_ROP := 0;
               OUT_RECORD.NRORDEM         := 9;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Devolucoes/Estornos (-)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'DEDUDEV';
               OUT_RECORD.VRREAL          := NVL(V_FATDEVCANC,0) * V_SINAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN V_FATDEVCANC/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                          THEN V_BRUTO
                                                                          ELSE V_LIQUIDO
                                                                     END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                     THEN V_BRUTO
                                                                     ELSE V_LIQUIDO
                                                                END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJMANUAL    := VP_VRDEDUDEVMAN * V_SINAL;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := 0;
               OUT_RECORD.VRPERORCADO     := 0;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;

               PIPE ROW (OUT_RECORD);
            END IF;

            V_NRRESUMO_ROP := 0;

            /* IF C_RESUMO_PAR.IDGERROPFOS = 'S' THEN */
            V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                            THEN (NVL(V_DESCSOBVENDA,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                            ELSE NVL(V_DESCSOBVENDA,0)
                       END;

            OUT_RECORD.NRORDEM         := 10;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := '    Descontos Sobre Vendas (-)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'DEDUDES';
            OUT_RECORD.VRREAL          := NVL(V_DESCSOBVENDA,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_DESCSOBVENDA,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                THEN V_BRUTO
                                                                                ELSE V_LIQUIDO
                                                                           END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                  THEN V_BRUTO
                                                                  ELSE V_LIQUIDO
                                                             END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := VP_VRDESCSOBVENDAMAN * V_SINAL;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := 0;
            OUT_RECORD.VRPERORCADO     := 0;
            OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
            PIPE ROW (OUT_RECORD);

            V_NRRESUMO_ROP := 0;
            /* END IF; */

            V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                            THEN (NVL(V_ENCARGOS,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                            ELSE NVL(V_ENCARGOS,0)
                       END;

            OUT_RECORD.NRORDEM         := 11;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := '    Encargos (+/-)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'DEDUEN';
            OUT_RECORD.VRREAL          := NVL(V_ENCARGOS,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_ENCARGOS,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                            THEN V_BRUTO
                                                                            ELSE V_LIQUIDO
                                                                       END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                  THEN V_BRUTO
                                                                  ELSE V_LIQUIDO
                                                             END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := VP_VRENCARGOMAN * V_SINAL;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := 0;
            OUT_RECORD.VRPERORCADO     := 0;
            OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
            PIPE ROW (OUT_RECORD);

            V_NRRESUMO_ROP := 4;

            V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                            THEN (NVL(V_LIQUIDO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                            ELSE NVL(V_LIQUIDO,0)
                       END;

            OUT_RECORD.NRORDEM         := 12;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := 'Faturamento Liquido (=)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'FATLIQ';
            OUT_RECORD.VRREAL          := NVL(V_LIQUIDO,0);
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_LIQUIDO,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                           THEN V_BRUTO
                                                                           ELSE V_LIQUIDO
                                                                      END
                                               ELSE 0
                                          END;
            OUT_RECORD.VRPROJECAO      := V_AUX_P;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                  THEN V_BRUTO
                                                                  ELSE V_LIQUIDO
                                                             END
                                               ELSE 0
                                          END;
            OUT_RECORD.VRPROJMANUAL    := VP_RESULTADO;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_VRORCLIQ;
            OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                               THEN 0
                                               ELSE (V_VRORCLIQ*100)/
                                                   CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                        THEN V_VRORCFAT
                                                        ELSE V_VRORCLIQ
                                                   END
                                          END;
            OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
            PIPE ROW (OUT_RECORD);

            V_NRRESUMO_ROP := 10;

            SELECT NVL(SUM(D.VRDESPESA),0)
              INTO V_VRTAXAADMIN
              FROM DESPESASROP D INNER JOIN FILIALROP F ON D.NRFILIALROP = F.NRFILIALROP
             WHERE D.IDTPLANCROP = 'TAXAADM'
               AND F.CDFILIAL = V_CDFILIAL
               AND D.DTDESPESA = F.DTFILIALROP
               AND F.DTFILIALROP BETWEEN P_DATAINI AND P_DATAFIN;

            V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                            THEN (NVL(V_VRTAXAADMIN,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                            ELSE NVL(V_VRTAXAADMIN,0)
                       END;

            OUT_RECORD.NRORDEM         := 13;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := 'Taxa Administrativa (-)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'TAXADM';
            OUT_RECORD.VRREAL          := NVL(V_VRTAXAADMIN,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_VRTAXAADMIN,0)/ CASE WHEN C_RESUMO_PAR.IDTIPOCALCTAXAADM = 'FATURAMENTO_BRUTO'
                                                                               THEN V_BRUTO
                                                                               ELSE V_LIQUIDO
                                                                          END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDTIPOCALCTAXAADM = 'FATURAMENTO_BRUTO'
                                                                  THEN V_BRUTO
                                                                  ELSE V_LIQUIDO
                                                             END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := VP_VRTAXAADMMAN * V_SINAL;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_VRORCTX * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                               THEN 0
                                               ELSE (V_VRORCTX*100)/
                                                   CASE WHEN C_RESUMO_PAR.IDTIPOCALCTAXAADM = 'FATURAMENTO_BRUTO'
                                                        THEN V_VRORCFAT
                                                        ELSE V_VRORCLIQ
                                                   END
                                          END * V_SINAL;
            OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
            PIPE ROW (OUT_RECORD);

            V_NRRESUMO_ROP := 5;

            SELECT NVL(MAX(A.VRCONSUMO),0)
              INTO V_VRCONSUMOREAL
              FROM TABLE(APURCONS_ROP(P_CDFILIAL,P_DATAINI, P_DATAFIN)) A
             WHERE A.NMSUBGRPROD = 'Valor Total';
             
            V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                            THEN (NVL(V_VRCONSUMOREAL,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                            ELSE NVL(V_VRCONSUMOREAL,0)
                       END;

            OUT_RECORD.NRORDEM         := 14;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := 3;
            /*OUT_RECORD.DSDESCRICAOTOP  := 'Consumo (CMV) (-)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'CMV';
            OUT_RECORD.VRREAL          := NVL(V_VRCONSUMOREAL,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_VRCONSUMOREAL,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                 THEN V_BRUTO
                                                                                 ELSE V_LIQUIDO
                                                                            END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                  THEN V_BRUTO
                                                                  ELSE V_LIQUIDO
                                                             END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := VP_VRCONSUMOMAN * V_SINAL;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_VRORCCON * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                               THEN 0
                                               ELSE (V_VRORCCON*100)/
                                                   CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                        THEN V_VRORCFAT
                                                        ELSE V_VRORCLIQ
                                                   END
                                          END * V_SINAL;
            OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
            PIPE ROW (OUT_RECORD);

            VP_RESULTADO := VP_RESULTADO - NVL(VP_VRTAXAADMMAN,0) - NVL(VP_VRCONSUMOMAN,0);

            V_NRRESUMO_ROP := 0;

            V_AUX := NVL(V_LIQUIDO,0) - NVL(V_VRTAXAADMIN,0) - NVL(V_VRCONSUMOREAL,0);

            V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                            THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                            ELSE NVL(V_AUX,0)
                       END;

            OUT_RECORD.NRORDEM         := 15;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := 'Margem de Contribuicao (=)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'MARCONT';
            OUT_RECORD.VRREAL          := V_AUX;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_AUX,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                       THEN V_BRUTO
                                                                       ELSE V_LIQUIDO
                                                                  END
                                               ELSE 0
                                          END;
            OUT_RECORD.VRPROJECAO      := V_AUX_P;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                  THEN V_BRUTO
                                                                  ELSE V_LIQUIDO
                                                             END
                                               ELSE 0
                                          END;
            OUT_RECORD.VRPROJMANUAL    := VP_RESULTADO;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_VRORCLIQ - V_VRORCTX - V_VRORCCON;
            OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                               THEN 0
                                               ELSE ((V_VRORCLIQ - V_VRORCTX - V_VRORCCON)*100)/
                                                   CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                        THEN V_VRORCFAT
                                                        ELSE V_VRORCLIQ
                                                   END
                                          END;
            OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
            PIPE ROW (OUT_RECORD);

            V_NRRESUMO_ROP := 6;

            SELECT CASE WHEN C_RESUMO_PAR.IDEXIBEDESPESAMOBRA = 'TOTAL_PERIODO'
                        THEN NVL(SUM(D.VRDESPESA),0)
                        ELSE (NVL(SUM(D.VRDESPESA),0) / V_NRDIA_UTIL) * V_NRDIA_REALIZADO
                   END
              INTO V_DESPESA
              FROM DESPESASROP D INNER JOIN FILIALROP F ON D.NRFILIALROP = F.NRFILIALROP
             WHERE D.IDTPLANCROP = 'DESPESA'
               AND NVL(D.IDTIPODESP,'LANC_ENTRADA') IN ('LANC_ENTRADA','CONTA_PAGAR')
               AND F.CDFILIAL = V_CDFILIAL
               AND D.DTDESPESA = F.DTFILIALROP
               AND F.DTFILIALROP BETWEEN P_DATAINI AND P_DATAFIN;

            IF (C_RESUMO_PAR.IDCONSLANCAJUSPAG = 'S') OR (C_RESUMO_PAR.IDCONSLANCAJUSREC = 'S') THEN
               SELECT NVL(SUM(D.VRDESPESA),0)
                 INTO V_AJUSTE
                 FROM DESPESASROP D INNER JOIN FILIALROP F ON D.NRFILIALROP = F.NRFILIALROP
                WHERE D.IDTPLANCROP = 'AJUSTECT'
                  AND D.IDTIPODESP IN ('CONTA_PAGAR', 'CONTA_RECEBER')
                  AND F.CDFILIAL = V_CDFILIAL
                  AND D.DTDESPESA = F.DTFILIALROP
                  AND F.DTFILIALROP BETWEEN P_DATAINI AND P_DATAFIN;
            ELSE
               V_AJUSTE := 0;
            END IF;

            IF (C_RESUMO_PAR.IDCONSCREDBCO = 'S') OR (C_RESUMO_PAR.IDCONSDEBBCO = 'S') THEN
               SELECT NVL(SUM(D.VRDESPESA),0)
                 INTO V_DEBCRED
                 FROM DESPESASROP D INNER JOIN FILIALROP F ON D.NRFILIALROP = F.NRFILIALROP
                WHERE D.IDTPLANCROP = 'AJUSTECT'
                  AND D.IDTIPODESP IN ('DEBITO', 'CREDITO')
                  AND F.CDFILIAL = V_CDFILIAL
                  AND D.DTDESPESA = F.DTFILIALROP
                  AND F.DTFILIALROP BETWEEN P_DATAINI AND P_DATAFIN;
            ELSE
               V_DEBCRED := 0;
            END IF;

            V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                            THEN (NVL(V_DESPESA+V_AJUSTE+V_DEBCRED,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                            ELSE NVL(V_DESPESA+V_AJUSTE+V_DEBCRED,0)
                       END;

            OUT_RECORD.NRORDEM         := 16;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := 4;
            /*OUT_RECORD.DSDESCRICAOTOP  := 'Despesas (=)';*/
            IF (C_RESUMO_PAR.IDCONSLANCAJUSPAG = 'N') AND (C_RESUMO_PAR.IDCONSLANCAJUSREC = 'N') AND
               (C_RESUMO_PAR.IDCONSCREDBCO = 'N') AND (C_RESUMO_PAR.IDCONSDEBBCO = 'N') THEN

               OUT_RECORD.DSDESCRICAOTOP  := 'DESPA';
            ELSE
               OUT_RECORD.DSDESCRICAOTOP  := 'DESP';
            END IF;                                 
            OUT_RECORD.VRREAL          := NVL(V_DESPESA+V_AJUSTE+V_DEBCRED,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_DESPESA+V_AJUSTE+V_DEBCRED,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                              THEN V_BRUTO
                                                                                              ELSE V_LIQUIDO
                                                                                         END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                  THEN V_BRUTO
                                                                  ELSE V_LIQUIDO
                                                             END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := (VP_VROUTRDESPMAN + VP_VRAJUSTEMAN + VP_VRDEBCREMAN) * V_SINAL;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := (V_VRORCDE + V_VRORCDM + V_VRORCDD) * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                               THEN 0
                                               ELSE ((V_VRORCDE + V_VRORCDM + V_VRORCDD)*100)/
                                                   CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                        THEN V_VRORCFAT
                                                        ELSE V_VRORCLIQ
                                                   END
                                          END * V_SINAL;
            OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
            PIPE ROW (OUT_RECORD);

            IF NOT ((C_RESUMO_PAR.IDCONSLANCAJUSPAG = 'N') AND (C_RESUMO_PAR.IDCONSLANCAJUSREC = 'N') AND
               (C_RESUMO_PAR.IDCONSCREDBCO = 'N') AND (C_RESUMO_PAR.IDCONSDEBBCO = 'N')) THEN

               V_NRRESUMO_ROP := 0;

               V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                               THEN (NVL(V_DESPESA,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                               ELSE NVL(V_DESPESA,0)
                          END;

               OUT_RECORD.NRORDEM         := 17;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Despesas (-)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'DESPESA';
               OUT_RECORD.VRREAL          := NVL(V_DESPESA,0) * V_SINAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_DESPESA,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                              THEN V_BRUTO
                                                                              ELSE V_LIQUIDO
                                                                         END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                     THEN V_BRUTO
                                                                     ELSE V_LIQUIDO
                                                                END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJMANUAL    := VP_VROUTRDESPMAN * V_SINAL;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := (V_VRORCDE + V_VRORCDM + V_VRORCDD) * V_SINAL;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                                  THEN 0
                                                  ELSE ((V_VRORCDE + V_VRORCDM + V_VRORCDD)*100)/
                                                      CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                           THEN V_VRORCFAT
                                                           ELSE V_VRORCLIQ
                                                      END
                                             END * V_SINAL;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               VP_RESULTADO := VP_RESULTADO - (NVL(V_VRORCDE,0) + NVL(V_VRORCDM,0) + NVL(V_VRORCDD,0));

               IF (C_RESUMO_PAR.IDCONSLANCAJUSPAG = 'S') OR (C_RESUMO_PAR.IDCONSLANCAJUSREC = 'S') THEN
                  V_NRRESUMO_ROP := 0;

                  V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                                  THEN (NVL(V_AJUSTE,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                                  ELSE NVL(V_AJUSTE,0)
                             END;

                  OUT_RECORD.NRORDEM         := 18;
                  OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
                  OUT_RECORD.DSFILIAL        := V_DSFILIAL;
                  OUT_RECORD.DSMODELO        := V_DSMODELO;
                  OUT_RECORD.NRTOPICO        := NULL;
                  /*OUT_RECORD.DSDESCRICAOTOP  := '    Ajustes (+/-)';*/
                  OUT_RECORD.DSDESCRICAOTOP  := 'AJUST';
                  OUT_RECORD.VRREAL          := NVL(V_AJUSTE,0) * V_SINAL;
                  OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                     THEN NVL(V_AJUSTE,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                THEN V_BRUTO
                                                                                ELSE V_LIQUIDO
                                                                           END
                                                     ELSE 0
                                                END * V_SINAL;
                  OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
                  OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                     THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                        THEN V_BRUTO
                                                                        ELSE V_LIQUIDO
                                                                   END
                                                     ELSE 0
                                                END * V_SINAL;
                  OUT_RECORD.VRPROJMANUAL    := VP_VRAJUSTEMAN * V_SINAL;
                  OUT_RECORD.VRPERPROJMANUAL := NULL;
                  OUT_RECORD.VRORCADO        := 0;
                  OUT_RECORD.VRPERORCADO     := 0;
                  OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
                  OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
                  PIPE ROW (OUT_RECORD);
               END IF;

               IF (C_RESUMO_PAR.IDCONSCREDBCO = 'S') OR (C_RESUMO_PAR.IDCONSDEBBCO = 'S') THEN
                  V_NRRESUMO_ROP := 0;

                  V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                                  THEN (NVL(V_DEBCRED,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                                  ELSE NVL(V_DEBCRED,0)
                             END;

                  OUT_RECORD.NRORDEM         := 19;
                  OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
                  OUT_RECORD.DSFILIAL        := V_DSFILIAL;
                  OUT_RECORD.DSMODELO        := V_DSMODELO;
                  OUT_RECORD.NRTOPICO        := NULL;
                  /*OUT_RECORD.DSDESCRICAOTOP  := '    Debito/Credito (+/-)';*/
                  OUT_RECORD.DSDESCRICAOTOP  := 'DEBCRE';
                  OUT_RECORD.VRREAL          := NVL(V_DEBCRED,0) * V_SINAL;
                  OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                     THEN NVL(V_DEBCRED,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                                 THEN V_BRUTO
                                                                                 ELSE V_LIQUIDO
                                                                            END
                                                     ELSE 0
                                                END * V_SINAL;
                  OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
                  OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                     THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                        THEN V_BRUTO
                                                                        ELSE V_LIQUIDO
                                                                   END
                                                     ELSE 0
                                                END * V_SINAL;
                  OUT_RECORD.VRPROJMANUAL    := VP_VRDEBCREMAN * V_SINAL;
                  OUT_RECORD.VRPERPROJMANUAL := NULL;
                  OUT_RECORD.VRORCADO        := 0;
                  OUT_RECORD.VRPERORCADO     := 0;
                  OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
                  OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
                  PIPE ROW (OUT_RECORD);
               END IF;
            END IF;

            IF (C_RESUMO_PAR.IDDESPMOBROP = 'S') OR (C_RESUMO_PAR.IDAPURDESPENTRMO = 'S') OR
               (C_RESUMO_PAR.IDDESPMOBROPCTB = 'S') OR (C_RESUMO_PAR.IDAPUMOCTPGROP = 'S') THEN
               V_NRRESUMO_ROP := 7;

               SELECT CASE WHEN C_RESUMO_PAR.IDEXIBEDESPESAMOBRA = 'TOTAL_PERIODO'
                           THEN NVL(SUM(D.VRDESPESA),0)
                           ELSE (NVL(SUM(D.VRDESPESA),0) / V_NRDIA_UTIL) * V_NRDIA_REALIZADO
                      END
                 INTO V_MAOOBRA
                 FROM DESPESASROP D INNER JOIN FILIALROP F ON D.NRFILIALROP = F.NRFILIALROP
                WHERE D.IDTPLANCROP = 'MAOOBRA'
                  AND F.CDFILIAL = V_CDFILIAL
                  AND D.DTDESPESA = F.DTFILIALROP
                  AND F.DTFILIALROP BETWEEN P_DATAINI AND P_DATAFIN;

               V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                               THEN (NVL(V_MAOOBRA,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                               ELSE NVL(V_MAOOBRA,0)
                          END;

               OUT_RECORD.NRORDEM         := 20;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := 5;
               /*OUT_RECORD.DSDESCRICAOTOP  := 'Mao de Obra (-)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'MAOBRA';
               OUT_RECORD.VRREAL          := NVL(V_MAOOBRA,0) * CASE WHEN V_TEMP = 'M' THEN V_SINAL ELSE 1 END;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_MAOOBRA,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                              THEN V_BRUTO
                                                                              ELSE V_LIQUIDO
                                                                         END
                                                  ELSE 0
                                             END * CASE WHEN V_TEMP = 'M' THEN V_SINAL ELSE 1 END;
               OUT_RECORD.VRPROJECAO      := V_AUX_P * CASE WHEN V_TEMP = 'M' THEN V_SINAL ELSE 1 END;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                     THEN V_BRUTO
                                                                     ELSE V_LIQUIDO
                                                                END
                                                  ELSE 0
                                             END * CASE WHEN V_TEMP = 'M' THEN V_SINAL ELSE 1 END;
               OUT_RECORD.VRPROJMANUAL    := VP_VRMAOOBRAMAN * CASE WHEN V_TEMP = 'M' THEN V_SINAL ELSE 1 END;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_VRORCMO * CASE WHEN V_TEMP = 'M' THEN V_SINAL ELSE 1 END;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                                  THEN 0
                                                  ELSE (V_VRORCMO*100)/
                                                      CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                           THEN V_VRORCFAT
                                                           ELSE V_VRORCLIQ
                                                      END
                                             END * CASE WHEN V_TEMP = 'M' THEN V_SINAL ELSE 1 END;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               VP_RESULTADO := VP_RESULTADO - NVL(VP_VRMAOOBRAMAN,0);
            END IF;

            V_NRRESUMO_ROP := 11;

            V_AUX := (NVL(V_LIQUIDO,0) - NVL(V_VRTAXAADMIN,0) - NVL(V_VRCONSUMOREAL,0)) -
                     (NVL(V_DESPESA,0) + NVL(V_AJUSTE,0) + NVL(V_DEBCRED,0)) + CASE WHEN V_TEMP = 'M' THEN NVL(V_MAOOBRA,0) * (-1) ELSE NVL(V_MAOOBRA,0) END;

            V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                            THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                            ELSE NVL(V_AUX,0)
                       END;

            OUT_RECORD.NRORDEM         := 21;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := 'Resultado (=)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'RESULT';
            OUT_RECORD.VRREAL          := V_AUX;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_AUX,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                       THEN V_BRUTO
                                                                       ELSE V_LIQUIDO
                                                                  END
                                               ELSE 0
                                          END;
            OUT_RECORD.VRPROJECAO      := V_AUX_P;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                  THEN V_BRUTO
                                                                  ELSE V_LIQUIDO
                                                             END
                                               ELSE 0
                                          END;
            OUT_RECORD.VRPROJMANUAL    := VP_RESULTADO;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := 0;
            OUT_RECORD.VRPERORCADO     := 0;
            OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
            PIPE ROW (OUT_RECORD); 

            IF C_RESUMO_PAR.IDAPURDEPRECROP = 'S' THEN
               V_NRRESUMO_ROP := 8;

               SELECT NVL(SUM(D.VRDESPESA),0)
                 INTO V_DEPREC
                 FROM DESPESASROP D INNER JOIN FILIALROP F ON D.NRFILIALROP = F.NRFILIALROP
                WHERE D.IDTPLANCROP = 'DEPRECIACAO'
                  AND F.CDFILIAL = V_CDFILIAL
                  AND D.DTDESPESA = F.DTFILIALROP
                  AND F.DTFILIALROP BETWEEN P_DATAINI AND P_DATAFIN;

               V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                               THEN (NVL(V_DEPREC,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                               ELSE NVL(V_DEPREC,0)
                          END;

               OUT_RECORD.NRORDEM         := 22;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := 'Depreciacao de Bens (-)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'DEPRE';
               OUT_RECORD.VRREAL          := NVL(V_DEPREC,0) * V_SINAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_DEPREC,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                             THEN V_BRUTO
                                                                             ELSE V_LIQUIDO
                                                                        END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                     THEN V_BRUTO
                                                                     ELSE V_LIQUIDO
                                                                END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJMANUAL    := VP_VRDEPRBENSMAN * V_SINAL;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_VRORCDEPREC * V_SINAL;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                                  THEN 0
                                                  ELSE (V_VRORCDEPREC*100)/
                                                      CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                           THEN V_VRORCFAT
                                                           ELSE V_VRORCLIQ
                                                      END
                                             END * V_SINAL;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               VP_RESULTADO := VP_RESULTADO - NVL(VP_VRDEPRBENSMAN,0);
            END IF;

            IF (C_RESUMO_PAR.IDAPUINVFILROP = 'S') OR (C_RESUMO_PAR.IDAPURAINVCTPAGROP = 'S') THEN
               V_NRRESUMO_ROP := 12;

               SELECT NVL(SUM(I.VRINVEST),0)
                 INTO V_INVESTI
                 FROM INVESTIMENTOROP I INNER JOIN FILIALROP F ON I.NRFILIALROP = F.NRFILIALROP
                WHERE I.IDTPLANCROP = 'INVESTIMENTO'
                  AND F.CDFILIAL = V_CDFILIAL
                  AND F.DTFILIALROP = I.DTINVESTIMENTO
                  AND F.DTFILIALROP BETWEEN P_DATAINI AND P_DATAFIN;

               V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                               THEN (NVL(V_INVESTI,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                               ELSE NVL(V_INVESTI,0)
                          END;

               OUT_RECORD.NRORDEM         := 23;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := 6;
               /*OUT_RECORD.DSDESCRICAOTOP  := 'Investimentos (-)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'INVEST';
               OUT_RECORD.VRREAL          := NVL(V_INVESTI,0) * V_SINAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_INVESTI,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                              THEN V_BRUTO
                                                                              ELSE V_LIQUIDO
                                                                         END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJECAO      := V_AUX_P * V_SINAL;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                     THEN V_BRUTO
                                                                     ELSE V_LIQUIDO
                                                                END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJMANUAL    := VP_VRINVESTMAN * V_SINAL;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_VRORCINV * V_SINAL;
               OUT_RECORD.VRPERORCADO     := CASE WHEN V_VRORCLIQ = 0
                                                  THEN 0
                                                  ELSE (V_VRORCINV*100)/
                                                      CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                           THEN V_VRORCFAT
                                                           ELSE V_VRORCLIQ
                                                      END
                                             END * V_SINAL;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);

               VP_RESULTADO := VP_RESULTADO - NVL(VP_VRINVESTMAN,0);

               V_NRRESUMO_ROP := 13;
                            
               V_AUX := CASE WHEN C_RESUMO_PAR.IDCALCIMPFAT = 'FATBRUTO'
                             THEN (NVL(V_LIQUIDO,0) - NVL(V_VRTAXAADMIN,0) - NVL(V_VRCONSUMOREAL,0)) -
                                  (NVL(V_DESPESA,0) + NVL(V_AJUSTE,0) + NVL(V_DEBCRED,0)) + CASE WHEN V_TEMP = 'M' THEN NVL(V_MAOOBRA,0)*(-1) ELSE NVL(V_MAOOBRA,0) END - NVL(V_INVESTI,0) - NVL(V_DEPREC,0)
                             ELSE (NVL(V_LIQUIDO,0) - NVL(V_VRTAXAADMIN,0) - NVL(V_VRCONSUMOREAL,0)) - NVL(V_DEDUIMPO,0) - NVL(V_DEDUIMPOFOS,0) -
                                  (NVL(V_DESPESA,0) + NVL(V_AJUSTE,0) + NVL(V_DEBCRED,0)) + CASE WHEN V_TEMP = 'M' THEN NVL(V_MAOOBRA,0)*(-1) ELSE NVL(V_MAOOBRA,0) END - NVL(V_INVESTI,0) - NVL(V_DEPREC,0)
                        END;

               V_AUX_P := CASE WHEN V_CALC_PROJ = 'S'
                               THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                               ELSE NVL(V_AUX,0)
                          END;
            END IF;
            
            IF (C_RESUMO_PAR.IDAPUINVFILROP = 'S') OR (C_RESUMO_PAR.IDAPURAINVCTPAGROP = 'S') OR (C_RESUMO_PAR.IDAPURDEPRECROP = 'S') THEN
               OUT_RECORD.NRORDEM         := 24;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := 'Resultado Total (=)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'RESULTOT';
               OUT_RECORD.VRREAL          := V_AUX;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_AUX,0)/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                          THEN V_BRUTO
                                                                          ELSE V_LIQUIDO
                                                                     END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := V_AUX_P;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN V_AUX_P/ CASE WHEN C_RESUMO_PAR.IDCALCPERCENTFAT = 'FATURAMENTO_BRUTO'
                                                                     THEN V_BRUTO
                                                                     ELSE V_LIQUIDO
                                                                END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := VP_RESULTADO;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := 0;
               OUT_RECORD.VRPERORCADO     := 0;
               OUT_RECORD.NRORG := C_RESUMO_PAR.NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := VP_NRPROJECAOMANUALROP;
               PIPE ROW (OUT_RECORD);
            END IF;
         END IF;
      END LOOP;
   ELSE
      V_DSFILIAL := 'Consolidado';

      SELECT SUM(D.VRFATBRUTO), SUM(D.VRFATBRUTOPROJ), SUM(D.VRFATBRUTOORCA),
             SUM(D.VRFATUREFET), SUM(D.VRFATUREFETPROJ), SUM(D.VRFATUREFETORCA),
             SUM(D.VRFATURNF), SUM(D.VRFATURNFPROJ), SUM(D.VRFATURNFORCA),
             SUM(D.VRFATURFOSBRUT) /*-SUM(D.VRFATURFOSDESC) */, SUM(D.VRFATURFOSBRUTPROJ) /*-SUM(D.VRFATURFOSDESCPROJ)*/, SUM(D.VRFATURFOSBRUTORCA)/*-SUM(D.VRFATURFOSDESCORCA) */,
             SUM(D.VRFATURDIV), SUM(D.VRFATURDIVPROJ), SUM(D.VRFATURDIVORCA),
             SUM(D.VRFATRECEXTRA), SUM(D.VRFATRECEXTRAPROJ), SUM(D.VRFATRECEXTRAORCA),
             SUM(D.VRDEDURECEITA), SUM(D.VRDEDURECEITAPROJ), SUM(D.VRDEDURECEITAORCA),
             SUM(D.VRIMPOSTO), SUM(D.VRIMPOSTOPROJ), SUM(D.VRIMPOSTOORCA),
             SUM(D.VRIMPOFOS), SUM(D.VRIMPOFOSPROJ), SUM(D.VRIMPOFOSORCA),      
             SUM(D.VRCREDIMPOSTO), SUM(D.VRCREDIMPOSTOPROJ), SUM(D.VRCREDIMPOSTOORCA),
             SUM(D.VRDEVOLUESTOR), SUM(D.VRDEVOLUESTORPROJ), SUM(D.VRDEVOLUESTORORCA),
             SUM(D.VRDEDUSOBVENDA), SUM(D.VRDEDUSOBVENDAPROJ), SUM(D.VRDEDUSOBVENDAORCA),
             SUM(D.VRENCARGO), SUM(D.VRENCARGOPROJ), SUM(D.VRENCARGOORCA),
             SUM(D.VRFATLIQUIDO), SUM(D.VRFATLIQUIDOPROJ), SUM(D.VRFATLIQUIDOORCA),
             SUM(D.VRTAXAADM), SUM(D.VRTAXAADMPROJ), SUM(D.VRTAXAADMORCA),
             SUM(D.VRMARGEMCONTR),SUM(D.VRMARGEMCONTRPROJ), SUM(D.VRMARGEMCONTRORCA),
             SUM(D.VRDESPESATOT), SUM(D.VRDESPESATOTPROJ), SUM(D.VRDESPESATOTORCA),
             SUM(D.VRDESPESA), SUM(D.VRDESPESAPROJ), SUM(D.VRDESPESAORCA),
             SUM(D.VRAJUSTE), SUM(D.VRAJUSTEPROJ), SUM(D.VRAJUSTEORCA),
             SUM(D.VRDEBITOBCO)+SUM(D.VRCREDITOBCO), SUM(D.VRDEBITOBCOPROJ)+SUM(D.VRCREDITOBCOPROJ), SUM(D.VRDEBITOBCOORCA)+SUM(D.VRCREDITOBCOORCA),
             SUM(D.VRMAOOBRA), SUM(D.VRMAOOBRAPROJ), SUM(D.VRMAOOBRAORCA),
             SUM(D.VRRESULTPARC), SUM(D.VRRESULTPARCPROJ), SUM(D.VRRESULTPARCORCA),
             SUM(D.VRDEPRECIACAO), SUM(D.VRDEPRECIACAOPROJ), SUM(D.VRDEPRECIACAOORCA),
             SUM(D.VRINVESTIMENTO), SUM(D.VRINVESTIMENTOPROJ), SUM(D.VRINVESTIMENTOORCA),
             SUM(D.VRRESULTFINAL), SUM(D.VRRESULTFINALPROJ), SUM(D.VRRESULTFINALORCA),
             SUM(D.VRCONSUMO), SUM(D.VRCONSUMOPROJ), SUM(D.VRCONSUMOORCA),
             MAX(D.NRORG)
        INTO V_BRUTO, V_BRUTO_P, V_BRUTO_O,
             V_FATEFETIVO, V_FATEFETIVO_P, V_FATEFETIVO_O,
             V_FATNOTAFISC, V_FATNOTAFISC_P, V_FATNOTAFISC_O,
             V_FATFORSALE, V_FATFORSALE_P, V_FATFORSALE_O,
             V_FATDIVERSO, V_FATDIVERSO_P, V_FATDIVERSO_O,
             V_FATRECEXTRA, V_FATRECEXTRA_P, V_FATRECEXTRA_O,
             V_VRDEDURECEITA, V_VRDEDURECEITA_P, V_VRDEDURECEITA_O,
             V_DEDUIMPO, V_DEDUIMPO_P, V_DEDUIMPO_O,               
             V_DEDUIMPOFOS, V_DEDUIMPOFOS_P, V_DEDUIMPOFOS_O,
             V_CREDIMPO, V_CREDIMPO_P, V_CREDIMPO_O,
             V_FATDEVCANC, V_FATDEVCANC_P, V_FATDEVCANC_O,
             V_DESCSOBVENDA, V_DESCSOBVENDA_P, V_DESCSOBVENDA_O,
             V_ENCARGOS, V_ENCARGOS_P, V_ENCARGOS_O,
             V_LIQUIDO, V_LIQUIDO_P, V_LIQUIDO_O,
             V_VRTAXAADMIN, V_VRTAXAADMIN_P, V_VRTAXAADMIN_O,
             V_VRMARGEMCONTR, V_VRMARGEMCONTR_P, V_VRMARGEMCONTR_O,
             V_VRDESPESATOT, V_VRDESPESATOT_P, V_VRDESPESATOT_O,
             V_DESPESA, V_DESPESA_P, V_DESPESA_O,
             V_AJUSTE, V_AJUSTE_P, V_AJUSTE_O,
             V_DEBCREDBCO, V_DEBCREDBCO_P, V_DEBCREDBCO_O,
             V_MAOOBRA, V_MAOOBRA_P, V_MAOOBRA_O,
             V_VRRESULTPARC, V_VRRESULTPARC_P, V_VRRESULTPARC_O,
             V_DEPREC, V_DEPREC_P, V_DEPREC_O,
             V_INVESTI, V_INVESTI_P, V_INVESTI_O,
             V_VRRESULTFINAL, V_VRRESULTFINAL_P, V_VRRESULTFINAL_O,
             V_VRCONSUMOREAL, V_VRCONSUMOREAL_P, V_VRCONSUMOREAL_O,
             V_NRORG
        FROM DM_RESULT_OPERACIONAL D, LISTA L, PARFILIALROP P
       WHERE L.NRLISTA = P_CDFILIAL
         AND D.DTRESUMO BETWEEN P_DATAINI AND P_DATAFIN
         AND D.CDFILIAL = L.CDLISTA
         AND P.CDFILIAL = TIPOPARAM_DRO(D.CDFILIAL);

      FOR C_PARFILIAL IN (SELECT 'NE' IDFATUROP, MAX(IDGERROPFOS) IDGERROPFOS, MAX(IDCONSFATUDIV) IDCONSFATUDIV,
                                  MAX(IDVOLUNEGOCIO) IDVOLUNEGOCIO, MAX(IDCONSLANCAJUSPAG) IDCONSLANCAJUSPAG,
                                  MAX(IDCONSLANCAJUSREC) IDCONSLANCAJUSREC, MAX(IDCONSCREDBCO) IDCONSCREDBCO,
                                  MAX(IDCONSDEBBCO) IDCONSDEBBCO, MAX(IDDESPMOBROP) IDDESPMOBROP,
                                  MAX(IDAPURDESPENTRMO) IDAPURDESPENTRMO, MAX(IDDESPMOBROPCTB) IDDESPMOBROPCTB,
                                  MAX(IDAPUMOCTPGROP) IDAPUMOCTPGROP, MAX(IDAPURDEPRECROP) IDAPURDEPRECROP,
                                  MAX(IDAPUINVFILROP) IDAPUINVFILROP, MAX(IDAPURAINVCTPAGROP) IDAPURAINVCTPAGROP
                             FROM PARFILIALROP P, LISTA L
                            WHERE P.CDFILIAL = TIPOPARAM_DRO(L.CDLISTA)
                              AND L.NRLISTA = P_CDFILIAL) LOOP

         OUT_RECORD.NRRESUMO_ROP    := 0;
         OUT_RECORD.DSFILIAL        := V_DSFILIAL;
         OUT_RECORD.DSMODELO        := V_DSMODELO;
         OUT_RECORD.NRTOPICO        := 1;
         OUT_RECORD.DSDESCRICAOTOP  := 'FAT';
         /* OUT_RECORD.DSDESCRICAOTOP  := 'Faturamento (=)'; */
         OUT_RECORD.VRREAL          := V_BRUTO;
         OUT_RECORD.VRPERREAL       := CASE WHEN P_IDPERCEN = 'B'
                                            THEN 1
                                            ELSE NULL
                                       END;
         OUT_RECORD.VRPROJECAO      := V_BRUTO_P;
         OUT_RECORD.VRPERPROJECAO   := CASE WHEN V_BRUTO_P > 0
                                            THEN CASE WHEN P_IDPERCEN = 'B'
                                                      THEN 1
                                                      ELSE NULL
                                                 END
                                            ELSE 0
                                       END;
         OUT_RECORD.VRPROJMANUAL    := 0;
         OUT_RECORD.VRPERPROJMANUAL := NULL;
         OUT_RECORD.VRORCADO        := V_BRUTO_O;
         OUT_RECORD.VRPERORCADO     := CASE WHEN V_BRUTO_O > 0
                                            THEN CASE WHEN P_IDPERCEN = 'B'
                                                      THEN 1
                                                      ELSE NULL
                                                 END
                                            ELSE 0
                                       END;
         OUT_RECORD.NRORDEM         := 1;
         OUT_RECORD.NRORG           := V_NRORG;
         OUT_RECORD.NRPROJECAOMANUALROP := 0;
         PIPE ROW (OUT_RECORD);

         IF P_IDRELAT <> 'GU' THEN
            IF (C_PARFILIAL.IDFATUROP = 'NE') OR (C_PARFILIAL.IDFATUROP = 'EF') THEN
               V_NRRESUMO_ROP := 1;

               OUT_RECORD.NRORDEM         := 2;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Faturamento Efetivos (+)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'FATEFET';
               OUT_RECORD.VRREAL          := NVL(V_FATEFETIVO,0);
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_FATEFETIVO,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                 THEN V_BRUTO
                                                                                 ELSE V_LIQUIDO
                                                                            END
                                                  ELSE 0
                                              END;
               OUT_RECORD.VRPROJECAO      := NVL(V_FATEFETIVO_P,0);
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                                  THEN NVL(V_FATEFETIVO_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                   THEN V_BRUTO_P
                                                                                   ELSE V_LIQUIDO_P
                                                                              END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := 0;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_FATEFETIVO_O;
               OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                                  THEN V_FATEFETIVO_O/ CASE WHEN P_IDPERCEN = 'B'
                                                                            THEN V_BRUTO_O
                                                                            ELSE V_LIQUIDO_O
                                                                       END
                                                  ELSE 0
                                             END;
               OUT_RECORD.NRORG := V_NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := 0;
               PIPE ROW (OUT_RECORD);
            END IF;

            IF (C_PARFILIAL.IDFATUROP = 'NE') OR (C_PARFILIAL.IDFATUROP = 'NF') THEN
               V_NRRESUMO_ROP := 0;

               OUT_RECORD.NRORDEM         := 3;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               OUT_RECORD.DSDESCRICAOTOP  := 'FATNF';
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Faturamento NF'||CASE WHEN V_IDTPLANCROP = 'AB' THEN '*' ELSE '' END||' (+)';*/
               OUT_RECORD.VRREAL          := NVL(V_FATNOTAFISC,0);
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_FATNOTAFISC,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                  THEN V_BRUTO
                                                                                  ELSE V_LIQUIDO
                                                                             END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := NVL(V_FATNOTAFISC_P,0);
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                                  THEN NVL(V_FATNOTAFISC_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                    THEN V_BRUTO_P
                                                                                    ELSE V_LIQUIDO_P
                                                                               END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := 0;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_FATNOTAFISC_O;
               OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                                  THEN V_FATNOTAFISC_O/ CASE WHEN P_IDPERCEN = 'B'
                                                                             THEN V_BRUTO_O
                                                                             ELSE V_LIQUIDO_O
                                                                        END
                                                  ELSE 0
                                             END;
               OUT_RECORD.NRORG := V_NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := 0;
               PIPE ROW (OUT_RECORD);
            END IF;

            IF C_PARFILIAL.IDGERROPFOS = 'S' THEN
               V_NRRESUMO_ROP := 3;

               OUT_RECORD.NRORDEM         := 4;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Faturamento ForSale (+)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'FATFOR';
               OUT_RECORD.VRREAL          := NVL(V_FATFORSALE,0);
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_FATFORSALE,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                 THEN V_BRUTO
                                                                                 ELSE V_LIQUIDO
                                                                            END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := NVL(V_FATFORSALE_P,0);
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                                  THEN NVL(V_FATFORSALE_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                   THEN V_BRUTO_P
                                                                                   ELSE V_LIQUIDO_P
                                                                              END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := 0;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_FATFORSALE_O;
               OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                                  THEN V_FATFORSALE_O/ CASE WHEN P_IDPERCEN = 'B'
                                                                            THEN V_BRUTO_O
                                                                            ELSE V_LIQUIDO_O
                                                                       END
                                                  ELSE 0
                                             END;
               OUT_RECORD.NRORG := V_NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := 0;
               PIPE ROW (OUT_RECORD);
            END IF;

            IF C_PARFILIAL.IDCONSFATUDIV = 'S' THEN
               V_NRRESUMO_ROP := 0;

               OUT_RECORD.NRORDEM         := 5;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Faturamento Diversos (+)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'FATDIV';
               OUT_RECORD.VRREAL          := NVL(V_FATDIVERSO,0);
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_FATDIVERSO,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                 THEN V_BRUTO
                                                                                 ELSE V_LIQUIDO
                                                                            END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := NVL(V_FATDIVERSO_P,0);
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                                  THEN NVL(V_FATDIVERSO_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                   THEN V_BRUTO_P
                                                                                   ELSE V_LIQUIDO_P
                                                                              END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := 0;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_FATDIVERSO_O;
               OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                                  THEN V_FATDIVERSO_O/ CASE WHEN P_IDPERCEN = 'B'
                                                                            THEN V_BRUTO_O
                                                                            ELSE V_LIQUIDO_O
                                                                       END
                                                  ELSE 0
                                             END;
               OUT_RECORD.NRORG := V_NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := 0;
               PIPE ROW (OUT_RECORD);
            END IF;
      
            IF C_PARFILIAL.IDVOLUNEGOCIO = 'S' THEN
               V_NRRESUMO_ROP := 0;

               OUT_RECORD.NRORDEM         := 5;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Faturamento Diversos (+)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'FATRECEXTRA';
               OUT_RECORD.VRREAL          := NVL(V_FATRECEXTRA,0);
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_FATRECEXTRA,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                  THEN V_BRUTO
                                                                                  ELSE V_LIQUIDO
                                                                             END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJECAO      := NVL(V_FATRECEXTRA_P,0);
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                                  THEN NVL(V_FATRECEXTRA_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                    THEN V_BRUTO_P
                                                                                    ELSE V_LIQUIDO_P
                                                                               END
                                                  ELSE 0
                                             END;
               OUT_RECORD.VRPROJMANUAL    := 0;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_FATRECEXTRA_O;
               OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                                  THEN V_FATRECEXTRA_O/ CASE WHEN P_IDPERCEN = 'B'
                                                                             THEN V_BRUTO_O
                                                                             ELSE V_LIQUIDO_O
                                                                        END
                                                  ELSE 0
                                             END;
               OUT_RECORD.NRORG := V_NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := 0;
               PIPE ROW (OUT_RECORD);
            END IF;
         END IF;

         V_NRRESUMO_ROP := 0;

         OUT_RECORD.NRORDEM         := 6;
         OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
         OUT_RECORD.DSFILIAL        := V_DSFILIAL;
         OUT_RECORD.DSMODELO        := V_DSMODELO;
         OUT_RECORD.NRTOPICO        := 2;
         /*OUT_RECORD.DSDESCRICAOTOP  := 'Deducoes da Receita (=)';*/
         OUT_RECORD.DSDESCRICAOTOP  := 'DEDUREC';
         OUT_RECORD.VRREAL          := NVL(V_VRDEDURECEITA,0) * V_SINAL;
         OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                            THEN V_VRDEDURECEITA/ CASE WHEN P_IDPERCEN = 'B'
                                                                       THEN V_BRUTO
                                                                       ELSE V_LIQUIDO
                                                                  END
                                            ELSE 0
                                       END * V_SINAL;
         OUT_RECORD.VRPROJECAO      := NVL(V_VRDEDURECEITA_P,0) * V_SINAL;
         OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                            THEN NVL(V_VRDEDURECEITA_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                THEN V_BRUTO_P
                                                                                ELSE V_LIQUIDO_P
                                                                           END
                                            ELSE 0
                                       END * V_SINAL;
         OUT_RECORD.VRPROJMANUAL    := 0;
         OUT_RECORD.VRPERPROJMANUAL := NULL;
         OUT_RECORD.VRORCADO        := V_VRDEDURECEITA_O * V_SINAL;
         OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                            THEN V_VRDEDURECEITA_O/ CASE WHEN P_IDPERCEN = 'B'
                                                                         THEN V_BRUTO_O
                                                                         ELSE V_LIQUIDO_O
                                                                    END
                                            ELSE 0
                                       END * V_SINAL;
         OUT_RECORD.NRORG := V_NRORG;
         OUT_RECORD.NRPROJECAOMANUALROP := 0;
         PIPE ROW (OUT_RECORD);

         IF P_IDRELAT <> 'GU' THEN
            V_NRRESUMO_ROP := 2;

            OUT_RECORD.NRORDEM         := 7;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := '    Imposto (+/-)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'DEDUIM';
            OUT_RECORD.VRREAL          := NVL(V_DEDUIMPO,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_DEDUIMPO,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                            THEN V_BRUTO
                                                                            ELSE V_LIQUIDO
                                                                       END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := NVL(V_DEDUIMPO_P,0) * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                               THEN NVL(V_DEDUIMPO_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                              THEN V_BRUTO_P
                                                                              ELSE V_LIQUIDO_P
                                                                         END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := 0;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_DEDUIMPO_O * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                               THEN NVL(V_DEDUIMPO_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                              THEN V_BRUTO_O
                                                                              ELSE V_LIQUIDO_O
                                                                         END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.NRORG := V_NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := 0;
            PIPE ROW (OUT_RECORD);

            OUT_RECORD.NRORDEM         := 7;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            OUT_RECORD.DSDESCRICAOTOP  := 'DEDUIMFOS';
            OUT_RECORD.VRREAL          := NVL(V_DEDUIMPOFOS,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_DEDUIMPOFOS,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                               THEN V_BRUTO
                                                                               ELSE V_LIQUIDO
                                                                          END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := NVL(V_DEDUIMPOFOS_P,0) * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P <> 0) AND (V_BRUTO_P <> 0)
                                               THEN NVL(V_DEDUIMPOFOS_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                 THEN V_BRUTO_P
                                                                                 ELSE V_LIQUIDO_P
                                                                            END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := NULL;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_DEDUIMPOFOS_O * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN V_LIQUIDO_O = 0 OR V_BRUTO_O = 0
                                               THEN 0
                                               ELSE (V_DEDUIMPOFOS_O*100)/
                                                   CASE WHEN P_IDPERCEN = 'B'
                                                        THEN V_BRUTO_O
                                                        ELSE V_LIQUIDO_O
                                                   END
                                          END * V_SINAL;
            OUT_RECORD.NRORG := V_NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := 0;
            PIPE ROW (OUT_RECORD);

            V_NRRESUMO_ROP := 9;

            OUT_RECORD.NRORDEM         := 8;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := '    Credito de Imposto (+)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'DEDUCR';
            OUT_RECORD.VRREAL          := NVL(V_CREDIMPO,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_CREDIMPO,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                            THEN V_BRUTO
                                                                            ELSE V_LIQUIDO
                                                                       END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := NVL(V_CREDIMPO_P,0) * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                               THEN NVL(V_CREDIMPO_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                              THEN V_BRUTO_P
                                                                              ELSE V_LIQUIDO_P
                                                                         END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := 0;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_CREDIMPO_O * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                               THEN V_CREDIMPO_O/ CASE WHEN P_IDPERCEN = 'B'
                                                                       THEN V_BRUTO_O
                                                                       ELSE V_LIQUIDO_O
                                                                  END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.NRORG := V_NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := 0;
            PIPE ROW (OUT_RECORD);

            IF (C_PARFILIAL.IDFATUROP = 'NE') OR (C_PARFILIAL.IDFATUROP = 'NF') THEN
               V_NRRESUMO_ROP := 0;

               OUT_RECORD.NRORDEM         := 9;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Devolucoes/Estornos (-)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'DEDUDEV';
               OUT_RECORD.VRREAL          := NVL(V_FATDEVCANC,0) * V_SINAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_FATDEVCANC,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                 THEN V_BRUTO
                                                                                 ELSE V_LIQUIDO
                                                                            END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJECAO      := NVL(V_FATDEVCANC_P,0) * V_SINAL;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                                  THEN NVL(V_FATDEVCANC_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                   THEN V_BRUTO_P
                                                                                   ELSE V_LIQUIDO_P
                                                                              END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJMANUAL    := 0;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_FATDEVCANC_O * V_SINAL;
               OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                                  THEN NVL(V_FATDEVCANC_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                   THEN V_BRUTO_O
                                                                                   ELSE V_LIQUIDO_O
                                                                              END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.NRORG := V_NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := 0;

               PIPE ROW (OUT_RECORD);
            END IF;

            V_NRRESUMO_ROP := 0;

            OUT_RECORD.NRORDEM         := 10;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := '    Descontos Sobre Vendas (-)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'DEDUDES';
            OUT_RECORD.VRREAL          := NVL(V_DESCSOBVENDA,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_DESCSOBVENDA,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                THEN V_BRUTO
                                                                                ELSE V_LIQUIDO
                                                                           END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := NVL(V_DESCSOBVENDA_P,0) * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                               THEN NVL(V_DESCSOBVENDA_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                  THEN V_BRUTO_P
                                                                                  ELSE V_LIQUIDO_P
                                                                             END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := 0;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_DESCSOBVENDA_O * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                               THEN NVL(V_DESCSOBVENDA_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                  THEN V_BRUTO_O
                                                                                  ELSE V_LIQUIDO_O
                                                                             END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.NRORG := V_NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := 0;
            PIPE ROW (OUT_RECORD);

            V_NRRESUMO_ROP := 0;

            OUT_RECORD.NRORDEM         := 11;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := '    Encargos (+/-)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'DEDUEN';
            OUT_RECORD.VRREAL          := NVL(V_ENCARGOS,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_ENCARGOS,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                            THEN V_BRUTO
                                                                            ELSE V_LIQUIDO
                                                                       END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := NVL(V_ENCARGOS_P,0) * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                               THEN NVL(V_ENCARGOS_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                              THEN V_BRUTO_P
                                                                              ELSE V_LIQUIDO_P
                                                                         END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := 0;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_ENCARGOS_O * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                               THEN NVL(V_ENCARGOS_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                              THEN V_BRUTO_O
                                                                              ELSE V_LIQUIDO_O
                                                                         END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.NRORG := V_NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := 0;
            PIPE ROW (OUT_RECORD);
         END IF;

         V_NRRESUMO_ROP := 4;

         OUT_RECORD.NRORDEM         := 12;
         OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
         OUT_RECORD.DSFILIAL        := V_DSFILIAL;
         OUT_RECORD.DSMODELO        := V_DSMODELO;
         OUT_RECORD.NRTOPICO        := NULL;
         /*OUT_RECORD.DSDESCRICAOTOP  := 'Faturamento Liquido (=)';*/
         OUT_RECORD.DSDESCRICAOTOP  := 'FATLIQ';
         OUT_RECORD.VRREAL          := NVL(V_LIQUIDO,0);
         OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                            THEN NVL(V_LIQUIDO,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                        THEN V_BRUTO
                                                                        ELSE V_LIQUIDO
                                                                   END
                                            ELSE 0
                                       END;
         OUT_RECORD.VRPROJECAO      := NVL(V_LIQUIDO_P,0);
         OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                            THEN NVL(V_LIQUIDO_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                          THEN V_BRUTO_P
                                                                          ELSE V_LIQUIDO_P
                                                                     END
                                            ELSE 0
                                       END;
         OUT_RECORD.VRPROJMANUAL    := 0;
         OUT_RECORD.VRPERPROJMANUAL := NULL;
         OUT_RECORD.VRORCADO        := V_LIQUIDO_O;
         OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                            THEN NVL(V_LIQUIDO_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                          THEN V_BRUTO_O
                                                                          ELSE V_LIQUIDO_O
                                                                     END
                                            ELSE 0
                                       END;
         OUT_RECORD.NRORG := V_NRORG;
         OUT_RECORD.NRPROJECAOMANUALROP := 0;
         PIPE ROW (OUT_RECORD);

         V_NRRESUMO_ROP := 10;

         OUT_RECORD.NRORDEM         := 13;
         OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
         OUT_RECORD.DSFILIAL        := V_DSFILIAL;
         OUT_RECORD.DSMODELO        := V_DSMODELO;
         OUT_RECORD.NRTOPICO        := NULL;
         /*OUT_RECORD.DSDESCRICAOTOP  := 'Taxa Administrativa (-)';*/
         OUT_RECORD.DSDESCRICAOTOP  := 'TAXADM';
         OUT_RECORD.VRREAL          := NVL(V_VRTAXAADMIN,0) * V_SINAL;
         OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                            THEN NVL(V_VRTAXAADMIN,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                            THEN V_BRUTO
                                                                            ELSE V_LIQUIDO
                                                                       END
                                            ELSE 0
                                       END * V_SINAL;
         OUT_RECORD.VRPROJECAO      := NVL(V_VRTAXAADMIN_P,0) * V_SINAL;
         OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                            THEN NVL(V_VRTAXAADMIN_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                              THEN V_BRUTO_P
                                                                              ELSE V_LIQUIDO_P
                                                                         END
                                            ELSE 0
                                       END * V_SINAL;
         OUT_RECORD.VRPROJMANUAL    := 0;
         OUT_RECORD.VRPERPROJMANUAL := NULL;
         OUT_RECORD.VRORCADO        := V_VRTAXAADMIN_O * V_SINAL;
         OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                            THEN NVL(V_VRTAXAADMIN_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                              THEN V_BRUTO_O
                                                                              ELSE V_LIQUIDO_O
                                                                         END
                                            ELSE 0
                                       END * V_SINAL;
         OUT_RECORD.NRORG := V_NRORG;
         OUT_RECORD.NRPROJECAOMANUALROP := 0;
         PIPE ROW (OUT_RECORD);

         V_NRRESUMO_ROP := 5;

         OUT_RECORD.NRORDEM         := 14;
         OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
         OUT_RECORD.DSFILIAL        := V_DSFILIAL;
         OUT_RECORD.DSMODELO        := V_DSMODELO;
         OUT_RECORD.NRTOPICO        := 3;
         /*OUT_RECORD.DSDESCRICAOTOP  := 'Consumo (CMV) (-)';*/
         OUT_RECORD.DSDESCRICAOTOP  := 'CMV';
         OUT_RECORD.VRREAL          := NVL(V_VRCONSUMOREAL,0) * V_SINAL;
         OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                            THEN NVL(V_VRCONSUMOREAL,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                              THEN V_BRUTO
                                                                              ELSE V_LIQUIDO
                                                                         END
                                            ELSE 0
                                       END * V_SINAL;
         OUT_RECORD.VRPROJECAO      := NVL(V_VRCONSUMOREAL_P,0) * V_SINAL;
         OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                            THEN NVL(V_VRCONSUMOREAL_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                THEN V_BRUTO_P
                                                                                ELSE V_LIQUIDO_P
                                                                           END
                                            ELSE 0
                                       END * V_SINAL;
         OUT_RECORD.VRPROJMANUAL    := 0;
         OUT_RECORD.VRPERPROJMANUAL := NULL;
         OUT_RECORD.VRORCADO        := V_VRCONSUMOREAL_O * V_SINAL;
         OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                            THEN NVL(V_VRCONSUMOREAL_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                THEN V_BRUTO_O
                                                                                ELSE V_LIQUIDO_O
                                                                           END
                                            ELSE 0
                                       END * V_SINAL;
         OUT_RECORD.NRORG := V_NRORG;
         OUT_RECORD.NRPROJECAOMANUALROP := 0;
         PIPE ROW (OUT_RECORD);

         V_NRRESUMO_ROP := 0;

         OUT_RECORD.NRORDEM         := 15;
         OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
         OUT_RECORD.DSFILIAL        := V_DSFILIAL;
         OUT_RECORD.DSMODELO        := V_DSMODELO;
         OUT_RECORD.NRTOPICO        := NULL;
         /*OUT_RECORD.DSDESCRICAOTOP  := 'Margem de Contribuicao (=)';*/
         OUT_RECORD.DSDESCRICAOTOP  := 'MARCONT';
         OUT_RECORD.VRREAL          := NVL(V_VRMARGEMCONTR,0);
         OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                            THEN NVL(V_VRMARGEMCONTR,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                              THEN V_BRUTO
                                                                              ELSE V_LIQUIDO
                                                                         END
                                            ELSE 0
                                       END;
         OUT_RECORD.VRPROJECAO      := NVL(V_VRMARGEMCONTR_P,0);
         OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                            THEN NVL(V_VRMARGEMCONTR_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                THEN V_BRUTO_P
                                                                                ELSE V_LIQUIDO_P
                                                                           END
                                            ELSE 0
                                       END;
         OUT_RECORD.VRPROJMANUAL    := 0;
         OUT_RECORD.VRPERPROJMANUAL := NULL;
         OUT_RECORD.VRORCADO        := V_VRMARGEMCONTR_O;
         OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                            THEN NVL(V_VRMARGEMCONTR_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                THEN V_BRUTO_O
                                                                                ELSE V_LIQUIDO_O
                                                                           END
                                            ELSE 0
                                       END;
         OUT_RECORD.NRORG := V_NRORG;
         OUT_RECORD.NRPROJECAOMANUALROP := 0;
         PIPE ROW (OUT_RECORD);

         V_NRRESUMO_ROP := 6;

         OUT_RECORD.NRORDEM         := 16;
         OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
         OUT_RECORD.DSFILIAL        := V_DSFILIAL;
         OUT_RECORD.DSMODELO        := V_DSMODELO;
         OUT_RECORD.NRTOPICO        := 4;
         /*OUT_RECORD.DSDESCRICAOTOP  := 'Despesas (=)';*/
         IF (C_PARFILIAL.IDCONSLANCAJUSPAG = 'N') AND (C_PARFILIAL.IDCONSLANCAJUSREC = 'N') AND
               (C_PARFILIAL.IDCONSCREDBCO = 'N') AND (C_PARFILIAL.IDCONSDEBBCO = 'N') THEN
            OUT_RECORD.DSDESCRICAOTOP  := 'DESPA';
         ELSE
            OUT_RECORD.DSDESCRICAOTOP  := 'DESP';
         END IF;
         OUT_RECORD.VRREAL          := NVL(V_VRDESPESATOT,0) * V_SINAL;
         OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                            THEN NVL(V_VRDESPESATOT,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                             THEN V_BRUTO
                                                                             ELSE V_LIQUIDO
                                                                        END
                                            ELSE 0
                                       END * V_SINAL;
         OUT_RECORD.VRPROJECAO      := NVL(V_VRDESPESATOT_P,0) * V_SINAL;
         OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                            THEN NVL(V_VRDESPESATOT_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                               THEN V_BRUTO_P
                                                                               ELSE V_LIQUIDO_P
                                                                          END
                                            ELSE 0
                                       END * V_SINAL;
         OUT_RECORD.VRPROJMANUAL    := 0;
         OUT_RECORD.VRPERPROJMANUAL := NULL;
         OUT_RECORD.VRORCADO        := V_VRDESPESATOT_O * V_SINAL;
         OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                            THEN NVL(V_VRDESPESATOT_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                               THEN V_BRUTO_O
                                                                               ELSE V_LIQUIDO_O
                                                                          END
                                            ELSE 0
                                       END * V_SINAL;
         OUT_RECORD.NRORG := V_NRORG;
         OUT_RECORD.NRPROJECAOMANUALROP := 0;
         PIPE ROW (OUT_RECORD);

         IF P_IDRELAT <> 'GU' THEN
            IF NOT ((C_PARFILIAL.IDCONSLANCAJUSPAG = 'N') AND (C_PARFILIAL.IDCONSLANCAJUSREC = 'N') AND
               (C_PARFILIAL.IDCONSCREDBCO = 'N') AND (C_PARFILIAL.IDCONSDEBBCO = 'N')) THEN
            
               V_NRRESUMO_ROP := 0;

               OUT_RECORD.NRORDEM         := 17;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Despesas (-)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'DESPESA';
               OUT_RECORD.VRREAL          := NVL(V_DESPESA,0) * V_SINAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_DESPESA,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                              THEN V_BRUTO
                                                                              ELSE V_LIQUIDO
                                                                         END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJECAO      := NVL(V_DESPESA_P,0) * V_SINAL;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                                  THEN NVL(V_DESPESA_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                THEN V_BRUTO_P
                                                                                ELSE V_LIQUIDO_P
                                                                           END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJMANUAL    := 0;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_DESPESA_O * V_SINAL;
               OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                                  THEN NVL(V_DESPESA_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                THEN V_BRUTO_O
                                                                                ELSE V_LIQUIDO_O
                                                                           END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.NRORG := V_NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := 0;
               PIPE ROW (OUT_RECORD);
            END IF;

            IF (C_PARFILIAL.IDCONSLANCAJUSPAG = 'S') OR (C_PARFILIAL.IDCONSLANCAJUSREC = 'S') THEN
               V_NRRESUMO_ROP := 0;

               OUT_RECORD.NRORDEM         := 18;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Ajustes (+/-)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'AJUST';
               OUT_RECORD.VRREAL          := NVL(V_AJUSTE,0) * V_SINAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_AJUSTE,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                             THEN V_BRUTO
                                                                             ELSE V_LIQUIDO
                                                                        END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJECAO      := NVL(V_AJUSTE_P,0) * V_SINAL;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                                  THEN NVL(V_AJUSTE_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                               THEN V_BRUTO_P
                                                                               ELSE V_LIQUIDO_P
                                                                          END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJMANUAL    := 0;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_AJUSTE_O * V_SINAL;
               OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                                  THEN NVL(V_AJUSTE_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                               THEN V_BRUTO_O
                                                                               ELSE V_LIQUIDO_O
                                                                          END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.NRORG := V_NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := 0;
               PIPE ROW (OUT_RECORD);
            END IF;
      
            IF (C_PARFILIAL.IDCONSCREDBCO = 'S') OR (C_PARFILIAL.IDCONSDEBBCO = 'S') THEN
               V_NRRESUMO_ROP := 0;

               OUT_RECORD.NRORDEM         := 19;
               OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
               OUT_RECORD.DSFILIAL        := V_DSFILIAL;
               OUT_RECORD.DSMODELO        := V_DSMODELO;
               OUT_RECORD.NRTOPICO        := NULL;
               /*OUT_RECORD.DSDESCRICAOTOP  := '    Debito/Credito (+/-)';*/
               OUT_RECORD.DSDESCRICAOTOP  := 'DEBCRE';
               OUT_RECORD.VRREAL          := NVL(V_DEBCREDBCO,0) * V_SINAL;
               OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                                  THEN NVL(V_DEBCREDBCO,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                 THEN V_BRUTO
                                                                                 ELSE V_LIQUIDO
                                                                            END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJECAO      := NVL(V_DEBCREDBCO_P,0) * V_SINAL;
               OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                                  THEN NVL(V_DEBCREDBCO_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                   THEN V_BRUTO_P
                                                                                   ELSE V_LIQUIDO_P
                                                                              END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.VRPROJMANUAL    := 0;
               OUT_RECORD.VRPERPROJMANUAL := NULL;
               OUT_RECORD.VRORCADO        := V_DEBCREDBCO_O * V_SINAL;
               OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                                  THEN NVL(V_DEBCREDBCO_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                   THEN V_BRUTO_O
                                                                                   ELSE V_LIQUIDO_O
                                                                              END
                                                  ELSE 0
                                             END * V_SINAL;
               OUT_RECORD.NRORG := V_NRORG;
               OUT_RECORD.NRPROJECAOMANUALROP := 0;
               PIPE ROW (OUT_RECORD);
            END IF;
         END IF;

         IF (C_PARFILIAL.IDDESPMOBROP = 'S') OR (C_PARFILIAL.IDAPURDESPENTRMO = 'S') OR
            (C_PARFILIAL.IDDESPMOBROPCTB = 'S') OR (C_PARFILIAL.IDAPUMOCTPGROP = 'S') THEN
            V_NRRESUMO_ROP := 7;

            OUT_RECORD.NRORDEM         := 20;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := 5;
            /*OUT_RECORD.DSDESCRICAOTOP  := 'Mao de Obra (-)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'MAOBRA';
            OUT_RECORD.VRREAL          := NVL(V_MAOOBRA,0);
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_MAOOBRA,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                           THEN V_BRUTO
                                                                           ELSE V_LIQUIDO
                                                                      END
                                               ELSE 0
                                          END;
            OUT_RECORD.VRPROJECAO      := NVL(V_MAOOBRA_P,0);
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                               THEN NVL(V_MAOOBRA_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                             THEN V_BRUTO_P
                                                                             ELSE V_LIQUIDO_P
                                                                        END
                                               ELSE 0
                                          END;
            OUT_RECORD.VRPROJMANUAL    := 0;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_MAOOBRA_O;
            OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                               THEN NVL(V_MAOOBRA_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                             THEN V_BRUTO_O
                                                                             ELSE V_LIQUIDO_O
                                                                        END
                                               ELSE 0
                                          END;
            OUT_RECORD.NRORG := V_NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := 0;
            PIPE ROW (OUT_RECORD);
         END IF;

         V_NRRESUMO_ROP := 11;

         OUT_RECORD.NRORDEM         := 21;
         OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
         OUT_RECORD.DSFILIAL        := V_DSFILIAL;
         OUT_RECORD.DSMODELO        := V_DSMODELO;
         OUT_RECORD.NRTOPICO        := NULL;
         /*OUT_RECORD.DSDESCRICAOTOP  := 'Resultado (=)';*/
         OUT_RECORD.DSDESCRICAOTOP  := 'RESULT';
         OUT_RECORD.VRREAL          := V_VRRESULTPARC;
         OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                            THEN NVL(V_VRRESULTPARC,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                             THEN V_BRUTO
                                                                             ELSE V_LIQUIDO
                                                                        END
                                            ELSE 0
                                       END;
         OUT_RECORD.VRPROJECAO      := V_VRRESULTPARC_P;
         OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                            THEN NVL(V_VRRESULTPARC_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                               THEN V_BRUTO_P
                                                                               ELSE V_LIQUIDO_P
                                                                          END
                                            ELSE 0
                                       END;
         OUT_RECORD.VRPROJMANUAL    := 0;
         OUT_RECORD.VRPERPROJMANUAL := NULL;
         OUT_RECORD.VRORCADO        := V_VRRESULTPARC_O;
         OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                            THEN NVL(V_VRRESULTPARC_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                               THEN V_BRUTO_O
                                                                               ELSE V_LIQUIDO_O
                                                                          END
                                            ELSE 0
                                       END;
         OUT_RECORD.NRORG := V_NRORG;
         OUT_RECORD.NRPROJECAOMANUALROP := 0;
         PIPE ROW (OUT_RECORD);

         IF C_PARFILIAL.IDAPURDEPRECROP = 'S' THEN
            V_NRRESUMO_ROP := 8;

            OUT_RECORD.NRORDEM         := 22;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := 'Depreciacao de Bens (-)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'DEPRE';
            OUT_RECORD.VRREAL          := NVL(V_DEPREC,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_DEPREC,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                          THEN V_BRUTO
                                                                          ELSE V_LIQUIDO
                                                                     END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := NVL(V_DEPREC_P,0) * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                               THEN NVL(V_DEPREC_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                            THEN V_BRUTO_P
                                                                            ELSE V_LIQUIDO_P
                                                                       END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := 0;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_DEPREC_O * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                               THEN NVL(V_DEPREC_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                            THEN V_BRUTO_O
                                                                            ELSE V_LIQUIDO_O
                                                                       END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.NRORG := V_NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := 0;
            PIPE ROW (OUT_RECORD);
         END IF;

         IF (C_PARFILIAL.IDAPUINVFILROP = 'S') OR (C_PARFILIAL.IDAPURAINVCTPAGROP = 'S') THEN
            V_NRRESUMO_ROP := 12;

            OUT_RECORD.NRORDEM         := 23;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := 6;
            /*OUT_RECORD.DSDESCRICAOTOP  := 'Investimentos (-)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'INVEST';
            OUT_RECORD.VRREAL          := NVL(V_INVESTI,0) * V_SINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_INVESTI,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                           THEN V_BRUTO
                                                                           ELSE V_LIQUIDO
                                                                      END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJECAO      := NVL(V_INVESTI_P,0) * V_SINAL;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                               THEN NVL(V_INVESTI_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                             THEN V_BRUTO_P
                                                                             ELSE V_LIQUIDO_P
                                                                        END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.VRPROJMANUAL    := 0;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_INVESTI_O * V_SINAL;
            OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                               THEN NVL(V_INVESTI_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                             THEN V_BRUTO_O
                                                                             ELSE V_LIQUIDO_O
                                                                        END
                                               ELSE 0
                                          END * V_SINAL;
            OUT_RECORD.NRORG := V_NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := 0;
            PIPE ROW (OUT_RECORD);
         END IF;

         IF (C_PARFILIAL.IDAPURDEPRECROP = 'S') OR (C_PARFILIAL.IDAPUINVFILROP = 'S') OR (C_PARFILIAL.IDAPURAINVCTPAGROP = 'S') THEN
            V_NRRESUMO_ROP := 13;

            OUT_RECORD.NRORDEM         := 24;
            OUT_RECORD.NRRESUMO_ROP    := V_NRRESUMO_ROP;
            OUT_RECORD.DSFILIAL        := V_DSFILIAL;
            OUT_RECORD.DSMODELO        := V_DSMODELO;
            OUT_RECORD.NRTOPICO        := NULL;
            /*OUT_RECORD.DSDESCRICAOTOP  := 'Resultado Total (=)';*/
            OUT_RECORD.DSDESCRICAOTOP  := 'RESULTOT';
            OUT_RECORD.VRREAL          := V_VRRESULTFINAL;
            OUT_RECORD.VRPERREAL       := CASE WHEN (V_LIQUIDO > 0) AND (V_BRUTO > 0)
                                               THEN NVL(V_VRRESULTFINAL,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                 THEN V_BRUTO
                                                                                 ELSE V_LIQUIDO
                                                                            END
                                               ELSE 0
                                          END;
            OUT_RECORD.VRPROJECAO      := V_VRRESULTFINAL_P;
            OUT_RECORD.VRPERPROJECAO   := CASE WHEN (V_LIQUIDO_P > 0) AND (V_BRUTO_P > 0)
                                               THEN NVL(V_VRRESULTFINAL_P,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                   THEN V_BRUTO_P
                                                                                   ELSE V_LIQUIDO_P
                                                                              END
                                               ELSE 0
                                          END;
            OUT_RECORD.VRPROJMANUAL    := 0;
            OUT_RECORD.VRPERPROJMANUAL := NULL;
            OUT_RECORD.VRORCADO        := V_VRRESULTFINAL_O;
            OUT_RECORD.VRPERORCADO     := CASE WHEN (V_LIQUIDO_O > 0) AND (V_BRUTO_O > 0)
                                               THEN NVL(V_VRRESULTFINAL_O,0)/ CASE WHEN P_IDPERCEN = 'B'
                                                                                   THEN V_BRUTO_O
                                                                                   ELSE V_LIQUIDO_O
                                                                              END
                                               ELSE 0
                                          END;
            OUT_RECORD.NRORG := V_NRORG;
            OUT_RECORD.NRPROJECAOMANUALROP := 0;
            PIPE ROW (OUT_RECORD);
         END IF;
      END LOOP;
   END IF;
END;
/