CREATE OR REPLACE PROCEDURE CALCULA_DELIVERYM(
  P_NRDELIVERY     IN  DELIVERYM.NRDELIVERY%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  PLANOVOOM.DTHRPLAVOOPV%TYPE,
  P_CDAGRDECO      IN  SERVPLANOM.CDAGRDECO%TYPE,
  P_CDLOCAFATCIA   IN  LOCALFATUCIA.CDLOCAFATCIA%TYPE,
  P_NRSEQDELIVER   IN  SERVPLANOM.NRSEQDELIVER%TYPE,
  P_CDOPERADOR     IN  VARCHAR2,
  P_PESOTOTSERV    OUT SERVCICLO.VRPESOSE%TYPE,
  P_TOTALGERALSERV OUT DELIVERYM.VRBACATX%TYPE,
  P_VRTAXA         OUT DELIVERYM.VRTXAEROR%TYPE,
  P_DESCNOTA       OUT DELIVERYM.VRDESCDEL%TYPE,
  P_DESCSERV       OUT DELIVERYM.VRDESCDEL%TYPE,
  P_DESCDESV       OUT DELIVERYM.VRDESCDEL%TYPE,
  P_PEDESCDESV     OUT DESPERCIA.VRPERDESC%TYPE,
  P_DESCFIXO       OUT DELIVERYM.VRDESCDEL%TYPE,
  P_PEDESCFIXO     OUT DESPERCIA.VRPERDESC%TYPE,
  P_DESCCOND       OUT DELIVERYM.VRDESCDEL%TYPE,
  P_PEDESCCOND     OUT DESPERCIA.VRPERDESC%TYPE,
  P_TOTSBYUT       OUT DELIVERYM.VRDESCDEL%TYPE,
  P_TOTSBYNUT      OUT DELIVERYM.VRDESCDEL%TYPE,
  P_VRTOTDESC      OUT DESPERCIA.VRTOTDESC%TYPE,
  P_VRUTIDESC      OUT DESPERCIA.VRUTIDESC%TYPE,
  P_IDDESCDESV     OUT CIAFILI.IDDESCDESV%TYPE,
  P_IDDESCCOND     OUT CIAFILI.IDDESCCOND%TYPE,
  P_IDDESCFIXO     OUT CIAFILI.IDDESCFIXO%TYPE,
  P_IDINCDESDESV   OUT VOO.IDINCDESDESV%TYPE,
  P_IDINCDESFIXO   OUT VOO.IDINCDESFIXO%TYPE,
  P_IDINCDESCOND   OUT VOO.IDINCDESCOND%TYPE
 )AS
  V_IDTIPMANUALSP  SERVPLANOM.IDTIPMANUALSP%TYPE;
  V_VRPRURESESP    SERVPLANOM.VRPRURESESP%TYPE;
  V_VRPRUDOLSESP   SERVPLANOM.VRPRUDOLSESP%TYPE;
  V_TOTALITEM      FLOAT;
  V_PESOTOTSERV    SERVCICLO.VRPESOSE%TYPE;
  V_VRTAXA         DELIVERYM.VRTXAEROR%TYPE;
  V_VRTXAEROR      DELIVERYM.VRTXAEROR%TYPE;
  V_VRTXAEROD      DELIVERYM.VRTXAEROD%TYPE;
  V_PRECUNITARIO   SERVPLANOM.VRPRUSBYSP%TYPE;
  V_PRECOSBYUT     SERVPLANOM.VRPRUSBYSP%TYPE;
  V_PRECOSBYNUT    SERVPLANOM.VRPRUSBYSP%TYPE;
  V_VALORSBY       SERVPLANOM.VRPRUSBYSP%TYPE;
  V_QTTOTPAX       PREVISAOPAXM.QTTOTPAXAT%TYPE;
  V_PAXSBY         PREVISAOPAXM.QTPAXSBY%TYPE;
  V_VRPRECORRENTE  FLOAT;
  V_VRPREDOLAR     FLOAT;
  V_VRPRECOSBY     FLOAT;
  V_VRPREDOLARSBY  FLOAT;
  V_DESCDESV       DELIVERYM.VRDESCDEL%TYPE;
  V_DESCFIXO       DELIVERYM.VRDESCDEL%TYPE;
  V_DESCCOND       DELIVERYM.VRDESCDEL%TYPE;
  V_DESCNOTA       DELIVERYM.VRDESCDEL%TYPE;
  V_DESCSERV       DELIVERYM.VRDESCDEL%TYPE;
  V_VRPEDESTXAER   LOCALFATUCIA.VRPEDESTXAER%TYPE;
  V_NRDECITOTANF   PARAMGERAL.NRDECITOTANF%TYPE;
  V_IDUTLOCAFATU   CIAFILI.IDUTLOCAFATU%TYPE;

  /* Variaveis dos cursores */
  VC_CDFILIAL      FILIAL.CDFILIAL%TYPE;
  VC_CDCIA         SERVPLANOM.CDCIA%TYPE;
  VC_NRVOO         SERVPLANOM.NRVOO%TYPE;
  VC_CDTRECHO      SERVPLANOM.CDTRECHO%TYPE;
  VC_DTHRPLAVOOPV  SERVPLANOM.DTHRPLAVOOPV%TYPE;
  VC_IDTIPMANUALSP SERVPLANOM.IDTIPMANUALSP%TYPE;
  VC_VRPRURESESP   SERVPLANOM.VRPRURESESP%TYPE;
  VC_VRPRUDOLSESP  SERVPLANOM.VRPRUDOLSESP%TYPE;
  VC_NRSERVICOCIAFILI SERVPLANOM.NRSERVICOCIAFILI%TYPE;
  VC_NRITEMPLANSP  SERVPLANOM.NRITEMPLANSP%TYPE;
  VC_QTSESOLICSP   SERVPLANOM.QTSESOLICSP%TYPE;
  VC_QTSERVFIXSP   SERVPLANOM.QTSERVFIXSP%TYPE;
  VC_CDCLASSE      CLASSE.CDCLASSE%TYPE;
  VC_CDCICLO       CICLO.CDCICLO%TYPE;
  VC_QTSESBYPRSP   SERVPLANOM.QTSESBYPRSP%TYPE;
  VC_QTSESBYUTSP   SERVPLANOM.QTSESBYUTSP%TYPE;
  VC_VRPERSBYUT    CIAFILI.VRPERSBYUT%TYPE;
  VC_VRPERSBYNUT   CIAFILI.VRPERSBYNUT%TYPE;
  VC_QTSERFATSP    SERVPLANOM.QTSERFATSP%TYPE;
  VC_IDFATSE       SERVICOCIAFILI.IDFATSE%TYPE;
  VC_IDTRIBTXPORT  CIAFILI.IDTRIBTXPORT%TYPE;
  VC_VRPESOSESP    NUMBER;
  VC_VRDESCDELTMP  OBSDELIVTMPM.VRDESCDELTMP%TYPE;
  VC_VRDESCSRVTMP  OBSDELIVTMPM.VRDESCSRVTMP%TYPE;
  VC_VRPERDESCCIA  DESCMENSCIA.VRPERDESCCIA%TYPE;
  VC_VRALTXPORT    CIAFILI.VRALTXPORT%TYPE;
  VC_IDDESCDESV    CIAFILI.IDDESCDESV%TYPE;
  VC_IDDESCCOND    CIAFILI.IDDESCCOND%TYPE;
  VC_IDDESCFIXO    CIAFILI.IDDESCFIXO%TYPE;
  VC_VRPEDESCFIXO  CIAFILI.VRPEDESCFIXO%TYPE;
  VC_VRPERDESC     DESPERCIA.VRPERDESC%TYPE;
  VC_IDINCDESDESV  VOO.IDINCDESDESV%TYPE;
  VC_IDINCDESFIXO  VOO.IDINCDESFIXO%TYPE;
  VC_IDINCDESCOND  VOO.IDINCDESCOND%TYPE;
  VC_VRFAIXADESC   NUMBER;
  VC_VRTOTDESC     DESPERCIA.VRTOTDESC%TYPE;
  VC_VRUTIDESC     DESPERCIA.VRUTIDESC%TYPE;
  VC_IDTPPRECO     SERVICOCIAFILI.IDTPPRECO%TYPE;
  VC_DTHRABASTPV   PLANOVOOM.DTHRABASTPV%TYPE;
  VC_QTATUAPRODSP  SERVPLANOM.QTATUAPRODSP%TYPE;
  VC_DESCCONDEL    DELIVERYM.VRDESCCOND%TYPE;
  VC_VRPRECMANUTSESP   SERVPLANOM.VRPRECMANUTSESP%TYPE;
  VC_VRDESCPROP    FLOAT;
  VC_VRPRUSBYSP    FLOAT;
  /*VARIAVEIS DA PROPORCIONALIDADE*/
  V_ORIGINAL       VARCHAR2(15);
  V_VRDESCPROP     FLOAT;
  V_QTSERFATSP     NUMBER;
  V_vrExibe        FLOAT;
  V_STBYPAGO       NUMBER(10,5);
  V_STBYAPAGAR     NUMBER(10,5);
  V_QTREALSTBAY    NUMBER;
  V_PROP           NUMBER;
  V_NRTABPRECO     TABPRECOSE.NRTABPRECO%TYPE;
  V_UTILIZADA      NUMBER;
  V_NAOUSADA       NUMBER;
  V_QTGERAL        NUMBER;
  V_QTGERALUSADOS  NUMBER;
  V_VRGERALUSADOS  FLOAT;
  V_VRGERAL        FLOAT;
  V_VRGERALUSADOSDOL  FLOAT;
  V_VRGERALDOL        FLOAT;
  V_TABELA         VARCHAR2(4);
  V_QTREAL         FLOAT;
  V_IDCALCPROPDLCOM PARAMCOMIS.IDCALCPROPDLCOM%TYPE;
  V_VRDESCONTOPROP OBSDELIVTMPM.VRDESCONTOPROP%TYPE;
  V_TOTAL           FLOAT;
  V_QTTOTAL         FLOAT;
  V_VRCOMPLEMENTAR  VARCHAR2(15);
  V_QTNAOUSADOS     NUMBER;
  V_TOTVRSBYNUT     FLOAT;
  V_NRVOORETROBILL  VARCHAR2(5);
  V_NRVOO VARCHAR2(5);
  V_CDCIA VARCHAR2(4);
  V_CDAGRDELRETROBILL VARCHAR2(4);

  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
  V_NRORG NUMBER;
