CREATE OR REPLACE PROCEDURE GERA_PLANO_VOO(
  P_CDOPERADOR     IN SERVPLANO.CDUSRRESPSP%TYPE,
  P_TPPREVISAO     IN PLANOVOO.IDTPPREVISAO%TYPE,
  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 PLANOVOO.DTHRPLAVOOPV%TYPE

 )AS
  V_CDCLASSE        CLASSE.CDCLASSE%TYPE;
  V_SGPREFIXO       PREFIXOCIA.SGPREFIXO%TYPE;
  V_CDCICLO_SE      CICLOCIA.CDCICLO%TYPE;
  V_RETORNA_CDCICLO CICLOCIA.CDCICLO%TYPE;
  V_CICLO_UNICO     CICLOCIA.CDCICLO%TYPE;
  V_IDSERVESP       SERVICOCIA.IDSERVESP%TYPE;
  V_CDSERVICO       SERVICOCIA.CDSERVICO%TYPE;
  V_NRPROPAXSE      PADVOOTRE.NRPROPAXVOO%TYPE;
  V_VRPELOADSE      PADVOOTRE.VRLOADSEVOO%TYPE;
  V_QTSERVSTBY      PADVOOTRE.QTSESTBYVOO%TYPE;
  V_QTSESBYPRSP     PADVOOTRE.QTSESTBYVOO%TYPE;
  V_QTSERVFIX       PADVOOTRE.QTSERVOO%TYPE;
  V_QTSERFATSP      SERVPLANO.QTSERFATSP%TYPE;
  V_VRPESOSE        SERVICOCIA.VRPESOSE%TYPE;
  V_QTTOTPAX        EQUIPCIA.QTTOTPAX%TYPE;
  V_PAXSBY          EQUIPCIA.QTTOTPAX%TYPE;
  V_CDEQUIP         EQUIPCIA.CDEQUIP%TYPE;
  V_IDABASATEN      VOO.IDABASATEN%TYPE;
  V_STABASVOO       VOO.IDABASATEN%TYPE;
  V_QTHRKDTCAR      VOO.QTHRKDTCAR%TYPE;
  V_QTHRKDTDES      VOO.QTHRKDTDES%TYPE;
  V_IDTIPAJUSFAT    VOOTRECHO.IDTIPAJUSFAT%TYPE;
  V_HRDECOLVOO      VOOTRECHO.HRDECOLVOO%TYPE;
  V_HRPOUSOVOO      VOOTRECHO.HRPOUSOVOO%TYPE;
  V_NRITEMPLANO     SERVPLANO.NRITEMPLANSP%TYPE;
  V_QTSESOLICSP     SERVPLANO.QTSESOLICSP%TYPE;
  V_QTSERVICO       PROPSERVICO.QTSERVICO%TYPE;
  V_VRPREDOLAR      PRECOSECIA.VRPRESERVD%TYPE;
  V_VRPRECORRENTE   PRECOSECIA.VRPRESERV%TYPE;
  V_VRPRECORR_SBY   PRECOSECIA.VRPRESERV%TYPE;
  V_VRPREDOLAR_SBY  PRECOSECIA.VRPRESERV%TYPE;
  V_VRPRUSBYSP      PRECOSECIA.VRPRESERV%TYPE;
  V_QTHRDIFGMT      PARAMCOMIS.QTHRDIFGMT%TYPE;
  V_IDBREAKGERA     PARAMGERAL.IDTPGERBREAK%TYPE;
  V_IDPROPSERV      PADVOOTRE.IDPROPSEVOO%TYPE;
  V_SGCLASSE        CLASSE.SGCLASSE%TYPE;
  V_DSSERVICO       SERVICOCIA.DSSERVICO%TYPE;
  V_SGCIA           COMPANHIA.SGCIA%TYPE;
  V_CDAGRDEL        AGRDELIVERY.CDAGRDEL%TYPE;
  V_IDTIPMANUAL     SERVICOCIA.IDTIPMANUAL%TYPE;
  V_IDTPPRECO       SERVICOCIA.IDTPPRECO%TYPE;
  V_IDFATSE         SERVICOCIA.IDFATSE%TYPE;
  V_NRDECITOTANF    PARAMGERAL.NRDECITOTANF%TYPE;  
  V_QTSESBYPRSP_INC SERVPLANO.QTSESBYPRSP%TYPE;
  V_EXISTECIA       NUMBER;
  V_EXISTE_EQUIP    NUMBER;
  V_TESTE           NUMBER;
  V_EXISTECICLO     NUMBER;
  V_DATAHORA        DATE;
  V_IDCALCPROPDLCOM PARAMCOMIS.IDCALCPROPDLCOM%TYPE;
  

  -- ===================================================================== --
  --                        DECLARACAO DE CURSOR                           --
  -- ===================================================================== --
  --   OS CURSORES C_CONFIGVOO, C_CONFIGPREF, C_CONFIGEQUI E C_CLASSECIA   --
  --   SAO UTILIZADOS PARA RECUPERAR A CLASSE, QTDE DE PASSAGEIROS E       --
  --   QTDE DE STANDBY, COM O OBJETIVO DE ATUALIZAR PREVISAOPAX, NA        --
  --   SEGUINTE ORDEM: PROCURAR EM CONFIGVOO SE NAO EXISTIR PESQUISAR EM   --
  --   CONFIGPREF CASO NAO ECONTRE PROCURAR EM CONFIGEQUI SE NAO ENCONTRAR --
  --   EM NENHUMA DAS TABELAS MENCIONADAS INSERIR PARA CADA CLASSE NA      --
  --   CLASSECIA PARA A COMPANHIA INFORMADA COM 0(ZERO) PARA PAX E STANDBY --
  -- ===================================================================== --
  CURSOR CUR_PAD_VOOPREFEQUI IS
   SELECT PA.CDSERVICO, PA.CDCLASSE   , PA.NRPROPAX,
          PA.VRLOADSE , PA.IDTIPMANUAL, PA.IDFATSE,
          PA.VRPESOSE , PA.QTSESTBY   , PA.QTSERV,
          PA.SGPREFIXO, PA.IDTPPRECO  , PA.CDEQUIP,
          PA.CDCICLO  , PA.IDPROPSERV , PA.DSSERVICO
   FROM   PAD_VOOPREFEQUI PA
   WHERE (PA.CDFILIAL     = P_CDFILIAL  ) AND
         (PA.CDCIA        = P_CDCIA     ) AND
         (PA.NRVOO        = P_NRVOO     ) AND
         (PA.CDTRECHO     = P_CDTRECHO  ) AND
         (PA.IDSITSERVICO = '1'         );

