CREATE OR REPLACE PROCEDURE ALTERA_QTDE_PAX(
  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,
  P_CDCLASSE     IN CLASSE.CDCLASSE%TYPE,
  P_QTTOTPAX     IN EQUIPCIA.QTTOTPAX%TYPE,
  P_QTPAXSBY_CLA IN EQUIPCIA.QTTOTPAX%TYPE,
  P_CDOPERADOR   IN SERVPLANO.CDUSRRESPSP%TYPE,
  P_TIPOOPER     IN CHAR DEFAULT '1' --> 2 - So atualiza a situacao do plano de voo.
) AS
  -- ============================================================= --
  -- VARIAVEL UTILIZADA PARA TESTAR SE EXISTE DADOS NAS TABELAS    --
  -- ============================================================= --

  V_CDSERVICO           SERVICOCIA.CDSERVICO%TYPE;
  V_QTSESBYPRSP         SERVPLANO.QTSESBYPRSP%TYPE;
  V_QTSESOLICSP         SERVPLANO.QTSESOLICSP%TYPE;
  V_NRPROPAXSP          SERVPLANO.NRPROPAXSP%TYPE;
  V_VRPELOADSP          SERVPLANO.VRLOADSESP%TYPE;
  V_NRITEMPLANSP        SERVPLANO.NRITEMPLANSP%TYPE;
  V_VRPREDOLAR          PRECOSECIA.VRPRESERVD%TYPE;
  V_VRPRECORRENTE       PRECOSECIA.VRPRESERV%TYPE;
  V_NRITEMPLANO         SERVPLANO.NRITEMPLANSP%TYPE;
  V_VRPESOSE            SERVICOCIA.VRPESOSE%TYPE;
  V_CDAGRDECO           CONFIGVOO.CDAGRDEL%TYPE;
  V_CDAGRDEDE           CLASSE.CDCLASSE%TYPE;
  V_CDEQUIP             PLANOVOO.CDEQUIP%TYPE;
  V_SGPREFIXO           PLANOVOO.SGPREFIXO%TYPE;
  V_IDTIPMANUAL         SERVICOCIA.IDTIPMANUAL%TYPE;
  V_IDTPPRECO           SERVICOCIA.IDTPPRECO%TYPE;
  V_VRPRECORR_SBY       SERVPLANO.VRPRUSBYSP%TYPE;
  V_VRPREDOLAR_SBY      SERVPLANO.VRPRUSBYSP%TYPE;
  V_VRPRUSBYSP          SERVPLANO.VRPRUSBYSP%TYPE;
  V_QTSESOLICSP_INC     SERVPLANO.QTSESOLICSP%TYPE;
  V_QTSESBYPRSP_INC     SERVPLANO.QTSESBYPRSP%TYPE;
  V_QTSERFATSP_INC      SERVPLANO.QTSERFATSP%TYPE;
  V_QTSESOLICSP_AUX     SERVPLANO.QTSESOLICSP%TYPE;
  V_QTSERVFIXSP_AUX     SERVPLANO.QTSERVFIXSP%TYPE;
  V_QTSESBYPRSP_AUX     SERVPLANO.QTSESBYPRSP%TYPE;
  V_QTSESOLICSP_ANT     SERVPLANO.QTSESOLICSP%TYPE;
  V_QTSERVFIXSP_ANT     SERVPLANO.QTSERVFIXSP%TYPE;
  V_QTSESBYPRSP_ANT     SERVPLANO.QTSESBYPRSP%TYPE;
  V_QTSESOLICSP_FINAL   SERVPLANO.QTSESOLICSP%TYPE;
  V_QTSESBYPRSP_FINAL   SERVPLANO.QTSESBYPRSP%TYPE;
  V_QTESPECIAL          SERVPLANO.QTSERVFIXSP%TYPE;
  V_CDCLASSE            CLASSE.CDCLASSE%TYPE;
  V_DTHRABASTPV         PLANOVOO.DTHRABASTPV%TYPE;
  V_CDCICLO             CICLOCIA.CDCICLO%TYPE;
  V_IDFATSESP           SERVICOCIA.IDFATSE%TYPE;
  V_IDTPPRECOSP         SERVICOCIA.IDTPPRECO%TYPE;
  V_EXISTE_PENDENTE     NUMBER;
  V_INCLUI_SERVICO      BOOLEAN;
  V_QTREAL         FLOAT;
  V_IDCALCPROPDLCOM PARAMCOMIS.IDCALCPROPDLCOM%TYPE;
  V_QTEMITIDA           NUMBER;

  -- ======================================================== --
  -- VARIAVEL UTILIZADA NA GERA_CLASSE_VOO                    --
  -- ======================================================== --
  V_IDPROPSERV          PADVOOTRE.IDPROPSEVOO%TYPE;
  V_PENDENTE            NUMBER;
  V_SITUACAO_VOO        PLANOVOO.IDSITABASPV%TYPE;
  V_SITUACAO            PLANOVOO.IDSITABASPV%TYPE;
  V_QTSTBYANTE          NUMBER;

  -- ================================================================ --
  --                      DECLARACAO DE CURSOR 1                      --
  -- ================================================================ --
  --   SERVICOS DA SERVPLANO - ALTERACAO NA QTDE PAX E QTDE.SERVICOS  --
  -- ================================================================ --
  CURSOR C_SERVPLANO IS
  SELECT DISTINCT SP.CDSERVICO, MAX(SP.IDTIPSCBOSP),
         MAX(SP.NRPROPAXSP)   , MAX(SP.VRLOADSESP) ,
         MAX(SP.IDTIPMANUALSP), MAX(SP.IDTPPRECOSP),
         MAX(PR.CDCLASSE)     , MAX(PV.CDEQUIP)    ,
         MAX(PV.SGPREFIXO)    , SUM(SP.QTSESOLICSP),
         SUM(SP.QTSESBYPRSP)  , SUM(SP.QTSERVFIXSP),
         MAX(SP.VRPESOSESP)   , PV.DTHRABASTPV     ,
         /*MAX(SP.CDCLASSE)     , MAX(SP.CDCICLO)    ,*/
         MAX(IDFATSESP)       , MAX(IDTPPRECOSP)
  FROM   SERVPLANO SP, PLANOVOO PV, PRODUTO PR
  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
        (PR.CDCLASSE     = P_CDCLASSE          ) AND
        (SP.IDSITSERSP  <> '3'                 ) AND
        (SP.IDTIPSCBOSP IN ('1', '2', '3', '4')) AND
        (PR.CDPRODUTO    = SP.CDSERVICO        )
  GROUP BY SP.CDCIA,SP.NRVOO,SP.CDTRECHO,SP.DTHRPLAVOOPV,
           SP.CDSERVICO,PV.DTHRABASTPV;

