CREATE OR REPLACE PROCEDURE PREPARA_ORCAMENTO(
   P_NRANOORCAOP    IN ORCAMENTOOP.NRANOORCAOP%TYPE,
   P_CDVERSORCAOP   IN ORCAMENTOOP.CDVERSORCAOP%TYPE,
   P_CDOPERADOR     IN VARCHAR,
   P_CDFILIAL       IN VARCHAR DEFAULT NULL,
   P_IDEXCLUI       IN VARCHAR DEFAULT 'N',
   P_LISTAOPER      IN VARCHAR DEFAULT 'N',
   P_LISTAFIL       IN VARCHAR DEFAULT 'N',
   P_ORCADETA       IN VARCHAR DEFAULT 'N',
   P_ITEMORCA       IN VARCHAR DEFAULT NULL,
   P_CDOPERLOGADO   IN VARCHAR DEFAULT NULL)
AS
   V_NRITEMORCA      VARCHAR2(10);
   V_FILIALPAR       VARCHAR2(4);
   V_CDFILCENTPME    VARCHAR2(4);
   V_IDFATUROP       VARCHAR2(2);
   V_IDSERVFATROP    CHAR;
   V_IDFILIALAGRROP  CHAR;
   V_IDUTPADRROPFIL  CHAR;
   V_IDUTPADRROP     CHAR;
   V_EXIST_MOFIL     INTEGER;
   V_EXIST_ORCFIL    INTEGER;
   V_EXIST_MOGER     INTEGER;
   V_TET_FILIMP      VARCHAR2(4);
   FIL_JA_USADA      VARCHAR2(4);
   V_ORCDESPENTROP   VARCHAR2(1);
   V_FILIAL          VARCHAR2(4);
   V_EXISTE          VARCHAR2(1);
   V_IDSTATUSORCA    VARCHAR2(1);
   V_NRITEMORCAGOP   NUMBER;
   V_NRORCAMENTOGOP  NUMBER;
   V_IDAPROVACAOGOP  VARCHAR2(30);
   V_IDPERIODOORCA   VARCHAR2(30);
   V_NRITORCAMESGOP  NUMBER;
   V_CDSERVFILI      VARCHAR2(05);
   C_ORCAMENTO_CDFILIAL VARCHAR2(04);
   V_CDTIPOOPER      VARCHAR2(02);
   V_CDIMPOSTOREC    VARCHAR2(02);
   V_CDIMPOSTOSOB    VARCHAR2(02);
   V_STATUSCONC      VARCHAR2(30);
   V_MENSAGEMLOG     VARCHAR2(100);
   V_NRLOGPREPARAORCAGOP NUMBER;
   V_IDUTILPERCFILGOP VARCHAR2(1);
    /* Variaveis para LOG*/
   V_NMTABELA      VARCHAR2(20);
   V_NMOPERADOR    VARCHAR2(40);
   V_TIPO          VARCHAR2(1);
   V_DESC          VARCHAR2(500);
   V_ITPESQ        VARCHAR2(40);
   V_OPERADOR      VARCHAR2(12);
   V_EXISTEORCA    VARCHAR2(1);
   V_UNIDADE       VARCHAR2(50);
   V_NMFILIAL      VARCHAR2(40);
   V_DSSERVERRDM   VARCHAR2(50);
   V_ITENS         VARCHAR2(2000);
   V_NRANOORCAGOP  VARCHAR2(4);
   V_NRPERIODOORCA VARCHAR2(13);
   V_IDEXCLUI      VARCHAR2(01);
   V_OUTRPERIODO   VARCHAR2(30);
   V_EXISTMAOOBRA  VARCHAR2(40);
   
   V_AUXEDIT       VARCHAR2(03);
   V_ERRO          VARCHAR2(04);

   CURSOR C_DESPENTRADA IS SELECT PD.CDTIPOOPER
                             FROM PERCITEMDETALGOP PD INNER JOIN PERCITEMGOP P ON PD.NRPERCITEMGOP = P.NRPERCITEMGOP
                            WHERE P.NRSEQITEMORCA = 7
                              AND V_IDUTILPERCFILGOP = 'N'
                            UNION
                           SELECT PDF.CDTIPOOPER
                             FROM PERCITFILGOP PF LEFT JOIN PERCITDETALFILGOP PDF ON PDF.NRPERCITFILGOP = PF.NRPERCITFILGOP
                            WHERE PDF.CDFILIAL = C_ORCAMENTO_CDFILIAL
                              AND PF.NRSEQITEMORCA = 7
                              AND V_IDUTILPERCFILGOP = 'S'
                            MINUS
                           SELECT CDTIPOOPER
                             FROM ITEMORCAGOP
                            WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                              AND CDFILIAL = C_ORCAMENTO_CDFILIAL
                              AND IDTIPOORCAOP = 'DESPENTRADA'
                              AND IDENTRSAIDOP = 'E';

   CURSOR C_IMPOSTOARECUPERAR IS SELECT CDIMPOSTO
                                   FROM DUAL LEFT JOIN (SELECT C_ORCAMENTO_CDFILIAL CDFILIAL, PD.CDIMPOSTO
                                                          FROM PERCITEMDETALGOP PD INNER JOIN PERCITEMGOP P ON PD.NRPERCITEMGOP = P.NRPERCITEMGOP
                                                         WHERE P.NRSEQITEMORCA = 8
                                                           AND V_IDUTILPERCFILGOP <> 'S'
                                                         UNION
                                                        SELECT C_ORCAMENTO_CDFILIAL CDFILIAL, PDF.CDIMPOSTO
                                                          FROM PERCITFILGOP PF LEFT JOIN PERCITDETALFILGOP PDF ON PDF.NRPERCITFILGOP = PF.NRPERCITFILGOP
                                                         WHERE PDF.CDFILIAL = C_ORCAMENTO_CDFILIAL
                                                           AND PF.NRSEQITEMORCA = 8
                                                           AND V_IDUTILPERCFILGOP = 'S') O ON O.CDFILIAL = C_ORCAMENTO_CDFILIAL
                                  MINUS
                                 SELECT CDIMPOSTO
                                   FROM ITEMORCAGOP
                                  WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                    AND CDFILIAL = C_ORCAMENTO_CDFILIAL
                                    AND IDTIPOORCAOP = 'IMPOSTOARECUPERAR';

   CURSOR C_IMPOSTOSOBLUCRO IS SELECT CDIMPOSTO
                                 FROM DUAL LEFT JOIN (SELECT C_ORCAMENTO_CDFILIAL CDFILIAL, PD.CDIMPOSTO
                                                        FROM PERCITEMDETALGOP PD INNER JOIN PERCITEMGOP P ON PD.NRPERCITEMGOP = P.NRPERCITEMGOP
                                                       WHERE P.NRSEQITEMORCA = 9
                                                         AND V_IDUTILPERCFILGOP <> 'S'
                                                       UNION
                                                      SELECT C_ORCAMENTO_CDFILIAL CDFILIAL, PDF.CDIMPOSTO
                                                        FROM PERCITFILGOP PF LEFT JOIN PERCITDETALFILGOP PDF ON PDF.NRPERCITFILGOP = PF.NRPERCITFILGOP
                                                       WHERE PDF.CDFILIAL = C_ORCAMENTO_CDFILIAL
                                                         AND PF.NRSEQITEMORCA = 9
                                                         AND V_IDUTILPERCFILGOP = 'S') O ON O.CDFILIAL = C_ORCAMENTO_CDFILIAL
                                MINUS
                               SELECT CDIMPOSTO
                                 FROM ITEMORCAGOP
                                WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                  AND CDFILIAL = C_ORCAMENTO_CDFILIAL
                                  AND IDTIPOORCAOP = 'IMPOSTOSOBLUCRO';

   FUNCTION PERMITE_EDICAO(P_NRITEMORCAOP IN NUMBER) RETURN VARCHAR2 AS
     V_PENDENTE VARCHAR2(3);
   BEGIN
      SELECT CASE WHEN MIN(X.STATUS) = 'PENDENTE' THEN 'SIM' ELSE 'NAO' END
        INTO V_PENDENTE
        FROM (SELECT NVL(MAX(NVL(CO.IDSTATUS,'PENDENTE')), 'PENDENTE') STATUS
                FROM CALENPREORCAGOP CA CROSS JOIN ITEMORCAGOP I
                                        INNER JOIN ORCAMENTOGOP O ON CA.NRANOORCAGOP = O.NRANOORCAGOP
                                                                 AND CA.IDPERIODOORCA = O.IDPERIODOORCA
                                                                 AND CA.NRPERIODOORCA = O.NRPERIODOORCA
                                        INNER JOIN CONCPREORCAGOP CO ON CO.NRCALENPREORCAGOP = CA.NRCALENPREORCAGOP
                                                                    AND CO.CDFILIAL = O.CDFILIAL
                                                                    AND CO.NRORCAMENTOGOP = O.NRORCAMENTOGOP
                                                                    AND CO.IDTIPOORCAOP = I.IDTIPOORCAOP
               WHERE I.NRITEMORCAOP = P_NRITEMORCAOP
               UNION
              SELECT NVL(MAX(NVL(CO.IDSTATUS,'PENDENTE')), 'PENDENTE') STATUS
                FROM ORCAMENTOGOP OG INNER JOIN ITEMORCAGOP I ON I.NRORCAMENTOGOP = OG.NRORCAMENTOGOP
                                     INNER JOIN CONCPREORCAGOP CO ON CO.NRORCAMENTOGOP = OG.NRORCAMENTOGOP
                                                                 AND CO.CDFILIAL = OG.CDFILIAL
                                                                 AND CO.IDTIPOORCAOP = I.IDTIPOORCAOP
               WHERE I.NRITEMORCAOP = P_NRITEMORCAOP
                 AND NOT EXISTS (SELECT 1
                                   FROM CALENPREORCAGOP CA
                                  WHERE CA.NRANOORCAGOP = OG.NRANOORCAGOP
                                    AND CA.IDPERIODOORCA = OG.IDPERIODOORCA
                                    AND CA.NRPERIODOORCA = OG.NRPERIODOORCA)) X;      
   
      RETURN V_PENDENTE;
   END;

   FUNCTION PREPARA_ESTRUTURA(P_CDFILIAL IN VARCHAR2, P_NRANOORCAOP IN VARCHAR2, P_IDPERIODOORCA IN VARCHAR2,
                              P_NRPERIODOORCA IN NUMBER, P_EXCLUI IN VARCHAR2) RETURN VARCHAR2
   AS 
     V_OUTRPERIODO    VARCHAR2(30);
     V_PERIODOATUAL   VARCHAR2(30);
     V_NRORCAMENTOGOP NUMBER;
     V_EDITAVEL       VARCHAR2(20);
     V_NRLOGPREPARAORCAGOP NUMBER;
     V_RETORNO        VARCHAR2(02);
   BEGIN
      V_RETORNO := 'OK';
      
      SELECT MAX(OG.NRORCAMENTOGOP)
        INTO V_OUTRPERIODO
        FROM ORCAMENTOGOP OG
       WHERE OG.CDFILIAL = P_CDFILIAL
         AND OG.NRANOORCAGOP = P_NRANOORCAOP
         AND ((OG.IDPERIODOORCA <> P_IDPERIODOORCA) AND (P_IDPERIODOORCA = 'ANUAL')
            OR ((OG.IDPERIODOORCA <> P_IDPERIODOORCA AND OG.NRPERIODOORCA <> P_NRPERIODOORCA) AND (P_IDPERIODOORCA <> 'ANUAL')));

      IF V_OUTRPERIODO IS NOT NULL THEN
         FOR C_PERIODO IN (SELECT I.NRITEMORCAOP, I.IDTIPOORCAOP, O.IDPERIODOORCA,
                                  (SELECT TRUNC(MIN(M.DTORCAMENTO),'MM')
                                     FROM ITORCAMESGOP M
                                    WHERE M.NRITEMORCAOP = I.NRITEMORCAOP) DTORCAMIN,
                                  TRUNC(DECODE(P.IDUTILDTPROC, 'S', NVL(PF.DTPROCESSA, SYSDATE), SYSDATE),'MM') DTPROC
                             FROM ORCAMENTOGOP O INNER JOIN ITEMORCAGOP I ON O.NRORCAMENTOGOP = I.NRORCAMENTOGOP                                 
                                                 INNER JOIN PARAMFILIAL PF ON PF.CDFILIAL = O.CDFILIAL
                                                 CROSS JOIN PARAMGERAL P
                            WHERE O.NRORCAMENTOGOP = V_OUTRPERIODO) LOOP
                            
            V_EDITAVEL := PERMITE_EDICAO(C_PERIODO.NRITEMORCAOP);

            IF V_EDITAVEL = 'SIM' THEN
               IF (P_EXCLUI = 'S') AND (C_PERIODO.DTORCAMIN >= C_PERIODO.DTPROC) THEN
                  DELETE FROM ITDETALORCAMESGOP
                        WHERE NRITORCAMESGOP IN (SELECT M.NRITORCAMESGOP
                                                   FROM ITORCAMESGOP M
                                                  WHERE M.NRITEMORCAOP = C_PERIODO.NRITEMORCAOP);
                                                    
                  DELETE FROM ITORCAMESGOP
                        WHERE NRITEMORCAOP = C_PERIODO.NRITEMORCAOP;
                          
                  DELETE FROM ITEMORCAGOP
                        WHERE NRITEMORCAOP = C_PERIODO.NRITEMORCAOP;
                        
                  DELETE FROM ORCAMENTOGOP
                        WHERE NRORCAMENTOGOP = V_OUTRPERIODO;
                        
                  V_RETORNO := 'NO';
               ELSIF P_EXCLUI <> 'S' THEN
                  V_RETORNO := 'OK';
                  NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);

                  INSERT INTO LOGPREPARAORCAGOP
                              (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, 
                              DSMOTIVO)
                       SELECT V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, P_IDPERIODOORCA, P_NRPERIODOORCA, P_CDFILIAL, 
                              'ORCA_'||C_PERIODO.IDPERIODOORCA||'_GERADA'
                         FROM DUAL
                        WHERE NOT EXISTS (SELECT NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, 
                                                 DSMOTIVO
                                            FROM LOGPREPARAORCAGOP
                                           WHERE NRANOORCAGOP = P_NRANOORCAOP
                                             AND IDPERIODOORCA = P_IDPERIODOORCA
                                             AND NRPERIODOORCA = P_NRPERIODOORCA
                                             AND CDFILIAL = P_CDFILIAL);
               ELSE
                  V_RETORNO := 'OK';
                  NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);

                  INSERT INTO LOGPREPARAORCAGOP
                              (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, 
                              DSMOTIVO)
                       SELECT V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, P_IDPERIODOORCA, P_NRPERIODOORCA, P_CDFILIAL, 
                              'ORCA_'||C_PERIODO.IDPERIODOORCA||'_DTPROC'
                         FROM DUAL
                        WHERE NOT EXISTS (SELECT NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, 
                                                 DSMOTIVO
                                            FROM LOGPREPARAORCAGOP
                                           WHERE NRANOORCAGOP = P_NRANOORCAOP
                                             AND IDPERIODOORCA = P_IDPERIODOORCA
                                             AND NRPERIODOORCA = P_NRPERIODOORCA
                                             AND CDFILIAL = P_CDFILIAL);
               END IF;
            ELSE
               V_RETORNO := 'OK';
               NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);

               INSERT INTO LOGPREPARAORCAGOP
                           (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, 
                           DSMOTIVO)
                    VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, P_IDPERIODOORCA, P_NRPERIODOORCA, P_CDFILIAL, 
                           'ORCA_'||C_PERIODO.IDPERIODOORCA||'_'||C_PERIODO.IDTIPOORCAOP||'_CONC');
            END IF;
         END LOOP;
         IF V_RETORNO = 'NO' THEN
            V_RETORNO := PREPARA_ESTRUTURA(P_CDFILIAL, P_NRANOORCAOP, P_IDPERIODOORCA, P_NRPERIODOORCA, P_EXCLUI);
         ELSE
            V_RETORNO := 'OK';
         END IF;
      END IF;
      RETURN V_RETORNO;
   END;

   PROCEDURE PREPARA_MES(P_CDFILIAL IN VARCHAR2, P_PERIODO IN NUMBER, P_NRITEMORCAOP IN NUMBER, P_DETALHA IN VARCHAR2,
                         P_ITEM IN NUMBER, P_CDDESPPREV IN VARCHAR2 DEFAULT NULL, P_IDUTILPERCFILGOP IN VARCHAR2 DEFAULT NULL,
                         P_CDORCAMENTOMO IN VARCHAR2 DEFAULT NULL, P_CDFILIALMO IN VARCHAR2 DEFAULT NULL, P_DATAPROC IN DATE DEFAULT NULL,
                         P_EXCLUI VARCHAR2 DEFAULT 'N')
   AS
     V_NRITORCAMESGOP    NUMBER;
     V_ITDETALORCAMESGOP NUMBER;
     V_DESPESA           VARCHAR2(30);
     V_ORCAMENTO         VARCHAR2(30);
     V_CDSERVFILI        VARCHAR2(05);
     V_AUX               VARCHAR2(20);
     V_CONSSERVFILI      VARCHAR2(20);
     V_CONSPRODUTO       VARCHAR2(20);
     V_EXISTDETPROD      VARCHAR2(20);
     V_EXISTDETSERV      VARCHAR2(20);
   BEGIN
      IF P_ITEM = 2 THEN
         V_DESPESA   := 'CONSUMO';
         V_ORCAMENTO := 'CONSUMO';
      ELSIF P_ITEM = 3 THEN
         V_ORCAMENTO := 'MAOOBRA';         
      ELSIF P_ITEM = 5 THEN
         V_DESPESA   := 'MATRIZ';
         V_ORCAMENTO := 'DESPMATRIZ';
      ELSIF P_ITEM = 6 THEN
         V_DESPESA   := 'DIVERSA';
         V_ORCAMENTO := 'DESPDIVERSA';
      ELSIF P_ITEM = 7 THEN
         V_DESPESA   := 'LANCENTRADA';
         V_ORCAMENTO := 'DESPENTRADA';
      END IF;
      
      V_AUX := 'INICIO';
      FOR C_MES IN (SELECT ADD_MONTHS(TO_DATE(CASE WHEN NVL(F.NRDIAINIORCAGOP,P.NRDIAINIORCA) > TO_NUMBER(TO_CHAR(LAST_DAY(D.DTMES),'DD'))
                                                   THEN TO_CHAR(LAST_DAY(D.DTMES),'DD')
                                                   ELSE LPAD(NVL(F.NRDIAINIORCAGOP,P.NRDIAINIORCA),2,'0')
                                              END ||'/'||TO_CHAR(D.DTMES,'MM')||'/'||P_NRANOORCAOP,'DD/MM/YYYY'), NVL(P.NRMESINIORCA,'01')-1) DT
                      FROM PERIODOORCAGOP PO, FILIAL F, PARAMORCAOPERAC P, DATA D
                     WHERE F.CDFILIAL = P_CDFILIAL
                       AND PO.NRPERIODO = P_PERIODO
                       AND PO.CDFILIAL = NVL((SELECT MAX(CDFILIAL) CDFILIAL FROM PERIODOORCAGOP WHERE CDFILIAL = P_CDFILIAL),'P')
                       AND D.DTMES BETWEEN TRUNC(PO.DTINIPERIORCA,'MM') AND TRUNC(PO.DTFIMPERIORCA+1,'MM')-1
                       AND TO_DATE(TO_CHAR(D.DTMES,'DD/MM/')||P_NRANOORCAOP,'DD/MM/YYYY') >= TRUNC(P_DATAPROC,'MM')
                     GROUP BY ADD_MONTHS(TO_DATE(CASE WHEN NVL(F.NRDIAINIORCAGOP,P.NRDIAINIORCA) > TO_NUMBER(TO_CHAR(LAST_DAY(D.DTMES),'DD'))
                                                      THEN TO_CHAR(LAST_DAY(D.DTMES),'DD')
                                                      ELSE LPAD(NVL(F.NRDIAINIORCAGOP,P.NRDIAINIORCA),2,'0')
                                                 END ||'/'||TO_CHAR(D.DTMES,'MM')||'/'||P_NRANOORCAOP,'DD/MM/YYYY'), NVL(P.NRMESINIORCA,'01')-1)
                     ORDER BY DT) LOOP
      
         SELECT MAX(M.NRITORCAMESGOP)
           INTO V_NRITORCAMESGOP
           FROM ITORCAMESGOP M LEFT JOIN ITDETALORCAMESGOP D ON D.NRITORCAMESGOP = M.NRITORCAMESGOP
                                                            AND ((D.CDFILIALMO = P_CDFILIALMO
                                                                  AND D.CDORCAMENTOMO = P_CDORCAMENTOMO
                                                                  AND D.CDDESPADMFAT = P_CDDESPPREV))
          WHERE M.NRITEMORCAOP = P_NRITEMORCAOP
            AND M.DTORCAMENTO = C_MES.DT;

         IF V_NRITORCAMESGOP IS NULL THEN
            NOVO_CODIGO('ITORCAMESGOP', V_NRITORCAMESGOP);
            
            INSERT INTO ITORCAMESGOP
                        (NRITEMORCAOP, NRITORCAMESGOP, DTORCAMENTO, IDALTMANUAL, VRORCAMENTO,
                         VRREAJUSTE, VRPERCENTUAL, QTORCAMENTO)
                 VALUES (P_NRITEMORCAOP, V_NRITORCAMESGOP, C_MES.DT, 'N', 0,
                         0, 0, 0);
          ELSIF P_EXCLUI = 'S' THEN
             UPDATE ITORCAMESGOP
                SET IDALTMANUAL = 'N',
                    VRORCAMENTO = 0,
                    VRREAJUSTE = 0,
                    VRPERCENTUAL = 0,
                    QTORCAMENTO = 0
              WHERE NRITORCAMESGOP = V_NRITORCAMESGOP;
                  
             UPDATE ITDETALORCAMESGOP
                SET VRORCAMENTO = 0,
                   VRPERCENTUAL = 0,
                   QTORCAMENTO = 0
             WHERE NRITORCAMESGOP = V_NRITORCAMESGOP;
         END IF;

         IF P_DETALHA = 'S' THEN
            /* FATURAMENTO */
            IF P_ITEM = 0 THEN
               FOR C_SERVICO IN (SELECT CDSERVFILI
                                   FROM (SELECT SC.CDSERVFILI
                                           FROM SERVICOROP SC, SERVICO S
                                          WHERE SC.CDFILIAL = TIPOPARAM_DRO(C_ORCAMENTO_CDFILIAL)
                                            AND IDTPSERVICOROP = 'E'
                                            AND V_IDSERVFATROP <> 'S'
                                            AND SC.CDSERVFILI = S.CDSERVFILI
                                            AND S.CDFILIAL = C_ORCAMENTO_CDFILIAL
                                          UNION
                                         SELECT S.CDSERVFILI
                                           FROM SERVICO S
                                          WHERE S.CDFILIAL = C_ORCAMENTO_CDFILIAL
                                            AND V_IDSERVFATROP = 'S'
                                            AND (QTCOMESTISEG IS NOT NULL OR
                                                 QTCOMESTITER IS NOT NULL OR
                                                 QTCOMESTIQUA IS NOT NULL OR
                                                 QTCOMESTIQUI IS NOT NULL OR
                                                 QTCOMESTISEX IS NOT NULL OR
                                                 QTCOMESTISAB IS NOT NULL OR
                                                 QTCOMESTIDOM IS NOT NULL OR
                                                 QTCOMESTIFER IS NOT NULL)) 
                                  MINUS 
                                 SELECT IT.CDSERVFILI
                                   FROM ITDETALORCAMESGOP IT
                                  WHERE IT.NRITORCAMESGOP = V_NRITORCAMESGOP) LOOP
                        
                  NOVO_CODIGO('ITDETALORCAMESGOP', V_ITDETALORCAMESGOP);

                  INSERT INTO ITDETALORCAMESGOP
                              (NRITORCAMESGOP, NRITDETALORCAMESGOP, VRORCAMENTO, VRPERCENTUAL, QTORCAMENTO, CDSERVFILI)
                       VALUES (V_NRITORCAMESGOP, V_ITDETALORCAMESGOP, 0, 0, 0, C_SERVICO.CDSERVFILI);
               END LOOP;

               IF P_EXCLUI = 'S' THEN               
                  DELETE FROM ITDETALORCAMESGOP
                        WHERE NRITORCAMESGOP = V_NRITORCAMESGOP
                          AND CDSERVFILI NOT IN (SELECT SC.CDSERVFILI
                                                   FROM SERVICOROP SC, SERVICO S
                                                  WHERE SC.CDFILIAL = TIPOPARAM_DRO(C_ORCAMENTO_CDFILIAL)
                                                    AND IDTPSERVICOROP = 'E'
                                                    AND V_IDSERVFATROP <> 'S'
                                                    AND SC.CDSERVFILI = S.CDSERVFILI
                                                    AND S.CDFILIAL = C_ORCAMENTO_CDFILIAL
                                                  UNION
                                                 SELECT S.CDSERVFILI
                                                   FROM SERVICO S
                                                  WHERE S.CDFILIAL = C_ORCAMENTO_CDFILIAL
                                                    AND V_IDSERVFATROP = 'S'
                                                    AND (QTCOMESTISEG IS NOT NULL OR
                                                         QTCOMESTITER IS NOT NULL OR
                                                         QTCOMESTIQUA IS NOT NULL OR
                                                         QTCOMESTIQUI IS NOT NULL OR
                                                         QTCOMESTISEX IS NOT NULL OR
                                                         QTCOMESTISAB IS NOT NULL OR
                                                         QTCOMESTIDOM IS NOT NULL OR
                                                         QTCOMESTIFER IS NOT NULL));
               END IF;
            /* CONSUMO */
            ELSIF P_ITEM = 2 THEN
               V_CONSSERVFILI := NULL;
               V_CONSPRODUTO  := NULL;
               V_EXISTDETSERV := NULL;
               V_EXISTDETPROD := NULL;
               
               SELECT MAX(IT.CDSERVFILI)
                 INTO V_CONSSERVFILI
                 FROM ITDETALORCAMESGOP IT
                WHERE IT.NRITORCAMESGOP = V_NRITORCAMESGOP;
                
               SELECT MAX(CDPRODUTO)
                 INTO V_EXISTDETPROD
                 FROM (SELECT PD.CDPRODUTO
                         FROM PERCITFILGOP P INNER JOIN PERCITDETALFILGOP PD ON P.NRPERCITFILGOP = PD.NRPERCITFILGOP
                        WHERE P.CDFILIAL = P_CDFILIAL
                          AND P.NRSEQITEMORCA = P_ITEM
                          AND P_IDUTILPERCFILGOP = 'S'
                        UNION
                       SELECT PD.CDPRODUTO
                         FROM PERCITEMGOP P INNER JOIN PERCITEMDETALGOP PD ON P.NRPERCITEMGOP = PD.NRPERCITEMGOP
                        WHERE P.NRSEQITEMORCA = P_ITEM
                          AND P_IDUTILPERCFILGOP = 'N');
                
               IF V_EXISTDETPROD IS NOT NULL AND (V_CONSSERVFILI IS NULL) THEN
                  /* PRODUTO */
                  FOR C_DETALHAMES IN (SELECT CDPRODUTO
                                         FROM (SELECT PD.CDPRODUTO
                                                 FROM PERCITFILGOP P INNER JOIN PERCITDETALFILGOP PD ON P.NRPERCITFILGOP = PD.NRPERCITFILGOP
                                                WHERE P.CDFILIAL = P_CDFILIAL
                                                  AND P.NRSEQITEMORCA = P_ITEM
                                                  AND P_IDUTILPERCFILGOP = 'S'
                                                UNION
                                               SELECT PD.CDPRODUTO
                                                 FROM PERCITEMGOP P INNER JOIN PERCITEMDETALGOP PD ON P.NRPERCITEMGOP = PD.NRPERCITEMGOP
                                                WHERE P.NRSEQITEMORCA = P_ITEM
                                                  AND P_IDUTILPERCFILGOP = 'N')
                                        MINUS
                                       SELECT IT.CDPRODUTO
                                         FROM ITEMORCAGOP I INNER JOIN ITORCAMESGOP IM ON I.NRITEMORCAOP = IM.NRITEMORCAOP
                                                            INNER JOIN ITDETALORCAMESGOP IT ON IM.NRITORCAMESGOP = IT.NRITORCAMESGOP
                                        WHERE IT.NRITORCAMESGOP = V_NRITORCAMESGOP
                                          AND I.IDTIPOORCAOP = V_ORCAMENTO) LOOP

                     NOVO_CODIGO('ITDETALORCAMESGOP', V_ITDETALORCAMESGOP);
                     INSERT INTO ITDETALORCAMESGOP
                                 (NRITORCAMESGOP, NRITDETALORCAMESGOP, VRORCAMENTO, VRPERCENTUAL, QTORCAMENTO, CDPRODUTO)
                          VALUES (V_NRITORCAMESGOP, V_ITDETALORCAMESGOP, 0, 0, 0, C_DETALHAMES.CDPRODUTO);
               
                     IF P_EXCLUI = 'S' THEN
                        DELETE FROM ITDETALORCAMESGOP IT
                              WHERE IT.NRITORCAMESGOP = V_NRITORCAMESGOP
                                AND (IT.CDPRODUTO NOT IN (SELECT PD.CDPRODUTO
                                                           FROM PERCITFILGOP P INNER JOIN PERCITDETALFILGOP PD 
                                                                                       ON P.NRPERCITFILGOP = PD.NRPERCITFILGOP
                                                          WHERE P.CDFILIAL = P_CDFILIAL
                                                            AND P.NRSEQITEMORCA = P_ITEM
                                                            AND P_IDUTILPERCFILGOP = 'S'
                                                          UNION
                                                         SELECT PD.CDPRODUTO
                                                           FROM PERCITEMGOP P INNER JOIN PERCITEMDETALGOP PD
                                                                                      ON P.NRPERCITEMGOP = PD.NRPERCITEMGOP
                                                          WHERE P.NRSEQITEMORCA = P_ITEM
                                                            AND P_IDUTILPERCFILGOP = 'N')
                                 OR IT.CDPRODUTO IS NULL);
                     END IF;
                  END LOOP;
               ELSIF V_EXISTDETPROD IS NOT NULL THEN
                  V_NRLOGPREPARAORCAGOP := NULL;
                  
                  IF V_AUX <> ('CONSUMO'||P_CDFILIAL||'SERVICO') THEN
                     NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);

                     INSERT INTO LOGPREPARAORCAGOP
                                 (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                          VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, P_CDFILIAL, 'EXIST_ORCA_CONSUMO_SERVICO');                     
                       
                     V_AUX := 'CONSUMO'||P_CDFILIAL||'SERVICO';
                  END IF;
               END IF;

               /* SERVICO */
               IF V_EXISTDETPROD IS NULL THEN
                  SELECT MAX(IT.CDPRODUTO)
                    INTO V_CONSPRODUTO
                    FROM ITDETALORCAMESGOP IT
                   WHERE IT.NRITORCAMESGOP = V_NRITORCAMESGOP;
                  
                  IF (P_EXCLUI = 'S') OR (V_CONSPRODUTO IS NULL) THEN               
                     FOR C_SERVICO IN (SELECT CDSERVFILI
                                         FROM (SELECT SC.CDSERVFILI
                                                 FROM SERVICOROP SC, SERVICO S
                                                WHERE SC.CDFILIAL = TIPOPARAM_DRO(C_ORCAMENTO_CDFILIAL)
                                                  AND IDTPSERVICOROP = 'E'
                                                  AND V_IDSERVFATROP <> 'S'
                                                  AND SC.CDSERVFILI = S.CDSERVFILI
                                                  AND S.CDFILIAL = C_ORCAMENTO_CDFILIAL
                                                UNION
                                               SELECT S.CDSERVFILI
                                                 FROM SERVICO S
                                                WHERE S.CDFILIAL = C_ORCAMENTO_CDFILIAL
                                                  AND V_IDSERVFATROP = 'S'
                                                  AND (QTCOMESTISEG IS NOT NULL OR
                                                       QTCOMESTITER IS NOT NULL OR
                                                       QTCOMESTIQUA IS NOT NULL OR
                                                       QTCOMESTIQUI IS NOT NULL OR
                                                       QTCOMESTISEX IS NOT NULL OR
                                                       QTCOMESTISAB IS NOT NULL OR
                                                       QTCOMESTIDOM IS NOT NULL OR
                                                       QTCOMESTIFER IS NOT NULL)) 
                                        MINUS 
                                       SELECT IT.CDSERVFILI
                                         FROM ITDETALORCAMESGOP IT
                                        WHERE IT.NRITORCAMESGOP = V_NRITORCAMESGOP) LOOP
                        
                        NOVO_CODIGO('ITDETALORCAMESGOP', V_ITDETALORCAMESGOP);

                        INSERT INTO ITDETALORCAMESGOP
                                    (NRITORCAMESGOP, NRITDETALORCAMESGOP, VRORCAMENTO, VRPERCENTUAL, QTORCAMENTO, CDSERVFILI)
                             VALUES (V_NRITORCAMESGOP, V_ITDETALORCAMESGOP, 0, 0, 0, C_SERVICO.CDSERVFILI);
                     END LOOP;
                  
                     IF P_EXCLUI = 'S' THEN                     
                        DELETE FROM ITDETALORCAMESGOP
                              WHERE NRITORCAMESGOP = V_NRITORCAMESGOP
                                AND (CDSERVFILI NOT IN (SELECT SC.CDSERVFILI
                                                         FROM SERVICOROP SC, SERVICO S
                                                        WHERE SC.CDFILIAL = TIPOPARAM_DRO(C_ORCAMENTO_CDFILIAL)
                                                          AND IDTPSERVICOROP = 'E'
                                                          AND V_IDSERVFATROP <> 'S'
                                                          AND SC.CDSERVFILI = S.CDSERVFILI
                                                          AND S.CDFILIAL = C_ORCAMENTO_CDFILIAL
                                                        UNION
                                                       SELECT S.CDSERVFILI
                                                         FROM SERVICO S
                                                        WHERE S.CDFILIAL = C_ORCAMENTO_CDFILIAL
                                                          AND V_IDSERVFATROP = 'S'
                                                          AND (QTCOMESTISEG IS NOT NULL OR
                                                               QTCOMESTITER IS NOT NULL OR
                                                               QTCOMESTIQUA IS NOT NULL OR
                                                               QTCOMESTIQUI IS NOT NULL OR
                                                               QTCOMESTISEX IS NOT NULL OR
                                                               QTCOMESTISAB IS NOT NULL OR
                                                               QTCOMESTIDOM IS NOT NULL OR
                                                               QTCOMESTIFER IS NOT NULL))
                                 OR CDSERVFILI IS NULL);
                     END IF;
                  ELSE
                     V_NRLOGPREPARAORCAGOP := NULL;
                     
                     IF V_AUX <> ('CONSUMO'||P_CDFILIAL||'PRODUTO') THEN
                        NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);

                        INSERT INTO LOGPREPARAORCAGOP
                                    (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                             VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, P_CDFILIAL, 'EXIST_ORCA_CONSUMO_PRODUTO');
                     
                        V_AUX := 'CONSUMO'||P_CDFILIAL||'PRODUTO';
                     END IF;
                  END IF;
               END IF;
            /* MAO OBRA */
            ELSIF P_ITEM IN (3) THEN
               IF P_IDUTILPERCFILGOP = 'S' THEN
                  SELECT MAX(PD.CDORCAMENTOMO)
                    INTO V_AUX
                    FROM PERCITFILGOP P INNER JOIN PERCITDETALFILGOP PD ON P.NRPERCITFILGOP = PD.NRPERCITFILGOP
                                        LEFT  JOIN DESPORCAMO DO ON DO.CDORCAMENTOMO = PD.CDORCAMENTOMO
                                                                AND DO.CDFILIAL = PD.CDFILIALMO
                   WHERE P.CDFILIAL = P_CDFILIAL
                     AND P.NRSEQITEMORCA = P_ITEM
                     AND PD.CDORCAMENTOMO = P_CDORCAMENTOMO
                     AND PD.CDFILIALMO = P_CDFILIALMO;

                  FOR C_DETALHAMESFIL IN (SELECT DISTINCT DO.CDDESPADMFAT, PD.CDFILIALMO, PD.CDORCAMENTOMO
                                            FROM PERCITFILGOP P INNER JOIN PERCITDETALFILGOP PD ON P.NRPERCITFILGOP = PD.NRPERCITFILGOP
                                                                LEFT  JOIN DESPORCAMO DO ON DO.CDORCAMENTOMO = PD.CDORCAMENTOMO
                                                                                        AND DO.CDFILIAL = PD.CDFILIALMO
                                           WHERE P.CDFILIAL = P_CDFILIAL
                                             AND P.NRSEQITEMORCA = P_ITEM
                                             AND PD.CDORCAMENTOMO = P_CDORCAMENTOMO
                                             AND PD.CDFILIALMO = P_CDFILIALMO
                                           MINUS
                                          SELECT IT.CDDESPADMFAT, IT.CDFILIALMO, IT.CDORCAMENTOMO
                                            FROM ITEMORCAGOP I INNER JOIN ITORCAMESGOP IM ON I.NRITEMORCAOP = IM.NRITEMORCAOP
                                                               INNER JOIN ITDETALORCAMESGOP IT ON IM.NRITORCAMESGOP = IT.NRITORCAMESGOP
                                           WHERE IT.NRITORCAMESGOP = V_NRITORCAMESGOP
                                             AND I.IDTIPOORCAOP = V_ORCAMENTO
                                             AND IT.CDORCAMENTOMO = P_CDORCAMENTOMO
                                             AND IT.CDFILIALMO = P_CDFILIALMO) LOOP

                     NOVO_CODIGO('ITDETALORCAMESGOP', V_ITDETALORCAMESGOP);

                     INSERT INTO ITDETALORCAMESGOP
                                 (NRITORCAMESGOP, NRITDETALORCAMESGOP, VRORCAMENTO, VRPERCENTUAL, QTORCAMENTO, CDDESPADMFAT, CDFILIALMO, CDORCAMENTOMO)
                          VALUES (V_NRITORCAMESGOP, V_ITDETALORCAMESGOP, 0, 0, 0, C_DETALHAMESFIL.CDDESPADMFAT, C_DETALHAMESFIL.CDFILIALMO, C_DETALHAMESFIL.CDORCAMENTOMO);
                  END LOOP;
                  
                  IF P_EXCLUI = 'S' THEN
                     DELETE FROM ITDETALORCAMESGOP
                           WHERE NRITORCAMESGOP = V_NRITORCAMESGOP
                             AND NVL(CDFILIALMO,'!')||NVL(CDORCAMENTOMO,'!')||NVL(CDDESPADMFAT,'!') NOT IN 
                                             (SELECT NVL(PD.CDFILIALMO,'!')||NVL(PD.CDORCAMENTOMO,'!')||NVL(DO.CDDESPADMFAT,'!')
                                                FROM PERCITFILGOP P INNER JOIN PERCITDETALFILGOP PD ON P.NRPERCITFILGOP = PD.NRPERCITFILGOP
                                                                    LEFT  JOIN DESPORCAMO DO ON DO.CDORCAMENTOMO = PD.CDORCAMENTOMO
                                                                                            AND DO.CDFILIAL = PD.CDFILIALMO
                                               WHERE P.CDFILIAL = P_CDFILIAL
                                                 AND P.NRSEQITEMORCA = P_ITEM
                                                 AND PD.CDORCAMENTOMO = P_CDORCAMENTOMO
                                                 AND PD.CDFILIALMO = P_CDFILIALMO);
                  END IF;
               ELSE
                  FOR C_DETALHAMES IN (SELECT DISTINCT DO.CDDESPADMFAT, PD.CDORCAMENTOMO, DO.CDFILIAL CDFILIALMO
                                         FROM PERCITEMGOP P INNER JOIN PERCITEMDETALGOP PD ON P.NRPERCITEMGOP = PD.NRPERCITEMGOP
                                                            LEFT  JOIN DESPORCAMO DO ON DO.CDORCAMENTOMO = PD.CDORCAMENTOMO
                                                                                    AND DO.CDFILIAL = 'T'
                                        WHERE P.NRSEQITEMORCA = P_ITEM
                                          AND PD.CDORCAMENTOMO = P_CDORCAMENTOMO
                                          AND PD.CDFILIALMO = P_CDFILIALMO
                                        MINUS
                                       SELECT IT.CDDESPADMFAT, IT.CDORCAMENTOMO, IT.CDFILIALMO
                                         FROM ITEMORCAGOP I INNER JOIN ITORCAMESGOP IM ON I.NRITEMORCAOP = IM.NRITEMORCAOP
                                                            INNER JOIN ITDETALORCAMESGOP IT ON IM.NRITORCAMESGOP = IT.NRITORCAMESGOP
                                        WHERE IT.NRITORCAMESGOP = V_NRITORCAMESGOP
                                          AND I.IDTIPOORCAOP = V_ORCAMENTO
                                          AND IT.CDORCAMENTOMO = P_CDORCAMENTOMO
                                          AND IT.CDFILIALMO = P_CDFILIALMO) LOOP

                     NOVO_CODIGO('ITDETALORCAMESGOP', V_ITDETALORCAMESGOP);
                     INSERT INTO ITDETALORCAMESGOP
                                 (NRITORCAMESGOP, NRITDETALORCAMESGOP, VRORCAMENTO, VRPERCENTUAL, QTORCAMENTO, CDDESPADMFAT, CDORCAMENTOMO, CDFILIALMO)
                          VALUES (V_NRITORCAMESGOP, V_ITDETALORCAMESGOP, 0, 0, 0, C_DETALHAMES.CDDESPADMFAT, C_DETALHAMES.CDORCAMENTOMO, C_DETALHAMES.CDFILIALMO);
                          
                  END LOOP;
                  
                  IF P_EXCLUI = 'S' THEN
                     DELETE FROM ITDETALORCAMESGOP
                           WHERE NRITORCAMESGOP = V_NRITORCAMESGOP
                             AND NVL(CDFILIALMO,'!')||NVL(CDORCAMENTOMO,'!')||NVL(CDDESPADMFAT,'!') NOT IN 
                                             (SELECT NVL(DO.CDFILIAL,'!')||NVL(PD.CDORCAMENTOMO,'!')||NVL(DO.CDDESPADMFAT,'!')
                                                FROM PERCITEMGOP P INNER JOIN PERCITEMDETALGOP PD ON P.NRPERCITEMGOP = PD.NRPERCITEMGOP
                                                                   LEFT  JOIN DESPORCAMO DO ON DO.CDORCAMENTOMO = PD.CDORCAMENTOMO
                                                                                           AND DO.CDFILIAL = 'T'
                                               WHERE P.NRSEQITEMORCA = P_ITEM
                                                 AND PD.CDORCAMENTOMO = P_CDORCAMENTOMO
                                                 AND PD.CDFILIALMO = P_CDFILIALMO);
                  END IF;
               END IF;
               
               DELETE FROM ITORCAMESGOP
                     WHERE NRITORCAMESGOP = V_NRITORCAMESGOP
                       AND NOT EXISTS (SELECT 1
                                         FROM ITDETALORCAMESGOP D
                                        WHERE ITORCAMESGOP.NRITORCAMESGOP = D.NRITORCAMESGOP)
                       AND EXISTS (SELECT 1
                                     FROM ITORCAMESGOP M INNER JOIN ITEMORCAGOP I ON I.NRITEMORCAOP = M.NRITEMORCAOP
                                    WHERE ITORCAMESGOP.NRITORCAMESGOP = M.NRITORCAMESGOP
                                      AND I.CDORCAMENTOMO IS NOT NULL);
               
               DELETE FROM ITORCAMESGOP
                     WHERE NRITORCAMESGOP = V_NRITORCAMESGOP
                       AND 0 < (SELECT COUNT(1)
                                  FROM ITORCAMESGOP M INNER JOIN ITEMORCAGOP I ON I.NRITEMORCAOP = M.NRITEMORCAOP
                                 WHERE ITORCAMESGOP.NRITORCAMESGOP = M.NRITORCAMESGOP
                                   AND I.CDORCAMENTOMO IS NOT NULL)
                       AND EXISTS (SELECT 1
                                     FROM ITORCAMESGOP M INNER JOIN ITEMORCAGOP I ON I.NRITEMORCAOP = M.NRITEMORCAOP
                                    WHERE ITORCAMESGOP.NRITORCAMESGOP = M.NRITORCAMESGOP
                                      AND I.CDORCAMENTOMO IS NULL);
            /* DESPESA MATRIZ, DESPESA DIVERSA, DESPESA ENTRADA */
            ELSIF P_ITEM IN (5,6,7) THEN
               IF P_IDUTILPERCFILGOP = 'S' THEN
                  FOR C_DETALHAMESFIL IN (SELECT PR.CDPRODUTO, CC.CDCONTCTBL
                                            FROM PERCITFILGOP P INNER JOIN PERCITDETALFILGOP PD
                                                                        ON P.NRPERCITFILGOP = PD.NRPERCITFILGOP
                                                                CROSS JOIN PARAMORCAOPERAC P
                                                                LEFT  JOIN PRODDESPPREV PR
                                                                        ON PD.CDDESPPREV = PR.CDDESPPREV
                                                                       AND ((P.IDORCDESPDIVERS = 'P' AND V_DESPESA = 'DIVERSA')
                                                                        OR (P.IDORCADESPMATRI = 'P' AND V_DESPESA = 'MATRIZ')
                                                                        OR (V_DESPESA = 'LANCENTRADA'))
                                                                LEFT  JOIN CTBLDESPPREV CC
                                                                        ON PD.CDDESPPREV = CC.CDDESPPREV
                                                                       AND ((P.IDORCDESPDIVERS = 'C' AND V_DESPESA = 'DIVERSA')
                                                                        OR (P.IDORCADESPMATRI = 'C' AND V_DESPESA = 'MATRIZ'))
                                           WHERE P.NRSEQITEMORCA = P_ITEM
                                             AND PD.IDDESPPREV = V_DESPESA
                                             AND P.CDFILIAL = P_CDFILIAL
                                             AND NVL(PD.CDDESPPREV,'T') = NVL(P_CDDESPPREV,'T')
                                           MINUS
                                          SELECT IT.CDPRODUTO, IT.CDCONTCTBL
                                            FROM ITEMORCAGOP I INNER JOIN ITORCAMESGOP IM ON I.NRITEMORCAOP = IM.NRITEMORCAOP
                                                               INNER JOIN ITDETALORCAMESGOP IT ON IM.NRITORCAMESGOP = IT.NRITORCAMESGOP
                                           WHERE IT.NRITORCAMESGOP = V_NRITORCAMESGOP
                                             AND I.IDTIPOORCAOP = V_ORCAMENTO) LOOP

                     NOVO_CODIGO('ITDETALORCAMESGOP', V_ITDETALORCAMESGOP);
                     INSERT INTO ITDETALORCAMESGOP
                                 (NRITORCAMESGOP, NRITDETALORCAMESGOP, VRORCAMENTO, VRPERCENTUAL, QTORCAMENTO, CDPRODUTO, CDCONTCTBL)
                          VALUES (V_NRITORCAMESGOP, V_ITDETALORCAMESGOP, 0, 0, 0, C_DETALHAMESFIL.CDPRODUTO, C_DETALHAMESFIL.CDCONTCTBL);
                  END LOOP;
                  
                  IF P_EXCLUI = 'S' THEN
                     DELETE FROM ITDETALORCAMESGOP
                           WHERE NRITORCAMESGOP = V_NRITORCAMESGOP
                             AND (NVL(CDPRODUTO,'!')||NVL(CDCONTCTBL,'!') NOT IN 
                                          (SELECT NVL(PR.CDPRODUTO,'!')||NVL(CC.CDCONTCTBL,'!')
                                             FROM PERCITFILGOP P INNER JOIN PERCITDETALFILGOP PD
                                                                         ON P.NRPERCITFILGOP = PD.NRPERCITFILGOP
                                                                 CROSS JOIN PARAMORCAOPERAC P
                                                                 LEFT  JOIN PRODDESPPREV PR
                                                                         ON PD.CDDESPPREV = PR.CDDESPPREV
                                                                        AND ((P.IDORCDESPDIVERS = 'P' AND V_DESPESA = 'DIVERSA')
                                                                         OR (P.IDORCADESPMATRI = 'P' AND V_DESPESA = 'MATRIZ')
                                                                         OR (V_DESPESA = 'LANCENTRADA'))
                                                                 LEFT  JOIN CTBLDESPPREV CC
                                                                         ON PD.CDDESPPREV = CC.CDDESPPREV
                                                                        AND ((P.IDORCDESPDIVERS = 'C' AND V_DESPESA = 'DIVERSA')
                                                                         OR (P.IDORCADESPMATRI = 'C' AND V_DESPESA = 'MATRIZ'))
                                            WHERE P.NRSEQITEMORCA = P_ITEM
                                              AND PD.IDDESPPREV = V_DESPESA
                                              AND P.CDFILIAL = P_CDFILIAL
                                              AND NVL(PD.CDDESPPREV,'T') = NVL(P_CDDESPPREV,'T'))
                              OR (CDPRODUTO IS NULL AND CDCONTCTBL IS NULL));
                  END IF;
               ELSE
                  FOR C_DETALHAMES IN (SELECT PR.CDPRODUTO, CC.CDCONTCTBL
                                         FROM PERCITEMGOP P INNER JOIN PERCITEMDETALGOP PD
                                                                    ON P.NRPERCITEMGOP = PD.NRPERCITEMGOP
                                                            CROSS JOIN PARAMORCAOPERAC P
                                                            LEFT  JOIN PRODDESPPREV PR
                                                                    ON PD.CDDESPPREV = PR.CDDESPPREV
                                                                   AND ((P.IDORCDESPDIVERS = 'P' AND V_DESPESA = 'DIVERSA')
                                                                    OR (P.IDORCADESPMATRI = 'P' AND V_DESPESA = 'MATRIZ')
                                                                    OR (V_DESPESA = 'LANCENTRADA'))
                                                            LEFT  JOIN CTBLDESPPREV CC
                                                                    ON PD.CDDESPPREV = CC.CDDESPPREV
                                                                   AND ((P.IDORCDESPDIVERS = 'C' AND V_DESPESA = 'DIVERSA')
                                                                    OR (P.IDORCADESPMATRI = 'C' AND V_DESPESA = 'MATRIZ'))
                                        WHERE P.NRSEQITEMORCA = P_ITEM
                                          AND PD.IDDESPPREV = V_DESPESA
                                          AND NVL(PD.CDDESPPREV,'T') = NVL(P_CDDESPPREV,'T')
                                        MINUS
                                       SELECT IT.CDPRODUTO, IT.CDCONTCTBL
                                         FROM ITEMORCAGOP I INNER JOIN ITORCAMESGOP IM ON I.NRITEMORCAOP = IM.NRITEMORCAOP
                                                            INNER JOIN ITDETALORCAMESGOP IT ON IM.NRITORCAMESGOP = IT.NRITORCAMESGOP
                                        WHERE IT.NRITORCAMESGOP = V_NRITORCAMESGOP
                                          AND I.IDTIPOORCAOP = V_ORCAMENTO) LOOP

                     NOVO_CODIGO('ITDETALORCAMESGOP', V_ITDETALORCAMESGOP);
                     INSERT INTO ITDETALORCAMESGOP
                                 (NRITORCAMESGOP, NRITDETALORCAMESGOP, VRORCAMENTO, VRPERCENTUAL, QTORCAMENTO, CDPRODUTO, CDCONTCTBL)
                          VALUES (V_NRITORCAMESGOP, V_ITDETALORCAMESGOP, 0, 0, 0, C_DETALHAMES.CDPRODUTO, C_DETALHAMES.CDCONTCTBL);
                  END LOOP;
                  
                  IF P_EXCLUI = 'S' THEN
                     DELETE FROM ITDETALORCAMESGOP
                           WHERE NRITORCAMESGOP = V_NRITORCAMESGOP
                             AND (NVL(CDPRODUTO,'!')||NVL(CDCONTCTBL,'!') NOT IN 
                                       (SELECT NVL(PR.CDPRODUTO,'!')||NVL(CC.CDCONTCTBL,'!')
                                          FROM PERCITEMGOP P INNER JOIN PERCITEMDETALGOP PD
                                                                     ON P.NRPERCITEMGOP = PD.NRPERCITEMGOP
                                                             CROSS JOIN PARAMORCAOPERAC P
                                                             LEFT  JOIN PRODDESPPREV PR
                                                                     ON PD.CDDESPPREV = PR.CDDESPPREV
                                                                    AND ((P.IDORCDESPDIVERS = 'P' AND V_DESPESA = 'DIVERSA')
                                                                     OR (P.IDORCADESPMATRI = 'P' AND V_DESPESA = 'MATRIZ')
                                                                     OR (V_DESPESA = 'LANCENTRADA'))
                                                             LEFT  JOIN CTBLDESPPREV CC
                                                                     ON PD.CDDESPPREV = CC.CDDESPPREV
                                                                    AND ((P.IDORCDESPDIVERS = 'C' AND V_DESPESA = 'DIVERSA')
                                                                     OR (P.IDORCADESPMATRI = 'C' AND V_DESPESA = 'MATRIZ'))
                                         WHERE P.NRSEQITEMORCA = P_ITEM
                                           AND PD.IDDESPPREV = V_DESPESA
                                           AND NVL(PD.CDDESPPREV,'T') = NVL(P_CDDESPPREV,'T'))
                               OR (CDPRODUTO IS NULL AND CDCONTCTBL IS NULL));
                  END IF;
               END IF;
               DELETE FROM ITORCAMESGOP
                     WHERE NRITORCAMESGOP = V_NRITORCAMESGOP
                       AND NOT EXISTS (SELECT 1
                                         FROM ITDETALORCAMESGOP D
                                        WHERE ITORCAMESGOP.NRITORCAMESGOP = D.NRITORCAMESGOP)
                       AND EXISTS (SELECT 1
                                     FROM ITORCAMESGOP M INNER JOIN ITEMORCAGOP I ON I.NRITEMORCAOP = M.NRITEMORCAOP
                                    WHERE ITORCAMESGOP.NRITORCAMESGOP = M.NRITORCAMESGOP
                                      AND I.CDDESPPREV IS NOT NULL);
            END IF;
         END IF;
      END LOOP;
   END;
   FUNCTION STRPERIODO (NRPERIODOORCA NUMBER, IDPERIODOORCA VARCHAR2) RETURN VARCHAR2 AS
     V_DESCNRPERIODOORCA VARCHAR2(40);
   BEGIN
      IF TO_CHAR(IDPERIODOORCA) = 'MENSAL' THEN
         V_DESCNRPERIODOORCA := CASE NRPERIODOORCA WHEN 1 THEN 'Janeiro'
                                                   WHEN 2 THEN 'Fevereiro'
                                                   WHEN 3 THEN 'Marco'
                                                   WHEN 4 THEN 'Abril'
                                                   WHEN 5 THEN 'Maio'
                                                   WHEN 6 THEN 'Junho'
                                                   WHEN 7 THEN 'Julho'
                                                   WHEN 8 THEN 'Agosto'
                                                   WHEN 9 THEN 'Setembro'
                                                   WHEN 10 THEN 'Outubro'
                                                   WHEN 11 THEN 'Novembro'
                                                   WHEN 12 THEN 'Dezembro'
                                                   ELSE ' '
                                END;
      ELSIF IDPERIODOORCA = 'BIMESTRAL' THEN
         V_DESCNRPERIODOORCA := CASE NRPERIODOORCA WHEN 1 THEN '1 Bimestre'
                                                   WHEN 2 THEN '2 Bimestre'
                                                   WHEN 3 THEN '3 Bimestre'
                                                   WHEN 4 THEN '4 Bimestre'
                                                   WHEN 5 THEN '5 Bimestre'
                                                   WHEN 6 THEN '6 Bimestre'
                                                   ELSE ' '
                                END;
      ELSIF IDPERIODOORCA = 'TRIMESTRAL' THEN
         V_DESCNRPERIODOORCA := CASE NRPERIODOORCA WHEN 1 THEN '1 Trimestre'
                                                   WHEN 2 THEN '2 Trimestre'
                                                   WHEN 3 THEN '3 Trimestre'
                                                   WHEN 4 THEN '4 Trimestre'
                                                   ELSE ' '
                                END;
      ELSIF IDPERIODOORCA = 'QUADRIMESTRAL' THEN
         V_DESCNRPERIODOORCA := CASE NRPERIODOORCA WHEN 1 THEN '1 Quadrimestre'
                                                   WHEN 2 THEN '2 Quadrimestre'
                                                   WHEN 3 THEN '3 Quadrimestre'
                                                   ELSE ' '
                                END;
      ELSIF IDPERIODOORCA = 'SEMESTRAL' THEN
         V_DESCNRPERIODOORCA := CASE NRPERIODOORCA WHEN 1 THEN '1 Semestre'
                                                   WHEN 2 THEN '2 Semestre'
                                                   ELSE ' '
                                END;
      END IF;
      RETURN V_DESCNRPERIODOORCA;
   END;
