CREATE OR REPLACE PROCEDURE EXPORTA_TM_SAP(P_FILIAL    IN VARCHAR2,
                                           P_DATAINI   IN DATE,
                                           P_DATAFINAL IN DATE,
                                           P_TCDFILIAL IN VARCHAR2)
IS
BEGIN
   FOR C_EXPORTACAOTM IN
       (SELECT E.CDEMPRESATM, E.CDFILIALTM, E.DTEXPORTTM, E.CDALMOXORIGTM, E.CDALMOXDESTTM, P.CDARVPROD AS CDPRODUTOTM,
               B.CDARVPROD AS CDPRODMOVITM, E.QTLANCTOESTTM, E.VRTOTLANCTOTM
          FROM EXPORTACAOTM E, USR_SAP_EXPORTACAOTM U, PRODUTO P, PRODUTO B
         WHERE E.CDEMPRESATM   = U.CDEMPRESATM(+)
           AND E.CDFILIALTM    = U.CDFILIALTM(+)
           AND E.DTEXPORTTM    = U.DTEXPORTTM(+)
           AND E.CDALMOXORIGTM = U.CDALMOXORIGTM(+)
           AND E.CDALMOXDESTTM = U.CDALMOXDESTTM(+)
           AND E.CDPRODUTOTM   = P.CDPRODUTO
           AND E.CDPRODMOVITM  = B.CDPRODUTO
           AND U.CDEMPRESATM IS NULL
           AND U.CDFILIALTM IS NULL
           AND U.DTEXPORTTM IS NULL
           AND U.CDALMOXORIGTM IS NULL
           AND U.CDALMOXDESTTM IS NULL
           AND U.CDPRODUTOTM IS NULL
           AND ((INSTR(P_FILIAL, '_' || E.CDFILIALTM || '_') > 0) OR (P_TCDFILIAL = 'T'))
           AND E.DTEXPORTTM BETWEEN P_DATAINI AND P_DATAFINAL)
   LOOP
      INSERT INTO USR_SAP_EXPORTACAOTM(CDEMPRESATM, CDFILIALTM, DTEXPORTTM, CDALMOXDESTTM, CDALMOXORIGTM,
                                       CDPRODUTOTM, CDPRODMOVITM, QTLANCTOESTTM, VRTOTLANCTOTM, IDINTERFACE,
                                       TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO)
      VALUES(C_EXPORTACAOTM.CDEMPRESATM, C_EXPORTACAOTM.CDFILIALTM, C_EXPORTACAOTM.DTEXPORTTM, C_EXPORTACAOTM.CDALMOXDESTTM, C_EXPORTACAOTM.CDALMOXORIGTM,
             C_EXPORTACAOTM.CDPRODUTOTM, C_EXPORTACAOTM.CDPRODMOVITM, C_EXPORTACAOTM.QTLANCTOESTTM, C_EXPORTACAOTM.VRTOTLANCTOTM, USR_SAP_IDINTERFACE.NEXTVAL,
             'I', SYSDATE, NULL, 1, 1, 2);
   END LOOP;
   COMMIT;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('EXPORTA_TM_SAP','6.19.XXX'); END;
/