Materialharmonisierung

www.4AP.de - ABAP Syntaxbeispiel: Materialharmonisierung mit dem Funktionsbaustein MATERIAL_MAINTAIN_DARK

Beispielcoding: Materialharmonisierung mit dem Funktionsbaustein MATERIAL_MAINTAIN_DARK (kopieren erlaubt aber auf eigene Gefahr :-))




*----------------------------------------------------------------------*
* Report  ZMMATHAR
* erstellt 25.07.2003 Thorsten Neubüser, www.4ap.de
*
*  die im Report ZMMATVER (Materialvergleich) exportierte Excel-Liste
*  kann (ohne oder nach manueller Nachbearbeitung) importiert werden
*  die zum Mustermaterial abweichenden Felder werden angepasst
*  (nur gefüllte Felder!!! 'blank' = nicht ändern!)
*  dazu wird der Fubau MATERIAL_MAINTAIN_DARK verwendet
*  die Einkaufsinfosätze werden per BTCI geändert (Tabelle aeine_ueb)
*                                                                      *
*---------------------------------------------------------------------*</span>

REPORT  ZMMATHAR LINE-SIZE 240 LINE-COUNT 65.

* DATENDEKLARATIONEN
DATA:
  H_FILELEN TYPE P,                " Hilfsfeld Dateilänge
*  hochkomma type x value '27', " hex27 = dec39 = '
  TRENN_SEM TYPE X VALUE '3B', " hex3B = dec59 = ;
  TRENN_TAB TYPE X VALUE '09', " hex09 = dec09 = Tabulator
  TRENNER TYPE X,
* hochgeladene Tabelle
  BEGIN OF UP_TAB OCCURS 0,
    LINE(999),                     " 999 Char
  END OF UP_TAB,
  REST LIKE UP_TAB-LINE,
* Tabellen-Felder
  BEGIN OF TABFELDER OCCURS 0,
    STRING(21),
    TABNAME LIKE DD03L-TABNAME,     " 10 Char
    FIELDNAME LIKE DD03L-FIELDNAME, " 10 Char
  END OF TABFELDER,
* Zähler für Materialzeile
  ZAEHLER LIKE MARA_UEB-TRANC, " 1 Zeile 1 Material
* Tabelle in die eine Zeile zerlegt wird
  BEGIN OF SPLIT_TAB OCCURS 0,
    LINE(40),     " 40 Char
  END OF SPLIT_TAB,
* Fehlermeldungen
  BEGIN OF FEHLER_TAB OCCURS 0,  " Tabelle für Fehlermeldungen
    LINE LIKE SY-LISEL,          " 255 Char
  END OF FEHLER_TAB,
* Einkaufsinfosätze
  BEGIN OF AEINE_UEB OCCURS 0,
    LFDNR TYPE I,
    LIFNR LIKE EINA-LIFNR,
    MATNR LIKE EINA-MATNR.
    INCLUDE STRUCTURE EINE.
  DATA: END OF AEINE_UEB,

* Vergleichsmaterial
  LAST_MATNR LIKE MARA-MATNR.

** da 3.1i die dynamische Erzeugung einer internen Tabelle noch nicht
**  unterstütz halt den 'Umweg' über einen subroutine pool
  DATA: CODE(72) OCCURS 100 WITH HEADER LINE,
        CODE2(72),
        HOCHKOMMA TYPE X VALUE '27', " hex27 = dec39 = '
        PROG(8), MSG(120), LIN(3), WRD(10), OFF(3).

* Batch-Input-Daten
  DATA: BEGIN OF XBDCDATA OCCURS 0.
          INCLUDE STRUCTURE BDCDATA.
  DATA: END OF XBDCDATA.



* SELEKTIONSBILDSCHIRM
SELECTION-SCREEN BEGIN OF BLOCK DATEI
  WITH FRAME TITLE TEXT-001. " 'Dateiauswahl:'
  SELECTION-SCREEN COMMENT /1(79) TEXT-002.
  SELECTION-SCREEN COMMENT /1(79) TEXT-003.
  SELECTION-SCREEN COMMENT /1(79) TEXT-004.
*  selection-screen skip.
  SELECTION-SCREEN COMMENT /1(79) TEXT-005.
  PARAMETERS:
    P_FILENM LIKE RLGRAP-FILENAME OBLIGATORY,
    P_TAB RADIOBUTTON GROUP RAD1,
    P_SEM RADIOBUTTON GROUP RAD1 DEFAULT 'X'.
  SELECTION-SCREEN SKIP.   SELECTION-SCREEN SKIP.
  SELECTION-SCREEN COMMENT 1(79) TEXT-024.
  SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN COMMENT 1(31) TEXT-023.
* die Buchung über MATERIAL_MAINTAIN_DARK gibt die Materialien erst
*   wieder frei, wenn ABAP beendet ist, daher getrennte Verbuchung
*   der EK-Infosätze nötig
    PARAMETERS: P_MAT RADIOBUTTON GROUP RAD2 DEFAULT 'X'. " Mat_Maint_dk
  SELECTION-SCREEN END OF LINE.
  SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN COMMENT 1(31) TEXT-019.
    PARAMETERS: P_EINE RADIOBUTTON GROUP RAD2.            " BTCI EINE
    SELECTION-SCREEN COMMENT 36(26) TEXT-020.
    PARAMETERS: P_E_BTCI DEFAULT 'E'.  " Abspielmodus BTCI
  SELECTION-SCREEN END OF LINE.
  SELECTION-SCREEN SKIP 3.
  PARAMETERS: P_CODE.
SELECTION-SCREEN END OF BLOCK DATEI.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILENM.
*Zeige alle Dateien mit Endung CSV
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
       EXPORTING
     mask                = '*.*' " bis 4.6  oder z.B. '*.xls'
*     mask                = ',Excel-Dateien,*.xls' " ab 4.7 ',Beschreibung,*.ausgewählt'
*     mask                = ',*.*,*.*.'       " ab 4.7 zeigt alle an
            STATIC    = 'X'
       CHANGING
            FILE_NAME = P_FILENM.