BEGIN
   V_QTEMITIDA := 0;

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

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

   IF V_SITUACAO = '3' THEN /* Se o voo estiver cancelado */
     	raise_application_error( -20502, 'Man_N_Perm', false);
      /*Manutecao nao permitida. O voo esta cancelado.*/

   END IF;

   /* ALTERACAO NA PREV.PAX OU NA QTDE SERVICOS */
   IF P_TIPOOPER <> '2' THEN
      OPEN C_SERVPLANO;
      LOOP
        	FETCH C_SERVPLANO INTO
           V_CDSERVICO      , 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_CDCLASSE       ,V_CDCICLO     ,*/
           V_IDFATSESP  , V_IDTPPRECOSP;

        	EXIT WHEN C_SERVPLANO%NOTFOUND;

        	RETORNA_AGRUPAMENTO(P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDAGRDEDE,
                             V_CDEQUIP,V_SGPREFIXO,V_CDAGRDECO);

         -- =================================================== --
         -- Calculo das quantidades                             --
         -- Salva valores da servplano antes de chamar a calc.  --
         -- quantidades                                         --
         -- =================================================== --

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

--DBMS_OUTPUT.PUT_LINE(V_EXISTE_PENDENTE||' - '||V_CDSERVICO||' - '||V_CDCLASSE||' - '||V_CDCICLO);
        	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  SERVPLANO
         	WHERE (CDFILIAL     = P_CDFILIAL    ) AND
                  (CDCIA        = P_CDCIA       ) AND
                  (NRVOO        = P_NRVOO       ) AND
                  (CDTRECHO     = P_CDTRECHO    ) AND
                  (DTHRPLAVOOPV = P_DTHRPLAVOOPV) AND
                  (CDSERVICO    = V_CDSERVICO   ) AND
                  /*(CDCLASSE     = V_CDCLASSE    ) AND
                  (CDCICLO      = V_CDCICLO     ) AND*/
                  (IDSITSERSP   = '1'           )
            GROUP BY CDCIA,NRVOO,CDTRECHO,DTHRPLAVOOPV,CDSERVICO;
            V_INCLUI_SERVICO  := FALSE;
        	ELSE
          	V_INCLUI_SERVICO  := TRUE;
          	V_QTSESOLICSP_ANT := 0;
          	V_QTSESBYPRSP_ANT := 0;
          	V_QTSERVFIXSP_ANT := 0;
        	END IF;

         /* CALCULA QUANTIDADE DE SERVICO ESPECIAL */
        	SELECT NVL(SUM(QTSERFATSP),0)
         INTO   V_QTESPECIAL
         FROM   SERVPLANO SP, SERVICOCIA SC
         WHERE (SP.CDFILIAL     = P_CDFILIAL    ) AND
               (SP.CDCIA        = P_CDCIA       ) AND
               (SP.NRVOO        = P_NRVOO       ) AND
               (SP.CDTRECHO     = P_CDTRECHO    ) AND
               (SP.DTHRPLAVOOPV = P_DTHRPLAVOOPV) AND
               (SP.CDSERVICO    = SC.CDSERVICO  ) AND
               (SC.IDSERVESP    = '2'           )/* AND
               (SP.CDCLASSE     = P_CDCLASSE    )*/;


        	IF P_QTPAXSBY_CLA IS NOT NULL THEN
                   V_QTSESBYPRSP := P_QTPAXSBY_CLA;
        	ELSE
                   V_QTSESBYPRSP := V_QTSESBYPRSP_AUX;
        	END IF;



