CREATE OR REPLACE PROCEDURE GERA_PREVISAO_PAXM(
  P_CDFILIAL        IN FILIAL.CDFILIAL%TYPE,
  P_DTRELPLANPAX    IN  RELPREVPAX.DTRELPLANPAX%TYPE,
  P_NRRELPLANPAX    IN  RELPREVPAX.NRRELPLANPAX%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_TIPPREVISAO     IN  VARCHAR2,
  P_IDTPGERPPT      IN  VARCHAR2 DEFAULT 'P',
  P_VRPEROCUPACAO   IN  FLOAT DEFAULT 100,
  P_SIMULACAO       CHAR,
  P_NRDIAS          NUMBER,
  P_CDEQUIP         IN VOO.CDEQUIP%TYPE DEFAULT NULL,
  P_SGPREFIXO       IN VOO.SGPREFIXO%TYPE DEFAULT NULL,
  P_CDOPERADOR      IN OPERADOR.CDOPERADOR%TYPE,
  P_NRORG           IN NUMBER DEFAULT 1
 )AS
  V_TESTE               NUMBER;
  V_DTHRPLAVOOPV        PLANOVOOM.DTHRPLAVOOPV%TYPE;
  V_EXISTECLASSE        CHAR(1);
  V_SGPREFIXO           PREFIXOCIA.SGPREFIXO%TYPE;
  V_CDEQUIP             EQUIPAMENTO.CDEQUIP%TYPE;
  V_CDCLASSE            CLASSE.CDCLASSE%TYPE;
  V_IDCLASSE            CLASSE.IDCLASSE%TYPE;
  V_QTTOTPAXAT_QTPAXSBY PREVISAOPAXM.QTTOTPAXAT%TYPE;
  V_QTTOTPAXAT          PREVISAOPAXM.QTTOTPAXAT%TYPE;
  V_QTPAXSBY            PREVISAOPAXM.QTTOTPAXAT%TYPE;
  V_QTTOTPAXATMED       PREVISAOPAXM.QTTOTPAXAT%TYPE;
  V_QTPAXSBYMED         PREVISAOPAXM.QTTOTPAXAT%TYPE;
  V_DTINICIAL           DATE;
  V_DTFINAL             DATE;
  V_DIASMEDIA           NUMBER;
  V_VRPEROCUPACAO       FLOAT;
  V_IDOBRIGAEQUIPPLAVOO VARCHAR2(1);
  V_VOO                 VARCHAR2(200);
  V_CDEQUIPINF          VARCHAR2(4);
  -- ================================================================ --
  --              DECLARACAO DE CURSOR                                --
  -- ================================================================ --
  CURSOR C_CONFIGVOO_ZERADA IS
   SELECT CV.CDCLASSE, IDCLASSE
   FROM   CONFIGVOO CV, CLASSE CL
   WHERE (CV.CDFILIAL = P_CDFILIAL) AND
         (CV.CDCIA    = P_CDCIA)    AND
         (CV.NRVOO    = P_NRVOO)    AND
         (CV.CDTRECHO = P_CDTRECHO) AND
         (CV.CDCLASSE = CL.CDCLASSE);

  CURSOR C_CONFIGPREF_ZERADA IS
   SELECT CP.CDCLASSE, IDCLASSE
   FROM   CONFIGPREF CP, CLASSE CL
   WHERE (CP.CDFILIAL  = P_CDFILIAL) AND
         (CP.CDCIA     = P_CDCIA)     AND
         (CP.SGPREFIXO = V_SGPREFIXO) AND
         (CP.CDCLASSE  = CL.CDCLASSE);

  CURSOR C_CONFIGEQUI_ZERADA IS
   SELECT CE.CDCLASSE, IDCLASSE
   FROM   CONFIGEQUI CE, CLASSE CL
   WHERE (CE.CDFILIAL = P_CDFILIAL) AND
         (CE.CDCIA    = P_CDCIA)   AND
         (CE.CDEQUIP  = V_CDEQUIP) AND
         (CE.CDCLASSE = CL.CDCLASSE);

  CURSOR C_CLASSECIA_ZERADA IS
   SELECT CC.CDCLASSE, IDCLASSE
   FROM   CLASSECIA CC, CLASSE CL
   WHERE (CC.CDFILIAL = P_CDFILIAL) AND
         (CC.CDCIA    = P_CDCIA) AND
         (CC.CDCLASSE = CL.CDCLASSE);
