CREATE OR REPLACE PROCEDURE DEFINE_DELIVERYM(
  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_VRPETXAERO   IN DELIVERYM.VRPETXAERO%TYPE,
  P_VRBACATX     IN DELIVERYM.VRBACATX%TYPE,
  P_DTEMISDEL    IN DELIVERYM.DTEMISDEL%TYPE,
  P_VRCOTDOLAR   IN DELIVERYM.VRCOTDOLAR%TYPE,
  P_VRTXAEROR    IN DELIVERYM.VRTXAEROR%TYPE,
  P_VRTXAEROD    IN DELIVERYM.VRTXAEROD%TYPE,
  P_VRDESCDEL    IN DELIVERYM.VRDESCDEL%TYPE,
  P_TXDESCDEL    IN DELIVERYM.TXDESCDEL%TYPE,
  P_VRDESCDESV   IN DELIVERYM.VRDESCDESV%TYPE,
  P_VRDESCCOND   IN DELIVERYM.VRDESCCOND%TYPE,
  P_VRDESCFIXO   IN DELIVERYM.VRDESCFIXO%TYPE,
  P_IDTIPNACDEL  IN DELIVERYM.IDTIPNACDEL%TYPE,
  P_HRPREVDECPV  IN PLANOVOOM.HRPREVDECPV%TYPE,
  P_CDLOCAFATCIA IN LOCALFATUCIA.CDLOCAFATCIA%TYPE,
  P_NMRESPDEL    IN DELIVERYM.NMRESPDEL %TYPE,
--  P_CDLOCALDELIV IN DELIVERYM.CDLOCALDELIV%TYPE,
  P_NRSEQDELIVER IN  SERVPLANOM.NRSEQDELIVER%TYPE,
  P_PXNRDELIVERY IN DELIVERYM.NRDELIVERY%TYPE DEFAULT NULL,
  P_CDOPERADOR IN VARCHAR2,
  P_NRDELIVERY  OUT DELIVERYM.NRDELIVERY%TYPE,
  P_NRORG IN NUMBER DEFAULT 1)
AS
  V_NRDELIVERY    DELIVERYM.NRDELIVERY%TYPE;
  V_CONTDELIV     NUMBER;
  V_CONT          NUMBER;
  V_TXOBSDELTMP   OBSDELIVTMPM.TXOBSDELTMP%TYPE;
  V_IDSTKDTABAPV  PLANOVOOM.IDSTKDTABAPV%TYPE;
  V_VRPEDESTXAER  LOCALFATUCIA.VRPEDESTXAER%TYPE;
  
  V_VRPERSBYUT    CIAFILI.VRPERSBYUT%TYPE;
  V_VRPERSBYNUT   CIAFILI.VRPERSBYNUT%TYPE;  
  V_VRALTXPORT FLOAT; 
  V_IDCALCPROPDLCOM VARCHAR2(1);
  V_IDDESCDESV VARCHAR2(1);
  V_IDDESCCOND VARCHAR2(1);
  V_IDDESCFIXO VARCHAR2(1);
  V_VRPEDESCFIXO FLOAT;
  V_VRPERDESCCIA FLOAT;
  V_VRPERDESC FLOAT;

  V_IDTRIBTXPORT  CIAFILI.IDTRIBTXPORT%TYPE;
  V_IDUTSEQNUMRE  PARAMCOMIS.IDUTSEQNUMRE%TYPE;
  V_NRDELEMISS    DELIVERYM.NRDELEMISS%TYPE;
  V_IDUTLOCAFATU  CIAFILI.IDUTLOCAFATU%TYPE;