--DBMS_OUTPUT.PUT_LINE(V_VRPELOADSP||' - '||V_NRPROPAXSP||' - '||V_QTSERVFIXSP_AUX||' - '||V_QTSESOLICSP);

        	CALCULA_QUANTIDADES(P_CDFILIAL  , P_CDCIA  , P_NRVOO   ,P_CDTRECHO  ,V_CDSERVICO,
                             P_CDCLASSE  , V_CDCICLO, P_QTTOTPAX,V_IDPROPSERV,V_VRPELOADSP,
                             V_NRPROPAXSP, V_QTSERVFIXSP_AUX,V_QTSESOLICSP, V_QTSESBYPRSP);

--DBMS_OUTPUT.PUT_LINE(V_VRPELOADSP||' - '||V_NRPROPAXSP||' - '||V_QTSERVFIXSP_AUX||' - '||V_QTSESOLICSP);

            /*	IF P_QTPAXSBY_CLA IS NOT NULL THEN
          	V_QTSESBYPRSP := P_QTPAXSBY_CLA;
        	ELSE
          	V_QTSESBYPRSP := V_QTSESBYPRSP_AUX;
        	END IF;
            */

        	V_QTSESOLICSP_INC := V_QTSESOLICSP - V_QTSESOLICSP_AUX;
         /* 04/07/2002 - Leonardo Monteiro
            A linha abaixo foi substituida, a pedido da Eliza (Central de
            Clientes - Sky Chefs) e do Neilor (Suporte - Teknisa), para que
            seja testado a quantidadade de stand-by que deve ser incluida
            para os servicos com o tipo de qunatidade proporcional fixa.
            Obs.: Este teste esta sendo feito apenas para inclusao de servicos.
            16/07/2002 - Leonardo Monteiro
            Alteracao para nao testar apenas para inclusao de servicos.
         V_QTSESBYPRSP_INC := V_QTSESBYPRSP - V_QTSESBYPRSP_AUX;  */
         SELECT IDCALCPROPDLCOM
           INTO V_IDCALCPROPDLCOM
           FROM PARAMCOMIS
          WHERE (CDFILIAL = P_CDFILIAL);

         IF (V_IDPROPSERV = '2') /*AND (V_INCLUI_SERVICO)*/ THEN
            V_QTSESBYPRSP_INC := CEIL((P_QTPAXSBY_CLA+P_QTTOTPAX)/V_NRPROPAXSP) - V_QTSESOLICSP_AUX - V_QTSESBYPRSP_AUX - V_QTSESOLICSP_INC;
         ELSE
            V_QTSESBYPRSP_INC := V_QTSESBYPRSP - V_QTSESBYPRSP_AUX;
         END IF;
         /**/
