REPORT ZTRBCHIP.
************************************************************************
* This program converts an online transaction run to a batch input
* program. The input parameters are the transaction code and the output
* program name (has to be in the customer name range)
* First the program runs the transaction as normal, and records all the
* screens, fields, input data in the CATT tables. The user has to leave
* the transaction at the end with the green arrow completely. Then, from
* the data in the CATT tables the program creates the source code of the
* batch input program and saves it with the specified name. Later this
* code can be further enhanced. 
************************************************************************
TABLES: TSTC.
TABLES: CATF, CATR, CATG, CATV, CATU, CATP, CATX, CATA.

PARAMETERS: TR_CODE LIKE TSTC-TCODE OBLIGATORY DEFAULT 'SU01',
            PR_NAME(8) OBLIGATORY DEFAULT 'ZTTTEEE'.

DATA: TRNAM LIKE CATF-TCODE, ABLNR LIKE CATF-ABLNR,
      FIRST(2), SECOND(2), FNAM(30), FVAL(50), I TYPE I.
DATA: BEGIN OF PROGRAM OCCURS 50,
      LINE(72),
END OF PROGRAM.

* Call the transaction and create the CATT data
CALL FUNCTION 'RS_CATT_MAINTAIN'
     EXPORTING
          AKTION       = 'GENR'
          CONFIRMATION = ' '
          TCODE        = TR_CODE
     IMPORTING
          ABLNR        = ABLNR.

* Create the program source of the bdc creator program
PROGRAM = 'PROGRAM         .'. PROGRAM+8(8) = PR_NAME.   APPEND PROGRAM.
PROGRAM = ' '.                                           APPEND PROGRAM.
PROGRAM = 'data: begin of bdc_tab occurs 100.'.          APPEND PROGRAM.
PROGRAM = '        include structure bdcdata.'.          APPEND PROGRAM.
PROGRAM = 'data: end of bdc_tab.'.                       APPEND PROGRAM.
PROGRAM = ' '.                                           APPEND PROGRAM.
*
SELECT * FROM CATF WHERE ABLNR = ABLNR.
  IF NOT CATF-TCODE IS INITIAL.
    TRNAM = CATF-TCODE.
  ENDIF.
*
  PROGRAM =  'PERFORM BDC_FILL USING ''X'''.
  APPEND PROGRAM.
*
  PROGRAM =  '     '''.  PROGRAM+6 = CATF-MPOOL.
  I = STRLEN( PROGRAM ). PROGRAM+I = ''''.
  APPEND PROGRAM.
*
  PROGRAM =  '     '''.  PROGRAM+6 = CATF-DYNNR.
  I = STRLEN( PROGRAM ). PROGRAM+I = '''.'.
  APPEND PROGRAM.
*
  DO.
    FIRST = CATF-VARF1.
    SHIFT CATF-VARF1 LEFT BY 2 PLACES.
    SECOND = CATF-VARF1.
    SHIFT CATF-VARF1 LEFT BY 2 PLACES.
    IF FIRST IS INITIAL.EXIT. ENDIF.
    FNAM = CATF-VARF1(FIRST).
    SHIFT CATF-VARF1 LEFT BY FIRST PLACES.
    FVAL = CATF-VARF1(SECOND).
    SHIFT CATF-VARF1 LEFT BY SECOND PLACES.
*
    PROGRAM =  'PERFORM BDC_FILL USING '' '''.
    APPEND PROGRAM.
*
    PROGRAM =  '     '''.  PROGRAM+6 = FNAM.
    I = STRLEN( PROGRAM ). PROGRAM+I = ''''.
    APPEND PROGRAM.
*
    PROGRAM =  '     '''.
    IF FVAL = ''' '''.
    I = STRLEN( PROGRAM ). PROGRAM+I = '''.'.
    ELSE.
    PROGRAM+6 = FVAL.
    I = STRLEN( PROGRAM ). PROGRAM+I = '''.'.
    ENDIF.
    APPEND PROGRAM.
*
  ENDDO.
ENDSELECT.
*
PROGRAM = ' '.                                           APPEND PROGRAM.
PROGRAM = 'call transaction '''.PROGRAM+18 = TR_CODE.
I = STRLEN( PROGRAM ). PROGRAM+I = ''''.                 APPEND PROGRAM.
PROGRAM = 'using bdc_tab mode ''N''.'.                   APPEND PROGRAM.
PROGRAM = 'if sy-subrc = 0.'.                            APPEND PROGRAM.
PROGRAM = '   write: / ''ok''.'.                         APPEND PROGRAM.
PROGRAM = 'else.'.                                       APPEND PROGRAM.
PROGRAM = '   write: / ''error''.'.                      APPEND PROGRAM.
PROGRAM = 'endif.'.                                      APPEND PROGRAM.
PROGRAM = ' '.                                           APPEND PROGRAM.
*
PROGRAM = ' '.                                           APPEND PROGRAM.
PROGRAM = 'FORM BDC_FILL USING P1 P2 P3.'.               APPEND PROGRAM.
PROGRAM = '  clear bdc_tab.'.                            APPEND PROGRAM.
PROGRAM = '  if p1 = ''X''.'.                            APPEND PROGRAM.
PROGRAM = '    bdc_tab-dynbegin = p1.'.                  APPEND PROGRAM.
PROGRAM = '    bdc_tab-program = p2.'.                   APPEND PROGRAM.
PROGRAM = '    bdc_tab-dynpro =  p3.'.                   APPEND PROGRAM.
PROGRAM = '  else.'.                                     APPEND PROGRAM.
PROGRAM = '    bdc_tab-dynbegin = p1.'.                  APPEND PROGRAM.
PROGRAM = '    bdc_tab-fnam = p2.'.                      APPEND PROGRAM.
PROGRAM = '    bdc_tab-fval = p3.'.                      APPEND PROGRAM.
PROGRAM = '  endif.'.                                    APPEND PROGRAM.
PROGRAM = '  append bdc_tab.'.                           APPEND PROGRAM.
PROGRAM = 'ENDFORM.'.                                    APPEND PROGRAM.

* Clean the CATT tables
DELETE FROM CATF WHERE ABLNR = ABLNR.
DELETE FROM CATR WHERE ABLNR = ABLNR.
DELETE FROM CATG WHERE ABLNR = ABLNR.
DELETE FROM CATV WHERE ABLNR = ABLNR.
DELETE FROM CATU WHERE ABLNR = ABLNR.
DELETE FROM CATP WHERE ABLNR = ABLNR.
DELETE FROM CATX WHERE ABLNR = ABLNR.
DELETE FROM CATA WHERE ABLNR = ABLNR.
COMMIT WORK.

* Insert the program
INSERT REPORT PR_NAME FROM PROGRAM.