CREATE OR REPLACE
PROCEDURE USR_EXPO_PRAZOPGTAF_SAP
IS
   V_CCOMMIT                 INTEGER;
   V_ALTERACAO               VARCHAR2(01);
   V_STATUS                  INTEGER;
   V_IDENTIFICADOR           INTEGER;
BEGIN
   V_CCOMMIT   := 0;
   /* Fixo T */
   V_ALTERACAO := 'T';
   /* 0 - Ok
      1 - Novo
      2 - Erro
      3 - Em processamento */
   V_STATUS := 1;

   V_IDENTIFICADOR := NULL;
   FOR C_PRAZOPGTAF IN (SELECT P.CDFILDEST, P.CDFILLANC, P.NRAUTOFORN, P.NRDIASPGTOAF, P.VRPERCPGTOAF,
                               'I' USR_TPINTERFACE_SAP
                          FROM PRAZOPGTAF P
                         WHERE NVL(USR_IDINTEGRAMESAP, 'N') = 'S'
                           AND EXISTS(SELECT 1
                                        FROM USR_SAP_AUTOFORNE U
                                       WHERE P.NRAUTOFORN = U.IDENTIFIC
                                         AND P.CDFILDEST  = U.CENTRO)) LOOP

      V_IDENTIFICADOR := USR_SAP_PGTAF_IDINTERFACE.NEXTVAL;

      INSERT INTO USR_SAP_PRAZOPGTAF(ALTERACAO, IDENTIFIC, CENTRO, NRSEQPGTIOAFORN, NRDIASPGTOAF,
                                     VRPERCPGTOAF, CANCELADO, IDINTERFACE, TPINTERFACE, DTINTERFACEORI,
                                     DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO)
           VALUES (V_ALTERACAO, C_PRAZOPGTAF.NRAUTOFORN, C_PRAZOPGTAF.CDFILDEST, LPAD(V_IDENTIFICADOR,10,'0'), C_PRAZOPGTAF.NRDIASPGTOAF,
                   C_PRAZOPGTAF.VRPERCPGTOAF, 'N', V_IDENTIFICADOR, C_PRAZOPGTAF.USR_TPINTERFACE_SAP, SYSDATE,
                   NULL, V_STATUS, 1, 4);
      UPDATE PRAZOPGTAF
         SET USR_IDINTEGRAMESAP = 'N'
       WHERE CDFILDEST    = C_PRAZOPGTAF.CDFILDEST
         AND CDFILLANC    = C_PRAZOPGTAF.CDFILLANC
         AND NRAUTOFORN   = C_PRAZOPGTAF.NRAUTOFORN
         AND NRDIASPGTOAF = C_PRAZOPGTAF.NRDIASPGTOAF;
   END LOOP;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('USR_EXPO_PRAZOPGTAF_SAP','6.02.xxx'); END;
/