* ES GEHT LOS!
START-OF-SELECTION.
* Datei in itab hochladen, und zerlegen
  PERFORM SICHERHEITSABFRAGE.         " nur berechtigte User
  PERFORM DATEI_LADEN.                " inkl. minimaler Formatprüfung
  PERFORM TABELLENFELDER_EXTRAHIEREN. " in tabfelder Tabelle + Feldname
  PERFORM HEADER_SUBROUTINE.          " start subpool
  PERFORM BODY_SUBROUTINE.            " alle Materialzeilen in subpool
  IF NOT P_MAT IS INITIAL.
    PERFORM FUBAU_MAT_MAINT_DARK.       " Fubauaufruf
  ENDIF. " not p_mat is initial
  PERFORM FOOTER_SUBROUTINE.          " Zeile schreiben + Ende subpool
  PERFORM MATERIAL_BUCHEN.            " subpool: material_maintain_dark

* VERARBEITUNG der Einkaufsinfosätze per BTCI
  COMMIT WORK.
  IF NOT P_EINE IS INITIAL.
    PERFORM EINKAUFSINFOS_BUCHEN.
  ENDIF. " not p_eine is initial

* FERTIG
END-OF-SELECTION.
  SKIP.
  ULINE.
  SKIP.
  WRITE: / 'Hochgeladene Daten:'(018).
  DATA: MATERIAL_ZAEHLER(4).
  LOOP AT UP_TAB.
    WRITE: / MATERIAL_ZAEHLER RIGHT-JUSTIFIED, UP_TAB(200).
    IF SY-TABIX > 2.
     MATERIAL_ZAEHLER = MATERIAL_ZAEHLER + 1.
    ENDIF.
  ENDLOOP. "at up_tab




*######################################################################*
* AB HIER NUR NOCH FORM-ROUTINEN
*######################################################################*


*---------------------------------------------------------------------*
*      Form  SICHERHEITSABFRAGE
*---------------------------------------------------------------------*
FORM SICHERHEITSABFRAGE.
  DATA: USER_NAME LIKE SY-UNAME.
  USER_NAME = SY-UNAME.
  CASE USER_NAME.
    WHEN 'NEUBUEST'.
*     va bene
    WHEN 'RATHM'
      OR 'KLENKEG'.
*      clear p_code.
    WHEN OTHERS.
      MESSAGE E000(ZP) WITH 'Sie haben keine Berechtigung.'(025).
  ENDCASE.
ENDFORM.                    " SICHERHEITSABFRAGE


*---------------------------------------------------------------------*
*      Form  DATEI_LADEN
*---------------------------------------------------------------------*
FORM DATEI_LADEN.
* Trennzeichen festlegen
  IF P_SEM IS INITIAL.
    TRENNER = TRENN_TAB.
  ELSE.
    TRENNER = TRENN_SEM.
  ENDIF.
* Datei hochladen
  CALL FUNCTION 'WS_UPLOAD'
       EXPORTING
            FILENAME            = P_FILENM
       IMPORTING
            FILELENGTH          = H_FILELEN
       TABLES
            DATA_TAB            = UP_TAB
       EXCEPTIONS
            CONVERSION_ERROR    = 1
            FILE_OPEN_ERROR     = 2
            FILE_READ_ERROR     = 3
            INVALID_TABLE_WIDTH = 4
            INVALID_TYPE        = 5
            NO_BATCH            = 6
            UNKNOWN_ERROR       = 7
            OTHERS              = 8.
  IF NOT SY-SUBRC IS INITIAL.
    MESSAGE E000(ZP) WITH TEXT-006 SY-SUBRC.
  ELSE.
    COMMIT WORK.
  ENDIF.
* prüfen on Zeilenaufbau stimmt
  READ TABLE UP_TAB INDEX 1.
* erstes Feld hat muss den Text 'Material' enthalten!
  IF UP_TAB(8) NE 'Material'(008).
    MESSAGE E000(ZP) WITH '1. Zeile beginnt nicht mit:'(009) TEXT-008.
  ENDIF.
  READ TABLE UP_TAB INDEX 2.
* SCHLÜSSELFELDER CHECKEN
* erstes Feld hat muss die Feldbeschreibung Materialnummer enthalten!
  IF UP_TAB(11) NE 'MARAV-MATNR'(010).
    MESSAGE E000(ZP) WITH '2. Zeile beginnt nicht mit:'(009) TEXT-010.
  ENDIF.
* zweites Feld muss das Werk enthalten
  IF UP_TAB+12(10) NE 'MARC-WERKS'(013).
    MESSAGE E000(ZP) WITH '2. Zeile+11 enthält nicht:'(014) TEXT-013.
  ENDIF.
* TO BE CONTINUED


*  read table up_tab index 2.
** Semikolon als Trennfeld vorgeschrieben!
*  IF UP_TAB+11(1) NE TRENNER.
*    MESSAGE E000(ZP) WITH 'Trennzeichen nicht:'(012) TRENNER.
*  ENDIF.
* Musterflag in erster Datenzeile???
  READ TABLE UP_TAB INDEX 3.
  SPLIT UP_TAB AT TRENNER INTO TABLE TABFELDER.
  READ TABLE TABFELDER INDEX 4.
  IF TABFELDER-STRING NE 'X'.
    MESSAGE E000(ZP) WITH 'Musterzeile nicht an Pos. 3.'(015).
  ENDIF.
  CLEAR TABFELDER. REFRESH TABFELDER.

ENDFORM.                    " DATEI_LADEN


*---------------------------------------------------------------------*
*      Form  TABELLENFELDER_EXTRAHIEREN
*---------------------------------------------------------------------*
FORM TABELLENFELDER_EXTRAHIEREN.
* Feldnamen extrahieren
  READ TABLE UP_TAB INDEX 2.
* erste Trennung
  SPLIT UP_TAB-LINE AT TRENNER INTO TABLE TABFELDER.
* zweite Trennung
  LOOP AT TABFELDER.
    SPLIT TABFELDER-STRING AT '-' INTO
      TABFELDER-TABNAME TABFELDER-FIELDNAME.
    IF TABFELDER-TABNAME = 'MARAV'.       " wird im ZMMATVER verwendet
      TABFELDER-TABNAME  = 'MARA'.
    ENDIF.
    MODIFY TABFELDER.
  ENDLOOP. " at tabfelder
ENDFORM.                    " TABELLENFELDER_EXTRAHIEREN


*---------------------------------------------------------------------*
*      Form  HEADER_SUBROUTINE
*---------------------------------------------------------------------*
FORM HEADER_SUBROUTINE.
* Subpool-Programmstart
  APPEND 'PROGRAM SUBPOOL.' TO CODE.
