CREATE OR REPLACE PROCEDURE COPIA_ITEMFAT_VOO
(
P_CDFILIAL IN VARCHAR2,
P_CDCIA IN VARCHAR2,
P_NRVOOORI IN VARCHAR2,
P_CDTRECHOORI IN VARCHAR2,
P_NRVOODES IN VARCHAR2,
P_CDTRECHODES IN VARCHAR2,
P_CDOPERADOR IN VARCHAR2,
P_NRORG IN NUMBER
)
AS
BEGIN
   IF P_NRVOOORI = P_NRVOODES AND
      P_CDTRECHOORI = P_CDTRECHODES THEN
      RETURN;
   END IF;

   DELETE FROM PROPSERVICOM 
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = P_CDCIA
      AND NRVOO = P_NRVOODES
      AND CDTRECHO = P_CDTRECHODES;

   DELETE FROM PADVOOTREM
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = P_CDCIA
      AND NRVOO = P_NRVOODES
      AND CDTRECHO = P_CDTRECHODES;
   
   INSERT INTO PADVOOTREM
   (CDFILIAL, CDCIA, NRVOO, CDTRECHO, 
   	NRSERVICOCIAFILI, CDCLASSE, CDCICLO,
   	QTSERVOO, QTSESTBYVOO,CDSERVICO, CDARVSERV,
   	VRLOADSEVOO, NRPROPAXVOO, IDPROPSEVOO,
   	NRORG, NRORGULTATU, NRORGINCLUSAO,
   	DTINCLUSAO, DTULTATU,
   	CDOPERINCLUSAO, CDOPERULTATU, IDATIVO)
   SELECT P_CDFILIAL, P_CDCIA, P_NRVOODES, P_CDTRECHODES,
          NRSERVICOCIAFILI, CDCLASSE, CDCICLO,
          QTSERVOO, QTSESTBYVOO,CDSERVICO, CDARVSERV,
          VRLOADSEVOO, NRPROPAXVOO, IDPROPSEVOO,
	   	  NRORG, NRORGULTATU, NRORGINCLUSAO,
	   	  DTINCLUSAO, DTULTATU,
	   	  CDOPERINCLUSAO, CDOPERULTATU, IDATIVO
     FROM PADVOOTREM
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = P_CDCIA
      AND NRVOO = P_NRVOOORI
      AND CDTRECHO = P_CDTRECHOORI;
END;
/