-- =====================================================================
-- CURSOR PARA CALCULO DO DELIVERY
-- =====================================================================
  CURSOR C_SERVPLANO IS
  SELECT SP.CDFILIAL, SP.CDCIA,SP.NRVOO,SP.CDTRECHO,SP.DTHRPLAVOOPV,
         SP.IDTIPMANUALSP,ROUND(SP.VRPRURESESP,2) VRPRURESESP, SP.VRPRUDOLSESP,
         SP.NRSERVICOCIAFILI,SP.NRITEMPLANSP,SP.QTSESOLICSP,SP.QTSERVFIXSP,
         SP.CDCLASSE,SP.QTSESBYPRSP, SP.QTSESBYUTSP,CF.VRPERSBYUT,
         CF.VRPERSBYNUT,SP.QTSERFATSP,SP.IDFATSESP, CF.IDTRIBTXPORT,
         (SP.VRPESOSESP*SP.QTSERFATSP) AS VRPESOSESP,
         OB.VRDESCDELTMP,OB.VRDESCSRVTMP,NVL(DD.VRPERDESCCIA,0) AS VRPERDESCCIA,
         CF.VRALTXPORT,CF.IDDESCDESV,CF.IDDESCCOND,CF.IDDESCFIXO,
         NVL(CF.VRPEDESCFIXO,0) AS VRPEDESCFIXO,NVL(DP.VRPERDESC,0) AS VRPERDESC,
         VO.IDINCDESDESV, VO.IDINCDESFIXO, VO.IDINCDESCOND,
         (NVL(DP.VRTOTDESC,0)-NVL(DP.VRUTIDESC,0)) AS VRFAIXADESC,
         NVL(DP.VRTOTDESC,0) AS VRTOTDESC,NVL(DP.VRUTIDESC,0) AS VRUTIDESC,
         SP.IDTPPRECOSP,PV.DTHRABASTPV, SP.QTATUAPRODSP, SP.VRPRECMANUTSESP, SP.VRDESCPROP,
         SP.VRPRUSBYSP,SP.CDCICLO
  FROM   SERVPLANOM SP, PLANOVOOM PV, OBSDELIVTMPM OB, DESCMENSCIA DD, DESPERCIA DP,
         VOO VO, CIAFILI CF
  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.CDAGRDECO       = P_CDAGRDECO    ) AND
        (NVL(SP.NRSEQDELIVER, 0) = P_NRSEQDELIVER ) AND
        (SP.NRDELIVERY      IS NULL          ) AND
        (SP.IDSITSERSP      = '1'            ) AND
        (CF.CDFILIAL        = P_CDFILIAL     ) AND
        (CF.CDCIA           = P_CDCIA        ) AND
        (VO.CDFILIAL        = P_CDFILIAL     ) AND
        (VO.CDCIA           = SP.CDCIA       ) AND
        (SP.IDSITSERSP  <> '3') AND
        (VO.NRVOO           = SP.NRVOO       ) AND
        (OB.CDFILIAL    (+) = P_CDFILIAL     ) AND
        (OB.CDCIATMP    (+) = SP.CDCIA       ) AND
        (OB.NRVOOTMP    (+) = SP.NRVOO       ) AND
        (OB.CDTRECHOTMP (+) = SP.CDTRECHO    ) AND
        (OB.DTHRPLAVOTMP(+) = P_DTHRPLAVOOPV ) AND
        (OB.CDAGRDECOTMP(+) = P_CDAGRDECO    ) AND
        (DP.CDFILIAL    (+) = P_CDFILIAL     ) AND
        (DP.CDCIA       (+) = SP.CDCIA       ) AND
        (DD.CDFILIAL    (+) = P_CDFILIAL     ) AND
        (DD.CDCIA       (+) = SP.CDCIA       ) AND
        ((P_DTHRPLAVOOPV BETWEEN DP.DTINIDESC(+) AND DP.DTFIMDESC(+))) AND
        ((TO_CHAR(DD.DTDESCCIA(+),'MM/YYYY') = TO_CHAR(P_DTHRPLAVOOPV,'MM/YYYY'))) AND
        (PV.CDFILIAL        = P_CDFILIAL     ) AND
        (PV.CDCIA           = P_CDCIA        ) AND
        (PV.NRVOO           = P_NRVOO        ) AND
        (PV.CDTRECHO        = P_CDTRECHO     ) AND
        (PV.DTHRPLAVOOPV    = P_DTHRPLAVOOPV );
-- =====================================================================
-- CURSOR PARA RECALCULO DO DELIVERY
-- =====================================================================
  CURSOR C_DELIVERY IS
  SELECT SP.CDFILIAL, SP.CDCIA,SP.NRVOO,SP.CDTRECHO,SP.DTHRPLAVOOPV,
         SP.IDTIPMANUALSP,ROUND(SP.VRPRURESESP,2) VRPRURESESP, SP.VRPRUDOLSESP,
         SP.NRSERVICOCIAFILI,SP.NRITEMPLANSP,SP.QTSESOLICSP,SP.QTSERVFIXSP,
         SP.CDCLASSE,SP.QTSESBYPRSP, SP.QTSESBYUTSP,CF.VRPERSBYUT,
         CF.VRPERSBYNUT,SP.QTSERFATSP,SP.IDFATSESP, CF.IDTRIBTXPORT,
         (SP.VRPESOSESP*SP.QTSERFATSP) AS VRPESOSESP,
         DE.VRDESCDEL,0 VRDESCSRVTMP,NVL(DD.VRPERDESCCIA,0) AS VRPERDESCCIA,
         DE.VRPETXAERO,CF.IDDESCDESV,CF.IDDESCCOND,CF.IDDESCFIXO,
         NVL(CF.VRPEDESCFIXO,0) AS VRPEDESCFIXO,NVL(DP.VRPERDESC,0) AS VRPERDESC,
         VO.IDINCDESDESV, VO.IDINCDESFIXO, VO.IDINCDESCOND,
         (NVL(DP.VRTOTDESC,0)-NVL(DP.VRUTIDESC,0)) AS VRFAIXADESC,
         NVL(DP.VRTOTDESC,0) AS VRTOTDESC,NVL(DP.VRUTIDESC,0) AS VRUTIDESC,
         SP.IDTPPRECOSP,DE.DTEMISDEL, SP.QTATUAPRODSP, VRDESCCOND, SP.VRPRECMANUTSESP, SP.VRDESCPROP,
         SP.VRPRUSBYSP, SP.CDCICLO
  FROM SERVPLANOM SP, DESCMENSCIA DD, DESPERCIA DP,CIAFILI CF, VOO VO, DELIVERYM DE
  WHERE (DE.CDFILIAL    = P_CDFILIAL  ) AND
        (DE.NRDELIVERY  = P_NRDELIVERY) AND
        (SP.CDFILIAL    = P_CDFILIAL  ) AND
        (SP.NRDELIVERY  = P_NRDELIVERY) AND
        (SP.IDSITSERSP  = '2'         ) AND
        (SP.IDSITSERSP  <> '3')         AND
        (CF.CDFILIAL    = P_CDFILIAL  ) AND
        (CF.CDCIA       = SP.CDCIA    ) AND
        (VO.CDFILIAL    = SP.CDFILIAL ) AND
        (VO.CDCIA       = SP.CDCIA    ) AND
        (VO.NRVOO       = SP.NRVOO    ) AND
        (DP.CDFILIAL(+) = SP.CDFILIAL ) AND
        (DP.CDCIA(+)    = SP.CDCIA    ) AND
        (DD.CDFILIAL(+) = SP.CDFILIAL ) AND
        (DD.CDCIA(+)    = SP.CDCIA    ) AND
        ((SP.DTHRPLAVOOPV BETWEEN DP.DTINIDESC(+) AND DP.DTFIMDESC(+))) AND
        ((TO_CHAR(DD.DTDESCCIA(+),'MM/YYYY') = TO_CHAR(SP.DTHRPLAVOOPV,'MM/YYYY')));