* Subpool-Datendeklarationen
* allgemeine Datendeklarationen

  APPEND 'data: begin of fehler_tab occurs 0,  " Fehlermeld.' TO CODE.
  APPEND '      line like sy-lisel,            " 255 Char   ' TO CODE.
  APPEND '    end of fehler_tab,                            ' TO CODE.
  APPEND '    returncode(2).                                ' TO CODE.
  APPEND '* an den Fubau MATERIAL_MAINTAIN_DARK             ' TO CODE.
  APPEND '*   zu übergebende Daten                          ' TO CODE.
  APPEND 'data:                                             ' TO CODE.
  APPEND '  amara_ueb like mara_ueb occurs 0 with header line,' TO CODE.
  APPEND '  amarc_ueb like marc_ueb occurs 0 with header line,' TO CODE.
  APPEND '  amakt_ueb like makt_ueb occurs 0 with header line,' TO CODE.
  APPEND '  ambew_ueb like mbew_ueb occurs 0 with header line,' TO CODE.
  APPEND '  amvke_ueb like mvke_ueb occurs 0 with header line,' TO CODE.
*       Nicht durch MATERIAL_MAINTAIN_DARK verarbeitete Tabellen
*  append '  aeine_ueb like eine occurs 0 with header line,  ' to code.
  APPEND '* Einkaufsinfosaetze                               ' TO CODE.
  APPEND '  begin of aeine_ueb occurs 0,                     ' TO CODE.
  APPEND '  lfdnr type i,                                    ' TO CODE.
  APPEND '  lifnr like eina-lifnr,                           ' TO CODE.
  APPEND '  matnr like eina-matnr.                           ' TO CODE.
  APPEND '  include structure eine.                          ' TO CODE.
  APPEND 'data: end of aeine_ueb,                            ' TO CODE.


  APPEND '  aj_1ne_ueb like j_1ne occurs 0 with header line,' TO CODE.
  APPEND 'aj_1nemm_ueb like j_1nemm occurs 0 with header line,' TO CODE.
* Einkaufsbestelltexte
  APPEND '  begin of ek_txt occurs 0.                       ' TO CODE.
  APPEND '    include structure tline. " tdformat  tdline   ' TO CODE.
  APPEND '  data: line_anz type i,                          ' TO CODE.
  APPEND '        end of ek_txt,                            ' TO CODE.

* Fehlertabelle
  APPEND '  amerrdat  like merrdat  occurs 0 with header line,' TO CODE.
* weitere Felder
  APPEND '  matnr_last like mara-matnr,                     ' TO CODE.
  APPEND '  number_errors_transaction like tbist-numerror,  ' TO CODE.
  APPEND '* sonstiges                                       ' TO CODE.
  APPEND '  matnr_last_save like mara-matnr,                ' TO CODE.
  CONCATENATE '  blank value' HOCHKOMMA HOCHKOMMA ','
    INTO CODE SEPARATED BY ' '.
  APPEND CODE.
  CONCATENATE HOCHKOMMA 'X' HOCHKOMMA ',' INTO CODE.
  CONCATENATE '  fest_x value ' CODE INTO CODE SEPARATED BY ' '.
  APPEND CODE.
  CONCATENATE HOCHKOMMA 'E' HOCHKOMMA '.' INTO CODE.
  CONCATENATE '  fest_e value ' CODE INTO CODE SEPARATED BY ' '.
  APPEND CODE.

* Subpool-EIGENTLICHES Programm
  APPEND '*                                                 ' TO CODE.
  APPEND '* LOS GEHTs                                       ' TO CODE.
* interne Tabelle füllen
  APPEND '*                                                 ' TO CODE.
  APPEND 'FORM MATERIAL_VERARBEITEN                         ' TO CODE.
  APPEND '   tables fehler_tab like fehler_tab[]            ' TO CODE.
  APPEND '          aeine_ueb  like aeine_ueb[]             ' TO CODE.
  APPEND '   using p_matnr.                                 ' TO CODE.
ENDFORM.                    " HEADER_SUBROUTINE

*---------------------------------------------------------------------*
*      Form  BODY_SUBROUTINE
*---------------------------------------------------------------------*
FORM BODY_SUBROUTINE.
  APPEND '*                                                 ' TO CODE.
  APPEND '* LOOP über UP_TAB extrahiert Daten               ' TO CODE.
  APPEND '*                                                 ' TO CODE.
  DATA: TABELLE(15).

*  hier loop über up_tab
  LOOP AT UP_TAB.
    CHECK SY-TABIX > 3. " das Mustermaterial wird NICHT geändert
    ZAEHLER = ZAEHLER + 1.
    CLEAR SPLIT_TAB. REFRESH SPLIT_TAB.
    SPLIT UP_TAB AT TRENNER INTO TABLE SPLIT_TAB.
    LOOP AT SPLIT_TAB.
      READ TABLE TABFELDER INDEX SY-TABIX.
      CHECK TABFELDER-TABNAME NS 'Musterflag'.
      CASE TABFELDER-TABNAME.
        WHEN 'MARA'.
          TABELLE = 'amara_ueb'.
        WHEN 'MARC'.
          TABELLE = 'amarc_ueb'.
        WHEN 'MAKT'.
          TABELLE = 'amakt_ueb'.
        WHEN 'MBEW'.
          TABELLE = 'ambew_ueb'.
        WHEN 'MVKE'.
          TABELLE = 'amvke_ueb'.
*       Nicht durch MATERIAL_MAINTAIN_DARK verarbeitete Tabellen
        WHEN 'EINE'.
          TABELLE = 'aeine_ueb'.
        WHEN 'J_1NE'.
          TABELLE = 'aj_1ne_ueb'.
        WHEN 'J_1NEMM'.
          TABELLE = 'aj_1NEMM_ueb'.
*        when 'EK_TXT'.
*          tabelle = 'EK_TXT'.

        WHEN OTHERS.
          TABELLE = TABFELDER-TABNAME.
      ENDCASE.

      IF SPLIT_TAB-LINE IS INITIAL.
        CONCATENATE HOCHKOMMA HOCHKOMMA INTO CODE SEPARATED BY ' '.
      ELSE.
        CONCATENATE HOCHKOMMA SPLIT_TAB-LINE HOCHKOMMA INTO CODE.
      ENDIF.
      CONCATENATE TABELLE '-' TABFELDER-FIELDNAME INTO CODE2.
