CREATE OR REPLACE PROCEDURE CANCELA_DELIVERYM(
  P_CDFILIAL     IN VARCHAR2, 
  P_CDCIA        IN VARCHAR2,
  P_NRVOO        IN VARCHAR2,
  P_CDTRECHO     IN VARCHAR2,
  P_DTHRPLAVOOPV IN DATE,
  P_VRDESCCOND   IN FLOAT,
  P_NRDELIVERY   IN VARCHAR2,
  P_CDOPCANCDELI IN VARCHAR2)
AS

BEGIN
   UPDATE SERVPLANOM
   SET NRDELIVERY = NULL,
       IDSITSERSP = '1'
   WHERE (CDFILIAL   = P_CDFILIAL  ) AND
         (NRDELIVERY = P_NRDELIVERY);

   UPDATE DELIVERYM
   SET    IDSITDEL = '3',
          CDOPCANCDELI = P_CDOPCANCDELI 
   WHERE (CDFILIAL   = P_CDFILIAL  ) AND
         (NRDELIVERY = P_NRDELIVERY);

   IF P_VRDESCCOND > 0 THEN
      UPDATE DESPERCIA
      SET VRUTIDESC = VRUTIDESC - P_VRDESCCOND
      WHERE (CDFILIAL = P_CDFILIAL) AND
            (CDCIA    = P_CDCIA   ) AND
            (P_DTHRPLAVOOPV BETWEEN DTINIDESC AND DTFIMDESC);
   END IF;

   UPDATE PLANOVOOM
   SET    IDSITABASPV = '1'
   WHERE (CDFILIAL     = P_CDFILIAL    ) AND
         (CDCIA        = P_CDCIA       ) AND
         (NRVOO        = P_NRVOO       ) AND
         (CDTRECHO     = P_CDTRECHO    ) AND
         (DTHRPLAVOOPV = P_DTHRPLAVOOPV) AND
         (IDSITABASPV  = '2'           );
END;
/