BEGIN

   V_TOTAL := 0;

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


   WEBTUNNING(V_DSREMOTEADDR, 
              V_DSREMOTEHOST, 
              V_DSSERVERRDM, 
              V_DSSERVMETHOD, 
              V_DSREMOTEOPER);
   
   /*----RETROBILL >>>>*/
   V_CDCIA := P_CDCIA;
   V_NRVOO := P_NRVOO;
   IF P_NRDELIVERY IS NOT NULL THEN   
      SELECT MAX(P_NRVOO), MAX(CDCIA)
        INTO V_NRVOO, V_CDCIA 
        FROM DELIVERYM
       WHERE CDFILIAL = P_CDFILIAL
         AND NRDELIVERY = P_NRDELIVERY;
   END IF;  
   
   /* Busca dados referentes a retrobill, para identificar  */
   SELECT MAX(NRVOORETROBILL), MAX(VRALTXPORT), MAX(CDAGRDELRETROBILL)
     INTO V_NRVOORETROBILL, VC_VRALTXPORT, V_CDAGRDELRETROBILL
     FROM CIAFILI
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = V_CDCIA;
   
   IF V_NRVOORETROBILL = V_NRVOO THEN
      IF P_NRDELIVERY IS NOT NULL THEN
         /* Busca o desconto de nota e desconto de servico da delivery */
         SELECT MAX(VRDESCDEL), MAX(0)
           INTO V_DESCNOTA, V_DESCSERV
           FROM DELIVERYM
          WHERE CDFILIAL = P_CDFILIAL
            AND NRDELIVERY = P_NRDELIVERY;
         
         /* Soma o total de cada item e peso */
         SELECT SUM(ROUND(QTSERFATSP * NVL(CASE WHEN IDTIPMANUALSP = '1' THEN VRPRURESESP ELSE VRPRUDOLSESP END, 0.00), 2)),
                SUM(NVL(VRPESOSESP, 0.00)),
                MAX(IDTIPMANUALSP)
           INTO V_TOTAL, V_PESOTOTSERV, VC_IDTIPMANUALSP
           FROM SERVPLANOM
          WHERE CDFILIAL = P_CDFILIAL
            AND NRDELIVERY = P_NRDELIVERY;
         
         V_VRTXAEROR := CASE WHEN VC_IDTIPMANUALSP = '1' THEN ROUND(V_TOTAL * VC_VRALTXPORT, 2) ELSE 0.00 END;
         V_VRTXAEROD := CASE WHEN VC_IDTIPMANUALSP = '2' THEN ROUND(V_TOTAL * VC_VRALTXPORT, 2) ELSE 0.00 END;         
         V_VRTAXA := CASE WHEN VC_IDTIPMANUALSP = '1' THEN V_VRTXAEROR ELSE V_VRTXAEROD END;
         
         /* Atualiza a taxa de aeroporto da delivery */
         UPDATE DELIVERYM
            SET VRBACATX   = NVL(V_TOTAL,0),
                VRTXAEROR  = V_VRTXAEROR,
                VRTXAEROD  = V_VRTXAEROD
            WHERE (CDFILIAL   = P_CDFILIAL  ) AND
                  (NRDELIVERY = P_NRDELIVERY) AND
                  (IDSITDEL   = '1');
      
      ELSE
         /* Busca o desconto de nota e desconto de servico da delivery */
         SELECT MAX(VRDESCDELTMP), MAX(VRDESCSRVTMP)
           INTO V_DESCNOTA, V_DESCSERV
           FROM OBSDELIVTMPM
          WHERE CDFILIAL = P_CDFILIAL
            AND CDCIATMP = P_CDCIA
            AND NRVOOTMP = P_NRVOO
            AND CDTRECHOTMP = P_CDTRECHO
            AND DTHRPLAVOTMP = P_DTHRPLAVOOPV
            AND CDAGRDECOTMP = V_CDAGRDELRETROBILL;
         
         /* Soma o total de cada item e peso */
         SELECT SUM(ROUND(QTSERFATSP * NVL(CASE WHEN IDTIPMANUALSP = '1' THEN VRPRURESESP ELSE VRPRUDOLSESP END, 0.00), 2)),
                SUM(NVL(VRPESOSESP, 0.00))
           INTO V_TOTAL, V_PESOTOTSERV
           FROM SERVPLANOM
          WHERE CDFILIAL = P_CDFILIAL
            AND CDCIA = P_CDCIA
            AND NRVOO = P_NRVOO
            AND CDTRECHO = P_CDTRECHO
            AND DTHRPLAVOOPV = P_DTHRPLAVOOPV;
            
         V_VRTXAEROR := CASE WHEN VC_IDTIPMANUALSP = '1' THEN ROUND(V_TOTAL * VC_VRALTXPORT, 2) ELSE 0.00 END;
         V_VRTXAEROD := CASE WHEN VC_IDTIPMANUALSP = '2' THEN ROUND(V_TOTAL * VC_VRALTXPORT, 2) ELSE 0.00 END;         
         V_VRTAXA := CASE WHEN VC_IDTIPMANUALSP = '1' THEN V_VRTXAEROR ELSE V_VRTXAEROD END;
      END IF;
      
      P_PESOTOTSERV    := NVL(V_PESOTOTSERV   ,0);
      P_TOTALGERALSERV := NVL(V_TOTAL         ,0);
      P_VRTAXA         := NVL(V_VRTAXA        ,0);
      P_DESCNOTA       := NVL(V_DESCNOTA      ,0);
      P_DESCSERV       := NVL(V_DESCSERV      ,0);
      P_DESCDESV       := 0.00;
      P_DESCFIXO       := 0.00;
      P_DESCCOND       := 0.00;
      P_TOTSBYUT       := 0.00;
      P_TOTSBYNUT      := 0.00;
      
      /* NAO SERA FEITO O CALCULO ITEM A ITEM PARA O RETROBILL */
      RETURN;
      
   END IF;
   /*<<<< RETROBILL----*/

   SELECT MAX(NRORG)
     INTO V_NRORG
     FROM PLANOVOOM
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = P_CDCIA
      AND NRVOO = P_NRVOO
      AND CDTRECHO = P_CDTRECHO
      AND DTHRPLAVOOPV = P_DTHRPLAVOOPV;

   /*--Calculo do desconto proporcional referente ao preco proporcional--*/
   CALC_DESC_PROP_DELIVERYM(P_CDFILIAL,  P_CDCIA, P_NRVOO, P_CDTRECHO,
                            P_DTHRPLAVOOPV, P_CDOPERADOR, V_NRORG);

   IF P_NRDELIVERY IS NULL THEN
      OPEN C_SERVPLANO;
   ELSE
      OPEN C_DELIVERY;
   END IF;
   
   LOOP
      IF P_NRDELIVERY IS NULL THEN
         FETCH
            C_SERVPLANO
         INTO
            VC_CDFILIAL     , VC_CDCIA        , VC_NRVOO       , VC_CDTRECHO    ,
            VC_DTHRPLAVOOPV , VC_IDTIPMANUALSP, VC_VRPRURESESP , VC_VRPRUDOLSESP,
            VC_NRSERVICOCIAFILI, VC_NRITEMPLANSP , VC_QTSESOLICSP , VC_QTSERVFIXSP ,
            VC_CDCLASSE     , VC_QTSESBYPRSP  , VC_QTSESBYUTSP , VC_VRPERSBYUT  ,
            VC_VRPERSBYNUT  , VC_QTSERFATSP   , VC_IDFATSE     , VC_IDTRIBTXPORT,
            VC_VRPESOSESP   , VC_VRDESCDELTMP , VC_VRDESCSRVTMP, VC_VRPERDESCCIA,
            VC_VRALTXPORT   , VC_IDDESCDESV   , VC_IDDESCCOND  , VC_IDDESCFIXO  ,
            VC_VRPEDESCFIXO , VC_VRPERDESC    , VC_IDINCDESDESV, VC_IDINCDESFIXO,
            VC_IDINCDESCOND , VC_VRFAIXADESC  , VC_VRTOTDESC   , VC_VRUTIDESC   ,
            VC_IDTPPRECO    , VC_DTHRABASTPV  , VC_QTATUAPRODSP, VC_VRPRECMANUTSESP,
            VC_VRDESCPROP, VC_VRPRUSBYSP,VC_CDCICLO;
         EXIT WHEN C_SERVPLANO%NOTFOUND;
      ELSE
         FETCH
            C_DELIVERY
         INTO
            VC_CDFILIAL     , VC_CDCIA        , VC_NRVOO       , VC_CDTRECHO    ,
            VC_DTHRPLAVOOPV , VC_IDTIPMANUALSP, VC_VRPRURESESP , VC_VRPRUDOLSESP,
            VC_NRSERVICOCIAFILI, VC_NRITEMPLANSP , VC_QTSESOLICSP , VC_QTSERVFIXSP ,
            VC_CDCLASSE     , VC_QTSESBYPRSP  , VC_QTSESBYUTSP , VC_VRPERSBYUT  ,
            VC_VRPERSBYNUT  , VC_QTSERFATSP   , VC_IDFATSE     , VC_IDTRIBTXPORT,
            VC_VRPESOSESP   , VC_VRDESCDELTMP , VC_VRDESCSRVTMP, VC_VRPERDESCCIA,
            VC_VRALTXPORT   , VC_IDDESCDESV   , VC_IDDESCCOND  , VC_IDDESCFIXO  ,
            VC_VRPEDESCFIXO , VC_VRPERDESC    , VC_IDINCDESDESV, VC_IDINCDESFIXO,
            VC_IDINCDESCOND , VC_VRFAIXADESC  , VC_VRTOTDESC   , VC_VRUTIDESC   ,
            VC_IDTPPRECO    , VC_DTHRABASTPV  , VC_QTATUAPRODSP, VC_DESCCONDEL  ,
            VC_VRPRECMANUTSESP, VC_VRDESCPROP, VC_VRPRUSBYSP,VC_CDCICLO;
         EXIT WHEN C_DELIVERY%NOTFOUND;
      END IF;

      -- =========================================================== --
      -- Calculo dos precos                                          --
      -- =========================================================== --
      SELECT MAX(QTTOTPAXAT),MAX(QTPAXSBY)
      INTO   V_QTTOTPAX, V_PAXSBY
      FROM   PREVISAOPAXM
      WHERE  (CDFILIAL     = VC_CDFILIAL    ) AND
             (CDCIA        = VC_CDCIA       ) AND
             (NRVOO        = VC_NRVOO       ) AND
             (CDTRECHO     = VC_CDTRECHO    ) AND
             (CDCLASSE     = VC_CDCLASSE    ) AND
             (DTHRPLAVOOPV = VC_DTHRPLAVOOPV);

      BEGIN
         SELECT NVL(MAX(NRSERVICOCIAFILI),0)
           INTO V_VRCOMPLEMENTAR
           FROM SERVPLANOM
          WHERE CDFILIAL     = VC_CDFILIAL     AND
                CDCIA        = VC_CDCIA        AND
                NRVOO        = VC_NRVOO        AND
                CDTRECHO     = VC_CDTRECHO     AND
                NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI AND
                CDCLASSE = VC_CDCLASSE AND
                CDCICLO  = VC_CDCICLO AND
                DTHRPLAVOOPV = VC_DTHRPLAVOOPV AND
                IDSITSERSP  <> '3'             AND
                NRDELIVERY IS NOT NULL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_VRCOMPLEMENTAR  := 0;
      END;

      BEGIN
         SELECT MAX(IDCALCPROPDLCOM)
           INTO V_IDCALCPROPDLCOM
          FROM PARAMCOMIS
          WHERE (CDFILIAL = VC_CDFILIAL);
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_IDCALCPROPDLCOM := NULL;
      END;

      BEGIN
         SELECT NRTABPRECO
           INTO V_TABELA
           FROM TABPRECOSEM
          WHERE (CDFILIAL = VC_CDFILIAL) AND
                (CDCIA    = VC_CDCIA) AND
                (TRUNC(SYSDATE) BETWEEN DTINIVIGTAB AND DTFIMVIGTAB);
      EXCEPTION
          WHEN NO_DATA_FOUND THEN
             V_TABELA := NULL;
      END;

      BEGIN
         SELECT COUNT(*) QTPROP
            INTO V_PROP
         FROM PROPRECOSEM
         WHERE (CDFILIAL   = VC_CDFILIAL  ) AND
               (CDCIA      = VC_CDCIA     ) AND
               (NRTABPRECO = V_TABELA)      AND
               (NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI) AND
               (CDCLASSE         = VC_CDCLASSE) AND
               (CDCICLO          = VC_CDCICLO);
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_PROP  := 0;
      END;

         -- =========================================================== --
         -- Calculo d preco Proporcional NORMAL                          --
         -- =========================================================== --
         BEGIN
         SELECT  NVL(SUM(QTSERFATSP)+SUM(QTSESBYUTSP),0)
           INTO V_QTREAL
           FROM SERVPLANOM SP
           WHERE (SP.CDFILIAL        = VC_CDFILIAL      ) AND
                 (SP.CDCIA           = VC_CDCIA         ) AND
                 (SP.NRVOO           = VC_NRVOO         ) AND
                 (SP.CDTRECHO        = VC_CDTRECHO      ) AND
                 (SP.NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI) AND
                 (SP.CDCLASSE = VC_CDCLASSE) AND
                 (SP.CDCICLO  = VC_CDCICLO) AND
                 (SP.DTHRPLAVOOPV    = VC_DTHRPLAVOOPV  ) AND
                 (SP.IDSITSERSP  <> '3')             AND
                 (SP.NRSERVICOCIAFILI||SP.CDCLASSE||SP.CDCICLO IN (SELECT SP.NRSERVICOCIAFILI||SP.CDCLASSE||SP.CDCICLO
                                      FROM SERVPLANOM SP
                                    WHERE SP.CDFILIAL        = VC_CDFILIAL       AND
                                          SP.CDCIA           = VC_CDCIA          AND
                                          SP.NRVOO           = VC_NRVOO          AND
                                          SP.CDTRECHO        = VC_CDTRECHO       AND
                                          SP.DTHRPLAVOOPV    = VC_DTHRPLAVOOPV   AND
                                          SP.NRSERVICOCIAFILI= VC_NRSERVICOCIAFILI AND
                                          SP.CDCLASSE        = VC_CDCLASSE  AND
                                          SP.CDCICLO         = VC_CDCICLO   AND
                                          SP.IDSITSERSP  <> '3'             AND
                                          SP.NRDELIVERY IS NULL)) AND
                 (SP.NRSERVICOCIAFILI||SP.CDCLASSE||SP.CDCICLO IN (SELECT PP.NRSERVICOCIAFILI||PP.CDCLASSE||PP.CDCICLO
                                     FROM PROPRECOSEM PP
                                     WHERE (PP.CDFILIAL      = VC_CDFILIAL  ) AND
                                           (PP.CDCIA         = VC_CDCIA     )));
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_QTREAL  := 0;
            END;




         -- =========================================================== --
         -- Calculo du preco Proporcional TOTAL                         --
         -- =========================================================== --
         BEGIN
         SELECT  NVL(SUM(QTATUAPRODSP),0)-NVL(SUM(QTSESBYPRSP-QTSESBYUTSP),0)
           INTO V_QTTOTAL
           FROM SERVPLANOM SP
           WHERE (SP.CDFILIAL        = VC_CDFILIAL      ) AND
                 (SP.CDCIA           = VC_CDCIA         ) AND
                 (SP.NRVOO           = VC_NRVOO         ) AND
                 (SP.CDTRECHO        = VC_CDTRECHO      ) AND
                 (SP.NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI) AND
                 (SP.CDCLASSE = VC_CDCLASSE) AND
                 (SP.CDCICLO  = VC_CDCICLO) AND
                 (SP.DTHRPLAVOOPV    = VC_DTHRPLAVOOPV  ) AND
                 (SP.IDSITSERSP  <> '3'                 ) AND
                 (SP.NRSERVICOCIAFILI||SP.CDCLASSE||SP.CDCICLO IN (SELECT SP.NRSERVICOCIAFILI||SP.CDCLASSE||SP.CDCICLO
                                      FROM SERVPLANOM SP
                                    WHERE SP.CDFILIAL        = VC_CDFILIAL       AND
                                          SP.CDCIA           = VC_CDCIA          AND
                                          SP.NRVOO           = VC_NRVOO          AND
                                          SP.CDTRECHO        = VC_CDTRECHO       AND
                                          SP.DTHRPLAVOOPV    = VC_DTHRPLAVOOPV   AND
                                          SP.NRSERVICOCIAFILI= VC_NRSERVICOCIAFILI AND
                                          SP.CDCLASSE        = VC_CDCLASSE AND
                                          SP.CDCICLO         = VC_CDCICLO  AND
                                          SP.IDSITSERSP  <> '3'                  AND
                                          SP.NRDELIVERY IS NULL)) AND
                 (SP.NRSERVICOCIAFILI||SP.CDCLASSE||SP.CDCICLO IN (SELECT PP.NRSERVICOCIAFILI||PP.CDCLASSE||PP.CDCICLO
                                     FROM PROPRECOSEM PP
                                     WHERE (PP.CDFILIAL      = VC_CDFILIAL  ) AND
                                           (PP.CDCIA         = VC_CDCIA     )));
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_QTTOTAL := 0;
            END;

            BEGIN
               SELECT MAX(NRSERVICOCIAFILI)
                 INTO V_ORIGINAL
                 FROM SERVPLANOM SP
                WHERE (SP.CDFILIAL         = VC_CDFILIAL      ) AND
                      (SP.CDCIA            = VC_CDCIA         ) AND
                      (SP.NRVOO            = VC_NRVOO         ) AND
                      (SP.CDTRECHO         = VC_CDTRECHO      ) AND
                      (SP.DTHRPLAVOOPV     = VC_DTHRPLAVOOPV  ) AND
                      (SP.NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI)AND
                      (SP.CDCLASSE = VC_CDCLASSE) AND
                      (SP.CDCICLO  = VC_CDCICLO ) AND
                      (SP.IDSITSERSP  <> '3'    ) AND
                      (SP.NRDELIVERY IS NOT NULL);
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_ORIGINAL := 0;
            END;