*     je nachdem ob Zahlenfeld oder Buchstaben
      IF SPLIT_TAB-LINE CO '0123456789 .,'.
        IF SPLIT_TAB-LINE CO ' '.
          CONCATENATE CODE2 '=' CODE '.' INTO CODE SEPARATED BY ' '.
        ELSE.
*         'Tausend'trenner entfernen, Punkt mit Komma tauschen
          REPLACE '.' WITH '' INTO CODE.
          REPLACE '.' WITH '' INTO CODE.
          REPLACE '.' WITH '' INTO CODE.
          REPLACE ',' WITH '.' INTO CODE.
          CONDENSE CODE NO-GAPS.
          CONCATENATE 'move' CODE 'to' CODE2 '.'
            INTO CODE SEPARATED BY ' '.
        ENDIF.
      ELSE. " split_tab-line co '0123456789'
        CONCATENATE 'move' CODE 'to' CODE2 '.'
          INTO CODE SEPARATED BY ' '.
      ENDIF. " split_tab-line co '0123456789'
      APPEND CODE.

    ENDLOOP. " at split_tab
*  ENDE LOOP über split_tab

    APPEND 'perform zeile_sichern tables aeine_ueb          ' TO CODE.
    CONCATENATE HOCHKOMMA ZAEHLER HOCHKOMMA '.' INTO CODE.
    CONCATENATE '  using' CODE INTO CODE SEPARATED BY ' '.
    APPEND CODE.
    APPEND '*                                               ' TO CODE.

  ENDLOOP. " at up_tab
  APPEND '*                                                 ' TO CODE.
  APPEND '* Letzte Materialnummer für Vergleich zuordnen    ' TO CODE.
  APPEND '  matnr_last_save = amara_ueb-matnr.              ' TO CODE.
  APPEND '*                                                 ' TO CODE.
  APPEND '* Ende LOOP über UP_TAB extrahiert Daten          ' TO CODE.
  APPEND '*                                                 ' TO CODE.
ENDFORM.                    " BODY_SUBROUTINE


*---------------------------------------------------------------------*
*      Form  FUBAU_MAT_MAINT_DARK
*---------------------------------------------------------------------*
FORM FUBAU_MAT_MAINT_DARK.
  APPEND '*                                                 ' TO CODE.
  APPEND '*        FUBAU  MATERIAL_MAINTAIN_DARK            ' TO CODE.
  APPEND '*                                                 ' TO CODE.
  APPEND '* Materialstammpflege ohne Bilder                 ' TO CODE.
  APPEND '* Der Funktionsbaustein kann aus jeder beliebigen ' TO CODE.
  APPEND '*   Anwendung heraus aufgerufen werden.           ' TO CODE.
  APPEND '* Die Übergabe der zu pflegenden Materialdaten    ' TO CODE.
  APPEND '*   erfolgt mittels der Tabellen AMARA_UEB,       ' TO CODE.
  APPEND '*   AMARC_UEB, ... im internen Format.            ' TO CODE.
  APPEND '* Weiteres in der FUBAU-Doku                      ' TO CODE.
  APPEND '*   [ ja, es gibt auch dokumentierte :-)) ]       ' TO CODE.
  CONCATENATE '  call function ' HOCHKOMMA 'MATERIAL_MAINTAIN_DARK'
    HOCHKOMMA INTO CODE.
  APPEND CODE.
  APPEND '       exporting                                  ' TO CODE.
  APPEND '           kz_activ_cad      = blank              ' TO CODE.
  APPEND '           flag_muss_pruefen = fest_x             ' TO CODE.
  APPEND '           sperrmodus        = fest_e             ' TO CODE.
  APPEND '           max_errors        = 0                  ' TO CODE.
  APPEND '           p_kz_no_warn      = fest_x             ' TO CODE.
  APPEND '           kz_prf            = blank              ' TO CODE.
  APPEND '           kz_verw           = fest_x             ' TO CODE.
  APPEND '           kz_aend           = fest_x             ' TO CODE.
  APPEND '           kz_dispo          = fest_x             ' TO CODE.
  APPEND '           kz_test           = blank              ' TO CODE.
  APPEND '           kz_mdip           = blank              ' TO CODE.
  APPEND '           kz_mprp           = blank              ' TO CODE.
  APPEND '           kz_ale            = blank              ' TO CODE.
  APPEND '           kz_actv           = blank              ' TO CODE.
  APPEND '      importing                                   ' TO CODE.
  APPEND '           matnr_last        = matnr_last         ' TO CODE.
  APPEND '           number_errors_transaction              ' TO CODE.
  APPEND '             = number_errors_transaction          ' TO CODE.
  APPEND '       tables                                     ' TO CODE.
  APPEND '           amara_ueb         = amara_ueb          ' TO CODE.
  APPEND '           amakt_ueb         = amakt_ueb          ' TO CODE.
  APPEND '           amarc_ueb         = amarc_ueb          ' TO CODE.
  APPEND '*           AMARD_UEB         =                   ' TO CODE.
  APPEND '*           AMFHM_UEB         =                   ' TO CODE.
  APPEND '*           AMARM_UEB         =                   ' TO CODE.
  APPEND '*           AMEA1_UEB         =                   ' TO CODE.
  APPEND '           ambew_ueb         = ambew_ueb          ' TO CODE.
  APPEND '*           ASTEU_UEB         =                   ' TO CODE.
  APPEND '*           ASTMM_UEB         =                   ' TO CODE.
  APPEND '*           AMLGN_UEB         =                   ' TO CODE.
  APPEND '*           AMLGT_UEB         =                   ' TO CODE.
  APPEND '*           AMPGD_UEB         =                   ' TO CODE.
  APPEND '*           AMPOP_UEB         =                   ' TO CODE.
  APPEND '*           AMVEG_UEB         =                   ' TO CODE.
  APPEND '*           AMVEU_UEB         =                   ' TO CODE.
  APPEND '           AMVKE_UEB         = AMVKE_UEB          ' TO CODE.
  APPEND '*           ALTX1_UEB         =                   ' TO CODE.
  APPEND '*           AMPRW_UEB         =                   ' TO CODE.
  APPEND '*           AMFIELDRES        =                   ' TO CODE.
  APPEND '           amerrdat          = amerrdat           ' TO CODE.
  APPEND '       exceptions                                 ' TO CODE.
  APPEND '            kstatus_empty             = 1         ' TO CODE.
  APPEND '            tkstatus_empty            = 2         ' TO CODE.
  APPEND '            t130m_error               = 3         ' TO CODE.
  APPEND '            internal_error            = 4         ' TO CODE.
  APPEND '            too_many_errors           = 5         ' TO CODE.
  APPEND '            update_error              = 6         ' TO CODE.
  APPEND '            others                    = 7.        ' TO CODE.
