CREATE OR REPLACE PROCEDURE EXCLUI_PLANOVOOM(
P_CDFILIAL IN VARCHAR2,
P_CDCIA IN VARCHAR2,
P_NRVOO IN VARCHAR2,
P_CDTRECHO IN VARCHAR2,
P_DTHRPLAVOOPV IN DATE,
P_NRORG IN NUMBER DEFAULT 1
)
AS
   V_NRVOORETROBILL VARCHAR2(5);
BEGIN

   VALIDA_NF_DELIVERYS_PLANOVOOM(P_CDFILIAL, P_CDCIA, P_NRVOO, P_CDTRECHO,
                                   P_DTHRPLAVOOPV, 'TODAS', 'S', P_NRORG);

   /*----RETROBILL >>>>*/
   /*Verifica se o voo e retrobill, apaga os campos de referencia do plano de voo no retrobill*/
   SELECT MAX(NRVOORETROBILL)
     INTO V_NRVOORETROBILL
     FROM CIAFILI
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = P_CDCIA;
   
   IF P_NRVOO = V_NRVOORETROBILL THEN
      UPDATE RETROBILL
         SET IDSITRETROBILL = 'P',
             NRVOO = NULL,
             CDTRECHO = NULL,
             DTHRPLAVOOPV = NULL
       WHERE CDFILIAL = P_CDFILIAL
         AND CDCIA = P_CDCIA
         AND NRVOO = P_NRVOO
         AND CDTRECHO = P_CDTRECHO
         AND DTHRPLAVOOPV = P_DTHRPLAVOOPV;
             
   END IF;
   /*<<<< RETROBILL----*/

   DELETE FROM CONEXAOSUBGRUPO
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = P_CDCIA
      AND NRVOO = P_NRVOO
      AND CDTRECHO = P_CDTRECHO
      AND DTHRPLAVOOPV = P_DTHRPLAVOOPV;

   DELETE FROM SERVPLANOM
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = P_CDCIA
      AND NRVOO = P_NRVOO
      AND CDTRECHO = P_CDTRECHO
      AND DTHRPLAVOOPV = P_DTHRPLAVOOPV;
  
   DELETE FROM PREVISAOPAXM
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = P_CDCIA
      AND NRVOO = P_NRVOO
      AND CDTRECHO = P_CDTRECHO
      AND DTHRPLAVOOPV = P_DTHRPLAVOOPV;
  
   DELETE FROM PLANOVOOM
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = P_CDCIA
      AND NRVOO = P_NRVOO
      AND CDTRECHO = P_CDTRECHO
      AND DTHRPLAVOOPV = P_DTHRPLAVOOPV;
END;
/