CREATE OR REPLACE PROCEDURE ALTERA_CLASSE_PLANOVOOM(
  P_CDFILIAL     IN FILIAL.CDFILIAL%TYPE,
  P_CDCIA        IN COMPANHIA.CDCIA%TYPE,
  P_NRVOO        IN VOO.NRVOO%TYPE,
  P_CDTRECHO     IN VOOTRECHO.CDTRECHO%TYPE,
  P_DTHRPLAVOOPV IN PLANOVOOM.DTHRPLAVOOPV%TYPE,
  P_CDCLASSE     IN CLASSE.CDCLASSE%TYPE,
  P_CDOPERADOR   IN VARCHAR2,
  P_NRORG IN NUMBER DEFAULT 1
) AS
  -- ============================================================= --
  -- VARIAVEL UTILIZADA PARA TESTAR SE EXISTE DADOS NAS TABELAS    --
  -- ============================================================= --

  V_NRSERVICOCIAFILI    NUMBER;
  V_QTSESBYPRSP         SERVPLANOM.QTSESBYPRSP%TYPE;
  V_QTSESOLICSP         SERVPLANOM.QTSESOLICSP%TYPE;
  V_NRPROPAXSP          SERVPLANOM.NRPROPAXSP%TYPE;
  V_VRPELOADSP          SERVPLANOM.VRLOADSESP%TYPE;
  V_NRITEMPLANSP        SERVPLANOM.NRITEMPLANSP%TYPE;
  V_VRPREDOLAR          PRECOSECIA.VRPRESERVD%TYPE;
  V_VRPRECORRENTE       PRECOSECIA.VRPRESERV%TYPE;
  V_NRITEMPLANO         SERVPLANOM.NRITEMPLANSP%TYPE;
  V_VRPESOSE            FLOAT;
  V_CDAGRDECO           CONFIGVOO.CDAGRDEL%TYPE;
  V_CDAGRDEDE           CLASSE.CDCLASSE%TYPE;
  V_CDEQUIP             PLANOVOOM.CDEQUIP%TYPE;
  V_SGPREFIXO           PLANOVOOM.SGPREFIXO%TYPE;
  V_IDTIPMANUAL         SERVICOCIA.IDTIPMANUAL%TYPE;
  V_IDTPPRECO           SERVICOCIA.IDTPPRECO%TYPE;
  V_VRPRECORR_SBY       SERVPLANOM.VRPRUSBYSP%TYPE;
  V_VRPREDOLAR_SBY      SERVPLANOM.VRPRUSBYSP%TYPE;
  V_VRPRUSBYSP          SERVPLANOM.VRPRUSBYSP%TYPE;
  V_QTSESOLICSP_INC     SERVPLANOM.QTSESOLICSP%TYPE;
  V_QTSESBYPRSP_INC     SERVPLANOM.QTSESBYPRSP%TYPE;
  V_QTSERFATSP      SERVPLANOM.QTSERFATSP%TYPE;
  V_QTSERFATSP_INC      SERVPLANOM.QTSERFATSP%TYPE;
  V_QTSESOLICSP_AUX     SERVPLANOM.QTSESOLICSP%TYPE;
  V_QTSERVFIXSP     SERVPLANOM.QTSERVFIXSP%TYPE;
  V_QTSERVFIXSP_AUX     SERVPLANOM.QTSERVFIXSP%TYPE;
  V_QTSESBYPRSP_AUX     SERVPLANOM.QTSESBYPRSP%TYPE;
  V_QTSESOLICSP_ANT     SERVPLANOM.QTSESOLICSP%TYPE;
  V_QTSERVFIXSP_ANT     SERVPLANOM.QTSERVFIXSP%TYPE;
  V_QTSESBYPRSP_ANT     SERVPLANOM.QTSESBYPRSP%TYPE;
  V_QTSESOLICSP_FINAL   SERVPLANOM.QTSESOLICSP%TYPE;
  V_QTSESBYPRSP_FINAL   SERVPLANOM.QTSESBYPRSP%TYPE;
  V_QTESPECIAL          SERVPLANOM.QTSERVFIXSP%TYPE;
  V_CDCLASSE            CLASSE.CDCLASSE%TYPE;
  V_DTHRABASTPV         PLANOVOOM.DTHRABASTPV%TYPE;
  V_CDCICLO             CICLOCIA.CDCICLO%TYPE;
  V_IDFATSESP           VARCHAR2(1);
  V_IDTPPRECOSP         VARCHAR2(1);
  V_EXISTE_PENDENTE     NUMBER;
  V_INCLUI_SERVICO      BOOLEAN;
  V_QTREAL         FLOAT;
  V_IDCALCPROPDLCOM PARAMCOMIS.IDCALCPROPDLCOM%TYPE;
  V_QTEMITIDA           NUMBER;
  V_QTTOTPAX FLOAT;
  V_QTPAXSBY_CLA FLOAT;
  V_SERVICO VARCHAR2(4000);
  V_IDCICLO VARCHAR2(10);
  V_SGCLASSE CLASSE.SGCLASSE%TYPE;
  V_DSSERVICO SERVICOCIAFILI.DSSERVICOCIAFILI%TYPE;
  V_MSGERRO VARCHAR2(2000);

  -- ======================================================== --
  -- VARIAVEL UTILIZADA NA GERA_CLASSE_VOO                    --
  -- ======================================================== --
  V_IDPROPSERV          VARCHAR2(1);
  V_PENDENTE            NUMBER;
  V_SITUACAO_VOO        PLANOVOOM.IDSITABASPV%TYPE;
  V_SITUACAO            PLANOVOOM.IDSITABASPV%TYPE;
  V_QTSTBYANTE          NUMBER;
  V_IDSITSERSP          VARCHAR2(1);
  V_NRDELIVERY          VARCHAR2(10);

  V_QTTOTPAX_PAXSBY FLOAT;
  V_QTSERVFATU FLOAT;
  -- ================================================================ --
  --                      DECLARACAO DE CURSOR 1                      --
  -- ================================================================ --
  --   SERVICOS DA SERVPLANOM - ALTERACAO NA QTDE PAX E QTDE.SERVICOS  --
  -- ================================================================ --
  CURSOR C_SERVPLANOM IS
  SELECT SP.NRSERVICOCIAFILI, SP.CDCLASSE, SP.CDCICLO, MAX(SP.IDTIPSCBOSP),
         MAX(SP.NRPROPAXSP)   , MAX(SP.VRLOADSESP) ,
         MAX(SP.IDTIPMANUALSP), MAX(SP.IDTPPRECOSP),
         MAX(SP.CDCLASSE)     , MAX(PV.CDEQUIP)    ,
         MAX(PV.SGPREFIXO)    , SUM(SP.QTSESOLICSP),
         SUM(SP.QTSESBYPRSP)  , SUM(SP.QTSERVFIXSP),
         MAX(SP.VRPESOSESP)   , MAX(PV.DTHRABASTPV),         
         MAX(SP.IDFATSESP)       , MAX(SP.IDTPPRECOSP),
         MAX(SF.DSSERVICOCIAFILI), MAX(CL.SGCLASSE),
         MAX(CI.IDCICLO)         
  FROM   SERVPLANOM SP, PLANOVOOM PV, SERVICOCIAFILI SF, CLASSE CL, CICLO CI
  WHERE (PV.CDFILIAL     = P_CDFILIAL          ) AND
        (PV.CDCIA        = P_CDCIA             ) AND
        (PV.NRVOO        = P_NRVOO             ) AND
        (PV.CDTRECHO     = P_CDTRECHO          ) AND
        (PV.DTHRPLAVOOPV = P_DTHRPLAVOOPV      ) AND
        (SP.CDFILIAL     = PV.CDFILIAL         ) AND
        (SP.CDCIA        = PV.CDCIA            ) AND
        (SP.NRVOO        = PV.NRVOO            ) AND
        (SP.CDTRECHO     = PV.CDTRECHO         ) AND
        (SP.DTHRPLAVOOPV = PV.DTHRPLAVOOPV     ) AND
        (SP.CDCLASSE     = P_CDCLASSE          ) AND
        (SP.IDSITSERSP  <> '3'                 ) AND
        (SF.CDFILIAL = SP.CDFILIAL) AND
        (SF.CDCIA = SP.CDCIA) AND
        (SF.NRSERVICOCIAFILI = SP.NRSERVICOCIAFILI) AND
        (CL.CDCLASSE = SP.CDCLASSE) AND
        (CI.CDCICLO = SP.CDCICLO) AND
        (SP.IDTIPSCBOSP IN ('1', '2', '3', '4'))  --exclui handling e miscelanea
  GROUP BY SP.NRSERVICOCIAFILI, SP.CDCLASSE, SP.CDCICLO;