* Abfrage sy-subrc
  APPEND '*                                                 ' TO CODE.
  APPEND '  if sy-subrc ne 0.                               ' TO CODE.
  APPEND '    move sy-subrc to returncode.                  ' TO CODE.
  CONCATENATE HOCHKOMMA 'material_maintain_dark:' HOCHKOMMA '(004)'
    INTO CODE.
  CONCATENATE '    concatenate' CODE INTO CODE SEPARATED BY ' '.
  APPEND CODE.
  APPEND '      returncode                                  ' TO CODE.
  APPEND '      into fehler_tab-line separated by blank.    ' TO CODE.
  APPEND '    append fehler_tab.                            ' TO CODE.
  APPEND '    else.                                         ' TO CODE.
  APPEND '      commit work. " sonst Material gesperrt!     ' TO CODE.
  APPEND '  endif.                                          ' TO CODE.
* Vergleich letzte Material-Nr
  APPEND '*                                                 ' TO CODE.
  APPEND '  if not matnr_last = matnr_last_save.            ' TO CODE.
  APPEND '*   Das darf eigentlich nicht passieren!          ' TO CODE.
  APPEND '*   Dann ganze Verarbeitung fehlerhaft!!          ' TO CODE.
  CONCATENATE HOCHKOMMA 'Material_Maintain_Dark:' HOCHKOMMA
    '(004)' INTO CODE.
  CONCATENATE '    concatenate' CODE INTO CODE SEPARATED BY ' '.
  APPEND CODE.
  CONCATENATE HOCHKOMMA 'Materialnummern ungleich:'
    HOCHKOMMA '(005)' INTO CODE.
  APPEND CODE.
  APPEND '      matnr_last_save matnr_last                  ' TO CODE.
  APPEND '      into fehler_tab-line separated by blank.    ' TO CODE.
  APPEND '    append fehler_tab.                            ' TO CODE.
  APPEND '  endif. " matnr_last = matnr_last_save           ' TO CODE.

*  Ausgabe Anzahl der Fehler
  APPEND '*                                                 ' TO CODE.
  APPEND '  if not number_errors_transaction is initial.    ' TO CODE.
  CONCATENATE HOCHKOMMA 'material_maintain_dark:'
    HOCHKOMMA '(004)' INTO CODE.
  CONCATENATE '    concatenate' CODE INTO CODE SEPARATED BY ' '.
  APPEND CODE.
  CONCATENATE HOCHKOMMA 'Anzahl Fehler:' HOCHKOMMA '(007)' INTO CODE.
  APPEND CODE.
  APPEND '      number_errors_transaction                   ' TO CODE.
  APPEND '      into fehler_tab-line separated by blank.    ' TO CODE.
  APPEND '    append fehler_tab.                            ' TO CODE.
  APPEND '  endif. " number_errors_transaction is initial   ' TO CODE.

* Meldungsliste auswerten
  APPEND '*                                                 ' TO CODE.
  APPEND '  if not amerrdat[] is initial.                   ' TO CODE.
  APPEND '*   Auswertung der Antwort                        ' TO CODE.
  CONCATENATE HOCHKOMMA 'material_maintain_dark:'
    HOCHKOMMA '(004)' INTO CODE.
  CONCATENATE '    concatenate' CODE INTO CODE SEPARATED BY ' '.
  APPEND CODE.
  CONCATENATE HOCHKOMMA 'Meldungsliste' HOCHKOMMA '(006)' INTO CODE.
  APPEND CODE.
  APPEND '      into fehler_tab-line separated by blank.    ' TO CODE.
  APPEND '    append fehler_tab.                            ' TO CODE.
  APPEND '    data: amerrdat_index(4).                      ' TO CODE.

* Schleife über die Auswertetabelle
  APPEND '    loop at amerrdat.                             ' TO CODE.
  APPEND '      move sy-tabix to amerrdat_index.            ' TO CODE.
  APPEND '*     Fehlermeldung nachschlagen                  ' TO CODE.
  CONCATENATE '      call function ' HOCHKOMMA
    'RPY_MESSAGE_COMPOSE' HOCHKOMMA INTO CODE.
  APPEND CODE.
  APPEND '          exporting                               ' TO CODE.
  APPEND '            language          = sy-langu          ' TO CODE.
  APPEND '            message_id        = amerrdat-msgid    ' TO CODE.
  APPEND '            message_number    = amerrdat-msgno    ' TO CODE.
  APPEND '            message_var1      = amerrdat-msgv1    ' TO CODE.
  APPEND '            message_var2      = amerrdat-msgv2    ' TO CODE.
  APPEND '            message_var3      = amerrdat-msgv3    ' TO CODE.
  APPEND '            message_var4      = amerrdat-msgv4    ' TO CODE.
  APPEND '          importing                               ' TO CODE.
  APPEND '            message_text      = sy-lisel          ' TO CODE.
  APPEND '*         tables                                  ' TO CODE.
  APPEND '*            longtext          =                  ' TO CODE.
  APPEND '         exceptions                               ' TO CODE.
  APPEND '              message_not_found = 1               ' TO CODE.
  APPEND '              others            = 2.              ' TO CODE.
  APPEND '*                                                 ' TO CODE.
* Auswertung Message compose
  APPEND '       if sy-subrc = 0.                           ' TO CODE.
  APPEND '         concatenate amerrdat_index sy-lisel      ' TO CODE.
  APPEND '          into fehler_tab-line separated by blank.' TO CODE.
  APPEND '         append fehler_tab.                       ' TO CODE.
  APPEND '       else. " sy-subrc = 0                       ' TO CODE.
