CREATE OR REPLACE TRIGGER APROVAORCAGOP_VERSAOGOP
AFTER UPDATE OR INSERT
ON APROVAORCAGOP FOR EACH ROW
DECLARE
  V_NRVERSAOGOP           NUMBER;
  V_CDVERSAOGOP           NUMBER;
  V_CDFILIAL              VARCHAR2(04);
  V_NRANOORCAGOP          NUMBER;
  V_IDPERIODOORCA         VARCHAR2(30);
  V_NRPERIODOORCA         NUMBER;
  V_IDUTILVERSAOORCA      VARCHAR2(01);

  V_IDITEMVERSAOGOP       VARCHAR2(20);
  V_VRITEMVERSAO          NUMBER;
  V_NRTIPOITVERSAOGOP     NUMBER;
  V_NRITVERSAOGOP         NUMBER;
  V_CDITVERSAOGOP         NUMBER;

  V_TIPOIDITEMVERSAOGOP   VARCHAR2(100);
  V_TIPONRITVERSAOGOP     NUMBER;
  V_VRTIPOITVERSAOGOP     NUMBER;
  V_CDTIPOITVERSAOGOP     VARCHAR2(50);
  V_NMTIPOITVERSAOGOP     VARCHAR(100);

  V_NRPRODTIPOITVERSAOGOP NUMBER;
  V_CDPRODTIPOITVERSAOGOP VARCHAR(50);
  V_NMPRODTIPOITVERSAOGOP VARCHAR(50);
  V_VRPRODTIPOITVERSAOGOP NUMBER;
  
  V_DTINICIAL             DATE;
  V_DTFINAL               DATE;
  
  /* Next */
  V_NRORCAMENTOGOP          NUMBER;
  V_NRHISTORCAMENTOGOP      NUMBER;
  V_NRHISTITEMORCAGOP       NUMBER;
  V_NRHISTITORCAMESGOP      NUMBER;
  V_NRHISTITDETALORCAMESGOP NUMBER;
  V_NRHISTDIETSERVORCAGOP   NUMBER;
  V_IDVERSORCAGOP           VARCHAR2(04);
  V_VERSAO                  NUMBER;
  V_VERSAOITEM              NUMBER;
  V_IDTIPOORCAOP            VARCHAR2(30);
