CREATE OR REPLACE PROCEDURE CALCULA_ORCAMENTO(
    P_CDFILIAL        IN    ORCAMENTOOP.CDFILIAL%TYPE,
    P_NRANOORCAOP     IN    ORCAMENTOOP.NRANOORCAOP%TYPE,
    P_IDITEMORCA      IN    VARCHAR,
    P_CDOPERADOR      IN    VARCHAR,
    P_DATACOMP        IN    VARCHAR,
    P_DATAINIC        IN    DATE,
    P_DATAFIN         IN    DATE,
    P_CDORCAMENTOMO   IN    VARCHAR DEFAULT NULL,
    P_RETORNO         OUT   VARCHAR
)AS

    V_FILIALPAR           VARCHAR2(4);
    V_CDFILCENTPME        VARCHAR2(4);
    V_IDFATUROP           VARCHAR2(2);
    V_NRLANC              VARCHAR2(10);
    V_CDSERVICO           VARCHAR2(05);
    V_NRAPURREL           VARCHAR2(20);
    V_NRITEMORCA          VARCHAR2(10);
    V_IDSERVFATROP        CHAR;
    V_IDFILIALAGRROP      CHAR;
    V_IDUTPADRROPFIL      CHAR;
    V_IDUTPADRROP         CHAR;
    V_IDCONSDIAUTFEC      CHAR;
    V_IDFDIAENTREST       CHAR;
    V_IDFDIASAIDEST       CHAR;
    V_IDFDIACARDAP        CHAR;
    V_IDFDIARETIEST       CHAR;
    V_IDFDIAREQEST        CHAR;
    V_IDFDIAREQPLA        CHAR;
    V_IDFDIAEFEPLA        CHAR;
    V_IDFDIAEFEFAT        CHAR;
    V_IDTIPOCALCTAXAADM   VARCHAR2(50);
    V_NRMES               INTEGER;
    V_COUNT               INTEGER;
    V_VRPERCDESPORCMO     INTEGER;
    V_VRORCAMENTOMES      NUMBER;
    V_QTEFETIVO           NUMBER;
    V_QTEFETMES           NUMBER;
    V_VREFETMES           NUMBER;
    V_VRTOTEFETMES        NUMBER;
    V_QTSERVTRAB          NUMBER;
    V_VRSERVTRAB          NUMBER;
    V_NRCONSPLANMESBASE   NOVOCODIGO.NRSEQUENCIAL%TYPE;
    V_INIMESCONS          DATE;
    V_DTANOORCA           DATE;
    V_AUXDTANOORCA        DATE;
    V_DTMESBASE           DATE;
    V_DATAINI             DATE;
    V_DATAFIN             DATE;
    V_BOFDIA              BOOLEAN;
    V_CDTPINDICEFAT       VARCHAR2(1);
    V_IDPERVALORFAT       VARCHAR2(1);
    V_CDINDICEFAT         VARCHAR2(4);
    V_CDTOINDICECON       VARCHAR2(1);
    V_IDPERVALORCON       VARCHAR2(1);
    V_CDINDICECON         VARCHAR2(4);
    V_CDTIPOCUSTO         NUMBER;
    V_VRPERCORRCOMJAN     NUMBER;
    V_VRPERCORRCOMFEV     NUMBER;
    V_VRPERCORRCOMMAR     NUMBER;
    V_VRPERCORRCOMABR     NUMBER;
    V_VRPERCORRCOMMAI     NUMBER;
    V_VRPERCORRCOMJUN     NUMBER;
    V_VRPERCORRCOMJUL     NUMBER;
    V_VRPERCORRCOMAGO     NUMBER;
    V_VRPERCORRCOMSET     NUMBER;
    V_VRPERCORRCOMOUT     NUMBER;
    V_VRPERCORRCOMNOV     NUMBER;
    V_VRPERCORRCOMDEZ     NUMBER;
    V_VRCONSPLANJAN       NUMBER;
    V_VRCONSPLANFEV       NUMBER;
    V_VRCONSPLANMAR       NUMBER;
    V_VRCONSPLANABR       NUMBER;
    V_VRCONSPLANMAI       NUMBER;
    V_VRCONSPLANJUN       NUMBER;
    V_VRCONSPLANJUL       NUMBER;
    V_VRCONSPLANAGO       NUMBER;
    V_VRCONSPLANSET       NUMBER;
    V_VRCONSPLANOUT       NUMBER;
    V_VRCONSPLANNOV       NUMBER;
    V_VRCONSPLANDEZ       NUMBER;
    V_QTCONSPLANJAN       NUMBER;
    V_QTCONSPLANFEV       NUMBER;
    V_QTCONSPLANMAR       NUMBER;
    V_QTCONSPLANABR       NUMBER;
    V_QTCONSPLANMAI       NUMBER;
    V_QTCONSPLANJUN       NUMBER;
    V_QTCONSPLANJUL       NUMBER;
    V_QTCONSPLANAGO       NUMBER;
    V_QTCONSPLANSET       NUMBER;
    V_QTCONSPLANOUT       NUMBER;
    V_QTCONSPLANNOV       NUMBER;
    V_QTCONSPLANDEZ       NUMBER;
    V_VRINDICEJAN         NUMBER;
    V_VRINDICEFEV         NUMBER;
    V_VRINDICEMAR         NUMBER(15, 5);
    V_VRINDICEABR         NUMBER;
    V_VRINDICEMAI         NUMBER;
    V_VRINDICEJUN         NUMBER;
    V_VRINDICEJUL         NUMBER;
    V_VRINDICEAGO         NUMBER;
    V_VRINDICESET         NUMBER;
    V_VRINDICEOUT         NUMBER;
    V_VRINDICENOV         NUMBER;
    V_VRINDICEDEZ         NUMBER;
    V_QTTOTEFETMES        NUMBER;
    V_PERCORRECAO         NUMBER;
    V_NRMESREAJCONTR      VARCHAR2(100);
    V_VRINDICE            NUMBER;
    WDIA                  CHAR(2):= '01';
    W_S                   CHAR := 'S';
    W_N                   CHAR := 'N';
    V_PRIM                CHAR;
    V_PRIMFATLIQ          CHAR;
    V_DATA                DATE;
    V_SERVICOS            VARCHAR2(3000);
    V_NRCONSPLAN          NOVOCODIGO.NRSEQUENCIAL%TYPE;
    V_IDSERVRETROP        CHAR;
    V_VRORCAMENTOJAN      NUMBER;
    V_VRORCAMENTOFEV      NUMBER;
    V_VRORCAMENTOMAR      NUMBER;
    V_VRORCAMENTOABR      NUMBER;
    V_VRORCAMENTOMAI      NUMBER;
    V_VRORCAMENTOJUN      NUMBER;
    V_VRORCAMENTOJUL      NUMBER;
    V_VRORCAMENTOAGO      NUMBER;
    V_VRORCAMENTOSET      NUMBER;
    V_VRORCAMENTOOUT      NUMBER;
    V_VRORCAMENTONOV      NUMBER;
    V_VRORCAMENTODEZ      NUMBER;
    V_NRAUDCALC           VARCHAR2(10);
    V_VRCONSMESBASE       NUMBER;
    V_VRMESBASE           NUMBER;
    V_QTMESBASE           NUMBER;
    V_VRINDICEMESBASE     NUMBER;
    V_VRREAJUSTE          NUMBER;
    V_CDVERSAOORCA        NUMBER;
    V_NRMESINIVERORC      VARCHAR2(2);
    V_NRANO               VARCHAR2(4);
    V_CALCOMESTORC        VARCHAR2(1);
    V_VRSERVFATU          FLOAT;
    V_SUMEFET             FLOAT;
    V_SUMEFETMES          FLOAT;
    V_DTANOORCAINI        DATE;
    V_DTANOORCAFIN        DATE;
    V_DTANOORCACONS       DATE;
    V_DTANOORCACONS2      DATE;
    V_VRCUSTPADR          FLOAT;
    V_NRMES2              INTEGER;
    V_CONTAMES            INTEGER;
    V_MESREFERENC         VARCHAR2(02);
    V_DTINICPER           VARCHAR2(02);
    V_DTFINALPER          VARCHAR2(02);
    V_DTINICPERANO        VARCHAR2(04);
    V_DTFINALPERANO       VARCHAR2(04);
    DATAINIVL             DATE;
    DATAFINVL             DATE;
    V_IDITEMCALCORCA      VARCHAR2(02);
    V_CDFILIAL            VARCHAR2(04);
    V_IDTIPOORCAOP        VARCHAR2(01);
    V_VERSORCA            NUMBER;
    V_VRORCAIMPOJAN       FLOAT;
    V_VRORCAIMPOFEV       FLOAT;
    V_VRORCAIMPOMAR       FLOAT;
    V_VRORCAIMPOABR       FLOAT;
    V_VRORCAIMPOMAI       FLOAT;
    V_VRORCAIMPOJUN       FLOAT;
    V_VRORCAIMPOJUL       FLOAT;
    V_VRORCAIMPOAGO       FLOAT;
    V_VRORCAIMPOSET       FLOAT;
    V_VRORCAIMPOOUT       FLOAT;
    V_VRORCAIMPONOV       FLOAT;
    V_VRORCAIMPODEZ       FLOAT;
    V_VRPERCIMPO          FLOAT;
    V_RETORNO             INTEGER;
    V_DTDEPRMES           DATE;
    V_VRORCAMO            FLOAT;
    V_DIASUTCARD          INTEGER;
    V_QTEFETIMES          NUMBER;
    V_TXADM               FLOAT;
    V_MESINI              INTEGER;
    V_VRTXADM             FLOAT;
    V_IDCALCFATUROP       VARCHAR2(10);
    V_SUMSERVCLI          FLOAT;
    V_VRIMPORCAJAN        FLOAT;
    V_VRIMPORCAFEV        FLOAT;
    V_VRIMPORCAMAR        FLOAT;
    V_VRIMPORCAABR        FLOAT;
    V_VRIMPORCAMAI        FLOAT;
    V_VRIMPORCAJUN        FLOAT;
    V_VRIMPORCAJUL        FLOAT;
    V_VRIMPORCAAGO        FLOAT;
    V_VRIMPORCASET        FLOAT;
    V_VRIMPORCAOUT        FLOAT;
    V_VRIMPORCANOV        FLOAT;
    V_VRIMPORCADEZ        FLOAT;
    V_IDDETALHEFATUR      VARCHAR2(10);
    V_DTINIANOBASE        DATE;
    V_DTFIMANOBASE        DATE;
    V_VRCONSPLAN          FLOAT;
    V_QTCONSPLAN          FLOAT;
    V_CAMPOMES            FLOAT;