--  V_NUMERACAONOVA VARCHAR2(20);
  V_VRDESCONTOPROP FLOAT;
  V_EXISTE_DELIVERY NUMBER;
  V_IDDELIVCOMPL VARCHAR2(1);
  V_DTEMISDEL DATE;

   PROCEDURE DEFINE_PURCHASEORDER(P_NRDELIVERY_PROCESSO IN VARCHAR2)
   AS
   V_CDPURCHASEORDER VARCHAR2(20);
   V_NRTABPRECO VARCHAR2(20);
   BEGIN
      SELECT MAX(CDPURCHASEORDER)
        INTO V_CDPURCHASEORDER
        FROM PURCHASEORDER
       WHERE CDFILIAL = P_CDFILIAL
         AND CDCIA = P_CDCIA
         AND P_DTHRPLAVOOPV BETWEEN DTINIVIGENCIA AND DTFIMVIGENCIA;

      IF V_CDPURCHASEORDER IS NOT NULL THEN
         UPDATE DELIVERYM
            SET CDPURCHASEORDER = V_CDPURCHASEORDER
          WHERE CDFILIAL = P_CDFILIAL
            AND NRDELIVERY = P_NRDELIVERY_PROCESSO;

         SELECT MAX(NRTABPRECO)
           INTO V_NRTABPRECO
           FROM TABPRECOSEM
          WHERE CDFILIAL = P_CDFILIAL
            AND CDCIA = P_CDCIA            
            AND (P_DTHRPLAVOOPV BETWEEN DTINIVIGTAB AND DTFIMVIGTAB);

         IF V_NRTABPRECO IS NULL THEN 
            RETURN;
         END IF;

         FOR CUR_SERV IN
         (
         SELECT S.NRSERVICOCIAFILI, S.CDCLASSE, S.CDCICLO, P.CDSEQPURCHASEORDER
           FROM SERVPLANOM S
                INNER JOIN PRECOSECIAM P ON P.CDFILIAL = S.CDFILIAL AND
                                           P.CDCIA = S.CDCIA AND
                                           P.NRSERVICOCIAFILI = S.NRSERVICOCIAFILI AND
                                           P.CDCLASSE = S.CDCLASSE AND
                                           P.CDCICLO = S.CDCICLO
          WHERE S.CDFILIAL = P_CDFILIAL
            AND S.CDCIA = P_CDCIA
            AND S.NRVOO = P_NRVOO
            AND S.CDTRECHO = P_CDTRECHO
            AND S.DTHRPLAVOOPV = P_DTHRPLAVOOPV
            AND P.CDFILIAL = P_CDFILIAL
            AND P.CDCIA = P_CDCIA
            AND S.NRDELIVERY = P_NRDELIVERY_PROCESSO
            AND P.NRTABPRECO = V_NRTABPRECO
            AND P_DTHRPLAVOOPV BETWEEN P.DTINIVIGSE  AND P.DTFIMVIGSE         
         ) LOOP
           UPDATE SERVPLANOM
              SET CDPURCHASEORDER = V_CDPURCHASEORDER,
                  CDSEQPURCHASEORDER = CUR_SERV.CDSEQPURCHASEORDER
            WHERE CDFILIAL = P_CDFILIAL
              AND CDCIA = P_CDCIA
              AND NRVOO = P_NRVOO
              AND CDTRECHO = P_CDTRECHO
              AND DTHRPLAVOOPV = P_DTHRPLAVOOPV
              AND NRSERVICOCIAFILI = CUR_SERV.NRSERVICOCIAFILI
              AND CDCLASSE = CUR_SERV.CDCLASSE
              AND CDCICLO = CUR_SERV.CDCICLO;
         END LOOP;

      END IF;

   END;