BEGIN
  -- ========================================================= --
  -- GARANTIR QUE TODAS AS QUANTIDADES NAO ESTEJAM NULAS       --
  -- ========================================================= --
  UPDATE PADVOOTRE
  SET    QTSESTBYVOO = 0
  WHERE (QTSESTBYVOO IS NULL);

  UPDATE PADPREFIXO
  SET    QTSESTBYPRE = 0
  WHERE (QTSESTBYPRE IS NULL);

  UPDATE PADEQUIP
  SET    QTSESTBYEQ = 0
  WHERE (QTSESTBYEQ IS NULL);

  UPDATE PADVOOTRE
  SET    QTSERVOO = 0
  WHERE (QTSERVOO IS NULL);

  UPDATE PADPREFIXO
  SET    QTSERPRE = 0
  WHERE (QTSERPRE IS NULL);

  UPDATE PADEQUIP
  SET    QTSERVEQ = 0
  WHERE (QTSERVEQ IS NULL);

  -- ========================================================= --
  -- TROCA O VOO NA TABELA DE VOO, PELO VOO INFORMADO NA HORA  --
  -- DA GERACAO DO PLANO, SE                                   --
  -- ========================================================= --
  SELECT COUNT(*)
  INTO   V_EXISTE_EQUIP
  FROM   PREVPAXTEMP PT, VOO VO
  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
         (ROWNUM          = 1             ) AND
         (PT.CDFILIAL     = VO.CDFILIAL   ) AND
         (PT.CDCIAPPT     = VO.CDCIA      ) AND
         (PT.NRVOOPPT     = VO.NRVOO      ) AND
         ((PT.CDEQUIPPPT   <> VO.CDEQUIP  ) OR
          (PT.SGPREFIXOPPT||'-' <> VO.SGPREFIXO||'-'));

  IF V_EXISTE_EQUIP > 0 THEN
    SELECT CDEQUIPPPT, SGPREFIXOPPT
    INTO   V_CDEQUIP,V_SGPREFIXO
    FROM   PREVPAXTEMP
    WHERE  (CDFILIAL    = P_CDFILIAL    ) AND
           (CDCIAPPT    = P_CDCIA       ) AND
           (NRVOOPPT    = P_NRVOO       ) AND
           (CDTRECHOPPT = P_CDTRECHO    ) AND
           (DTHRPLAPPT  = P_DTHRPLAVOOPV) AND
           (ROWNUM      = 1);

    ALTERA_PREFEQUI('1',P_CDFILIAL,P_CDCIA,P_NRVOO,V_CDEQUIP,V_SGPREFIXO);
  END IF;

  SELECT VO.CDEQUIP   , VO.SGPREFIXO , VO.QTHRKDTCAR  , VO.QTHRKDTDES,
         VT.HRDECOLVOO, VT.HRPOUSOVOO, VT.IDTIPAJUSFAT, VO.IDABASATEN
  INTO   V_CDEQUIP    , V_SGPREFIXO  , V_QTHRKDTCAR   , V_QTHRKDTDES,
         V_HRDECOLVOO , V_HRPOUSOVOO , V_IDTIPAJUSFAT , V_IDABASATEN
  FROM   VOOTRECHO VT, VOO VO
  WHERE (VT.CDFILIAL = P_CDFILIAL ) AND
        (VT.CDCIA    = P_CDCIA    ) AND
        (VT.NRVOO    = P_NRVOO    ) AND
        (VT.CDTRECHO = P_CDTRECHO ) AND
        (VO.CDFILIAL = VT.CDFILIAL) AND
        (VO.CDCIA    = VT.CDCIA   ) AND
        (VO.NRVOO    = VT.NRVOO   );


  V_STABASVOO := '1';

  IF (V_IDTIPAJUSFAT = '1') OR (V_IDTIPAJUSFAT IS NULL) THEN
    V_DATAHORA := P_DTHRPLAVOOPV;
  ELSIF (V_IDTIPAJUSFAT = '2') THEN
  BEGIN
    IF V_HRDECOLVOO IS NULL THEN
       V_DATAHORA:= P_DTHRPLAVOOPV;
    ELSE BEGIN
       SELECT NVL(MAX(QTHRDIFGMT),-3)
       INTO V_QTHRDIFGMT
       FROM PARAMCOMIS
       WHERE (CDFILIAL = P_CDFILIAL);

       IF (TO_NUMBER(SUBSTR(V_HRDECOLVOO,1,2))-V_QTHRDIFGMT) > 23 THEN
          V_DATAHORA:= P_DTHRPLAVOOPV+1;
       ELSIF (TO_NUMBER(SUBSTR(V_HRDECOLVOO,1,2))-V_QTHRDIFGMT) < 0 THEN
          V_DATAHORA:= P_DTHRPLAVOOPV-1;
       ELSE
          V_DATAHORA:= P_DTHRPLAVOOPV;
       END IF;
    END;
    END IF;
  END;
  END IF;

  -- ============================================================ --
  -- BUSCA  O CICLO UNICO DA COMPANHIA                            --
  -- ============================================================ --
  SELECT COUNT(*)
  INTO   V_EXISTECICLO
  FROM   CIAFILI
  WHERE CDFILIAL = P_CDFILIAL AND
        CDCIA    = P_CDCIA;
  IF V_EXISTECICLO <> 0 THEN
  BEGIN
    SELECT CDCICLO
    INTO   V_CICLO_UNICO
    FROM   CIAFILI
    WHERE  CDFILIAL = P_CDFILIAL AND
           CDCIA    = P_CDCIA;
  END;
  END IF;


  V_IDBREAKGERA := '1';

  -- ======================================================= --
  -- INSERCAO DA TABELA PLANOVOO                             --
  -- ======================================================= --
  INSERT INTO PLANOVOO
    (CDFILIAL,CDCIA, NRVOO, CDTRECHO, DTHRPLAVOOPV, CDEQUIP, SGPREFIXO,
    HRPREVDECPV, HRPREVPOUPV, QTHRKDTCARPV, QTHRKDTDESPV,
    DTHRABASTPV, IDSITABASPV, DTHRINCPV,
    CDUSRRESPPV, CDPOSPISTAPV, IDBREAKGERA, IDTPPREVISAO, IDREGERACAO)
  VALUES
    (P_CDFILIAL,P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV, V_CDEQUIP, V_SGPREFIXO,
    V_HRDECOLVOO, V_HRPOUSOVOO, V_QTHRKDTCAR, V_QTHRKDTDES,
    V_DATAHORA, V_STABASVOO, SYSDATE,
    P_CDOPERADOR, '', V_IDBREAKGERA, P_TPPREVISAO, '1');

  -- ==================================================== --
  -- INSERCAO DA TABELA PREVISAOPAX                       --
  -- ==================================================== --
  INSERT INTO PREVISAOPAX
    (CDFILIAL,CDCIA, NRVOO, DTHRPLAVOOPV, CDTRECHO, CDCLASSE,
    QTTOTPAXAN, QTTOTPAXAT, QTPAXSBY, CDUSRRESPPP)
  SELECT CDFILIAL, CDCIAPPT, NRVOOPPT, P_DTHRPLAVOOPV, CDTRECHOPPT, CDCLASSEPPT,
          0, QTTOTPAXATPPT, QTPAXSBYPPT, P_CDOPERADOR
  FROM   PREVPAXTEMP
  WHERE (CDFILIAL    = P_CDFILIAL    ) AND
        (CDCIAPPT    = P_CDCIA       ) AND
        (NRVOOPPT    = P_NRVOO       ) AND
        (CDTRECHOPPT = P_CDTRECHO    ) AND
        (DTHRPLAPPT  = P_DTHRPLAVOOPV) AND
        (IDTPGERPPT  = 'P'           ) ;

  UPDATE PREVISAOPAX
  SET    QTPAXSBY      = NULL
  WHERE (CDFILIAL      = P_CDFILIAL    ) AND
        (CDCIA         = P_CDCIA       ) AND
        (NRVOO         = P_NRVOO       ) AND
        (CDTRECHO      = P_CDTRECHO    ) AND
        (DTHRPLAVOOPV  = P_DTHRPLAVOOPV) AND
        (QTPAXSBY      = 0             );

  BEGIN
     SELECT NVL(NRDECITOTANF, 2)
     INTO V_NRDECITOTANF
     FROM PARAMGERAL
     WHERE
        (CDFILIAL = P_CDFILIAL);
  EXCEPTION
     WHEN No_Data_Found THEN
      V_NRDECITOTANF := NULL;
  END;              

  FOR CUR_PREVPAX IN
    (SELECT CDCLASSE
     FROM   PREVISAOPAX
     WHERE (CDFILIAL      = P_CDFILIAL) AND
           (CDCIA         = P_CDCIA   ) AND
           (NRVOO         = P_NRVOO   ) AND
           (CDTRECHO      = P_CDTRECHO) AND
           (DTHRPLAVOOPV  = P_DTHRPLAVOOPV)) LOOP

    -- ========================================================= --
    -- BUSCAR O CICLO VIGENTE NESTA DATA                         --
    -- ========================================================= --
    RETORNA_CICLO(P_CDFILIAL,P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV,
                  '',CUR_PREVPAX.CDCLASSE,V_RETORNA_CDCICLO);

    UPDATE PREVISAOPAX
    SET    CDCICLO = V_RETORNA_CDCICLO
    WHERE (CDFILIAL      = P_CDFILIAL    ) AND
          (CDCIA         = P_CDCIA       ) AND
          (NRVOO         = P_NRVOO       ) AND
          (CDTRECHO      = P_CDTRECHO    ) AND
          (DTHRPLAVOOPV  = P_DTHRPLAVOOPV) AND
          (CDCLASSE      = CUR_PREVPAX.CDCLASSE);

  END LOOP;

  /* LOOP NA TABELA DE SERVICOS DO VOO */
  OPEN CUR_PAD_VOOPREFEQUI;
  LOOP
    FETCH
      CUR_PAD_VOOPREFEQUI
    INTO
      V_CDSERVICO , V_CDCLASSE   , V_NRPROPAXSE,
      V_VRPELOADSE, V_IDTIPMANUAL, V_IDFATSE,
      V_VRPESOSE  , V_QTSERVSTBY , V_QTSERVFIX,
      V_SGPREFIXO , V_IDTPPRECO  , V_CDEQUIP,
      V_CDCICLO_SE, V_IDPROPSERV , V_DSSERVICO;
    EXIT WHEN CUR_PAD_VOOPREFEQUI%NOTFOUND;

    -- ============================================================ --
    -- GARANTIR QUE TODAS AS QUANTIDADES, LOADINGS E                --
    -- PROPORCAO NAO ESTEJAM NULAS                                  --
    -- ============================================================ --
    IF (V_VRPESOSE   IS NULL) THEN
      V_VRPESOSE  := 0;
    END IF;
    IF (V_QTSERVSTBY IS NULL) THEN
      V_QTSERVSTBY:= 0;
    END IF;

    V_QTSESBYPRSP_INC := 0;

    -- ============================================================ --
    -- BUSCAR O CICLO VIGENTE NESTA DATA                            --
    -- ============================================================ --
    V_RETORNA_CDCICLO := V_CDCICLO_SE;
    RETORNA_CICLO(P_CDFILIAL,P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV,
                  V_CDSERVICO,V_CDCLASSE,V_RETORNA_CDCICLO);

    IF (V_CDCICLO_SE = V_RETORNA_CDCICLO) OR (V_CDCICLO_SE = V_CICLO_UNICO) THEN
      SELECT COUNT(*)
      INTO   V_TESTE
      FROM   PREVISAOPAX
      WHERE (CDFILIAL     = P_CDFILIAL) AND
            (CDCIA        = P_CDCIA   ) AND
            (NRVOO        = P_NRVOO   ) AND
            (CDTRECHO     = P_CDTRECHO) AND
            (CDCLASSE     = V_CDCLASSE) AND
            (DTHRPLAVOOPV = P_DTHRPLAVOOPV);

      IF (V_TESTE <> 0) THEN   /* SE A CLASSE DO SERVICO EXISTE NA PREVISAO DE PAX */
        RETORNA_AGRUPAMENTO(P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDCLASSE,
                            V_CDEQUIP,V_SGPREFIXO,V_CDAGRDEL);

        SELECT NVL(MAX(QTTOTPAXAT), 0), NVL(MAX(QTPAXSBY), 0)
        INTO   V_QTTOTPAX, V_PAXSBY
        FROM   PREVISAOPAX
        WHERE  (CDFILIAL     = P_CDFILIAL) AND
               (CDCIA        = P_CDCIA   ) AND
               (NRVOO        = P_NRVOO   ) AND
               (CDTRECHO     = P_CDTRECHO) AND
               (CDCLASSE     = V_CDCLASSE) AND
               (DTHRPLAVOOPV = P_DTHRPLAVOOPV);

        -- ============================================================ --
        -- UTILIZO O STANDBY INFORMADO NA GERACAO DO PLANO DE VOO, SE   --
        -- ESTE NAO FOR INFORMADO , PEGO O STANDBY DA VIEW              --
        -- ============================================================ --
        IF (V_PAXSBY <> 0) AND (V_PAXSBY IS NOT NULL) THEN
           V_QTSESBYPRSP := V_PAXSBY;
        ELSE
           V_QTSESBYPRSP := V_QTSERVSTBY;
        END IF;

        -- =========================================================== --
        -- Calculo das quantidades                                     --
        -- =========================================================== --
        CALCULA_QUANTIDADES(P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDSERVICO,
                            V_CDCLASSE,V_CDCICLO_SE,V_QTTOTPAX,
                            V_IDPROPSERV,V_VRPELOADSE,V_NRPROPAXSE,
                            V_QTSERVFIX, V_QTSESOLICSP, V_QTSESBYPRSP);


        IF V_PAXSBY <> 0 THEN
           V_QTSESBYPRSP := V_PAXSBY;

           IF (V_IDPROPSERV = '2') THEN
              V_QTSESBYPRSP_INC := CEIL((V_QTSESOLICSP)/V_NRPROPAXSE);
           ELSE
              V_QTSESBYPRSP_INC := 0;
           END IF;

           IF (V_IDPROPSERV = '1') THEN
              V_QTSESBYPRSP_INC := V_QTSESBYPRSP;
           END IF;
        END IF;

        IF (V_QTSESBYPRSP_INC IS NULL) THEN
           V_QTSESBYPRSP_INC := 0;
        END IF;


        -- =========================================================== --
        -- TESTA O TIPO DE PRECO (1 - UNITARIO OU 2 - TOTAL PARA       --
        -- CALCULAR A QUANTIDADE A FATURAR                             --
        -- =========================================================== --
        V_QTSERFATSP := 0;

        IF (V_IDTPPRECO = '2') and     /* PRECO TOTAL */
           ((V_QTSERVFIX+V_QTSESOLICSP) > 0) THEN
           V_QTSERFATSP := 1;
        ELSE
           V_QTSERFATSP := V_QTSERVFIX + V_QTSESOLICSP;
        END IF;

        /*PARAMETROS*/
        SELECT IDCALCPROPDLCOM
          INTO V_IDCALCPROPDLCOM
          FROM PARAMCOMIS
         WHERE (CDFILIAL = P_CDFILIAL);

        IF V_IDCALCPROPDLCOM = 'S' THEN
            -- =========================================================== --
            -- Calculo dos precos do servico                               --
            -- =========================================================== --
            CALCULA_PRECO('N',P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDSERVICO,
                          P_DTHRPLAVOOPV,V_DATAHORA, V_QTTOTPAX+V_QTSESBYPRSP,(V_QTSERVFIX + V_QTSESOLICSP)+V_QTSESBYPRSP,NULL,
                          V_VRPRECORRENTE,V_VRPREDOLAR);

            -- =========================================================== --
            -- Calculo do preco do stand-by                                --
            -- =========================================================== --
            IF (V_QTSESBYPRSP > 0) THEN
              CALCULA_PRECO('N',P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDSERVICO,
                            P_DTHRPLAVOOPV,V_DATAHORA, V_QTTOTPAX+V_QTSESBYPRSP,(V_QTSERVFIX + V_QTSESOLICSP)+V_QTSESBYPRSP,NULL,
                            V_VRPRECORR_SBY,V_VRPREDOLAR_SBY);

              IF V_IDTIPMANUAL = '1' THEN
                V_VRPRUSBYSP := V_VRPRECORR_SBY;
              ELSE
                V_VRPRUSBYSP := V_VRPREDOLAR_SBY;
              END IF;
            ELSE
              V_VRPRUSBYSP := 0;
            END IF;
        ELSE
            -- =========================================================== --
            -- Calculo dos precos do servico                               --
            -- =========================================================== --
            CALCULA_PRECO('N',P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDSERVICO,
                          P_DTHRPLAVOOPV,V_DATAHORA, V_QTTOTPAX,(V_QTSERVFIX + V_QTSESOLICSP),NULL,
                          V_VRPRECORRENTE,V_VRPREDOLAR);

            -- =========================================================== --
            -- Calculo do preco do stand-by                                --
            -- =========================================================== --
            IF (V_QTSESBYPRSP > 0) THEN
              CALCULA_PRECO('N',P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDSERVICO,
                            P_DTHRPLAVOOPV,V_DATAHORA, V_QTTOTPAX,V_QTSERFATSP,NULL,
                            V_VRPRECORR_SBY,V_VRPREDOLAR_SBY);

              IF V_IDTIPMANUAL = '1' THEN
                V_VRPRUSBYSP := V_VRPRECORR_SBY;
              ELSE
                V_VRPRUSBYSP := V_VRPREDOLAR_SBY;
              END IF;
            ELSE
              V_VRPRUSBYSP := 0;
            END IF;
        END IF;

        -- =========================================================== --
        -- GRAVA NA TABELA  SERVPLANO/RELPLANOVOO                      --
        -- =========================================================== --
        SELECT MAX(NRITEMPLANSP) INTO V_NRITEMPLANO
        FROM   SERVPLANO
        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_QTSERVFIX+V_QTSESOLICSP+V_QTSESBYPRSP) > 0 THEN
           IF (V_NRDECITOTANF = 0) THEN
              INSERT INTO SERVPLANO
                (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  , CDSERVICO    , VRPRUDOLSESP, VRPRURESESP,
                IDTIPSCBOSP , VRLOADSESP   , NRPROPAXSP  ,
                VRPRUSBYSP  , IDTIPMANUALSP, IDTPMANUTSP , IDTPPRECOSP, IDFATSESP)
              VALUES
                (P_CDFILIAL,P_CDCIA, V_NRITEMPLANO, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV,
                NULL, '1', '1', P_CDOPERADOR, V_QTSESOLICSP, 0,
                V_QTSESBYPRSP_INC, 0, 0, V_QTSERVFIX,
                (V_QTSERVFIX+V_QTSESBYPRSP+V_QTSESOLICSP),V_QTSERFATSP,
                (V_QTSERVFIX+V_QTSESBYPRSP+V_QTSESOLICSP),NULL, V_VRPESOSE, SYSDATE,
                NULL, NULL,  V_CDAGRDEL, V_CDCLASSE,
                '1',  V_CDSERVICO,  V_VRPREDOLAR, ROUND(V_VRPRECORRENTE, 0),
                V_IDPROPSERV, V_VRPELOADSE, V_NRPROPAXSE,
                V_VRPRUSBYSP,V_IDTIPMANUAL, '1A',V_IDTPPRECO,V_IDFATSE);
           ELSE
               INSERT INTO SERVPLANO
                 (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  , CDSERVICO    , VRPRUDOLSESP, VRPRURESESP,
                 IDTIPSCBOSP , VRLOADSESP   , NRPROPAXSP  ,
                 VRPRUSBYSP  , IDTIPMANUALSP, IDTPMANUTSP , IDTPPRECOSP, IDFATSESP)
               VALUES
                 (P_CDFILIAL,P_CDCIA, V_NRITEMPLANO, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV,
                 NULL, '1', '1', P_CDOPERADOR, V_QTSESOLICSP, 0,
                 V_QTSESBYPRSP_INC, 0, 0, V_QTSERVFIX,
                 (V_QTSERVFIX+V_QTSESBYPRSP+V_QTSESOLICSP),V_QTSERFATSP,
                 (V_QTSERVFIX+V_QTSESBYPRSP+V_QTSESOLICSP),NULL, V_VRPESOSE, SYSDATE,
                 NULL, NULL,  V_CDAGRDEL, V_CDCLASSE,
                 '1',  V_CDSERVICO, V_VRPREDOLAR, V_VRPRECORRENTE,
                 V_IDPROPSERV, V_VRPELOADSE, V_NRPROPAXSE,
                 V_VRPRUSBYSP,V_IDTIPMANUAL, '1A',V_IDTPPRECO,V_IDFATSE);
           END IF;
         END IF;
      END IF; /* END DA CLASSE */
    END IF;  /* ENDIF DO CICLO */
  END LOOP;

  -- =========================================================== --
  -- VOLTA O EQUIPAMENTO E PREFIXO DO VOO                        --
  -- =========================================================== --
  IF V_EXISTE_EQUIP > 0 THEN
    ALTERA_PREFEQUI('2',P_CDFILIAL,P_CDCIA,P_NRVOO,NULL,NULL);
  END IF;

  CLOSE CUR_PAD_VOOPREFEQUI;
  SELECT MAX(DTHRULTGERA)
  INTO   V_DATAHORA
  FROM   PARAMCOMIS
  WHERE  CDFILIAL = P_CDFILIAL;
  IF (P_DTHRPLAVOOPV > V_DATAHORA) OR (V_DATAHORA IS NULL) THEN
     UPDATE PARAMCOMIS
     SET    DTHRULTGERA = P_DTHRPLAVOOPV
     WHERE  CDFILIAL = P_CDFILIAL;
  END IF;
END;
/