CREATE OR REPLACE
PROCEDURE USR_IMPO_ENVIOAFFORN_SAP
IS
   V_CCOMMIT         INTEGER;
   V_ALTERACAO       VARCHAR2(01);
   V_STATUS          INTEGER;
   V_IDENTIFICADOR   INTEGER;
   V_NRSEQDOWNLOADAF VARCHAR2(20);
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_ENVIOAFFORN IN (SELECT U.ALTERACAO, U.IDENTIFIC, U.NRSEQENVIOAFORN, U.DTDOWNLOAD,
                                U.DTRECEBEENVAF, U.IDTIPENVLIDO, U.CANCELADO, U.IDINTERFACE, U.TPINTERFACE,
                                U.DTINTERFACEORI, U.DTINTERFACEDEST, U.STATUS, U.APPORIGEM, U.APPDESTINO,
                                A.CDFORNECED, U.DSUSUARIOME, A.CDFILDEST, A.CDFILLANC
                           FROM USR_SAP_ENVIOAFFORN U INNER JOIN AUTOFORNE A ON U.IDENTIFIC = A.NRSEQENVIOAFORN
                          WHERE U.DTINTERFACEDEST IS NULL
                            AND U.DSUSUARIOME IS NOT NULL
                            AND U.APPORIGEM = 4
                            AND U.STATUS    = 1) LOOP

      UPDATE ENVIOAFFORN
         SET DTRECEBEENVAF = C_ENVIOAFFORN.DTRECEBEENVAF,
             IDTIPENVLIDO  = C_ENVIOAFFORN.IDTIPENVLIDO
       WHERE NRSEQENVIOAFORN = C_ENVIOAFFORN.NRSEQENVIOAFORN;

      NOVO_CODIGO('LOGDOWNLOADAF', V_NRSEQDOWNLOADAF);

      INSERT INTO LOGDOWNLOADAF(NRSEQDOWNLOADAF, NRSEQENVIOAFORN, CDFORNECED, NRAUTOFORN, CDFILDEST,
                                CDFILLANC, DSLOGINECO, DTDOWNLOAD)
           VALUES (V_NRSEQDOWNLOADAF, C_ENVIOAFFORN.NRSEQENVIOAFORN, C_ENVIOAFFORN.CDFORNECED, C_ENVIOAFFORN.IDENTIFIC, C_ENVIOAFFORN.CDFILDEST,
                   C_ENVIOAFFORN.CDFILLANC, C_ENVIOAFFORN.DSUSUARIOME, NVL(C_ENVIOAFFORN.DTDOWNLOAD, C_ENVIOAFFORN.DTRECEBEENVAF));

      UPDATE USR_SAP_ENVIOAFFORN
         SET DTINTERFACEDEST = SYSDATE,
             STATUS          = 0
       WHERE NRSEQENVIOAFORN = C_ENVIOAFFORN.NRSEQENVIOAFORN;

      V_CCOMMIT := V_CCOMMIT + 1;

      /*IF V_CCOMMIT > 200 THEN
         COMMIT;
      END IF;*/
   END LOOP;
   --COMMIT;
END;
/

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