BEGIN
   V_DTEMISDEL := P_DTHRPLAVOOPV;

   SELECT MAX(NVL(IDUTSEQNUMRE, 'N')), MAX(IDCALCPROPDLCOM)
   INTO V_IDUTSEQNUMRE, V_IDCALCPROPDLCOM
   FROM
      PARAMCOMIS
   WHERE
      CDFILIAL = P_CDFILIAL;

   V_NRDELEMISS := '';

   IF V_IDUTSEQNUMRE = 'S' THEN
      SELECT NVL(MAX(NRULTDELIVERY),0)+1
      INTO V_NRDELIVERY
      FROM PARAMCOMIS
      WHERE CDFILIAL = P_CDFILIAL;
      IF P_PXNRDELIVERY IS NOT NULL THEN
         V_NRDELEMISS := P_PXNRDELIVERY;
      END IF;
   ELSE
      IF P_PXNRDELIVERY IS NULL THEN
         SELECT NVL(MAX(NRULTDELIVERY),0)+1
         INTO V_NRDELIVERY
         FROM PARAMCOMIS
         WHERE CDFILIAL = P_CDFILIAL;
      ELSE
         V_NRDELIVERY := P_PXNRDELIVERY;
      END IF;
   END IF;

   V_VRPEDESTXAER := NVL(P_VRPETXAERO, 0);

   SELECT MAX(IDTRIBTXPORT), MAX(IDUTLOCAFATU),
          MAX(NVL(VRPERSBYUT, 0.00)),MAX(NVL(VRPERSBYNUT, 0.0)), MAX(NVL(VRALTXPORT, 0.00)), 
          MAX(NVL(VRPEDESCFIXO,0.00)),
          MAX(IDDESCDESV), MAX(IDDESCCOND), MAX(IDDESCFIXO)
   INTO V_IDTRIBTXPORT, V_IDUTLOCAFATU, 
        V_VRPERSBYUT,V_VRPERSBYNUT, V_VRALTXPORT, 
        V_VRPEDESCFIXO,
        V_IDDESCDESV, V_IDDESCCOND, V_IDDESCFIXO
   FROM CIAFILI
   WHERE (CDFILIAL = P_CDFILIAL) AND
         (CDCIA    = P_CDCIA   );

   /* Busca o desconto desvalorizacao */
   SELECT MAX(NVL(VRPERDESCCIA,0))
     INTO V_VRPERDESCCIA
     FROM DESCMENSCIA
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = P_CDCIA
      AND ((TO_CHAR(DTDESCCIA,'MM/YYYY') = TO_CHAR(P_DTHRPLAVOOPV,'MM/YYYY')));
   V_VRPERDESCCIA := NVL(V_VRPERDESCCIA, 0.00);
   
    /* Busca o desconto proporcional */
   SELECT MAX(NVL(VRPERDESC,0))
     INTO V_VRPERDESC
     FROM DESPERCIA
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = P_CDCIA
      AND (P_DTHRPLAVOOPV BETWEEN DTINIDESC AND DTFIMDESC);
   V_VRPERDESC := NVL(V_VRPERDESC, 0.00);

   IF V_IDTRIBTXPORT <> '1' THEN
      IF V_IDUTLOCAFATU = 'S' THEN
         SELECT NVL(MAX(VRPEDESTXAER), 0)
         INTO V_VRPEDESTXAER
         FROM LOCALFATUCIA
         WHERE(CDFILIAL     = P_CDFILIAL    ) AND
              (CDCIA        = P_CDCIA       ) AND
              (CDLOCAFATCIA = P_CDLOCAFATCIA);
      END IF;
   END IF;

   SELECT COUNT(1)
     INTO V_EXISTE_DELIVERY
     FROM DELIVERYM
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = P_CDCIA
      AND NRVOO = P_NRVOO
      AND CDTRECHO = P_CDTRECHO
      AND DTHRPLAVOOPV = P_DTHRPLAVOOPV
      AND IDSITDEL <> '3';   
  
   V_IDDELIVCOMPL := CASE WHEN V_EXISTE_DELIVERY > 0 THEN 'S' ELSE 'N' END;

   INSERT INTO DELIVERYM
   (CDFILIAL, NRDELIVERY, CDCIA, NRVOO, CDTRECHO, DTHRPLAVOOPV, VRBACATX,
    VRPETXAERO, HREMISDEL, IDSITDEL, DTEMISDEL, VRCOTDOLAR, VRTXAEROR, VRTXAEROD,
    VRDESCDEL, TXDESCDEL,  VRDESCDESV, VRDESCCOND, VRDESCFIXO,
    IDTIPNACDEL, NMRESPDEL, NRDELEMISS, IDDELIVCOMPL,
    VRPERSBYUT, VRPERSBYNUT, VRALTXPORT,
    VRPERDESCFIXO, VRPERDESCDESV, VRPERDESCCOND,
    IDCALCPROPDLCOM, IDDESCDESV, IDDESCCOND, IDDESCFIXO,
    NRORG, NRORGINCLUSAO, NRORGULTATU,
    DTINCLUSAO, DTULTATU,
    CDOPERINCLUSAO, CDOPERULTATU)
   VALUES
   (P_CDFILIAL, V_NRDELIVERY, P_CDCIA, P_NRVOO, P_CDTRECHO, P_DTHRPLAVOOPV, P_VRBACATX,
    V_VRPEDESTXAER, TO_CHAR(SYSDATE,'HH24MI'), '1', V_DTEMISDEL, P_VRCOTDOLAR, P_VRTXAEROR,
    P_VRTXAEROD, P_VRDESCDEL, P_TXDESCDEL, P_VRDESCDESV, P_VRDESCCOND, P_VRDESCFIXO,
    P_IDTIPNACDEL, P_NMRESPDEL, V_NRDELEMISS, V_IDDELIVCOMPL,
    V_VRPERSBYUT, V_VRPERSBYNUT, V_VRALTXPORT,
    V_VRPEDESCFIXO, V_VRPERDESCCIA, V_VRPERDESC,
    V_IDCALCPROPDLCOM, V_IDDESCDESV, V_IDDESCCOND, V_IDDESCFIXO,    
    P_NRORG, P_NRORG, P_NRORG,
    SYSDATE, SYSDATE,
    P_CDOPERADOR, P_CDOPERADOR);

   SELECT COUNT(*)
   INTO V_CONT
   FROM OBSDELIVTMPM
   WHERE (CDFILIAL     = P_CDFILIAL    ) AND
         (CDCIATMP     = P_CDCIA       ) AND
         (NRVOOTMP     = P_NRVOO       ) AND
         (CDTRECHOTMP  = P_CDTRECHO    ) AND
         (DTHRPLAVOTMP = P_DTHRPLAVOOPV) AND
         (CDAGRDECOTMP = P_CDAGRDECO   );

   IF V_CONT > 0 THEN

      SELECT TXOBSDELTMP, VRDESCONTOPROP
      INTO V_TXOBSDELTMP, V_VRDESCONTOPROP
      FROM OBSDELIVTMPM
      WHERE (CDFILIAL     = P_CDFILIAL    ) AND
            (CDCIATMP     = P_CDCIA       ) AND
            (NRVOOTMP     = P_NRVOO       ) AND
            (CDTRECHOTMP  = P_CDTRECHO    ) AND
            (DTHRPLAVOTMP = P_DTHRPLAVOOPV) AND
            (CDAGRDECOTMP = P_CDAGRDECO   );

      INSERT INTO OBSDELIVERYM
      (CDFILIAL, NRDELIVERY, TXOBSDEL, VRDESCONTOPROP,
       NRORG, NRORGINCLUSAO, NRORGULTATU,
       DTINCLUSAO, DTULTATU,
       CDOPERINCLUSAO, CDOPERULTATU)
      VALUES
      (P_CDFILIAL, V_NRDELIVERY, V_TXOBSDELTMP, V_VRDESCONTOPROP,
       P_NRORG, P_NRORG, P_NRORG,
       SYSDATE, SYSDATE,
       P_CDOPERADOR, P_CDOPERADOR);

   END IF;

   DELETE FROM OBSDELIVTMPM
   WHERE (CDFILIAL     = P_CDFILIAL    ) AND
         (CDCIATMP     = P_CDCIA       ) AND
         (NRVOOTMP     = P_NRVOO       ) AND
         (CDTRECHOTMP  = P_CDTRECHO    ) AND
         (DTHRPLAVOTMP = P_DTHRPLAVOOPV) AND
         (CDAGRDECOTMP = P_CDAGRDECO   );

   UPDATE PARAMCOMIS
      SET NRULTDELIVERY = V_NRDELIVERY,
          NRORGULTATU = P_NRORG,
          CDOPERULTATU = P_CDOPERADOR,
          DTULTATU = SYSDATE
   WHERE CDFILIAL = P_CDFILIAL;

  /*   IF V_IDUTSEQNUMRE = 'S' THEN
      V_NUMERACAONOVA := LPAD(TO_CHAR(V_NRDELEMISS),20,'0');
      UPDATE NOVOCODIGO
      SET NRSEQUENCIAL = V_NUMERACAONOVA
      WHERE CDCONTADOR = 'DELIVERYM'||P_CDFILIAL||P_CDLOCALDELIV;
   END IF; */   

   UPDATE SERVPLANOM
   SET NRDELIVERY   = V_NRDELIVERY,
       IDSITSERSP   = '2',
       /* Desconto de Delivery */
       VRDESCRESESP = CASE WHEN P_IDTIPNACDEL = '1' THEN DECODE(P_VRBACATX,0,0,
                      (/*Valor do desconto*/
                       P_VRDESCDEL*
                       /*Valor total em reais do servio*/
                       (NVL(QTSERFATSP*VRPRURESESP,0) +
                        NVL(VRPRUSBYSP*(V_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                        NVL(VRPRUSBYSP*(V_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                       /*Valor total da delivery*/
                       P_VRBACATX)) ELSE 0.00 END,
       VRDESCDOSESP = CASE WHEN P_IDTIPNACDEL = '2' THEN DECODE(P_VRBACATX,0,0,
                      (/*Valor do desconto*/
                       P_VRDESCDEL*
                       /*Valor total em dlares do servio*/
                       (NVL(QTSERFATSP*VRPRUDOLSESP,0) +
                        NVL(VRPRUSBYSP*(V_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                        NVL(VRPRUSBYSP*(V_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                       /*Valor total da delivery*/
                       P_VRBACATX)) ELSE 0.00 END,
       /* Desconto Contratual Condicional */
       VRDESCONDRSP = CASE WHEN P_IDTIPNACDEL = '1' THEN DECODE(P_VRBACATX,0,0,
                      (/*Valor do desconto*/
                       P_VRDESCCOND*
                       /*Valor total em reais do servio*/
                       (NVL(QTSERFATSP*VRPRURESESP,0) +
                        NVL(VRPRUSBYSP*(V_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                        NVL(VRPRUSBYSP*(V_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                       /*Valor total da delivery*/
                       P_VRBACATX)) ELSE 0.00 END,
       VRDESCONDDSP = CASE WHEN P_IDTIPNACDEL = '2' THEN DECODE(P_VRBACATX,0,0,
                      (/*Valor do desconto*/
                       P_VRDESCCOND*
                       /*Valor total em dlares do servio*/
                       (NVL(QTSERFATSP*VRPRUDOLSESP,0) +
                        NVL(VRPRUSBYSP*(V_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                        NVL(VRPRUSBYSP*(V_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                       /*Valor total da delivery*/
                       P_VRBACATX)) ELSE 0.00 END,
       /* Desconto Contratual Fixo */
       VRDESFIXORSP = CASE WHEN P_IDTIPNACDEL = '1' THEN DECODE(P_VRBACATX,0,0,
                      (/*Valor do desconto*/
                       P_VRDESCFIXO*
                       /*Valor total em reais do servio*/
                       (NVL(QTSERFATSP*VRPRURESESP,0) +
                        NVL(VRPRUSBYSP*(V_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                        NVL(VRPRUSBYSP*(V_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                       /*Valor total da delivery*/
                       P_VRBACATX)) ELSE 0.00 END,
       VRDESFIXODSP = CASE WHEN P_IDTIPNACDEL = '2' THEN DECODE(P_VRBACATX,0,0,
                      (/*Valor do desconto*/
                       P_VRDESCFIXO*
                       /*Valor total em dlares do servio*/
                       (NVL(QTSERFATSP*VRPRUDOLSESP,0) +
                        NVL(VRPRUSBYSP*(V_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                        NVL(VRPRUSBYSP*(V_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                       /*Valor total da delivery*/
                       P_VRBACATX)) ELSE 0.00 END,
       /* Desconto de Desvalorizao */
       VRDESDESVRSP = CASE WHEN P_IDTIPNACDEL = '1' THEN DECODE(P_VRBACATX,0,0,
                      (/*Valor do desconto*/
                       P_VRDESCDESV*
                       /*Valor total em reais do servio*/
                       (NVL(QTSERFATSP*VRPRURESESP,0) +
                        NVL(VRPRUSBYSP*(V_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                        NVL(VRPRUSBYSP*(V_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                       /*Valor total da delivery*/
                       P_VRBACATX)) ELSE 0.00 END,
       VRDESDESVDSP = CASE WHEN P_IDTIPNACDEL = '2' THEN DECODE(P_VRBACATX,0,0,
                      (/*Valor do desconto*/
                       P_VRDESCDESV*
                       /*Valor total em dlares do servio*/
                       (NVL(QTSERFATSP*VRPRUDOLSESP,0) +
                        NVL(VRPRUSBYSP*(V_VRPERSBYUT/100)*QTSESBYUTSP,0)+
                        NVL(VRPRUSBYSP*(V_VRPERSBYNUT/100)*(QTSESBYPRSP-QTSESBYUTSP),0))/
                       /*Valor total da delivery*/
                       P_VRBACATX)) ELSE 0.00 END,       
       NRORGULTATU = P_NRORG,
       CDOPERULTATU = P_CDOPERADOR,
       DTULTATU = SYSDATE
   WHERE (CDFILIAL     = P_CDFILIAL    ) AND
         (CDCIA        = P_CDCIA       ) AND
         (NRVOO        = P_NRVOO       ) AND
         (CDTRECHO     = P_CDTRECHO    ) AND
         (DTHRPLAVOOPV = P_DTHRPLAVOOPV) AND
         (CDAGRDECO    = P_CDAGRDECO   ) AND
         (NRDELIVERY   IS NULL         ) AND
         (IDSITSERSP   = '1'           ) AND
         (NVL(NRSEQDELIVER, 0) = P_NRSEQDELIVER);

   SELECT COUNT(*)
   INTO V_CONTDELIV
   FROM SERVPLANOM
   WHERE (CDFILIAL      = P_CDFILIAL    ) AND
         (CDCIA         = P_CDCIA       ) AND
         (NRVOO         = P_NRVOO       ) AND
         (CDTRECHO      = P_CDTRECHO    ) AND
         (DTHRPLAVOOPV  = P_DTHRPLAVOOPV) AND
         (NRDELIVERY      IS NULL       ) AND
         (IDSITSERSP   <> 3             );

   IF V_CONTDELIV = 0 THEN
    UPDATE PLANOVOOM
      SET    IDSITABASPV = '2',             
             NRORGULTATU = P_NRORG,
             CDOPERULTATU = P_CDOPERADOR,
             DTULTATU = SYSDATE
      WHERE (CDFILIAL     = P_CDFILIAL    ) AND
            (CDCIA        = P_CDCIA       ) AND
            (NRVOO        = P_NRVOO       ) AND
            (CDTRECHO     = P_CDTRECHO    ) AND
            (DTHRPLAVOOPV = P_DTHRPLAVOOPV) AND
            (IDSITABASPV  = '1'           );
   END IF;

   /* UPDATADA A SITUACAO DO KDT */
   IF P_DTHRPLAVOOPV < SYSDATE THEN
     V_IDSTKDTABAPV := '2';   /* ATRASADO */
   ELSIF P_DTHRPLAVOOPV > SYSDATE THEN
     V_IDSTKDTABAPV := '1';   /* NO HORARIO */
   ELSIF P_DTHRPLAVOOPV = SYSDATE THEN
     IF P_HRPREVDECPV > TO_CHAR(SYSDATE,'HH24MM') THEN
       V_IDSTKDTABAPV := '1'; /* NO HORARIO */
     ELSE
       V_IDSTKDTABAPV := '2'; /* ATRASADO */
     END IF;
   ELSE
     V_IDSTKDTABAPV := NULL;
   END IF;

   UPDATE PLANOVOOM
   SET IDSTKDTABAPV = V_IDSTKDTABAPV,       
       NRORGULTATU = P_NRORG,
       CDOPERULTATU = P_CDOPERADOR,
       DTULTATU = SYSDATE
   WHERE (CDFILIAL     = P_CDFILIAL    ) AND
         (CDCIA        = P_CDCIA       ) AND
         (NRVOO        = P_NRVOO       ) AND
         (CDTRECHO     = P_CDTRECHO    ) AND
         (DTHRPLAVOOPV = P_DTHRPLAVOOPV);

   /* UPDATA O VALOR DESCONTADO DO DESCONTO CONDICIONAL */
   IF P_VRDESCCOND > 0 THEN
     UPDATE DESPERCIA
     SET    VRUTIDESC = VRUTIDESC + P_VRDESCCOND,
            NRORGULTATU = P_NRORG,
            CDOPERULTATU = P_CDOPERADOR,
            DTULTATU = SYSDATE
     WHERE  (CDFILIAL = P_CDFILIAL) AND
            (CDCIA    = P_CDCIA   ) AND
            (P_DTHRPLAVOOPV BETWEEN DTINIDESC AND DTFIMDESC);
   END IF;
   P_NRDELIVERY := V_NRDELIVERY;

   DEFINE_PURCHASEORDER(V_NRDELIVERY);
END;
/