create or replace PROCEDURE COPIA_ITEMFAT_VOO_VIGENCIA (
    P_CDFILIAL IN VARCHAR2,
    P_CDCIA IN VARCHAR2,
    P_NRVOOORI IN VARCHAR2,
    P_CDTRECHOORI IN VARCHAR2,
    P_NRSEQPADVOOTREMDATAORI IN NUMBER,
    P_NRVOODES IN VARCHAR2,
    P_DTINIVIGENCIA IN DATE, 
    P_CDOPERADOR IN VARCHAR2,
    P_NRORG IN NUMBER
)
AS

  V_NRSEQPADVOOTREMDATA NUMBER;

BEGIN

   V_NRSEQPADVOOTREMDATA := NULL;
    
   FOR CUR_VOO IN (SELECT NRVOO, CDTRECHO
                          FROM VOOTRECHO
                          WHERE CDFILIAL = P_CDFILIAL
                          AND CDCIA      = P_CDCIA
                          AND VERIF_VALOR_LISTA(NRVOO||'|'||CDTRECHO, P_NRVOODES) IS NOT NULL
   ) LOOP 
        
    /* Sequencial de vigencia para a data*/
    SELECT MAX(NRSEQPADVOOTREMDATA)
     INTO V_NRSEQPADVOOTREMDATA 
    FROM PADVOOTREMDATA
       WHERE CDFILIAL = P_CDFILIAL AND CDCIA = P_CDCIA
       AND NRVOO = CUR_VOO.NRVOO
       AND CDTRECHO = CUR_VOO.CDTRECHO
        AND DTINIVIGENCIA = P_DTINIVIGENCIA;
        
    IF (CUR_VOO.NRVOO = P_NRVOOORI AND
        CUR_VOO.CDTRECHO = P_CDTRECHOORI AND
        V_NRSEQPADVOOTREMDATA = P_NRSEQPADVOOTREMDATAORI) THEN
        CONTINUE;    
    END IF;

    IF V_NRSEQPADVOOTREMDATA IS NULL
    THEN 
      /* Cria sequencial para a nova vigencia */ 
        NOVO_CODIGO('PADVOOTREMDATA', V_NRSEQPADVOOTREMDATA,1, P_NRORG);
      /* Insere a nova configuracao para o Voo */
        INSERT INTO PADVOOTREMDATA
          (CDFILIAL, CDCIA, NRVOO, CDTRECHO,
            NRSEQPADVOOTREMDATA,DTINIVIGENCIA,
            NRORG, NRORGULTATU, NRORGINCLUSAO,
            DTINCLUSAO, DTULTATU,
            CDOPERINCLUSAO, CDOPERULTATU, IDATIVO)
          VALUES (P_CDFILIAL, P_CDCIA, CUR_VOO.NRVOO, CUR_VOO.CDTRECHO, 
                  V_NRSEQPADVOOTREMDATA, P_DTINIVIGENCIA, P_NRORG,
                  P_NRORG, P_NRORG, SYSDATE, SYSDATE, P_CDOPERADOR, P_CDOPERADOR, 'S');
    ELSE 
      DELETE FROM PADVOOTREM
        WHERE CDFILIAL = P_CDFILIAL
          AND CDCIA = P_CDCIA
          AND NRVOO = CUR_VOO.NRVOO
          AND CDTRECHO = CUR_VOO.CDTRECHO
          AND NRSEQPADVOOTREMDATA = V_NRSEQPADVOOTREMDATA;
    END IF;

    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, NRSEQPADVOOTREMDATA)
     SELECT P_CDFILIAL, P_CDCIA, CUR_VOO.NRVOO, CUR_VOO.CDTRECHO,
            NRSERVICOCIAFILI, CDCLASSE, CDCICLO,
            QTSERVOO, QTSESTBYVOO,CDSERVICO, CDARVSERV,
            VRLOADSEVOO, NRPROPAXVOO, IDPROPSEVOO,
          NRORG, NRORGULTATU, NRORGINCLUSAO,
          DTINCLUSAO, DTULTATU,
          CDOPERINCLUSAO, CDOPERULTATU, IDATIVO, V_NRSEQPADVOOTREMDATA
       FROM PADVOOTREM
      WHERE CDFILIAL = P_CDFILIAL
        AND CDCIA = P_CDCIA
        AND NRVOO = P_NRVOOORI
        AND CDTRECHO = P_CDTRECHOORI
        AND NRSEQPADVOOTREMDATA = P_NRSEQPADVOOTREMDATAORI;
    
      V_NRSEQPADVOOTREMDATA := NULL;         
    
  END LOOP; 
END;
/