*
  CONCATENATE HOCHKOMMA 'rpy_message_compose'
    HOCHKOMMA '(008)' INTO CODE.
  CONCATENATE '         concatenate' CODE 'amerrdat_index'
   INTO CODE SEPARATED BY ' '.
  APPEND CODE.
  APPEND '             amerrdat-msgid amerrdat-msgno        ' TO CODE.
  APPEND '             amerrdat-msgv1(10)                   ' TO CODE.
  APPEND '             amerrdat-msgv2(10) amerrdat-msgv3(10)' TO CODE.
  APPEND '             amerrdat-msgv4(10)                   ' TO CODE.
  APPEND '           into fehler_tab-line separated by blank.' TO CODE.
  APPEND '         append fehler_tab.                       ' TO CODE.
  APPEND '       endif. " sy-subrc = 0                      ' TO CODE.
  APPEND '    endloop. " at amerrdat                        ' TO CODE.
  APPEND '    append initial line to fehler_tab.            ' TO CODE.
  APPEND '  endif. " not amerrdat[] is initial              ' TO CODE.
  APPEND '*                                                 ' TO CODE.

ENDFORM.                    " FUBAU_MAT_MAINT_DARK


*---------------------------------------------------------------------*
*      Form  FOOTER_SUBROUTINE
*---------------------------------------------------------------------*
FORM FOOTER_SUBROUTINE.

* Ausgaben der Fehler / Erfolgsmeldungen
  APPEND '*                                                 ' TO CODE.
  APPEND 'loop at fehler_tab.                               ' TO CODE.
  APPEND '  write : / fehler_tab.                           ' TO CODE.
  APPEND 'endloop. " at fehler_tab                          ' TO CODE.
  APPEND 'uline.                                            ' TO CODE.
  APPEND '*                                                 ' TO CODE.

* Ausgabe für Entwicklung
  IF NOT P_CODE IS INITIAL.
    APPEND '*                                               ' TO CODE.
    APPEND '* TEST                                          ' TO CODE.
    APPEND '  skip.                                         ' TO CODE.
    CONCATENATE '  WRITE: / ' HOCHKOMMA
      ' Subroutine: Ausgabe für Entwicklung [innerhalb Subr.] '
      HOCHKOMMA '.' INTO CODE.
    APPEND CODE.
    CONCATENATE HOCHKOMMA 'matnr_last :' HOCHKOMMA ',' INTO CODE.
    CONCATENATE '  write : /' CODE 'matnr_last.'
      INTO CODE SEPARATED BY ' '.
    APPEND CODE.
    CONCATENATE HOCHKOMMA 'matnr_last_save :' HOCHKOMMA ',' INTO CODE.
    CONCATENATE '  write : /' CODE 'matnr_last_save.'
      INTO CODE SEPARATED BY ' '.
    APPEND CODE.

    CONCATENATE HOCHKOMMA 'aeine_ueb :' HOCHKOMMA INTO CODE.
    CONCATENATE '  write : /' CODE '.' INTO CODE SEPARATED BY ' '.
    APPEND CODE.
    APPEND '  loop at aeine_ueb.                            ' TO CODE.
    APPEND '    write: /                                    ' TO CODE.
    APPEND '      aeine_ueb-lfdnr,                          ' TO CODE.
    APPEND '      aeine_ueb-lifnr,                          ' TO CODE.
    APPEND '      aeine_ueb-matnr,                          ' TO CODE.
    APPEND '      aeine_ueb-minbm,                          ' TO CODE.
    APPEND '      aeine_ueb-norbm,                          ' TO CODE.
    APPEND '      aeine_ueb-uebto,                          ' TO CODE.
    APPEND '      aeine_ueb-untto,                          ' TO CODE.
    APPEND '      aeine_ueb-mwskz.                          ' TO CODE.
    APPEND '  endloop. " at aeine_ueb                       ' TO CODE.

*    APPEND ' write : / fest_x.                             ' TO CODE.
*    APPEND ' write : / amarc_ueb-werks.                    ' TO CODE.
*    APPEND ' write : / fest_e.                             ' TO CODE.
*    APPEND ' write : / ambew_ueb-bwkey.                    ' TO CODE.
*    APPEND ' write : / fest_x, fest_x.                     ' TO CODE.
*    APPEND ' write : / blank.                              ' TO CODE.
*    APPEND ' write : / fest_e, fest_e.                     ' TO CODE.
   APPEND '  skip.                                           ' TO CODE.
  ENDIF. " p_code is initial



* Subpool-Form-Ende
  APPEND 'ENDFORM. " MATERIAL_VERARBEITEN                   ' TO CODE.
  APPEND '*                                                 ' TO CODE.

* FORM Zeile schreiben
  APPEND '*                                                 ' TO CODE.
  APPEND 'FORM zeile_sichern                                ' TO CODE.
  APPEND '  tables aeine_ueb like aeine_ueb[]               ' TO CODE.
  APPEND '  using zaehler.                                  ' TO CODE.
  CONCATENATE HOCHKOMMA 'MM02' HOCHKOMMA '.' INTO CODE.
  CONCATENATE '  amara_ueb-TCODE =' CODE INTO CODE SEPARATED BY ' '.
  APPEND CODE.