--DBMS_OUTPUT.PUT_LINE(V_QTSESOLICSP||' - '||V_QTSESOLICSP_AUX);

         -- =========================================================== --
         -- TESTA O TIPO DE PRECO (1 - UNITARIO OU 2 - TOTAL PARA       --
         -- CALCULAR A QUANTIDADE A FATURAR                             --
         -- =========================================================== --
        	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;

         -- ================================================== --
         -- Calculo dos precos dos servicos                    --
         -- ================================================== --
			/*IF V_QTSERFATSP_INC < 0 THEN
     			raise_application_error( -20502, 'Manutecao nao permitida. Existem servicos atendidos com quantidade superior a '||P_QTTOTPAX||'.', false);
         END IF;*/

         /*16/07/2002 - Leonardo Monteiro - solicitacao 8441
           Teste incluido para nao ocorrer erro em servicos com preco proporcional
           para quantidade de passageiro negativa.*/


         -- =========================================================== --
         -- Calculo dos precos Proporcional                             --
         -- =========================================================== --
          /*traz qt de standy by pax + utilizado somente*/
         BEGIN
            SELECT SUM(DECODE (NVL(QTSESBYPRSP,0), 0, 0, NVL(QTSESBYUTSP,0)))
              INTO V_QTREAL
              FROM SERVPLANO SP
             WHERE SP.CDFILIAL  = P_CDFILIAL       AND
             SP.CDCIA           = P_CDCIA          AND
             SP.NRVOO           = P_NRVOO          AND
             SP.CDTRECHO        = P_CDTRECHO       AND
             SP.DTHRPLAVOOPV    = P_DTHRPLAVOOPV   AND
             SP.CDSERVICO       = V_CDSERVICO;
           EXCEPTION
                WHEN NO_DATA_FOUND THEN
                   V_QTREAL  := 0;
         END;

          /*valores anteriores entre pax e sby utilizados*/
         IF V_IDTPPRECOSP = '1' THEN
             BEGIN
                SELECT nvl(SUM(NVL(QTSESBYUTSP,0)+NVL(QTSERFATSP,0)),0)
                  INTO V_QTEMITIDA
                  FROM SERVPLANO SP
                 WHERE SP.CDFILIAL  = P_CDFILIAL       AND
                 SP.CDCIA           = P_CDCIA          AND
                 SP.NRVOO           = P_NRVOO          AND
                 SP.CDTRECHO        = P_CDTRECHO       AND
                 SP.DTHRPLAVOOPV    = P_DTHRPLAVOOPV   AND
                 SP.CDSERVICO       = V_CDSERVICO      AND
                 SP.NRDELIVERY IS NOT NULL;
               EXCEPTION
                    WHEN NO_DATA_FOUND THEN
                       V_QTEMITIDA  := 0;
             END;
         ELSE
             BEGIN
                SELECT nvl(nvl(SUM(nvl(QTATUAPRODSP,0))-SUM(nvl(QTSESBYPRSP,0)-nvl(QTSESBYUTSP,0)),0),0)
                  INTO V_QTEMITIDA
                  FROM SERVPLANO SP
                 WHERE SP.CDFILIAL  = P_CDFILIAL       AND
                 SP.CDCIA           = P_CDCIA          AND
                 SP.NRVOO           = P_NRVOO          AND
                 SP.CDTRECHO        = P_CDTRECHO       AND
                 SP.DTHRPLAVOOPV    = P_DTHRPLAVOOPV   AND
                 SP.CDSERVICO       = V_CDSERVICO      AND
                 SP.NRDELIVERY IS NOT NULL;
               EXCEPTION
                    WHEN NO_DATA_FOUND THEN
                       V_QTEMITIDA  := 0;
             END;
         END IF;
                IF V_QTEMITIDA > 0 THEN
                   V_QTREAL := 0;
                END IF;
         IF V_IDCALCPROPDLCOM = 'S' THEN
            IF V_IDTPPRECOSP = '1' THEN  /*PRECO NORMAL*/
                /*CASO JA TENHA DELIVERYS EMITIDAS NAO VAI CONSIDERAR PENDENTES POIS ESTAS ESTAO SENDO INCLUIDAS NESTE MOMENTO*/
                IF V_QTEMITIDA > 0 THEN
                   V_QTREAL := 0;
                END IF;
                --raise_application_error(-20501, TO_CHAR('incluida:'||V_QTSERFATSP_INC||'atual:'||V_QTREAL||'emitida:'||V_QTEMITIDA), false);
                CALCULA_PRECO('N',P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDSERVICO,
                              P_DTHRPLAVOOPV,V_DTHRABASTPV,
                              V_QTSERFATSP_INC+V_QTREAL+V_QTEMITIDA,
                              V_QTSERFATSP_INC+V_QTREAL+V_QTEMITIDA,
                              NULL,V_VRPRECORRENTE,V_VRPREDOLAR);

                IF V_QTSERFATSP_INC = 0 THEN
                   V_VRPRECORRENTE := 0;
                   V_VRPREDOLAR    := 0;
                END IF;
            ELSE  /*PRECO TOTAL*/
                IF V_QTEMITIDA > 0 THEN
                   V_QTEMITIDA := 0;
                END IF;
                --raise_application_error(-20501, TO_CHAR('incluida:'||P_QTTOTPAX||'atual:'||V_QTREAL||'emitida:'||V_QTEMITIDA), false);
                CALCULA_PRECO('N',P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDSERVICO,
                              P_DTHRPLAVOOPV,V_DTHRABASTPV,
                              P_QTTOTPAX+V_QTREAL+V_QTEMITIDA,
                              P_QTTOTPAX+V_QTREAL+V_QTEMITIDA,
                              NULL,V_VRPRECORRENTE,V_VRPREDOLAR);

                IF V_QTSERFATSP_INC = 0 THEN
                   V_VRPRECORRENTE := 0;
                   V_VRPREDOLAR    := 0;
                END IF;
            END IF;
         ELSE
            CALCULA_PRECO('N',P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO,V_CDSERVICO,
                          /*P_CDCLASSE, V_CDCICLO,*/P_DTHRPLAVOOPV,V_DTHRABASTPV,V_QTREAL+P_QTTOTPAX,
                          V_QTREAL+V_QTSERFATSP_INC,NULL,V_VRPRECORRENTE,V_VRPREDOLAR);
         END IF;

         -- =========================================================== --
         -- Calculo do preco do stand-by                                --
         -- =========================================================== --

        	IF ((V_QTSESBYPRSP_ANT+V_QTSESBYPRSP_INC) > 0) THEN

                IF V_IDCALCPROPDLCOM = 'S' THEN
                    IF V_IDTPPRECOSP = '1' THEN  /*PRECO NORMAL*/
                        /*CASO JA TENHA DELIVERYS EMITIDAS NAO VAI CONSIDERAR PENDENTES POIS ESTAS ESTAO SENDO INCLUIDAS NESTE MOMENTO*/
                        IF V_QTEMITIDA > 0 THEN
                           V_QTREAL := 0;
                        END IF;
                        CALCULA_PRECO('N',P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO, V_CDSERVICO,
                                      P_DTHRPLAVOOPV,V_DTHRABASTPV,
                                      V_QTSERFATSP_INC+V_QTREAL+V_QTEMITIDA,
                                      V_QTSERFATSP_INC+V_QTREAL+V_QTEMITIDA,
                                      NULL,V_VRPRECORR_SBY,V_VRPREDOLAR_SBY);

                        IF V_QTSERFATSP_INC > 0 THEN
                           V_VRPRECORRENTE := V_VRPRECORR_SBY;
                           V_VRPREDOLAR    := V_VRPREDOLAR_SBY;
                        END IF;
                    ELSE /*PRECO TOTAL*/
                    --raise_application_error(-20501, TO_CHAR(P_QTTOTPAX+V_QTREAL)||'*', false);
                        IF V_QTEMITIDA > 0 THEN
                           V_QTEMITIDA := 0;
                        END IF;
                        CALCULA_PRECO('N',P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO, V_CDSERVICO,
                                      P_DTHRPLAVOOPV,V_DTHRABASTPV,
                                      P_QTTOTPAX+V_QTREAL+V_QTEMITIDA+V_QTEMITIDA,
                                      P_QTTOTPAX+V_QTREAL+V_QTEMITIDA+V_QTEMITIDA,
                                      NULL,V_VRPRECORR_SBY,V_VRPREDOLAR_SBY);

                        IF V_QTSERFATSP_INC > 0 THEN
                           V_VRPRECORRENTE := V_VRPRECORR_SBY;
                           V_VRPREDOLAR    := V_VRPREDOLAR_SBY;
                        END IF;
                    END IF;

                ELSE
                    CALCULA_PRECO('N',P_CDFILIAL,P_CDCIA,P_NRVOO,P_CDTRECHO, V_CDSERVICO,
                                 /*P_CDCLASSE,V_CDCICLO,*/P_DTHRPLAVOOPV,V_DTHRABASTPV,
                                 P_QTTOTPAX,V_QTSERFATSP_INC,NULL,V_VRPRECORR_SBY,V_VRPREDOLAR_SBY);
                END IF;

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

         -- ================================================ --
         --       ATUALIZACAO DA TABELA SERVPLANO            --
         -- ================================================ --

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

--DBMS_OUTPUT.PUT_LINE('Passo 1');
        	IF NOT V_INCLUI_SERVICO THEN     /* PARA SERVICOS PENDENTE ALTERA A QTDE */
--DBMS_OUTPUT.PUT_LINE('Passo 2');
          	IF ((V_QTSESOLICSP_ANT+V_QTSESOLICSP_INC > 0) OR
                (V_QTSESBYPRSP_ANT+V_QTSESBYPRSP_INC > 0)) THEN

--DBMS_OUTPUT.PUT_LINE('Alteracao -> '||V_QTSESOLICSP_FINAL||' - '||V_NRITEMPLANSP);
            	UPDATE SERVPLANO
            	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
            	WHERE (CDFILIAL      = P_CDFILIAL    ) AND
                     (CDCIA         = P_CDCIA       ) AND
                     (NRVOO         = P_NRVOO       ) AND
                     (CDTRECHO      = P_CDTRECHO    ) AND
                     (DTHRPLAVOOPV  = P_DTHRPLAVOOPV) AND
                     (CDSERVICO     = V_CDSERVICO   ) AND
                     /*(CDCLASSE      = V_CDCLASSE    ) AND
                     (CDCICLO       = V_CDCICLO     ) AND*/
                     (NRITEMPLANSP  = V_NRITEMPLANSP) AND
                     (IDSITSERSP    = '1'           ) AND
                     (NRDELIVERY   IS NULL          );
          	ELSE
            	DELETE FROM SERVPLANO
            	WHERE (CDFILIAL      = P_CDFILIAL    ) AND
                     (CDCIA         = P_CDCIA       ) AND
                     (NRVOO         = P_NRVOO       ) AND
                     (CDTRECHO      = P_CDTRECHO    ) AND
                     (DTHRPLAVOOPV  = P_DTHRPLAVOOPV) AND
                     (CDSERVICO     = V_CDSERVICO   ) AND
                     /*(CDCLASSE      = V_CDCLASSE    ) AND
                     (CDCICLO       = V_CDCICLO     ) AND*/
                     (NRITEMPLANSP  = V_NRITEMPLANSP) AND
                     (IDSITSERSP    = '1'           ) AND
                     (NRDELIVERY   IS NULL          );
          	END IF;
        	END IF;

        	IF V_INCLUI_SERVICO THEN /* INCLUI SERVICO */

          	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_QTSESOLICSP_INC > 0) OR (V_QTSESBYPRSP_INC > 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,IDFATSESP  ,IDTPPRECOSP,
                CDAGRDECO, CDAGRDEDE, IDSEREXTSP, CDSERVICO, VRPRUDOLSESP,
                VRPRURESESP, IDTIPSCBOSP, VRLOADSESP, NRPROPAXSP,
                VRPRUSBYSP,IDTIPMANUALSP, IDTPMANUTSP/*,CDCLASSE,CDCICLO*/)
               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, 0,
                (V_QTSESBYPRSP_INC+V_QTSESOLICSP_INC),V_QTSERFATSP_INC,
                (V_QTSESBYPRSP_INC+V_QTSESOLICSP_INC),NULL, V_VRPESOSE, SYSDATE,
                NULL, NULL, V_IDFATSESP  ,V_IDTPPRECOSP,
                V_CDAGRDECO, V_CDAGRDEDE, '1', V_CDSERVICO, V_VRPREDOLAR,
                V_VRPRECORRENTE, V_IDPROPSERV, V_VRPELOADSP, V_NRPROPAXSP,
                V_VRPRUSBYSP,V_IDTIPMANUAL, '1D'/*,P_CDCLASSE,V_CDCICLO*/);
          	END IF;
        	END IF;

		END LOOP;
   END IF;

   /* update a plano voo para ajustar a situacao do plano de voo */
   SELECT COUNT(*)
   INTO   V_PENDENTE
   FROM   SERVPLANO
   WHERE  CDFILIAL     = P_CDFILIAL AND
          CDCIA        = P_CDCIA    AND
          NRVOO        = P_NRVOO    AND
          CDTRECHO     = P_CDTRECHO AND
          DTHRPLAVOOPV = P_DTHRPLAVOOPV AND
          IDSITSERSP   = '1';
   IF V_PENDENTE > 0 THEN
     	V_SITUACAO_VOO := '1';
   ELSE
     	V_SITUACAO_VOO := '2';
   END IF;

   UPDATE PLANOVOO
   SET    IDSITABASPV = V_SITUACAO_VOO
   WHERE (CDFILIAL     = P_CDFILIAL    ) AND
         (CDCIA        = P_CDCIA       ) AND
         (NRVOO        = P_NRVOO       ) AND
         (CDTRECHO     = P_CDTRECHO    ) AND
         (DTHRPLAVOOPV = P_DTHRPLAVOOPV);
END;
/