BEGIN
   /* DELPHI */
   IF NVL(P_ORCADETA, 'N') = 'N' THEN      
      BEGIN
         SELECT IDUTPADRROP
           INTO V_IDUTPADRROP
           FROM PARAMGERAL G;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_IDUTPADRROP := NULL;
         END;
      END;

      IF (P_LISTAOPER = 'S') AND (P_LISTAFIL = 'S') THEN
         FOR C_ORCOP01 IN (SELECT NRANOORCAOP, CDVERSORCAOP, CDFILIAL, MAX(CDOPERADOR) AS CDOPERADOR
                             FROM (SELECT DISTINCT P_NRANOORCAOP AS NRANOORCAOP, P_CDVERSORCAOP AS CDVERSORCAOP, CDFILIAL, FO.CDOPERADOR
                                     FROM FILIALOPEROP FO, LISTA O, LISTA F
                                    WHERE O.NRLISTA       = P_CDOPERADOR
                                      AND F.NRLISTA       = P_CDFILIAL
                                      AND FO.CDFILIAL     = F.CDLISTA
                                      AND FO.CDOPERADOR   = O.CDLISTA
                                      AND FO.IDACESSOCALC = 'S')
                                    GROUP BY NRANOORCAOP, CDVERSORCAOP, CDFILIAL) LOOP
            BEGIN
               SELECT CDFILIAL, 'S',IDSTATUSORCA
                 INTO V_FILIAL, V_EXISTE,V_IDSTATUSORCA
                 FROM ORCAMENTOOP
                WHERE NRANOORCAOP  = C_ORCOP01.NRANOORCAOP
                  AND CDVERSORCAOP = C_ORCOP01.CDVERSORCAOP
                  AND CDFILIAL     = C_ORCOP01.CDFILIAL;
            EXCEPTION
                 WHEN NO_DATA_FOUND THEN BEGIN
                      V_FILIAL       := NULL;
                      V_EXISTE       := 'N';
                      V_IDSTATUSORCA := NULL;
                 END;
            END;
            IF V_EXISTE = 'S' THEN
               IF P_IDEXCLUI = 'S' AND V_IDSTATUSORCA <> 'A' THEN
                  UPDATE ORCAMENTOOP
                     SET IDSTATUSORCA = 'P',
                         DTORCAMENTO  = SYSDATE,
                         CDOPERADOR   = C_ORCOP01.CDOPERADOR
                   WHERE NRANOORCAOP  = C_ORCOP01.NRANOORCAOP
                     AND CDVERSORCAOP = C_ORCOP01.CDVERSORCAOP
                     AND CDFILIAL     = C_ORCOP01.CDFILIAL;
                  UPDATE ITEMORCAOP
                     SET IDITEMORCAMENTO = 'P'
                   WHERE NRANOORCAOP     = C_ORCOP01.NRANOORCAOP
                     AND CDVERSORCAOP    = C_ORCOP01.CDVERSORCAOP
                     AND CDFILIAL        = C_ORCOP01.CDFILIAL;
               END IF;
            ELSE
               INSERT INTO ORCAMENTOOP
                      (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, CDOPERADOR, DTORCAMENTO, IDSTATUSORCA)
               VALUES (C_ORCOP01.NRANOORCAOP, C_ORCOP01.CDVERSORCAOP, C_ORCOP01.CDFILIAL, C_ORCOP01.CDOPERADOR, SYSDATE, 'P');
            END IF;
         END LOOP;
      ELSE
         IF (P_LISTAOPER = 'S') AND (NVL(P_LISTAFIL,'S') = 'N') THEN
            FOR C_ORCOP02 IN (SELECT NRANOORCAOP, CDVERSORCAOP, CDFILIAL, MAX(CDOPERADOR) AS CDOPERADOR
                                FROM (SELECT DISTINCT P_NRANOORCAOP AS NRANOORCAOP, P_CDVERSORCAOP AS CDVERSORCAOP, CDFILIAL, FO.CDOPERADOR
                                        FROM FILIALOPEROP FO, LISTA O
                                       WHERE O.NRLISTA       = P_CDOPERADOR
                                         AND FO.CDOPERADOR   = O.CDLISTA
                                         AND FO.IDACESSOCALC = 'S')
                              GROUP BY NRANOORCAOP, CDVERSORCAOP, CDFILIAL) LOOP
               BEGIN
                  SELECT CDFILIAL, 'S',IDSTATUSORCA
                    INTO V_FILIAL,V_EXISTE,V_IDSTATUSORCA
                    FROM ORCAMENTOOP
                   WHERE NRANOORCAOP  = C_ORCOP02.NRANOORCAOP
                     AND CDVERSORCAOP = C_ORCOP02.CDVERSORCAOP
                     AND CDFILIAL     = C_ORCOP02.CDFILIAL;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN BEGIN
                     V_FILIAL := NULL;
                     V_EXISTE := 'N';
                     V_IDSTATUSORCA := NULL;
                  END;
               END;
               IF V_EXISTE = 'S' THEN
                  IF P_IDEXCLUI = 'S' AND V_IDSTATUSORCA <> 'A' THEN
                     UPDATE ORCAMENTOOP
                        SET IDSTATUSORCA = 'P',
                            DTORCAMENTO  = SYSDATE,
                            CDOPERADOR   = C_ORCOP02.CDOPERADOR
                      WHERE NRANOORCAOP  = C_ORCOP02.NRANOORCAOP
                        AND CDVERSORCAOP = C_ORCOP02.CDVERSORCAOP
                        AND CDFILIAL     = C_ORCOP02.CDFILIAL;
                     UPDATE ITEMORCAOP
                        SET IDITEMORCAMENTO = 'P'
                      WHERE NRANOORCAOP  = C_ORCOP02.NRANOORCAOP
                        AND CDVERSORCAOP = C_ORCOP02.CDVERSORCAOP
                        AND CDFILIAL     = C_ORCOP02.CDFILIAL;
                  END IF;
               ELSE
                  INSERT INTO ORCAMENTOOP
                         (NRANOORCAOP, CDVERSORCAOP, CDFILIAL,CDOPERADOR,DTORCAMENTO, IDSTATUSORCA)
                  VALUES (C_ORCOP02.NRANOORCAOP,C_ORCOP02.CDVERSORCAOP,C_ORCOP02.CDFILIAL, C_ORCOP02.CDOPERADOR, SYSDATE, 'P');
               END IF;
            END LOOP;
         ELSE
            IF (P_LISTAOPER = 'N') AND (NVL(P_LISTAFIL,'S') = 'S') THEN
               FOR C_ORCOP03 IN (SELECT NRANOORCAOP, CDVERSORCAOP, CDFILIAL, MAX(CDOPERADOR) AS CDOPERADOR
                                   FROM (SELECT DISTINCT P_NRANOORCAOP AS NRANOORCAOP, P_CDVERSORCAOP AS CDVERSORCAOP, CDFILIAL, FO.CDOPERADOR
                                           FROM FILIALOPEROP FO, LISTA F
                                          WHERE F.NRLISTA       = P_CDFILIAL
                                            AND FO.CDFILIAL     = F.CDLISTA
                                            AND FO.IDACESSOCALC = 'S'
                                            AND FO.CDOPERADOR   = P_CDOPERADOR)
                                 GROUP BY NRANOORCAOP, CDVERSORCAOP, CDFILIAL) LOOP
                  BEGIN
                     SELECT CDFILIAL, 'S',IDSTATUSORCA
                       INTO V_FILIAL, V_EXISTE,V_IDSTATUSORCA
                       FROM ORCAMENTOOP
                      WHERE NRANOORCAOP  = C_ORCOP03.NRANOORCAOP
                        AND CDVERSORCAOP = C_ORCOP03.CDVERSORCAOP
                        AND CDFILIAL     = C_ORCOP03.CDFILIAL;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_FILIAL       := NULL;
                        V_EXISTE       := 'N';
                        V_IDSTATUSORCA := NULL;
                     END;
                  END;
                  IF V_EXISTE = 'S' THEN
                     IF P_IDEXCLUI = 'S' AND V_IDSTATUSORCA <> 'A' THEN
                        UPDATE ORCAMENTOOP
                           SET IDSTATUSORCA = 'P',
                               DTORCAMENTO  = SYSDATE,
                               CDOPERADOR   = C_ORCOP03.CDOPERADOR
                         WHERE NRANOORCAOP  = C_ORCOP03.NRANOORCAOP
                           AND CDVERSORCAOP = C_ORCOP03.CDVERSORCAOP
                           AND CDFILIAL     = C_ORCOP03.CDFILIAL;
                        UPDATE ITEMORCAOP
                           SET IDITEMORCAMENTO = 'P'
                         WHERE NRANOORCAOP  = C_ORCOP03.NRANOORCAOP
                           AND CDVERSORCAOP = C_ORCOP03.CDVERSORCAOP
                           AND CDFILIAL     = C_ORCOP03.CDFILIAL;
                     END IF;
                  ELSE
                     INSERT INTO ORCAMENTOOP
                            (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, CDOPERADOR, DTORCAMENTO, IDSTATUSORCA)
                     VALUES (C_ORCOP03.NRANOORCAOP, C_ORCOP03.CDVERSORCAOP, C_ORCOP03.CDFILIAL, C_ORCOP03.CDOPERADOR, SYSDATE, 'P');
                  END IF;
               END LOOP;
            ELSE
               FOR C_ORCOP04 IN (SELECT NRANOORCAOP, CDVERSORCAOP, CDFILIAL, MAX(CDOPERADOR) AS CDOPERADOR
                                   FROM (SELECT DISTINCT P_NRANOORCAOP AS NRANOORCAOP, P_CDVERSORCAOP AS CDVERSORCAOP, CDFILIAL, FO.CDOPERADOR
                                           FROM FILIALOPEROP FO
                                          WHERE FO.IDACESSOCALC = 'S'
                                            AND FO.CDOPERADOR   = P_CDOPERADOR)
                                 GROUP BY NRANOORCAOP, CDVERSORCAOP, CDFILIAL) LOOP
                  BEGIN
                     SELECT CDFILIAL, 'S',IDSTATUSORCA
                       INTO V_FILIAL, V_EXISTE,V_IDSTATUSORCA
                       FROM ORCAMENTOOP
                      WHERE NRANOORCAOP  = C_ORCOP04.NRANOORCAOP
                        AND CDVERSORCAOP = C_ORCOP04.CDVERSORCAOP
                        AND CDFILIAL     = C_ORCOP04.CDFILIAL;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_FILIAL := NULL;
                        V_EXISTE := 'N';
                        V_IDSTATUSORCA := NULL;
                     END;
                  END;
                  IF V_EXISTE = 'S' THEN
                     IF P_IDEXCLUI = 'S' AND V_IDSTATUSORCA <> 'A'THEN
                        UPDATE ORCAMENTOOP
                           SET IDSTATUSORCA = 'P',
                               DTORCAMENTO  = SYSDATE,
                               CDOPERADOR   = C_ORCOP04.CDOPERADOR
                         WHERE NRANOORCAOP  = C_ORCOP04.NRANOORCAOP
                           AND CDVERSORCAOP = C_ORCOP04.CDVERSORCAOP
                           AND CDFILIAL     = C_ORCOP04.CDFILIAL;
                        UPDATE ITEMORCAOP
                           SET IDITEMORCAMENTO = 'P'
                         WHERE NRANOORCAOP  = C_ORCOP04.NRANOORCAOP
                           AND CDVERSORCAOP = C_ORCOP04.CDVERSORCAOP
                           AND CDFILIAL     = C_ORCOP04.CDFILIAL;
                     END IF;
                  ELSE
                     INSERT INTO ORCAMENTOOP
                            (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, CDOPERADOR, DTORCAMENTO, IDSTATUSORCA)
                     VALUES (C_ORCOP04.NRANOORCAOP, C_ORCOP04.CDVERSORCAOP, C_ORCOP04.CDFILIAL, C_ORCOP04.CDOPERADOR, SYSDATE, 'P');
                  END IF;
               END LOOP;
            END IF;
         END IF;
      END IF;
      FOR C_FILIAL IN (SELECT O.CDFILIAL,O.IDSTATUSORCA
                         FROM ORCAMENTOOP O
                        WHERE O.NRANOORCAOP  = P_NRANOORCAOP
                          AND O.CDVERSORCAOP = P_CDVERSORCAOP
                          AND O.IDSTATUSORCA <> 'A'
                          AND ((O.CDFILIAL IN ( SELECT CDLISTA
                                                  FROM LISTA
                                                 WHERE NRLISTA = P_CDFILIAL ) ) OR (P_CDFILIAL IS NULL))) LOOP
           IF C_FILIAL.IDSTATUSORCA <> 'A' THEN
               IF (( C_FILIAL.IDSTATUSORCA <> 'E') OR (C_FILIAL.IDSTATUSORCA = 'E' AND P_IDEXCLUI = 'S')) THEN
                     IF (V_IDUTPADRROP = 'S') THEN
                        V_FILIALPAR := 'P';
                     ELSE
                        BEGIN
                           SELECT IDUTPADRROPFIL, CDFILCENTPME
                             INTO V_IDUTPADRROPFIL, V_CDFILCENTPME
                             FROM FILIAL F LEFT JOIN PARFILIALROP P ON F.CDFILIAL = P.CDFILIAL
                            WHERE F.CDFILIAL = C_FILIAL.CDFILIAL;
                        EXCEPTION
                          WHEN NO_DATA_FOUND THEN BEGIN
                            V_IDUTPADRROPFIL := NULL;
                            V_CDFILCENTPME   := NULL;
                          END;
                        END;
                        IF (V_IDUTPADRROPFIL = 'S') THEN
                           V_FILIALPAR := 'P';
                        ELSE
                           IF V_CDFILCENTPME IS NULL THEN
                              V_FILIALPAR := C_FILIAL.CDFILIAL;
                           ELSE
                              BEGIN
                                 SELECT IDFILIALAGRROP
                                   INTO V_IDFILIALAGRROP
                                   FROM PARFILIALROP
                                  WHERE CDFILIAL = V_CDFILCENTPME;
                              EXCEPTION
                                WHEN NO_DATA_FOUND THEN
                                  V_IDFILIALAGRROP := 'N';
                              END;
                              IF V_IDFILIALAGRROP = 'S' THEN
                                 V_FILIALPAR := V_CDFILCENTPME;
                              ELSE
                                 V_FILIALPAR := C_FILIAL.CDFILIAL;
                              END IF;
                           END IF;
                        END IF;
                     END IF;
                     BEGIN
                        SELECT IDFATUROP, IDSERVFATROP
                          INTO V_IDFATUROP, V_IDSERVFATROP
                          FROM PARFILIALROP P
                         WHERE P.CDFILIAL = V_FILIALPAR;
                     EXCEPTION
                       WHEN NO_DATA_FOUND THEN BEGIN
                         V_IDFATUROP    := NULL;
                         V_IDSERVFATROP := NULL;
                       END;
                     END;
                     SELECT LPAD((TO_NUMBER(NVL(MAX(NRSEQITEMORCA),'0')) + 1), 10, 0)
                       INTO V_NRITEMORCA
                       FROM ITEMORCAOP
                      WHERE CDFILIAL     = C_FILIAL.CDFILIAL
                        AND NRANOORCAOP  = P_NRANOORCAOP
                        AND CDVERSORCAOP = P_CDVERSORCAOP;
                     FOR C_SERVICO IN(  SELECT SC.CDSERVFILI
                                            FROM SERVICOROP SC, SERVICO S
                                             WHERE SC.CDFILIAL = TIPOPARAM_DRO(C_FILIAL.CDFILIAL)
                                             AND (IDTPSERVICOROP   = 'E')
                                             AND (V_IDSERVFATROP <> 'S')
                                             AND SC.CDSERVFILI = S.CDSERVFILI
                                             AND S.CDFILIAL = C_FILIAL.CDFILIAL
                                           UNION ALL
                                           SELECT CDSERVFILI
                                           FROM SERVICO S
                                           WHERE (S.CDFILIAL = C_FILIAL.CDFILIAL)
                                             AND (V_IDSERVFATROP   = 'S')
                                             AND (QTCOMESTISEG IS NOT  NULL OR
                                                  QTCOMESTITER IS NOT  NULL OR
                                                  QTCOMESTIQUA IS NOT  NULL OR
                                                  QTCOMESTIQUI IS NOT  NULL OR
                                                  QTCOMESTISEX IS NOT  NULL OR
                                                  QTCOMESTISAB IS NOT  NULL OR
                                                  QTCOMESTIDOM IS NOT  NULL OR
                                                  QTCOMESTIFER IS NOT  NULL OR
                                                  'S' = 'S'))  LOOP
                     INSERT INTO ITEMORCAOP
                      (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO, CDDESPADMFAT, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO, CDSERVFILI)
                     (SELECT  P_NRANOORCAOP, P_CDVERSORCAOP, C_FILIAL.CDFILIAL, V_NRITEMORCA, '','', '', '','',SYSDATE, 'F','P', C_SERVICO.CDSERVFILI
                        FROM DUAL
                      MINUS
                      SELECT P_NRANOORCAOP, P_CDVERSORCAOP, C_FILIAL.CDFILIAL, V_NRITEMORCA, '','', '', '', '',SYSDATE, 'F','P', C_SERVICO.CDSERVFILI
                        FROM ITEMORCAOP
                       WHERE NRANOORCAOP  = P_NRANOORCAOP
                         AND CDVERSORCAOP = P_CDVERSORCAOP
                         AND CDFILIAL = C_FILIAL.CDFILIAL
                         AND IDTIPOORCAOP = 'F'
                         AND CDSERVFILI = C_SERVICO.CDSERVFILI);
                     V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                     INSERT INTO ITEMORCAOP
                      (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO, CDDESPADMFAT, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO, CDSERVFILI)
                     (SELECT P_NRANOORCAOP, P_CDVERSORCAOP, C_FILIAL.CDFILIAL, V_NRITEMORCA, '','', '', '', '',SYSDATE, 'C','P', C_SERVICO.CDSERVFILI
                        FROM DUAL
                       MINUS
                      SELECT P_NRANOORCAOP, P_CDVERSORCAOP, C_FILIAL.CDFILIAL, V_NRITEMORCA, '','', '', '', '',SYSDATE, 'C','P', C_SERVICO.CDSERVFILI
                        FROM ITEMORCAOP
                       WHERE NRANOORCAOP = P_NRANOORCAOP
                         AND CDVERSORCAOP = P_CDVERSORCAOP
                         AND CDFILIAL = C_FILIAL.CDFILIAL
                         AND IDTIPOORCAOP = 'C'
                         AND CDSERVFILI = C_SERVICO.CDSERVFILI );
                         V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                    END LOOP;
                    FOR C_IMPOSTO IN (SELECT DISTINCT DECODE(CDFILIAL, 'T',  C_FILIAL.CDFILIAL , CDFILIAL) AS CDFILIAL , CDIMPOSTO
                                        FROM IMPOSTOORCAFIL
                                       WHERE ((CDFILIAL = C_FILIAL.CDFILIAL) OR (CDFILIAL = 'T'))
                                         AND VRPERCSOBFAT  <> 0
                                      MINUS
                                      SELECT C_FILIAL.CDFILIAL, CDIMPOSTO
                                        FROM ITEMORCAOP
                                       WHERE NRANOORCAOP  = P_NRANOORCAOP
                                         AND CDVERSORCAOP = P_CDVERSORCAOP
                                         AND CDFILIAL = C_FILIAL.CDFILIAL
                                         AND IDTIPOORCAOP = 'I') LOOP
                             V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                             INSERT INTO ITEMORCAOP
                              (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO ,CDDESPADMFAT, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO)
                             VALUES
                              (P_NRANOORCAOP, P_CDVERSORCAOP, C_IMPOSTO.CDFILIAL, V_NRITEMORCA, '','', C_IMPOSTO.CDIMPOSTO,'','',SYSDATE, 'I','P');
                    END LOOP;
                    FOR C_IMPOSTOREC IN (SELECT DISTINCT DECODE(CDFILIAL, 'T',  C_FILIAL.CDFILIAL , CDFILIAL) AS CDFILIAL, CDIMPOSTO
                                           FROM IMPOSTOORCAFIL
                                          WHERE ((CDFILIAL = C_FILIAL.CDFILIAL) OR (CDFILIAL = 'T'))
                                            AND VRPERCSOBCONS  <> 0
                                         MINUS
                                         SELECT C_FILIAL.CDFILIAL, CDIMPOSTO
                                           FROM ITEMORCAOP
                                          WHERE NRANOORCAOP  = P_NRANOORCAOP
                                            AND CDVERSORCAOP = P_CDVERSORCAOP
                                            AND CDFILIAL = C_FILIAL.CDFILIAL
                                            AND IDTIPOORCAOP = 'R') LOOP
                             V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                             INSERT INTO ITEMORCAOP
                              (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO ,CDDESPADMFAT, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO)
                             VALUES
                              (P_NRANOORCAOP, P_CDVERSORCAOP, C_IMPOSTOREC.CDFILIAL, V_NRITEMORCA, '','',  C_IMPOSTOREC.CDIMPOSTO,'','',SYSDATE, 'R','P');
                    END LOOP;
                    FOR C_IMPOSLUCR IN (SELECT DISTINCT DECODE(CDFILIAL, 'T',  C_FILIAL.CDFILIAL , CDFILIAL) AS CDFILIAL
                                           FROM IMPOSTOORCAFIL
                                          WHERE CDFILIAL = C_FILIAL.CDFILIAL OR CDFILIAL = 'T'
                                         MINUS
                                         SELECT C_FILIAL.CDFILIAL
                                           FROM ITEMORCAOP
                                          WHERE NRANOORCAOP  = P_NRANOORCAOP
                                            AND CDVERSORCAOP = P_CDVERSORCAOP
                                            AND CDFILIAL = C_FILIAL.CDFILIAL
                                            AND IDTIPOORCAOP = 'S') LOOP
                             V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                             INSERT INTO ITEMORCAOP
                              (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO ,CDDESPADMFAT, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO)
                             VALUES
                              (P_NRANOORCAOP, P_CDVERSORCAOP, C_IMPOSLUCR.CDFILIAL, V_NRITEMORCA, '','', '','','',SYSDATE, 'S','P');
                    END LOOP;
                    FOR C_INVEST IN (SELECT DISTINCT DECODE(CDFILIAL, 'T',  C_FILIAL.CDFILIAL , CDFILIAL) AS CDFILIAL
                                           FROM ITEMORCAOP
                                          WHERE CDFILIAL = C_FILIAL.CDFILIAL
                                         MINUS
                                         SELECT C_FILIAL.CDFILIAL
                                           FROM ITEMORCAOP
                                          WHERE NRANOORCAOP  = P_NRANOORCAOP
                                            AND CDVERSORCAOP = P_CDVERSORCAOP
                                            AND CDFILIAL = C_FILIAL.CDFILIAL
                                            AND IDTIPOORCAOP = 'N') LOOP
                             V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                             INSERT INTO ITEMORCAOP
                              (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO ,CDDESPADMFAT, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO)
                             VALUES
                              (P_NRANOORCAOP, P_CDVERSORCAOP, C_INVEST.CDFILIAL, V_NRITEMORCA, '','', '','','',SYSDATE, 'N','P');
                    END LOOP;
                    FOR C_DEPREC IN (SELECT DISTINCT DECODE(CDFILIAL, 'T',  C_FILIAL.CDFILIAL , CDFILIAL) AS CDFILIAL
                                           FROM ITEMORCAOP
                                          WHERE CDFILIAL = C_FILIAL.CDFILIAL
                                         MINUS
                                         SELECT C_FILIAL.CDFILIAL
                                           FROM ITEMORCAOP
                                          WHERE NRANOORCAOP  = P_NRANOORCAOP
                                            AND CDVERSORCAOP = P_CDVERSORCAOP
                                            AND CDFILIAL = C_FILIAL.CDFILIAL
                                            AND IDTIPOORCAOP = 'P') LOOP
                             V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                             INSERT INTO ITEMORCAOP
                              (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO ,CDDESPADMFAT, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO)
                             VALUES
                              (P_NRANOORCAOP, P_CDVERSORCAOP, C_DEPREC.CDFILIAL, V_NRITEMORCA, '','', '','','',SYSDATE, 'P','P');
                    END LOOP;
                    /*Mao de obra em duas etapas. Primeiro os orcamentos POR FILIAL e depois os Gerais
                     1 - Orcamento de MO por filial*/
                    FOR C_MAOOBRA_FILIAL IN (
                                              SELECT C_FILIAL.CDFILIAL, CDDESPADMFAT
                                                FROM DESPADMFATUR
                                               MINUS
                                              SELECT C_FILIAL.CDFILIAL, CDDESPADMFAT
                                                FROM ITEMORCAOP
                                               WHERE NRANOORCAOP  = P_NRANOORCAOP
                                                 AND CDVERSORCAOP = P_CDVERSORCAOP
                                                 AND CDFILIAL     = C_FILIAL.CDFILIAL
                                                 AND IDTIPOORCAOP = 'O'
                                            ) LOOP
                       BEGIN
                         SELECT COUNT(CDORCAMENTOMO) AS ORCAFILI
                           INTO V_EXIST_ORCFIL
                           FROM ORCAMENTOMO
                          WHERE CDFILIAL = C_MAOOBRA_FILIAL.CDFILIAL;
                       EXCEPTION
                          WHEN NO_DATA_FOUND THEN
                             V_EXIST_ORCFIL := 0;
                       END;
                       /*Verifica se existe MO por filial*/
                       IF V_EXIST_ORCFIL > 0 THEN
                          BEGIN
                             SELECT COUNT(CDORCAMENTOMO)
                               INTO V_EXIST_MOFIL
                               FROM DESPORCAMO
                              WHERE CDFILIAL = C_MAOOBRA_FILIAL.CDFILIAL
                                AND CDDESPADMFAT = C_MAOOBRA_FILIAL.CDDESPADMFAT;
                          EXCEPTION
                             WHEN NO_DATA_FOUND THEN
                                V_EXIST_MOFIL := 0;
                          END;
                          IF V_EXIST_MOFIL > 0 THEN
                             FOR C_ITEMMO IN(SELECT DE.CDDESPADMFAT,DM.CDORCAMENTOMO
                                               FROM DESPORCAMO DM, DESPADMFATUR DE
                                              WHERE DM.CDDESPADMFAT = DE.CDDESPADMFAT
                                                AND DM.CDDESPADMFAT = C_MAOOBRA_FILIAL.CDDESPADMFAT
                                                AND CDFILIAL = C_MAOOBRA_FILIAL.CDFILIAL
                                             )LOOP
                                   V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                                   INSERT INTO ITEMORCAOP
                                    (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO, CDDESPADMFAT,CDORCAMENTOMO, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO)
                                   VALUES
                                    (P_NRANOORCAOP, P_CDVERSORCAOP, C_FILIAL.CDFILIAL, V_NRITEMORCA, '','', '',C_MAOOBRA_FILIAL.CDDESPADMFAT,C_ITEMMO.CDORCAMENTOMO,'',SYSDATE, 'O','P');
                             END LOOP;
                          ELSE
                             BEGIN
                                SELECT COUNT(CDORCAMENTOMO)
                                  INTO V_EXIST_MOGER
                                  FROM DESPORCAMO
                                 WHERE CDFILIAL = 'T'
                                   AND CDDESPADMFAT = C_MAOOBRA_FILIAL.CDDESPADMFAT;
                              EXCEPTION
                                 WHEN NO_DATA_FOUND THEN
                                    V_EXIST_MOGER := 0;
                              END;
                          END IF;
                       END IF;
                    END LOOP;
                    /*2 - Orcamento de MO Geral*/
                    FOR C_GERALMO IN( SELECT C_FILIAL.CDFILIAL,DE.CDDESPADMFAT,DM.CDORCAMENTOMO
                                       FROM DESPORCAMO DM, DESPADMFATUR DE
                                      WHERE DM.CDDESPADMFAT = DE.CDDESPADMFAT
                                        AND CDFILIAL = 'T'
                                     MINUS
                                     SELECT C_FILIAL.CDFILIAL, CDDESPADMFAT ,CDORCAMENTOMO
                                       FROM ITEMORCAOP
                                      WHERE NRANOORCAOP  = P_NRANOORCAOP
                                        AND CDVERSORCAOP = P_CDVERSORCAOP
                                        AND CDFILIAL     = C_FILIAL.CDFILIAL
                                        AND IDTIPOORCAOP = 'O'
                                     )LOOP
                                    SELECT COUNT(CDORCAMENTOMO)
                                      INTO V_EXIST_MOFIL
                                      FROM DESPORCAMO
                                     WHERE CDFILIAL     = C_FILIAL.CDFILIAL
                                       AND CDDESPADMFAT = C_GERALMO.CDDESPADMFAT;
                                   V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                                   INSERT INTO ITEMORCAOP
                                    (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO, CDDESPADMFAT,CDORCAMENTOMO, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO)
                                   VALUES
                                    (P_NRANOORCAOP, P_CDVERSORCAOP, C_FILIAL.CDFILIAL, V_NRITEMORCA, '','', '', C_GERALMO.CDDESPADMFAT, C_GERALMO.CDORCAMENTOMO,'',SYSDATE, 'O','P');
                    END LOOP;
                    FOR C_TAXAADM IN(SELECT DISTINCT DECODE(CDFILIAL, 'T',  C_FILIAL.CDFILIAL , CDFILIAL) AS CDFILIAL
                                           FROM ITEMORCAOP
                                          WHERE CDFILIAL = C_FILIAL.CDFILIAL
                                          MINUS
                                         SELECT C_FILIAL.CDFILIAL
                                           FROM ITEMORCAOP
                                          WHERE NRANOORCAOP  = P_NRANOORCAOP
                                            AND CDVERSORCAOP = P_CDVERSORCAOP
                                            AND CDFILIAL = C_FILIAL.CDFILIAL
                                            AND IDTIPOORCAOP = 'T') LOOP
                             V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                             INSERT INTO ITEMORCAOP
                              (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO ,CDDESPADMFAT, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO)
                             VALUES
                              (P_NRANOORCAOP, P_CDVERSORCAOP, C_TAXAADM.CDFILIAL, V_NRITEMORCA, '','', '','','',SYSDATE, 'T','P');
                    END LOOP;
                    FOR C_DIVERSAS IN (SELECT C_FILIAL.CDFILIAL, CDDESPPREV
                                          FROM DESPPREV
                                         WHERE IDDESPESAMATRIZ <> 'S'
                                           AND IDDESPLANENTR <> 'S'
                                           AND IDATIVO = 'S'
                                         MINUS
                                        SELECT C_FILIAL.CDFILIAL,CDDESPPREV
                                          FROM ITEMORCAOP
                                         WHERE NRANOORCAOP  = P_NRANOORCAOP
                                           AND CDVERSORCAOP = P_CDVERSORCAOP
                                           AND CDFILIAL     = C_FILIAL.CDFILIAL
                                           AND IDTIPOORCAOP IN ('D')) LOOP
                       V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                       INSERT INTO ITEMORCAOP
                        (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO, CDDESPADMFAT, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO)
                       VALUES
                        (P_NRANOORCAOP, P_CDVERSORCAOP, C_FILIAL.CDFILIAL, V_NRITEMORCA, '','', '', '', C_DIVERSAS.CDDESPPREV, SYSDATE, 'D','P');
                     END LOOP;
                     FOR C_MATRIZ IN (SELECT C_FILIAL.CDFILIAL, CDDESPPREV
                                        FROM DESPPREV
                                       WHERE IDDESPESAMATRIZ =  'S'
                                         AND IDDESPLANENTR   <> 'S'
                                         AND IDATIVO         =  'S'
                                       MINUS
                                      SELECT C_FILIAL.CDFILIAL, CDDESPPREV
                                        FROM ITEMORCAOP
                                       WHERE NRANOORCAOP  = P_NRANOORCAOP
                                         AND CDVERSORCAOP = P_CDVERSORCAOP
                                         AND CDFILIAL     = C_FILIAL.CDFILIAL
                                         AND IDTIPOORCAOP IN ('M')) LOOP
                       V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                       INSERT INTO ITEMORCAOP
                        (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO, CDDESPADMFAT, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO)
                       VALUES
                        (P_NRANOORCAOP, P_CDVERSORCAOP, C_FILIAL.CDFILIAL, V_NRITEMORCA, '','', '', '', C_MATRIZ.CDDESPPREV,SYSDATE, 'M','P');
                     END LOOP;
                     SELECT IDORCDESPENTROP
                       INTO V_ORCDESPENTROP
                       FROM PARAMORCAOPERAC;
                     IF V_ORCDESPENTROP = 'O' THEN
                        FOR C_ENTRADAS_0 IN (SELECT C_FILIAL.CDFILIAL, CDTIPOOPER
                                               FROM TIPOOPERACROP
                                              WHERE IDTPOPERACROP = 'DE'
                                                AND IDENTRSAIDOP  = 'E'
                                                AND CDFILIAL = V_FILIALPAR
                                           MINUS
                                             SELECT C_FILIAL.CDFILIAL, CDTIPOOPER
                                               FROM ITEMORCAOP
                                              WHERE NRANOORCAOP  = P_NRANOORCAOP
                                                AND CDVERSORCAOP = P_CDVERSORCAOP
                                                AND CDFILIAL = C_FILIAL.CDFILIAL
                                                AND IDTIPOORCAOP = 'E'
                                                AND IDENTRSAIDOP = 'E' )LOOP
                          V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                          INSERT INTO ITEMORCAOP
                           (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO, CDDESPADMFAT, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO)
                          VALUES
                           (P_NRANOORCAOP, P_CDVERSORCAOP, C_FILIAL.CDFILIAL, V_NRITEMORCA, 'E',C_ENTRADAS_0.CDTIPOOPER, '', '','',SYSDATE, 'E','P');
                        END LOOP;
                     ELSE
                        BEGIN
                           SELECT G.IDUTPADRROP, R.IDUTPADRROPFIL
                             INTO V_IDUTPADRROP, V_IDUTPADRROPFIL
                             FROM PARAMGERAL G, PARFILIALROP R
                            WHERE R.CDFILIAL = C_FILIAL.CDFILIAL;
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN BEGIN
                              V_IDUTPADRROP := 'S';
                              V_IDUTPADRROPFIL := 'S';
                           END;
                        END;
                        FOR C_ENTRADAS_D2 IN(
                            SELECT C_FILIAL.CDFILIAL,DP.CDDESPPREV
                              FROM DESPPREV DP
                             WHERE (DP.IDDESPESAMATRIZ <> 'S')
                               AND (DP.IDDESPLANENTR = 'S')
                               AND (DP.IDATIVO = 'S')
                             MINUS
                            SELECT C_FILIAL.CDFILIAL, CDDESPPREV
                              FROM ITEMORCAOP
                             WHERE NRANOORCAOP  = P_NRANOORCAOP
                               AND CDVERSORCAOP = P_CDVERSORCAOP
                               AND CDFILIAL     = C_FILIAL.CDFILIAL
                               AND IDTIPOORCAOP IN ('E')
                               ) LOOP
                             V_NRITEMORCA := LPAD((TO_NUMBER(NVL(V_NRITEMORCA,'0')) + 1), 10, 0);
                             INSERT INTO ITEMORCAOP
                             (NRANOORCAOP, CDVERSORCAOP, CDFILIAL, NRSEQITEMORCA, IDENTRSAIDOP, CDTIPOOPER, CDIMPOSTO, CDDESPADMFAT, CDDESPPREV, DTORCAMENTOITEM, IDTIPOORCAOP, IDITEMORCAMENTO)
                             VALUES
                             (P_NRANOORCAOP, P_CDVERSORCAOP, C_FILIAL.CDFILIAL, V_NRITEMORCA, '','', '', '', C_ENTRADAS_D2.CDDESPPREV, SYSDATE, 'E','P');
                  END LOOP;
               END IF;
            END IF;
         END IF;
      END LOOP;
   ELSE
      /* MOBILE */
      FOR C_ORCAMENTO IN (SELECT F.CDFILIAL, NVL(F.IDUTILPERCFILGOP,'N') IDUTILPERCFILGOP, TIPOPARAM_DRO(F.CDFILIAL) FILIALPAR,
                                 DECODE(P.IDUTILDTPROC, 'S', NVL(PF.DTPROCESSA, SYSDATE), SYSDATE) DTPROC
                            FROM FILIAL F INNER JOIN PARAMFILIAL PF ON PF.CDFILIAL = F.CDFILIAL
                                          CROSS JOIN PARAMGERAL P
                           WHERE EXISTS (SELECT 1
                                           FROM FILIALOPEROP FO INNER JOIN LISTA LF ON FO.CDOPERADOR = LF.CDLISTA
                                          WHERE LF.NRLISTA = P_CDOPERADOR
                                            AND FO.IDACESSOCALC = 'S'
                                            AND FO.CDFILIAL = F.CDFILIAL)
                             AND EXISTS (SELECT 1
                                           FROM FILIALOPEROP FO INNER JOIN LISTA LF ON FO.CDFILIAL = LF.CDLISTA
                                          WHERE LF.NRLISTA = P_CDFILIAL
                                            AND FO.IDACESSOCALC = 'S'
                                            AND FO.CDFILIAL = F.CDFILIAL)) LOOP                                        

         C_ORCAMENTO_CDFILIAL := C_ORCAMENTO.CDFILIAL;
         V_IDUTILPERCFILGOP := C_ORCAMENTO.IDUTILPERCFILGOP;

         SELECT NVL(MAX(IDPERIODOORCA), 'ANUAL'), NVL(MAX(IDORCDESPENTROP), 'D')
           INTO V_IDPERIODOORCA, V_ORCDESPENTROP
           FROM PARAMORCAOPERAC;

         DELETE FROM LOGPREPARAORCAGOP
               WHERE NRANOORCAGOP = P_NRANOORCAOP
                 AND CDFILIAL = C_ORCAMENTO.CDFILIAL;

         V_IDEXCLUI := P_IDEXCLUI;
         
         V_ERRO := PREPARA_ESTRUTURA(C_ORCAMENTO.CDFILIAL, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, P_IDEXCLUI);
         
         SELECT MAX(NRANOORCAGOP)
           INTO V_ERRO
           FROM LOGPREPARAORCAGOP
          WHERE NRANOORCAGOP = P_NRANOORCAOP
            AND CDFILIAL = C_ORCAMENTO.CDFILIAL;
            
         IF V_ERRO IS NOT NULL THEN
            CONTINUE;
         END IF;
         
         BEGIN
            SELECT O.NRORCAMENTOGOP
              INTO V_NRORCAMENTOGOP
              FROM ORCAMENTOGOP O
             WHERE O.NRANOORCAGOP  = P_NRANOORCAOP
               AND O.IDPERIODOORCA = V_IDPERIODOORCA
               AND O.NRPERIODOORCA = P_CDVERSORCAOP
               AND O.CDFILIAL      = C_ORCAMENTO.CDFILIAL;
         EXCEPTION
           WHEN NO_DATA_FOUND THEN BEGIN
              V_NRORCAMENTOGOP := NULL;
           END;
         END;
         
         IF V_NRORCAMENTOGOP IS NULL THEN
            NOVO_CODIGO('ORCAMENTOGOP', V_NRORCAMENTOGOP);
            
            INSERT INTO ORCAMENTOGOP
                       (NRORCAMENTOGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL)
                 VALUES
                       (V_NRORCAMENTOGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, C_ORCAMENTO.CDFILIAL);
         END IF;
         
         /* FATURAMENTO */
         IF SUBSTR(P_ITEMORCA,1,1) = 'S' THEN
            V_AUXEDIT := 'SIM';
            BEGIN
               SELECT IDSERVFATROP
                 INTO V_IDSERVFATROP
                 FROM PARFILIALROP P
                WHERE P.CDFILIAL = C_ORCAMENTO.FILIALPAR;
            EXCEPTION
              WHEN NO_DATA_FOUND THEN BEGIN
                V_IDSERVFATROP := NULL;
              END;
            END;

            SELECT MAX(NRITEMORCAOP)
              INTO V_NRITEMORCAGOP
              FROM ITEMORCAGOP
             WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
               AND CDFILIAL = C_ORCAMENTO.CDFILIAL
               AND IDTIPOORCAOP = 'FATURAMENTO';

            IF V_NRITEMORCAGOP IS NULL THEN
               NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);
               INSERT INTO ITEMORCAGOP
                           (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                            CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                            DTMESBASEITEM, IDTIPOORCAOP)
                      (SELECT V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', '',
                              '', C_ORCAMENTO.CDFILIAL, '', '', '',
                              '', 'FATURAMENTO'
                         FROM DUAL
                        MINUS
                       SELECT V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', '',
                              '', C_ORCAMENTO.CDFILIAL, '', '', '',
                              '', 'FATURAMENTO'
                         FROM ITEMORCAGOP
                        WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                          AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                          AND IDTIPOORCAOP = 'FATURAMENTO');
            ELSE
               V_AUXEDIT := PERMITE_EDICAO(V_NRITEMORCAGOP);
            END IF;
            
            IF V_AUXEDIT = 'SIM' THEN
               PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, V_NRITEMORCAGOP, 'S', 0,
                           NULL, NULL, NULL, NULL, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
            ELSE
               NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);
               
               INSERT INTO LOGPREPARAORCAGOP
                           (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                    VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, C_ORCAMENTO.CDFILIAL, 'ORCAMENTO_FATURAM_CONCLUIDO');
            END IF;
         END IF;
         
         /* IMPOSTO */
         IF SUBSTR(P_ITEMORCA,2,1) = 'S' THEN
            V_AUXEDIT := 'SIM';
            
            SELECT MAX(NRITEMORCAOP)
              INTO V_NRITEMORCAGOP
              FROM ITEMORCAGOP
             WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
               AND CDFILIAL = C_ORCAMENTO.CDFILIAL
               AND IDTIPOORCAOP = 'IMPOSTO';
            
            IF V_NRITEMORCAGOP IS NOT NULL THEN
               V_AUXEDIT := PERMITE_EDICAO(V_NRITEMORCAGOP);
            END IF;
            
            IF V_AUXEDIT = 'SIM' THEN                                                        
               FOR C_IMPOSTO IN (SELECT NVL(O.CDFILIAL,C_ORCAMENTO.CDFILIAL) CDFILIAL, NVL(O.CDIMPOSTO, NULL) CDIMPOSTO
                                   FROM DUAL LEFT JOIN (SELECT C_ORCAMENTO.CDFILIAL CDFILIAL, PD.CDIMPOSTO
                                                          FROM PERCITEMDETALGOP PD INNER JOIN PERCITEMGOP P ON PD.NRPERCITEMGOP = P.NRPERCITEMGOP
                                                         WHERE P.NRSEQITEMORCA = 1
                                                           AND C_ORCAMENTO.IDUTILPERCFILGOP = 'N'
                                                         UNION
                                                        SELECT PDF.CDFILIAL, PDF.CDIMPOSTO
                                                          FROM PERCITFILGOP PF LEFT JOIN PERCITDETALFILGOP PDF ON PDF.NRPERCITFILGOP = PF.NRPERCITFILGOP
                                                         WHERE PDF.CDFILIAL = C_ORCAMENTO.CDFILIAL
                                                           AND PF.NRSEQITEMORCA = 1
                                                           AND C_ORCAMENTO.IDUTILPERCFILGOP = 'S') O ON O.CDFILIAL = C_ORCAMENTO.CDFILIAL
                                  MINUS
                                 SELECT C_ORCAMENTO.CDFILIAL CDFILIAL, CDIMPOSTO
                                   FROM ITEMORCAGOP
                                  WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                    AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                    AND IDTIPOORCAOP = 'IMPOSTO') LOOP

                  NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);
                  INSERT INTO ITEMORCAGOP
                              (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                               CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                               DTMESBASEITEM, IDTIPOORCAOP)
                       VALUES (V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', '',
                               '', C_IMPOSTO.CDFILIAL, '', '', C_IMPOSTO.CDIMPOSTO,
                               '', 'IMPOSTO');
               END LOOP;
               
               FOR C_ITEMORCA IN (SELECT NRITEMORCAOP, CDIMPOSTO
                                    FROM ITEMORCAGOP
                                   WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                     AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                     AND IDTIPOORCAOP = 'IMPOSTO') LOOP              

                  PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, C_ITEMORCA.NRITEMORCAOP, 'N', 1,
                              NULL, NULL, NULL, NULL, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
               END LOOP;
            ELSE
               NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);
               
               INSERT INTO LOGPREPARAORCAGOP
                           (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                    VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, C_ORCAMENTO.CDFILIAL, 'ORCAMENTO_IMPOSTO_CONCLUIDO');
            END IF;
         END IF;

         /* CONSUMO */
         IF SUBSTR(P_ITEMORCA,3,1) = 'S' THEN
            V_AUXEDIT := 'SIM';
            
            BEGIN
               SELECT IDSERVFATROP
                 INTO V_IDSERVFATROP
                 FROM PARFILIALROP P
                WHERE P.CDFILIAL = C_ORCAMENTO.FILIALPAR;
            EXCEPTION
              WHEN NO_DATA_FOUND THEN BEGIN
                V_IDSERVFATROP := NULL;
              END;
            END;

            SELECT MAX(NRITEMORCAOP)
              INTO V_NRITEMORCAGOP
              FROM ITEMORCAGOP
             WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
               AND CDFILIAL = C_ORCAMENTO.CDFILIAL
               AND IDTIPOORCAOP = 'CONSUMO';

            IF V_NRITEMORCAGOP IS NULL THEN
               NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);
               INSERT INTO ITEMORCAGOP
                           (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                            CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                            DTMESBASEITEM, IDTIPOORCAOP)
                      (SELECT V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', '',
                              '', C_ORCAMENTO.CDFILIAL, '', '', '',
                              '', 'CONSUMO'
                         FROM DUAL
                        MINUS
                       SELECT V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', '',
                              '', C_ORCAMENTO.CDFILIAL, '', '', '',
                              '', 'CONSUMO'
                         FROM ITEMORCAGOP
                        WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                          AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                          AND IDTIPOORCAOP = 'CONSUMO');
            ELSE
               V_AUXEDIT := PERMITE_EDICAO(V_NRITEMORCAGOP);
            END IF;
            
            IF V_AUXEDIT = 'SIM' THEN
               PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, V_NRITEMORCAGOP, 'S', 2,
                           NULL, C_ORCAMENTO.IDUTILPERCFILGOP, NULL, NULL, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
            ELSE
               NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);
               
               INSERT INTO LOGPREPARAORCAGOP
                           (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                    VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, C_ORCAMENTO.CDFILIAL, 'ORCAMENTO_CONSUMO_CONCLUIDO');
            END IF;
         END IF;

         /* MAO OBRA */
         IF SUBSTR(P_ITEMORCA,4,1) = 'S' THEN
            V_AUXEDIT := 'SIM';
            
            SELECT MAX(NRITEMORCAOP)
              INTO V_NRITEMORCAGOP
              FROM ITEMORCAGOP
             WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
               AND CDFILIAL = C_ORCAMENTO.CDFILIAL
               AND IDTIPOORCAOP = 'MAOOBRA';
               
            IF V_NRITEMORCAGOP IS NOT NULL THEN
               V_AUXEDIT := PERMITE_EDICAO(V_NRITEMORCAGOP);
            END IF;
            
            IF V_AUXEDIT = 'SIM' THEN
               FOR C_MAOOBRA IN (SELECT NVL(O.CDFILIAL,C_ORCAMENTO.CDFILIAL) CDFILIAL, NVL(O.CDORCAMENTOMO, NULL) CDORCAMENTOMO, NVL(O.CDFILIALMO, NULL) CDFILIALMO
                                   FROM DUAL LEFT JOIN (SELECT C_ORCAMENTO.CDFILIAL CDFILIAL, PD.CDORCAMENTOMO, 'T' CDFILIALMO
                                                          FROM PERCITEMDETALGOP PD INNER JOIN PERCITEMGOP P ON PD.NRPERCITEMGOP = P.NRPERCITEMGOP
                                                                                   LEFT  JOIN PERCDETALPRODGOP PDP ON PD.NRPERCITEMDETALGOP = PDP.NRPERCITEMDETALGOP
                                                         WHERE P.NRSEQITEMORCA = 3
                                                           AND C_ORCAMENTO.IDUTILPERCFILGOP = 'N'
                                                         GROUP BY C_ORCAMENTO.CDFILIAL, PD.CDORCAMENTOMO, 'T'
                                                         UNION
                                                        SELECT PDF.CDFILIAL, PDF.CDORCAMENTOMO, PDF.CDFILIALMO
                                                          FROM PERCITFILGOP PF INNER JOIN PERCITDETALFILGOP PDF ON PDF.NRPERCITFILGOP = PF.NRPERCITFILGOP
                                                                               LEFT  JOIN PERCDETPRODFILGOP PDP ON PDP.NRPERCITDETALFILGOP = PDF.NRPERCITDETALFILGOP
                                                         WHERE PDF.CDFILIAL = C_ORCAMENTO.CDFILIAL
                                                           AND PF.NRSEQITEMORCA = 3
                                                           AND C_ORCAMENTO.IDUTILPERCFILGOP = 'S'
                                                         GROUP BY PDF.CDFILIAL, PDF.CDORCAMENTOMO, PDF.CDFILIALMO) O ON O.CDFILIAL = C_ORCAMENTO.CDFILIAL
                                  MINUS
                                 SELECT C_ORCAMENTO.CDFILIAL CDFILIAL, CDORCAMENTOMO, CDFILIALMO
                                   FROM ITEMORCAGOP
                                  WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                    AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                    AND IDTIPOORCAOP = 'MAOOBRA') LOOP

                  NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);
                  INSERT INTO ITEMORCAGOP
                              (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                               CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                               DTMESBASEITEM, IDTIPOORCAOP)
                       VALUES (V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', C_MAOOBRA.CDORCAMENTOMO,
                               NULL, C_ORCAMENTO.CDFILIAL, C_MAOOBRA.CDFILIALMO, '', '',
                               '', 'MAOOBRA');
               END LOOP;
               
               FOR C_ITEMORCA IN (SELECT NRITEMORCAOP, CDORCAMENTOMO, CDFILIALMO
                                    FROM ITEMORCAGOP
                                   WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                     AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                     AND IDTIPOORCAOP = 'MAOOBRA') LOOP               

                  PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, C_ITEMORCA.NRITEMORCAOP, 'S', 3, NULL, C_ORCAMENTO.IDUTILPERCFILGOP,
                              C_ITEMORCA.CDORCAMENTOMO, C_ITEMORCA.CDFILIALMO, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
               END LOOP;               
            ELSE
               NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);
               
               INSERT INTO LOGPREPARAORCAGOP
                           (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                    VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, C_ORCAMENTO.CDFILIAL, 'ORCAMENTO_MAOOBRA_CONCLUIDO');
            END IF;
         END IF;

         /* TAXA ADMIN */
         IF SUBSTR(P_ITEMORCA,5,1) = 'S' THEN
            V_AUXEDIT := 'SIM';
            
            SELECT MAX(NRITEMORCAOP)
              INTO V_NRITEMORCAGOP
              FROM ITEMORCAGOP
             WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
               AND CDFILIAL = C_ORCAMENTO.CDFILIAL
               AND IDTIPOORCAOP = 'TAXAADM';
               
            IF V_NRITEMORCAGOP IS NOT NULL THEN
               V_AUXEDIT := PERMITE_EDICAO(V_NRITEMORCAGOP);
            END IF;
            
            IF V_AUXEDIT = 'SIM' THEN
               FOR C_TAXAADM IN (SELECT C_ORCAMENTO.CDFILIAL AS CDFILIAL
                                   FROM DUAL
                                  MINUS
                                 SELECT C_ORCAMENTO.CDFILIAL
                                   FROM ITEMORCAGOP
                                  WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                    AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                    AND IDTIPOORCAOP = 'TAXAADM') LOOP

                  NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);
                  INSERT INTO ITEMORCAGOP
                              (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                               CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                               DTMESBASEITEM, IDTIPOORCAOP)
                       VALUES (V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', '',
                               '', C_TAXAADM.CDFILIAL, '', '', '',
                               '', 'TAXAADM');
               END LOOP;
               
               PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, V_NRITEMORCAGOP, 'N', 4,
                           NULL, NULL, NULL, NULL, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
            ELSE
               NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);
               
               INSERT INTO LOGPREPARAORCAGOP
                           (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                    VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, C_ORCAMENTO.CDFILIAL, 'ORCAMENTO_TAXAADM_CONCLUIDO');
            END IF;
         END IF;

         /* DESPESA MATRIZ */
         IF SUBSTR(P_ITEMORCA,6,1) = 'S' THEN
            V_AUXEDIT := 'SIM';
            
            SELECT MAX(NRITEMORCAOP)
              INTO V_NRITEMORCAGOP
              FROM ITEMORCAGOP
             WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
               AND CDFILIAL = C_ORCAMENTO.CDFILIAL
               AND IDTIPOORCAOP = 'DESPMATRIZ';
               
            IF V_NRITEMORCAGOP IS NOT NULL THEN
               V_AUXEDIT := PERMITE_EDICAO(V_NRITEMORCAGOP);
            END IF;
            
            IF V_AUXEDIT = 'SIM' THEN
               FOR C_MATRIZ IN (SELECT NVL(O.CDFILIAL,C_ORCAMENTO.CDFILIAL) CDFILIAL, NVL(O.CDDESPPREV, NULL) CDDESPPREV
                                  FROM DUAL LEFT JOIN (SELECT C_ORCAMENTO.CDFILIAL CDFILIAL, PD.CDDESPPREV
                                                         FROM PERCITEMDETALGOP PD INNER JOIN PERCITEMGOP P ON PD.NRPERCITEMGOP = P.NRPERCITEMGOP
                                                        WHERE P.NRSEQITEMORCA = 5
                                                          AND C_ORCAMENTO.IDUTILPERCFILGOP = 'N'
                                                        UNION
                                                       SELECT PDF.CDFILIAL, PDF.CDDESPPREV
                                                         FROM PERCITFILGOP PF LEFT JOIN PERCITDETALFILGOP PDF ON PDF.NRPERCITFILGOP = PF.NRPERCITFILGOP
                                                        WHERE PDF.CDFILIAL = C_ORCAMENTO.CDFILIAL
                                                          AND PF.NRSEQITEMORCA = 5
                                                          AND C_ORCAMENTO.IDUTILPERCFILGOP = 'S') O ON O.CDFILIAL = C_ORCAMENTO.CDFILIAL
                                 MINUS
                                SELECT C_ORCAMENTO.CDFILIAL CDFILIAL, CDDESPPREV
                                  FROM ITEMORCAGOP
                                 WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                   AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                   AND IDTIPOORCAOP = 'DESPMATRIZ') LOOP

                  NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);
                  INSERT INTO ITEMORCAGOP
                              (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                               CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                               DTMESBASEITEM, IDTIPOORCAOP)
                       VALUES (V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', '',
                               C_MATRIZ.CDDESPPREV, C_ORCAMENTO.CDFILIAL, '', '', '',
                               '', 'DESPMATRIZ');
               END LOOP;
               
               FOR C_MATRIZ IN (SELECT NRITEMORCAOP, CDDESPPREV
                                  FROM ITEMORCAGOP
                                 WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                   AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                   AND IDTIPOORCAOP = 'DESPMATRIZ'
                                 GROUP BY NRITEMORCAOP, CDDESPPREV) LOOP

                  PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, C_MATRIZ.NRITEMORCAOP, 'S', 5, 
                              C_MATRIZ.CDDESPPREV, C_ORCAMENTO.IDUTILPERCFILGOP, NULL, NULL, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
               END LOOP;
            ELSE
               NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);
               
               INSERT INTO LOGPREPARAORCAGOP
                           (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                    VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, C_ORCAMENTO.CDFILIAL, 'ORCAMENTO_DESPMATRIZ_CONCLUIDO');
            END IF;
         END IF;

         /* DESPESA DIVERSA */
         IF SUBSTR(P_ITEMORCA,7,1) = 'S' THEN
            V_AUXEDIT := 'SIM';
            
            SELECT MAX(NRITEMORCAOP)
              INTO V_NRITEMORCAGOP
              FROM ITEMORCAGOP
             WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
               AND CDFILIAL = C_ORCAMENTO.CDFILIAL
               AND IDTIPOORCAOP = 'DESPDIVERSA';
               
            IF V_NRITEMORCAGOP IS NOT NULL THEN
               V_AUXEDIT := PERMITE_EDICAO(V_NRITEMORCAGOP);
            END IF;
            
            IF V_AUXEDIT = 'SIM' THEN
               FOR C_DIVERSAS IN (SELECT NVL(O.CDFILIAL,C_ORCAMENTO.CDFILIAL) CDFILIAL, NVL(O.CDDESPPREV, NULL) CDDESPPREV
                                    FROM DUAL LEFT JOIN (SELECT C_ORCAMENTO.CDFILIAL CDFILIAL, PD.CDDESPPREV
                                                           FROM PERCITEMDETALGOP PD INNER JOIN PERCITEMGOP P ON PD.NRPERCITEMGOP = P.NRPERCITEMGOP
                                                          WHERE P.NRSEQITEMORCA = 6
                                                            AND C_ORCAMENTO.IDUTILPERCFILGOP = 'N'
                                                          UNION
                                                         SELECT PDF.CDFILIAL, PDF.CDDESPPREV
                                                           FROM PERCITFILGOP PF LEFT JOIN PERCITDETALFILGOP PDF ON PDF.NRPERCITFILGOP = PF.NRPERCITFILGOP
                                                          WHERE PDF.CDFILIAL = C_ORCAMENTO.CDFILIAL
                                                            AND PF.NRSEQITEMORCA = 6
                                                            AND C_ORCAMENTO.IDUTILPERCFILGOP = 'S') O ON O.CDFILIAL = C_ORCAMENTO.CDFILIAL
                                   MINUS
                                  SELECT C_ORCAMENTO.CDFILIAL CDFILIAL, CDDESPPREV
                                    FROM ITEMORCAGOP
                                   WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                     AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                     AND IDTIPOORCAOP = 'DESPDIVERSA') LOOP

                  NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);
                  INSERT INTO ITEMORCAGOP
                              (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                               CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                               DTMESBASEITEM, IDTIPOORCAOP)
                       VALUES (V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', '',
                               C_DIVERSAS.CDDESPPREV, C_ORCAMENTO.CDFILIAL, '', '', '',
                               '', 'DESPDIVERSA');
               END LOOP;
               
               FOR C_DIVERSA IN (SELECT NRITEMORCAOP, CDDESPPREV
                                   FROM ITEMORCAGOP
                                  WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                    AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                    AND IDTIPOORCAOP = 'DESPDIVERSA'
                                  GROUP BY NRITEMORCAOP, CDDESPPREV) LOOP

                  PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, C_DIVERSA.NRITEMORCAOP, 'S', 6, 
                              C_DIVERSA.CDDESPPREV, C_ORCAMENTO.IDUTILPERCFILGOP, NULL, NULL, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
               END LOOP;
            ELSE
               NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);
               
               INSERT INTO LOGPREPARAORCAGOP
                           (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                    VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, C_ORCAMENTO.CDFILIAL, 'ORCAMENTO_DESPDIVERSA_CONCLUIDO');
            END IF;
         END IF;

         /* DESPESA ENTRADA */
         IF SUBSTR(P_ITEMORCA,8,1) = 'S' THEN
            V_AUXEDIT := 'SIM';
            
            SELECT MAX(NRITEMORCAOP)
              INTO V_NRITEMORCAGOP
              FROM ITEMORCAGOP
             WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
               AND CDFILIAL = C_ORCAMENTO.CDFILIAL
               AND IDTIPOORCAOP = 'DESPENTRADA';
               
            IF V_NRITEMORCAGOP IS NOT NULL THEN
               V_AUXEDIT := PERMITE_EDICAO(V_NRITEMORCAGOP);
            END IF;
            
            IF V_AUXEDIT = 'SIM' THEN
               /* TIPO OPERACAO */                 
               IF V_ORCDESPENTROP = 'O' THEN
                  OPEN C_DESPENTRADA;
                  FETCH C_DESPENTRADA INTO V_CDTIPOOPER;

                  IF C_DESPENTRADA%ROWCOUNT > 0 THEN
                     LOOP
                        NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);
                        
                        INSERT INTO ITEMORCAGOP
                                    (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                                     CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                                     DTMESBASEITEM, IDTIPOORCAOP)
                             VALUES (V_NRORCAMENTOGOP, V_NRITEMORCAGOP, 'E', V_CDTIPOOPER, '',
                                    '', C_ORCAMENTO.CDFILIAL, '', '', '',
                                    '', 'DESPENTRADA');

                        FETCH C_DESPENTRADA INTO V_CDTIPOOPER;
                        EXIT WHEN C_DESPENTRADA%NOTFOUND;
                     END LOOP;
                  ELSE
                     NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);

                     INSERT INTO ITEMORCAGOP
                                 (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                                  CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                                  DTMESBASEITEM, IDTIPOORCAOP)
                          VALUES (V_NRORCAMENTOGOP, V_NRITEMORCAGOP, 'E', '', '',
                                  '', C_ORCAMENTO.CDFILIAL, '', '', '',
                                  '', 'DESPENTRADA');
                  END IF;
                  CLOSE C_DESPENTRADA;
                  
                  FOR C_ITEMORCA IN (SELECT NRITEMORCAOP, CDTIPOOPER
                                       FROM ITEMORCAGOP
                                      WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                        AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                        AND IDTIPOORCAOP = 'DESPENTRADA'
                                        AND IDENTRSAIDOP = 'E') LOOP
                  
                     PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, C_ITEMORCA.NRITEMORCAOP, 'N', 7, 
                                 NULL, C_ORCAMENTO.IDUTILPERCFILGOP, NULL, NULL, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
                  END LOOP;
               ELSE
                  /* DESPESA */
                  FOR C_ENTRADAS_D2 IN (SELECT NVL(O.CDFILIAL,C_ORCAMENTO.CDFILIAL) CDFILIAL, NVL(O.CDDESPPREV, NULL) CDDESPPREV
                                          FROM DUAL LEFT JOIN (SELECT C_ORCAMENTO.CDFILIAL CDFILIAL, PD.CDDESPPREV
                                                                 FROM PERCITEMDETALGOP PD INNER JOIN PERCITEMGOP P ON PD.NRPERCITEMGOP = P.NRPERCITEMGOP
                                                                WHERE P.NRSEQITEMORCA = 7
                                                                  AND C_ORCAMENTO.IDUTILPERCFILGOP = 'N'
                                                                UNION
                                                               SELECT PDF.CDFILIAL, PDF.CDDESPPREV
                                                                 FROM PERCITFILGOP PF LEFT JOIN PERCITDETALFILGOP PDF ON PDF.NRPERCITFILGOP = PF.NRPERCITFILGOP
                                                                WHERE PDF.CDFILIAL = C_ORCAMENTO.CDFILIAL
                                                                  AND PF.NRSEQITEMORCA = 7
                                                                  AND C_ORCAMENTO.IDUTILPERCFILGOP = 'S') O ON O.CDFILIAL = C_ORCAMENTO.CDFILIAL
                                         MINUS
                                        SELECT C_ORCAMENTO.CDFILIAL CDFILIAL, CDDESPPREV
                                          FROM ITEMORCAGOP
                                         WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                           AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                           AND IDTIPOORCAOP = 'DESPENTRADA') LOOP

                     NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);
                     INSERT INTO ITEMORCAGOP
                                 (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                                  CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                                  DTMESBASEITEM, IDTIPOORCAOP)
                          VALUES (V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', '',
                                  C_ENTRADAS_D2.CDDESPPREV, C_ORCAMENTO.CDFILIAL, '', '', '',
                                  '', 'DESPENTRADA');

                     PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, V_NRITEMORCAGOP, 'S', 7, 
                                 C_ENTRADAS_D2.CDDESPPREV, C_ORCAMENTO.IDUTILPERCFILGOP, NULL, NULL, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
                  END LOOP;
                  
                  FOR C_ENTRADAS IN (SELECT NRITEMORCAOP, CDDESPPREV
                                       FROM ITEMORCAGOP
                                      WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                        AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                        AND IDTIPOORCAOP = 'DESPENTRADA'
                                      GROUP BY NRITEMORCAOP, CDDESPPREV) LOOP

                     PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, C_ENTRADAS.NRITEMORCAOP, 'S', 7, 
                                 C_ENTRADAS.CDDESPPREV, C_ORCAMENTO.IDUTILPERCFILGOP, NULL, NULL, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
                  END LOOP;
               END IF;
            ELSE
               NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);
               
               INSERT INTO LOGPREPARAORCAGOP
                           (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                    VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, C_ORCAMENTO.CDFILIAL, 'ORCAMENTO_DESPENTRADA_CONCLUIDO');
            END IF;
         END IF;

         /* IMPOSTO A RECUPERAR */
         IF SUBSTR(P_ITEMORCA,9,1) = 'S' THEN
            V_AUXEDIT := 'SIM';
            
            SELECT MAX(NRITEMORCAOP)
              INTO V_NRITEMORCAGOP
              FROM ITEMORCAGOP
             WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
               AND CDFILIAL = C_ORCAMENTO.CDFILIAL
               AND IDTIPOORCAOP = 'IMPOSTOARECUPERAR';
               
            IF V_NRITEMORCAGOP IS NOT NULL THEN
               V_AUXEDIT := PERMITE_EDICAO(V_NRITEMORCAGOP);
            END IF;
            
            IF V_AUXEDIT = 'SIM' THEN            
               OPEN C_IMPOSTOARECUPERAR;
               FETCH C_IMPOSTOARECUPERAR INTO V_CDIMPOSTOREC;

               IF C_IMPOSTOARECUPERAR%ROWCOUNT > 0 THEN
                  LOOP
                     NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);
                     INSERT INTO ITEMORCAGOP
                                 (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                                  CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                                  DTMESBASEITEM, IDTIPOORCAOP)
                          VALUES (V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', '',
                                  '', C_ORCAMENTO.CDFILIAL, '', '', V_CDIMPOSTOREC,
                                  '', 'IMPOSTOARECUPERAR');
                                  
                     FETCH C_IMPOSTOARECUPERAR INTO V_CDIMPOSTOREC;
                     EXIT WHEN C_IMPOSTOARECUPERAR%NOTFOUND;
                  END LOOP;
               END IF;
               CLOSE C_IMPOSTOARECUPERAR;               
               
               FOR C_IMPOSTOREC IN (SELECT NRITEMORCAOP, CDIMPOSTO
                                      FROM ITEMORCAGOP
                                     WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                       AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                       AND IDTIPOORCAOP = 'IMPOSTOARECUPERAR') LOOP
                                       
                  PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, C_IMPOSTOREC.NRITEMORCAOP, 'N', 8,
                              NULL, NULL, NULL, NULL, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
               END LOOP;
            ELSE
               NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);
               
               INSERT INTO LOGPREPARAORCAGOP
                           (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                    VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, C_ORCAMENTO.CDFILIAL, 'ORCAMENTO_IMPOSTOARECUPERAR_CONCLUIDO');
            END IF;
         END IF;

         /* IMPOSTO SOBRE LUCRO */
         IF SUBSTR(P_ITEMORCA,10,1) = 'S' THEN
            V_AUXEDIT := 'SIM';
            
            SELECT MAX(NRITEMORCAOP)
              INTO V_NRITEMORCAGOP
              FROM ITEMORCAGOP
             WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
               AND CDFILIAL = C_ORCAMENTO.CDFILIAL
               AND IDTIPOORCAOP = 'IMPOSTOSOBLUCRO';
               
            IF V_NRITEMORCAGOP IS NOT NULL THEN
               V_AUXEDIT := PERMITE_EDICAO(V_NRITEMORCAGOP);
            END IF;
            
            IF V_AUXEDIT = 'SIM' THEN
               OPEN C_IMPOSTOSOBLUCRO;
               FETCH C_IMPOSTOSOBLUCRO INTO V_CDIMPOSTOSOB;

               IF C_IMPOSTOSOBLUCRO%ROWCOUNT > 0 THEN
                  LOOP
                     NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);

                     INSERT INTO ITEMORCAGOP
                                 (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                                  CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                                  DTMESBASEITEM, IDTIPOORCAOP)
                          VALUES (V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', '',
                                  '', C_ORCAMENTO.CDFILIAL, '', '', V_CDIMPOSTOSOB,
                                  '', 'IMPOSTOSOBLUCRO');

                     FETCH C_IMPOSTOSOBLUCRO INTO V_CDIMPOSTOSOB;
                     EXIT WHEN C_IMPOSTOSOBLUCRO%NOTFOUND;
                  END LOOP;
               END IF;
               CLOSE C_IMPOSTOSOBLUCRO;
               
               FOR C_IMPOSTOLUCRO IN (SELECT NRITEMORCAOP, CDIMPOSTO
                                        FROM ITEMORCAGOP
                                       WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                         AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                         AND IDTIPOORCAOP = 'IMPOSTOSOBLUCRO') LOOP
                                         
                  PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, C_IMPOSTOLUCRO.NRITEMORCAOP, 'N', 9,
                              NULL, NULL, NULL, NULL, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
               END LOOP;
            ELSE
               NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);
               
               INSERT INTO LOGPREPARAORCAGOP
                           (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                    VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, C_ORCAMENTO.CDFILIAL, 'ORCAMENTO_IMPOSTOSOBLUCRO_CONCLUIDO');
            END IF;
         END IF;

         /* INVESTIMENTO */
         IF SUBSTR(P_ITEMORCA,11,1) = 'S' THEN
            V_AUXEDIT := 'SIM';
            
            SELECT MAX(NRITEMORCAOP)
              INTO V_NRITEMORCAGOP
              FROM ITEMORCAGOP
             WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
               AND CDFILIAL = C_ORCAMENTO.CDFILIAL
               AND IDTIPOORCAOP = 'INVESTIMENTO';
               
            IF V_NRITEMORCAGOP IS NOT NULL THEN
               V_AUXEDIT := PERMITE_EDICAO(V_NRITEMORCAGOP);
            END IF;
            
            IF V_AUXEDIT = 'SIM' THEN
               FOR C_INVEST IN (SELECT NVL(O.CDFILIAL,C_ORCAMENTO.CDFILIAL) CDFILIAL, NVL(O.CDINVEST, NULL) CDINVEST
                                  FROM DUAL LEFT JOIN (SELECT C_ORCAMENTO.CDFILIAL CDFILIAL, PD.CDINVEST
                                                         FROM PERCITEMDETALGOP PD INNER JOIN PERCITEMGOP P ON PD.NRPERCITEMGOP = P.NRPERCITEMGOP
                                                        WHERE P.NRSEQITEMORCA = 10
                                                          AND C_ORCAMENTO.IDUTILPERCFILGOP = 'N'
                                                        UNION
                                                       SELECT PDF.CDFILIAL, PDF.CDINVEST
                                                         FROM PERCITFILGOP PF LEFT JOIN PERCITDETALFILGOP PDF ON PDF.NRPERCITFILGOP = PF.NRPERCITFILGOP
                                                        WHERE PDF.CDFILIAL = C_ORCAMENTO.CDFILIAL
                                                          AND PF.NRSEQITEMORCA = 10
                                                          AND C_ORCAMENTO.IDUTILPERCFILGOP = 'S') O ON O.CDFILIAL = C_ORCAMENTO.CDFILIAL
                                 MINUS
                                SELECT CDFILIAL, CDINVEST
                                  FROM ITEMORCAGOP
                                 WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                   AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                   AND IDTIPOORCAOP = 'INVESTIMENTO') LOOP

                  NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);
                  INSERT INTO ITEMORCAGOP
                              (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                               CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                               DTMESBASEITEM, IDTIPOORCAOP, CDINVEST)
                       VALUES (V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', '',
                               '', C_INVEST.CDFILIAL, '', '', '',
                               '', 'INVESTIMENTO', C_INVEST.CDINVEST);
               END LOOP;
               
               FOR C_INVESTIMENTO IN (SELECT NRITEMORCAOP, CDFILIAL, CDINVEST
                                        FROM ITEMORCAGOP
                                       WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                         AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                         AND IDTIPOORCAOP = 'INVESTIMENTO') LOOP
               
                  PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, C_INVESTIMENTO.NRITEMORCAOP, 'N', 10,
                              NULL, NULL, NULL, NULL, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
               END LOOP;
            ELSE
               NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);
               
               INSERT INTO LOGPREPARAORCAGOP
                           (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                    VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, C_ORCAMENTO.CDFILIAL, 'ORCAMENTO_INVESTIMENTO_CONCLUIDO');
            END IF;
         END IF;
         
         /* DEPRECIACAO */
         IF SUBSTR(P_ITEMORCA,12,1) = 'S' THEN
            V_AUXEDIT := 'SIM';
            
            SELECT MAX(NRITEMORCAOP)
              INTO V_NRITEMORCAGOP
              FROM ITEMORCAGOP
             WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
               AND CDFILIAL = C_ORCAMENTO.CDFILIAL
               AND IDTIPOORCAOP = 'DEPRECIACAO';
               
            IF V_NRITEMORCAGOP IS NOT NULL THEN
               V_AUXEDIT := PERMITE_EDICAO(V_NRITEMORCAGOP);
            END IF;
            
            IF V_AUXEDIT = 'SIM' THEN
               FOR C_DEPREC IN (SELECT C_ORCAMENTO.CDFILIAL AS CDFILIAL
                                  FROM DUAL
                                 MINUS
                                SELECT C_ORCAMENTO.CDFILIAL
                                  FROM ITEMORCAGOP
                                 WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                   AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                                   AND IDTIPOORCAOP = 'DEPRECIACAO') LOOP

                  NOVO_CODIGO('ITEMORCAGOP', V_NRITEMORCAGOP);
                  INSERT INTO ITEMORCAGOP
                              (NRORCAMENTOGOP, NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                               CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                               DTMESBASEITEM, IDTIPOORCAOP)
                       VALUES (V_NRORCAMENTOGOP, V_NRITEMORCAGOP, '', '', '',
                               '', C_DEPREC.CDFILIAL, '', '', '',
                               '', 'DEPRECIACAO');
               END LOOP;
               
               PREPARA_MES(C_ORCAMENTO.CDFILIAL, P_CDVERSORCAOP, V_NRITEMORCAGOP, 'N', 11,
                           NULL, NULL, NULL, NULL, C_ORCAMENTO.DTPROC, V_IDEXCLUI);
            ELSE
               NOVO_CODIGO('LOGPREPARAORCAGOP', V_NRLOGPREPARAORCAGOP);
               
               INSERT INTO LOGPREPARAORCAGOP
                           (NRLOGPREPARAORCAGOP, NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL, DSMOTIVO)
                    VALUES (V_NRLOGPREPARAORCAGOP, P_NRANOORCAOP, V_IDPERIODOORCA, P_CDVERSORCAOP, C_ORCAMENTO.CDFILIAL, 'ORCAMENTO_DEPRECIACAO_CONCLUIDO');
            END IF;
         END IF;
         
         DELETE FROM ITEMORCAGOP
               WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                 AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                 AND NOT EXISTS (SELECT 1
                                   FROM ITORCAMESGOP M
                                  WHERE ITEMORCAGOP.NRITEMORCAOP = M.NRITEMORCAOP);
         
         DELETE FROM ORCAMENTOGOP
               WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                 AND CDFILIAL = C_ORCAMENTO.CDFILIAL
                 AND NOT EXISTS (SELECT 1
                                   FROM ITEMORCAGOP I
                                  WHERE I.NRORCAMENTOGOP = ORCAMENTOGOP.NRORCAMENTOGOP);
                                  
         IF P_CDOPERLOGADO IS NOT NULL THEN
            -- LOG PREPARACAO ORCAMENTO
            V_OPERADOR := P_CDOPERLOGADO;

            SELECT NMOPERADOR
              INTO V_NMOPERADOR
              FROM OPERADOR
             WHERE CDOPERADOR = V_OPERADOR;

            V_DSSERVERRDM := 'gop00240_preparacao_do_orcamento';

            IF V_EXISTEORCA = 'N' THEN
               V_TIPO := '1';
            ELSE
               V_TIPO := '2';
            END IF;

            IF TO_CHAR(C_ORCAMENTO.CDFILIAL) = 'T' OR TO_CHAR(C_ORCAMENTO.CDFILIAL) =  'P' THEN
               V_UNIDADE := 'Unidade: Padrao';
            ELSE
               SELECT NMFILIAL
                 INTO V_NMFILIAL
                 FROM FILIAL
                WHERE CDFILIAL = C_ORCAMENTO.CDFILIAL;
                V_UNIDADE := 'Unidade: '|| C_ORCAMENTO.CDFILIAL || ' - ' || V_NMFILIAL;
            END IF;

            SELECT NRANOORCAGOP, NRPERIODOORCA
              INTO V_NRANOORCAGOP, V_NRPERIODOORCA
              FROM ORCAMENTOGOP
             WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP;

            V_DESC := 'Preparacao do Orcamento' || chr(10) || chr(10) || V_UNIDADE || chr(10);
            V_DESC :=  V_DESC || 'Tipo do Orcamento: ' || V_IDPERIODOORCA || chr(10)
                              || 'Ano: ' || V_NRANOORCAGOP || chr(10)
                              || 'Periodo: ' || STRPERIODO(V_NRPERIODOORCA, V_IDPERIODOORCA);
            V_ITENS := ' ';
            IF SUBSTR(P_ITEMORCA,1,1) = 'S' THEN
               V_ITENS := 'Faturamento, ';
            END IF;

            IF SUBSTR(P_ITEMORCA,2,1) = 'S' THEN
               V_ITENS := V_ITENS || 'Imposto, ';
            END IF;

            IF SUBSTR(P_ITEMORCA,3,1) = 'S' THEN
               V_ITENS := V_ITENS || 'Consumo, ';
            END IF;

            IF SUBSTR(P_ITEMORCA,4,1) = 'S' THEN
               V_ITENS := V_ITENS || 'Mao de Obra, ';
            END IF;

            IF SUBSTR(P_ITEMORCA,5,1) = 'S' THEN
               V_ITENS := V_ITENS || 'Taxa Administrativa, ';
            END IF;

            IF SUBSTR(P_ITEMORCA,6,1) = 'S' THEN
               V_ITENS := V_ITENS || 'Despesas Matriz, ';
            END IF;

            IF SUBSTR(P_ITEMORCA,7,1) = 'S' THEN
               V_ITENS := V_ITENS || 'Despesas Diversas, ';
            END IF;

            IF SUBSTR(P_ITEMORCA,8,1) = 'S' THEN
               V_ITENS := V_ITENS || 'Despesas Lancamento de Entrada, ';
            END IF;

            IF SUBSTR(P_ITEMORCA,9,1) = 'S' THEN
               V_ITENS := V_ITENS || 'Imposto a Recuperar, ';
            END IF;

            IF SUBSTR(P_ITEMORCA,10,1) = 'S' THEN
               V_ITENS := V_ITENS || 'Imposto sobre o Lucro, ';
            END IF;

            IF SUBSTR(P_ITEMORCA,11,1) = 'S' THEN
               V_ITENS := V_ITENS || 'Investimento, ';
            END IF;

            IF SUBSTR(P_ITEMORCA,12,1) = 'S' THEN
               V_ITENS := V_ITENS || 'Depreciacoes, ';
            END IF;

            V_DESC := V_DESC || chr(10) || 'Foi gerado o orcamento para os itens: ' || V_ITENS;
            IF V_IDEXCLUI = 'S' THEN
               V_DESC := V_DESC || 'substituindo o orcamento do periodo informado, caso existisse.';
            ELSE
               V_DESC := V_DESC || 'respeitando o orcamento do periodo informado, caso existisse.';
            END IF;

            V_NMTABELA := 'ORCAMENTOGOP';
            V_ITPESQ := V_NRORCAMENTOGOP;

            INSERT INTO LOGOPERAGES
               (NMTABELA,     DTOPERACAO,   NMRESPOPER,  IDTIPOOPER,   DSOPERACAO,  CDPESQITEM,
                DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER, CDFILLOGOPER)
            VALUES
               (V_NMTABELA, SYSDATE, NVL(V_NMOPERADOR,' '), V_TIPO, V_DESC, V_ITPESQ, NVL(V_DSSERVERRDM, ' '),
                ' ', ' ', ' ', NVL(P_CDOPERLOGADO, ' '),
                ' ');
         END IF;
      END LOOP;

      DELETE FROM LISTA WHERE NRLISTA = P_CDOPERADOR;
      DELETE FROM LISTA WHERE NRLISTA = P_CDFILIAL;
      COMMIT;
   END IF;
END;
/