*tn !!!!!!! zaehler
*  concatenate hochkomma zaehler hochkomma '.' into code.
*  concatenate 'amara_ueb-tranc =' code into code separated by ' '.
*  append code.
  APPEND '  amara_ueb-mandt = sy-mandt.                   ' TO CODE.
  APPEND '  amara_ueb-tranc = zaehler.                    ' TO CODE.
  APPEND 'append amara_ueb.                               ' TO CODE.

  APPEND '  amarc_ueb-mandt = sy-mandt.                   ' TO CODE.
  APPEND '  amarc_ueb-MATNR = amara_ueb-MATNR.            ' TO CODE.
  APPEND '  amarc_ueb-tranc = amara_ueb-tranc.            ' TO CODE.
  APPEND 'append amarc_ueb.                               ' TO CODE.

  APPEND '  ambew_ueb-mandt = sy-mandt.                   ' TO CODE.
  APPEND '  ambew_ueb-MATNR = amara_ueb-MATNR.            ' TO CODE.
  APPEND '  ambew_ueb-BWKEY = amarc_ueb-werks.            ' TO CODE.
  APPEND '  ambew_ueb-BWTAR = blank.                      ' TO CODE.
  APPEND '  ambew_ueb-tranc = amara_ueb-tranc.            ' TO CODE.
  APPEND 'append ambew_ueb.                               ' TO CODE.

  APPEND '  amakt_ueb-mandt = sy-mandt.                   ' TO CODE.
  APPEND '  amakt_ueb-spras = sy-langu.                   ' TO CODE.
  APPEND '  amakt_ueb-MATNR = amara_ueb-MATNR.            ' TO CODE.
  APPEND '  amakt_ueb-tranc = amara_ueb-tranc.            ' TO CODE.
  APPEND 'append amakt_ueb.                               ' TO CODE.

  APPEND '  amvke_ueb-mandt = sy-mandt.                   ' TO CODE.
  APPEND '  amvke_ueb-MATNR = amara_ueb-MATNR.            ' TO CODE.
  APPEND '  amvke_ueb-tranc = amara_ueb-tranc.            ' TO CODE.
  APPEND 'append amvke_ueb.                               ' TO CODE.
  APPEND '*                                               ' TO CODE.
  APPEND '* SONSTIGE TABELLEN                             ' TO CODE.
  APPEND '  aeine_ueb-lfdnr = zaehler.                    ' TO CODE.
  APPEND 'append aeine_ueb.                               ' TO CODE.
  APPEND 'append aj_1ne_ueb.                              ' TO CODE.
  APPEND 'append aj_1nemm_ueb.                            ' TO CODE.
  APPEND 'append ek_txt.                                  ' TO CODE.
  APPEND '*                                               ' TO CODE.
  APPEND '*                                               ' TO CODE.

  APPEND 'ENDFORM. " Zeile_sichern                        ' TO CODE.


ENDFORM.                    " FOOTER_SUBROUTINE


*---------------------------------------------------------------------*
*      Form  MATERIAL_BUCHEN
*---------------------------------------------------------------------*
FORM MATERIAL_BUCHEN.
* Generierung der Subroutine
  GENERATE SUBROUTINE POOL CODE NAME PROG
                           MESSAGE MSG
                           LINE    LIN
                           WORD    WRD
                           OFFSET  OFF.
  IF SY-SUBRC <> 0.
   WRITE: / 'Error during generation in line', LIN,
          / MSG,
          / 'Word:', WRD, 'at offset', OFF.
  ELSE.
*   write: / 'The name of the subroutine pool is', prog.
*   skip.
   PERFORM MATERIAL_VERARBEITEN IN PROGRAM (PROG)
     TABLES FEHLER_TAB AEINE_UEB
     USING  LAST_MATNR.
*   write: / 'ENDE dyn1 in program (prog)'.
  ENDIF.

* nur für Testzwecke
  IF NOT P_CODE IS INITIAL.
    WRITE / '~~~~~~~~~~~~~~~~~~~~~~~~~~~'.
    WRITE: / 'Tabelle *code* in Hüllprogramm:', SY-REPID.
    LOOP AT CODE.
      WRITE: / SY-TABIX, CODE.
    ENDLOOP.
    WRITE / '~~~~~~~~~~~~~~~~~~~~~~~~~~~'.
    WRITE: / 'Ausgabe aeine_ueb im Hüllprogramm: ', SY-REPID.
    LOOP AT AEINE_UEB.
      WRITE: /
        AEINE_UEB-WERKS,
        AEINE_UEB-EKORG,
        '#',
        AEINE_UEB-LFDNR,
        AEINE_UEB-LIFNR,
        AEINE_UEB-MATNR,
        AEINE_UEB-MINBM,
        AEINE_UEB-NORBM,
        AEINE_UEB-UEBTO,
        AEINE_UEB-UNTTO,
        AEINE_UEB-MWSKZ.
    ENDLOOP. " at aeine_ueb
    WRITE / '~+~+~+~+~+~+~+~+~+~+~+~+~+~'.
  ENDIF. "  not p_code is initial

ENDFORM.                    " MATERIAL_BUCHEN


*---------------------------------------------------------------------*
*      Form  EINKAUFSINFOS_BUCHEN
*---------------------------------------------------------------------*
FORM EINKAUFSINFOS_BUCHEN.
* Tabelle für Messages des Call Transaction
  DATA: BEGIN OF XMESSTAB OCCURS 100.
          INCLUDE STRUCTURE BDCMSGCOLL.
  DATA: END OF XMESSTAB.

* Einkausfinfosätze alle einzeln per BTCI buchen
  LOOP AT AEINE_UEB. " where not lifnr is initial.
*   Lifnr ist z.B. initial, wenn kein EINE-Satz im Werk vorhanden
*    check sy-tabix = 1.
*   Formatumwandlung
    DATA: AEINE_UEB_UNTTO(5),
          AEINE_UEB_UEBTO(5),
          AEINE_UEB_NORBM(15),
          AEINE_UEB_MINBM(15).
    CHECK NOT AEINE_UEB-LIFNR IS INITIAL.
    MOVE AEINE_UEB-UNTTO TO AEINE_UEB_UNTTO.
    MOVE AEINE_UEB-UEBTO TO AEINE_UEB_UEBTO.
    MOVE AEINE_UEB-NORBM TO AEINE_UEB_NORBM.
    MOVE AEINE_UEB-MINBM TO AEINE_UEB_MINBM.

    REPLACE ',' WITH ' ' INTO AEINE_UEB_UNTTO.
    REPLACE ',' WITH ' ' INTO AEINE_UEB_UNTTO.
    CONDENSE AEINE_UEB_UNTTO NO-GAPS.
    REPLACE '.' WITH ',' INTO AEINE_UEB_UNTTO.

    REPLACE ',' WITH ' ' INTO AEINE_UEB_UEBTO.
    REPLACE ',' WITH ' ' INTO AEINE_UEB_UEBTO.
    CONDENSE AEINE_UEB_UEBTO NO-GAPS.
    REPLACE '.' WITH ',' INTO AEINE_UEB_UEBTO.

    REPLACE ',' WITH ' ' INTO AEINE_UEB_NORBM.
    REPLACE ',' WITH ' ' INTO AEINE_UEB_NORBM.
    CONDENSE AEINE_UEB_NORBM NO-GAPS.
    REPLACE '.' WITH ',' INTO AEINE_UEB_NORBM.