BEGIN
    BEGIN
        SELECT IDFATUROP,
               IDSERVFATROP,
               IDCONSDIAUTFEC,
               IDFDIAENTREST,
               IDFDIASAIDEST,
               IDFDIACARDAP,
               IDFDIARETIEST,
               IDFDIAREQEST,
               IDFDIAREQPLA,
               IDFDIAEFEPLA,
               IDFDIAEFEFAT,
               IDSERVRETROP,
               IDTIPOCALCTAXAADM,
               IDDETALHEFATUR
          INTO
            V_IDFATUROP,
            V_IDSERVFATROP,
            V_IDCONSDIAUTFEC,
            V_IDFDIAENTREST,
            V_IDFDIASAIDEST,
            V_IDFDIACARDAP,
            V_IDFDIARETIEST,
            V_IDFDIAREQEST,
            V_IDFDIAREQPLA,
            V_IDFDIAEFEPLA,
            V_IDFDIAEFEFAT,
            V_IDSERVRETROP,
            V_IDTIPOCALCTAXAADM,
            V_IDDETALHEFATUR
          FROM PARFILIALROP   P,
               PARAMFILIAL    F
         WHERE P.CDFILIAL   = TIPOPARAM_DRO(P_CDFILIAL)AND
               F.CDFILIAL   = P_CDFILIAL;

    EXCEPTION
        WHEN NO_DATA_FOUND THEN
            BEGIN
                V_IDFATUROP        := '';
                V_IDSERVFATROP     := NULL;
                V_IDSERVRETROP     := 'N';
                V_IDCONSDIAUTFEC   := 'S';
                V_IDFDIAENTREST    := 'N';
                V_IDFDIASAIDEST    := 'N';
                V_IDFDIACARDAP     := 'N';
                V_IDFDIARETIEST    := 'N';
                V_IDFDIAREQEST     := 'N';
                V_IDFDIAREQPLA     := 'N';
                V_IDFDIAEFEPLA     := 'N';
                V_IDFDIAEFEFAT     := 'N';
            END;
    END;

    SELECT O.CDVERSAOORCA,
           O.NRMESINIVERORC
      INTO
        V_CDVERSAOORCA,
        V_NRMESINIVERORC
      FROM VERSAOORCA O,
           (
               SELECT MAX(CDVERSAOORCA)CDVERSAOORCA,
                      CDANOVERORCA
                 FROM VERSAOORCA
                WHERE CDANOVERORCA = P_NRANOORCAOP
                GROUP BY CDANOVERORCA
           )V
     WHERE O.CDANOVERORCA   = V.CDANOVERORCA AND
           O.CDVERSAOORCA   = V.CDVERSAOORCA;

    SELECT MAX(NVL(VRPERCORCOMFJAN,(
        SELECT NVL(VRPERCORRCOMJAN, 100)
          FROM PARAMORCAOPERAC
    ))),
           MAX(NVL(VRPERCORCOMFFEV,(
               SELECT NVL(VRPERCORRCOMFEV, 100)
                 FROM PARAMORCAOPERAC
           ))),
           MAX(NVL(VRPERCORCOMFMAR,(
               SELECT NVL(VRPERCORRCOMMAR, 100)
                 FROM PARAMORCAOPERAC
           ))),
           MAX(NVL(VRPERCORCOMFABR,(
               SELECT NVL(VRPERCORRCOMABR, 100)
                 FROM PARAMORCAOPERAC
           ))),
           MAX(NVL(VRPERCORCOMFMAI,(
               SELECT NVL(VRPERCORRCOMMAI, 100)
                 FROM PARAMORCAOPERAC
           ))),
           MAX(NVL(VRPERCORCOMFJUN,(
               SELECT NVL(VRPERCORRCOMJUN, 100)
                 FROM PARAMORCAOPERAC
           ))),
           MAX(NVL(VRPERCORCOMFJUL,(
               SELECT NVL(VRPERCORRCOMJUL, 100)
                 FROM PARAMORCAOPERAC
           ))),
           MAX(NVL(VRPERCORCOMFAGO,(
               SELECT NVL(VRPERCORRCOMAGO, 100)
                 FROM PARAMORCAOPERAC
           ))),
           MAX(NVL(VRPERCORCOMFSET,(
               SELECT NVL(VRPERCORRCOMSET, 100)
                 FROM PARAMORCAOPERAC
           ))),
           MAX(NVL(VRPERCORCOMFOUT,(
               SELECT NVL(VRPERCORRCOMOUT, 100)
                 FROM PARAMORCAOPERAC
           ))),
           MAX(NVL(VRPERCORCOMFNOV,(
               SELECT NVL(VRPERCORRCOMNOV, 100)
                 FROM PARAMORCAOPERAC
           ))),
           MAX(NVL(VRPERCORCOMFDEZ,(
               SELECT NVL(VRPERCORRCOMDEZ, 100)
                 FROM PARAMORCAOPERAC
           )))
      INTO
        V_VRPERCORRCOMJAN,
        V_VRPERCORRCOMFEV,
        V_VRPERCORRCOMMAR,
        V_VRPERCORRCOMABR,
        V_VRPERCORRCOMMAI,
        V_VRPERCORRCOMJUN,
        V_VRPERCORRCOMJUL,
        V_VRPERCORRCOMAGO,
        V_VRPERCORRCOMSET,
        V_VRPERCORRCOMOUT,
        V_VRPERCORRCOMNOV,
        V_VRPERCORRCOMDEZ
      FROM FILIAL
     WHERE CDFILIAL = P_CDFILIAL;

    SELECT MAX(CDTPINDICEFAT),
           MAX(IDPERVALORFAT),
           MAX(CDINDICEFAT),
           MAX(CDTOINDICECON),
           MAX(IDPERVALORCON),
           MAX(CDINDICECON),
           MAX(TO_NUMBER(NVL(CDTIPOCUSTO, 3))),
           MAX(IDCALCFATUROP)
      INTO
        V_CDTPINDICEFAT,
        V_IDPERVALORFAT,
        V_CDINDICEFAT,
        V_CDTOINDICECON,
        V_IDPERVALORCON,
        V_CDINDICECON,
        V_CDTIPOCUSTO,
        V_IDCALCFATUROP
      FROM PARAMORCAOPERAC;

    V_DATA          := SYSDATE;
    V_CDTIPOCUSTO   := TO_NUMBER(NVL(V_CDTIPOCUSTO, 3));
    INSERT INTO ORCAMENTOOP(
        NRANOORCAOP,
        CDVERSORCAOP,
        CDFILIAL,
        CDOPERADOR,
        DTORCAMENTO,
        IDSTATUSORCA
    )
        (SELECT P_NRANOORCAOP,
                V_CDVERSAOORCA,
                P_CDFILIAL,
                P_CDOPERADOR,
                V_DATA,
                'P'
          FROM DUAL
        MINUS
        SELECT P_NRANOORCAOP,
               V_CDVERSAOORCA,
               P_CDFILIAL,
               P_CDOPERADOR,
               V_DATA,
               'P'
          FROM ORCAMENTOOP
         WHERE CDFILIAL       = P_CDFILIAL AND
               NRANOORCAOP    = P_NRANOORCAOP AND
               CDVERSORCAOP   = V_CDVERSAOORCA
        );

    IF P_IDITEMORCA = 'F' THEN
        SELECT NVL(MAX(DTMESBASEITEM), SYSDATE)
          INTO V_DTMESBASE
          FROM ITEMORCAOP
         WHERE CDFILIAL       = P_CDFILIAL AND
               IDTIPOORCAOP   = 'F' AND
               NRANOORCAOP    = P_NRANOORCAOP AND
               CDVERSORCAOP   = V_CDVERSAOORCA;

        V_PRIMFATLIQ   := 'S';
        V_NRITEMORCA   := '0000000000';
        V_SUMEFETMES   := 0;
        IF V_IDFATUROP <> 'FS' THEN
            V_INIMESCONS := TO_DATE(WDIA || '/' || TO_CHAR(V_DTMESBASE, 'MM/YYYY'));

            SELECT LPAD(ABS(ROUND(DBMS_RANDOM.VALUE(9999, 9999999999))), 10, '0')
              INTO V_NRAPURREL
              FROM DUAL;

            APURAC_RELGESDIRETA(P_CDFILIAL, V_INIMESCONS, LAST_DAY(V_INIMESCONS), V_NRAPURREL);
            FOR C_SERVICOS IN(
                SELECT SERV.*
                  FROM(
                    SELECT SC.CDSERVFILI,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTISEG, SE.QTCOMESTISEG), 0)AS QTCOMESTISEG,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTITER, SE.QTCOMESTITER), 0)AS QTCOMESTITER,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTIQUA, SE.QTCOMESTIQUA), 0)AS QTCOMESTIQUA,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTIQUI, SE.QTCOMESTIQUI), 0)AS QTCOMESTIQUI,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTISEX, SE.QTCOMESTISEX), 0)AS QTCOMESTISEX,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTISAB, SE.QTCOMESTISAB), 0)AS QTCOMESTISAB,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTIDOM, SE.QTCOMESTIDOM), 0)AS QTCOMESTIDOM,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTIFER, SE.QTCOMESTIFER), 0)AS QTCOMESTIFER
                      FROM SERVICOROP    SC,
                           SERVICO       SE,
                           PARAMFILIAL   PA
                     WHERE SE.CDFILIAL      = P_CDFILIAL AND
                           SE.CDSERVFILI    = SC.CDSERVFILI AND
                           SE.CDFILIAL      = PA.CDFILIAL AND
                           (SC.CDFILIAL      = TIPOPARAM_DRO(P_CDFILIAL))AND
                           (IDTPSERVICOROP   = 'E')AND
                           (V_IDSERVFATROP <> 'S')
                    UNION ALL
                    SELECT SE.CDSERVFILI,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTISEG, SE.QTCOMESTISEG), 0)AS QTCOMESTISEG,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTITER, SE.QTCOMESTITER), 0)AS QTCOMESTITER,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTIQUA, SE.QTCOMESTIQUA), 0)AS QTCOMESTIQUA,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTIQUI, SE.QTCOMESTIQUI), 0)AS QTCOMESTIQUI,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTISEX, SE.QTCOMESTISEX), 0)AS QTCOMESTISEX,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTISAB, SE.QTCOMESTISAB), 0)AS QTCOMESTISAB,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTIDOM, SE.QTCOMESTIDOM), 0)AS QTCOMESTIDOM,
                           NVL(DECODE(PA.IDCOMCUSPERC, 'C', SE.QTCLIESTIFER, SE.QTCOMESTIFER), 0)AS QTCOMESTIFER
                      FROM SERVICO       SE,
                           PARAMFILIAL   PA
                     WHERE SE.CDFILIAL      = P_CDFILIAL AND
                           SE.CDFILIAL      = PA.CDFILIAL AND
                           V_IDSERVFATROP   = 'S' AND
                           (SE.QTCOMESTISEG IS NOT NULL OR
                            SE.QTCOMESTITER IS NOT NULL OR
                            SE.QTCOMESTIQUA IS NOT NULL OR
                            SE.QTCOMESTIQUI IS NOT NULL OR
                            SE.QTCOMESTISEX IS NOT NULL OR
                            SE.QTCOMESTISAB IS NOT NULL OR
                            SE.QTCOMESTIDOM IS NOT NULL OR
                            SE.QTCOMESTIFER IS NOT NULL)
                     ORDER BY CDSERVFILI
                )SERV,
                      (
                          SELECT CDSERVFILI
                            FROM ITEMORCAOP
                           WHERE CDFILIAL       = P_CDFILIAL AND
                                 NRANOORCAOP    = P_NRANOORCAOP AND
                                 CDVERSORCAOP   = V_CDVERSAOORCA AND
                                 IDTIPOORCAOP   = 'F'
                      )ORC
                 WHERE SERV.CDSERVFILI = ORC.CDSERVFILI
                 ORDER BY SERV.CDSERVFILI
            )LOOP
                V_QTSERVTRAB     := 0;
                V_VRSERVTRAB     := 0;
                SELECT SUM(NVL((VRPRECUNITAPGD * NREFETIVOSAPGD), 0)),
                       SUM(NVL(NREFETIVOSAPGD, 0))
                  INTO
                    V_VRSERVTRAB,
                    V_QTSERVTRAB
                  FROM TRAB_APURAC_GD T
                 WHERE(T.CDFILIALAPGD     = P_CDFILIAL)AND
                      (T.NRAPURACGD       = V_NRAPURREL)AND
                      (T.CDSERVFILIAPGD   = C_SERVICOS.CDSERVFILI)
                 ORDER BY CDSERVFILIAPGD,
                          DTAPURACFATGD;

                V_VREFETMES      := 0;
                IF(V_QTSERVTRAB > 0)AND(V_VRSERVTRAB > 0)THEN
                    V_VREFETMES :=(V_VRSERVTRAB / V_QTSERVTRAB);
                END IF;

                V_NRMES          := 0;
                V_VRTOTEFETMES   := 0;
                V_QTTOTEFETMES   := 0;
                IF P_DATACOMP = 'S' THEN
                    V_CONTAMES      := 12;
                    V_MESREFERENC   := V_NRMESINIVERORC;
                ELSE
                    SELECT TO_CHAR(P_DATAINIC, 'MM'),
                           TO_CHAR(P_DATAFIN, 'MM'),
                           TO_CHAR(P_DATAINIC, 'YYYY'),
                           TO_CHAR(P_DATAFIN, 'YYYY')
                      INTO
                        V_DTINICPER,
                        V_DTFINALPER,
                        V_DTINICPERANO,
                        V_DTFINALPERANO
                      FROM DUAL;

                    V_MESREFERENC   := V_DTINICPER;
                    V_CONTAMES      := 0;
                    DATAINIVL       := '01/' || V_DTINICPER || '/' || V_DTINICPERANO;
                    DATAFINVL       := '01/' || V_DTFINALPER || '/' || V_DTFINALPERANO;
                    WHILE(DATAINIVL <= DATAFINVL)LOOP
                        V_CONTAMES   := V_CONTAMES + 1;
                        DATAINIVL    := ADD_MONTHS(DATAINIVL, 1);
                    END LOOP;

                END IF;

                WHILE(V_NRMES < V_CONTAMES)LOOP
                    V_DTANOORCA      := ADD_MONTHS(WDIA || '/' || V_MESREFERENC || '/' || P_NRANOORCAOP, V_NRMES);

                    V_DTANOORCAINI   := V_DTANOORCA;
                    V_AUXDTANOORCA   := V_DTANOORCA;
                    V_QTEFETMES      := 0;
                    WHILE(V_DTANOORCA <= LAST_DAY(V_AUXDTANOORCA))LOOP
                        BEGIN
                            SELECT COUNT(*)
                              INTO V_COUNT
                              FROM FERIFILI
                             WHERE(CDFILIAL     = P_CDFILIAL)AND
                                  (DTFERIFILI   = V_DTANOORCA);

                        EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                                V_COUNT := 0;
                        END;

                        IF(V_COUNT = 0)OR(V_IDCONSDIAUTFEC = 'N')THEN
                            BEGIN
                                SELECT DECODE(TO_CHAR(V_DTANOORCA, 'D'), '2', C_SERVICOS.QTCOMESTISEG, DECODE(TO_CHAR(V_DTANOORCA
                                , 'D'), '3', C_SERVICOS.QTCOMESTITER, DECODE(TO_CHAR(V_DTANOORCA, 'D'), '4', C_SERVICOS.QTCOMESTIQUA
                                , DECODE(TO_CHAR(V_DTANOORCA, 'D'), '5', C_SERVICOS.QTCOMESTIQUI, DECODE(TO_CHAR(V_DTANOORCA, 'D'
                                ), '6', C_SERVICOS.QTCOMESTISEX, DECODE(TO_CHAR(V_DTANOORCA, 'D'), '7', C_SERVICOS.QTCOMESTISAB, C_SERVICOS
                                .QTCOMESTIDOM))))))
                                  INTO V_QTEFETIVO
                                  FROM DUAL;

                            EXCEPTION
                                WHEN NO_DATA_FOUND THEN
                                    V_QTEFETIVO := 0;
                            END;

                            V_QTEFETMES := V_QTEFETMES + V_QTEFETIVO;
                        ELSE
                            IF(V_IDFDIAENTREST = 'S')AND(V_BOFDIA = FALSE)THEN
                                BEGIN
                                    SELECT NRLANCESTQ
                                      INTO V_NRLANC
                                      FROM LANCTOESTOQ L
                                     WHERE(L.CDFILIMOVI   = P_CDFILIAL)AND
                                          (L.DTLANCESTQ   = V_DTANOORCA)AND
                                          (L.IDTPLANCTO IN(
                                              '0',
                                              '1',
                                              'E',
                                              '2'
                                          ));

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_NRLANC := NULL;
                                END;

                                IF(SQL%ROWCOUNT = 0)THEN
                                    V_BOFDIA := TRUE;
                                END IF;
                            END IF;

                            IF(V_IDFDIASAIDEST = 'S')AND(V_BOFDIA = FALSE)THEN
                                BEGIN
                                    SELECT NRLANCESTQ
                                      INTO V_NRLANC
                                      FROM LANCTOESTOQ L
                                     WHERE(L.CDFILIMOVI   = P_CDFILIAL)AND
                                          (L.DTLANCESTQ   = V_DTANOORCA)AND
                                          (L.IDTPLANCTO IN(
                                              '0',
                                              '1',
                                              'S',
                                              '4'
                                          ));

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_NRLANC := NULL;
                                END;

                                IF(SQL%ROWCOUNT = 0)THEN
                                    V_BOFDIA := TRUE;
                                END IF;
                            END IF;

                            IF(V_IDFDIARETIEST = 'S')AND(V_BOFDIA = FALSE)THEN
                                BEGIN
                                    SELECT NRLANCESTQ
                                      INTO V_NRLANC
                                      FROM(
                                        SELECT L.NRLANCESTQ
                                          FROM LANCTOESTOQ   L,
                                               RETIRADA      R
                                         WHERE L.CDFILIMOVI   = P_CDFILIAL AND
                                               L.DTLANCESTQ   = V_DTANOORCA AND
                                               L.IDTPLANCTO   = '3' AND
                                               L.CDFILIAL     = R.CDFILANCESTQ AND
                                               L.NRLANCESTQ   = R.NRLANCESTQ
                                        UNION ALL
                                        SELECT L.NRLANCESTQ
                                          FROM LANCTOESTOQ    L,
                                               RETIRADAESTO   R
                                         WHERE L.CDFILIMOVI   = P_CDFILIAL AND
                                               L.DTLANCESTQ   = V_DTANOORCA AND
                                               L.IDTPLANCTO   = '3' AND
                                               L.CDFILIAL     = R.CDFILIALLAN AND
                                               L.NRLANCESTQ   = R.NRLANCESTQ
                                    );

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_NRLANC := NULL;
                                END;

                                IF(SQL%ROWCOUNT = 0)THEN
                                    V_BOFDIA := TRUE;
                                END IF;
                            END IF;

                            IF(V_IDFDIACARDAP = 'S')AND(V_BOFDIA = FALSE)THEN
                                BEGIN
                                    SELECT NRCARDAPIO
                                      INTO V_NRLANC
                                      FROM CARDADIA
                                     WHERE(CDFILIAL     = P_CDFILIAL)AND
                                          (DTCARDAPIO   = V_DTANOORCA);

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_NRLANC := NULL;
                                END;

                                IF(SQL%ROWCOUNT = 0)THEN
                                    V_BOFDIA := TRUE;
                                END IF;
                            END IF;

                            IF(V_IDFDIAREQEST = 'S')AND(V_BOFDIA = FALSE)THEN
                                BEGIN
                                    SELECT NRREQUESTO
                                      INTO V_NRLANC
                                      FROM REQUESTO R
                                     WHERE(R.CDFILIAL     = P_CDFILIAL)AND
                                          (R.DTREQUESTO   = V_DTANOORCA);

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_NRLANC := NULL;
                                END;

                                IF(SQL%ROWCOUNT = 0)THEN
                                    V_BOFDIA := TRUE;
                                END IF;
                            END IF;

                            IF(V_IDFDIAREQPLA = 'S')AND(V_BOFDIA = FALSE)THEN
                                BEGIN
                                    SELECT NRREQUEXTR
                                      INTO V_NRLANC
                                      FROM PREVRETI PR
                                     WHERE(PR.CDFILIAL     = P_CDFILIAL)AND
                                          (PR.DTPREVRETI   = V_DTANOORCA);

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_NRLANC := NULL;
                                END;

                                IF(SQL%ROWCOUNT = 0)THEN
                                    V_BOFDIA := TRUE;
                                END IF;
                            END IF;

                            IF(V_IDFDIAEFEFAT = 'S')AND(V_BOFDIA = FALSE)THEN
                                BEGIN
                                    SELECT CDSERVFILI
                                      INTO V_CDSERVICO
                                      FROM(
                                        SELECT CDSERVFILI
                                          FROM EFECLIFAT
                                         WHERE CDFILIAL    = P_CDFILIAL AND
                                               DTFATURAM   = V_DTANOORCA
                                        UNION ALL
                                        SELECT CDSERVFILI
                                          FROM EFECLIFATTUR
                                         WHERE CDFILIAL       = P_CDFILIAL AND
                                               DTFATURAMTUR   = V_DTANOORCA
                                    );

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_NRLANC := NULL;
                                END;

                                IF(SQL%ROWCOUNT = 0)THEN
                                    V_BOFDIA := TRUE;
                                END IF;
                            END IF;

                            IF(V_IDFDIAEFEPLA = 'S')AND(V_BOFDIA = FALSE)THEN
                                BEGIN
                                    SELECT CDSERVFILI
                                      INTO V_CDSERVICO
                                      FROM EFETCLIE
                                     WHERE CDFILIAL     = P_CDFILIAL AND
                                           DTEFETCLIE   = V_DTANOORCA;

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_CDSERVICO := NULL;
                                END;

                                IF(SQL%ROWCOUNT = 0)THEN
                                    V_BOFDIA := TRUE;
                                END IF;
                            END IF;

                            IF V_BOFDIA THEN
                                V_QTEFETMES := V_QTEFETMES + C_SERVICOS.QTCOMESTIFER;
                            END IF;
                        END IF;

                        V_DTANOORCA := TO_DATE(V_DTANOORCA + 1);
                    END LOOP;

                    SELECT(DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), '01', V_VRPERCORRCOMJAN, DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), '02'
                    , V_VRPERCORRCOMFEV, DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), '03', V_VRPERCORRCOMMAR, DECODE(TO_CHAR(V_AUXDTANOORCA
                    , 'MM'), '04', V_VRPERCORRCOMABR, DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), '05', V_VRPERCORRCOMMAI, DECODE(TO_CHAR
                    (V_AUXDTANOORCA, 'MM'), '06', V_VRPERCORRCOMJUN, DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), '07', V_VRPERCORRCOMJUL
                    , DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), '08', V_VRPERCORRCOMAGO, DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), '09', V_VRPERCORRCOMSET
                    , DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), '10', V_VRPERCORRCOMOUT, DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), '11', V_VRPERCORRCOMNOV
                    , DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), '12', V_VRPERCORRCOMDEZ, 100))))))))))))/ 100)
                      INTO V_PERCORRECAO
                      FROM DUAL;

                    V_VRINDICE       := 0;
                    IF TO_CHAR(V_AUXDTANOORCA, 'MM')>= V_NRMESREAJCONTR THEN
                        SELECT MAX(VRINDICE / 100)
                          INTO V_VRINDICE
                          FROM MOVINDICE
                         WHERE(CDTPINDICE   = V_CDTPINDICEFAT)AND
                              (IDPERVALOR   = V_IDPERVALORFAT)AND
                              (CDINDICE     = V_CDINDICEFAT)AND
                              DTREFIND     =(
                                  SELECT MAX(DTREFIND)
                                    FROM MOVINDICE
                                   WHERE(CDTPINDICE   = V_CDTPINDICEFAT)AND
                                        (IDPERVALOR   = V_IDPERVALORFAT)AND
                                        (CDINDICE     = V_CDINDICEFAT)AND
                                        (DTREFIND <= '01/' || TO_CHAR(V_AUXDTANOORCA, 'MM')|| '/' || P_NRANOORCAOP)
                              );

                    END IF;

                    IF NVL(V_PERCORRECAO, 0)= 0 THEN
                        V_PERCORRECAO := 1;
                    END IF;
                    IF V_QTEFETMES > 0 THEN
                        V_QTTOTEFETMES :=(V_QTEFETMES * V_PERCORRECAO);
                    END IF;
                    BEGIN
                        SELECT IDCALCOMESTORC
                          INTO V_CALCOMESTORC
                          FROM FILIAL
                         WHERE CDFILIAL = P_CDFILIAL;

                    EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                            V_CALCOMESTORC := 'N';
                    END;

                    IF V_CALCOMESTORC = 'N' THEN
                        V_VRTOTEFETMES :=(V_VREFETMES +(V_VREFETMES * V_VRINDICE))* V_QTTOTEFETMES;
                    ELSE
                        BEGIN
                            SELECT(SUM(VRSERVFATU)/ COUNT(CDCLIENTE))AS VRSERVFATU
                              INTO V_VRSERVFATU
                              FROM SERVCLIE
                             WHERE(CDFILIAL     = P_CDFILIAL)AND
                                  (CDSERVFILI   = C_SERVICOS.CDSERVFILI);

                        EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                                V_VRSERVFATU := 1;
                        END;

                        V_DTANOORCAFIN   := LAST_DAY(V_DTANOORCAINI);
                        FOR C_SERVFILI IN(
                            SELECT SC.CDSERVFILI,
                                   NVL(SE.QTCLIESTISEG, 0)AS QTCOMESTISEG,
                                   NVL(SE.QTCLIESTITER, 0)AS QTCOMESTITER,
                                   NVL(SE.QTCLIESTIQUA, 0)AS QTCOMESTIQUA,
                                   NVL(SE.QTCLIESTIQUI, 0)AS QTCOMESTIQUI,
                                   NVL(SE.QTCLIESTISEX, 0)AS QTCOMESTISEX,
                                   NVL(SE.QTCLIESTISAB, 0)AS QTCOMESTISAB,
                                   NVL(SE.QTCLIESTIDOM, 0)AS QTCOMESTIDOM,
                                   NVL(SE.QTCLIESTIFER, 0)AS QTCOMESTIFER
                              FROM SERVICOROP   SC,
                                   SERVICO      SE
                             WHERE SE.CDFILIAL      = P_CDFILIAL AND
                                   SE.CDSERVFILI    = SC.CDSERVFILI AND
                                   (SC.CDFILIAL      = TIPOPARAM_DRO(P_CDFILIAL))AND
                                   (IDTPSERVICOROP   = 'E')AND
                                   (V_IDSERVFATROP <> 'S')AND
                                   (SC.CDSERVFILI    = C_SERVICOS.CDSERVFILI)
                            UNION ALL
                            SELECT SE.CDSERVFILI,
                                   NVL(SE.QTCLIESTISEG, 0)AS QTCOMESTISEG,
                                   NVL(SE.QTCLIESTITER, 0)AS QTCOMESTITER,
                                   NVL(SE.QTCLIESTIQUA, 0)AS QTCOMESTIQUA,
                                   NVL(SE.QTCLIESTIQUI, 0)AS QTCOMESTIQUI,
                                   NVL(SE.QTCLIESTISEX, 0)AS QTCOMESTISEX,
                                   NVL(SE.QTCLIESTISAB, 0)AS QTCOMESTISAB,
                                   NVL(SE.QTCLIESTIDOM, 0)AS QTCOMESTIDOM,
                                   NVL(SE.QTCLIESTIFER, 0)AS QTCOMESTIFER
                              FROM SERVICO SE
                             WHERE SE.CDFILIAL      = P_CDFILIAL AND
                                   V_IDSERVFATROP   = 'S' AND
                                   (SE.CDSERVFILI    = C_SERVICOS.CDSERVFILI)AND
                                   (SE.QTCOMESTISEG IS NOT NULL OR
                                    SE.QTCOMESTITER IS NOT NULL OR
                                    SE.QTCOMESTIQUA IS NOT NULL OR
                                    SE.QTCOMESTIQUI IS NOT NULL OR
                                    SE.QTCOMESTISEX IS NOT NULL OR
                                    SE.QTCOMESTISAB IS NOT NULL OR
                                    SE.QTCOMESTIDOM IS NOT NULL OR
                                    SE.QTCOMESTIFER IS NOT NULL)
                             ORDER BY CDSERVFILI
                        )LOOP WHILE(V_DTANOORCAINI <= V_DTANOORCAFIN)LOOP
                            BEGIN
                                SELECT COUNT(*)
                                  INTO V_COUNT
                                  FROM FERIFILI
                                 WHERE(CDFILIAL     = P_CDFILIAL)AND
                                      (DTFERIFILI   = V_DTANOORCAINI);

                            EXCEPTION
                                WHEN NO_DATA_FOUND THEN
                                    V_COUNT := 0;
                            END;

                            IF(V_COUNT = 0)OR(V_IDCONSDIAUTFEC = 'N')THEN
                                BEGIN
                                    SELECT DECODE(TO_CHAR(V_DTANOORCAINI, 'D'), '2', C_SERVFILI.QTCOMESTISEG, DECODE(TO_CHAR(V_DTANOORCAINI
                                    , 'D'), '3', C_SERVFILI.QTCOMESTITER, DECODE(TO_CHAR(V_DTANOORCAINI, 'D'), '4', C_SERVFILI.QTCOMESTIQUA
                                    , DECODE(TO_CHAR(V_DTANOORCAINI, 'D'), '5', C_SERVFILI.QTCOMESTIQUI, DECODE(TO_CHAR(V_DTANOORCAINI
                                    , 'D'), '6', C_SERVFILI.QTCOMESTISEX, DECODE(TO_CHAR(V_DTANOORCAINI, 'D'), '7', C_SERVFILI.QTCOMESTISAB
                                    , C_SERVFILI.QTCOMESTIDOM))))))
                                      INTO V_SUMEFET
                                      FROM DUAL;

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_SUMEFET := 0;
                                END;

                                V_SUMEFETMES := V_SUMEFETMES + V_SUMEFET;
                            END IF;

                            V_DTANOORCAINI := TO_DATE(V_DTANOORCAINI + 1);
                        END LOOP;
                        END LOOP;

                        IF NVL(V_PERCORRECAO, 0)= 0 THEN
                            V_PERCORRECAO := 1;
                        END IF;
                        IF V_SUMEFETMES > 0 THEN
                            V_SUMEFETMES :=(V_SUMEFETMES * V_PERCORRECAO);
                        END IF;

                        /*Se V_IDCALCFATUROP = CL,  significa que o total de comensais do mes deve ser aplicado a cada um dos
                        clientes que utilizam o servico, considerando o valor unitario individual de cadaum somando o resultado final */
                        IF V_IDCALCFATUROP = 'CL' THEN
                            V_SUMSERVCLI     := 0;
                            FOR C_SERVCLI IN(
                                SELECT CDCLIENTE,
                                       CDFILIAL,
                                       CDSERVFILI,
                                       DTSERVFATU,
                                       VRSERVFATU
                                  FROM SERVCLIE
                                 WHERE(CDFILIAL     = P_CDFILIAL)AND
                                      (CDSERVFILI   = C_SERVICOS.CDSERVFILI)
                            )LOOP V_SUMSERVCLI := V_SUMSERVCLI +(V_SUMEFETMES * C_SERVCLI.VRSERVFATU);
                            END LOOP;

                            V_VRTOTEFETMES   := V_SUMSERVCLI;
                        ELSE
                            V_VRTOTEFETMES := V_SUMEFETMES * V_VRSERVFATU;
                        END IF;

                        V_SUMEFETMES     := 0;
                    END IF;

                    V_DTANOORCAINI   := V_DTANOORCA;
                    V_VRREAJUSTE     :=(V_VREFETMES * V_VRINDICE);
                    SELECT LPAD((TO_NUMBER(NVL(MAX(NRSEQITEMORCA), '0'))), 10, 0)
                      INTO V_NRITEMORCA
                      FROM ITEMORCAOP
                     WHERE CDFILIAL       = P_CDFILIAL AND
                           NRANOORCAOP    = P_NRANOORCAOP AND
                           CDVERSORCAOP   = V_CDVERSAOORCA AND
                           IDTIPOORCAOP   = 'F' AND
                           CDSERVFILI     = C_SERVICOS.CDSERVFILI;

                    DELETE FROM ITEMORCAOP
                     WHERE CDFILIAL         = P_CDFILIAL AND
                           IDTIPOORCAOP     = 'F' AND
                           NRANOORCAOP      = P_NRANOORCAOP AND
                           CDVERSORCAOP     = V_CDVERSAOORCA AND
                           IDITEMCALCORCA   = 'N';

                    UPDATE ITEMORCAOP
                       SET VRORCAMENTOJAN = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 1, 0, NVL(VRORCAMENTOJAN, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                       , 'MM'), 1, V_VRTOTEFETMES, 0),
                           VRORCAMENTOFEV = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 2, 0, NVL(VRORCAMENTOFEV, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 2, V_VRTOTEFETMES, 0),
                           VRORCAMENTOMAR = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 3, 0, NVL(VRORCAMENTOMAR, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 3, V_VRTOTEFETMES, 0),
                           VRORCAMENTOABR = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 4, 0, NVL(VRORCAMENTOABR, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 4, V_VRTOTEFETMES, 0),
                           VRORCAMENTOMAI = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 5, 0, NVL(VRORCAMENTOMAI, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 5, V_VRTOTEFETMES, 0),
                           VRORCAMENTOJUN = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 6, 0, NVL(VRORCAMENTOJUN, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 6, V_VRTOTEFETMES, 0),
                           VRORCAMENTOJUL = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 7, 0, NVL(VRORCAMENTOJUL, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 7, V_VRTOTEFETMES, 0),
                           VRORCAMENTOAGO = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 8, 0, NVL(VRORCAMENTOAGO, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 8, V_VRTOTEFETMES, 0),
                           VRORCAMENTOSET = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 9, 0, NVL(VRORCAMENTOSET, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 9, V_VRTOTEFETMES, 0),
                           VRORCAMENTOOUT = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 10, 0, NVL(VRORCAMENTOOUT, 0))+ DECODE(TO_CHAR(
                           V_AUXDTANOORCA, 'MM'), 10, V_VRTOTEFETMES, 0),
                           VRORCAMENTONOV = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 11, 0, NVL(VRORCAMENTONOV, 0))+ DECODE(TO_CHAR(
                           V_AUXDTANOORCA, 'MM'), 11, V_VRTOTEFETMES, 0),
                           VRORCAMENTODEZ = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 12, 0, NVL(VRORCAMENTODEZ, 0))+ DECODE(TO_CHAR(
                           V_AUXDTANOORCA, 'MM'), 12, V_VRTOTEFETMES, 0),
                           QTORCAMENTOJAN = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 1, 0, NVL(QTORCAMENTOJAN, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 1, V_QTTOTEFETMES, 0),
                           QTORCAMENTOFEV = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 2, 0, NVL(QTORCAMENTOFEV, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 2, V_QTTOTEFETMES, 0),
                           QTORCAMENTOMAR = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 3, 0, NVL(QTORCAMENTOMAR, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 3, V_QTTOTEFETMES, 0),
                           QTORCAMENTOABR = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 4, 0, NVL(QTORCAMENTOABR, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 4, V_QTTOTEFETMES, 0),
                           QTORCAMENTOMAI = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 5, 0, NVL(QTORCAMENTOMAI, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 5, V_QTTOTEFETMES, 0),
                           QTORCAMENTOJUN = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 6, 0, NVL(QTORCAMENTOJUN, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 6, V_QTTOTEFETMES, 0),
                           QTORCAMENTOJUL = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 7, 0, NVL(QTORCAMENTOJUL, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 7, V_QTTOTEFETMES, 0),
                           QTORCAMENTOAGO = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 8, 0, NVL(QTORCAMENTOAGO, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 8, V_QTTOTEFETMES, 0),
                           QTORCAMENTOSET = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 9, 0, NVL(QTORCAMENTOSET, 0))+ DECODE(TO_CHAR(V_AUXDTANOORCA
                           , 'MM'), 9, V_QTTOTEFETMES, 0),
                           QTORCAMENTOOUT = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 10, 0, NVL(QTORCAMENTOOUT, 0))+ DECODE(TO_CHAR(
                           V_AUXDTANOORCA, 'MM'), 10, V_QTTOTEFETMES, 0),
                           QTORCAMENTONOV = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 11, 0, NVL(QTORCAMENTONOV, 0))+ DECODE(TO_CHAR(
                           V_AUXDTANOORCA, 'MM'), 11, V_QTTOTEFETMES, 0),
                           QTORCAMENTODEZ = DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 12, 0, NVL(QTORCAMENTODEZ, 0))+ DECODE(TO_CHAR(
                           V_AUXDTANOORCA, 'MM'), 12, V_QTTOTEFETMES, 0),
                           VRREAJORCJAN = NVL(V_VRREAJUSTE, 0),
                           VRREAJORCFEV = NVL(V_VRREAJUSTE, 0),
                           VRREAJORCMAR = NVL(V_VRREAJUSTE, 0),
                           VRREAJORCABR = NVL(V_VRREAJUSTE, 0),
                           VRREAJORCMAI = NVL(V_VRREAJUSTE, 0),
                           VRREAJORCJUN = NVL(V_VRREAJUSTE, 0),
                           VRREAJORCJUL = NVL(V_VRREAJUSTE, 0),
                           VRREAJORCAGO = NVL(V_VRREAJUSTE, 0),
                           VRREAJORCSET = NVL(V_VRREAJUSTE, 0),
                           VRREAJORCOUT = NVL(V_VRREAJUSTE, 0),
                           VRREAJORCNOV = NVL(V_VRREAJUSTE, 0),
                           VRREAJORCDEZ = NVL(V_VRREAJUSTE, 0),
                           IDITEMCALCORCA = 'S'
                     WHERE NRANOORCAOP     = P_NRANOORCAOP AND
                           CDVERSORCAOP    = V_CDVERSAOORCA AND
                           CDFILIAL        = P_CDFILIAL AND
                           IDTIPOORCAOP    = 'F' AND
                           CDSERVFILI      = C_SERVICOS.CDSERVFILI AND
                           CDSERVFILI IS NOT NULL AND
                           NRSEQITEMORCA   = V_NRITEMORCA;

                    IF(SQL%ROWCOUNT = 0)THEN
                        V_IDITEMCALCORCA := 'S';
                        BEGIN
                            SELECT IDITEMCALCORCA
                              INTO V_IDITEMCALCORCA
                              FROM ITEMORCAOP
                             WHERE NRANOORCAOP      = P_NRANOORCAOP AND
                                   CDVERSORCAOP     = V_CDVERSAOORCA AND
                                   CDFILIAL         = P_CDFILIAL AND
                                   IDTIPOORCAOP     = 'F' AND
                                   IDITEMCALCORCA   = 'N';

                        EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                                V_IDITEMCALCORCA := 'S';
                        END;

                        IF(V_IDITEMCALCORCA = 'S')THEN
                            SELECT LPAD((TO_NUMBER(NVL(MAX(NRSEQITEMORCA), '0'))+ 1), 10, 0)
                              INTO V_NRITEMORCA
                              FROM ITEMORCAOP
                             WHERE CDFILIAL       = P_CDFILIAL AND
                                   NRANOORCAOP    = P_NRANOORCAOP AND
                                   CDVERSORCAOP   = V_CDVERSAOORCA;

                            INSERT INTO ITEMORCAOP(
                                NRANOORCAOP,
                                CDVERSORCAOP,
                                CDFILIAL,
                                NRSEQITEMORCA,
                                IDENTRSAIDOP,
                                CDTIPOOPER,
                                CDSERVFILI,
                                CDDESPADMFAT,
                                CDDESPPREV,
                                DTMESBASEITEM,
                                DTORCAMENTOITEM,
                                IDTIPOORCAOP,
                                IDITEMORCAMENTO,
                                VRORCAMENTOJAN,
                                VRORCAMENTOFEV,
                                VRORCAMENTOMAR,
                                VRORCAMENTOABR,
                                VRORCAMENTOMAI,
                                VRORCAMENTOJUN,
                                VRORCAMENTOJUL,
                                VRORCAMENTOAGO,
                                VRORCAMENTOSET,
                                VRORCAMENTOOUT,
                                VRORCAMENTONOV,
                                VRORCAMENTODEZ,
                                QTORCAMENTOJAN,
                                QTORCAMENTOFEV,
                                QTORCAMENTOMAR,
                                QTORCAMENTOABR,
                                QTORCAMENTOMAI,
                                QTORCAMENTOJUN,
                                QTORCAMENTOJUL,
                                QTORCAMENTOAGO,
                                QTORCAMENTOSET,
                                QTORCAMENTOOUT,
                                QTORCAMENTONOV,
                                QTORCAMENTODEZ,
                                VRREAJORCJAN,
                                VRREAJORCFEV,
                                VRREAJORCMAR,
                                VRREAJORCABR,
                                VRREAJORCMAI,
                                VRREAJORCJUN,
                                VRREAJORCJUL,
                                VRREAJORCAGO,
                                VRREAJORCSET,
                                VRREAJORCOUT,
                                VRREAJORCNOV,
                                VRREAJORCDEZ,
                                IDITEMCALCORCA
                            )
                                (SELECT P_NRANOORCAOP,
                                        V_CDVERSAOORCA,
                                        P_CDFILIAL,
                                        V_NRITEMORCA,
                                        '',
                                        '',
                                        C_SERVICOS.CDSERVFILI,
                                        '',
                                        '',
                                        V_DTMESBASE,
                                        V_DATA,
                                        'F',
                                        'P',
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 1, V_VRTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 2, V_VRTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 3, V_VRTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 4, V_VRTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 5, V_VRTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 6, V_VRTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 7, V_VRTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 8, V_VRTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 9, V_VRTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 10, V_VRTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 11, V_VRTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 12, V_VRTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 1, V_QTTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 2, V_QTTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 3, V_QTTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 4, V_QTTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 5, V_QTTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 6, V_QTTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 7, V_QTTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 8, V_QTTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 9, V_QTTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 10, V_QTTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 11, V_QTTOTEFETMES, 0),
                                        DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 12, V_QTTOTEFETMES, 0),
                                        NVL(V_VRREAJUSTE, 0),
                                        NVL(V_VRREAJUSTE, 0),
                                        NVL(V_VRREAJUSTE, 0),
                                        NVL(V_VRREAJUSTE, 0),
                                        NVL(V_VRREAJUSTE, 0),
                                        NVL(V_VRREAJUSTE, 0),
                                        NVL(V_VRREAJUSTE, 0),
                                        NVL(V_VRREAJUSTE, 0),
                                        NVL(V_VRREAJUSTE, 0),
                                        NVL(V_VRREAJUSTE, 0),
                                        NVL(V_VRREAJUSTE, 0),
                                        NVL(V_VRREAJUSTE, 0),
                                        'S'
                                  FROM DUAL
                                MINUS
                                SELECT P_NRANOORCAOP,
                                       V_CDVERSAOORCA,
                                       P_CDFILIAL,
                                       V_NRITEMORCA,
                                       '',
                                       '',
                                       C_SERVICOS.CDSERVFILI,
                                       '',
                                       '',
                                       V_DTMESBASE,
                                       V_DATA,
                                       'F',
                                       'P',
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 1, V_VRTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 2, V_VRTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 3, V_VRTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 4, V_VRTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 5, V_VRTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 6, V_VRTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 7, V_VRTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 8, V_VRTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 9, V_VRTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 10, V_VRTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 11, V_VRTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 12, V_VRTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 1, V_QTTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 2, V_QTTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 3, V_QTTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 4, V_QTTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 5, V_QTTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 6, V_QTTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 7, V_QTTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 8, V_QTTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 9, V_QTTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 10, V_QTTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 11, V_QTTOTEFETMES, 0),
                                       DECODE(TO_CHAR(V_AUXDTANOORCA, 'MM'), 12, V_QTTOTEFETMES, 0),
                                       NVL(V_VRREAJUSTE, 0),
                                       NVL(V_VRREAJUSTE, 0),
                                       NVL(V_VRREAJUSTE, 0),
                                       NVL(V_VRREAJUSTE, 0),
                                       NVL(V_VRREAJUSTE, 0),
                                       NVL(V_VRREAJUSTE, 0),
                                       NVL(V_VRREAJUSTE, 0),
                                       NVL(V_VRREAJUSTE, 0),
                                       NVL(V_VRREAJUSTE, 0),
                                       NVL(V_VRREAJUSTE, 0),
                                       NVL(V_VRREAJUSTE, 0),
                                       NVL(V_VRREAJUSTE, 0),
                                       'S'
                                  FROM ITEMORCAOP
                                 WHERE NRANOORCAOP    = P_NRANOORCAOP AND
                                       CDVERSORCAOP   = V_CDVERSAOORCA AND
                                       CDFILIAL       = P_CDFILIAL AND
                                       IDTIPOORCAOP   = 'F' AND
                                       CDSERVFILI     = C_SERVICOS.CDSERVFILI
                                );

                        END IF;

                    END IF;

                    V_NRMES          := V_NRMES + 1;
                END LOOP;

                IMPOSTOORCA_RELGESDIRETA(P_CDFILIAL, P_NRANOORCAOP, V_CDVERSAOORCA, V_NRITEMORCA, C_SERVICOS.CDSERVFILI);
                SELECT SUM(VRORCAMENTOJAN + NVL(VRRELITORCOPJAN, 0)),
                       SUM(VRORCAMENTOFEV + NVL(VRRELITORCOPFEV, 0)),
                       SUM(VRORCAMENTOMAR + NVL(VRRELITORCOPMAR, 0)),
                       SUM(VRORCAMENTOABR + NVL(VRRELITORCOPABR, 0)),
                       SUM(VRORCAMENTOMAI + NVL(VRRELITORCOPMAI, 0)),
                       SUM(VRORCAMENTOJUN + NVL(VRRELITORCOPJUN, 0)),
                       SUM(VRORCAMENTOJUL + NVL(VRRELITORCOPJUL, 0)),
                       SUM(VRORCAMENTOAGO + NVL(VRRELITORCOPAGO, 0)),
                       SUM(VRORCAMENTOSET + NVL(VRRELITORCOPSET, 0)),
                       SUM(VRORCAMENTOOUT + NVL(VRRELITORCOPOUT, 0)),
                       SUM(VRORCAMENTONOV + NVL(VRRELITORCOPNOV, 0)),
                       SUM(VRORCAMENTODEZ + NVL(VRRELITORCOPDEZ, 0))
                  INTO
                    V_VRORCAMENTOJAN,
                    V_VRORCAMENTOFEV,
                    V_VRORCAMENTOMAR,
                    V_VRORCAMENTOABR,
                    V_VRORCAMENTOMAI,
                    V_VRORCAMENTOJUN,
                    V_VRORCAMENTOJUL,
                    V_VRORCAMENTOAGO,
                    V_VRORCAMENTOSET,
                    V_VRORCAMENTOOUT,
                    V_VRORCAMENTONOV,
                    V_VRORCAMENTODEZ
                  FROM ITEMORCAOP I,
                       (
                           SELECT SUM(DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* VRRELITORCOPJAN)VRRELITORCOPJAN
                           ,
                                  SUM(DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* VRRELITORCOPFEV)VRRELITORCOPFEV
                                  ,
                                  SUM(DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* VRRELITORCOPMAR)VRRELITORCOPMAR
                                  ,
                                  SUM(DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* VRRELITORCOPABR)VRRELITORCOPABR
                                  ,
                                  SUM(DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* VRRELITORCOPMAI)VRRELITORCOPMAI
                                  ,
                                  SUM(DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* VRRELITORCOPJUN)VRRELITORCOPJUN
                                  ,
                                  SUM(DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* VRRELITORCOPJUL)VRRELITORCOPJUL
                                  ,
                                  SUM(DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* VRRELITORCOPAGO)VRRELITORCOPAGO
                                  ,
                                  SUM(DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* VRRELITORCOPSET)VRRELITORCOPSET
                                  ,
                                  SUM(DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* VRRELITORCOPOUT)VRRELITORCOPOUT
                                  ,
                                  SUM(DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* VRRELITORCOPNOV)VRRELITORCOPNOV
                                  ,
                                  SUM(DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* VRRELITORCOPDEZ)VRRELITORCOPDEZ
                                  ,
                                  R.NRSEQITEMORCA,
                                  R.CDFILIAL
                             FROM ITRELACITORCAOP   R,
                                  IMPOSTOROP        IR
                            WHERE R.NRANOORCAOP    = P_NRANOORCAOP AND
                                  R.CDVERSORCAOP   = V_CDVERSAOORCA AND
                                  R.CDFILIAL       = P_CDFILIAL AND
                                  IR.CDFILIAL      = TIPOPARAM_DRO(P_CDFILIAL)AND
                                  IR.CDIMPOSTO     = CDITRELITORCAOP AND
                                  IDTPIMPOSTOROP   = 'FE'
                            GROUP BY R.NRSEQITEMORCA,
                                     R.CDFILIAL
                       )R
                 WHERE I.NRANOORCAOP     = P_NRANOORCAOP AND
                       I.CDVERSORCAOP    = V_CDVERSAOORCA AND
                       I.CDFILIAL        = P_CDFILIAL AND
                       I.IDTIPOORCAOP    = 'F' AND
                       I.CDSERVFILI      = C_SERVICOS.CDSERVFILI AND
                       I.CDFILIAL        = R.CDFILIAL(+)AND
                       I.NRSEQITEMORCA   = R.NRSEQITEMORCA(+);

                SELECT LPAD((TO_NUMBER(NVL(MAX(NRSEQITEMORCA), '0'))+ 1), 10, 0)
                  INTO V_NRITEMORCA
                  FROM ITEMORCAOP
                 WHERE CDFILIAL       = P_CDFILIAL AND
                       NRANOORCAOP    = P_NRANOORCAOP AND
                       CDVERSORCAOP   = V_CDVERSAOORCA;

                IF V_NRITEMORCA IS NULL THEN
                    V_NRITEMORCA := '0001';
                END IF;
                IF V_PRIMFATLIQ = 'S' THEN
                    DELETE ITEMORCAOP
                     WHERE CDFILIAL       = P_CDFILIAL AND
                           NRANOORCAOP    = P_NRANOORCAOP AND
                           CDVERSORCAOP   = V_CDVERSAOORCA AND
                           IDTIPOORCAOP   = 'L';

                    V_PRIMFATLIQ := 'N';
                END IF;

                INSERT INTO ITEMORCAOP(
                    NRANOORCAOP,
                    CDVERSORCAOP,
                    CDFILIAL,
                    NRSEQITEMORCA,
                    IDENTRSAIDOP,
                    CDTIPOOPER,
                    CDSERVFILI,
                    CDDESPADMFAT,
                    CDDESPPREV,
                    DTMESBASEITEM,
                    DTORCAMENTOITEM,
                    IDTIPOORCAOP,
                    IDITEMORCAMENTO,
                    VRORCAMENTOJAN,
                    VRORCAMENTOFEV,
                    VRORCAMENTOMAR,
                    VRORCAMENTOABR,
                    VRORCAMENTOMAI,
                    VRORCAMENTOJUN,
                    VRORCAMENTOJUL,
                    VRORCAMENTOAGO,
                    VRORCAMENTOSET,
                    VRORCAMENTOOUT,
                    VRORCAMENTONOV,
                    VRORCAMENTODEZ,
                    QTORCAMENTOJAN,
                    QTORCAMENTOFEV,
                    QTORCAMENTOMAR,
                    QTORCAMENTOABR,
                    QTORCAMENTOMAI,
                    QTORCAMENTOJUN,
                    QTORCAMENTOJUL,
                    QTORCAMENTOAGO,
                    QTORCAMENTOSET,
                    QTORCAMENTOOUT,
                    QTORCAMENTONOV,
                    QTORCAMENTODEZ
                )
                    (SELECT P_NRANOORCAOP,
                            V_CDVERSAOORCA,
                            P_CDFILIAL,
                            V_NRITEMORCA,
                            '',
                            '',
                            C_SERVICOS.CDSERVFILI,
                            '',
                            '',
                            V_DTMESBASE,
                            V_DATA,
                            'L',
                            'P',
                            V_VRORCAMENTOJAN,
                            V_VRORCAMENTOFEV,
                            V_VRORCAMENTOMAR,
                            V_VRORCAMENTOABR,
                            V_VRORCAMENTOMAI,
                            V_VRORCAMENTOJUN,
                            V_VRORCAMENTOJUL,
                            V_VRORCAMENTOAGO,
                            V_VRORCAMENTOSET,
                            V_VRORCAMENTOOUT,
                            V_VRORCAMENTONOV,
                            V_VRORCAMENTODEZ,
                            0,
                            0,
                            0,
                            0,
                            0,
                            0,
                            0,
                            0,
                            0,
                            0,
                            0,
                            0
                      FROM DUAL
                    MINUS
                    SELECT P_NRANOORCAOP,
                           V_CDVERSAOORCA,
                           P_CDFILIAL,
                           V_NRITEMORCA,
                           '',
                           '',
                           C_SERVICOS.CDSERVFILI,
                           '',
                           '',
                           V_DTMESBASE,
                           V_DATA,
                           'L',
                           'P',
                           V_VRORCAMENTOJAN,
                           V_VRORCAMENTOFEV,
                           V_VRORCAMENTOMAR,
                           V_VRORCAMENTOABR,
                           V_VRORCAMENTOMAI,
                           V_VRORCAMENTOJUN,
                           V_VRORCAMENTOJUL,
                           V_VRORCAMENTOAGO,
                           V_VRORCAMENTOSET,
                           V_VRORCAMENTOOUT,
                           V_VRORCAMENTONOV,
                           V_VRORCAMENTODEZ,
                           0,
                           0,
                           0,
                           0,
                           0,
                           0,
                           0,
                           0,
                           0,
                           0,
                           0,
                           0
                      FROM ITEMORCAOP
                     WHERE NRANOORCAOP    = P_NRANOORCAOP AND
                           CDVERSORCAOP   = V_CDVERSAOORCA AND
                           CDFILIAL       = P_CDFILIAL AND
                           IDTIPOORCAOP   = 'L' AND
                           CDSERVFILI     = C_SERVICOS.CDSERVFILI
                    );

                IF NOT(SQL%ROWCOUNT = 0)THEN
                    SELECT LPAD(TO_NUMBER(MAX(NRSEQAUDCALCORC))+ 1, 4, '0')AS NRSEQLOGAUD
                      INTO V_NRAUDCALC
                      FROM AUDCALCORCA
                     WHERE CDFILIAL       = P_CDFILIAL AND
                           NRANOORCAOP    = P_NRANOORCAOP AND
                           CDVERSORCAOP   = V_CDVERSAOORCA;

                    IF V_NRAUDCALC IS NULL THEN
                        V_NRAUDCALC := '0001';
                    END IF;
                    INSERT INTO AUDCALCORCA(
                        NRANOORCAOP,
                        CDVERSORCAOP,
                        CDFILIAL,
                        NRSEQITEMORCA,
                        NRSEQAUDCALCORC,
                        DTHRAUDAPROORC,
                        DTMESBASEAUDIT,
                        IDTIPOCALCORCA,
                        CDSERVFILI,
                        VRORCAUDCALJAN,
                        VRORCAUDCALFEV,
                        VRORCAUDCALMAR,
                        VRORCAUDCALABR,
                        VRORCAUDCALMAI,
                        VRORCAUDCALJUN,
                        VRORCAUDCALJUL,
                        VRORCAUDCALAGO,
                        VRORCAUDCALSET,
                        VRORCAUDCALOUT,
                        VRORCAUDCALNOV,
                        VRORCAUDCALDEZ
                    )VALUES(
                        P_NRANOORCAOP,
                        V_CDVERSAOORCA,
                        P_CDFILIAL,
                        V_NRITEMORCA,
                        V_NRAUDCALC,
                        V_DATA,
                        V_DTMESBASE,
                        'M',
                        C_SERVICOS.CDSERVFILI,
                        V_VRORCAMENTOJAN,
                        V_VRORCAMENTOFEV,
                        V_VRORCAMENTOMAR,
                        V_VRORCAMENTOABR,
                        V_VRORCAMENTOMAI,
                        V_VRORCAMENTOJUN,
                        V_VRORCAMENTOJUL,
                        V_VRORCAMENTOAGO,
                        V_VRORCAMENTOSET,
                        V_VRORCAMENTOOUT,
                        V_VRORCAMENTONOV,
                        V_VRORCAMENTODEZ
                    );

                END IF;

            END LOOP;

            DELETE FROM TRAB_APURAC_GD
             WHERE(CDFILIALAPGD   = P_CDFILIAL)AND
                  (NRAPURACGD     = V_NRAPURREL)AND
                  (DTAPURACFATGD BETWEEN V_INIMESCONS AND LAST_DAY(V_INIMESCONS));

        END IF;

    END IF;

    IF P_IDITEMORCA = 'C' THEN
        SELECT NVL(MAX(DTMESBASEITEM), SYSDATE)
          INTO V_DTMESBASE
          FROM ITEMORCAOP
         WHERE CDFILIAL       = P_CDFILIAL AND
               IDTIPOORCAOP   = 'C' AND
               NRANOORCAOP    = P_NRANOORCAOP AND
               CDVERSORCAOP   = V_CDVERSAOORCA;

        IF P_DATACOMP = 'S' THEN
            V_DATAINI    := TO_DATE('01/' || V_NRMESINIVERORC || '/' || P_NRANOORCAOP, 'DD/MM/YYYY');

            V_DATAFIN    := LAST_DAY(ADD_MONTHS(TO_DATE('01/' || V_NRMESINIVERORC || '/' || P_NRANOORCAOP, 'DD/MM/YYYY'), 11));

            V_CONTAMES   := 12;
        ELSE
            V_DATAINI       := P_DATAINIC;
            V_DATAFIN       := P_DATAFIN;
            SELECT TO_CHAR(P_DATAINIC, 'MM'),
                   TO_CHAR(P_DATAFIN, 'MM'),
                   TO_CHAR(P_DATAINIC, 'YYYY'),
                   TO_CHAR(P_DATAFIN, 'YYYY')
              INTO
                V_DTINICPER,
                V_DTFINALPER,
                V_DTINICPERANO,
                V_DTFINALPERANO
              FROM DUAL;

            V_MESREFERENC   := V_DTINICPER;
            V_CONTAMES      := 0;
            DATAINIVL       := '01/' || V_DTINICPER || '/' || V_DTINICPERANO;
            DATAFINVL       := '01/' || V_DTFINALPER || '/' || V_DTFINALPERANO;
            WHILE(DATAINIVL <= DATAFINVL)LOOP
                V_CONTAMES   := V_CONTAMES + 1;
                DATAINIVL    := ADD_MONTHS(DATAINIVL, 1);
            END LOOP;

        END IF;

        BEGIN
            SELECT IDCALCOMESTORC
              INTO V_CALCOMESTORC
              FROM FILIAL
             WHERE CDFILIAL = P_CDFILIAL;

        EXCEPTION
            WHEN NO_DATA_FOUND THEN
                V_CALCOMESTORC := 'N';
        END;

        IF V_CALCOMESTORC = 'N' THEN
            V_SERVICOS            := '_';
            FOR C_SERVRETI IN(
                SELECT S.CDSERVFILI
                  FROM SERVICOROP   S,
                       SERVICO      SE
                 WHERE(S.CDFILIAL       = TIPOPARAM_DRO(P_CDFILIAL))AND
                      (IDTPSERVICOROP   = 'R')AND
                      (V_IDSERVRETROP <> 'S')AND
                      S.CDSERVFILI     = SE.CDSERVFILI AND
                      SE.CDFILIAL      = P_CDFILIAL
                UNION ALL
                SELECT CDSERVFILI
                  FROM SERVICO S
                 WHERE(S.CDFILIAL       = P_CDFILIAL)AND
                      (V_IDSERVRETROP   = 'S')
            )LOOP V_SERVICOS := V_SERVICOS || C_SERVRETI.CDSERVFILI || '_';
            END LOOP;

            NOVO_CODIGO('CONSPLAN' || P_CDFILIAL, V_NRCONSPLANMESBASE, 1);
            V_NRCONSPLANMESBASE   := SUBSTR(V_NRCONSPLANMESBASE, 11, 10);

        /*Se o periodo calculado for um ano completo, realiza o calculo do consumo para o ANO BASE informado*/
            IF P_DATACOMP = 'S' THEN
                V_DTINIANOBASE   := TO_DATE('01/01/' || TO_CHAR(V_DTMESBASE, 'YYYY'), 'DD/MM/YYYY');
                V_DTFIMANOBASE   := TO_DATE('31/12/' || TO_CHAR(V_DTMESBASE, 'YYYY'), 'DD/MM/YYYY');
                CONSUMO_PLANEJADO_PLA(P_CDFILIAL, V_SERVICOS, V_DTINIANOBASE, V_DTFIMANOBASE, 'T',
                                      'CFP', 'N', 0, 'N', V_NRCONSPLANMESBASE,
                                      '0', 'zzzzzzzzzz', P_CDOPERADOR);

            ELSE
                V_DTINIANOBASE   := TO_DATE(TO_CHAR(V_DATAINI, 'DD/MM/')|| TO_CHAR(V_DTMESBASE, 'YYYY'));

                V_DTFIMANOBASE   := LAST_DAY(V_DTINIANOBASE);
                CONSUMO_PLANEJADO_PLA(P_CDFILIAL, V_SERVICOS, V_DTINIANOBASE, V_DTFIMANOBASE, 'T',
                                      'CFP', 'N', 0, 'N', V_NRCONSPLANMESBASE,
                                      '0', 'zzzzzzzzzz', P_CDOPERADOR);

            END IF;

            SELECT MAX(NRMESREAJCONTR)
              INTO V_NRMESREAJCONTR
              FROM FILIAL
             WHERE CDFILIAL = P_CDFILIAL;

            V_DTANOORCA           := ADD_MONTHS(WDIA || '/' || V_NRMESINIVERORC || '/' || P_NRANOORCAOP, V_NRMES);

            V_VRCONSPLAN          := 0;
            V_QTCONSPLAN          := 0;
            FOR C_CONSUMO IN(
                SELECT SERV.*
                  FROM(
                    SELECT S.CDSERVFILI
                      FROM SERVICOROP   S,
                           SERVICO      SE
                     WHERE(S.CDFILIAL       = TIPOPARAM_DRO(P_CDFILIAL))AND
                          (IDTPSERVICOROP   = 'R')AND
                          (V_IDSERVRETROP <> 'S')AND
                          S.CDSERVFILI     = SE.CDSERVFILI AND
                          SE.CDFILIAL      = P_CDFILIAL
                    UNION ALL
                    SELECT CDSERVFILI
                      FROM SERVICO S
                     WHERE(S.CDFILIAL       = P_CDFILIAL)AND
                          (V_IDSERVRETROP   = 'S')
                )SERV,
                      (
                          SELECT CDSERVFILI
                            FROM ITEMORCAOP
                           WHERE CDFILIAL       = P_CDFILIAL AND
                                 NRANOORCAOP    = P_NRANOORCAOP AND
                                 CDVERSORCAOP   = V_CDVERSAOORCA AND
                                 IDTIPOORCAOP   = 'C'
                      )ORC
                 WHERE SERV.CDSERVFILI = ORC.CDSERVFILI
                 ORDER BY SERV.CDSERVFILI
            )LOOP
                BEGIN
                    SELECT SUM(QTCOMESTICAR)
                      INTO V_QTMESBASE
                      FROM CARDADIA
                     WHERE CDFILIAL     = P_CDFILIAL AND
                           CDSERVFILI   = C_CONSUMO.CDSERVFILI AND
                           DTCARDAPIO BETWEEN V_DTMESBASE AND LAST_DAY(V_DTMESBASE);

                EXCEPTION
                    WHEN NO_DATA_FOUND THEN
                        V_QTMESBASE := 0;
                END;

          /*VALOR DO CARDAPIO DO MES BASE, SERA USADO COMO DEFAULT SEMPRE QUE UM MES ESPECIFICO ESTIVER ZERADO */

                V_VRMESBASE   := NVL(CUSTO_SERVICO_CARDAPIO(P_CDFILIAL, C_CONSUMO.CDSERVFILI, V_NRCONSPLANMESBASE, V_DTMESBASE, LAST_DAY
                (V_DTMESBASE),
                       V_CDTIPOCUSTO, 'PLANEJADO'), 0);

                V_NRMES       := 0;
                BEGIN
                    WHILE(V_NRMES < V_CONTAMES)LOOP
                        IF P_DATACOMP = 'S' THEN
                            V_DTANOORCA := ADD_MONTHS(WDIA || '/' || V_NRMESINIVERORC || '/' || P_NRANOORCAOP, V_NRMES);
                        ELSE
                            V_DTANOORCA := ADD_MONTHS(WDIA || '/' || TO_CHAR(P_DATAINIC, 'MM')|| '/' || P_NRANOORCAOP, V_NRMES);
                        END IF;
              
              
                /*SELECIONA O VALOR DO INDICE PARA CORRECAO DO CONSUMO(VALOR)*/

                        IF TO_CHAR(V_DTANOORCA, 'MM')>= V_NRMESREAJCONTR THEN
                            BEGIN
                                SELECT NVL(MAX(VRINDICE / 100), 0)+ 1
                                  INTO V_VRINDICE
                                  FROM MOVINDICE
                                 WHERE CDTPINDICE   = V_CDTOINDICECON AND
                                       IDPERVALOR   = V_IDPERVALORCON AND
                                       CDINDICE     = V_CDINDICECON AND
                                       DTREFIND     =(
                                           SELECT MAX(DTREFIND)
                                             FROM MOVINDICE
                                            WHERE CDTPINDICE   = V_CDTOINDICECON AND
                                                  IDPERVALOR   = V_IDPERVALORCON AND
                                                  CDINDICE     = V_CDINDICECON AND
                                                  DTREFIND <= '01/' || TO_CHAR(TO_DATE(V_DTANOORCA), 'MM')|| '/' || P_NRANOORCAOP
                                       );

                            EXCEPTION
                                WHEN NO_DATA_FOUND THEN
                                    V_VRINDICE := 1;
                            END;

                        ELSE
                            V_VRINDICE := 1;
                        END IF;
              

                      /*VERIFICA QUANTIDADE DE CARDAPIO NO MES DO LOOP, SE NAO HOUVER, PEGA O VALOR DO MES BASE*/

                        SELECT NVL(SUM(QTCOMESTICAR), V_QTMESBASE)
                          INTO V_QTCONSPLAN
                          FROM CARDADIA
                         WHERE CDFILIAL     = P_CDFILIAL AND
                               CDSERVFILI   = C_CONSUMO.CDSERVFILI AND
                               DTCARDAPIO BETWEEN ADD_MONTHS(V_DTINIANOBASE, V_NRMES)AND ADD_MONTHS(LAST_DAY(V_DTINIANOBASE), V_NRMES
                               );

                        V_VRCONSPLAN   := CUSTO_SERVICO_CARDAPIO(P_CDFILIAL, C_CONSUMO.CDSERVFILI, V_NRCONSPLANMESBASE, ADD_MONTHS(
                        V_DTINIANOBASE, V_NRMES), ADD_MONTHS(LAST_DAY(V_DTINIANOBASE), V_NRMES), V_CDTIPOCUSTO, 'PLANEJADO');

                        V_VRCONSPLAN   := V_VRCONSPLAN *(V_VRINDICE);
                        IF(V_VRCONSPLAN = 0)THEN
                            V_VRCONSPLAN := V_VRMESBASE;
                        END IF;
                        UPDATE ITEMORCAOP
                           SET VRORCAMENTOJAN = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '01', V_VRCONSPLAN, NVL(VRORCAMENTOJAN
                           , 0)),
                               VRORCAMENTOFEV = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '02', V_VRCONSPLAN, NVL(VRORCAMENTOFEV
                               , 0)),
                               VRORCAMENTOMAR = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '03', V_VRCONSPLAN, NVL(VRORCAMENTOMAR
                               , 0)),
                               VRORCAMENTOABR = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '04', V_VRCONSPLAN, NVL(VRORCAMENTOABR
                               , 0)),
                               VRORCAMENTOMAI = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '05', V_VRCONSPLAN, NVL(VRORCAMENTOMAI
                               , 0)),
                               VRORCAMENTOJUN = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '06', V_VRCONSPLAN, NVL(VRORCAMENTOJUN
                               , 0)),
                               VRORCAMENTOJUL = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '07', V_VRCONSPLAN, NVL(VRORCAMENTOJUL
                               , 0)),
                               VRORCAMENTOAGO = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '08', V_VRCONSPLAN, NVL(VRORCAMENTOAGO
                               , 0)),
                               VRORCAMENTOSET = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '09', V_VRCONSPLAN, NVL(VRORCAMENTOSET
                               , 0)),
                               VRORCAMENTOOUT = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '10', V_VRCONSPLAN, NVL(VRORCAMENTOOUT
                               , 0)),
                               VRORCAMENTONOV = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '11', V_VRCONSPLAN, NVL(VRORCAMENTONOV
                               , 0)),
                               VRORCAMENTODEZ = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '12', V_VRCONSPLAN, NVL(VRORCAMENTODEZ
                               , 0)),
                               QTORCAMENTOJAN = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '01', V_QTCONSPLAN, NVL(QTORCAMENTOJAN
                               , 0)),
                               QTORCAMENTOFEV = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '02', V_QTCONSPLAN, NVL(QTORCAMENTOFEV
                               , 0)),
                               QTORCAMENTOMAR = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '03', V_QTCONSPLAN, NVL(QTORCAMENTOMAR
                               , 0)),
                               QTORCAMENTOABR = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '04', V_QTCONSPLAN, NVL(QTORCAMENTOABR
                               , 0)),
                               QTORCAMENTOMAI = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '05', V_QTCONSPLAN, NVL(QTORCAMENTOMAI
                               , 0)),
                               QTORCAMENTOJUN = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '06', V_QTCONSPLAN, NVL(QTORCAMENTOJUN
                               , 0)),
                               QTORCAMENTOJUL = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '07', V_QTCONSPLAN, NVL(QTORCAMENTOJUL
                               , 0)),
                               QTORCAMENTOAGO = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '08', V_QTCONSPLAN, NVL(QTORCAMENTOAGO
                               , 0)),
                               QTORCAMENTOSET = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '09', V_QTCONSPLAN, NVL(QTORCAMENTOSET
                               , 0)),
                               QTORCAMENTOOUT = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '10', V_QTCONSPLAN, NVL(QTORCAMENTOOUT
                               , 0)),
                               QTORCAMENTONOV = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '11', V_QTCONSPLAN, NVL(QTORCAMENTONOV
                               , 0)),
                               QTORCAMENTODEZ = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '12', V_QTCONSPLAN, NVL(QTORCAMENTODEZ
                               , 0)),
                               IDITEMCALCORCA = 'S'
                         WHERE CDFILIAL       = P_CDFILIAL AND
                               IDTIPOORCAOP   = 'C' AND
                               NRANOORCAOP    = P_NRANOORCAOP AND
                               CDVERSORCAOP   = V_CDVERSAOORCA AND
                               CDSERVFILI     = C_CONSUMO.CDSERVFILI;

                        IF(SQL%ROWCOUNT = 0)THEN
                            V_IDITEMCALCORCA := 'S';
                            IF(V_IDITEMCALCORCA = 'S')THEN
                                SELECT LPAD((TO_NUMBER(NVL(MAX(NRSEQITEMORCA), '0'))+ 1), 10, 0)
                                  INTO V_NRITEMORCA
                                  FROM ITEMORCAOP
                                 WHERE CDFILIAL       = P_CDFILIAL AND
                                       NRANOORCAOP    = P_NRANOORCAOP AND
                                       CDVERSORCAOP   = V_CDVERSAOORCA;

                                INSERT INTO ITEMORCAOP(
                                    NRANOORCAOP,
                                    CDVERSORCAOP,
                                    CDFILIAL,
                                    NRSEQITEMORCA,
                                    IDENTRSAIDOP,
                                    CDTIPOOPER,
                                    CDSERVFILI,
                                    CDDESPADMFAT,
                                    CDDESPPREV,
                                    DTMESBASEITEM,
                                    DTORCAMENTOITEM,
                                    IDTIPOORCAOP,
                                    IDITEMORCAMENTO,
                                    IDITEMCALCORCA,
                                    VRORCAMENTOJAN,
                                    VRORCAMENTOFEV,
                                    VRORCAMENTOMAR,
                                    VRORCAMENTOABR,
                                    VRORCAMENTOMAI,
                                    VRORCAMENTOJUN,
                                    VRORCAMENTOJUL,
                                    VRORCAMENTOAGO,
                                    VRORCAMENTOSET,
                                    VRORCAMENTOOUT,
                                    VRORCAMENTONOV,
                                    VRORCAMENTODEZ
                                )
                                    (SELECT P_NRANOORCAOP,
                                            V_CDVERSAOORCA,
                                            P_CDFILIAL,
                                            V_NRITEMORCA,
                                            '',
                                            '',
                                            C_CONSUMO.CDSERVFILI,
                                            '',
                                            '',
                                            V_DTMESBASE,
                                            V_DATA,
                                            'C',
                                            'P',
                                            'S',
                                            DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '01', V_VRCONSPLAN, 0),
                                            DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '02', V_VRCONSPLAN, 0),
                                            DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '03', V_VRCONSPLAN, 0),
                                            DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '04', V_VRCONSPLAN, 0),
                                            DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '05', V_VRCONSPLAN, 0),
                                            DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '06', V_VRCONSPLAN, 0),
                                            DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '09', V_VRCONSPLAN, 0),
                                            DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '07', V_VRCONSPLAN, 0),
                                            DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '08', V_VRCONSPLAN, 0),
                                            DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '10', V_VRCONSPLAN, 0),
                                            DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '11', V_VRCONSPLAN, 0),
                                            DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '12', V_VRCONSPLAN, 0)
                                      FROM DUAL
                                    MINUS
                                    SELECT P_NRANOORCAOP,
                                           V_CDVERSAOORCA,
                                           P_CDFILIAL,
                                           V_NRITEMORCA,
                                           '',
                                           '',
                                           C_CONSUMO.CDSERVFILI,
                                           '',
                                           '',
                                           V_DTMESBASE,
                                           V_DATA,
                                           'C',
                                           'P',
                                           'S',
                                           DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '01', V_VRCONSPLAN, VRORCAMENTOJAN
                                           ),
                                           DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '02', V_VRCONSPLAN, VRORCAMENTOFEV
                                           ),
                                           DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '03', V_VRCONSPLAN, VRORCAMENTOMAR
                                           ),
                                           DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '04', V_VRCONSPLAN, VRORCAMENTOABR
                                           ),
                                           DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '05', V_VRCONSPLAN, VRORCAMENTOMAI
                                           ),
                                           DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '06', V_VRCONSPLAN, VRORCAMENTOJUN
                                           ),
                                           DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '09', V_VRCONSPLAN, VRORCAMENTOJUL
                                           ),
                                           DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '07', V_VRCONSPLAN, VRORCAMENTOAGO
                                           ),
                                           DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '08', V_VRCONSPLAN, VRORCAMENTOSET
                                           ),
                                           DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '10', V_VRCONSPLAN, VRORCAMENTOOUT
                                           ),
                                           DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '11', V_VRCONSPLAN, VRORCAMENTONOV
                                           ),
                                           DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '12', V_VRCONSPLAN, VRORCAMENTODEZ
                                           )
                                      FROM ITEMORCAOP
                                     WHERE NRANOORCAOP    = P_NRANOORCAOP AND
                                           CDVERSORCAOP   = V_CDVERSAOORCA AND
                                           IDTIPOORCAOP   = 'C' AND
                                           CDFILIAL       = P_CDFILIAL AND
                                           CDSERVFILI     = C_CONSUMO.CDSERVFILI
                                    );

                            END IF;

                        END IF;

                        V_NRMES        := V_NRMES + 1;
                    END LOOP;
                END;

            END LOOP; /*LOOP DO CURSOR QUE POSSUI OS SERVICOS*/

            DELETE ITEMORCAOP
             WHERE CDFILIAL         = P_CDFILIAL AND
                   IDTIPOORCAOP     = 'C' AND
                   NRANOORCAOP      = P_NRANOORCAOP AND
                   IDITEMCALCORCA   = 'N';

        ELSE
            FOR C_SERVRETI IN(
                SELECT SERV.*
                  FROM(
                    SELECT S.CDSERVFILI
                      FROM SERVICOROP   S,
                           SERVICO      SE
                     WHERE(S.CDFILIAL       = TIPOPARAM_DRO(P_CDFILIAL))AND
                          (IDTPSERVICOROP   = 'R')AND
                          (V_IDSERVRETROP <> 'S')AND
                          S.CDSERVFILI     = SE.CDSERVFILI AND
                          SE.CDFILIAL      = P_CDFILIAL
                    UNION ALL
                    SELECT CDSERVFILI
                      FROM SERVICO S
                     WHERE(S.CDFILIAL       = P_CDFILIAL)AND
                          (V_IDSERVRETROP   = 'S')
                )SERV,
                      (
                          SELECT CDSERVFILI
                            FROM ITEMORCAOP
                           WHERE CDFILIAL       = P_CDFILIAL AND
                                 NRANOORCAOP    = P_NRANOORCAOP AND
                                 CDVERSORCAOP   = V_CDVERSAOORCA AND
                                 IDTIPOORCAOP   = 'C'
                      )ORC
                 WHERE SERV.CDSERVFILI = ORC.CDSERVFILI
                 ORDER BY SERV.CDSERVFILI
            )LOOP
                V_NRMES        := 0;
                V_SUMEFETMES   := 0;
                V_QTEFETIMES   := 0;
                WHILE(V_NRMES < V_CONTAMES)LOOP
                    IF P_DATACOMP = 'S' THEN
                        V_DTANOORCA := ADD_MONTHS(WDIA || '/' || V_NRMESINIVERORC || '/' || P_NRANOORCAOP, V_NRMES);
                    ELSE
                        V_DTANOORCA := ADD_MONTHS(WDIA || '/' || TO_CHAR(P_DATAINIC, 'MM')|| '/' || P_NRANOORCAOP, V_NRMES);
                    END IF;

                    V_DTANOORCACONS    := V_DTANOORCA;
                    V_DTANOORCACONS2   := V_DTANOORCA;
                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '01')THEN
                        V_VRCONSPLANJAN   := 0;
                        V_QTCONSPLANJAN   := 0;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '02')THEN
                        V_VRCONSPLANFEV   := 0;
                        V_QTCONSPLANFEV   := 0;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '03')THEN
                        V_VRCONSPLANMAR   := 0;
                        V_QTCONSPLANMAR   := 0;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '04')THEN
                        V_VRCONSPLANABR   := 0;
                        V_QTCONSPLANABR   := 0;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '05')THEN
                        V_VRCONSPLANMAI   := 0;
                        V_QTCONSPLANMAI   := 0;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '06')THEN
                        V_VRCONSPLANJUN   := 0;
                        V_QTCONSPLANJUN   := 0;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '07')THEN
                        V_VRCONSPLANJUL   := 0;
                        V_QTCONSPLANJUL   := 0;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '08')THEN
                        V_VRCONSPLANAGO   := 0;
                        V_QTCONSPLANAGO   := 0;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '09')THEN
                        V_VRCONSPLANSET   := 0;
                        V_QTCONSPLANSET   := 0;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '10')THEN
                        V_VRCONSPLANOUT   := 0;
                        V_QTCONSPLANOUT   := 0;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '11')THEN
                        V_VRCONSPLANNOV   := 0;
                        V_QTCONSPLANNOV   := 0;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '12')THEN
                        V_VRCONSPLANDEZ   := 0;
                        V_QTCONSPLANDEZ   := 0;
                    END IF;

                    FOR C_SERVFILI IN(
                        SELECT SC.CDSERVFILI,
                               NVL(SE.QTCLIESTISEG, 0)AS QTCOMESTISEG,
                               NVL(SE.QTCLIESTITER, 0)AS QTCOMESTITER,
                               NVL(SE.QTCLIESTIQUA, 0)AS QTCOMESTIQUA,
                               NVL(SE.QTCLIESTIQUI, 0)AS QTCOMESTIQUI,
                               NVL(SE.QTCLIESTISEX, 0)AS QTCOMESTISEX,
                               NVL(SE.QTCLIESTISAB, 0)AS QTCOMESTISAB,
                               NVL(SE.QTCLIESTIDOM, 0)AS QTCOMESTIDOM,
                               NVL(SE.QTCLIESTIFER, 0)AS QTCOMESTIFER
                          FROM SERVICOROP   SC,
                               SERVICO      SE
                         WHERE SE.CDFILIAL      = P_CDFILIAL AND
                               SE.CDSERVFILI    = SC.CDSERVFILI AND
                               (SC.CDFILIAL      = TIPOPARAM_DRO(P_CDFILIAL))AND
                               (IDTPSERVICOROP   = 'E')AND
                               (V_IDSERVFATROP <> 'S')AND
                               (SC.CDSERVFILI    = C_SERVRETI.CDSERVFILI)
                        UNION ALL
                        SELECT SE.CDSERVFILI,
                               NVL(SE.QTCLIESTISEG, 0)AS QTCOMESTISEG,
                               NVL(SE.QTCLIESTITER, 0)AS QTCOMESTITER,
                               NVL(SE.QTCLIESTIQUA, 0)AS QTCOMESTIQUA,
                               NVL(SE.QTCLIESTIQUI, 0)AS QTCOMESTIQUI,
                               NVL(SE.QTCLIESTISEX, 0)AS QTCOMESTISEX,
                               NVL(SE.QTCLIESTISAB, 0)AS QTCOMESTISAB,
                               NVL(SE.QTCLIESTIDOM, 0)AS QTCOMESTIDOM,
                               NVL(SE.QTCLIESTIFER, 0)AS QTCOMESTIFER
                          FROM SERVICO SE
                         WHERE SE.CDFILIAL      = P_CDFILIAL AND
                               V_IDSERVFATROP   = 'S' AND
                               (SE.CDSERVFILI    = C_SERVRETI.CDSERVFILI)AND
                               (SE.QTCOMESTISEG IS NOT NULL OR
                                SE.QTCOMESTITER IS NOT NULL OR
                                SE.QTCOMESTIQUA IS NOT NULL OR
                                SE.QTCOMESTIQUI IS NOT NULL OR
                                SE.QTCOMESTISEX IS NOT NULL OR
                                SE.QTCOMESTISAB IS NOT NULL OR
                                SE.QTCOMESTIDOM IS NOT NULL OR
                                SE.QTCOMESTIFER IS NOT NULL)
                         ORDER BY CDSERVFILI
                    )LOOP
                        V_DTANOORCAFIN   := LAST_DAY(V_DTANOORCACONS);
                        WHILE(V_DTANOORCACONS <= V_DTANOORCAFIN)LOOP
                            BEGIN
                                SELECT COUNT(*)
                                  INTO V_COUNT
                                  FROM FERIFILI
                                 WHERE(CDFILIAL     = P_CDFILIAL)AND
                                      (DTFERIFILI   = V_DTANOORCACONS);

                            EXCEPTION
                                WHEN NO_DATA_FOUND THEN
                                    V_COUNT := 0;
                            END;

                            IF(V_COUNT = 0)OR(V_IDCONSDIAUTFEC = 'N')THEN
                                BEGIN
                                    SELECT DECODE(TO_CHAR(V_DTANOORCACONS, 'D'), '2', C_SERVFILI.QTCOMESTISEG, DECODE(TO_CHAR(V_DTANOORCACONS
                                    , 'D'), '3', C_SERVFILI.QTCOMESTITER, DECODE(TO_CHAR(V_DTANOORCACONS, 'D'), '4', C_SERVFILI.QTCOMESTIQUA
                                    , DECODE(TO_CHAR(V_DTANOORCACONS, 'D'), '5', C_SERVFILI.QTCOMESTIQUI, DECODE(TO_CHAR(V_DTANOORCACONS
                                    , 'D'), '6', C_SERVFILI.QTCOMESTISEX, DECODE(TO_CHAR(V_DTANOORCACONS, 'D'), '7', C_SERVFILI.QTCOMESTISAB
                                    , C_SERVFILI.QTCOMESTIDOM))))))
                                      INTO V_SUMEFET
                                      FROM DUAL;

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_SUMEFET := 0;
                                END;

                                V_SUMEFETMES   := V_SUMEFETMES + V_SUMEFET;
                                V_QTEFETIMES   := V_QTEFETIMES + V_SUMEFET;
                            END IF;

                            V_DTANOORCACONS := TO_DATE(V_DTANOORCACONS + 1);
                        END LOOP;

                        BEGIN
                            SELECT VRCUSTPADR
                              INTO V_VRCUSTPADR
                              FROM CUSTPADR
                             WHERE CDFILIAL       = P_CDFILIAL AND
                                   CDSERVFILI     = C_SERVRETI.CDSERVFILI AND
                                   DTREFCUSPADR   = V_DTANOORCACONS2;

                        EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                                V_VRCUSTPADR := 0;
                        END;

                        SELECT(DECODE(TO_CHAR(V_DTANOORCA, 'MM'), '01', V_VRPERCORRCOMJAN, DECODE(TO_CHAR(V_DTANOORCA, 'MM'), '02'
                        , V_VRPERCORRCOMFEV, DECODE(TO_CHAR(V_DTANOORCA, 'MM'), '03', V_VRPERCORRCOMMAR, DECODE(TO_CHAR(V_DTANOORCA
                        , 'MM'), '04', V_VRPERCORRCOMABR, DECODE(TO_CHAR(V_DTANOORCA, 'MM'), '05', V_VRPERCORRCOMMAI, DECODE(TO_CHAR
                        (V_DTANOORCA, 'MM'), '06', V_VRPERCORRCOMJUN, DECODE(TO_CHAR(V_DTANOORCA, 'MM'), '07', V_VRPERCORRCOMJUL,
                        DECODE(TO_CHAR(V_DTANOORCA, 'MM'), '08', V_VRPERCORRCOMAGO, DECODE(TO_CHAR(V_DTANOORCA, 'MM'), '09', V_VRPERCORRCOMSET
                        , DECODE(TO_CHAR(V_DTANOORCA, 'MM'), '10', V_VRPERCORRCOMOUT, DECODE(TO_CHAR(V_DTANOORCA, 'MM'), '11', V_VRPERCORRCOMNOV
                        , DECODE(TO_CHAR(V_DTANOORCA, 'MM'), '12', V_VRPERCORRCOMDEZ, 100))))))))))))/ 100)
                          INTO V_PERCORRECAO
                          FROM DUAL;

                        IF NVL(V_PERCORRECAO, 0)= 0 THEN
                            V_PERCORRECAO := 1;
                        END IF;
                        IF V_SUMEFETMES > 0 THEN
                            V_SUMEFETMES   :=(V_SUMEFETMES * V_PERCORRECAO); -- AQUI
                            V_QTEFETIMES   :=(V_QTEFETIMES * V_PERCORRECAO);
                        END IF;

                        V_VRTOTEFETMES   := V_SUMEFETMES * V_VRCUSTPADR;
                        V_SUMEFETMES     := 0;
                    END LOOP;

                    V_NRMES2           := 0;
                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '01')THEN
                        V_VRCONSPLANJAN   := V_VRCONSPLANJAN + V_VRTOTEFETMES;
                        V_QTCONSPLANJAN   := V_QTCONSPLANJAN + V_QTEFETIMES;  -- AQUI 
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '02')THEN
                        V_VRCONSPLANFEV   := V_VRCONSPLANFEV + V_VRTOTEFETMES;
                        V_QTCONSPLANFEV   := V_QTCONSPLANFEV + V_QTEFETIMES;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '03')THEN
                        V_VRCONSPLANMAR   := V_VRCONSPLANMAR + V_VRTOTEFETMES;
                        V_QTCONSPLANMAR   := V_QTCONSPLANMAR + V_QTEFETIMES;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '04')THEN
                        V_VRCONSPLANABR   := V_VRCONSPLANABR + V_VRTOTEFETMES;
                        V_QTCONSPLANABR   := V_QTCONSPLANABR + V_QTEFETIMES;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '05')THEN
                        V_VRCONSPLANMAI   := V_VRCONSPLANMAI + V_VRTOTEFETMES;
                        V_QTCONSPLANMAI   := V_QTCONSPLANMAI + V_QTEFETIMES;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '06')THEN
                        V_VRCONSPLANJUN   := V_VRCONSPLANJUN + V_VRTOTEFETMES;
                        V_QTCONSPLANJUN   := V_QTCONSPLANJUN + V_QTEFETIMES;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '07')THEN
                        V_VRCONSPLANJUL   := V_VRCONSPLANJUL + V_VRTOTEFETMES;
                        V_QTCONSPLANJUL   := V_QTCONSPLANJUL + V_QTEFETIMES;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '08')THEN
                        V_VRCONSPLANAGO   := V_VRCONSPLANAGO + V_VRTOTEFETMES;
                        V_QTCONSPLANAGO   := V_QTCONSPLANAGO + V_QTEFETIMES;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '09')THEN
                        V_VRCONSPLANSET   := V_VRCONSPLANSET + V_VRTOTEFETMES;
                        V_QTCONSPLANSET   := V_QTCONSPLANSET + V_QTEFETIMES;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '10')THEN
                        V_VRCONSPLANOUT   := V_VRCONSPLANOUT + V_VRTOTEFETMES;
                        V_QTCONSPLANOUT   := V_QTCONSPLANOUT + V_QTEFETIMES;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '11')THEN
                        V_VRCONSPLANNOV   := V_VRCONSPLANNOV + V_VRTOTEFETMES;
                        V_QTCONSPLANNOV   := V_QTCONSPLANNOV + V_QTEFETIMES;
                    END IF;

                    IF(TO_CHAR(V_DTANOORCA, 'MM')= '12')THEN
                        V_VRCONSPLANDEZ   := V_VRCONSPLANDEZ + V_VRTOTEFETMES;
                        V_QTCONSPLANDEZ   := V_QTCONSPLANDEZ + V_QTEFETIMES;
                    END IF;

                    V_VRTOTEFETMES     := 0;
                    V_QTEFETIMES       := 0;
                    V_NRITEMORCA       := '0000000020';
                    UPDATE ITEMORCAOP
                       SET VRORCAMENTOJAN = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '01', V_VRCONSPLANJAN, NVL(VRORCAMENTOJAN
                       , 0)),
                           VRORCAMENTOFEV = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '02', V_VRCONSPLANFEV, NVL(VRORCAMENTOFEV
                           , 0)),
                           VRORCAMENTOMAR = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '03', V_VRCONSPLANMAR, NVL(VRORCAMENTOMAR
                           , 0)),
                           VRORCAMENTOABR = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '04', V_VRCONSPLANABR, NVL(VRORCAMENTOABR
                           , 0)),
                           VRORCAMENTOMAI = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '05', V_VRCONSPLANMAI, NVL(VRORCAMENTOMAI
                           , 0)),
                           VRORCAMENTOJUN = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '06', V_VRCONSPLANJUN, NVL(VRORCAMENTOJUN
                           , 0)),
                           VRORCAMENTOJUL = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '07', V_VRCONSPLANJUL, NVL(VRORCAMENTOJUL
                           , 0)),
                           VRORCAMENTOAGO = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '08', V_VRCONSPLANAGO, NVL(VRORCAMENTOAGO
                           , 0)),
                           VRORCAMENTOSET = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '09', V_VRCONSPLANSET, NVL(VRORCAMENTOSET
                           , 0)),
                           VRORCAMENTOOUT = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '10', V_VRCONSPLANOUT, NVL(VRORCAMENTOOUT
                           , 0)),
                           VRORCAMENTONOV = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '11', V_VRCONSPLANNOV, NVL(VRORCAMENTONOV
                           , 0)),
                           VRORCAMENTODEZ = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '12', V_VRCONSPLANDEZ, NVL(VRORCAMENTODEZ
                           , 0)),
                           QTORCAMENTOJAN = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '01', V_QTCONSPLANJAN, NVL(QTORCAMENTOJAN
                           , 0)),
                           QTORCAMENTOFEV = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '02', V_QTCONSPLANFEV, NVL(QTORCAMENTOFEV
                           , 0)),
                           QTORCAMENTOMAR = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '03', V_QTCONSPLANMAR, NVL(QTORCAMENTOMAR
                           , 0)),
                           QTORCAMENTOABR = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '04', V_QTCONSPLANABR, NVL(QTORCAMENTOABR
                           , 0)),
                           QTORCAMENTOMAI = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '05', V_QTCONSPLANMAI, NVL(QTORCAMENTOMAI
                           , 0)),
                           QTORCAMENTOJUN = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '06', V_QTCONSPLANJUN, NVL(QTORCAMENTOJUN
                           , 0)),
                           QTORCAMENTOJUL = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '07', V_QTCONSPLANJUL, NVL(QTORCAMENTOJUL
                           , 0)),
                           QTORCAMENTOAGO = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '08', V_QTCONSPLANAGO, NVL(QTORCAMENTOAGO
                           , 0)),
                           QTORCAMENTOSET = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '09', V_QTCONSPLANSET, NVL(QTORCAMENTOSET
                           , 0)),
                           QTORCAMENTOOUT = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '10', V_QTCONSPLANOUT, NVL(QTORCAMENTOOUT
                           , 0)),
                           QTORCAMENTONOV = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '11', V_QTCONSPLANNOV, NVL(QTORCAMENTONOV
                           , 0)),
                           QTORCAMENTODEZ = DECODE(TO_CHAR(ADD_MONTHS(V_DATAINI, V_NRMES), 'MM'), '12', V_QTCONSPLANDEZ, NVL(QTORCAMENTODEZ
                           , 0)),
                           IDITEMCALCORCA = 'S'
                     WHERE CDFILIAL       = P_CDFILIAL AND
                           IDTIPOORCAOP   = 'C' AND
                           NRANOORCAOP    = P_NRANOORCAOP AND
                           CDVERSORCAOP   = V_CDVERSAOORCA AND
                           CDSERVFILI     = C_SERVRETI.CDSERVFILI;

                    IF(SQL%ROWCOUNT = 0)THEN
                        V_IDITEMCALCORCA := 'S';
                        BEGIN
                            SELECT IDITEMCALCORCA
                              INTO V_IDITEMCALCORCA
                              FROM ITEMORCAOP
                             WHERE NRANOORCAOP      = P_NRANOORCAOP AND
                                   CDVERSORCAOP     = V_CDVERSAOORCA AND
                                   CDFILIAL         = P_CDFILIAL AND
                                   IDTIPOORCAOP     = 'C' AND
                                   IDITEMCALCORCA   = 'N';

                        EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                                V_IDITEMCALCORCA := 'S';
                        END;

                        IF(V_IDITEMCALCORCA = 'S')THEN
                            SELECT LPAD((TO_NUMBER(NVL(MAX(NRSEQITEMORCA), '0'))+ 1), 10, 0)
                              INTO V_NRITEMORCA
                              FROM ITEMORCAOP
                             WHERE CDFILIAL       = P_CDFILIAL AND
                                   NRANOORCAOP    = P_NRANOORCAOP AND
                                   CDVERSORCAOP   = V_CDVERSAOORCA;

                            INSERT INTO ITEMORCAOP(
                                NRANOORCAOP,
                                CDVERSORCAOP,
                                CDFILIAL,
                                NRSEQITEMORCA,
                                IDENTRSAIDOP,
                                CDTIPOOPER,
                                CDSERVFILI,
                                CDDESPADMFAT,
                                CDDESPPREV,
                                DTMESBASEITEM,
                                DTORCAMENTOITEM,
                                IDTIPOORCAOP,
                                IDITEMORCAMENTO,
                                VRORCAMENTOJAN,
                                VRORCAMENTOFEV,
                                VRORCAMENTOMAR,
                                VRORCAMENTOABR,
                                VRORCAMENTOMAI,
                                VRORCAMENTOJUN,
                                VRORCAMENTOJUL,
                                VRORCAMENTOAGO,
                                VRORCAMENTOSET,
                                VRORCAMENTOOUT,
                                VRORCAMENTONOV,
                                VRORCAMENTODEZ,
                                QTORCAMENTOJAN,
                                QTORCAMENTOFEV,
                                QTORCAMENTOMAR,
                                QTORCAMENTOABR,
                                QTORCAMENTOMAI,
                                QTORCAMENTOJUN,
                                QTORCAMENTOJUL,
                                QTORCAMENTOAGO,
                                QTORCAMENTOSET,
                                QTORCAMENTOOUT,
                                QTORCAMENTONOV,
                                QTORCAMENTODEZ,
                                IDITEMCALCORCA
                            )
                                (SELECT P_NRANOORCAOP,
                                        V_CDVERSAOORCA,
                                        P_CDFILIAL,
                                        V_NRITEMORCA,
                                        '',
                                        '',
                                        C_SERVRETI.CDSERVFILI,
                                        '',
                                        '',
                                        V_DTMESBASE,
                                        V_DATA,
                                        'C',
                                        'P',
                                        V_VRCONSPLANJAN,
                                        V_VRCONSPLANFEV,
                                        V_VRCONSPLANMAR,
                                        V_VRCONSPLANABR,
                                        V_VRCONSPLANMAI,
                                        V_VRCONSPLANJUN,
                                        V_VRCONSPLANJUL,
                                        V_VRCONSPLANAGO,
                                        V_VRCONSPLANSET,
                                        V_VRCONSPLANOUT,
                                        V_VRCONSPLANNOV,
                                        V_VRCONSPLANDEZ,
                                        V_QTCONSPLANJAN,
                                        V_QTCONSPLANFEV,
                                        V_QTCONSPLANMAR,
                                        V_QTCONSPLANABR,
                                        V_QTCONSPLANMAI,
                                        V_QTCONSPLANJUN,
                                        V_QTCONSPLANJUL,
                                        V_QTCONSPLANAGO,
                                        V_QTCONSPLANSET,
                                        V_QTCONSPLANOUT,
                                        V_QTCONSPLANNOV,
                                        V_QTCONSPLANDEZ,
                                        'S'
                                  FROM DUAL
                                MINUS
                                SELECT P_NRANOORCAOP,
                                       V_CDVERSAOORCA,
                                       P_CDFILIAL,
                                       V_NRITEMORCA,
                                       '',
                                       '',
                                       C_SERVRETI.CDSERVFILI,
                                       '',
                                       '',
                                       V_DTMESBASE,
                                       V_DATA,
                                       'C',
                                       'P',
                                       V_VRCONSPLANJAN,
                                       V_VRCONSPLANFEV,
                                       V_VRCONSPLANMAR,
                                       V_VRCONSPLANABR,
                                       V_VRCONSPLANMAI,
                                       V_VRCONSPLANJUN,
                                       V_VRCONSPLANJUL,
                                       V_VRCONSPLANAGO,
                                       V_VRCONSPLANSET,
                                       V_VRCONSPLANOUT,
                                       V_VRCONSPLANNOV,
                                       V_VRCONSPLANDEZ,
                                       V_QTCONSPLANJAN,
                                       V_QTCONSPLANFEV,
                                       V_QTCONSPLANMAR,
                                       V_QTCONSPLANABR,
                                       V_QTCONSPLANMAI,
                                       V_QTCONSPLANJUN,
                                       V_QTCONSPLANJUL,
                                       V_QTCONSPLANAGO,
                                       V_QTCONSPLANSET,
                                       V_QTCONSPLANOUT,
                                       V_QTCONSPLANNOV,
                                       V_QTCONSPLANDEZ,
                                       'S'
                                  FROM ITEMORCAOP
                                 WHERE NRANOORCAOP    = P_NRANOORCAOP AND
                                       CDVERSORCAOP   = V_CDVERSAOORCA AND
                                       IDTIPOORCAOP   = 'C' AND
                                       CDFILIAL       = P_CDFILIAL AND
                                       CDSERVFILI     = C_SERVRETI.CDSERVFILI
                                );

                        END IF;

                    END IF;

                    V_NRMES            := V_NRMES + 1;
                END LOOP;

            END LOOP;
        END IF;

        DELETE FROM CONSPLAN
         WHERE CDFILICPLA   = P_CDFILIAL AND
               NRCONSPLAN   = V_NRCONSPLANMESBASE;

        DELETE FROM CONSPLAN
         WHERE CDFILICPLA   = P_CDFILIAL AND
               NRCONSPLAN   = V_NRCONSPLAN;

    END IF;

    IF P_IDITEMORCA = 'I' OR P_IDITEMORCA = 'R' THEN
        V_RETORNO := 0;
        FOR C_IMPOSTO IN(
            SELECT CDFILIAL,
                   CDIMPOSTO,
                   VRPERCSOBFAT,
                   VRPERCSOBCONS
              FROM IMPOSTOORCAFIL
             WHERE CDFILIAL = P_CDFILIAL AND
                   CDIMPOSTO IN(
                       SELECT CDIMPOSTO
                         FROM ITEMORCAOP
                        WHERE NRANOORCAOP    = P_NRANOORCAOP AND
                              CDVERSORCAOP   =(
                                  SELECT MAX(CDVERSORCAOP)
                                    FROM ITEMORCAOP
                                   WHERE NRANOORCAOP = P_NRANOORCAOP
                              )AND
                              CDFILIAL       = P_CDFILIAL AND
                              IDTIPOORCAOP   = P_IDITEMORCA
                   )
            UNION ALL
            SELECT(P_CDFILIAL)AS CDFILIAL,
                  CDIMPOSTO,
                  VRPERCSOBFAT,
                  VRPERCSOBCONS
              FROM IMPOSTOORCAFIL
             WHERE CDFILIAL = 'T' AND
                   CDIMPOSTO NOT IN(
                       SELECT CDIMPOSTO
                         FROM IMPOSTOORCAFIL
                        WHERE CDFILIAL = P_CDFILIAL
                   )AND
                   CDIMPOSTO IN(
                       SELECT CDIMPOSTO
                         FROM ITEMORCAOP
                        WHERE NRANOORCAOP    = P_NRANOORCAOP AND
                              CDVERSORCAOP   =(
                                  SELECT MAX(CDVERSORCAOP)
                                    FROM ITEMORCAOP
                                   WHERE NRANOORCAOP = P_NRANOORCAOP
                              )AND
                              CDFILIAL       = P_CDFILIAL AND
                              IDTIPOORCAOP   = P_IDITEMORCA
                   )
        )LOOP IF((P_IDITEMORCA = 'I')AND(C_IMPOSTO.VRPERCSOBFAT <> 0))OR((P_IDITEMORCA = 'R')AND(C_IMPOSTO.VRPERCSOBCONS <> 0))THEN
            BEGIN
                SELECT CDVERSORCAOP,
                       CDFILIAL,
                       IDTIPOORCAOP,
                       SUM(VRORCAMENTOJAN)AS VRORCAMENTOJAN,
                       SUM(VRORCAMENTOFEV)AS VRORCAMENTOFEV,
                       SUM(VRORCAMENTOMAR)AS VRORCAMENTOMAR,
                       SUM(VRORCAMENTOABR)AS VRORCAMENTOABR,
                       SUM(VRORCAMENTOMAI)AS VRORCAMENTOMAI,
                       SUM(VRORCAMENTOJUN)AS VRORCAMENTOJUN,
                       SUM(VRORCAMENTOJUL)AS VRORCAMENTOJUL,
                       SUM(VRORCAMENTOAGO)AS VRORCAMENTOAGO,
                       SUM(VRORCAMENTOSET)AS VRORCAMENTOSET,
                       SUM(VRORCAMENTOOUT)AS VRORCAMENTOOUT,
                       SUM(VRORCAMENTONOV)AS VRORCAMENTONOV,
                       SUM(VRORCAMENTODEZ)AS VRORCAMENTODEZ
                  INTO
                    V_VERSORCA,
                    V_CDFILIAL,
                    V_IDTIPOORCAOP,
                    V_VRORCAIMPOJAN,
                    V_VRORCAIMPOFEV,
                    V_VRORCAIMPOMAR,
                    V_VRORCAIMPOABR,
                    V_VRORCAIMPOMAI,
                    V_VRORCAIMPOJUN,
                    V_VRORCAIMPOJUL,
                    V_VRORCAIMPOAGO,
                    V_VRORCAIMPOSET,
                    V_VRORCAIMPOOUT,
                    V_VRORCAIMPONOV,
                    V_VRORCAIMPODEZ
                  FROM ITEMORCAOP
                 WHERE NRANOORCAOP    = P_NRANOORCAOP AND
                       CDVERSORCAOP   =(
                           SELECT MAX(CDVERSORCAOP)
                             FROM ITEMORCAOP
                            WHERE NRANOORCAOP = P_NRANOORCAOP
                       )AND
                       CDFILIAL       = P_CDFILIAL AND
                       IDTIPOORCAOP   =(
                           SELECT DECODE(P_IDITEMORCA, 'I', 'F', 'C')AS IDTIPOORCA
                             FROM DUAL
                       )
                 GROUP BY CDVERSORCAOP,
                          CDFILIAL,
                          IDTIPOORCAOP;

            EXCEPTION
                WHEN NO_DATA_FOUND THEN
                    V_VERSORCA        := 0;
                    V_CDFILIAL        := P_CDFILIAL;
                    V_IDTIPOORCAOP    := P_IDITEMORCA;
                    V_VRORCAIMPOJAN   := 0;
                    V_VRORCAIMPOFEV   := 0;
                    V_VRORCAIMPOMAR   := 0;
                    V_VRORCAIMPOABR   := 0;
                    V_VRORCAIMPOMAI   := 0;
                    V_VRORCAIMPOJUN   := 0;
                    V_VRORCAIMPOJUL   := 0;
                    V_VRORCAIMPOAGO   := 0;
                    V_VRORCAIMPOSET   := 0;
                    V_VRORCAIMPOOUT   := 0;
                    V_VRORCAIMPONOV   := 0;
                    V_VRORCAIMPODEZ   := 0;
            END;

            IF V_VERSORCA <> 0 THEN
                IF V_VRORCAIMPOJAN IS NULL THEN
                    V_VRORCAIMPOJAN := 0;
                END IF;
                IF V_VRORCAIMPOFEV IS NULL THEN
                    V_VRORCAIMPOFEV := 0;
                END IF;
                IF V_VRORCAIMPOMAR IS NULL THEN
                    V_VRORCAIMPOMAR := 0;
                END IF;
                IF V_VRORCAIMPOABR IS NULL THEN
                    V_VRORCAIMPOABR := 0;
                END IF;
                IF V_VRORCAIMPOMAI IS NULL THEN
                    V_VRORCAIMPOMAI := 0;
                END IF;
                IF V_VRORCAIMPOJUN IS NULL THEN
                    V_VRORCAIMPOJUN := 0;
                END IF;
                IF V_VRORCAIMPOJUL IS NULL THEN
                    V_VRORCAIMPOJUL := 0;
                END IF;
                IF V_VRORCAIMPOAGO IS NULL THEN
                    V_VRORCAIMPOAGO := 0;
                END IF;
                IF V_VRORCAIMPOSET IS NULL THEN
                    V_VRORCAIMPOSET := 0;
                END IF;
                IF V_VRORCAIMPOOUT IS NULL THEN
                    V_VRORCAIMPOOUT := 0;
                END IF;
                IF V_VRORCAIMPONOV IS NULL THEN
                    V_VRORCAIMPONOV := 0;
                END IF;
                IF V_VRORCAIMPODEZ IS NULL THEN
                    V_VRORCAIMPODEZ := 0;
                END IF;
                IF P_IDITEMORCA = 'I' THEN
                    V_VRPERCIMPO := C_IMPOSTO.VRPERCSOBFAT;
                ELSE
                    V_VRPERCIMPO := C_IMPOSTO.VRPERCSOBCONS;
                END IF;

                UPDATE ITEMORCAOP
                   SET VRORCAMENTOJAN =((V_VRORCAIMPOJAN * V_VRPERCIMPO)/ 100),
                       VRORCAMENTOFEV =((V_VRORCAIMPOFEV * V_VRPERCIMPO)/ 100),
                       VRORCAMENTOMAR =((V_VRORCAIMPOMAR * V_VRPERCIMPO)/ 100),
                       VRORCAMENTOABR =((V_VRORCAIMPOABR * V_VRPERCIMPO)/ 100),
                       VRORCAMENTOMAI =((V_VRORCAIMPOMAI * V_VRPERCIMPO)/ 100),
                       VRORCAMENTOJUN =((V_VRORCAIMPOJUN * V_VRPERCIMPO)/ 100),
                       VRORCAMENTOJUL =((V_VRORCAIMPOJUL * V_VRPERCIMPO)/ 100),
                       VRORCAMENTOAGO =((V_VRORCAIMPOAGO * V_VRPERCIMPO)/ 100),
                       VRORCAMENTOSET =((V_VRORCAIMPOSET * V_VRPERCIMPO)/ 100),
                       VRORCAMENTOOUT =((V_VRORCAIMPOOUT * V_VRPERCIMPO)/ 100),
                       VRORCAMENTONOV =((V_VRORCAIMPONOV * V_VRPERCIMPO)/ 100),
                       VRORCAMENTODEZ =((V_VRORCAIMPODEZ * V_VRPERCIMPO)/ 100)
                 WHERE CDFILIAL       = C_IMPOSTO.CDFILIAL AND
                       NRANOORCAOP    = P_NRANOORCAOP AND
                       CDVERSORCAOP   = V_VERSORCA AND
                       CDIMPOSTO      = C_IMPOSTO.CDIMPOSTO AND
                       IDTIPOORCAOP   = P_IDITEMORCA;

            END IF;

        ELSE
            V_RETORNO := V_RETORNO + 1;
        END IF;
        END LOOP;

        IF V_RETORNO > 0 THEN
            P_RETORNO := 'ERRO';
        ELSE
            P_RETORNO := 'OK';
        END IF;

    END IF;

    IF P_IDITEMORCA = 'P' THEN
        BEGIN
            IF P_DATACOMP = 'S' THEN
                V_CONTAMES := 12;
            ELSE
                SELECT TO_CHAR(P_DATAINIC, 'MM'),
                       TO_CHAR(P_DATAFIN, 'MM'),
                       TO_CHAR(P_DATAINIC, 'YYYY'),
                       TO_CHAR(P_DATAFIN, 'YYYY')
                  INTO
                    V_DTINICPER,
                    V_DTFINALPER,
                    V_DTINICPERANO,
                    V_DTFINALPERANO
                  FROM DUAL;

                V_CONTAMES   := 0;
                DATAINIVL    := '01/' || V_DTINICPER || '/' || V_DTINICPERANO;
                DATAFINVL    := '01/' || V_DTFINALPER || '/' || V_DTFINALPERANO;
                WHILE(DATAINIVL <= DATAFINVL)LOOP
                    V_CONTAMES   := V_CONTAMES + 1;
                    DATAINIVL    := ADD_MONTHS(DATAINIVL, 1);
                END LOOP;

            END IF;

            V_NRMES     := 0;
            WHILE V_NRMES < V_CONTAMES LOOP
                IF P_DATACOMP = 'S' THEN
                    V_DTDEPRMES := ADD_MONTHS('01/01/' || P_NRANOORCAOP, V_NRMES);
                ELSE
                    V_DTDEPRMES := ADD_MONTHS('01/' || V_DTINICPER || '/' || V_DTINICPERANO, V_NRMES);
                END IF;

                FOR C_DEPREC IN(
                    SELECT NVL(SUM(DECODE(RB.CDFILICCUST, NULL, ROUND((BA.VRINIDEPBEM / BA.QTMESDEPBEM), 2), ROUND((((BA.VRINIDEPBEM / BA.QTMESDEPBEM)* RB.VRRATEBEM)/ 100), 2))), 0)AS VRDEPREC
                      FROM BEMATIVO    BA,
                           RATEIOBEM   RB
                     WHERE NVL(CDFILICCUST, CDFILIAL)  = P_CDFILIAL AND
                           (BA.CDEMPRESA                = RB.CDEMPRESA(+))AND
                           (BA.DTBAIXATOTABEMA IS NULL)AND
                           (BA.CDBEMATIVO               = RB.CDBEMATIVO(+))AND
                           (V_DTDEPRMES BETWEEN TO_DATE((LAST_DAY(ADD_MONTHS(BA.DTINIDEPRECBEM, - 1))+ 1))AND TO_DATE(ADD_MONTHS(
                           BA.DTINIDEPRECBEM, BA.QTMESDEPBEM)))AND
                           (BA.CDBEMATIVO NOT IN(
                               SELECT CDBEMATIVO
                                 FROM SUSPBEMATIVO
                                WHERE(CDEMPRESA    = BA.CDEMPRESA)AND
                                     (CDBEMATIVO   = BA.CDBEMATIVO)AND
                                     (V_DTDEPRMES BETWEEN(LAST_DAY(ADD_MONTHS(DTINISUSPATIVO, - 1))+ 1)AND LAST_DAY(DTFINSUSPATIVO
                                     ))
                           ))
                )LOOP UPDATE ITEMORCAOP
                   SET VRORCAMENTOJAN = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '01', C_DEPREC.VRDEPREC, NVL(VRORCAMENTOJAN, 0)),
                       VRORCAMENTOFEV = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '02', C_DEPREC.VRDEPREC, NVL(VRORCAMENTOFEV, 0)),
                       VRORCAMENTOMAR = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '03', C_DEPREC.VRDEPREC, NVL(VRORCAMENTOMAR, 0)),
                       VRORCAMENTOABR = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '04', C_DEPREC.VRDEPREC, NVL(VRORCAMENTOABR, 0)),
                       VRORCAMENTOMAI = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '05', C_DEPREC.VRDEPREC, NVL(VRORCAMENTOMAI, 0)),
                       VRORCAMENTOJUN = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '06', C_DEPREC.VRDEPREC, NVL(VRORCAMENTOJUN, 0)),
                       VRORCAMENTOJUL = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '07', C_DEPREC.VRDEPREC, NVL(VRORCAMENTOJUL, 0)),
                       VRORCAMENTOAGO = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '08', C_DEPREC.VRDEPREC, NVL(VRORCAMENTOAGO, 0)),
                       VRORCAMENTOSET = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '09', C_DEPREC.VRDEPREC, NVL(VRORCAMENTOSET, 0)),
                       VRORCAMENTOOUT = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '10', C_DEPREC.VRDEPREC, NVL(VRORCAMENTOOUT, 0)),
                       VRORCAMENTONOV = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '11', C_DEPREC.VRDEPREC, NVL(VRORCAMENTONOV, 0)),
                       VRORCAMENTODEZ = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '12', C_DEPREC.VRDEPREC, NVL(VRORCAMENTODEZ, 0))
                 WHERE CDFILIAL       = P_CDFILIAL AND
                       IDTIPOORCAOP   = 'P' AND
                       NRANOORCAOP    = P_NRANOORCAOP AND
                       CDVERSORCAOP   = V_CDVERSAOORCA;

                END LOOP;

                V_NRMES := V_NRMES + 1;
            END LOOP;

            P_RETORNO   := 'OK';
        EXCEPTION
            WHEN OTHERS THEN
                BEGIN
                    P_RETORNO := SQLERRM(SQLCODE);
                    ROLLBACK;
                END;
        END;
    END IF;

    IF P_IDITEMORCA = 'O' THEN
        BEGIN
            IF P_DATACOMP = 'S' THEN
                V_CONTAMES := 12;
            ELSE
                SELECT TO_CHAR(P_DATAINIC, 'MM'),
                       TO_CHAR(P_DATAFIN, 'MM'),
                       TO_CHAR(P_DATAINIC, 'YYYY'),
                       TO_CHAR(P_DATAFIN, 'YYYY')
                  INTO
                    V_DTINICPER,
                    V_DTFINALPER,
                    V_DTINICPERANO,
                    V_DTFINALPERANO
                  FROM DUAL;

                V_CONTAMES   := 0;
                DATAINIVL    := '01/' || V_DTINICPER || '/' || V_DTINICPERANO;
                DATAFINVL    := '01/' || V_DTFINALPER || '/' || V_DTFINALPERANO;
                WHILE(DATAINIVL <= DATAFINVL)LOOP
                    V_CONTAMES   := V_CONTAMES + 1;
                    DATAINIVL    := ADD_MONTHS(DATAINIVL, 1);
                END LOOP;

            END IF;

            V_NRMES     := 0;
            WHILE V_NRMES < V_CONTAMES LOOP
                IF P_DATACOMP = 'S' THEN
                    V_DTDEPRMES := ADD_MONTHS('01/01/' || P_NRANOORCAOP, V_NRMES);
                ELSE
                    V_DTDEPRMES := ADD_MONTHS('01/' || V_DTINICPER || '/' || V_DTINICPERANO, V_NRMES);
                END IF;

                FOR C_MAOOBRA IN(
                    SELECT O.CDFILIAL,
                           O.IDTPORCAMENTOMO,
                           D.CDDESPADMFAT,
                           NVL(D.VRPERCDESPORCMO, 0)AS VRPERCDESPORCMO,
                           SUM(DECODE(O.IDTPORCAMENTOMO, 'D',(O.QTFUNCORCAMO * DECODE(D.IDORCBASEDIAUTIL, 'S',((D.VRUNITDESPORCMO
                           * D.QTDIADESPFUNORC)* DI.DIASUTCARD), 'N',(D.VRUNITDESPORCMO * D.QTHORAMESORC))), 'M',(O.QTFUNCORCAMO *
                           D.VRMENSDESPORCMO)))VRDESPDIAMO
                      FROM ORCAMENTOMO   O,
                           DESPORCAMO    D,
                           (
                               SELECT(TO_NUMBER(TO_CHAR(LAST_DAY(V_DTDEPRMES), 'DD'))- COUNT(DTFERIFILI))AS DIASUTCARD
                                 FROM FERIFILI
                                WHERE(CDFILIAL = P_CDFILIAL)AND
                                     (DTFERIFILI BETWEEN V_DTDEPRMES AND LAST_DAY(V_DTDEPRMES))
                           )DI
                     WHERE O.CDFILIAL        = P_CDFILIAL AND
                           ((INSTR(P_CDORCAMENTOMO, '_' || O.CDFILIAL || O.CDORCAMENTOMO || '_')> 0)OR
                            (P_CDORCAMENTOMO   = 'T'))AND
                           O.CDORCAMENTOMO   = D.CDORCAMENTOMO AND
                           D.CDFILIAL        = O.CDFILIAL
                     GROUP BY O.CDFILIAL,
                              O.IDTPORCAMENTOMO,
                              D.CDDESPADMFAT,
                              NVL(D.VRPERCDESPORCMO, 0)
                    UNION ALL
                    SELECT O.CDFILIAL,
                           O.IDTPORCAMENTOMO,
                           D.CDDESPADMFAT,
                           NVL(D.VRPERCDESPORCMO, 0)AS VRPERCDESPORCMO,
                           SUM(DECODE(O.IDTPORCAMENTOMO, 'D',(O.QTFUNCORCAMO * DECODE(D.IDORCBASEDIAUTIL, 'S',((D.VRUNITDESPORCMO
                           * D.QTDIADESPFUNORC)* DI.DIASUTCARD), 'N',(D.VRUNITDESPORCMO * D.QTHORAMESORC))), 'M',(O.QTFUNCORCAMO *
                           D.VRMENSDESPORCMO)))VRDESPDIAMO
                      FROM ORCAMENTOMO   O,
                           DESPORCAMO    D,
                           (
                               SELECT(TO_NUMBER(TO_CHAR(LAST_DAY(V_DTDEPRMES), 'DD'))- COUNT(DTFERIFILI))AS DIASUTCARD
                                 FROM FERIFILI
                                WHERE(CDFILIAL = P_CDFILIAL)AND
                                     (DTFERIFILI BETWEEN V_DTDEPRMES AND LAST_DAY(V_DTDEPRMES))
                           )DI
                     WHERE O.CDFILIAL        = 'T' AND
                           ((INSTR(P_CDORCAMENTOMO, '_' || O.CDFILIAL || O.CDORCAMENTOMO || '_')> 0)OR
                            (P_CDORCAMENTOMO   = 'T'))AND
                           O.CDORCAMENTOMO   = D.CDORCAMENTOMO AND
                           D.CDFILIAL        = O.CDFILIAL AND
                           D.CDDESPADMFAT NOT IN(
                               SELECT D.CDDESPADMFAT
                                 FROM ORCAMENTOMO   O,
                                      DESPORCAMO    D
                                WHERE O.CDFILIAL        = P_CDFILIAL AND
                                      ((INSTR(P_CDORCAMENTOMO, '_' || O.CDFILIAL || O.CDORCAMENTOMO || '_')> 0)OR
                                       (P_CDORCAMENTOMO   = 'T'))AND
                                      O.CDORCAMENTOMO   = D.CDORCAMENTOMO AND
                                      O.CDFILIAL        = D.CDFILIAL
                           )
                     GROUP BY O.CDFILIAL,
                              O.IDTPORCAMENTOMO,
                              D.CDDESPADMFAT,
                              NVL(D.VRPERCDESPORCMO, 0)
                )LOOP IF C_MAOOBRA.IDTPORCAMENTOMO = 'P' THEN
            /*Caso o tipo do orcamento seja baseado em Porcentagem do Faturamento*/
                    SELECT SUM(DECODE(TO_CHAR(TO_DATE(V_DTDEPRMES), 'MM'), '01', NVL(VRORCAMENTOJAN, 0), DECODE(TO_CHAR(TO_DATE(V_DTDEPRMES
                    ), 'MM'), '02', NVL(VRORCAMENTOFEV, 0), DECODE(TO_CHAR(TO_DATE(V_DTDEPRMES), 'MM'), '03', NVL(VRORCAMENTOMAR,
                    0), DECODE(TO_CHAR(TO_DATE(V_DTDEPRMES), 'MM'), '04', NVL(VRORCAMENTOABR, 0), DECODE(TO_CHAR(TO_DATE(V_DTDEPRMES
                    ), 'MM'), '05', NVL(VRORCAMENTOMAI, 0), DECODE(TO_CHAR(TO_DATE(V_DTDEPRMES), 'MM'), '06', NVL(VRORCAMENTOJUN,
                    0), DECODE(TO_CHAR(TO_DATE(V_DTDEPRMES), 'MM'), '07', NVL(VRORCAMENTOJUL, 0), DECODE(TO_CHAR(TO_DATE(V_DTDEPRMES
                    ), 'MM'), '08', NVL(VRORCAMENTOAGO, 0), DECODE(TO_CHAR(TO_DATE(V_DTDEPRMES), 'MM'), '09', NVL(VRORCAMENTOSET,
                    0), DECODE(TO_CHAR(TO_DATE(V_DTDEPRMES), 'MM'), '10', NVL(VRORCAMENTOOUT, 0), DECODE(TO_CHAR(TO_DATE(V_DTDEPRMES
                    ), 'MM'), '11', NVL(VRORCAMENTONOV, 0), DECODE(TO_CHAR(TO_DATE(V_DTDEPRMES), 'MM'), '12', NVL(VRORCAMENTODEZ,
                    0), 0)))))))))))))AS VRORCAMENTOMES
                      INTO V_VRORCAMENTOMES
                      FROM ITEMORCAOP
                     WHERE CDFILIAL       = P_CDFILIAL AND
                           NRANOORCAOP    = P_NRANOORCAOP AND
                           CDVERSORCAOP   = V_CDVERSAOORCA AND
                           IDTIPOORCAOP   = 'F';

                    V_VRPERCDESPORCMO :=((V_VRORCAMENTOMES / 100)* C_MAOOBRA.VRPERCDESPORCMO);
                    UPDATE ITEMORCAOP
                       SET VRORCAMENTOJAN = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '01', V_VRPERCDESPORCMO, NVL(VRORCAMENTOJAN, 0)),
                           VRORCAMENTOFEV = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '02', V_VRPERCDESPORCMO, NVL(VRORCAMENTOFEV, 0)),
                           VRORCAMENTOMAR = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '03', V_VRPERCDESPORCMO, NVL(VRORCAMENTOMAR, 0)),
                           VRORCAMENTOABR = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '04', V_VRPERCDESPORCMO, NVL(VRORCAMENTOABR, 0)),
                           VRORCAMENTOMAI = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '05', V_VRPERCDESPORCMO, NVL(VRORCAMENTOMAI, 0)),
                           VRORCAMENTOJUN = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '06', V_VRPERCDESPORCMO, NVL(VRORCAMENTOJUN, 0)),
                           VRORCAMENTOJUL = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '07', V_VRPERCDESPORCMO, NVL(VRORCAMENTOJUL, 0)),
                           VRORCAMENTOAGO = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '08', V_VRPERCDESPORCMO, NVL(VRORCAMENTOAGO, 0)),
                           VRORCAMENTOSET = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '09', V_VRPERCDESPORCMO, NVL(VRORCAMENTOSET, 0)),
                           VRORCAMENTOOUT = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '10', V_VRPERCDESPORCMO, NVL(VRORCAMENTOOUT, 0)),
                           VRORCAMENTONOV = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '11', V_VRPERCDESPORCMO, NVL(VRORCAMENTONOV, 0)),
                           VRORCAMENTODEZ = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '12', V_VRPERCDESPORCMO, NVL(VRORCAMENTODEZ, 0))
                     WHERE CDFILIAL       = P_CDFILIAL AND
                           IDTIPOORCAOP   = 'O' AND
                           NRANOORCAOP    = P_NRANOORCAOP AND
                           CDVERSORCAOP   = V_CDVERSAOORCA AND
                           CDDESPADMFAT   = C_MAOOBRA.CDDESPADMFAT;

                ELSE
                    UPDATE ITEMORCAOP
                       SET VRORCAMENTOJAN = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '01', C_MAOOBRA.VRDESPDIAMO, NVL(VRORCAMENTOJAN, 0
                       )),
                           VRORCAMENTOFEV = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '02', C_MAOOBRA.VRDESPDIAMO, NVL(VRORCAMENTOFEV, 0
                           )),
                           VRORCAMENTOMAR = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '03', C_MAOOBRA.VRDESPDIAMO, NVL(VRORCAMENTOMAR, 0
                           )),
                           VRORCAMENTOABR = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '04', C_MAOOBRA.VRDESPDIAMO, NVL(VRORCAMENTOABR, 0
                           )),
                           VRORCAMENTOMAI = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '05', C_MAOOBRA.VRDESPDIAMO, NVL(VRORCAMENTOMAI, 0
                           )),
                           VRORCAMENTOJUN = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '06', C_MAOOBRA.VRDESPDIAMO, NVL(VRORCAMENTOJUN, 0
                           )),
                           VRORCAMENTOJUL = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '07', C_MAOOBRA.VRDESPDIAMO, NVL(VRORCAMENTOJUL, 0
                           )),
                           VRORCAMENTOAGO = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '08', C_MAOOBRA.VRDESPDIAMO, NVL(VRORCAMENTOAGO, 0
                           )),
                           VRORCAMENTOSET = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '09', C_MAOOBRA.VRDESPDIAMO, NVL(VRORCAMENTOSET, 0
                           )),
                           VRORCAMENTOOUT = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '10', C_MAOOBRA.VRDESPDIAMO, NVL(VRORCAMENTOOUT, 0
                           )),
                           VRORCAMENTONOV = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '11', C_MAOOBRA.VRDESPDIAMO, NVL(VRORCAMENTONOV, 0
                           )),
                           VRORCAMENTODEZ = DECODE(TO_CHAR(V_DTDEPRMES, 'MM'), '12', C_MAOOBRA.VRDESPDIAMO, NVL(VRORCAMENTODEZ, 0
                           ))
                     WHERE CDFILIAL       = P_CDFILIAL AND
                           IDTIPOORCAOP   = 'O' AND
                           NRANOORCAOP    = P_NRANOORCAOP AND
                           CDVERSORCAOP   = V_CDVERSAOORCA AND
                           CDDESPADMFAT   = C_MAOOBRA.CDDESPADMFAT;

                END IF;
                END LOOP;

                V_NRMES := V_NRMES + 1;
            END LOOP;

            P_RETORNO   := 'OK';
        EXCEPTION
            WHEN OTHERS THEN
                BEGIN
                    P_RETORNO := SQLERRM(SQLCODE);
                    ROLLBACK;
                END;
        END;
    END IF;

    IF P_IDITEMORCA = 'T' THEN
        FOR C_TXADM IN(
            SELECT DISTINCT O.NRANOORCAOP,
                            O.CDVERSORCAOP,
                            O.CDFILIAL
              FROM ORCAMENTOOP    O,
                   ITEMORCAOP     I,
                   FILIAL         F,
                   FILIALOPEROP   FO,
                   SEGMERCADO     S,
                   REGIAO         R,
                   FILGRUPFILI    FG
             WHERE O.CDFILIAL              = P_CDFILIAL AND
                   O.NRANOORCAOP           = P_NRANOORCAOP AND
                   O.CDVERSORCAOP          = V_CDVERSAOORCA AND
                   F.CDSEGMER              = S.CDSEGMER(+)AND
                   F.CDREGIAO              = R.CDREGIAO(+)AND
                   O.CDFILIAL              = FG.CDFILIAL(+)AND
                   O.CDFILIAL              = F.CDFILIAL AND
                   NVL(F.IDCNTRRESC, 'N')  = 'N' AND
                   I.CDFILIAL              = O.CDFILIAL AND
                   I.NRANOORCAOP           = O.NRANOORCAOP AND
                   I.CDVERSORCAOP          = O.CDVERSORCAOP AND
                   I.CDFILIAL              = FO.CDFILIAL AND
                   FO.CDOPERADOR           = P_CDOPERADOR AND
                   IDTIPOORCAOP            = 'T'
        )LOOP
            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 AREAGERE   AR,
                       FILIAL     F
                 WHERE AR.CDAREAGERE(+)   = F.CDAREAGERE AND
                       F.CDFILIAL         = P_CDFILIAL
            )A,
                  (
                      SELECT 'A' CDCODIGO,
                             NVL(P.TAXAADMINMATRIZ, 0)TAXAADMINMATRIZ
                        FROM PARAMGESTAO P
                  )B
             WHERE A.CDCODIGO = B.CDCODIGO(+);

            SELECT SUM(VRORCAMENTOJAN),
                   SUM(VRORCAMENTOFEV),
                   SUM(VRORCAMENTOMAR),
                   SUM(VRORCAMENTOABR),
                   SUM(VRORCAMENTOMAI),
                   SUM(VRORCAMENTOJUN),
                   SUM(VRORCAMENTOJUL),
                   SUM(VRORCAMENTOAGO),
                   SUM(VRORCAMENTOSET),
                   SUM(VRORCAMENTOOUT),
                   SUM(VRORCAMENTONOV),
                   SUM(VRORCAMENTODEZ)
              INTO
                V_VRIMPORCAJAN,
                V_VRIMPORCAFEV,
                V_VRIMPORCAMAR,
                V_VRIMPORCAABR,
                V_VRIMPORCAMAI,
                V_VRIMPORCAJUN,
                V_VRIMPORCAJUL,
                V_VRIMPORCAAGO,
                V_VRIMPORCASET,
                V_VRIMPORCAOUT,
                V_VRIMPORCANOV,
                V_VRIMPORCADEZ
              FROM(
                SELECT NRANOORCAOP,
                       CDFILIAL,
                       NRSEQITEMORCA,
                       IDTRATIMPROP,
                       DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* NVL(VRORCAMENTOJAN, 0)VRORCAMENTOJAN,
                       DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* NVL(VRORCAMENTOFEV, 0)VRORCAMENTOFEV,
                       DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* NVL(VRORCAMENTOMAR, 0)VRORCAMENTOMAR,
                       DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* NVL(VRORCAMENTOABR, 0)VRORCAMENTOABR,
                       DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* NVL(VRORCAMENTOMAI, 0)VRORCAMENTOMAI,
                       DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* NVL(VRORCAMENTOJUN, 0)VRORCAMENTOJUN,
                       DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* NVL(VRORCAMENTOJUL, 0)VRORCAMENTOJUL,
                       DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* NVL(VRORCAMENTOAGO, 0)VRORCAMENTOAGO,
                       DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* NVL(VRORCAMENTOSET, 0)VRORCAMENTOSET,
                       DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* NVL(VRORCAMENTOOUT, 0)VRORCAMENTOOUT,
                       DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* NVL(VRORCAMENTONOV, 0)VRORCAMENTONOV,
                       DECODE(IDTRATIMPROP, 'A', 1, DECODE(IDTRATIMPROP, 'D', - 1, 0))* NVL(VRORCAMENTODEZ, 0)VRORCAMENTODEZ
                  FROM(
                    SELECT NVL(IP.IDTRATIMPROP, 'A')IDTRATIMPROP,
                           I.NRANOORCAOP,
                           I.CDFILIAL,
                           I.NRSEQITEMORCA,
                           I.VRORCAMENTOJAN,
                           I.VRORCAMENTOFEV,
                           I.VRORCAMENTOMAR,
                           I.VRORCAMENTOABR,
                           I.VRORCAMENTOMAI,
                           I.VRORCAMENTOJUN,
                           I.VRORCAMENTOJUL,
                           I.VRORCAMENTOAGO,
                           I.VRORCAMENTOSET,
                           I.VRORCAMENTOOUT,
                           I.VRORCAMENTONOV,
                           I.VRORCAMENTODEZ
                      FROM IMPOSTOORCAFIL   IO
                      LEFT JOIN(
                        SELECT TIPOPARAM_DRO(CDFILIAL)AS CDFILIAL,
                               CDIMPOSTO,
                               IDTRATIMPROP
                          FROM IMPOSTOROP
                         WHERE(CDFILIAL         = TIPOPARAM_DRO(P_CDFILIAL)OR
                               (P_CDFILIAL       = 'T' AND
                                CDFILIAL         = TIPOPARAM_DRO(CDFILIAL)))AND
                              (IDTPIMPOSTOROP   = DECODE(V_IDDETALHEFATUR, 'N', 'FS', 'FE'))
                    )IP ON(IP.CDIMPOSTO = IO.CDIMPOSTO)
                     INNER JOIN ITEMORCAOP       I ON((IO.CDFILIAL    = I.CDFILIAL)OR
                                                (IO.CDFILIAL    = 'T'))AND
                                               (IO.CDIMPOSTO   = I.CDIMPOSTO)
                     WHERE P_CDFILIAL       =(
                        CASE
                            WHEN P_CDFILIAL       = 'T' THEN
                                'T'
                            ELSE
                                I.CDFILIAL
                        END
                    )AND
                           (I.NRANOORCAOP    = P_NRANOORCAOP)AND
                           (I.CDVERSORCAOP   =(
                               SELECT MAX(CDVERSORCAOP)
                                 FROM ITEMORCAOP
                                WHERE NRANOORCAOP = P_NRANOORCAOP
                           ))AND
                           (I.IDTIPOORCAOP   = 'I')
                )
            );

            SELECT CDVERSORCAOP,
                   CDFILIAL,
                   IDTIPOORCAOP,
                   SUM(VRORCAMENTOJAN)+ DECODE(V_IDTIPOCALCTAXAADM, 'FATURAMENTO_BRUTO', 0, V_VRIMPORCAJAN),
                   SUM(VRORCAMENTOFEV)+ DECODE(V_IDTIPOCALCTAXAADM, 'FATURAMENTO_BRUTO', 0, V_VRIMPORCAFEV),
                   SUM(VRORCAMENTOMAR)+ DECODE(V_IDTIPOCALCTAXAADM, 'FATURAMENTO_BRUTO', 0, V_VRIMPORCAMAR),
                   SUM(VRORCAMENTOABR)+ DECODE(V_IDTIPOCALCTAXAADM, 'FATURAMENTO_BRUTO', 0, V_VRIMPORCAABR),
                   SUM(VRORCAMENTOMAI)+ DECODE(V_IDTIPOCALCTAXAADM, 'FATURAMENTO_BRUTO', 0, V_VRIMPORCAMAI),
                   SUM(VRORCAMENTOJUN)+ DECODE(V_IDTIPOCALCTAXAADM, 'FATURAMENTO_BRUTO', 0, V_VRIMPORCAJUN),
                   SUM(VRORCAMENTOJUL)+ DECODE(V_IDTIPOCALCTAXAADM, 'FATURAMENTO_BRUTO', 0, V_VRIMPORCAJUL),
                   SUM(VRORCAMENTOAGO)+ DECODE(V_IDTIPOCALCTAXAADM, 'FATURAMENTO_BRUTO', 0, V_VRIMPORCAAGO),
                   SUM(VRORCAMENTOSET)+ DECODE(V_IDTIPOCALCTAXAADM, 'FATURAMENTO_BRUTO', 0, V_VRIMPORCASET),
                   SUM(VRORCAMENTOOUT)+ DECODE(V_IDTIPOCALCTAXAADM, 'FATURAMENTO_BRUTO', 0, V_VRIMPORCAOUT),
                   SUM(VRORCAMENTONOV)+ DECODE(V_IDTIPOCALCTAXAADM, 'FATURAMENTO_BRUTO', 0, V_VRIMPORCANOV),
                   SUM(VRORCAMENTODEZ)+ DECODE(V_IDTIPOCALCTAXAADM, 'FATURAMENTO_BRUTO', 0, V_VRIMPORCADEZ)
              INTO
                V_CDVERSAOORCA,
                V_CDFILIAL,
                V_IDTIPOORCAOP,
                V_VRORCAMENTOJAN,
                V_VRORCAMENTOFEV,
                V_VRORCAMENTOMAR,
                V_VRORCAMENTOABR,
                V_VRORCAMENTOMAI,
                V_VRORCAMENTOJUN,
                V_VRORCAMENTOJUL,
                V_VRORCAMENTOAGO,
                V_VRORCAMENTOSET,
                V_VRORCAMENTOOUT,
                V_VRORCAMENTONOV,
                V_VRORCAMENTODEZ
              FROM ITEMORCAOP
             WHERE NRANOORCAOP    = P_NRANOORCAOP AND
                   CDVERSORCAOP   = V_CDVERSAOORCA AND
                   CDFILIAL       = P_CDFILIAL AND
                   IDTIPOORCAOP   = 'F'
             GROUP BY CDVERSORCAOP,
                      CDFILIAL,
                      IDTIPOORCAOP;

            V_MESINI    := 1;
            V_VRTXADM   := 0;
            WHILE(V_MESINI <= 12)LOOP
                SELECT DECODE(V_MESINI, 1, V_VRORCAMENTOJAN / 100 * V_TXADM, DECODE(V_MESINI, 2, V_VRORCAMENTOFEV / 100 * V_TXADM, DECODE(V_MESINI, 3, V_VRORCAMENTOMAR / 100 * V_TXADM, DECODE(V_MESINI, 4, V_VRORCAMENTOABR / 100 * V_TXADM, DECODE (V_MESINI, 5, V_VRORCAMENTOMAI / 100 * V_TXADM, DECODE(V_MESINI, 6, V_VRORCAMENTOJUN / 100 * V_TXADM, DECODE(V_MESINI, 7, V_VRORCAMENTOJUL / 100 * V_TXADM, DECODE(V_MESINI, 8, V_VRORCAMENTOAGO / 100 * V_TXADM, DECODE(V_MESINI, 9, V_VRORCAMENTOSET/ 100 * V_TXADM, DECODE(V_MESINI, 10, V_VRORCAMENTOOUT / 100 * V_TXADM, DECODE(V_MESINI, 11, V_VRORCAMENTONOV / 100 * V_TXADM, DECODE(V_MESINI, 12, V_VRORCAMENTODEZ / 100 * V_TXADM, 0))))))))))))
                  INTO V_VRTXADM
                  FROM DUAL;

                UPDATE ITEMORCAOP
                   SET VRORCAMENTOJAN = DECODE(V_MESINI, 1, V_VRTXADM, VRORCAMENTOJAN),
                       VRORCAMENTOFEV = DECODE(V_MESINI, 2, V_VRTXADM, VRORCAMENTOFEV),
                       VRORCAMENTOMAR = DECODE(V_MESINI, 3, V_VRTXADM, VRORCAMENTOMAR),
                       VRORCAMENTOABR = DECODE(V_MESINI, 4, V_VRTXADM, VRORCAMENTOABR),
                       VRORCAMENTOMAI = DECODE(V_MESINI, 5, V_VRTXADM, VRORCAMENTOMAI),
                       VRORCAMENTOJUN = DECODE(V_MESINI, 6, V_VRTXADM, VRORCAMENTOJUN),
                       VRORCAMENTOJUL = DECODE(V_MESINI, 7, V_VRTXADM, VRORCAMENTOJUL),
                       VRORCAMENTOAGO = DECODE(V_MESINI, 8, V_VRTXADM, VRORCAMENTOAGO),
                       VRORCAMENTOSET = DECODE(V_MESINI, 9, V_VRTXADM, VRORCAMENTOSET),
                       VRORCAMENTOOUT = DECODE(V_MESINI, 10, V_VRTXADM, VRORCAMENTOOUT),
                       VRORCAMENTONOV = DECODE(V_MESINI, 11, V_VRTXADM, VRORCAMENTONOV),
                       VRORCAMENTODEZ = DECODE(V_MESINI, 12, V_VRTXADM, VRORCAMENTODEZ)
                 WHERE CDFILIAL       = P_CDFILIAL AND
                       IDTIPOORCAOP   = 'T' AND
                       NRANOORCAOP    = P_NRANOORCAOP AND
                       CDVERSORCAOP   = V_CDVERSAOORCA;

                COMMIT;
                V_MESINI := V_MESINI + 1;
            END LOOP;

        END LOOP;
    END IF;

END;