/* -------------------------------------- PROPORCIONALIDADE ----------------------------------------*/
        /*esta trazendo errado, traz todos os deliverys*/
      BEGIN
         IF P_NRDELIVERY IS NULL THEN
            SELECT NVL(SUM(NVL(VRDESCPROP,0)),0), NVL(SUM(NVL(QTSERFATSP,0)),0), NVL(SUM(NVL(QTSESBYUTSP,0)),0), NVL(SUM(NVL(QTSESBYPRSP - QTSESBYUTSP,0)),0)
            INTO V_VRDESCPROP, V_QTSERFATSP, V_UTILIZADA, V_NAOUSADA
            FROM SERVPLANOM
            WHERE CDFILIAL     = VC_CDFILIAL     AND
                  CDCIA        = VC_CDCIA        AND
                  NRVOO        = VC_NRVOO        AND
                  CDTRECHO     = VC_CDTRECHO     AND
                  DTHRPLAVOOPV = VC_DTHRPLAVOOPV AND
                  NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI AND
                  CDCLASSE         = VC_CDCLASSE AND
                  CDCICLO          = VC_CDCICLO  AND
                  IDSITSERSP  <> '3'             AND
                  NRDELIVERY IS NULL;
         ELSE
            SELECT NVL(SUM(NVL(VRDESCPROP,0)),0), NVL(SUM(NVL(QTSERFATSP,0)),0), NVL(SUM(NVL(QTSESBYUTSP,0)),0), NVL(SUM(NVL(QTSESBYPRSP - QTSESBYUTSP,0)),0)
            INTO V_VRDESCPROP, V_QTSERFATSP, V_UTILIZADA, V_NAOUSADA
            FROM SERVPLANOM
            WHERE CDFILIAL     = VC_CDFILIAL     AND
                  CDCIA        = VC_CDCIA        AND
                  NRVOO        = VC_NRVOO        AND
                  CDTRECHO     = VC_CDTRECHO     AND
                  DTHRPLAVOOPV = VC_DTHRPLAVOOPV AND
                  NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI AND
                  CDCLASSE         = VC_CDCLASSE AND
                  CDCICLO          = VC_CDCICLO  AND
                  IDSITSERSP  <> '3'             AND
                  NRDELIVERY   = P_NRDELIVERY;
         END IF;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_VRDESCPROP := 0;
            V_QTSERFATSP := 0;
            V_UTILIZADA  := 0;
            V_NAOUSADA   := 0;
      END;

      -- preco dos itens de pax normal
      IF (V_IDCALCPROPDLCOM = 'S') AND (V_VRCOMPLEMENTAR IS NOT NULL) AND (V_ORIGINAL IS NOT NULL) AND(V_PROP > 0) THEN
         IF VC_IDTPPRECO = '2' THEN

            CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                          P_DTHRPLAVOOPV,VC_DTHRABASTPV,V_QTTOTPAX,V_QTTOTAL,
                          VC_IDTIPMANUALSP, V_VRPRECORRENTE,V_VRPREDOLAR);
         ELSE

            CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                          P_DTHRPLAVOOPV,VC_DTHRABASTPV,V_QTREAL,V_QTREAL,
                          VC_IDTIPMANUALSP,V_VRPRECORRENTE,V_VRPREDOLAR);
         END IF;



         /* SUBSTITUI O VALOR DO ITEM PELO VALOR PROPORCIONAL*/
         IF (V_QTSERFATSP+V_UTILIZADA) > 0 THEN            
            V_vrExibe := (V_VRDESCPROP/(V_QTSERFATSP+V_UTILIZADA));            
         ELSE
            V_vrExibe := 0;
         END IF;


         --se tiver standy by usado participa da divisao do desconto pois sao considerados juntos ao pax.
         IF (V_UTILIZADA > 0) THEN
            V_VALORSBY := V_vrExibe;
         ELSE
            V_VALORSBY := 0;
         END IF;

         IF V_QTSERFATSP = 0 THEN
            V_vrExibe := 0;
         END IF;