*    shift aeine_ueb_norbm right deleting trailing ' '.

    REPLACE ',' WITH ' ' INTO AEINE_UEB_MINBM.
    REPLACE ',' WITH ' ' INTO AEINE_UEB_MINBM.
    CONDENSE AEINE_UEB_MINBM NO-GAPS.
    REPLACE '.' WITH ',' INTO AEINE_UEB_MINBM.
*    shift aeine_ueb_minbm right deleting trailing ' '.

    PERFORM BDC_DYNPRO      USING 'SAPMM06I'   '0100'.
    PERFORM BDC_FIELD       USING 'BDC_OKCODE' '/00'.         " ENTER
    PERFORM BDC_FIELD       USING 'BDC_CURSOR' 'EINE-WERKS'.
    PERFORM BDC_FIELD       USING 'EINA-LIFNR' AEINE_UEB-LIFNR.
    PERFORM BDC_FIELD       USING 'EINA-MATNR' AEINE_UEB-MATNR.
    PERFORM BDC_FIELD       USING 'EINE-EKORG' AEINE_UEB-EKORG.
    PERFORM BDC_FIELD       USING 'EINE-WERKS' AEINE_UEB-WERKS.
*   springen Einkausforgdaten = EINE
    PERFORM BDC_DYNPRO      USING 'SAPMM06I'   '0101'.
    PERFORM BDC_FIELD       USING 'BDC_OKCODE' 'EINE'.
    PERFORM BDC_FIELD       USING 'BDC_CURSOR' 'EINA-MAHN1'.
    PERFORM BDC_DYNPRO      USING 'SAPMM06I'   '0102'.
*   Daten ändern
    PERFORM BDC_FIELD       USING 'BDC_OKCODE' 'BU'.
    PERFORM BDC_FIELD       USING 'BDC_CURSOR' 'EINE-MWSKZ'.
    IF NOT AEINE_UEB_UNTTO IS INITIAL.
      PERFORM BDC_FIELD       USING 'EINE-UNTTO' AEINE_UEB_UNTTO.
    ENDIF. " not aeine_ueb_untto is initial
    IF NOT AEINE_UEB_UEBTO IS INITIAL.
      PERFORM BDC_FIELD       USING 'EINE-UEBTO' AEINE_UEB_UEBTO.
    ENDIF. " not aeine_ueb_uebto is initial
    IF NOT AEINE_UEB_NORBM IS INITIAL.
      PERFORM BDC_FIELD       USING 'EINE-NORBM' AEINE_UEB_NORBM.
    ENDIF. " not aeine_ueb_norbm is initial
    IF NOT AEINE_UEB_MINBM IS INITIAL.
      PERFORM BDC_FIELD       USING 'EINE-MINBM' AEINE_UEB_MINBM.
    ENDIF. " not aeine_ueb_minbm is initial
    IF NOT AEINE_UEB-MWSKZ IS INITIAL.
      PERFORM BDC_FIELD       USING 'EINE-MWSKZ' AEINE_UEB-MWSKZ.
    ENDIF. " not aeine_ueb-mwskz is initial
*    perform bdc_transaction using 'ME12'.
*tnt
    CALL TRANSACTION 'ME12' USING XBDCDATA MODE P_E_BTCI UPDATE 'S'
                                MESSAGES INTO XMESSTAB.
    IF SY-SUBRC EQ 0.
      DATA: MESSAGE_LINE(80) TYPE C.    " im FB 300 Char!
      LOOP AT XMESSTAB.
        CALL FUNCTION 'MESSAGE_PREPARE'
             EXPORTING
               LANGUAGE   = SY-LANGU
               MSG_ID     = XMESSTAB-MSGID
               MSG_NO     = XMESSTAB-MSGNR
               MSG_VAR1   = XMESSTAB-MSGV1
               MSG_VAR2   = XMESSTAB-MSGV2
               MSG_VAR3   = XMESSTAB-MSGV3
               MSG_VAR4   = XMESSTAB-MSGV3
            IMPORTING
               MSG_TEXT   = MESSAGE_LINE
            EXCEPTIONS
               FUNCTION_NOT_COMPLETED = 1
               MESSAGE_NOT_FOUND      = 2
               OTHERS                 = 3.
        WRITE : / MESSAGE_LINE,
                  AEINE_UEB-LFDNR, 'Material'(008), AEINE_UEB-MATNR,
                  'Werk'(021), AEINE_UEB-WERKS,
                  AEINE_UEB_UNTTO, AEINE_UEB_UEBTO,
                  AEINE_UEB_NORBM, AEINE_UEB_MINBM,
                  AEINE_UEB-MWSKZ.
      ENDLOOP. " at xmesstab
      COMMIT WORK.
**     10 Sekunden auf Verbuchung warten!
*      DATA: ZEIT LIKE SY-UZEIT.
*      CLEAR ZEIT.
*      GET TIME FIELD ZEIT.
*      ZEIT = ZEIT + 10.
*      DO.
*        GET TIME.
*        IF SY-UZEIT GT ZEIT.
*          EXIT.
*        ENDIF.
*      ENDDO.

    ELSE. " sy-subrc eq 0
      WRITE: / 'Einkaufsinfosatz ändern fehlgeschlagen.'(022),
               AEINE_UEB-LFDNR,
               AEINE_UEB-LIFNR, AEINE_UEB-EKORG,
               AEINE_UEB-MATNR, AEINE_UEB-WERKS.
    ENDIF. " sy-subrc eq 0
    REFRESH: XBDCDATA, XMESSTAB.
  ENDLOOP. " at aeine_ueb
ENDFORM.                    " EINKAUFSINFOS_BUCHEN


*---------------------------------------------------------------------*
*       FORM BDC_DYNPRO                                               *
*---------------------------------------------------------------------*
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
  CLEAR XBDCDATA.
  MOVE: PROGRAM TO XBDCDATA-PROGRAM,
        DYNPRO  TO XBDCDATA-DYNPRO,
        'X'     TO XBDCDATA-DYNBEGIN.
  APPEND XBDCDATA.
ENDFORM.                               " DYNPRO_ANFANG


*---------------------------------------------------------------------*
*      Form  BDC_FIELD
*---------------------------------------------------------------------*
FORM BDC_FIELD USING FNAM FVAL.
  CLEAR XBDCDATA.
  MOVE: FNAM     TO XBDCDATA-FNAM,
        FVAL     TO XBDCDATA-FVAL.
  APPEND XBDCDATA.
ENDFORM.                               " BDC_FIELD