CREATE OR REPLACE PROCEDURE EXPORTA_TC_SAP(P_FILIAL    IN VARCHAR2,
                                           P_DATAINI   IN DATE,
                                           P_DATAFINAL IN DATE,
                                           P_TCDFILIAL IN VARCHAR2)
IS
BEGIN
  FOR C_EXPORTACAOTC IN
     (SELECT E.CDEMPRESATG CDEMPRESATC, E.CDFILIALTG CDFILIALTC, ROWNUM NRNUMERORETC, TO_CHAR(E.DTCONTABILTG, 'YYYYMMDD') DTCONTABILTC, E.VRSOMNUMDCTG VRSOMNUMDCTC,
             E.CDTIPOOPERTG CDTIPOOPERTC, E.NRINSJURNFTG NRINSJURNFTC, E.CDSERIEENTTG NRNOTAFISCTC, E.DTEMISSAOTG DTEMISSAOTC,
             E.DTATVENPAGTG DTATVENPAGTC, E.CDFILIRECETG CDFILIRECETC, E.VRDESPESATG VRDESPESATC, E.VRDESCONTOTG VRDESCONTOTC, E.VRICMSRETITG VRICMSRETITC,
             E.VRIRRETIDOTG VRIRRETIDOTC, E.NRINIMAQRETG NRINIMAQRETC, E.NRFIMMAQRETG NRFIMMAQRETC, E.NRTOTMAQRETG NRTOTMAQRETC, E.CDPRODUTOTG CDPRODUTOTC,
             E.QTITEMPRODTG QTITEMPRODTC, E.VRUNITITEMTG VRUNITITEMTC, E.VRDESCITEMTG VRDESCITEMTC, E.VRIPIPRODTG VRIPIPRODTC, E.VRNOTAFISCTG VRNOTAFISCTC,
             E.CDFILIEMITTG CDFILIEMITTC, E.IDALTERADATG IDALTERADATC, E.CDFIORIGDETG CDFIORIGDETC, E.CDSERORIDETG CDSERORIDETC, E.NRIJURORIGDE, 
             E.CDCFISCOPTG CDCFISCOPTC, E.NRLANBOLETTG NRLANBOLETTC, E.VRDOTACAOTG VRDOTACAOTC, E.VRIMPINSSTG VRIMPINSSTC, E.VRIMPISSTG VRIMPISSTC,
             E.NRMAQREGTG NRMAQREGTC, E.NRREDUCAOTG NRREDUCAOTC, E.VLRMAQRETG VLRMAQRETC, E.VLRCANCANTG VLRCANCANTC, E.CROTG CROTC
        FROM EXPORTACAOTG E, USR_SAP_EXPORTACAOTC U
       WHERE IDTPEXPORTACAO = 'TC'
         AND E.CDEMPRESATG  = U.CDEMPRESATC(+)
         AND E.CDFILIALTG   = U.CDFILIALTC(+)
         AND E.DTCONTABILTG = U.DTCONTABILTC(+)
         AND U.CDEMPRESATC  IS NULL
         AND U.CDFILIALTC   IS NULL
         AND U.DTCONTABILTC IS NULL
         AND ((INSTR(P_FILIAL, '_' || E.CDFILIALTG || '_') > 0) OR (P_TCDFILIAL = 'T'))
         AND DTCONTABILTG  BETWEEN P_DATAINI AND P_DATAFINAL)
   LOOP
      INSERT INTO USR_SAP_EXPORTACAOTC
      VALUES (C_EXPORTACAOTC.CDEMPRESATC, C_EXPORTACAOTC.CDFILIALTC, C_EXPORTACAOTC.NRNUMERORETC, C_EXPORTACAOTC.DTCONTABILTC, C_EXPORTACAOTC.VRSOMNUMDCTC,
              C_EXPORTACAOTC.CDTIPOOPERTC, 'J', C_EXPORTACAOTC.NRINSJURNFTC, C_EXPORTACAOTC.NRNOTAFISCTC, C_EXPORTACAOTC.DTEMISSAOTC, 
              C_EXPORTACAOTC.DTATVENPAGTC, C_EXPORTACAOTC.CDFILIRECETC, C_EXPORTACAOTC.VRDESPESATC, C_EXPORTACAOTC.VRDESCONTOTC, C_EXPORTACAOTC.VRICMSRETITC, 
              C_EXPORTACAOTC.VRIRRETIDOTC, C_EXPORTACAOTC.NRINIMAQRETC, C_EXPORTACAOTC.NRFIMMAQRETC, C_EXPORTACAOTC.NRTOTMAQRETC, C_EXPORTACAOTC.CDPRODUTOTC, 
              C_EXPORTACAOTC.QTITEMPRODTC, C_EXPORTACAOTC.VRUNITITEMTC, C_EXPORTACAOTC.VRDESCITEMTC, C_EXPORTACAOTC.VRIPIPRODTC, C_EXPORTACAOTC.VRNOTAFISCTC,
              C_EXPORTACAOTC.CDFILIEMITTC, C_EXPORTACAOTC.IDALTERADATC, C_EXPORTACAOTC.CDFIORIGDETC, C_EXPORTACAOTC.CDSERORIDETC, C_EXPORTACAOTC.NRIJURORIGDE,
              C_EXPORTACAOTC.CDCFISCOPTC, C_EXPORTACAOTC.NRLANBOLETTC, C_EXPORTACAOTC.VRDOTACAOTC, C_EXPORTACAOTC.VRIMPINSSTC, C_EXPORTACAOTC.VRIMPISSTC,
              C_EXPORTACAOTC.NRMAQREGTC, C_EXPORTACAOTC.NRREDUCAOTC, C_EXPORTACAOTC.VLRMAQRETC, C_EXPORTACAOTC.VLRCANCANTC, C_EXPORTACAOTC.CROTC,
              USR_SAP_IDINTERFACE.NEXTVAL, 'I', SYSDATE, NULL, 1, 1, 2);
   END LOOP;
   COMMIT;
END;

/

-- VERSAO --
BEGIN ATUVEROBJ('EXPORTA_TC_SAP','5.42.XXX'); END;
/