/* ----------------------------------------PROPORCIONALIDADE ----------------------------------------*/
      ELSE
         IF (V_IDCALCPROPDLCOM = 'S') THEN  /*SE ESTIVER MARCADO O PARAM DEVE CALCULAR O STANDY BY UTILIZADO NA FAXA DE PRECO*/
           IF VC_IDTPPRECO = '2' THEN   --TOTAL

           CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                         P_DTHRPLAVOOPV,VC_DTHRABASTPV,V_QTTOTPAX,V_QTTOTAL,
                         VC_IDTIPMANUALSP,V_VRPRECORRENTE,V_VRPREDOLAR);
           ELSE -- real

           CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                         P_DTHRPLAVOOPV,VC_DTHRABASTPV,V_QTREAL,V_QTREAL,
                         VC_IDTIPMANUALSP,V_VRPRECORRENTE,V_VRPREDOLAR);

           END IF;
         ELSE

           CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                         P_DTHRPLAVOOPV,VC_DTHRABASTPV,V_QTTOTPAX,VC_QTATUAPRODSP,
                         VC_IDTIPMANUALSP,V_VRPRECORRENTE,V_VRPREDOLAR);
         END IF;
      END IF;


      IF VC_IDTIPMANUALSP = '1' THEN  /* SERVICOS NACIONAIS */
         V_PRECUNITARIO := NVL(VC_VRPRECMANUTSESP, V_VRPRECORRENTE);
      ELSE
         V_PRECUNITARIO := NVL(VC_VRPRECMANUTSESP,V_VRPREDOLAR);
      END IF;

      IF V_PRECUNITARIO IS NULL THEN
        V_PRECUNITARIO := 0;
      END IF;
      