BEGIN
   V_QTEMITIDA := 0;

   SELECT MAX(IDCALCPROPDLCOM)
     INTO V_IDCALCPROPDLCOM
     FROM PARAMCOMIS
    WHERE (CDFILIAL = P_CDFILIAL);

   /* Seleciona a situacao do voo */
   SELECT NVL(MAX(IDSITABASPV),0)
   INTO V_SITUACAO
   FROM PLANOVOOM
   WHERE (CDFILIAL     = P_CDFILIAL    ) AND
         (CDCIA        = P_CDCIA       ) AND
         (NRVOO        = P_NRVOO       ) AND
         (CDTRECHO     = P_CDTRECHO    ) AND
         (DTHRPLAVOOPV = P_DTHRPLAVOOPV);

   SELECT MAX(QTTOTPAXAT), MAX(QTPAXSBY)
     INTO   V_QTTOTPAX, V_QTPAXSBY_CLA
     FROM   PREVISAOPAXM
     WHERE  (CDFILIAL     = P_CDFILIAL) AND
            (CDCIA        = P_CDCIA)    AND
            (NRVOO        = P_NRVOO)    AND
            (CDTRECHO     = P_CDTRECHO) AND
            (CDCLASSE     = P_CDCLASSE) AND
            (DTHRPLAVOOPV = P_DTHRPLAVOOPV); 

   IF V_SITUACAO = '3' THEN
    RAISE_APPLICATION_ERROR( -20502, 'APPERRMSGSTR' || 'APP-00015' || 'APPERRMSGEND', FALSE);

    /*Manutecao nao permitida. O voo ja esta cancelado.', false);*/
  END IF;

    OPEN C_SERVPLANOM;
    LOOP
        FETCH C_SERVPLANOM INTO
         V_NRSERVICOCIAFILI, V_CDCLASSE, V_CDCICLO, V_IDPROPSERV,
         V_NRPROPAXSP     , V_VRPELOADSP,
         V_IDTIPMANUAL    , V_IDTPPRECO ,
         V_CDAGRDEDE      , V_CDEQUIP   ,
         V_SGPREFIXO      , V_QTSESOLICSP_AUX,
         V_QTSESBYPRSP_AUX, V_QTSERVFIXSP_AUX,
         V_VRPESOSE       , V_DTHRABASTPV,
         V_IDFATSESP  , V_IDTPPRECOSP,
         V_DSSERVICO, V_SGCLASSE,
         V_IDCICLO;

        V_SERVICO := V_DSSERVICO||'$$'||V_SGCLASSE||'$$'||V_IDCICLO;

        EXIT WHEN C_SERVPLANOM%NOTFOUND;

        V_CDAGRDECO := F_RETORNA_AGRUPAMENTO(P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDCLASSE,
                                             V_CDEQUIP,V_SGPREFIXO);
       -- =================================================== --
       -- Calculo das quantidades                             --
       -- Salva valores da SERVPLANOM antes de chamar a calc.  --
       -- quantidades                                         --
       -- =================================================== --

       SELECT COUNT(*)
       INTO  V_EXISTE_PENDENTE
       FROM  SERVPLANOM
       WHERE (CDFILIAL     = P_CDFILIAL    ) AND
             (CDCIA        = P_CDCIA       ) AND
             (NRVOO        = P_NRVOO       ) AND
             (CDTRECHO     = P_CDTRECHO    ) AND
             (DTHRPLAVOOPV = P_DTHRPLAVOOPV) AND
             (NRSERVICOCIAFILI = V_NRSERVICOCIAFILI   ) AND
             (CDCLASSE = V_CDCLASSE) AND
             (CDCICLO = V_CDCICLO) AND
             (IDSITSERSP   = '1'           );

        IF V_EXISTE_PENDENTE > 0 THEN
          SELECT MAX(NRITEMPLANSP), MAX(QTSESOLICSP),
                 MAX(QTSESBYPRSP) , MAX(QTSERVFIXSP)
          INTO   V_NRITEMPLANSP   , V_QTSESOLICSP_ANT,
                 V_QTSESBYPRSP_ANT, V_QTSERVFIXSP_ANT
          FROM  SERVPLANOM
        WHERE (CDFILIAL     = P_CDFILIAL    ) AND
                (CDCIA        = P_CDCIA       ) AND
                (NRVOO        = P_NRVOO       ) AND
                (CDTRECHO     = P_CDTRECHO    ) AND
                (DTHRPLAVOOPV = P_DTHRPLAVOOPV) AND
                (NRSERVICOCIAFILI    = V_NRSERVICOCIAFILI   ) AND                  
                (CDCLASSE = V_CDCLASSE) AND
                (CDCICLO = V_CDCICLO) AND
                (IDSITSERSP   = '1'           )
          GROUP BY CDFILIAL, CDCIA,NRVOO,CDTRECHO,DTHRPLAVOOPV,
                   NRSERVICOCIAFILI, CDCLASSE, CDCICLO;
          V_INCLUI_SERVICO  := FALSE;
        ELSE
          V_INCLUI_SERVICO  := TRUE;
          V_QTSESOLICSP_ANT := 0;
          V_QTSESBYPRSP_ANT := 0;
          V_QTSERVFIXSP_ANT := 0;
        END IF;

        IF (V_QTPAXSBY_CLA <> 0 ) AND (V_QTPAXSBY_CLA IS NOT NULL) THEN
           V_QTSESBYPRSP := V_QTPAXSBY_CLA;
         ELSE
           V_QTSESBYPRSP := V_QTSESBYPRSP_AUX;
         END IF;
         IF (V_QTSESBYPRSP IS NULL) THEN
           V_QTSESBYPRSP := 0;
         END IF;


        CONEXAO_COM_SUBGRU_PLANOVOOM(P_CDFILIAL, P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV, 
                                     V_NRSERVICOCIAFILI, V_CDCLASSE, V_CDCICLO, V_DTHRABASTPV, 
                                     'S', NULL, P_CDOPERADOR, P_NRORG, V_MSGERRO);
        IF V_MSGERRO IS NOT NULL THEN
           FOR CUR_MSERROS IN
                     (
                      SELECT REGEXP_SUBSTR(V_MSGERRO,'[^'||'>><<'||']+', 1, LEVEL) AS MSG FROM DUAL
                        CONNECT BY REGEXP_SUBSTR(V_MSGERRO, '[^'||'>><<'||']+', 1, LEVEL) IS NOT NULL
                     ) LOOP
              RAISE_APPLICATION_ERROR(-20000, CUR_MSERROS.MSG);
           END LOOP;
        END IF;        
        
         -- =========================================================== --
         -- Calculo das quantidades                                     --
         -- =========================================================== --
         CALCULA_QUANTIDADESM(P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO,V_NRSERVICOCIAFILI,
                            V_CDCLASSE,V_CDCICLO,V_DTHRABASTPV, V_QTTOTPAX,
                            V_IDPROPSERV, V_QTSESOLICSP, V_QTSESBYPRSP,
                            V_NRPROPAXSP, V_VRPELOADSP, V_QTSERVFIXSP, V_QTSERFATSP);

         V_QTSESOLICSP_INC := V_QTSESOLICSP - V_QTSESOLICSP_AUX;

         SELECT IDCALCPROPDLCOM 
           INTO V_IDCALCPROPDLCOM 
           FROM PARAMCOMIS 
          WHERE (CDFILIAL = P_CDFILIAL);
         
         IF (V_IDPROPSERV = '2')  THEN 
            V_QTSESBYPRSP_INC := CEIL((NVL(V_QTPAXSBY_CLA, 0)+V_QTTOTPAX)/V_NRPROPAXSP) - V_QTSESOLICSP_AUX - V_QTSESBYPRSP_AUX - V_QTSESOLICSP_INC; 
         ELSE 
            V_QTSESBYPRSP_INC := V_QTSESBYPRSP - V_QTSESBYPRSP_AUX; 
         END IF;

         IF (V_IDTPPRECO = '2') and     /* PRECO TOTAL */
            ((V_QTSERVFIXSP_ANT+V_QTSESOLICSP_ANT+V_QTSESOLICSP_INC) > 0) THEN
            V_QTSERFATSP_INC := 1;
         ELSE
            V_QTSERFATSP_INC := V_QTSERVFIXSP_ANT+V_QTSESOLICSP_ANT+V_QTSESOLICSP_INC;
         END IF;

        V_QTSESBYPRSP_FINAL := V_QTSESBYPRSP_ANT+V_QTSESBYPRSP_INC;
        V_QTSESOLICSP_FINAL := V_QTSESOLICSP_ANT+V_QTSESOLICSP_INC;

        /*So calcula preco se a quantidade for maior que zero*/
        IF ((V_QTSESOLICSP_FINAL > 0) OR
              (V_QTSESBYPRSP_FINAL > 0)) THEN 
             V_QTTOTPAX_PAXSBY := CASE WHEN V_IDCALCPROPDLCOM = 'S' THEN V_QTTOTPAX+V_QTSESBYPRSP_INC 
                                       ELSE V_QTTOTPAX 
                                  END;
             V_QTSERVFATU := CASE WHEN V_IDCALCPROPDLCOM = 'S' THEN V_QTSESOLICSP_INC + V_QTSESOLICSP_AUX + V_QTSESBYPRSP_INC 
                                  ELSE (V_QTSERVFIXSP_ANT+V_QTSESOLICSP_ANT+V_QTSESOLICSP_INC) 
                             END;

             CALCULA_PRECOM('N',P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO,V_NRSERVICOCIAFILI,V_CDCLASSE, V_CDCICLO,
                               P_DTHRPLAVOOPV,V_DTHRABASTPV, V_QTTOTPAX_PAXSBY,V_QTSERVFATU,NULL,
                               V_VRPRECORRENTE,V_VRPREDOLAR);

             V_VRPRUSBYSP := CASE WHEN V_QTSESBYPRSP_INC > 0 THEN 
                                     CASE WHEN V_IDTIPMANUAL = '1' THEN V_VRPRECORRENTE
                                          ELSE V_VRPREDOLAR
                                     END 
                                  ELSE 0 
                             END; 
        END IF;
       -- ================================================ --
       --       ATUALIZACAO DA TABELA SERVPLANOM            --
       -- ================================================ --
        IF NOT V_INCLUI_SERVICO THEN     /* PARA SERVICOS PENDENTE ALTERA A QTDE */
          IF ((V_QTSESOLICSP_FINAL > 0) OR
              (V_QTSESBYPRSP_FINAL > 0)) THEN


            UPDATE SERVPLANOM
            SET    QTSESOLICSP   = V_QTSESOLICSP_FINAL,
                    QTSESBYPRSP   = V_QTSESBYPRSP_FINAL,
                    QTANTAPRODSP  = QTATUAPRODSP,
                    QTATUAPRODSP  = (QTSERVFIXSP+NVL(V_QTSESBYPRSP_FINAL,0)+V_QTSESOLICSP_FINAL+QTCORTSERVSP),
                    QTREALPROD    = (QTSERVFIXSP+NVL(V_QTSESBYPRSP_FINAL,0)+V_QTSESOLICSP_FINAL+QTCORTSERVSP),
                    QTSERFATSP    = V_QTSERFATSP_INC,
                    VRPRUDOLSESP  = V_VRPREDOLAR,
                    VRPRURESESP   = V_VRPRECORRENTE,
                    VRPRUSBYSP    = V_VRPRUSBYSP,
                    CDUSRRESPSP   = P_CDOPERADOR,
                    CDOPERULTATU  = P_CDOPERADOR,
                    NRORGULTATU   = P_NRORG,
                    DTULTATU = SYSDATE
            WHERE (CDFILIAL      = P_CDFILIAL    ) AND
                   (CDCIA         = P_CDCIA       ) AND
                   (NRVOO         = P_NRVOO       ) AND
                   (CDTRECHO      = P_CDTRECHO    ) AND
                   (DTHRPLAVOOPV  = P_DTHRPLAVOOPV) AND
                   (NRSERVICOCIAFILI  = V_NRSERVICOCIAFILI   ) AND
                   (CDCLASSE      = V_CDCLASSE    ) AND
                   (CDCICLO       = V_CDCICLO     ) AND
                   (NRITEMPLANSP  = V_NRITEMPLANSP) AND
                   (IDSITSERSP    = '1'           ) AND
                   (NRDELIVERY   IS NULL          );
          ELSE
            SELECT MAX(IDSITSERSP), MAX(NRDELIVERY)
              INTO V_IDSITSERSP, V_NRDELIVERY
              FROM SERVPLANOM
             WHERE (CDFILIAL      = P_CDFILIAL    ) AND
                   (CDCIA         = P_CDCIA       ) AND
                   (NRVOO         = P_NRVOO       ) AND
                   (CDTRECHO      = P_CDTRECHO    ) AND
                   (DTHRPLAVOOPV  = P_DTHRPLAVOOPV) AND
                   (NRSERVICOCIAFILI  = V_NRSERVICOCIAFILI   ) AND
                   (CDCLASSE      = V_CDCLASSE    ) AND
                   (CDCICLO       = V_CDCICLO     ) AND
                   (NRITEMPLANSP  = V_NRITEMPLANSP);

            IF V_IDSITSERSP = '1' AND
               V_NRDELIVERY IS NULL THEN
              DELETE FROM CONEXAOSUBGRUPO
              WHERE (CDFILIAL      = P_CDFILIAL    ) AND
                     (CDCIA         = P_CDCIA       ) AND
                     (NRVOO         = P_NRVOO       ) AND
                     (CDTRECHO      = P_CDTRECHO    ) AND
                     (DTHRPLAVOOPV  = P_DTHRPLAVOOPV) AND
                     (NRSERVICOCIAFILISP  = V_NRSERVICOCIAFILI   ) AND
                     (CDCLASSESP      = V_CDCLASSE    ) AND
                     (CDCICLOSP       = V_CDCICLO     ) AND
                     (NRITEMPLANSP  = V_NRITEMPLANSP);  
              DELETE FROM SERVPLANOM
              WHERE (CDFILIAL      = P_CDFILIAL    ) AND
                     (CDCIA         = P_CDCIA       ) AND
                     (NRVOO         = P_NRVOO       ) AND
                     (CDTRECHO      = P_CDTRECHO    ) AND
                     (DTHRPLAVOOPV  = P_DTHRPLAVOOPV) AND
                     (NRSERVICOCIAFILI  = V_NRSERVICOCIAFILI   ) AND
                     (CDCLASSE      = V_CDCLASSE    ) AND
                     (CDCICLO       = V_CDCICLO     ) AND
                     (NRITEMPLANSP  = V_NRITEMPLANSP) AND
                     (IDSITSERSP    = '1'           ) AND
                     (NRDELIVERY   IS NULL          );
            END IF;
          END IF;
        END IF;

        IF V_INCLUI_SERVICO THEN /* INCLUI SERVICO */

          SELECT MAX(NRITEMPLANSP)
          INTO   V_NRITEMPLANO
          FROM   SERVPLANOM
          WHERE (CDFILIAL     = P_CDFILIAL    ) AND
                (CDCIA        = P_CDCIA       ) AND
                (NRVOO        = P_NRVOO       ) AND
                (CDTRECHO     = P_CDTRECHO    ) AND
                (DTHRPLAVOOPV = P_DTHRPLAVOOPV);

          IF (V_NRITEMPLANO IS NOT NULL) THEN
            V_NRITEMPLANO:= V_NRITEMPLANO+1;
          ELSE
            V_NRITEMPLANO:= 1;
          END IF;

          IF ((V_QTSESOLICSP_INC > 0) OR (V_QTSESBYPRSP_INC > 0)) THEN
            INSERT INTO SERVPLANOM
            (CDFILIAL   , CDCIA        , NRITEMPLANSP, NRVOO      , CDTRECHO    , DTHRPLAVOOPV,
            NRDELIVERY  , IDSITSERSP   , IDSTKDTSP   , CDUSRRESPSP, QTSESOLICSP , QTANTAPRODSP,
            QTSESBYPRSP , QTSESBYUTSP  , QTCORTSERVSP, QTSERVFIXSP,
            QTATUAPRODSP, QTSERFATSP   ,
            QTREALPROD  , DTHRFEPRODSP , VRPESOSESP  , DTHRINCSESP,
            DTHRTRASESP , DTHRCANCSESP , CDAGRDECO   , CDAGRDEDE  ,
            IDSEREXTSP  , NRSERVICOCIAFILI, CDCLASSE, CDCICLO, VRPRUDOLSESP, VRPRURESESP,
            IDTIPSCBOSP , VRLOADSESP   , NRPROPAXSP  ,
            VRPRUSBYSP  , IDTIPMANUALSP, IDTPMANUTSP , IDTPPRECOSP, IDFATSESP,
            NRORG, NRORGINCLUSAO, NRORGULTATU,
            CDOPERINCLUSAO, CDOPERULTATU,
            DTINCLUSAO, DTULTATU
            )
          VALUES
            (P_CDFILIAL ,P_CDCIA, V_NRITEMPLANO, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV,
             NULL, '1', '1',   P_CDOPERADOR, V_QTSESOLICSP_INC, 0,
             V_QTSESBYPRSP_INC, 0, 0, V_QTSERVFIXSP,
             (V_QTSERVFIXSP+V_QTSESBYPRSP_INC+V_QTSESOLICSP_INC),V_QTSERFATSP_INC,
             (V_QTSERVFIXSP+V_QTSESBYPRSP+V_QTSESOLICSP),NULL, V_VRPESOSE, SYSDATE,
             NULL, NULL, V_CDAGRDECO, V_CDCLASSE,
             '1', V_NRSERVICOCIAFILI, V_CDCLASSE, V_CDCICLO, V_VRPREDOLAR,V_VRPRECORRENTE,
             V_IDPROPSERV, V_VRPELOADSP, V_NRPROPAXSP,
             V_VRPRUSBYSP, V_IDTIPMANUAL,'1D',V_IDTPPRECO,V_IDFATSESP,
             P_NRORG, P_NRORG, P_NRORG,
             P_CDOPERADOR, P_CDOPERADOR,
             SYSDATE, SYSDATE);

            CONEXAO_COM_SUBGRU_PLANOVOOM(P_CDFILIAL, P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV, 
                                         V_NRSERVICOCIAFILI, V_CDCLASSE, V_CDCICLO, V_DTHRABASTPV, 
                                         'N', V_NRITEMPLANO, P_CDOPERADOR, P_NRORG, V_MSGERRO);
          END IF;          
          
        END IF;

  END LOOP;
   

   /* update a plano voo para ajustar a situacao do plano de voo */
   REVALIDA_STATUS_PLANOVOOM(P_CDFILIAL, P_CDCIA, P_NRVOO, P_CDTRECHO, 
                             P_DTHRPLAVOOPV, P_CDOPERADOR, P_NRORG);
END;
/