BEGIN
   V_NRORCAMENTOGOP := NVL(:NEW.NRORCAMENTOGOP, :OLD.NRORCAMENTOGOP);
   
   SELECT MAX(IDVERSORCAGOP)
     INTO V_IDVERSORCAGOP
     FROM ORCAMENTOGOP
    WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
      AND IDVERSORCAGOP = 'NEXT';
   
   IF V_IDVERSORCAGOP IS NULL THEN
      /* REMOVER FUTURAMENTE */
      SELECT NVL(MAX(IDUTILVERSAOORCA),'N')
        INTO V_IDUTILVERSAOORCA
        FROM PARAMORCAOPERAC;

      IF (V_IDUTILVERSAOORCA = 'S' AND :NEW.IDAPROVACAOGOP = 'APROVADO') THEN
         SELECT O.CDFILIAL, O.NRANOORCAGOP, O.IDPERIODOORCA, O.NRPERIODOORCA
           INTO V_CDFILIAL, V_NRANOORCAGOP, V_IDPERIODOORCA, V_NRPERIODOORCA
           FROM ORCAMENTOGOP O
          WHERE O.NRORCAMENTOGOP = :NEW.NRORCAMENTOGOP;

         SELECT MAX(CDVERSAOGOP)
           INTO V_CDVERSAOGOP
           FROM VERSAOGOP
          WHERE NRAPROVAORCAGOP = :NEW.NRAPROVAORCAGOP;

         IF V_CDVERSAOGOP IS NULL THEN
            V_CDVERSAOGOP := 1;
         ELSE
            V_CDVERSAOGOP := (V_CDVERSAOGOP + 1);
         END IF;
         NOVO_CODIGO_SC('VERSAOGOP', V_NRVERSAOGOP);

         INSERT INTO VERSAOGOP
             (NRVERSAOGOP, NRAPROVAORCAGOP, CDVERSAOGOP, CDFILIAL, NRANOVERSAOGOP, IDPERIODOORCA, NRPERIODOORCA)
         VALUES
             (V_NRVERSAOGOP, :NEW.NRAPROVAORCAGOP, V_CDVERSAOGOP, V_CDFILIAL, V_NRANOORCAGOP, V_IDPERIODOORCA, V_NRPERIODOORCA);

         FOR C_ITVERSAOGOP IN (SELECT IO.IDTIPOORCAOP, SUM(NVL(I.VRORCAMENTO, 0)) VRORCAMENTO
                                 FROM ITORCAMESGOP I INNER JOIN ITEMORCAGOP IO ON I.NRITEMORCAOP = IO.NRITEMORCAOP
                                WHERE IO.NRORCAMENTOGOP = :NEW.NRORCAMENTOGOP
                                GROUP BY IO.IDTIPOORCAOP) LOOP
            NOVO_CODIGO_SC('ITVERSAOGOP', V_NRITVERSAOGOP);

            INSERT INTO ITVERSAOGOP
                (NRITVERSAOGOP, NRVERSAOGOP, CDVERSAOGOP, CDFILIAL, IDITEMVERSAOGOP, VRITEMVERSAO)
            VALUES
                (V_NRITVERSAOGOP, V_NRVERSAOGOP, V_CDVERSAOGOP, V_CDFILIAL, C_ITVERSAOGOP.IDTIPOORCAOP, C_ITVERSAOGOP.VRORCAMENTO);
         END LOOP;

         FOR C_TIPOITVERSAOGOP IN (SELECT R.IDTIPOORCAOP, SUM(R.VRORCAMENTO) VRORCAMENTO, R.CDPRODUTO, R.NMPRODUTO, ITV.NRITVERSAOGOP
                                     FROM (SELECT IO.IDTIPOORCAOP,
                                                  CASE IO.IDTIPOORCAOP WHEN 'MAOOBRA' THEN CDORCAMENTOMO
                                                                       WHEN 'DESPMATRIZ' THEN CDDESPPREV
                                                                       WHEN 'DESPDIVERSA' THEN CDDESPPREV
                                                                       WHEN 'DESPENTRADA' THEN CDDESPPREV
                                                                       WHEN 'INVESTIMENTO' THEN CDINVEST
                                                                       WHEN 'DEPRECIACAO' THEN CDDESPPREV
                                                                       WHEN 'IMPOSTOARECUPERAR' THEN CDIMPOSTO
                                                                       WHEN 'IMPOSTOSOBLUCRO' THEN CDIMPOSTO
                                                                       WHEN 'IMPOSTO' THEN CDIMPOSTO
                                                                       ELSE NULL
                                                  END CDPRODUTO,
                                                  NVL(CASE IO.IDTIPOORCAOP WHEN 'MAOOBRA' THEN (SELECT MAX(DSORCAMENTOMO)
                                                                                                  FROM ORCAMENTOMO O
                                                                                                 WHERE O.CDORCAMENTOMO = IO.CDORCAMENTOMO
                                                                                                   AND (O.CDFILIAL = OG.CDFILIAL OR O.CDFILIAL = 'T'))
                                                                           WHEN 'DESPMATRIZ' THEN (SELECT MAX(NMDESPPREV)
                                                                                                     FROM DESPPREV D 
                                                                                                    WHERE D.CDDESPPREV = IO.CDDESPPREV
                                                                                                      AND D.IDTIPODESPESA = 'MATRIZ')
                                                                           WHEN 'DESPDIVERSA' THEN (SELECT MAX(NMDESPPREV)
                                                                                                      FROM DESPPREV D 
                                                                                                     WHERE D.CDDESPPREV = IO.CDDESPPREV
                                                                                                       AND D.IDTIPODESPESA = 'DIVERSA')
                                                                           WHEN 'DESPENTRADA' THEN (SELECT NMTIPOOPER
                                                                                                      FROM TIPOOPERAC T 
                                                                                                     WHERE T.CDTIPOOPER = IO.CDTIPOOPER 
                                                                                                       AND T.IDENTRSAIDOP = IO.IDENTRSAIDOP
                                                                                                       AND IO.CDDESPPREV IS NULL
                                                                                                     UNION 
                                                                                                    SELECT NMDESPPREV
                                                                                                      FROM DESPPREV D 
                                                                                                     WHERE D.CDDESPPREV = IO.CDDESPPREV
                                                                                                       AND D.IDTIPODESPESA = 'ENTRADA'
                                                                                                       AND IO.CDTIPOOPER IS NULL)
                                                                           WHEN 'INVESTIMENTO' THEN (SELECT NMINVEST 
                                                                                                       FROM INVESTIMENTOS IV 
                                                                                                      WHERE IV.CDINVEST = IO.CDINVEST)
                                                                           WHEN 'IMPOSTOARECUPERAR' THEN (SELECT NMIMPOSTO 
                                                                                                            FROM IMPOSTO IP 
                                                                                                           WHERE IP.CDIMPOSTO = IO.CDIMPOSTO)
                                                                           WHEN 'IMPOSTOSOBLUCRO' THEN (SELECT NMIMPOSTO 
                                                                                                          FROM IMPOSTO IP 
                                                                                                         WHERE IP.CDIMPOSTO = IO.CDIMPOSTO)
                                                                           WHEN 'IMPOSTO' THEN (SELECT NMIMPOSTO 
                                                                                                  FROM IMPOSTO IP 
                                                                                                 WHERE IP.CDIMPOSTO = IO.CDIMPOSTO)
                                                                           ELSE ' '
                                                  END,' ') NMPRODUTO, SUM(NVL(I.VRORCAMENTO, 0)) VRORCAMENTO, I.NRITEMORCAOP
                                             FROM ORCAMENTOGOP OG INNER JOIN ITEMORCAGOP IO ON OG.NRORCAMENTOGOP = IO.NRORCAMENTOGOP
                                                                  INNER JOIN ITORCAMESGOP I ON I.NRITEMORCAOP = IO.NRITEMORCAOP
                                            WHERE IO.NRORCAMENTOGOP = :NEW.NRORCAMENTOGOP
                                            GROUP BY IO.IDTIPOORCAOP,I.NRITEMORCAOP, IO.CDDESPPREV, IO.CDINVEST, IO.CDIMPOSTO, 
                                                     IO.CDORCAMENTOMO, IO.IDENTRSAIDOP, IO.CDTIPOOPER, OG.CDFILIAL
                                            ORDER BY IO.IDTIPOORCAOP, I.NRITEMORCAOP) R, ITVERSAOGOP ITV
                                    WHERE R.CDPRODUTO IS NOT NULL
                                      AND ITV.IDITEMVERSAOGOP = R.IDTIPOORCAOP
                                      AND ITV.CDVERSAOGOP = V_CDVERSAOGOP
                                    GROUP BY R.IDTIPOORCAOP, R.CDPRODUTO, R.NMPRODUTO, ITV.NRITVERSAOGOP) LOOP

            NOVO_CODIGO_SC('TIPOITVERSAOGOP', V_NRTIPOITVERSAOGOP);

            INSERT INTO TIPOITVERSAOGOP
                (NRTIPOITVERSAOGOP, NRITVERSAOGOP, IDITEMVERSAOGOP, CDTIPOITVERSAOGOP, NMTIPOITVERSAOGOP, VRTIPOITVERSAOGOP)
            VALUES
                (V_NRTIPOITVERSAOGOP, C_TIPOITVERSAOGOP.NRITVERSAOGOP, C_TIPOITVERSAOGOP.IDTIPOORCAOP, V_CDVERSAOGOP, C_TIPOITVERSAOGOP.NMPRODUTO, NVL(C_TIPOITVERSAOGOP.VRORCAMENTO, 0));
         END LOOP;

         FOR C_PRODTIPOITVERSAOGOP IN (SELECT T.NRTIPOITVERSAOGOP, RESULTA.PRODUTO CDPRODUTO, RESULTA.NMPRODUTO,  NVL(SUM(RESULTA.VRORCAMENTO), 0) VRORCAMENTO
                                         FROM (SELECT DETAL.NRITORCAMESGOP, DETAL.NRITDETALORCAMESGOP, DETAL.VRORCAMENTO, DETAL.NRITEMORCAOP, DETAL.IDTIPOORCAOP,
                                                      CASE IDTIPOORCAOP WHEN 'MAOOBRA' THEN DETAL.CDORCAMENTOMO
                                                                        WHEN 'DESPMATRIZ' THEN DETAL.CDDESPPREV
                                                                        WHEN 'DESPDIVERSA' THEN DETAL.CDDESPPREV
                                                                        WHEN 'DESPENTRADA' THEN DETAL.CDDESPPREV
                                                                        WHEN 'INVESTIMENTO' THEN DETAL.CDINVEST
                                                                        WHEN 'DEPRECIACAO' THEN DETAL.CDDESPPREV
                                                                        WHEN 'IMPOSTOARECUPERAR' THEN DETAL.CDIMPOSTO
                                                                        WHEN 'IMPOSTOSOBLUCRO' THEN DETAL.CDIMPOSTO
                                                                        WHEN 'IMPOSTO' THEN DETAL.CDIMPOSTO 
                                                                        ELSE NULL
                                                      END CDPRODUTO, NMPRODUTO, CDPRODUTO AS PRODUTO
                                                 FROM (SELECT I.IDTIPOORCAOP, IM.NRITEMORCAOP, IM.NRITORCAMESGOP, IP.NRITDETALORCAMESGOP, IP.VRORCAMENTO,
                                                              I.CDDESPPREV, I.CDINVEST, I.CDIMPOSTO, I.CDORCAMENTOMO, I.IDENTRSAIDOP, I.CDTIPOOPER,
                                                              IP.CDPRODUTO, P.NMPRODUTO
                                                         FROM ITEMORCAGOP I INNER JOIN ITORCAMESGOP IM ON I.NRITEMORCAOP = IM.NRITEMORCAOP
                                                                            LEFT  JOIN ITDETALORCAMESGOP IP ON IM.NRITORCAMESGOP = IP.NRITORCAMESGOP
                                                                            LEFT  JOIN PRODUTO P ON IP.CDPRODUTO = P.CDPRODUTO
                                                        WHERE I.NRORCAMENTOGOP = :NEW.NRORCAMENTOGOP) DETAL) RESULTA INNER JOIN TIPOITVERSAOGOP T 
                                                                                                                             ON RESULTA.CDPRODUTO = T.CDTIPOITVERSAOGOP
                                                                                                                            AND RESULTA.IDTIPOORCAOP = T.IDITEMVERSAOGOP
                                                                                                                     INNER JOIN ITVERSAOGOP I 
                                                                                                                             ON T.NRITVERSAOGOP = I.NRITVERSAOGOP
                                        WHERE I.CDVERSAOGOP = V_CDVERSAOGOP
                                        GROUP BY T.NRTIPOITVERSAOGOP, RESULTA.PRODUTO, RESULTA.NMPRODUTO) LOOP
            NOVO_CODIGO_SC('PRODTIPOITVERSAOGOP', V_NRPRODTIPOITVERSAOGOP);
   
            INSERT INTO PRODTIPOITVERSAOPGOP
               (NRPRODTIPOITVERSAOPGOP, NRTIPOITVERSAOGOP, CDPRODTIPOITVERSAOPGOP, NMPRODTIPOITVERSAOPGOP, VRPRODTIPOITVERSAOPGOP)
            VALUES
               (V_NRPRODTIPOITVERSAOGOP, C_PRODTIPOITVERSAOGOP.NRTIPOITVERSAOGOP, C_PRODTIPOITVERSAOGOP.CDPRODUTO,
                C_PRODTIPOITVERSAOGOP.NMPRODUTO, C_PRODTIPOITVERSAOGOP.VRORCAMENTO);
         END LOOP;
      END IF;
   
      IF :NEW.IDAPROVACAOGOP = 'APROVADO' THEN
         SELECT O.CDFILIAL, O.NRANOORCAGOP, O.IDPERIODOORCA, O.NRPERIODOORCA
           INTO V_CDFILIAL, V_NRANOORCAGOP, V_IDPERIODOORCA, V_NRPERIODOORCA
           FROM ORCAMENTOGOP O
          WHERE O.NRORCAMENTOGOP = :NEW.NRORCAMENTOGOP;
    
         SELECT TO_DATE(TO_CHAR(MAX(P.DTINIPERIORCA),'DD/MM')||'/'||V_NRANOORCAGOP,'DD/MM/YYYY'),
                TO_DATE(TO_CHAR(MAX(P.DTFIMPERIORCA),'DD/MM')||'/'||V_NRANOORCAGOP,'DD/MM/YYYY')
           INTO V_DTINICIAL, V_DTFINAL
           FROM PERIODOORCAGOP P
          WHERE P.IDPERIODOORCA = V_IDPERIODOORCA
            AND P.NRPERIODO = V_NRPERIODOORCA
            AND P.CDFILIAL = NVL((SELECT MAX(X.CDFILIAL) FROM PERIODOORCAGOP X WHERE X.CDFILIAL = V_CDFILIAL),'P');
      
         APURAC_DM_RESULT_OPERACIONAL(V_CDFILIAL, V_DTINICIAL, V_DTFINAL, NULL, 'S');
      END IF;
   ELSE
      IF (:NEW.IDAPROVACAOGOP = 'APROVADO') THEN
         SELECT MAX(NRULTVERSAO), MAX(NRHISTORCAMENTOGOP)
           INTO V_VERSAO, V_NRHISTORCAMENTOGOP
           FROM HISTORCAMENTOGOP
          WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP;
          
         IF V_VERSAO IS NULL THEN
            V_VERSAO := 1;
            V_NRHISTORCAMENTOGOP := SEQ_HISTORCAMENTOGOP.NEXTVAL;
            
            INSERT INTO HISTORCAMENTOGOP (NRHISTORCAMENTOGOP, NRORCAMENTOGOP, NRULTVERSAO,
                        NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL)
                 SELECT V_NRHISTORCAMENTOGOP, NRORCAMENTOGOP, V_VERSAO,
                        NRANOORCAGOP, IDPERIODOORCA, NRPERIODOORCA, CDFILIAL
                   FROM ORCAMENTOGOP
                  WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP;
         END IF;
         
         V_IDTIPOORCAOP := NVL(:NEW.IDTIPOORCAOP, :OLD.IDTIPOORCAOP);
         
         SELECT NVL(MAX(NRVERSAO),0) + 1
           INTO V_VERSAOITEM
           FROM HISTITEMORCAGOP
          WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
            AND IDTIPOORCAOP = V_IDTIPOORCAOP;
            
         IF V_VERSAO < V_VERSAOITEM THEN
            UPDATE HISTORCAMENTOGOP
               SET NRULTVERSAO = V_VERSAOITEM
             WHERE NRHISTORCAMENTOGOP = V_NRHISTORCAMENTOGOP;
         END IF;
         
         FOR C_ITEMORCAGOP IN (SELECT NRITEMORCAOP, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO,
                                      CDDESPPREV, CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO,
                                      DTMESBASEITEM, IDTIPOORCAOP, CDINVEST, VRTOTALMES
                                 FROM ITEMORCAGOP
                                WHERE NRORCAMENTOGOP = V_NRORCAMENTOGOP
                                  AND IDTIPOORCAOP = V_IDTIPOORCAOP) LOOP
            
            V_NRHISTITEMORCAGOP := SEQ_HISTITEMORCAGOP.NEXTVAL;
                                  
            INSERT INTO HISTITEMORCAGOP (NRHISTITEMORCAGOP, NRHISTORCAMENTOGOP, NRORCAMENTOGOP, NRITEMORCAOP,
                                         NRVERSAO, IDENTRSAIDOP, CDTIPOOPER, CDORCAMENTOMO, CDDESPPREV,
                                         CDFILIAL, CDFILIALMO, CDSERVFILI, CDIMPOSTO, DTMESBASEITEM,
                                         IDTIPOORCAOP, CDINVEST, VRTOTALMES)
                 VALUES (V_NRHISTITEMORCAGOP, V_NRHISTORCAMENTOGOP, V_NRORCAMENTOGOP, C_ITEMORCAGOP.NRITEMORCAOP,
                         V_VERSAOITEM, C_ITEMORCAGOP.IDENTRSAIDOP, C_ITEMORCAGOP.CDTIPOOPER, C_ITEMORCAGOP.CDORCAMENTOMO,
                         C_ITEMORCAGOP.CDDESPPREV, C_ITEMORCAGOP.CDFILIAL, C_ITEMORCAGOP.CDFILIALMO,
                         C_ITEMORCAGOP.CDSERVFILI, C_ITEMORCAGOP.CDIMPOSTO, C_ITEMORCAGOP.DTMESBASEITEM,
                         C_ITEMORCAGOP.IDTIPOORCAOP, C_ITEMORCAGOP.CDINVEST, C_ITEMORCAGOP.VRTOTALMES);
                         
            FOR C_ITORCAMESGOP IN (SELECT NRITEMORCAOP, NRITORCAMESGOP, DTORCAMENTO, IDALTMANUAL,
                                          VRORCAMENTO, VRREAJUSTE, VRPERCENTUAL, QTORCAMENTO, VRTOTALMES
                                     FROM ITORCAMESGOP
                                    WHERE NRITEMORCAOP = C_ITEMORCAGOP.NRITEMORCAOP) LOOP
                
               V_NRHISTITORCAMESGOP := SEQ_HISTITORCAMESGOP.NEXTVAL;
                                      
               INSERT INTO HISTITORCAMESGOP (NRHISTITORCAMESGOP, NRHISTITEMORCAGOP, NRITEMORCAOP,
                                             NRITORCAMESGOP, DTORCAMENTO, IDALTMANUAL,
                                             VRORCAMENTO, VRREAJUSTE, VRPERCENTUAL,
                                             QTORCAMENTO, VRTOTALMES)
                    VALUES (V_NRHISTITORCAMESGOP, V_NRHISTITEMORCAGOP, C_ITORCAMESGOP.NRITEMORCAOP,
                            C_ITORCAMESGOP.NRITORCAMESGOP, C_ITORCAMESGOP.DTORCAMENTO, C_ITORCAMESGOP.IDALTMANUAL,
                            C_ITORCAMESGOP.VRORCAMENTO, C_ITORCAMESGOP.VRREAJUSTE, C_ITORCAMESGOP.VRPERCENTUAL,
                            C_ITORCAMESGOP.QTORCAMENTO, C_ITORCAMESGOP.VRTOTALMES);
               
               FOR C_ITDETALORCAMESGOP IN (SELECT NRITORCAMESGOP, NRITDETALORCAMESGOP, VRORCAMENTO, VRPERCENTUAL,
                                                  QTORCAMENTO, CDPRODUTO, CDTIPOOPER, CDFILIALMO, IDENTRSAIDOP,
                                                  CDORCAMENTOMO, CDDESPADMFAT, CDSERVFILI, VRPRECOBASE, NRDIASUTEIS,
                                                  CDCONTCTBL, CDINVEST, CDTPCTPAGAR, NMDESCRICAO, CDSUBGRPROD,
                                                  CDDESPPREV, CDIMPOSTO, NMDESPESA, CDGRUPPROD,
                                                  CDFATUDIVER, CDFILICONTRIB, CDFILIAL, QTCOMESTISEG, QTCOMESTITER,
                                                  QTCOMESTIQUA, QTCOMESTIQUI, QTCOMESTISEX, QTCOMESTISAB,
                                                  QTCOMESTIDOM, VRCUSTPADR
                                             FROM ITDETALORCAMESGOP
                                            WHERE NRITORCAMESGOP = C_ITORCAMESGOP.NRITORCAMESGOP) LOOP

                  V_NRHISTITDETALORCAMESGOP := SEQ_HISTITDETALORCAMESGOP.NEXTVAL;
                                          
                  INSERT INTO HISTITDETALORCAMESGOP (NRHISTITDETALORCAMESGOP, NRHISTITORCAMESGOP,
                               NRITORCAMESGOP, NRITDETALORCAMESGOP,
                               VRORCAMENTO, VRPERCENTUAL,
                               QTORCAMENTO, CDPRODUTO,
                               CDTIPOOPER, CDFILIALMO,
                               IDENTRSAIDOP, CDORCAMENTOMO,
                               CDDESPADMFAT, CDSERVFILI,
                               VRPRECOBASE, NRDIASUTEIS,
                               CDCONTCTBL, CDINVEST,
                               CDTPCTPAGAR, NMDESCRICAO,
                               CDSUBGRPROD, CDDESPPREV,
                               CDIMPOSTO, NMDESPESA,
                               CDGRUPPROD,
                               CDFATUDIVER, CDFILICONTRIB,
                               CDFILIAL, QTCOMESTISEG,
                               QTCOMESTITER, QTCOMESTIQUA,
                               QTCOMESTIQUI, QTCOMESTISEX,
                               QTCOMESTISAB, QTCOMESTIDOM,
                               VRCUSTPADR)
                       VALUES (V_NRHISTITDETALORCAMESGOP, V_NRHISTITORCAMESGOP,
                               C_ITDETALORCAMESGOP.NRITORCAMESGOP, C_ITDETALORCAMESGOP.NRITDETALORCAMESGOP,
                               C_ITDETALORCAMESGOP.VRORCAMENTO, C_ITDETALORCAMESGOP.VRPERCENTUAL,
                               C_ITDETALORCAMESGOP.QTORCAMENTO, C_ITDETALORCAMESGOP.CDPRODUTO,
                               C_ITDETALORCAMESGOP.CDTIPOOPER, C_ITDETALORCAMESGOP.CDFILIALMO,
                               C_ITDETALORCAMESGOP.IDENTRSAIDOP, C_ITDETALORCAMESGOP.CDORCAMENTOMO,
                               C_ITDETALORCAMESGOP.CDDESPADMFAT, C_ITDETALORCAMESGOP.CDSERVFILI,
                               C_ITDETALORCAMESGOP.VRPRECOBASE, C_ITDETALORCAMESGOP.NRDIASUTEIS,
                               C_ITDETALORCAMESGOP.CDCONTCTBL, C_ITDETALORCAMESGOP.CDINVEST,
                               C_ITDETALORCAMESGOP.CDTPCTPAGAR, C_ITDETALORCAMESGOP.NMDESCRICAO,
                               C_ITDETALORCAMESGOP.CDSUBGRPROD, C_ITDETALORCAMESGOP.CDDESPPREV,
                               C_ITDETALORCAMESGOP.CDIMPOSTO, C_ITDETALORCAMESGOP.NMDESPESA,
                               C_ITDETALORCAMESGOP.CDGRUPPROD,
                               C_ITDETALORCAMESGOP.CDFATUDIVER, C_ITDETALORCAMESGOP.CDFILICONTRIB,
                               C_ITDETALORCAMESGOP.CDFILIAL, C_ITDETALORCAMESGOP.QTCOMESTISEG,
                               C_ITDETALORCAMESGOP.QTCOMESTITER, C_ITDETALORCAMESGOP.QTCOMESTIQUA,
                               C_ITDETALORCAMESGOP.QTCOMESTIQUI, C_ITDETALORCAMESGOP.QTCOMESTISEX,
                               C_ITDETALORCAMESGOP.QTCOMESTISAB, C_ITDETALORCAMESGOP.QTCOMESTIDOM,
                               C_ITDETALORCAMESGOP.VRCUSTPADR);
                
                  IF C_ITDETALORCAMESGOP.CDSERVFILI IS NOT NULL THEN
                     FOR C_HISTDIET IN (SELECT NRDIETSERVORCAGOP, NRITDETALORCAMESGOP, CDDIETPAD, CDFILIAL,
                                               CDSERVFILI, QTCOMDIETSEG, QTCOMDIETTER, QTCOMDIETQUA,
                                               QTCOMDIETQUI, QTCOMDIETSEX, QTCOMDIETSAB, QTCOMDIETDOM,
                                               VRCUSTPADR
                                          FROM DIETSERVORCAGOP
                                         WHERE NRITDETALORCAMESGOP = C_ITDETALORCAMESGOP.NRITDETALORCAMESGOP) LOOP

                        V_NRHISTDIETSERVORCAGOP := SEQ_HISTDIETSERVORCAGOP.NEXTVAL;

                        INSERT INTO HISTDIETSERVORCAGOP (NRHISTDIETSERVORCAGOP, NRHISTITDETALORCAMESGOP,
                                     NRDIETSERVORCAGOP, NRITDETALORCAMESGOP, CDDIETPAD, CDFILIAL,
                                     CDSERVFILI, QTCOMDIETSEG, QTCOMDIETTER, QTCOMDIETQUA,
                                     QTCOMDIETQUI, QTCOMDIETSEX, QTCOMDIETSAB, QTCOMDIETDOM,
                                     VRCUSTPADR)
                             VALUES (V_NRHISTDIETSERVORCAGOP, V_NRHISTITDETALORCAMESGOP,
                                     C_HISTDIET.NRDIETSERVORCAGOP, C_HISTDIET.NRITDETALORCAMESGOP, C_HISTDIET.CDDIETPAD,
                                     C_HISTDIET.CDFILIAL, C_HISTDIET.CDSERVFILI, C_HISTDIET.QTCOMDIETSEG,
                                     C_HISTDIET.QTCOMDIETTER, C_HISTDIET.QTCOMDIETQUA, C_HISTDIET.QTCOMDIETQUI,
                                     C_HISTDIET.QTCOMDIETSEX, C_HISTDIET.QTCOMDIETSAB, C_HISTDIET.QTCOMDIETDOM,
                                     C_HISTDIET.VRCUSTPADR);
                     END LOOP;
                  END IF;
               END LOOP;
            END LOOP;
         END LOOP;
      END IF;
   END IF;
END;
/