--INICIO---------------standy by------------standy by----------------standy by------------standy by----------------standy by------------standy by----------------

      IF VC_QTSESBYPRSP > 0 THEN
        /*SE ESTIVER MARCADO O PARAM DEVE CALCULAR O STANDY BY UTILIZADO NA FAXA DE PRECO DO PAX NORMAL*/
        IF (V_IDCALCPROPDLCOM = 'S') AND (V_VRCOMPLEMENTAR IS NOT NULL) AND (V_ORIGINAL IS NOT NULL) AND(V_PROP > 0) THEN
          IF VC_IDTPPRECO = '2' THEN   --TOTAL

          CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                        P_DTHRPLAVOOPV,VC_DTHRABASTPV, V_QTTOTPAX , V_QTTOTAL,
                        VC_IDTIPMANUALSP,V_VRPRECOSBY,V_VRPREDOLARSBY);
          ELSE

          CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                        P_DTHRPLAVOOPV,VC_DTHRABASTPV, V_QTREAL , V_QTREAL,
                        VC_IDTIPMANUALSP,V_VRPRECOSBY,V_VRPREDOLARSBY);
          END IF;
        ELSE
          IF (V_IDCALCPROPDLCOM = 'S') AND (V_PROP > 0) THEN
              IF VC_IDTPPRECO = '1' THEN

                  CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                                P_DTHRPLAVOOPV,VC_DTHRABASTPV,V_QTREAL, V_QTREAL,
                                VC_IDTIPMANUALSP,V_VRPRECOSBY,V_VRPREDOLARSBY);
              ELSE

                 CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                               P_DTHRPLAVOOPV,VC_DTHRABASTPV,V_QTTOTPAX, V_QTTOTAL,
                               VC_IDTIPMANUALSP,V_VRPRECOSBY,V_VRPREDOLARSBY);
              END IF;
          ELSE

             CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                           P_DTHRPLAVOOPV,VC_DTHRABASTPV,V_QTTOTPAX, VC_QTATUAPRODSP,
                           VC_IDTIPMANUALSP,V_VRPRECOSBY,V_VRPREDOLARSBY);
          END IF;

        END IF;

        V_VRPRECOSBY    := NVL(V_VRPRECOSBY,0);
        V_VRPREDOLARSBY := NVL(V_VRPREDOLARSBY,0);

        IF (V_IDCALCPROPDLCOM = 'S') AND (V_PROP > 0) THEN
           IF (V_ORIGINAL IS NULL) THEN
              IF VC_IDTIPMANUALSP = '1' THEN
                 V_VALORSBY := V_VRPRECOSBY;
              ELSE
                 V_VALORSBY := V_VRPREDOLARSBY;
              END IF;
           END IF;

        ELSE
            IF VC_IDTIPMANUALSP = '1' THEN  /*recebe a moeda atual*/
              V_VALORSBY  := ROUND(V_VRPRECOSBY, V_NRDECITOTANF);
            ELSE
              V_VALORSBY  := V_VRPREDOLARSBY;
            END IF;
        END IF;

        /*DEVE ENTRAR AQUI INEPENDENTE SE E COMPLEMENTAR MAS O PARAM DEVE TA MARCADO*/
        IF (V_IDCALCPROPDLCOM = 'S') AND (V_VRCOMPLEMENTAR IS NOT NULL) AND (V_ORIGINAL IS NOT NULL) AND(V_PROP > 0) THEN
          /*Rotina utilizado*/
          V_PRECOSBYUT  := V_VALORSBY*V_UTILIZADA;

          /*Rotina de nao utilizado*/
          IF VC_IDTPPRECO = '2' THEN  /*Total DELIVERY COMPLEMENTAR*/

          -- deve ser a diferenca entre o range do pax+usados e pax+usados+naousados
             BEGIN
             SELECT NVL(SUM(QTATUAPRODSP),0), NVL(SUM(QTATUAPRODSP-(QTSESBYPRSP-QTSESBYUTSP)),0)
               INTO V_QTGERAL, V_QTGERALUSADOS
               FROM SERVPLANOM
              WHERE CDFILIAL     = VC_CDFILIAL     AND
                    CDCIA        = VC_CDCIA        AND
                    NRVOO        = VC_NRVOO        AND
                    CDTRECHO     = VC_CDTRECHO     AND
                    DTHRPLAVOOPV = VC_DTHRPLAVOOPV AND
                    IDSITSERSP  <> '3'             AND
                    NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI AND
                    CDCLASSE = VC_CDCLASSE AND
                    CDCICLO  = VC_CDCICLO;
              EXCEPTION
                 WHEN NO_DATA_FOUND THEN
                    V_QTGERAL := 0;
                    V_QTGERALUSADOS := 0;
              END;

              -- calcula preco para achar o valor do range dos dois tipos

              CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                            P_DTHRPLAVOOPV,VC_DTHRABASTPV,V_QTGERAL,V_QTGERAL,
                            VC_IDTIPMANUALSP,V_VRGERAL,V_VRGERALDOL);


              CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                            P_DTHRPLAVOOPV,VC_DTHRABASTPV,V_QTGERALUSADOS,V_QTGERALUSADOS,
                            VC_IDTIPMANUALSP,V_VRGERALUSADOS,V_VRGERALUSADOSDOL);

              IF VC_IDTIPMANUALSP = '1' THEN --Servicos nacionais
                 V_PRECOSBYNUT := ((V_VRGERAL-V_VRGERALUSADOS)*VC_VRPERSBYNUT)/100;
              ELSE --Servicos internecionais
                 V_PRECOSBYNUT := ((V_VRGERALDOL-V_VRGERALUSADOSDOL)*VC_VRPERSBYNUT)/100;
              END IF;

          ELSE  /*Normal PARA PROPORCAO*/

             /*Busca a quantidade de nao utilizados geral para recalculo proporcional*/
             BEGIN
             SELECT NVL(SUM(QTSESBYPRSP-QTSESBYUTSP),0)
               INTO V_QTNAOUSADOS
               FROM SERVPLANOM
              WHERE CDFILIAL     = VC_CDFILIAL     AND
                    CDCIA        = VC_CDCIA        AND
                    NRVOO        = VC_NRVOO        AND
                    CDTRECHO     = VC_CDTRECHO     AND
                    DTHRPLAVOOPV = VC_DTHRPLAVOOPV AND
                    IDSITSERSP  <> '3'             AND
                    NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI AND
                    CDCLASSE = VC_CDCLASSE AND
                    CDCICLO  = VC_CDCICLO;
              EXCEPTION
                 WHEN NO_DATA_FOUND THEN
                    V_QTNAOUSADOS := 0;
              END;

             /*Busca a quantidade de nao utilizados geral para recalculo proporcional*/
             BEGIN
             SELECT NVL(SUM(VRNUSADO),0)
               INTO V_TOTVRSBYNUT
               FROM SERVPLANOM
              WHERE CDFILIAL     = VC_CDFILIAL     AND
                    CDCIA        = VC_CDCIA        AND
                    NRVOO        = VC_NRVOO        AND
                    CDTRECHO     = VC_CDTRECHO     AND
                    DTHRPLAVOOPV = VC_DTHRPLAVOOPV AND
                    IDSITSERSP  <> '3'             AND
                    NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI AND
                    CDCLASSE = VC_CDCLASSE AND
                    CDCICLO  = VC_CDCICLO AND
                    NRDELIVERY IS NOT NULL;
              EXCEPTION
                 WHEN NO_DATA_FOUND THEN
                    V_TOTVRSBYNUT := 0;
              END;

            IF VC_IDTIPMANUALSP = '1' THEN
                V_PRECOSBYNUT :=  ((((V_VRPRECOSBY/100)*VC_VRPERSBYNUT)*V_QTNAOUSADOS)- V_TOTVRSBYNUT);
            ELSE
                V_PRECOSBYNUT :=  ((((V_VRPREDOLARSBY/100)*VC_VRPERSBYNUT)*V_QTNAOUSADOS)- V_TOTVRSBYNUT);
            END IF;

            V_PRECOSBYUT  :=  V_VALORSBY*VC_QTSESBYUTSP;

          END IF;
        ELSE
          IF (V_IDCALCPROPDLCOM = 'S') AND (V_PROP > 0) THEN /*Se for o primeiro caso tenha o param marcado pois se for o segundo entra no if de cima*/
              /*Rotina de nao utilizado*/
              IF VC_IDTPPRECO = '2' THEN  /*Total PRIMEIRO DELIVERY*/
                -- deve ser a diferenca entre o range do pax+usados e pax+usados+naousados
                 BEGIN
                 SELECT NVL(SUM(QTATUAPRODSP),0), NVL(SUM(QTATUAPRODSP-(QTSESBYPRSP-QTSESBYUTSP)),0)
                   INTO V_QTGERAL, V_QTGERALUSADOS
                   FROM SERVPLANOM
                  WHERE CDFILIAL     = VC_CDFILIAL     AND
                        CDCIA        = VC_CDCIA        AND
                        NRVOO        = VC_NRVOO        AND
                        CDTRECHO     = VC_CDTRECHO     AND
                        DTHRPLAVOOPV = VC_DTHRPLAVOOPV AND
                        IDSITSERSP  <> '3'             AND
                        NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI AND
                        CDCLASSE = VC_CDCLASSE AND
                        CDCICLO  = VC_CDCICLO;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_QTGERAL := 0;
                        V_QTGERALUSADOS := 0;
                  END;
                  -- calcula preco para achar o valor do range dos dois tipos

                  CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                                P_DTHRPLAVOOPV,VC_DTHRABASTPV,V_QTGERAL,V_QTGERAL,
                                VC_IDTIPMANUALSP,V_VRGERAL,V_VRGERALDOL);


                  CALCULA_PRECOM('S',VC_CDFILIAL,VC_CDCIA,VC_NRVOO,VC_CDTRECHO,VC_NRSERVICOCIAFILI,VC_CDCLASSE,VC_CDCICLO,
                                P_DTHRPLAVOOPV,VC_DTHRABASTPV,V_QTGERALUSADOS,V_QTGERALUSADOS,
                                VC_IDTIPMANUALSP,V_VRGERALUSADOS,V_VRGERALUSADOSDOL);

                  IF VC_IDTIPMANUALSP = '1' THEN --Servicos nacionais
                     V_PRECOSBYNUT := ((V_VRGERAL-V_VRGERALUSADOS)*VC_VRPERSBYNUT)/100;
                  ELSE --Servicos internecionais
                     V_PRECOSBYNUT := ((V_VRGERALDOL-V_VRGERALUSADOSDOL)*VC_VRPERSBYNUT)/100;
                  END IF;

              ELSE  /*Normal PRIMEIRO DELIVERY*/
                 -- deve ser percentual calculado do valor unitario do pax
                 V_PRECOSBYNUT := ((V_VALORSBY*VC_VRPERSBYNUT)/100)*(VC_QTSESBYPRSP - VC_QTSESBYUTSP);
                 V_PRECOSBYUT  :=  V_VALORSBY*VC_QTSESBYUTSP;
              END IF;
          ELSE
              V_PRECOSBYUT  := ROUND(ROUND(V_VALORSBY*VC_VRPERSBYUT /100, V_NRDECITOTANF) * VC_QTSESBYUTSP, 2);
              V_PRECOSBYNUT := ROUND(ROUND(V_VALORSBY*VC_VRPERSBYNUT/100, V_NRDECITOTANF) * (VC_QTSESBYPRSP - VC_QTSESBYUTSP), 2);
          END IF;

        END IF;
      ELSE
        V_PRECOSBYUT  := 0;
        V_PRECOSBYNUT := 0;
        V_VALORSBY    := 0;
      END IF;

      V_PRECOSBYUT  := NVL(V_PRECOSBYUT,0);
      V_PRECOSBYNUT := NVL(V_PRECOSBYNUT,0);