BEGIN
   SELECT MAX(CF.IDOBRIGAEQUIPPLAVOO), 
          MAX(C.SGCIA || '-' || VT.NRVOO || ' ' || BO.SGBASE ||'-'||BD.SGBASE),
          MAX(P.CDEQUIP)
     INTO V_IDOBRIGAEQUIPPLAVOO, V_VOO, V_CDEQUIPINF
     FROM VOOTRECHO VT
          INNER JOIN BASE BO ON BO.CDBASE = VT.CDBASEORI
          INNER JOIN BASE BD ON BD.CDBASE = VT.CDBASEDES
          LEFT JOIN CIAFILI CF ON CF.CDFILIAL = VT.CDFILIAL AND 
                                  CF.CDCIA = VT.CDCIA
          INNER JOIN COMPANHIA C ON C.CDCIA = VT.CDCIA
          LEFT JOIN PLANOVOOEQUIP P ON P.CDFILIAL = VT.CDFILIAL AND
                                     P.CDCIA = VT.CDCIA AND
                                     P.NRVOO = VT.NRVOO AND
                                     P.CDTRECHO = VT.CDTRECHO AND
                                     P.DTHRPLAVOOPV = P_DTHRPLAVOOPV
    WHERE VT.CDFILIAL = P_CDFILIAL
      AND VT.CDCIA = P_CDCIA
      AND VT.NRVOO = P_NRVOO 
      AND VT.CDTRECHO = P_CDTRECHO;

   IF V_CDEQUIPINF IS NULL THEN
      IF V_IDOBRIGAEQUIPPLAVOO = 'S' THEN
         /*Equipamento nao informado para o voo param[0]*/
         RAISE_APPLICATION_ERROR(-20502, 'APPERRMSGSTR' || 'APP-00153' || '$$' || V_VOO || 'APPERRMSGEND');
      ELSE
         SELECT CDEQUIP, NULL
         INTO V_CDEQUIP, V_SGPREFIXO
         FROM VOOTRECHODATA
         WHERE (CDFILIAL = P_CDFILIAL) AND
               (CDCIA    = P_CDCIA   ) AND
               (NRVOO    = P_NRVOO   ) AND
               (CDTRECHO = P_CDTRECHO) AND
               (P_DTHRPLAVOOPV BETWEEN DTINIVIGENCIA AND NVL(DTFIMVIGENCIA, P_DTHRPLAVOOPV));   
      END IF;
   ELSE 
      V_CDEQUIP := V_CDEQUIPINF;
   END IF;   
  -- =========================================================== --
  -- Busca configuracao das classes primeiro na configvoo.       --
  -- =========================================================== --
  V_EXISTECLASSE := '0';

  -- 1 = ZERADA COM INSERT NA PREVPAXTEMPM
  IF P_TIPPREVISAO = '1' THEN  
  BEGIN
    --Apaga previsao de passageiros (caso exista)
    DELETE FROM PREVPAXTEMPM
     WHERE CDFILIAL = P_CDFILIAL 
       AND CDCIAPPT = P_CDCIA
       AND NRVOOPPT = P_NRVOO 
       AND CDTRECHOPPT = P_CDTRECHO 
       AND DTHRPLAPPT = P_DTHRPLAVOOPV;
       
    OPEN C_CONFIGVOO_ZERADA;
    LOOP
      FETCH C_CONFIGVOO_ZERADA INTO V_CDCLASSE, V_IDCLASSE;
      EXIT WHEN C_CONFIGVOO_ZERADA%NOTFOUND;


      SOMATORIA_QTDE_PAX_STANDBYM(P_CDFILIAL, P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDCLASSE,
                                 V_QTTOTPAXAT_QTPAXSBY,V_QTTOTPAXAT,V_QTPAXSBY,V_CDEQUIP,V_SGPREFIXO);

      IF V_IDCLASSE IN ('2', '3') THEN
        V_QTTOTPAXAT:= 1;
      ELSE
        V_QTTOTPAXAT:= 0;
      END IF;

      IF P_SIMULACAO = '1' THEN
        INSERT INTO RELPREVPAX
         (CDFILIAL, DTRELPLANPAX, NRRELPLANPAX, CDCIAPAX, NRVOOPAX, CDTRECHOPAX,
          CDCLASSEPAX, QTTOPAXATPAX, QTPAXSBYPAX )
        VALUES
         (P_CDFILIAL, P_DTRELPLANPAX, P_NRRELPLANPAX, P_CDCIA, P_NRVOO, P_CDTRECHO,
          V_CDCLASSE, V_QTTOTPAXAT, V_QTPAXSBY);
      ELSE 
        IF P_SIMULACAO = '2' THEN  
           ----------------------------------------------------------------------------------------------------------
           --Quando P_SIMULACAO = '2', a previsao de passageiros e para a geracao de programacao de servico (MAC06700) 
           -----------------------------------------------------------------------------------------------------------
                    
            INSERT INTO PREVPAXSRVTEMP
              (CDFILIAL, CDCIAPPT, NRVOOPPT, CDTRECHOPPT,  DTHRPLAPPT, CDCLASSEPPT,IDTPGERPPT,
               QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, CDEQUIPPPT, SGPREFIXOPPT)
            VALUES
              (P_CDFILIAL, P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV, V_CDCLASSE, P_IDTPGERPPT,
               V_QTTOTPAXAT, V_QTPAXSBY, '3', V_CDEQUIP, V_SGPREFIXO);
        ELSE           
           INSERT INTO PREVPAXTEMPM
            (CDFILIAL, CDCIAPPT, NRVOOPPT, CDTRECHOPPT,  DTHRPLAPPT, CDCLASSEPPT,IDTPGERPPT,
             QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, CDEQUIPPPT, SGPREFIXOPPT,
             NRORG, DTINCLUSAO, CDOPERINCLUSAO, DTULTATU, NRORGINCLUSAO, NRORGULTATU, CDOPERULTATU, IDATIVO)
           VALUES
            (P_CDFILIAL, P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV, V_CDCLASSE, P_IDTPGERPPT,
             V_QTTOTPAXAT, V_QTPAXSBY, '3', V_CDEQUIP, V_SGPREFIXO,
             P_NRORG, SYSDATE, P_CDOPERADOR, SYSDATE, P_NRORG, P_NRORG, P_CDOPERADOR, 'S');
        END IF;
      END IF;

      V_EXISTECLASSE:= '1';
    END LOOP;
    CLOSE C_CONFIGVOO_ZERADA;

    -- =========================================================== --
    -- Se nao tiver configuracao de pax por voo, verifica se tem   --
    -- por prefixo                                  --
    -- =========================================================== --
    IF V_EXISTECLASSE <> '1' THEN
    BEGIN

      OPEN C_CONFIGPREF_ZERADA;

      LOOP
        FETCH C_CONFIGPREF_ZERADA INTO V_CDCLASSE, V_IDCLASSE;
        EXIT WHEN C_CONFIGPREF_ZERADA%NOTFOUND;

         SOMATORIA_QTDE_PAX_STANDBYM(P_CDFILIAL, P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDCLASSE,
                                   V_QTTOTPAXAT_QTPAXSBY,V_QTTOTPAXAT,V_QTPAXSBY,V_CDEQUIP,V_SGPREFIXO);

        IF V_IDCLASSE IN ('2', '3') THEN
          V_QTTOTPAXAT:= 1;
        ELSE
          V_QTTOTPAXAT:= 0;
        END IF;

        IF P_SIMULACAO = '1' THEN
          INSERT INTO RELPREVPAX
           (CDFILIAL, DTRELPLANPAX, NRRELPLANPAX, CDCIAPAX, NRVOOPAX, CDTRECHOPAX,
            CDCLASSEPAX, QTTOPAXATPAX, QTPAXSBYPAX)
          VALUES
           (P_CDFILIAL, P_DTRELPLANPAX, P_NRRELPLANPAX, P_CDCIA, P_NRVOO, P_CDTRECHO,
           V_CDCLASSE, V_QTTOTPAXAT, V_QTPAXSBY);
        ELSE
           IF P_SIMULACAO = '2' THEN
             --Quando P_SIMULACAO = '2', a previsao de passageiros e para a geracao de programacao de servico (MAC06700) 
             INSERT INTO PREVPAXSRVTEMP
              (CDFILIAL, CDCIAPPT, NRVOOPPT, CDTRECHOPPT, DTHRPLAPPT, CDCLASSEPPT, IDTPGERPPT,
               QTTOTPAXATPPT,QTPAXSBYPPT, IDTPPREVPPT, CDEQUIPPPT, SGPREFIXOPPT,VRPARAMPREVPAX)
             VALUES
              (P_CDFILIAL, P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV, V_CDCLASSE, P_IDTPGERPPT,
               V_QTTOTPAXAT, V_QTPAXSBY, '3', V_CDEQUIP, V_SGPREFIXO,0);
           ELSE
             INSERT INTO PREVPAXTEMPM
              (CDFILIAL, CDCIAPPT, NRVOOPPT, CDTRECHOPPT, DTHRPLAPPT, CDCLASSEPPT, IDTPGERPPT,
               QTTOTPAXATPPT,QTPAXSBYPPT, IDTPPREVPPT, CDEQUIPPPT, SGPREFIXOPPT,
             NRORG, DTINCLUSAO, CDOPERINCLUSAO, DTULTATU, NRORGINCLUSAO, NRORGULTATU, CDOPERULTATU, IDATIVO)
             VALUES
              (P_CDFILIAL, P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV, V_CDCLASSE, P_IDTPGERPPT,
               V_QTTOTPAXAT, V_QTPAXSBY, '3', V_CDEQUIP, V_SGPREFIXO,
               P_NRORG, SYSDATE, P_CDOPERADOR, SYSDATE, P_NRORG, P_NRORG, P_CDOPERADOR, 'S');
          END IF;
        END IF;
        V_EXISTECLASSE:= '1';
      END LOOP;
      CLOSE C_CONFIGPREF_ZERADA;

      -- =========================================================== --
      -- Se nao tiver configuracao de pax por voo, e nem por prefixo --
      -- verifica se tem por equipamento                             --
      -- =========================================================== --
      IF V_EXISTECLASSE <> '1' THEN
      BEGIN
        OPEN C_CONFIGEQUI_ZERADA;
        LOOP
          FETCH C_CONFIGEQUI_ZERADA INTO V_CDCLASSE, V_IDCLASSE;
          EXIT WHEN C_CONFIGEQUI_ZERADA%NOTFOUND;

          SOMATORIA_QTDE_PAX_STANDBYM(P_CDFILIAL, P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDCLASSE,
                                     V_QTTOTPAXAT_QTPAXSBY,V_QTTOTPAXAT,V_QTPAXSBY,V_CDEQUIP,V_SGPREFIXO);

          IF V_IDCLASSE IN ('2', '3') THEN
            V_QTTOTPAXAT:= 1;
          ELSE
            V_QTTOTPAXAT:= 0;
          END IF;

          IF P_SIMULACAO = '1' THEN
            INSERT INTO RELPREVPAX
             (CDFILIAL, DTRELPLANPAX, NRRELPLANPAX, CDCIAPAX, NRVOOPAX, CDTRECHOPAX,
              CDCLASSEPAX, QTTOPAXATPAX, QTPAXSBYPAX)
            VALUES
             (P_CDFILIAL, P_DTRELPLANPAX, P_NRRELPLANPAX, P_CDCIA, P_NRVOO, P_CDTRECHO,
             V_CDCLASSE, V_QTTOTPAXAT, V_QTPAXSBY);
          ELSE
             IF P_SIMULACAO = '2' THEN
                --Quando P_SIMULACAO = '2', a previsao de passageiros e para a geracao de programacao de servico (MAC06700) 
                INSERT INTO PREVPAXSRVTEMP
                (CDFILIAL, CDCIAPPT, NRVOOPPT, CDTRECHOPPT, DTHRPLAPPT, CDCLASSEPPT, IDTPGERPPT,
                 QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, CDEQUIPPPT, SGPREFIXOPPT,VRPARAMPREVPAX)
               VALUES
                (P_CDFILIAL, P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV, V_CDCLASSE, P_IDTPGERPPT,
                V_QTTOTPAXAT, V_QTPAXSBY, '3', V_CDEQUIP, V_SGPREFIXO,0);
             ELSE
               INSERT INTO PREVPAXTEMPM
                (CDFILIAL, CDCIAPPT, NRVOOPPT, CDTRECHOPPT, DTHRPLAPPT, CDCLASSEPPT, IDTPGERPPT,
                 QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, CDEQUIPPPT, SGPREFIXOPPT,
             NRORG, DTINCLUSAO, CDOPERINCLUSAO, DTULTATU, NRORGINCLUSAO, NRORGULTATU, CDOPERULTATU, IDATIVO)
               VALUES
               (P_CDFILIAL, P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV, V_CDCLASSE, P_IDTPGERPPT,
                V_QTTOTPAXAT, V_QTPAXSBY, '3', V_CDEQUIP, V_SGPREFIXO,
                P_NRORG, SYSDATE, P_CDOPERADOR, SYSDATE, P_NRORG, P_NRORG, P_CDOPERADOR, 'S');
            END IF;
          END IF;
          V_EXISTECLASSE:= '1';
        END LOOP;
        CLOSE C_CONFIGEQUI_ZERADA;

        -- =========================================================== --
        -- Se nao tiver configuracao de pax por voo, nem o prefixo,    --
        -- nem no equipamento, emite uma mensagem de erro              --
        -- =========================================================== --
        IF V_EXISTECLASSE <> '1' THEN
        BEGIN
          --raise_application_error( -20502, 'Nao existe configuracao de passageiros para o voo '||P_NRVOO||', nem para seu prefixo e nem para seu equipamento! Favor verificar na opcao "Menu Ambiente - Cadastros - Configuracao de Passageiros" e tentar novamente.', false);
          OPEN C_CLASSECIA_ZERADA;
          LOOP
            FETCH C_CLASSECIA_ZERADA INTO V_CDCLASSE, V_IDCLASSE;

            EXIT WHEN C_CLASSECIA_ZERADA%NOTFOUND;

            IF V_IDCLASSE IN ('2', '3') THEN
              V_QTTOTPAXAT:= 1;
            ELSE
              V_QTTOTPAXAT:= 0;
            END IF;
            V_QTPAXSBY := 0;

            IF P_SIMULACAO = '1' THEN
              INSERT INTO RELPREVPAX
               (CDFILIAL, DTRELPLANPAX, NRRELPLANPAX, CDCIAPAX, NRVOOPAX, CDTRECHOPAX,
                CDCLASSEPAX, QTTOPAXATPAX, QTPAXSBYPAX )
              VALUES
               (P_CDFILIAL, P_DTRELPLANPAX, P_NRRELPLANPAX, P_CDCIA, P_NRVOO, P_CDTRECHO,
                V_CDCLASSE, V_QTTOTPAXAT, V_QTPAXSBY);
            ELSE
               IF P_SIMULACAO = '2' THEN
                 --Quando P_SIMULACAO = '2', a previsao de passageiros e para a geracao de programacao de servico (MAC06700) 
                 INSERT INTO PREVPAXSRVTEMP
                  (CDFILIAL, CDCIAPPT, NRVOOPPT, CDTRECHOPPT, DTHRPLAPPT, CDCLASSEPPT,IDTPGERPPT,
                   QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, CDEQUIPPPT, SGPREFIXOPPT,VRPARAMPREVPAX)
                 VALUES
                   (P_CDFILIAL, P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV, V_CDCLASSE, P_IDTPGERPPT,
                    V_QTTOTPAXAT, V_QTPAXSBY, '3', V_CDEQUIP, V_SGPREFIXO,0);
               ELSE
                 INSERT INTO PREVPAXTEMPM
                  (CDFILIAL, CDCIAPPT, NRVOOPPT, CDTRECHOPPT, DTHRPLAPPT, CDCLASSEPPT,IDTPGERPPT,
                   QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, CDEQUIPPPT, SGPREFIXOPPT,
             NRORG, DTINCLUSAO, CDOPERINCLUSAO, DTULTATU, NRORGINCLUSAO, NRORGULTATU, CDOPERULTATU, IDATIVO)
                 VALUES
                   (P_CDFILIAL, P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV, V_CDCLASSE, P_IDTPGERPPT,
                    V_QTTOTPAXAT, V_QTPAXSBY, '3', V_CDEQUIP, V_SGPREFIXO,
                    P_NRORG, SYSDATE, P_CDOPERADOR, SYSDATE, P_NRORG, P_NRORG, P_CDOPERADOR, 'S');
               END IF;  
            END IF;
          END LOOP;
          CLOSE C_CLASSECIA_ZERADA;
        END;
        END IF;
      END;
       --fim do teste do primeiro se V_EXISTECLASSE <> 0 
      END IF;
    END;
      --fim do teste do segundo se V_EXISTECLASSE <> 0 
    END IF;
    -- Salva o equipamento e prefixo padrao do voo para restaura-lo
    -- na GeraPlanoVoo apos a geracao do plano de voo. 
    UPDATE VOO
    SET CDEQUIPTMP   = CDEQUIP,
        SGPREFIXOTMP = SGPREFIXO
    WHERE CDFILIAL = P_CDFILIAL AND
          CDCIA    = P_CDCIA    AND
          NRVOO    = P_NRVOO;

  END;
  -- 2 = HISTORICO 
  ELSIF P_TIPPREVISAO = '2' THEN 
  BEGIN
    
    IF P_SIMULACAO <> '2' THEN
       SELECT MAX(DTHRPLAVOOPV)
       INTO   V_DTHRPLAVOOPV
       FROM   PLANOVOOM
       WHERE (CDFILIAL = P_CDFILIAL) AND
             (CDCIA    = P_CDCIA   ) AND
             (NRVOO    = P_NRVOO   ) AND
             (CDTRECHO = P_CDTRECHO);
    ELSE
       SELECT MAX(DTDECOLVOO)
       INTO   V_DTHRPLAVOOPV
       FROM   VOOSPROGSERV
       WHERE (CDFILIAL = P_CDFILIAL) AND
             (CDCIA    = P_CDCIA   ) AND
             (NRVOO    = P_NRVOO   ) AND
             (CDTRECHO = P_CDTRECHO);
    END IF;

    IF (V_DTHRPLAVOOPV IS NOT NULL) THEN
    BEGIN
      
      
      ---Para Programacao de servicos (P_SIMULACAO = 2) e utilizada a tabela PREVISAOPAXSRFV
      IF P_SIMULACAO <> '2' THEN
         SELECT COUNT(*)
         INTO   V_TESTE
         FROM   PREVISAOPAXM
         WHERE (CDFILIAL     = P_CDFILIAL) AND
               (CDCIA        = P_CDCIA   ) AND
               (NRVOO        = P_NRVOO   ) AND
               (CDTRECHO     = P_CDTRECHO) AND
               (DTHRPLAVOOPV = V_DTHRPLAVOOPV);
      ELSE
         SELECT COUNT(*)
         INTO   V_TESTE
         FROM   PREVISAOPAXSRV
         WHERE (CDFILIAL     = P_CDFILIAL) AND
               (CDCIA        = P_CDCIA   ) AND
               (NRVOO        = P_NRVOO   ) AND
               (CDTRECHO     = P_CDTRECHO) AND
               (DTDECOLVOO   = V_DTHRPLAVOOPV);
      
      END IF;

      IF (V_TESTE <> 0) THEN
      BEGIN
        IF P_SIMULACAO = '1' THEN
          UPDATE RELPREVPAX PPT
          SET   (QTTOPAXATPAX, QTPAXSBYPAX) = (SELECT PP.QTTOTPAXAT, NVL(PP.QTPAXSBY,0)
                                               FROM   PREVISAOPAXM PP
                                               WHERE (PP.CDFILIAL     = P_CDFILIAL)      AND
                                                     (PP.CDCIA        = P_CDCIA)         AND
                                                     (PP.NRVOO        = P_NRVOO)         AND
                                                     (PP.CDTRECHO     = P_CDTRECHO)      AND
                                                     (PP.DTHRPLAVOOPV = V_DTHRPLAVOOPV)  AND
                                                     (PP.CDCLASSE     = PPT.CDCLASSEPAX) AND
                                                     (PPT.CDFILIAL    = PP.CDFILIAL    ) AND
                                                     (PPT.CDCIAPAX    = PP.CDCIA       ) AND
                                                     (PPT.NRVOOPAX    = PP.NRVOO       ) AND
                                                     (PPT.CDTRECHOPAX = PP.CDTRECHO))
          WHERE (PPT.CDFILIAL    = P_CDFILIAL  )   AND
                (PPT.CDCIAPAX    = P_CDCIA)        AND
                (PPT.NRVOOPAX    = P_NRVOO)        AND
                (PPT.CDTRECHOPAX = P_CDTRECHO)     AND
                (PPT.DTRELPLANPAX= P_DTRELPLANPAX) AND
                (PPT.NRRELPLANPAX= P_NRRELPLANPAX) AND
                (PPT.CDCLASSEPAX IN (SELECT CDCLASSE
                                     FROM PREVISAOPAXM
                                     WHERE (CDFILIAL     = P_CDFILIAL)      AND
                                           (CDCIA        = P_CDCIA)         AND
                                           (NRVOO        = P_NRVOO)         AND
                                           (CDTRECHO     = P_CDTRECHO)      AND
                                           (DTHRPLAVOOPV = V_DTHRPLAVOOPV)));
        ELSE
          IF P_SIMULACAO = '2' THEN
            
          
             
             UPDATE PREVPAXSRVTEMP PPT
             SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT,VRPARAMPREVPAX) =
                 (SELECT PP.QTTOTPAXAT, NVL(PP.QTPAXSBY,0), '1' IDTPPREVPPT, P_IDTPGERPPT, 0 VRPARAMPREVPAX
                  FROM   PREVISAOPAXSRV PP
                  WHERE (PP.CDFILIAL     = P_CDFILIAL  )    AND
                        (PP.CDCIA        = P_CDCIA)         AND
                        (PP.NRVOO        = P_NRVOO)         AND
                        (PP.CDTRECHO     = P_CDTRECHO)      AND
                        (PP.DTDECOLVOO   = V_DTHRPLAVOOPV)  AND
                        (PP.CDCLASSE     = PPT.CDCLASSEPPT) AND
                        (PPT.CDFILIAL    = PP.CDFILIAL    ) AND
                        (PPT.CDCIAPPT    = PP.CDCIA       ) AND
                        (PPT.NRVOOPPT    = PP.NRVOO       ) AND
                        (PPT.CDTRECHOPPT = PP.CDTRECHO))
             WHERE (PPT.CDFILIAL    = P_CDFILIAL  ) AND
                   (PPT.CDCIAPPT    = P_CDCIA     ) AND
                   (PPT.NRVOOPPT    = P_NRVOO     ) AND
                   (PPT.CDTRECHOPPT = P_CDTRECHO  ) AND
                   (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                   (PPT.DTHRPLAPPT  = P_DTHRPLAVOOPV) AND
                   (PPT.CDCLASSEPPT IN (SELECT CDCLASSE
                                        FROM PREVISAOPAXSRV
                                        WHERE (CDFILIAL     = P_CDFILIAL   )   AND
                                              (CDCIA        = P_CDCIA)         AND
                                              (NRVOO        = P_NRVOO)         AND
                                              (CDTRECHO     = P_CDTRECHO)      AND
                                              (DTDECOLVOO   = V_DTHRPLAVOOPV)));
          ELSE   
             UPDATE PREVPAXTEMPM PPT
             SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT) =
                 (SELECT PP.QTTOTPAXAT, NVL(PP.QTPAXSBY,0), '1' IDTPPREVPPT, P_IDTPGERPPT
                  FROM   PREVISAOPAXM PP
                  WHERE (PP.CDFILIAL     = P_CDFILIAL  )    AND
                        (PP.CDCIA        = P_CDCIA)         AND
                        (PP.NRVOO        = P_NRVOO)         AND
                        (PP.CDTRECHO     = P_CDTRECHO)      AND
                        (PP.DTHRPLAVOOPV = V_DTHRPLAVOOPV)  AND
                        (PP.CDCLASSE     = PPT.CDCLASSEPPT) AND
                        (PPT.CDFILIAL    = PP.CDFILIAL    ) AND
                        (PPT.CDCIAPPT    = PP.CDCIA       ) AND
                        (PPT.NRVOOPPT    = PP.NRVOO       ) AND
                        (PPT.CDTRECHOPPT = PP.CDTRECHO))
             WHERE (PPT.CDFILIAL    = P_CDFILIAL  ) AND
                   (PPT.CDCIAPPT    = P_CDCIA     ) AND
                   (PPT.NRVOOPPT    = P_NRVOO     ) AND
                   (PPT.CDTRECHOPPT = P_CDTRECHO  ) AND
                   (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                   (PPT.CDCLASSEPPT IN (SELECT CDCLASSE
                                        FROM PREVISAOPAXM
                                        WHERE (CDFILIAL     = P_CDFILIAL   )   AND
                                              (CDCIA        = P_CDCIA)         AND
                                              (NRVOO        = P_NRVOO)         AND
                                              (CDTRECHO     = P_CDTRECHO)      AND
                                              (DTHRPLAVOOPV = V_DTHRPLAVOOPV)));
            END IF;
        END IF;
      END;
      ELSE RAISE_APPLICATION_ERROR( -20501, 'APPERRMSGSTR' || 'APP-00016' || 'APPERRMSGEND', FALSE);

      --Nao existe historico para a companhia/voo/trecho informados!', false);
      END IF;
    END;
    ELSE RAISE_APPLICATION_ERROR( -20502, 'APPERRMSGSTR' || 'APP-00016' || 'APPERRMSGEND', FALSE);

    --Nao existe historico para a companhia/voo/trecho informados!', false);
    END IF;
  END;
  -- 3 = MAXIMA 
  ELSIF P_TIPPREVISAO = '3' THEN  
  BEGIN
    -- ============================================================ --
    --  GARANTIR QUE TODAS AS QUANTIDADES NAO ESTEJAM NULAS         --
    -- ============================================================ --
    UPDATE CONFIGVOO
    SET    QTPAXSBYVOO = 0
    WHERE (QTPAXSBYVOO IS NULL);

    UPDATE CONFIGPREF
    SET    QTPAXSBYPREF = 0
    WHERE (QTPAXSBYPREF IS NULL);

    UPDATE CONFIGEQUI
    SET    QTPAXSBYEQUI = 0
    WHERE (QTPAXSBYEQUI IS NULL);

    -- =========================================================== --
    -- Busca configuracao das classes primeiro na configvoo.       --
    -- =========================================================== --
    SELECT COUNT(CDCLASSE)
    INTO   V_TESTE
    FROM   CONFIGVOO
    WHERE (CDFILIAL = P_CDFILIAL) AND
          (CDCIA    = P_CDCIA   ) AND
          (NRVOO    = P_NRVOO   ) AND
          (CDTRECHO = P_CDTRECHO);

    IF V_TESTE <> 0 THEN
    BEGIN
      IF P_SIMULACAO = '1' THEN
        UPDATE RELPREVPAX PPT
        SET   (QTTOPAXATPAX,QTPAXSBYPAX) = (SELECT CV.QTPAXVOO, CV.QTPAXSBYVOO
                                            FROM   CONFIGVOO CV
                                            WHERE (CV.CDFILIAL     = P_CDFILIAL ) AND
                                                  (CV.CDCIA        = P_CDCIA    ) AND
                                                  (CV.NRVOO        = P_NRVOO    ) AND
                                                  (CV.CDTRECHO     = P_CDTRECHO ) AND
                                                  (PPT.CDFILIAL    = CV.CDFILIAL) AND
                                                  (PPT.CDCIAPAX    = CV.CDCIA   ) AND
                                                  (PPT.NRVOOPAX    = CV.NRVOO   ) AND
                                                  (PPT.CDTRECHOPAX = CV.CDTRECHO) AND
                                                  (PPT.CDCLASSEPAX = CV.CDCLASSE))
        WHERE (PPT.CDFILIAL    = P_CDFILIAL )    AND
              (PPT.CDCIAPAX    = P_CDCIA)        AND
              (PPT.NRVOOPAX    = P_NRVOO)        AND
              (PPT.CDTRECHOPAX = P_CDTRECHO)     AND
              (PPT.DTRELPLANPAX= P_DTRELPLANPAX) AND
              (PPT.NRRELPLANPAX= P_NRRELPLANPAX) AND
              (PPT.CDCLASSEPAX IN (SELECT CDCLASSE
                                   FROM CONFIGVOO
                                   WHERE (CDFILIAL = P_CDFILIAL) AND
                                         (CDCIA    = P_CDCIA   ) AND
                                         (NRVOO    = P_NRVOO   ) AND
                                         (CDTRECHO = P_CDTRECHO)));
      ELSE
        IF P_SIMULACAO = '2' THEN
           UPDATE PREVPAXSRVTEMP PPT
           SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT,VRPARAMPREVPAX) =
               (SELECT CV.QTPAXVOO, CV.QTPAXSBYVOO, '2' IDTPPREVPPT, P_IDTPGERPPT, 0 VRPARAMPREVPAX
                FROM CONFIGVOO CV
                WHERE (CV.CDFILIAL     = P_CDFILIAL ) AND
                      (CV.CDCIA        = P_CDCIA    ) AND
                      (CV.NRVOO        = P_NRVOO    ) AND
                      (CV.CDTRECHO     = P_CDTRECHO ) AND
                      (PPT.CDFILIAL    = CV.CDFILIAL) AND
                      (PPT.CDCIAPPT    = CV.CDCIA   ) AND
                      (PPT.NRVOOPPT    = CV.NRVOO   ) AND
                      (PPT.CDTRECHOPPT = CV.CDTRECHO) AND
                      (PPT.CDCLASSEPPT = CV.CDCLASSE))
           WHERE (PPT.CDFILIAL    = P_CDFILIAL  ) AND
                 (PPT.CDCIAPPT    = P_CDCIA     ) AND
                 (PPT.NRVOOPPT    = P_NRVOO     ) AND
                 (PPT.CDTRECHOPPT = P_CDTRECHO  ) AND
                 (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                 (PPT.DTHRPLAPPT  = P_DTHRPLAVOOPV) AND
                 (PPT.CDCLASSEPPT IN (SELECT CDCLASSE
                                      FROM CONFIGVOO
                                      WHERE (CDFILIAL     = P_CDFILIAL )     AND
                                            (CDCIA        = P_CDCIA)         AND
                                            (NRVOO        = P_NRVOO)         AND
                                            (CDTRECHO     = P_CDTRECHO)));
        ELSE
           UPDATE PREVPAXTEMPM PPT
           SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT) =
               (SELECT CV.QTPAXVOO, CV.QTPAXSBYVOO, '2' IDTPPREVPPT, P_IDTPGERPPT
                FROM CONFIGVOO CV
                WHERE (CV.CDFILIAL     = P_CDFILIAL ) AND
                      (CV.CDCIA        = P_CDCIA    ) AND
                      (CV.NRVOO        = P_NRVOO    ) AND
                      (CV.CDTRECHO     = P_CDTRECHO ) AND
                      (PPT.CDFILIAL    = CV.CDFILIAL) AND
                      (PPT.CDCIAPPT    = CV.CDCIA   ) AND
                      (PPT.NRVOOPPT    = CV.NRVOO   ) AND
                      (PPT.CDTRECHOPPT = CV.CDTRECHO) AND
                      (PPT.CDCLASSEPPT = CV.CDCLASSE))
           WHERE (PPT.CDFILIAL    = P_CDFILIAL  ) AND
                 (PPT.CDCIAPPT    = P_CDCIA     ) AND
                 (PPT.NRVOOPPT    = P_NRVOO     ) AND
                 (PPT.CDTRECHOPPT = P_CDTRECHO  ) AND
                 (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                 (PPT.CDCLASSEPPT IN (SELECT CDCLASSE
                                      FROM CONFIGVOO
                                      WHERE (CDFILIAL     = P_CDFILIAL )     AND
                                            (CDCIA        = P_CDCIA)         AND
                                            (NRVOO        = P_NRVOO)         AND
                                            (CDTRECHO     = P_CDTRECHO)));
        END IF;
      END IF;
    END;
    END IF;

    -- =========================================================== --
    -- Se nao tiver configuracao de pax por voo, verifica se tem   --
    -- por prefixo                                  --
    -- =========================================================== --
    IF V_TESTE = 0 THEN
    BEGIN
      /* Foi retirado dia 08/01/2003 pois foi verificado que existe um select
         igual a este no inicio da procedure e as variaveis utilizadas nele
         nao sao alteradas.
      SELECT VOO.CDEQUIP, VOO.SGPREFIXO
      INTO V_CDEQUIP, V_SGPREFIXO
      FROM VOO
      WHERE (CDFILIAL = P_CDFILIAL) AND
            (CDCIA    = P_CDCIA) AND
            (NRVOO    = P_NRVOO);*/

      SELECT COUNT(CDCLASSE)
      INTO   V_TESTE
      FROM   CONFIGPREF
      WHERE (CDFILIAL  = P_CDFILIAL ) AND
            (CDCIA     = P_CDCIA    ) AND
            (SGPREFIXO = V_SGPREFIXO);

      IF V_TESTE <> 0 THEN
      BEGIN
        IF P_SIMULACAO = '1' THEN
          UPDATE RELPREVPAX PPT
          SET   (QTTOPAXATPAX,QTPAXSBYPAX) = (SELECT CP.QTPAXPREFIXO, CP.QTPAXSBYPREF
                                              FROM   CONFIGPREF CP
                                              WHERE (CP.CDFILIAL     = P_CDFILIAL ) AND
                                                    (CP.CDCIA        = P_CDCIA    ) AND
                                                    (CP.SGPREFIXO    = V_SGPREFIXO) AND
                                                    (PPT.CDFILIAL    = CP.CDFILIAL) AND
                                                    (PPT.CDCIAPAX    = CP.CDCIA   ) AND
                                                    (PPT.CDCLASSEPAX = CP.CDCLASSE))
          WHERE (PPT.CDFILIAL     = P_CDFILIAL    ) AND
                (PPT.CDCIAPAX     = P_CDCIA       ) AND
                (PPT.NRVOOPAX     = P_NRVOO       ) AND
                (PPT.CDTRECHOPAX  = P_CDTRECHO    ) AND
                (PPT.DTRELPLANPAX = P_DTRELPLANPAX) AND
                (PPT.NRRELPLANPAX = P_NRRELPLANPAX) AND
                (PPT.CDCLASSEPAX IN (SELECT CDCLASSE
                                     FROM  CONFIGPREF
                                     WHERE (CDFILIAL  = P_CDFILIAL) AND
                                           (CDCIA     = P_CDCIA   ) AND
                                           (SGPREFIXO = V_SGPREFIXO)));
        ELSE
          IF P_SIMULACAO = '2' THEN
             UPDATE PREVPAXSRVTEMP PPT
             SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT, VRPARAMPREVPAX) =
                 (SELECT CP.QTPAXPREFIXO, CP.QTPAXSBYPREF, '2' IDTPPREVPPT, P_IDTPGERPPT, 0 VRPARAMPREVPAX
                  FROM   CONFIGPREF CP
                  WHERE (CP.CDFILIAL     = P_CDFILIAL ) AND
                        (CP.CDCIA        = P_CDCIA    ) AND
                        (CP.SGPREFIXO    = V_SGPREFIXO) AND
                        (PPT.CDFILIAL    = CP.CDFILIAL) AND
                        (PPT.CDCIAPPT    = CP.CDCIA)    AND
                        (PPT.CDCLASSEPPT = CP.CDCLASSE))
             WHERE (PPT.CDFILIAL    = P_CDFILIAL  ) AND
                   (PPT.CDCIAPPT    = P_CDCIA     ) AND
                   (PPT.NRVOOPPT    = P_NRVOO     ) AND
                   (PPT.CDTRECHOPPT = P_CDTRECHO  ) AND
                   (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                   (PPT.DTHRPLAPPT  = P_DTHRPLAVOOPV) AND
                   (PPT.CDCLASSEPPT IN (SELECT CDCLASSE FROM CONFIGPREF
                                        WHERE (CDFILIAL  = P_CDFILIAL) AND
                                              (CDCIA     = P_CDCIA   ) AND
                                              (SGPREFIXO = V_SGPREFIXO)));
          ELSE
             UPDATE PREVPAXTEMPM PPT
             SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT) =
                 (SELECT CP.QTPAXPREFIXO, CP.QTPAXSBYPREF, '2' IDTPPREVPPT, P_IDTPGERPPT
                  FROM   CONFIGPREF CP
                  WHERE (CP.CDFILIAL     = P_CDFILIAL ) AND
                        (CP.CDCIA        = P_CDCIA    ) AND
                        (CP.SGPREFIXO    = V_SGPREFIXO) AND
                        (PPT.CDFILIAL    = CP.CDFILIAL) AND
                        (PPT.CDCIAPPT    = CP.CDCIA)    AND
                        (PPT.CDCLASSEPPT = CP.CDCLASSE))
             WHERE (PPT.CDFILIAL    = P_CDFILIAL  ) AND
                   (PPT.CDCIAPPT    = P_CDCIA     ) AND
                   (PPT.NRVOOPPT    = P_NRVOO     ) AND
                   (PPT.CDTRECHOPPT = P_CDTRECHO  ) AND
                   (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                   (PPT.CDCLASSEPPT IN (SELECT CDCLASSE FROM CONFIGPREF
                                        WHERE (CDFILIAL  = P_CDFILIAL) AND
                                              (CDCIA     = P_CDCIA   ) AND
                                              (SGPREFIXO = V_SGPREFIXO)));
           END IF;
        END IF;
      END;
      END IF;

      -- =========================================================== --
      -- Se nao tiver configuracao de pax por voo, e nem por prefixo --
      -- verifica se tem por equipamento                             --
      -- =========================================================== --
      IF V_TESTE = 0 THEN
      BEGIN
        SELECT COUNT(CDCLASSE)
        INTO   V_TESTE
        FROM   CONFIGEQUI
        WHERE (CDFILIAL = P_CDFILIAL) AND
              (CDCIA    = P_CDCIA   ) AND
              (CDEQUIP  = V_CDEQUIP );

        IF V_TESTE <> 0 THEN
        BEGIN
          IF P_SIMULACAO = '1' THEN
            UPDATE RELPREVPAX PPT
            SET   (QTTOPAXATPAX,QTPAXSBYPAX) = (SELECT CE.QTPAXEQUI, CE.QTPAXSBYEQUI
                                                FROM   CONFIGEQUI CE
                                                WHERE (CE.CDFILIAL     = P_CDFILIAL ) AND
                                                      (CE.CDCIA        = P_CDCIA    ) AND
                                                      (CE.CDEQUIP      = V_CDEQUIP  ) AND
                                                      (PPT.CDFILIAL    = CE.CDFILIAL) AND
                                                      (PPT.CDCIAPAX    = CE.CDCIA   ) AND
                                                      (PPT.CDCLASSEPAX = CE.CDCLASSE))
            WHERE (PPT.CDFILIAL     = P_CDFILIAL    ) AND
                  (PPT.CDCIAPAX     = P_CDCIA       ) AND
                  (PPT.NRVOOPAX     = P_NRVOO       ) AND
                  (PPT.CDTRECHOPAX  = P_CDTRECHO    ) AND
                  (PPT.DTRELPLANPAX = P_DTRELPLANPAX) AND
                  (PPT.NRRELPLANPAX = P_NRRELPLANPAX) AND
                  (PPT.CDCLASSEPAX IN (SELECT CDCLASSE
                                       FROM CONFIGEQUI
                                       WHERE (CDFILIAL = P_CDFILIAL) AND
                                             (CDCIA    = P_CDCIA   ) AND
                                             (CDEQUIP  = V_CDEQUIP)));
          ELSE
            IF P_SIMULACAO = '2' THEN
               UPDATE PREVPAXSRVTEMP PPT
               SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT, VRPARAMPREVPAX) =
                   (SELECT CE.QTPAXEQUI, CE.QTPAXSBYEQUI, '2' IDTPPREVPPT, P_IDTPGERPPT, 0 VRPARAMPREVPAX
                    FROM CONFIGEQUI CE
                    WHERE (CE.CDFILIAL     = P_CDFILIAL ) AND
                          (CE.CDCIA        = P_CDCIA    ) AND
                          (CE.CDEQUIP      = V_CDEQUIP  ) AND
                          (PPT.CDFILIAL    = CE.CDFILIAL) AND
                          (PPT.CDCIAPPT    = CE.CDCIA   ) AND
                          (PPT.CDCLASSEPPT = CE.CDCLASSE))
               WHERE (PPT.CDFILIAL    = P_CDFILIAL) AND
                     (PPT.CDCIAPPT    = P_CDCIA   ) AND
                     (PPT.NRVOOPPT    = P_NRVOO   ) AND
                     (PPT.CDTRECHOPPT = P_CDTRECHO) AND
                     (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                     (PPT.DTHRPLAPPT  = P_DTHRPLAVOOPV) AND
                     (PPT.CDCLASSEPPT IN (SELECT CDCLASSE
                                          FROM   CONFIGEQUI
                                          WHERE (CDFILIAL = P_CDFILIAL) AND
                                                (CDCIA    = P_CDCIA   ) AND
                                                (CDEQUIP  = V_CDEQUIP)));
            ELSE
               UPDATE PREVPAXTEMPM PPT
               SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT) =
                   (SELECT CE.QTPAXEQUI, CE.QTPAXSBYEQUI, '2' IDTPPREVPPT, P_IDTPGERPPT
                    FROM CONFIGEQUI CE
                    WHERE (CE.CDFILIAL     = P_CDFILIAL ) AND
                          (CE.CDCIA        = P_CDCIA    ) AND
                          (CE.CDEQUIP      = V_CDEQUIP  ) AND
                          (PPT.CDFILIAL    = CE.CDFILIAL) AND
                          (PPT.CDCIAPPT    = CE.CDCIA   ) AND
                          (PPT.CDCLASSEPPT = CE.CDCLASSE))
               WHERE (PPT.CDFILIAL    = P_CDFILIAL) AND
                     (PPT.CDCIAPPT    = P_CDCIA   ) AND
                     (PPT.NRVOOPPT    = P_NRVOO   ) AND
                     (PPT.CDTRECHOPPT = P_CDTRECHO) AND
                     (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                     (PPT.CDCLASSEPPT IN (SELECT CDCLASSE
                                          FROM   CONFIGEQUI
                                          WHERE (CDFILIAL = P_CDFILIAL) AND
                                                (CDCIA    = P_CDCIA   ) AND
                                                (CDEQUIP  = V_CDEQUIP)));
            END IF;
          END IF;
        END;
        END IF;

        IF V_TESTE = 0 THEN
        BEGIN
--          raise_application_error( -20501, 'Nao existe configuracao maxima de passageiros para o voo '||P_NRVOO||', nem para seu prefixo e nem para o seu equipamento! Favor verificar na opcao "Menu Ambiente - Cadastros - Configuracao de Passageiros" e tentar novamente.', false);

          RAISE_APPLICATION_ERROR( -20501, 'APPERRMSGSTR' || 'APP-00017' || '$$' || P_NRVOO || 'APPERRMSGEND', TRUE);

          --Nao existe configuracao maxima de passageiros para o voo '||P_NRVOO||',
          --nem para seu prefixo e equipamento! Favor verificar na opcao
          --"Menu Ambiente - Cadastros - Configuracao de Passageiros" e tentar novamente.', true);

          /*IF P_SIMULACAO = '1' THEN
            UPDATE RELPREVPAX
            SET   QTTOPAXATPAX= 1,QTPAXSBYPAX  = 0
            WHERE (CDCIAPAX    = P_CDCIA)        AND
                  (NRVOOPAX    = P_NRVOO)        AND
                  (CDTRECHOPAX = P_CDTRECHO)     AND
                  (DTRELPLANPAX= P_DTRELPLANPAX) AND
                  (NRRELPLANPAX= P_NRRELPLANPAX);
          ELSE
            UPDATE PREVPAXTEMPM
            SET   QTTOTPAXATPPT= 1,QTPAXSBYPPT  = 0
            WHERE (CDCIAPPT    = P_CDCIA) AND
                  (NRVOOPPT    = P_NRVOO) AND
                  (CDTRECHOPPT = P_CDTRECHO);
          END IF;*/
        END;
        END IF;
      END;
      END IF;
    END;
    END IF;
  END;
  ELSIF P_TIPPREVISAO = '4' AND P_SIMULACAO <> '1' THEN
  -- ZERADA COM UPDATE 

  -- ERA UTILIZADO NA TROCA DE EQUIPAMENTO NA HORA DA GERACAO DO PLANO
  -- DE VOO, PARA ZERAR AS PREVISOES. HOJE NAO SE ZERA MAIS A PREVISAO
  -- QUANDO TROCAMOS O EQUIPAMENTO 

  BEGIN
    OPEN C_CONFIGVOO_ZERADA;
    LOOP
      FETCH C_CONFIGVOO_ZERADA
      INTO  V_CDCLASSE, V_IDCLASSE;
      EXIT WHEN C_CONFIGVOO_ZERADA%NOTFOUND;

      SOMATORIA_QTDE_PAX_STANDBYM(P_CDFILIAL, P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDCLASSE,
                                 V_QTTOTPAXAT_QTPAXSBY,V_QTTOTPAXAT,V_QTPAXSBY,V_CDEQUIP,V_SGPREFIXO);

      IF V_IDCLASSE IN ('2', '3') THEN
        V_QTTOTPAXAT:= 1;
      ELSE
        V_QTTOTPAXAT:= 0;
      END IF;

      IF P_SIMULACAO <> '2' THEN
         UPDATE PREVPAXTEMPM
         SET   QTTOTPAXATPPT = V_QTTOTPAXAT,
               QTPAXSBYPPT   = V_QTPAXSBY,
               IDTPGERPPT    = P_IDTPGERPPT,
               IDTPPREVPPT   = '3'
         WHERE CDFILIAL      = P_CDFILIAL AND
               CDCIAPPT      = P_CDCIA    AND
               NRVOOPPT      = P_NRVOO    AND
               CDTRECHOPPT   = P_CDTRECHO AND
               CDCLASSEPPT   = V_CDCLASSE;
      ELSE
         UPDATE PREVPAXSRVTEMP
         SET   QTTOTPAXATPPT = V_QTTOTPAXAT,
               QTPAXSBYPPT   = V_QTPAXSBY,
               IDTPGERPPT    = P_IDTPGERPPT,
               IDTPPREVPPT   = '3',               
               VRPARAMPREVPAX = 0
         WHERE CDFILIAL      = P_CDFILIAL AND
               CDCIAPPT      = P_CDCIA    AND
               NRVOOPPT      = P_NRVOO    AND
               CDTRECHOPPT   = P_CDTRECHO AND
               DTHRPLAPPT  = P_DTHRPLAVOOPV AND
               CDCLASSEPPT   = V_CDCLASSE;
      END IF;
      V_EXISTECLASSE:= '1';
    END LOOP;
    CLOSE C_CONFIGVOO_ZERADA;

    IF V_EXISTECLASSE <> '1' THEN
    BEGIN
      /* Foi retirado dia 08/01/2003 pois foi verificado que existe um select
         igual a este no inicio da procedure e as variaveis utilizadas nele
         nao sao alteradas.
      SELECT CDEQUIP, SGPREFIXO
      INTO V_CDEQUIP, V_SGPREFIXO
      FROM VOO
      WHERE (CDFILIAL = P_CDFILIAL) AND
            (CDCIA    = P_CDCIA   ) AND
            (NRVOO    = P_NRVOO   );*/

      OPEN C_CONFIGPREF_ZERADA;
      LOOP
        FETCH C_CONFIGPREF_ZERADA
        INTO  V_CDCLASSE, V_IDCLASSE;
        EXIT WHEN C_CONFIGPREF_ZERADA%NOTFOUND;

        SOMATORIA_QTDE_PAX_STANDBYM(P_CDFILIAL, P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDCLASSE,
                                   V_QTTOTPAXAT_QTPAXSBY,V_QTTOTPAXAT,V_QTPAXSBY,V_CDEQUIP,V_SGPREFIXO);

        IF V_IDCLASSE IN ('2', '3') THEN
          V_QTTOTPAXAT:= 1;
        ELSE
          V_QTTOTPAXAT:= 0;
        END IF;

        IF P_SIMULACAO <> '2' THEN
           UPDATE PREVPAXTEMPM
           SET QTTOTPAXATPPT = V_QTTOTPAXAT,
               QTPAXSBYPPT   = V_QTPAXSBY,
               IDTPGERPPT    = P_IDTPGERPPT,
               IDTPPREVPPT   = '3'
           WHERE  CDFILIAL    = P_CDFILIAL AND
                  CDCIAPPT    = P_CDCIA    AND
                  NRVOOPPT    = P_NRVOO    AND
                  CDTRECHOPPT = P_CDTRECHO AND
                  CDCLASSEPPT = V_CDCLASSE;
        ELSE
           UPDATE PREVPAXSRVTEMP
           SET QTTOTPAXATPPT = V_QTTOTPAXAT,
               QTPAXSBYPPT   = V_QTPAXSBY,
               IDTPGERPPT    = P_IDTPGERPPT,
               IDTPPREVPPT   = '3',
               VRPARAMPREVPAX = 0
           WHERE  CDFILIAL    = P_CDFILIAL AND
                  CDCIAPPT    = P_CDCIA    AND
                  NRVOOPPT    = P_NRVOO    AND
                  CDTRECHOPPT = P_CDTRECHO AND
                  DTHRPLAPPT  = P_DTHRPLAVOOPV AND
                  CDCLASSEPPT = V_CDCLASSE; 
        END IF;
        V_EXISTECLASSE:= '1';
      END LOOP;
      CLOSE C_CONFIGPREF_ZERADA;

      IF V_EXISTECLASSE <> '1' THEN
      BEGIN
        OPEN C_CONFIGEQUI_ZERADA;
        LOOP
          FETCH C_CONFIGEQUI_ZERADA
          INTO V_CDCLASSE, V_IDCLASSE;
          EXIT WHEN C_CONFIGEQUI_ZERADA%NOTFOUND;

          SOMATORIA_QTDE_PAX_STANDBYM(P_CDFILIAL, P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDCLASSE,
                                     V_QTTOTPAXAT_QTPAXSBY,V_QTTOTPAXAT,V_QTPAXSBY,V_CDEQUIP,V_SGPREFIXO);

          IF V_IDCLASSE IN ('2', '3') THEN
            V_QTTOTPAXAT:= 1;
          ELSE
            V_QTTOTPAXAT:= 0;
          END IF;

          IF P_SIMULACAO <> '2' THEN
             UPDATE PREVPAXTEMPM
             SET QTTOTPAXATPPT = V_QTTOTPAXAT,
                 QTPAXSBYPPT   = V_QTPAXSBY,
                 IDTPGERPPT    = P_IDTPGERPPT,
                 IDTPPREVPPT   = '3'
             WHERE CDFILIAL    = P_CDFILIAL AND
                   CDCIAPPT    = P_CDCIA    AND
                   NRVOOPPT    = P_NRVOO    AND
                   CDTRECHOPPT = P_CDTRECHO AND
                   CDCLASSEPPT = V_CDCLASSE;
          ELSE
             UPDATE PREVPAXSRVTEMP
             SET QTTOTPAXATPPT = V_QTTOTPAXAT,
                 QTPAXSBYPPT   = V_QTPAXSBY,
                 IDTPGERPPT    = P_IDTPGERPPT,
                 IDTPPREVPPT   = '3',
                 VRPARAMPREVPAX = 0
             WHERE CDFILIAL    = P_CDFILIAL AND
                   CDCIAPPT    = P_CDCIA    AND
                   NRVOOPPT    = P_NRVOO    AND
                   CDTRECHOPPT = P_CDTRECHO AND
                   DTHRPLAPPT  = P_DTHRPLAVOOPV AND
                   CDCLASSEPPT = V_CDCLASSE;
          END IF;
          V_EXISTECLASSE:= '1';
        END LOOP;
        CLOSE C_CONFIGEQUI_ZERADA;

        IF V_EXISTECLASSE <> '1' THEN
        BEGIN
          --raise_application_error( -20502, 'Nao existe configuracao de passageiros para o voo '||P_NRVOO||', nem para seu prefixo e nem para seu equipamento! Favor verificar na opcao "Menu Ambiente - Cadastros - Configuracao de Passageiros" e tentar novamente.', false);
          OPEN C_CLASSECIA_ZERADA;
          LOOP
            FETCH C_CLASSECIA_ZERADA
            INTO  V_CDCLASSE, V_IDCLASSE;
            EXIT WHEN C_CLASSECIA_ZERADA%NOTFOUND;

            IF V_IDCLASSE IN ('2', '3') THEN
              V_QTTOTPAXAT:= 1;
            ELSE
              V_QTTOTPAXAT:= 0;
            END IF;
            V_QTPAXSBY := 0;

            IF P_SIMULACAO <> '2' THEN 
               UPDATE PREVPAXTEMPM
               SET QTTOTPAXATPPT = V_QTTOTPAXAT,
                   QTPAXSBYPPT   = V_QTPAXSBY,
                   IDTPGERPPT    = P_IDTPGERPPT,
                   IDTPPREVPPT   = '3'
               WHERE  CDFILIAL    = P_CDFILIAL AND
                      CDCIAPPT    = P_CDCIA    AND
                      NRVOOPPT    = P_NRVOO    AND
                      CDTRECHOPPT = P_CDTRECHO AND
                      CDCLASSEPPT = V_CDCLASSE;
            ELSE
               UPDATE PREVPAXSRVTEMP
               SET QTTOTPAXATPPT = V_QTTOTPAXAT,
                   QTPAXSBYPPT   = V_QTPAXSBY,
                   IDTPGERPPT    = P_IDTPGERPPT,
                   IDTPPREVPPT   = '3',
                   VRPARAMPREVPAX = 0
               WHERE  CDFILIAL    = P_CDFILIAL AND
                      CDCIAPPT    = P_CDCIA    AND
                      NRVOOPPT    = P_NRVOO    AND
                      CDTRECHOPPT = P_CDTRECHO AND
                      DTHRPLAPPT  = P_DTHRPLAVOOPV AND
                      CDCLASSEPPT = V_CDCLASSE;
            END IF;            
          END LOOP;
          CLOSE C_CLASSECIA_ZERADA;
        END;
        END IF;
        -- END DO IF V_EXISTECLASSE <> 0
      END;
      END IF;
      -- END DO IF V_EXISTECLASSE <> 0
    END;
    END IF;
  END;
  -- 5 = MEDIA 
  ELSIF P_TIPPREVISAO = '5' THEN 
  BEGIN
  
     IF P_SIMULACAO <> '2' THEN 
        SELECT MIN(DTHRPLAVOOPV), MAX(DTHRPLAVOOPV)
        INTO V_DTINICIAL, V_DTFINAL
        FROM(SELECT NRVOO,CDTRECHO,DTHRPLAVOOPV
            FROM PLANOVOOM
            WHERE (CDFILIAL = P_CDFILIAL) AND
                  (CDCIA    = P_CDCIA   ) AND
                  (NRVOO    = P_NRVOO   ) AND
                  (CDTRECHO = P_CDTRECHO) AND
                  (DTHRPLAVOOPV >= (P_DTHRPLAVOOPV - P_NRDIAS)));

        SELECT COUNT(DTHRPLAVOOPV)
        INTO V_DIASMEDIA
        FROM(SELECT DTHRPLAVOOPV
            FROM PLANOVOOM
            WHERE (CDFILIAL = P_CDFILIAL) AND
                  (CDCIA    = P_CDCIA   ) AND
                  (NRVOO    = P_NRVOO   ) AND
                  (CDTRECHO = P_CDTRECHO) AND
                  (DTHRPLAVOOPV >= (P_DTHRPLAVOOPV - P_NRDIAS)));
     ELSE
        SELECT MIN(DTDECOLVOO), MAX(DTDECOLVOO)
        INTO V_DTINICIAL, V_DTFINAL
        FROM(SELECT NRVOO,CDTRECHO,DTDECOLVOO
            FROM VOOSPROGSERV
            WHERE (CDFILIAL = P_CDFILIAL) AND
                  (CDCIA    = P_CDCIA   ) AND
                  (NRVOO    = P_NRVOO   ) AND
                  (CDTRECHO = P_CDTRECHO) AND
                  (DTDECOLVOO >= (P_DTHRPLAVOOPV - P_NRDIAS)));

        SELECT COUNT(DTDECOLVOO)
        INTO V_DIASMEDIA
        FROM(SELECT DTDECOLVOO
            FROM VOOSPROGSERV
            WHERE (CDFILIAL = P_CDFILIAL) AND
                  (CDCIA    = P_CDCIA   ) AND
                  (NRVOO    = P_NRVOO   ) AND
                  (CDTRECHO = P_CDTRECHO) AND
                  (DTDECOLVOO >= (P_DTHRPLAVOOPV - P_NRDIAS)));     
     
     END IF; 

-- raise_application_error( -20502, 'FILIAL-> '||P_CDFILIAL||' CIA-> '||P_CDCIA||' VOO-> '||P_NRVOO||' TRECHO-> '||P_CDTRECHO||' '||V_DTINICIAL||' - '||V_DTFINAL||' - '||V_DIASMEDIA, false); 

     IF (P_SIMULACAO <> '1')AND(P_SIMULACAO <> '2') THEN
        UPDATE PREVPAXTEMPM PT
        SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT) =
            (SELECT NVL(ROUND(SUM(NVL(PP.QTTOTPAXAT,0))/V_DIASMEDIA),0),NVL(ROUND(SUM(NVL(PP.QTPAXSBY,0))/V_DIASMEDIA),0),'5',P_IDTPGERPPT
             FROM PREVISAOPAXM PP
             WHERE (PP.CDFILIAL = P_CDFILIAL) AND
                   (PP.CDCIA    = P_CDCIA   ) AND
                   (PP.NRVOO    = P_NRVOO   ) AND
                   (PP.CDTRECHO = P_CDTRECHO) AND
                   (PP.DTHRPLAVOOPV BETWEEN V_DTINICIAL AND V_DTFINAL) AND
                   (PP.CDCLASSE = PT.CDCLASSEPPT))
        WHERE (PT.CDFILIAL    = P_CDFILIAL  ) AND
              (PT.CDCIAPPT    = P_CDCIA     ) AND
              (PT.NRVOOPPT    = P_NRVOO     ) AND
              (PT.CDTRECHOPPT = P_CDTRECHO  ) AND  
              (PT.IDTPGERPPT  = P_IDTPGERPPT);
     ELSIF P_SIMULACAO = '2' THEN
        UPDATE PREVPAXSRVTEMP PT
        SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT, VRPARAMPREVPAX) =
            (SELECT NVL(ROUND(SUM(NVL(PP.QTTOTPAXAT,0))/V_DIASMEDIA),0),NVL(ROUND(SUM(NVL(PP.QTPAXSBY,0))/V_DIASMEDIA),0),'5',P_IDTPGERPPT, P_NRDIAS
             FROM PREVISAOPAXSRV PP
             WHERE (PP.CDFILIAL = P_CDFILIAL) AND
                   (PP.CDCIA    = P_CDCIA   ) AND
                   (PP.NRVOO    = P_NRVOO   ) AND
                   (PP.CDTRECHO = P_CDTRECHO) AND
                   (PP.DTDECOLVOO BETWEEN V_DTINICIAL AND V_DTFINAL) AND
                   (PP.CDCLASSE = PT.CDCLASSEPPT ))
        WHERE (PT.CDFILIAL    = P_CDFILIAL  ) AND
              (PT.CDCIAPPT    = P_CDCIA     ) AND
              (PT.NRVOOPPT    = P_NRVOO     ) AND
              (PT.CDTRECHOPPT = P_CDTRECHO  ) AND
              (PT.DTHRPLAPPT  = P_DTHRPLAVOOPV) AND
              (PT.IDTPGERPPT  = P_IDTPGERPPT);
    END IF;
  END;
          
  -- 6 =  percentual de ocupacao
  ELSIF P_TIPPREVISAO = '6'  THEN
  
  --***************************************************************
  --  Pega a quantidade maxima e aplica sobre ela um Percentual 
  --  de Ocupacao. Se nao houver percentual de ocupacao cadastrado, 
  --  ou nao houver quantidade maxima cadastrada, 
  --  pega o valor "0"  
  --***************************************************************
  
  
  BEGIN
   
    -- ============================================================ --
    --  GARANTIR QUE TODAS AS QUANTIDADES NAO ESTEJAM NULAS         --
    -- ============================================================ --
    UPDATE CONFIGVOO
    SET    QTPAXSBYVOO = 0
    WHERE (QTPAXSBYVOO IS NULL);

    UPDATE CONFIGPREF
    SET    QTPAXSBYPREF = 0
    WHERE (QTPAXSBYPREF IS NULL);

    UPDATE CONFIGEQUI
    SET    QTPAXSBYEQUI = 0
    WHERE (QTPAXSBYEQUI IS NULL);

    -- =========================================================== --
    -- Busca configuracao das classes primeiro na configvoo.       --
    -- =========================================================== --
    SELECT COUNT(CDCLASSE)
    INTO   V_TESTE
    FROM   CONFIGVOO
    WHERE (CDFILIAL = P_CDFILIAL) AND
          (CDCIA    = P_CDCIA   ) AND
          (NRVOO    = P_NRVOO   ) AND
          (CDTRECHO = P_CDTRECHO);

    IF V_TESTE <> 0 THEN
    BEGIN
      IF P_SIMULACAO = '1' THEN
        UPDATE RELPREVPAX PPT
        SET   (QTTOPAXATPAX,QTPAXSBYPAX) = (SELECT DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPAX),'1',(CV.QTPAXVOO * NVL(P_VRPEROCUPACAO,0))/100,CV.QTPAXVOO),
                                                   CV.QTPAXSBYVOO
                                            FROM   CONFIGVOO CV
                                            WHERE (CV.CDFILIAL     = P_CDFILIAL ) AND
                                                  (CV.CDCIA        = P_CDCIA    ) AND
                                                  (CV.NRVOO        = P_NRVOO    ) AND
                                                  (CV.CDTRECHO     = P_CDTRECHO ) AND
                                                  (PPT.CDFILIAL    = CV.CDFILIAL) AND
                                                  (PPT.CDCIAPAX    = CV.CDCIA   ) AND
                                                  (PPT.NRVOOPAX    = CV.NRVOO   ) AND
                                                  (PPT.CDTRECHOPAX = CV.CDTRECHO) AND
                                                  (PPT.CDCLASSEPAX = CV.CDCLASSE))
        WHERE (PPT.CDFILIAL    = P_CDFILIAL )    AND
              (PPT.CDCIAPAX    = P_CDCIA)        AND
              (PPT.NRVOOPAX    = P_NRVOO)        AND
              (PPT.CDTRECHOPAX = P_CDTRECHO)     AND
              (PPT.DTRELPLANPAX= P_DTRELPLANPAX) AND
              (PPT.NRRELPLANPAX= P_NRRELPLANPAX) AND
              (PPT.CDCLASSEPAX IN (SELECT CDCLASSE
                                   FROM CONFIGVOO
                                   WHERE (CDFILIAL = P_CDFILIAL) AND
                                         (CDCIA    = P_CDCIA   ) AND
                                         (NRVOO    = P_NRVOO   ) AND                                    
                                         (CDTRECHO = P_CDTRECHO)));
      ELSE 
        IF P_SIMULACAO = '2' THEN
           UPDATE PREVPAXSRVTEMP PPT
           SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT,VRPARAMPREVPAX) =
               (SELECT DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1',(CV.QTPAXVOO * NVL(P_VRPEROCUPACAO,0))/100,CV.QTPAXVOO),
               CV.QTPAXSBYVOO, DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1','6','2') IDTPPREVPPT, P_IDTPGERPPT,
               DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1',NVL(P_VRPEROCUPACAO,0),0)
                FROM CONFIGVOO CV
                WHERE (CV.CDFILIAL     = P_CDFILIAL ) AND
                      (CV.CDCIA        = P_CDCIA    ) AND
                      (CV.NRVOO        = P_NRVOO    ) AND
                      (CV.CDTRECHO     = P_CDTRECHO ) AND
                      (PPT.CDFILIAL    = CV.CDFILIAL) AND
                      (PPT.CDCIAPPT    = CV.CDCIA   ) AND
                      (PPT.NRVOOPPT    = CV.NRVOO   ) AND
                      (PPT.CDTRECHOPPT = CV.CDTRECHO) AND
                      (PPT.CDCLASSEPPT = CV.CDCLASSE))
           WHERE (PPT.CDFILIAL    = P_CDFILIAL  ) AND
                 (PPT.CDCIAPPT    = P_CDCIA     ) AND
                 (PPT.NRVOOPPT    = P_NRVOO     ) AND
                 (PPT.CDTRECHOPPT = P_CDTRECHO  ) AND
                 (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                 (PPT.DTHRPLAPPT  = P_DTHRPLAVOOPV) AND
                 (PPT.CDCLASSEPPT IN (SELECT CDCLASSE
                                      FROM CONFIGVOO
                                      WHERE (CDFILIAL     = P_CDFILIAL )     AND
                                            (CDCIA        = P_CDCIA)         AND
                                            (NRVOO        = P_NRVOO)         AND                                           
                                            (CDTRECHO     = P_CDTRECHO)));                
                                            
        ELSE
           UPDATE PREVPAXTEMPM PPT
           SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT) =
               (SELECT DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1',( CV.QTPAXVOO * NVL(P_VRPEROCUPACAO,0))/100,CV.QTPAXVOO),               
                CV.QTPAXSBYVOO, DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1','6','2') IDTPPREVPPT, P_IDTPGERPPT
                FROM CONFIGVOO CV
                WHERE (CV.CDFILIAL     = P_CDFILIAL ) AND
                      (CV.CDCIA        = P_CDCIA    ) AND
                      (CV.NRVOO        = P_NRVOO    ) AND
                      (CV.CDTRECHO     = P_CDTRECHO ) AND
                      (PPT.CDFILIAL    = CV.CDFILIAL) AND
                      (PPT.CDCIAPPT    = CV.CDCIA   ) AND
                      (PPT.NRVOOPPT    = CV.NRVOO   ) AND
                      (PPT.CDTRECHOPPT = CV.CDTRECHO) AND
                      (PPT.CDCLASSEPPT = CV.CDCLASSE))
           WHERE (PPT.CDFILIAL    = P_CDFILIAL  ) AND
                 (PPT.CDCIAPPT    = P_CDCIA     ) AND
                 (PPT.NRVOOPPT    = P_NRVOO     ) AND
                 (PPT.CDTRECHOPPT = P_CDTRECHO  ) AND
                 (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                 (PPT.CDCLASSEPPT IN (SELECT CDCLASSE
                                      FROM CONFIGVOO
                                      WHERE (CDFILIAL     = P_CDFILIAL )     AND
                                            (CDCIA        = P_CDCIA)         AND
                                            (NRVOO        = P_NRVOO)         AND
                                            (CDTRECHO     = P_CDTRECHO)));
        END IF;
      END IF;
    END;
    END IF;

    -- =========================================================== --
    -- Se nao tiver configuracao de pax por voo, verifica se tem   --
    -- por prefixo                                  --
    -- =========================================================== --
    IF V_TESTE = 0 THEN
    BEGIN
      /* Foi retirado dia 08/01/2003 pois foi verificado que existe um select
         igual a este no inicio da procedure e as variaveis utilizadas nele
         nao sao alteradas.
      SELECT VOO.CDEQUIP, VOO.SGPREFIXO
      INTO V_CDEQUIP, V_SGPREFIXO
      FROM VOO
      WHERE (CDFILIAL = P_CDFILIAL) AND
            (CDCIA    = P_CDCIA) AND
            (NRVOO    = P_NRVOO);*/

      SELECT COUNT(CDCLASSE)
      INTO   V_TESTE
      FROM   CONFIGPREF
      WHERE (CDFILIAL  = P_CDFILIAL ) AND
            (CDCIA     = P_CDCIA    ) AND
            (SGPREFIXO = V_SGPREFIXO);

      IF V_TESTE <> 0 THEN
      BEGIN
        IF P_SIMULACAO = '1' THEN
          UPDATE RELPREVPAX PPT
          SET   (QTTOPAXATPAX,QTPAXSBYPAX) = (SELECT DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPAX),'1',( CP.QTPAXPREFIXO * NVL(P_VRPEROCUPACAO,0))/100,CP.QTPAXPREFIXO),
                                                     CP.QTPAXSBYPREF
                                              FROM   CONFIGPREF CP
                                              WHERE (CP.CDFILIAL     = P_CDFILIAL ) AND
                                                    (CP.CDCIA        = P_CDCIA    ) AND
                                                    (CP.SGPREFIXO    = V_SGPREFIXO) AND
                                                    (PPT.CDFILIAL    = CP.CDFILIAL) AND
                                                    (PPT.CDCIAPAX    = CP.CDCIA   ) AND
                                                    (PPT.CDCLASSEPAX = CP.CDCLASSE))
          WHERE (PPT.CDFILIAL     = P_CDFILIAL    ) AND
                (PPT.CDCIAPAX     = P_CDCIA       ) AND
                (PPT.NRVOOPAX     = P_NRVOO       ) AND
                (PPT.CDTRECHOPAX  = P_CDTRECHO    ) AND
                (PPT.DTRELPLANPAX = P_DTRELPLANPAX) AND
                (PPT.NRRELPLANPAX = P_NRRELPLANPAX) AND
                (PPT.CDCLASSEPAX IN (SELECT CDCLASSE
                                     FROM  CONFIGPREF
                                     WHERE (CDFILIAL  = P_CDFILIAL) AND
                                           (CDCIA     = P_CDCIA   ) AND
                                           (SGPREFIXO = V_SGPREFIXO)));
        ELSE
          IF P_SIMULACAO = '2' THEN
             UPDATE PREVPAXSRVTEMP PPT
             SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT,VRPARAMPREVPAX) =
                 (SELECT  DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1',(CP.QTPAXPREFIXO * NVL(P_VRPEROCUPACAO,0))/100,CP.QTPAXPREFIXO),
                          CP.QTPAXSBYPREF, DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1','6','2') IDTPPREVPPT, P_IDTPGERPPT,
                          DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1',NVL(P_VRPEROCUPACAO,0),0)
                  FROM   CONFIGPREF CP
                  WHERE (CP.CDFILIAL     = P_CDFILIAL ) AND
                        (CP.CDCIA        = P_CDCIA    ) AND
                        (CP.SGPREFIXO    = V_SGPREFIXO) AND
                        (PPT.CDFILIAL    = CP.CDFILIAL) AND
                        (PPT.CDCIAPPT    = CP.CDCIA)    AND
                        (PPT.CDCLASSEPPT = CP.CDCLASSE))
             WHERE (PPT.CDFILIAL    = P_CDFILIAL  ) AND
                   (PPT.CDCIAPPT    = P_CDCIA     ) AND
                   (PPT.NRVOOPPT    = P_NRVOO     ) AND
                   (PPT.CDTRECHOPPT = P_CDTRECHO  ) AND
                   (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                   (PPT.DTHRPLAPPT  = P_DTHRPLAVOOPV) AND
                   (PPT.CDCLASSEPPT IN (SELECT CDCLASSE FROM CONFIGPREF
                                        WHERE (CDFILIAL  = P_CDFILIAL) AND
                                              (CDCIA     = P_CDCIA   ) AND
                                              (SGPREFIXO = V_SGPREFIXO)));
                                            
          ELSE
             UPDATE PREVPAXTEMPM PPT
             SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT) =
                 (SELECT DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1',(CP.QTPAXPREFIXO * NVL(P_VRPEROCUPACAO,0))/100,CP.QTPAXPREFIXO),
                         CP.QTPAXSBYPREF, DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1','6','2') IDTPPREVPPT, P_IDTPGERPPT
                  FROM   CONFIGPREF CP
                  WHERE (CP.CDFILIAL     = P_CDFILIAL ) AND
                        (CP.CDCIA        = P_CDCIA    ) AND
                        (CP.SGPREFIXO    = V_SGPREFIXO) AND
                        (PPT.CDFILIAL    = CP.CDFILIAL) AND
                        (PPT.CDCIAPPT    = CP.CDCIA)    AND
                        (PPT.CDCLASSEPPT = CP.CDCLASSE))
             WHERE (PPT.CDFILIAL    = P_CDFILIAL  ) AND
                   (PPT.CDCIAPPT    = P_CDCIA     ) AND
                   (PPT.NRVOOPPT    = P_NRVOO     ) AND
                   (PPT.CDTRECHOPPT = P_CDTRECHO  ) AND
                   (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                   (PPT.CDCLASSEPPT IN (SELECT CDCLASSE FROM CONFIGPREF
                                        WHERE (CDFILIAL  = P_CDFILIAL) AND
                                              (CDCIA     = P_CDCIA   ) AND
                                              (SGPREFIXO = V_SGPREFIXO)));
           END IF;
        END IF;
      END;
      END IF;

      -- =========================================================== --
      -- Se nao tiver configuracao de pax por voo, e nem por prefixo --
      -- verifica se tem por equipamento                             --
      -- =========================================================== --
      IF V_TESTE = 0 THEN
      BEGIN
        SELECT COUNT(CDCLASSE)
        INTO   V_TESTE
        FROM   CONFIGEQUI
        WHERE (CDFILIAL = P_CDFILIAL) AND
              (CDCIA    = P_CDCIA   ) AND
              (CDEQUIP  = V_CDEQUIP );

        IF V_TESTE <> 0 THEN
        BEGIN
          IF P_SIMULACAO = '1' THEN
            UPDATE RELPREVPAX PPT
            SET   (QTTOPAXATPAX,QTPAXSBYPAX) = (SELECT DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPAX),'1',(CE.QTPAXEQUI * NVL(P_VRPEROCUPACAO,0))/100,CE.QTPAXEQUI),
                                                        CE.QTPAXSBYEQUI
                                                FROM   CONFIGEQUI CE
                                                WHERE (CE.CDFILIAL     = P_CDFILIAL ) AND
                                                      (CE.CDCIA        = P_CDCIA    ) AND
                                                      (CE.CDEQUIP      = V_CDEQUIP  ) AND
                                                      (PPT.CDFILIAL    = CE.CDFILIAL) AND
                                                      (PPT.CDCIAPAX    = CE.CDCIA   ) AND
                                                      (PPT.CDCLASSEPAX = CE.CDCLASSE))
            WHERE (PPT.CDFILIAL     = P_CDFILIAL    ) AND
                  (PPT.CDCIAPAX     = P_CDCIA       ) AND
                  (PPT.NRVOOPAX     = P_NRVOO       ) AND
                  (PPT.CDTRECHOPAX  = P_CDTRECHO    ) AND
                  (PPT.DTRELPLANPAX = P_DTRELPLANPAX) AND
                  (PPT.NRRELPLANPAX = P_NRRELPLANPAX) AND
                  (PPT.CDCLASSEPAX IN (SELECT CDCLASSE
                                       FROM CONFIGEQUI
                                       WHERE (CDFILIAL = P_CDFILIAL) AND
                                             (CDCIA    = P_CDCIA   ) AND
                                             (CDEQUIP  = V_CDEQUIP)));
          ELSE
            IF P_SIMULACAO = '2' THEN
               UPDATE PREVPAXSRVTEMP PPT
               SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT,VRPARAMPREVPAX) =
                   (SELECT DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1',(CE.QTPAXEQUI * NVL(P_VRPEROCUPACAO,0))/100,CE.QTPAXEQUI), 
                           CE.QTPAXSBYEQUI, DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1','6','2') IDTPPREVPPT, P_IDTPGERPPT,
                           DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1',NVL(P_VRPEROCUPACAO,0),0)
                    FROM CONFIGEQUI CE
                    WHERE (CE.CDFILIAL     = P_CDFILIAL ) AND
                          (CE.CDCIA        = P_CDCIA    ) AND
                          (CE.CDEQUIP      = V_CDEQUIP  ) AND
                          (PPT.CDFILIAL    = CE.CDFILIAL) AND
                          (PPT.CDCIAPPT    = CE.CDCIA   ) AND
                          (PPT.CDCLASSEPPT = CE.CDCLASSE))
               WHERE (PPT.CDFILIAL    = P_CDFILIAL) AND
                     (PPT.CDCIAPPT    = P_CDCIA   ) AND
                     (PPT.NRVOOPPT    = P_NRVOO   ) AND
                     (PPT.CDTRECHOPPT = P_CDTRECHO) AND
                     (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                     (PPT.DTHRPLAPPT  = P_DTHRPLAVOOPV) AND
                     (PPT.CDCLASSEPPT IN (SELECT CDCLASSE
                                          FROM   CONFIGEQUI
                                          WHERE (CDFILIAL = P_CDFILIAL) AND
                                                (CDCIA    = P_CDCIA   ) AND
                                                (CDEQUIP  = V_CDEQUIP)));
            ELSE
               UPDATE PREVPAXTEMPM PPT
               SET (QTTOTPAXATPPT, QTPAXSBYPPT, IDTPPREVPPT, IDTPGERPPT) =
                   (SELECT DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1',(CE.QTPAXEQUI * NVL(P_VRPEROCUPACAO,0))/100,CE.QTPAXEQUI), 
                                   CE.QTPAXSBYEQUI, DECODE((SELECT IDCLASSE FROM CLASSE WHERE CDCLASSE = PPT.CDCLASSEPPT),'1','6','2') IDTPPREVPPT, P_IDTPGERPPT
                    FROM CONFIGEQUI CE
                    WHERE (CE.CDFILIAL     = P_CDFILIAL ) AND
                          (CE.CDCIA        = P_CDCIA    ) AND
                          (CE.CDEQUIP      = V_CDEQUIP  ) AND
                          (PPT.CDFILIAL    = CE.CDFILIAL) AND
                          (PPT.CDCIAPPT    = CE.CDCIA   ) AND
                          (PPT.CDCLASSEPPT = CE.CDCLASSE))
               WHERE (PPT.CDFILIAL    = P_CDFILIAL) AND
                     (PPT.CDCIAPPT    = P_CDCIA   ) AND
                     (PPT.NRVOOPPT    = P_NRVOO   ) AND
                     (PPT.CDTRECHOPPT = P_CDTRECHO) AND
                     (PPT.IDTPGERPPT  = P_IDTPGERPPT) AND
                     (PPT.CDCLASSEPPT IN (SELECT CDCLASSE
                                          FROM   CONFIGEQUI
                                          WHERE (CDFILIAL = P_CDFILIAL) AND
                                                (CDCIA    = P_CDCIA   ) AND
                                                (CDEQUIP  = V_CDEQUIP)));
            END IF;
          END IF;
        END;
        END IF;

        --  raise_application_error( -20501, 'APP-00017'||'*'||P_NRVOO||'*', true);

        --  Nao existe configuracao maxima de passageiros para o voo '||P_NRVOO||',
        --  nem para seu prefixo e equipamento! Favor verificar na opcao
        --  "Menu Ambiente - Cadastros - Configuracao de Passageiros" e tentar novamente.', true);

      END;
      END IF;
    END;
    END IF;
  END;
  -- END DO IF TPPREVISAO 
  END IF;  
END;
/