--FIM--------standy by------------standy by----------------standy by------------standy by----------------standy by------------standy by----------------

      IF (V_IDCALCPROPDLCOM = 'S') AND (V_VRCOMPLEMENTAR IS NOT NULL) AND (V_ORIGINAL IS NOT NULL) AND(V_PROP > 0) AND(V_PROP > 0) THEN /*se for preco proporcional / TEM COMPELMENTAR / TEM ORIGINAL*/

         IF V_NAOUSADA = 0 THEN
            V_PRECOSBYNUT := 0;
         END IF;
         /* GRAVA O PRECO DO SERVICO E DO STAND-BY NA SERVPLANO */
         IF V_VRPRECORRENTE > 0 THEN  /*REAL*/
            UPDATE SERVPLANOM
             SET   VRPRUDOLSESP = 0,
                   VRPRURESESP  = V_vrExibe,
                   VRPRUSBYSP   = V_VALORSBY,
                   VRNUSADO     = V_PRECOSBYNUT
             WHERE CDFILIAL     = VC_CDFILIAL     AND
                   CDCIA        = VC_CDCIA        AND
                   NRVOO        = VC_NRVOO        AND
                   CDTRECHO     = VC_CDTRECHO     AND
                   DTHRPLAVOOPV = VC_DTHRPLAVOOPV AND
                   NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI AND
                   CDCLASSE = VC_CDCLASSE AND
                   CDCICLO  = VC_CDCICLO AND
                   NRITEMPLANSP = VC_NRITEMPLANSP;

         ELSE    /*DOLAR*/
            UPDATE SERVPLANOM
            SET   VRPRUDOLSESP = V_vrExibe,
                  VRPRURESESP  = 0,
                  VRPRUSBYSP   = V_VALORSBY,
                  VRNUSADO     = V_PRECOSBYNUT
            WHERE CDFILIAL     = VC_CDFILIAL     AND
                  CDCIA        = VC_CDCIA        AND
                  NRVOO        = VC_NRVOO        AND
                  CDTRECHO     = VC_CDTRECHO     AND
                  DTHRPLAVOOPV = VC_DTHRPLAVOOPV AND
                  NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI AND
                  CDCLASSE = VC_CDCLASSE AND
                  CDCICLO  = VC_CDCICLO AND
                  NRITEMPLANSP = VC_NRITEMPLANSP;
         END IF;
      ELSE
         /*SE FOR O PRIMEIRO STANDY BY ZERA ELE POIS ELE JA ESTA INTEGRADO AO PRECO UNITARIO - SO P TOTAL MARCADO*/
         IF (V_IDCALCPROPDLCOM = 'S') AND (VC_IDTPPRECO = '2') AND (V_PROP > 0) THEN
            V_VALORSBY := 0;
            V_PRECOSBYUT := 0;
         END IF;
         /*se nao tiver nao utilizadas deve limpara o campo*/
         IF V_NAOUSADA = 0 THEN
            V_PRECOSBYNUT := 0;
         END IF;

         /* GRAVA O PRECO DO SERVICO E DO STAND-BY NA SERVPLANO */
         UPDATE SERVPLANOM
         SET   VRPRUDOLSESP = V_VRPREDOLAR,
               VRPRURESESP  = V_VRPRECORRENTE,
               VRPRUSBYSP   = V_VALORSBY,
               VRNUSADO     = V_PRECOSBYNUT
         WHERE CDFILIAL     = VC_CDFILIAL     AND
               CDCIA        = VC_CDCIA        AND
               NRVOO        = VC_NRVOO        AND
               CDTRECHO     = VC_CDTRECHO     AND
               DTHRPLAVOOPV = VC_DTHRPLAVOOPV AND
               NRSERVICOCIAFILI = VC_NRSERVICOCIAFILI AND
               CDCLASSE = VC_CDCLASSE AND
               CDCICLO  = VC_CDCICLO AND
               NRITEMPLANSP = VC_NRITEMPLANSP;
      END IF;

      /*DELIVERY COMPLEMENTAR TIVER MARCADO O PARAM */
      IF (V_IDCALCPROPDLCOM = 'S')  THEN
          /*Valor da tabela para primeiro delivery pois ainda nao atualizou p chamar o cursor*/

          IF  (V_VRCOMPLEMENTAR IS NULL)  THEN /*Se for o primeiro delivery deve calcular o range*/
              IF VC_IDTIPMANUALSP = '1' THEN
                 V_TOTALITEM := (VC_QTSERFATSP * V_VRPRECORRENTE) + V_PRECOSBYUT + V_PRECOSBYNUT;
              ELSE
                 V_TOTALITEM := (VC_QTSERFATSP * V_VRPREDOLAR) + V_PRECOSBYUT + V_PRECOSBYNUT;
              END IF;
          ELSE /*se for um complementar deve calcular o valor da tabela*/
              IF (V_ORIGINAL IS NOT NULL) AND (V_PROP > 0) THEN
                IF VC_IDTIPMANUALSP = '1' THEN
                   V_TOTALITEM := (VC_QTSERFATSP * V_vrExibe) + V_PRECOSBYUT + V_PRECOSBYNUT;
                ELSE
                   V_TOTALITEM := (VC_QTSERFATSP * V_vrExibe) + V_PRECOSBYUT + V_PRECOSBYNUT;
                END IF;
              ELSE
                IF VC_IDTIPMANUALSP = '1' THEN
                   V_TOTALITEM := (VC_QTSERFATSP * VC_VRPRURESESP) + V_PRECOSBYUT + V_PRECOSBYNUT;
                ELSE
                   V_TOTALITEM := (VC_QTSERFATSP * VC_VRPRUDOLSESP) + V_PRECOSBYUT + V_PRECOSBYNUT;
                END IF;
              END IF;
          END IF;

      ELSE
         V_TOTALITEM := (VC_QTSERFATSP * V_PRECUNITARIO) + V_PRECOSBYUT + V_PRECOSBYNUT;
      END IF;

      V_TOTALITEM := NVL(V_TOTALITEM,0);
      /*SE TIVER SO STANDY BY NO DELIVERY DEVE SOMAR*/
      IF VC_QTSERFATSP = 0 THEN
         V_TOTALITEM := V_TOTALITEM+NVL(V_vrExibe,0);
      END IF;

      IF VC_IDFATSE = '1' THEN    /* SERVICO FATURADO */
        V_TOTAL := V_TOTAL + nvl(V_TOTALITEM,0);
      END IF;

      V_PESOTOTSERV := V_PESOTOTSERV + VC_VRPESOSESP;

      /* DESCONTO DE NOTA DE DELIVERY */
      V_DESCNOTA := NVL(VC_VRDESCDELTMP,0);

      /* DESCONTO DE SERVICO DA NOTA DE DELIVERY */
      V_DESCSERV := NVL(VC_VRDESCSRVTMP,0);

      /* CALCULO DO DESCONTO DE DESVALORIZACAO */
      IF (VC_IDDESCDESV = '2') AND (VC_IDINCDESDESV = '1') AND
         (VC_VRPERDESCCIA IS NOT NULL) THEN
        V_DESCDESV   := (((V_TOTAL - V_DESCNOTA - V_DESCSERV) * VC_VRPERDESCCIA/100)*100)/100;
      ELSE
        V_DESCDESV   := 0;
      END IF;

      /* CALCULO DO DESCONTO CONTRATUAL FIXO */
      IF (VC_IDDESCFIXO = '2') AND (VC_IDINCDESFIXO = '1') AND
         (VC_VRPEDESCFIXO IS NOT NULL) THEN

        V_DESCFIXO   := ((((V_TOTAL - V_DESCNOTA - V_DESCSERV - V_DESCDESV) * VC_VRPEDESCFIXO/100)*100)/100);
      ELSE
        V_DESCFIXO   := 0;
      END IF;

      /* CALCULO DO DESCONTO CONTRATUAL CONDICIONAL */
      IF P_NRDELIVERY IS NOT NULL THEN
         IF (VC_IDDESCCOND = '2') AND (VC_IDINCDESCOND = '1') AND
            (VC_VRPERDESC IS NOT NULL) THEN
           V_DESCCOND := (((V_TOTAL - V_DESCNOTA - V_DESCSERV - V_DESCDESV - V_DESCFIXO) * VC_VRPERDESC/100)*100)/100;
           IF ((V_DESCCOND - VC_DESCCONDEL) > VC_VRFAIXADESC) THEN
              V_DESCCOND   := VC_DESCCONDEL + VC_VRFAIXADESC;
           END IF;
         ELSE
           V_DESCCOND   := 0;
         END IF;
      ELSE
         IF (VC_IDDESCCOND = '2') AND (VC_IDINCDESCOND = '1') AND
            (VC_VRPERDESC IS NOT NULL) AND (VC_VRFAIXADESC > 0) THEN
           V_DESCCOND := (((V_TOTAL - V_DESCNOTA - V_DESCSERV - V_DESCDESV - V_DESCFIXO) * VC_VRPERDESC/100)*100)/100;
           IF (V_DESCCOND > VC_VRFAIXADESC) THEN
              V_DESCCOND   := VC_VRFAIXADESC;
           END IF;
         ELSE
           V_DESCCOND   := 0;
         END IF;
      END IF;


      IF VC_IDTRIBTXPORT = '1' THEN   /* SE COMPANHIA POSSUI TAXA DE AEROPORTO */

         IF VC_VRALTXPORT IS NOT NULL THEN
            V_VRTAXA := ((NVL(V_TOTAL,0) - NVL(V_DESCNOTA,0) - NVL(V_DESCSERV,0) - NVL(V_DESCDESV,0) - NVL(V_DESCFIXO,0) - NVL(V_DESCCOND,0)) *  NVL(VC_VRALTXPORT,0) )/ 100;
         ELSE
            V_VRTAXA := 0;
         END IF;

      ELSE
         BEGIN
            SELECT IDUTLOCAFATU
            INTO V_IDUTLOCAFATU
            FROM CIAFILI
            WHERE
               (CDFILIAL = VC_CDFILIAL) AND
               (CDCIA    = VC_CDCIA   );

         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               NULL;
         END;

         IF V_IDUTLOCAFATU = 'S' THEN
            SELECT NVL(MAX(VRPEDESTXAER), 0)
            INTO V_VRPEDESTXAER
            FROM LOCALFATUCIA
            WHERE
               (CDFILIAL     = VC_CDFILIAL   ) AND
               (CDCIA        = VC_CDCIA      ) AND
               (CDLOCAFATCIA = P_CDLOCAFATCIA);

            IF V_VRPEDESTXAER IS NOT NULL THEN
               V_VRTAXA := ((V_TOTAL - V_DESCNOTA - V_DESCSERV - V_DESCDESV - V_DESCFIXO - V_DESCCOND) *  V_VRPEDESTXAER ) / 100;
            ELSE
               V_VRTAXA := 0;
            END IF;

         ELSE
            V_VRTAXA := 0;
         END IF;
      END IF;

      P_PEDESCCOND   := VC_VRPERDESC;
      P_VRTOTDESC    := VC_VRTOTDESC;
      P_VRUTIDESC    := VC_VRUTIDESC;
      P_PEDESCFIXO   := VC_VRPEDESCFIXO;
      P_PEDESCDESV   := VC_VRPERDESCCIA;

      P_IDDESCDESV   := VC_IDDESCDESV;
      P_IDDESCCOND   := VC_IDDESCCOND;
      P_IDDESCFIXO   := VC_IDDESCFIXO;
      P_IDINCDESDESV := VC_IDINCDESDESV;
      P_IDINCDESFIXO := VC_IDINCDESFIXO;
      P_IDINCDESCOND := VC_IDINCDESCOND;

      V_IDTIPMANUALSP:= VC_IDTIPMANUALSP;
   END LOOP;

   IF P_NRDELIVERY IS NOT NULL THEN
    IF V_IDTIPMANUALSP = '1' THEN
         V_VRTXAEROR := NVL(V_VRTAXA,0);
         V_VRTXAEROD := 0;
    ELSE
         V_VRTXAEROR := 0;
         V_VRTXAEROD := NVL(V_VRTAXA,0);
      END IF;

      IF V_NRDECITOTANF = 0 THEN
         UPDATE DELIVERYM
         SET VRBACATX   = NVL(V_TOTAL,0),
             VRTXAEROR  = V_VRTXAEROR,
             VRTXAEROD  = V_VRTXAEROD,
             VRDESCDESV = NVL(V_DESCDESV,0),
             VRDESCCOND = NVL(V_DESCCOND,0),
             VRDESCFIXO = NVL(V_DESCFIXO,0)
         WHERE (CDFILIAL   = P_CDFILIAL  ) AND
               (NRDELIVERY = P_NRDELIVERY) AND
               (IDSITDEL   = '1'         );
      ELSE
         UPDATE DELIVERYM
         SET VRBACATX   = ROUND(NVL(V_TOTAL,0), 2),
             VRTXAEROR  = ROUND(V_VRTXAEROR, 2),
             VRTXAEROD  = ROUND(V_VRTXAEROD, 2),
             VRDESCDESV = ROUND(NVL(V_DESCDESV,0), 2),
             VRDESCCOND = ROUND(V_DESCCOND, 2),
             VRDESCFIXO = ROUND(NVL(V_DESCFIXO,0), 2)
         WHERE (CDFILIAL   = P_CDFILIAL  ) AND
               (NRDELIVERY = P_NRDELIVERY) AND
               (IDSITDEL   = '1'         );
      END IF;

      /* Atualiza o valor de desconto ja concedido */
      UPDATE DESPERCIA
      SET
         VRUTIDESC = ROUND(VRUTIDESC + (V_DESCCOND - VC_DESCCONDEL), 2)
      WHERE
         (CDFILIAL = P_CDFILIAL) AND
         (CDCIA    = VC_CDCIA  ) AND
         (VC_DTHRPLAVOOPV BETWEEN DTINIDESC AND DTFIMDESC);


      IF NVL(V_TOTAL,0) <> 0 THEN
         IF (V_IDCALCPROPDLCOM = 'S') AND (V_PROP > 0) THEN
           UPDATE SERVPLANOM
           SET /* Desconto de Delivery */
               VRDESCRESESP = CASE WHEN V_IDTIPMANUALSP = '1' THEN (/*Valor do desconto*/
                               V_DESCNOTA*
                               /*Valor total em reais do servico*/
                               (NVL(QTSERFATSP*VRPRURESESP,0) +
                                NVL(VRPRUSBYSP*QTSESBYUTSP,0)+
                                NVL(VRNUSADO,0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               VRDESCDOSESP = CASE WHEN V_IDTIPMANUALSP = '2' THEN (/*Valor do desconto*/
                               V_DESCNOTA*
                               /*Valor total em dolares do servico*/
                               (NVL(QTSERFATSP*VRPRUDOLSESP,0) +
                                NVL(VRPRUSBYSP*QTSESBYUTSP,0)+
                                NVL(VRNUSADO,0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               /* Desconto Contratual Condicional */
               VRDESCONDRSP = CASE WHEN V_IDTIPMANUALSP = '1' THEN (/*Valor do desconto*/
                               V_DESCCOND*
                               /*Valor total em reais do servico*/
                               (NVL(QTSERFATSP*VRPRURESESP,0) +
                               NVL(VRPRUSBYSP*QTSESBYUTSP,0)+
                                NVL(VRNUSADO,0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               VRDESCONDDSP = CASE WHEN V_IDTIPMANUALSP = '2' THEN (/*Valor do desconto*/
                               V_DESCCOND*
                               /*Valor total em dolares do servico*/
                               (NVL(QTSERFATSP*VRPRUDOLSESP,0) +
                                NVL(VRPRUSBYSP*QTSESBYUTSP,0)+
                                NVL(VRNUSADO,0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               /* Desconto Contratual Fixo */
               VRDESFIXORSP = CASE WHEN V_IDTIPMANUALSP = '1' THEN (/*Valor do desconto*/
                               V_DESCFIXO*
                               /*Valor total em reais do servico*/
                               (NVL(QTSERFATSP*VRPRURESESP,0) +
                                NVL(VRPRUSBYSP*QTSESBYUTSP,0)+
                                NVL(VRNUSADO,0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               VRDESFIXODSP = CASE WHEN V_IDTIPMANUALSP = '2' THEN (/*Valor do desconto*/
                               V_DESCFIXO*
                               /*Valor total em dolares do servico*/
                               (NVL(QTSERFATSP*VRPRUDOLSESP,0) +
                                NVL(VRPRUSBYSP*QTSESBYUTSP,0)+
                                NVL(VRNUSADO,0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               /* Desconto de Desvalorizacao */
               VRDESDESVRSP = CASE WHEN V_IDTIPMANUALSP = '1' THEN (/*Valor do desconto*/
                               V_DESCDESV*
                               /*Valor total em reais do servico*/
                               (NVL(QTSERFATSP*VRPRURESESP,0) +
                                NVL(VRPRUSBYSP*QTSESBYUTSP,0)+
                                NVL(VRNUSADO,0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               VRDESDESVDSP = CASE WHEN V_IDTIPMANUALSP = '2' THEN (/*Valor do desconto*/
                               V_DESCDESV*
                               /*Valor total em dolares do servico*/
                               (NVL(QTSERFATSP*VRPRUDOLSESP,0) +
                                NVL(VRPRUSBYSP*QTSESBYUTSP,0)+
                                NVL(VRNUSADO,0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END
           WHERE (CDFILIAL   = P_CDFILIAL  ) AND
                 (NRDELIVERY = P_NRDELIVERY) AND
                 (IDSITSERSP = '2'         );
         ELSE
           UPDATE SERVPLANOM
           SET /* Desconto de Delivery */
               VRDESCRESESP = CASE WHEN V_IDTIPMANUALSP = '1' THEN (/*Valor do desconto*/
                               V_DESCNOTA*
                               /*Valor total em reais do servico*/
                               (NVL(QTSERFATSP*VRPRURESESP,0) +
                                NVL(VRPRUSBYSP*(VC_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                                NVL(VRPRUSBYSP*(VC_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               VRDESCDOSESP = CASE WHEN V_IDTIPMANUALSP = '2' THEN (/*Valor do desconto*/
                               V_DESCNOTA*
                               /*Valor total em dolares do servico*/
                               (NVL(QTSERFATSP*VRPRUDOLSESP,0) +
                                NVL(VRPRUSBYSP*(VC_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                                NVL(VRPRUSBYSP*(VC_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               /* Desconto Contratual Condicional */
               VRDESCONDRSP = CASE WHEN V_IDTIPMANUALSP = '1' THEN (/*Valor do desconto*/
                               V_DESCCOND*
                               /*Valor total em reais do servico*/
                               (NVL(QTSERFATSP*VRPRURESESP,0) +
                                NVL(VRPRUSBYSP*(VC_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                                NVL(VRPRUSBYSP*(VC_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               VRDESCONDDSP = CASE WHEN V_IDTIPMANUALSP = '2' THEN (/*Valor do desconto*/
                               V_DESCCOND*
                               /*Valor total em dolares do servico*/
                               (NVL(QTSERFATSP*VRPRUDOLSESP,0) +
                                NVL(VRPRUSBYSP*(VC_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                                NVL(VRPRUSBYSP*(VC_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               /* Desconto Contratual Fixo */
               VRDESFIXORSP = CASE WHEN V_IDTIPMANUALSP = '1' THEN (/*Valor do desconto*/
                               V_DESCFIXO*
                               /*Valor total em reais do servico*/
                               (NVL(QTSERFATSP*VRPRURESESP,0) +
                                NVL(VRPRUSBYSP*(VC_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                                NVL(VRPRUSBYSP*(VC_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               VRDESFIXODSP = CASE WHEN V_IDTIPMANUALSP = '2' THEN (/*Valor do desconto*/
                               V_DESCFIXO*
                               /*Valor total em dolares do servico*/
                               (NVL(QTSERFATSP*VRPRUDOLSESP,0) +
                                NVL(VRPRUSBYSP*(VC_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                                NVL(VRPRUSBYSP*(VC_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               /* Desconto de Desvalorizacao */
               VRDESDESVRSP = CASE WHEN V_IDTIPMANUALSP = '1' THEN (/*Valor do desconto*/
                               V_DESCDESV*
                               /*Valor total em reais do servico*/
                               (NVL(QTSERFATSP*VRPRURESESP,0) +
                                NVL(VRPRUSBYSP*(VC_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                                NVL(VRPRUSBYSP*(VC_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END,
               VRDESDESVDSP = CASE WHEN V_IDTIPMANUALSP = '2' THEN (/*Valor do desconto*/
                               V_DESCDESV*
                               /*Valor total em dolares do servico*/
                               (NVL(QTSERFATSP*VRPRUDOLSESP,0) +
                                NVL(VRPRUSBYSP*(VC_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                                NVL(VRPRUSBYSP*(VC_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                               /*Valor total da delivery*/
                               V_TOTAL) 
                              ELSE 0.00 END
           WHERE (CDFILIAL   = P_CDFILIAL  ) AND
                 (NRDELIVERY = P_NRDELIVERY) AND
                 (IDSITSERSP = '2'         );
         END IF;
      ELSE
         UPDATE SERVPLANOM
         SET VRDESCRESESP = NULL,
             VRDESCDOSESP = NULL
         WHERE (CDFILIAL   = P_CDFILIAL  ) AND
               (NRDELIVERY = P_NRDELIVERY) AND
               (IDSITSERSP = '2'         );
      END IF;
   END IF;
   
   P_PESOTOTSERV    := ROUND(NVL(V_PESOTOTSERV   ,0), 2);
   P_TOTALGERALSERV := ROUND(NVL(V_TOTAL,0), 2);
   P_VRTAXA         := ROUND(NVL(V_VRTAXA        ,0), 2);
   P_DESCNOTA       := ROUND(NVL(V_DESCNOTA      ,0), 2);
   P_DESCSERV       := ROUND(NVL(V_DESCSERV      ,0), 2);
   P_DESCDESV       := ROUND(NVL(V_DESCDESV      ,0), 2);
   P_DESCFIXO       := ROUND(NVL(V_DESCFIXO      ,0), 2);
   P_DESCCOND       := ROUND(NVL(V_DESCCOND      ,0), 2);
   P_TOTSBYUT       := ROUND(NVL(V_PRECOSBYUT    ,0), 2);
   P_TOTSBYNUT      := ROUND(NVL(V_PRECOSBYNUT   ,0), 2);  
   
END;
/