[ABAP] Material-Ledger: Materialpreise für unterschiedliche Währungstypen pflegen

* MATNR
PARAMETERS: p_matnr TYPE mara-matnr.
* Bewertungskreis
PARAMETERS: p_bwkey TYPE t001k-bwkey.
* Preis
PARAMETERS: p_price TYPE bapi_matval_prices-price DEFAULT '1.00'.
* Währung
PARAMETERS: p_waers TYPE tcurc-waers DEFAULT 'EUR'.

START-OF-SELECTION.

* Bewertungskreis (MBEW-BWKEY) == WERKS
  DATA(lv_valuationarea) = CONV bapi_matval_key-val_area( p_bwkey ).
* Bewertungsart (MBEW-BWTAR), siehe Tabelle T149D
  DATA(lv_valuationtype) = CONV bapi_matval_key-val_type( ' ' ).
* Struktur Preisänderungs-Datum
  DATA(lv_pricedate) = VALUE bapi_matval_pricedate( price_date  = sy-datum
                                                    fisc_period = sy-datum+4(2)
                                                    fisc_year   = sy-datum+0(4)
                                                  ).
* Belegkopftext
  DATA(lv_doc_header_text) = CONV bktxt( 'Materialbeleg Text' ).

* Material-Ledger-Typ zum Bewertungskreis
  SELECT SINGLE FROM tckm2
    FIELDS matled
    WHERE bwkey = @p_bwkey
    INTO @DATA(lv_matled).

  IF sy-subrc = 0.
* Währungs-, Bewertungstypen zum Material-Ledger-Typ
* CURTP (Währungstyp)
    SELECT FROM tckmit
      INNER JOIN tcval ON tckmit~cvtyp = tcval~cvtyp
      FIELDS tckmit~cvtyp,  " Währungs- und Bewertungstyp
             tcval~currtyp, " Währungstyp
             tcval~valutyp  " Bewertungssicht
      WHERE matled = @lv_matled
      INTO TABLE @DATA(it_cvtyp).

    IF sy-subrc = 0.
* Währungscodes
      SELECT SINGLE FROM tcurc
        FIELDS altwr
        WHERE waers = @p_waers
        INTO @DATA(lv_iso).

      IF sy-subrc = 0.
* Preise für alle Währungstypen (CVTYP) setzen
        DATA: it_prices TYPE STANDARD TABLE OF bapi_matval_prices WITH DEFAULT KEY.

        LOOP AT it_cvtyp ASSIGNING FIELD-SYMBOL(<cv>).
* Beispiel:
*   Preise + Währungen für alle Währungstypen des Ledgers zum Bewertungskreis gleich setzen
* Beachte: es gibt andere Anwendungsfälle, insbesondere bei abweichenden Währungstypen (anderen Konzernwährungen (USD usw.)),
*          bei denen die Preise unterschiedlich sein können
* siehe auch MM03 (Reiter "Buchhaltung 1")
          APPEND VALUE #( valuation_view = <cv>-valutyp            " DOMÄNE VALUTYP: 0  - Legale Bewertung, 1 - Konzernbewertung, 2 - Profit-Center-Bewertung
                          curr_type      = <cv>-cvtyp              " 10 - Buchungskreiswährung, 30 - Konzernwährung, 31 - Konzernwährung, Konzernbewertung
                          currency       = p_waers                 " EUR
                          currency_iso   = lv_iso                  " EUR -> 978
                          price_unit     = 1                       " Preiseinheit aus MBEW-BWPEI
                          price          = p_price ) TO it_prices. " Preis ggf. aus MBEW-VERPR
        ENDLOOP.

* Materialpreis-Änderungsbeleg
        DATA: lv_pricechangedocument TYPE bapi_pricechange_document.
* Fehlermeldungen des BAPIs
        DATA(it_bapiret2) = VALUE bapiret2_t( ).

* Materialpreis-Änderung
* Voraussetzung ist Pflege der Materialbewertung zum Materialstamm
        CALL FUNCTION 'BAPI_MATVAL_PRICE_CHANGE'
          EXPORTING
            material            = CONV bapi_matval_key-material( p_matnr ) " MARA-MATNR
            valuationarea       = lv_valuationarea                         " MBEW-BWKEY
            valuationtype       = lv_valuationtype                         " MBEW-BWTAR
            pricedate           = lv_pricedate
            doc_header_text     = lv_doc_header_text
          IMPORTING
            pricechangedocument = lv_pricechangedocument
          TABLES
            prices              = it_prices
            return              = it_bapiret2.

        IF lines( it_bapiret2 ) > 0.
          IF it_bapiret2[ 1 ]-type CA 'EAX'.
* bei Fehler: Rollback
            CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
          ELSE.
            DATA(lv_return) = VALUE bapiret2( ).
* alles ok: COMMIT
            CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
              EXPORTING
                wait   = abap_true
              IMPORTING
                return = lv_return.
          ENDIF.
        ENDIF.

        cl_demo_output=>write_data( p_matnr ).
        cl_demo_output=>write_data( p_bwkey ).
        cl_demo_output=>write_data( lv_pricedate ).
        cl_demo_output=>write_data( lv_doc_header_text ).
        cl_demo_output=>write_data( it_prices ).
        cl_demo_output=>write_data( lv_pricechangedocument ).
        cl_demo_output=>write_data( it_bapiret2 ).
        cl_demo_output=>write_data( lv_return ).

* HTML-Code vom Demo-Output holen
        DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
        cl_abap_browser=>show_html( EXPORTING title       = 'Daten'
                                              html_string = lv_html
                                              container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
        WRITE: space.
      ENDIF.
    ENDIF.
  ENDIF.

[ABAP] DMS: Dokumenteninfosätze löschen (BAPI_DOCUMENT_DELETE_DIRECT)

* Dokumentart (TA DC10, Tabelle TDWA)
PARAMETERS: p_dtype TYPE bapi_doc_aux-doctype OBLIGATORY.
* Objekttyp (Material -> MARA)
PARAMETERS: p_otype TYPE bapi_doc_drad-objecttype OBLIGATORY DEFAULT 'MARA'.
* Objektkey (Material -> MATNR)
PARAMETERS: p_okey TYPE bapi_doc_drad-objectkey OBLIGATORY DEFAULT '000000001122334455'.

START-OF-SELECTION.

  DATA: it_doc_keys TYPE STANDARD TABLE OF bapi_doc_keys WITH DEFAULT KEY.
  DATA: lv_return   TYPE bapiret2.

* Determine Documents for an Object
  CALL FUNCTION 'BAPI_DOCUMENT_GETOBJECTDOCS'
    EXPORTING
      objecttype          = p_otype
      objectkey           = p_okey
      currentversionsonly = abap_false " alle Versionen lesen
      date                = sy-datum
    IMPORTING
      return              = lv_return
    TABLES
      documentlist        = it_doc_keys.

  IF lv_return-type CA 'EA'.
    WRITE: / lv_return-message.
  ELSE.
* Alle gefundenen Dokumenteninfosätze durchgehen
    LOOP AT it_doc_keys ASSIGNING FIELD-SYMBOL(<d>).
* Wenn Dokumenttyp gefunden
      IF <d>-documenttype = p_dtype.
        DATA: ret TYPE string.

* Abfrage zum Löschen
        CALL FUNCTION 'POPUP_TO_CONFIRM'
          EXPORTING
            titlebar              = 'Frage'
            text_question         = |Löschen DI { <d>-documentnumber } ?|
            display_cancel_button = abap_true
          IMPORTING
            answer                = ret.

        CASE ret.
          WHEN '1'.
* Ja --> löschen
* Delete document or set deletion indicator
            CALL FUNCTION 'BAPI_DOCUMENT_DELETE_DIRECT'
              EXPORTING
                documenttype    = <d>-documenttype
                documentnumber  = <d>-documentnumber
                documentpart    = <d>-documentpart
                documentversion = <d>-documentversion
              IMPORTING
                return          = lv_return.

            IF lv_return-type CA 'EA'.
* Rollback bei Fehler
              CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
            ELSE.
* Daten verbuchen
              CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
                EXPORTING
                  wait = abap_true.
            ENDIF.
          WHEN OTHERS.

        ENDCASE.

      ENDIF.
    ENDLOOP.
  ENDIF.

[ABAP] DMS: Dateien an Dokumenteninfosätze anhängen (CVAPI_DOC_CREATE)

TYPES: BEGIN OF bin_file,
         name TYPE string,
         size TYPE i,
         data TYPE solix_tab,
       END OF bin_file.

* Dokumentart (TA DC10, Tabelle TDWA)
PARAMETERS: p_type TYPE bapi_doc_aux-doctype OBLIGATORY.
* Teildokument
PARAMETERS: p_part TYPE bapi_doc_aux-docpart OBLIGATORY DEFAULT '000'.
* Dokumentversion
PARAMETERS: p_ver TYPE bapi_doc_aux-docversion OBLIGATORY DEFAULT '00'.
* Beschreibung
PARAMETERS: p_desc TYPE bapi_doc_drat-description DEFAULT 'Test 123'.
* Ablagekategorie (TA OACT, Tabelle SDOKSTCAE, View V_SDOKSTCA)
PARAMETERS: p_stor TYPE bapi_doc_files2-storagecategory OBLIGATORY.
* Logisches Dokument/Workstation Application (TA S_ALR_87008829, Tabelle TDWP, Bild -> JPG)
PARAMETERS: p_appl TYPE bapi_doc_files2-application_id OBLIGATORY DEFAULT 'JPG'.
* Objekttyp (Material -> MARA)
PARAMETERS: p_otype TYPE bapi_doc_drad-objecttype OBLIGATORY DEFAULT 'MARA'.
* Objektkey (Material -> MATNR)
PARAMETERS: p_okey TYPE bapi_doc_drad-objectkey OBLIGATORY DEFAULT '000000001122334455'.

START-OF-SELECTION.
  TRY.
      DATA: lv_rc TYPE i.
      DATA: it_files TYPE filetable.
      DATA: lv_action TYPE i.

* FileOpen-Dialog aufrufen
      cl_gui_frontend_services=>file_open_dialog( EXPORTING file_filter = |jpg (*.jpg)\|*.jpg\|{ cl_gui_frontend_services=>filetype_all }|
                                                  CHANGING  file_table  = it_files
                                                            rc          = lv_rc
                                                            user_action = lv_action ).

      IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
        IF lines( it_files ) > 0.
* Dateien auf den Appl.-Server hochladen
          DATA: lv_file TYPE bin_file.
          cl_gui_frontend_services=>gui_upload( EXPORTING filename   = CONV #( it_files[ 1 ]-filename )
                                                          filetype   = 'BIN'
                                                IMPORTING filelength = lv_file-size
                                                CHANGING  data_tab   = lv_file-data ).

          DATA: lv_xstring TYPE xstring.

* Datei binär zu xstring wandeln
          CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
            EXPORTING
              input_length = lv_file-size
            IMPORTING
              buffer       = lv_xstring
            TABLES
              binary_tab   = lv_file-data.

* Dokumentinformationssatz
          DATA(lv_draw) = VALUE draw(
                                      dokar = p_type
                                      doktl = p_part
                                      dokvr = p_ver
                                    ).

* DVS: Steuerung für APIS
          DATA(lv_api_control) = VALUE cvapi_api_control(
                                                            no_update_task = abap_true
                                                        ).

* Kurztexte für Dokuinfosätze
          DATA: it_drat TYPE STANDARD TABLE OF dms_db_drat WITH DEFAULT KEY.
          it_drat = VALUE #( (
                                langu = sy-langu
                                dktxt = p_desc
                             )
                           ).

* Verknüpfung Dokument-Objekt
          DATA: it_drad TYPE STANDARD TABLE OF dms_db_drad WITH DEFAULT KEY.

          it_drad = VALUE #( (
                               dokob = p_otype
                               objky = p_okey
                             )
                           ).

* Originale für Dokumente (Dateianhang)
          DATA: it_bin_content TYPE STANDARD TABLE OF drao WITH DEFAULT KEY.

          DATA: lv_data_length TYPE i.
          DATA: it_content_raw TYPE STANDARD TABLE OF orblk WITH DEFAULT KEY.

* xstring --> ORBLK (Binärformat mit Zeilenlänge 2550 Bytes)
          CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
            EXPORTING
              buffer        = lv_xstring
            IMPORTING
              output_length = lv_data_length
            TABLES
              binary_tab    = it_content_raw.

* Datenaufbereitung
          LOOP AT it_content_raw ASSIGNING FIELD-SYMBOL(<raw>).

            APPEND VALUE #(
                            zaehl = sy-tabix
                            appnr = '1'
                            dokar = p_type
                            orln  = lv_data_length
*                            orbkl = 2550
                            orblk = <raw>
                          ) TO it_bin_content.
          ENDLOOP.

* DVS: Originale zu einem Dokument
          DATA: it_doc_files TYPE STANDARD TABLE OF cvapi_doc_file WITH DEFAULT KEY.

          it_doc_files = VALUE #( ( updateflag  = 'I'
                                    appnr       = '1'
                                    dappl       = p_appl
                                    filename    = it_files[ 1 ]-filename
                                    storage_cat = p_stor
                                    description = p_desc ) ).

          DATA(lv_message) = VALUE messages( ).
          DATA(lv_pfx_dokar) = VALUE draw-dokar( ).
          DATA(lv_pfx_doknr) = VALUE draw-doknr( ).
          DATA(lv_pfx_dokvr) = VALUE draw-dokvr( ).
          DATA(lv_pfx_doktl) = VALUE draw-doktl( ).

* DVS: Dokument anlegen
          CALL FUNCTION 'CVAPI_DOC_CREATE'
            EXPORTING
              ps_draw            = lv_draw
              ps_api_control     = lv_api_control
              pf_content_provide = 'TBL' " Binärdaten des Files als Tabelle übergeben
            IMPORTING
              psx_message        = lv_message
              pfx_dokar          = lv_pfx_dokar
              pfx_doknr          = lv_pfx_doknr
              pfx_dokvr          = lv_pfx_dokvr
              pfx_doktl          = lv_pfx_doktl
            TABLES
              pt_drad_x          = it_drad
              pt_drat_x          = it_drat
              pt_files_x         = it_doc_files
              pt_content         = it_bin_content.

          IF lv_message-msg_type CA 'EA'.
* Rollback bei Fehler
            CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
          ELSE.
* Daten verbuchen
            CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
              EXPORTING
                wait = abap_true.

            cl_demo_output=>write_data( lv_message ).
            cl_demo_output=>write_data( lv_pfx_dokar ).
            cl_demo_output=>write_data( lv_pfx_doknr ).
            cl_demo_output=>write_data( lv_pfx_dokvr ).
            cl_demo_output=>write_data( lv_pfx_doktl ).

* HTML-Code vom Demo-Output holen
            DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
            cl_abap_browser=>show_html( EXPORTING title       = 'CVAPI_DOC_CREATE'
                                                  html_string = lv_html
                                                  container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
            WRITE: space.
          ENDIF.
        ENDIF.
      ENDIF.
    CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
  ENDTRY.

[ABAP] DMS: Dateien an Dokumenteninfosätze anhängen (BAPI_DOCUMENT_CREATE2)

* Dokumentart (TA DC10, Tabelle TDWA)
PARAMETERS: p_dtype TYPE bapi_doc_aux-doctype OBLIGATORY.
* Teildokument
PARAMETERS: p_dpart TYPE bapi_doc_aux-docpart OBLIGATORY DEFAULT '000'.
* Dokumentversion
PARAMETERS: p_dver TYPE bapi_doc_aux-docversion OBLIGATORY DEFAULT '00'.
* Beschreibung
PARAMETERS: p_desc TYPE bapi_doc_drat-description DEFAULT 'Test 123'.
* Ablagekategorie (TA OACT, Tabelle SDOKSTCAE, View V_SDOKSTCA)
PARAMETERS: p_stor TYPE bapi_doc_files2-storagecategory OBLIGATORY.
* Logisches Dokument/Workstation Application (TA S_ALR_87008829, Tabelle TDWP, Bild -> JPG)
PARAMETERS: p_appl TYPE bapi_doc_files2-application_id OBLIGATORY DEFAULT 'JPG'.
* Objekttyp (Material -> MARA)
PARAMETERS: p_otype TYPE bapi_doc_drad-objecttype OBLIGATORY DEFAULT 'MARA'.
* Objektkey (Material -> MATNR)
PARAMETERS: p_okey TYPE bapi_doc_drad-objectkey OBLIGATORY DEFAULT '000000001122334455'.

START-OF-SELECTION.

  TRY.
      DATA: lv_rc TYPE i.
      DATA: it_files TYPE filetable.
      DATA: lv_action TYPE i.

* FileOpen-Dialog aufrufen
      cl_gui_frontend_services=>file_open_dialog( EXPORTING file_filter = |jpg (*.jpg)\|*.jpg\|{ cl_gui_frontend_services=>filetype_all }|
                                                  CHANGING  file_table  = it_files
                                                            rc          = lv_rc
                                                            user_action = lv_action ).

      IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
        IF lines( it_files ) > 0.

          DATA(lv_bapi_doc_draw2) = VALUE bapi_doc_draw2(
                                                          documenttype    = p_dtype
                                                          documentversion = p_dver
                                                          documentpart    = p_dpart
                                                        ).

          DATA: lv_documenttype TYPE bapi_doc_aux-doctype.
          DATA: lv_documentnumber TYPE bapi_doc_aux-docnumber.
          DATA: lv_documentpart TYPE bapi_doc_aux-docpart.
          DATA: lv_documentversion TYPE bapi_doc_aux-docversion.
          DATA: lv_return TYPE bapiret2.

          DATA: it_documentdescriptions TYPE STANDARD TABLE OF bapi_doc_drat WITH DEFAULT KEY.
          it_documentdescriptions = VALUE #( (
                                                description  = p_desc
                                                language     = 'D'
                                                language_iso = 'DE'
                                             )
                                           ).

          DATA: it_documentfiles TYPE STANDARD TABLE OF bapi_doc_files2 WITH DEFAULT KEY.

          it_documentfiles = VALUE #( (
                                        originaltype      = '1'
*                                        sourcedatacarrier = 'SAP-SYSTEM' " oder 'DEFAULT'
                                        created_by        = sy-uname
                                        storagecategory   = p_stor
                                        docfile           = it_files[ 1 ]-filename
                                        wsapplication     = p_appl
*                                        checkedin         = abap_true
                                      )
                                    ).

          DATA: it_objectlinks TYPE STANDARD TABLE OF bapi_doc_drad WITH DEFAULT KEY.

          it_objectlinks = VALUE #( (
                                      objecttype    = p_otype
                                      objectkey     = p_okey
                                    )
                                  ).

* Create Document
          CALL FUNCTION 'BAPI_DOCUMENT_CREATE2'
            EXPORTING
              documentdata         = lv_bapi_doc_draw2
            IMPORTING
              documenttype         = lv_documenttype
              documentnumber       = lv_documentnumber
              documentversion      = lv_documentversion
              documentpart         = lv_documentpart
              return               = lv_return
            TABLES
              documentdescriptions = it_documentdescriptions
              documentfiles        = it_documentfiles
              objectlinks          = it_objectlinks.

          IF lv_return-type CA 'EA'.
* Rollback bei Fehler
            CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
          ELSE.
* Daten verbuchen
            CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
              EXPORTING
                wait = abap_true.

            cl_demo_output=>write_data( lv_documenttype ).
            cl_demo_output=>write_data( lv_documentnumber ).
            cl_demo_output=>write_data( lv_documentversion ).
            cl_demo_output=>write_data( lv_documentpart ).
            cl_demo_output=>write_data( lv_return ).

* HTML-Code vom Demo-Output holen
            DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
            cl_abap_browser=>show_html( EXPORTING title       = 'BAPI_DOCUMENT_CREATE2'
                                                  html_string = lv_html
                                                  container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
            WRITE: space.
          ENDIF.
        ENDIF.
      ENDIF.
    CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
  ENDTRY.

Links

[ABAP] Leistungstammsatz anlegen

DATA: lv_service  TYPE bapisrv_asmd.
DATA: lv_servicex TYPE bapisrv_asmdx.
DATA: it_bapiret2 TYPE STANDARD TABLE OF bapiret2 WITH DEFAULT KEY.

lv_service-service           = '1234567890'. " Leistungsnummer
lv_service-matl_group        = '10000000'.   " Warengruppe
lv_service-base_uom          = 'ST'.         " Basismengeneinheit
lv_service-serv_cat          = 'SERV'.       " Leistungstyp
lv_service-val_class         = '1000'.       " Bewertungsklasse
lv_service-master_langu      = sy-langu.     " Sprache
lv_service-master_langu_iso  = cl_i18n_languages=>sap1_to_sap2( im_lang_sap1 = lv_service-master_langu ).

lv_servicex-service          = lv_service-service.
lv_servicex-matl_group       = abap_true.
lv_servicex-base_uom         = abap_true.
lv_servicex-serv_cat         = abap_true.
lv_servicex-val_class        = abap_true.
lv_servicex-master_langu     = abap_true.
lv_servicex-master_langu_iso = abap_true.

DATA it_shorttext TYPE STANDARD TABLE OF bapisrv_asmdt WITH DEFAULT KEY.

it_shorttext = VALUE #( ( language     = lv_service-master_langu
                          language_iso = lv_service-master_langu_iso
                          short_text   = 'Kurztext'
                          change_id    = 'I' ) ).

DATA(it_longtext) = VALUE srv_sm_text_tty( ( language     = sy-langu
                                             language_iso = cl_i18n_languages=>sap1_to_sap2( im_lang_sap1 = sy-langu )
                                             format       = '/'
                                             line         = 'Langtext'
                                             change_id    = 'I' ) ).

CALL FUNCTION 'BAPI_SERVICE_CREATE'
  EXPORTING
    im_service_data     = lv_service
    im_service_datax    = lv_servicex
    testrun             = ''
  TABLES
    return              = it_bapiret2
    service_description = it_shorttext
    service_long_texts  = it_longtext.

TRY.
    IF lines( it_bapiret2 ) > 0.
      IF it_bapiret2[ 1 ]-type CA 'EAX'.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
      ENDIF.
    ENDIF.
  CATCH cx_sy_itab_line_not_found.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENDTRY.

cl_demo_output=>write_data( lv_service ).
cl_demo_output=>write_data( lv_servicex ).
cl_demo_output=>write_data( it_shorttext ).
cl_demo_output=>write_data( it_longtext ).
cl_demo_output=>write_data( it_bapiret2 ).

* HTML-Code vom Demo-Output holen
DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
cl_abap_browser=>show_html( EXPORTING
                              title        = 'Leistungsstammsatz'
                              html_string  = lv_html
                              container    = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
WRITE: space.

[ABAP] Warenbewegungen buchen

TYPES: BEGIN OF ty_s_material,
         matnr         TYPE mara-matnr,                            " Materialnummer
         erfmg         TYPE mseg-erfmg,                            " Menge in Erfassungsmengeneinheit (mseg-erfmg) / Menge (mseg-menge)
         entry_uom_iso TYPE bapi2017_gm_item_create-entry_uom_iso, " ISO-Code Maßeinheit
       END OF ty_s_material.

TYPES: ty_it_material TYPE STANDARD TABLE OF ty_s_material WITH KEY matnr.

PARAMETERS: p_test AS CHECKBOX DEFAULT 'X'.

START-OF-SELECTION.

  DATA(it_material) = VALUE ty_it_material( ( matnr = '0815' erfmg = '1' entry_uom_iso = 'PCE' )
                                            ( matnr = '0816' erfmg = '2' entry_uom_iso = 'PCE' )
                                          ).

* Header
  DATA(lv_header) = VALUE bapi2017_gm_head_01( pstng_date    = sy-datum " Buchungsdatum im Beleg
                                               doc_date      = sy-datum " Belegdatum im Beleg
                                               pr_uname      = sy-uname " Benutzername
                                               gr_gi_slip_no = '12345'  " Warenbegleitscheinnummer
                                               header_txt    = 'Text1'  " Belegkopftext
                                               ref_doc_no    = 'Refdoc' " Nummer des externen Materialscheins
                                             ).

* gm_code (siehe auch Tabelle T158G)
* 01  MB01 (Wareneingang zur Bestellung buchen)
* 02  MB31 (Wareneingang zum Fertigungsauftrag)
* 03  MB1A (Warenentnahme)
* 04  MB1B (Umbuchung)
* 05  MB1C (Wareneingang Sonstige)
* 06  MB11 (Warenbewegung)
* 07  MB04 (Nachverrechnung von Beistellmaterial)
  DATA(lv_mvt_code) = VALUE bapi2017_gm_code( gm_code = '03' ). " Zuordnung Code zu Transaktion für BAPI Warenbewegung

* Items für Bewegungsdaten füllen
  DATA(it_items_to_move) = VALUE bapi2017_gm_item_create_t( FOR <m> IN it_material
                                                            (
                                                              material  = CONV #( <m>-matnr ) " 18-stellige Materialnummer
                                                              plant     = '10'                " Werk
                                                              stge_loc  = '01'                " Lagerort
                                                              move_type = '262'               " Bewegungsart (Bestandsführung)
* KZBEW	(Bewegungskennzeichen)
* ' '  Warenbewegung ohne Referenz
*  B  Warenbewegung zur Bestellung
*  F  Warenbewegung zum Auftrag
*  L  Warenbewegung zum Lieferschein
*  K  Warenbewegung zur Kanban-Anforderung (Nur WM intern)
*  O  Nachverrechnung von Beistellmaterial
*  W  Nachverrechnung von Anteils-/Produkt ME Material
                                                              mvt_ind       = ' '                     " Bewegungskennzeichen (Warenbewegung ohne Referenz)
                                                              entry_qnt     = CONV #( <m>-erfmg )     " Menge in Erfassungsmengeneinheit
                                                              entry_uom_iso = <m>-entry_uom_iso       " ISO-Code Maßeinheit
                                                              gl_account    = ''                      " Nummer des Sachkontos
                                                              gr_rcpt       = 'ABC'                   " Warenempfänger
                                                              costcenter    = '100200'                " Kostenstelle
                                                              orderid       = ''                      " Auftragsnummer
                                                              wbs_elem      = ''                      " Projektstrukturplanelement (PSP-Element)
                                                            )
                                                          ).
  DATA(lv_headret) = VALUE bapi2017_gm_head_ret( ).
  DATA(it_return) = VALUE bapiret2_t( ).

  CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      goodsmvt_header  = lv_header
      goodsmvt_code    = lv_mvt_code
      testrun          = p_test
    IMPORTING
      goodsmvt_headret = lv_headret
    TABLES
      goodsmvt_item    = it_items_to_move
      return           = it_return.

* Fehlerauswertung: E - Error, A - Abort
  LOOP AT it_return ASSIGNING FIELD-SYMBOL(<e>).
    WRITE: / |RETURN [{ <e>-type } { <e>-number }]: { <e>-message }|.
  ENDLOOP.

  DATA(lv_return) = VALUE bapiret2( ).

  IF lines( it_return ) > 0.
* Rollback bei Fehler
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
      IMPORTING
        return = lv_return.

    WRITE: / |ROLLBACK [{ lv_return-type } { lv_return-number }]: { lv_return-message }|.
  ELSE.
* Daten verbuchen
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait   = abap_true
      IMPORTING
        return = lv_return.

    WRITE: / |COMMIT [{ lv_return-type } { lv_return-number }]: { lv_return-message }|.
  ENDIF.

Links

[ABAP] BAPI: Bestellung anlegen

PARAMETERS: p_bukrs TYPE bukrs DEFAULT '0001'.
PARAMETERS: p_bsart TYPE bsart DEFAULT 'NB'.
PARAMETERS: p_ekorg TYPE ekorg DEFAULT '0001'.
PARAMETERS: p_ekgrp TYPE ekgrp DEFAULT '123'.
PARAMETERS: p_werks TYPE werks DEFAULT '01'.
PARAMETERS: p_lgort TYPE lgort_d DEFAULT '10'.
PARAMETERS: p_matnr TYPE matnr DEFAULT '1234567890'.
PARAMETERS: p_vendor TYPE lifnr DEFAULT '0000012345'.

* Testmodus
PARAMETERS: p_test TYPE bapiflag-bapiflag DEFAULT abap_true.
* Authority-Check
PARAMETERS: p_noauth TYPE bapiflag-bapiflag DEFAULT abap_true.

* Returnparameter
DATA: it_return TYPE STANDARD TABLE OF bapiret2 WITH DEFAULT KEY.
DATA: it_poitem TYPE STANDARD TABLE OF bapimepoitem WITH DEFAULT KEY.
DATA: it_poitemx TYPE STANDARD TABLE OF bapimepoitemx WITH DEFAULT KEY.

DATA(ls_poheader) = VALUE bapimepoheader( comp_code  = p_bukrs
                                          doc_type   = p_bsart
                                          purch_org  = p_ekorg
                                          pur_group  = p_ekgrp
                                          doc_date   = sy-datum
                                          suppl_plnt = p_werks
                                          vendor     = p_vendor
                                        ).

DATA(ls_poheaderx) = VALUE bapimepoheaderx( comp_code  = abap_true
                                            doc_type   = abap_true
                                            purch_org  = abap_true
                                            pur_group  = abap_true
                                            doc_date   = abap_true
                                            suppl_plnt = abap_true
                                            vendor     = abap_true
                                          ).


it_poitem = VALUE #( ( po_item  = '00001'
                       material = p_matnr
                       plant    = p_werks
                       stge_loc = p_lgort
                       quantity = '1.0'
                     )
                   ).

it_poitemx = VALUE #( ( po_item  = '00001'
                        material = abap_true
                        plant    = abap_true
                        stge_loc = abap_true
                        quantity = abap_true
                      )
                    ).

DATA: lv_new_po TYPE bapimepoheader-po_number.
DATA: lv_expheader TYPE bapimepoheader.
DATA: lv_exppoexpimpheader TYPE bapieikp.

* Bestellung anlegen
CALL FUNCTION 'BAPI_PO_CREATE1'
  EXPORTING
    no_authority      = p_noauth
    testrun           = p_test
    poheader          = ls_poheader
    poheaderx         = ls_poheaderx
  IMPORTING
    exppurchaseorder  = lv_new_po
    expheader         = lv_expheader
    exppoexpimpheader = lv_exppoexpimpheader
  TABLES
    return            = it_return
    poitem            = it_poitem
    poitemx           = it_poitemx.

IF NOT lv_new_po IS INITIAL.
* Wenn Bestellung angelegt wurde, COMMIT durchführen
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait = abap_true.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENDIF.

* Ausgabe der Meldungen
cl_demo_output=>write_data( it_return ).
cl_demo_output=>write_data( it_poitem ).
cl_demo_output=>write_data( it_poitemx ).
cl_demo_output=>write_data( lv_new_po ).
cl_demo_output=>write_data( lv_expheader ).
cl_demo_output=>write_data( lv_exppoexpimpheader ).

* HTML-Code holen
DATA(lv_html) = cl_demo_output=>get( ).
* Daten im Inline-Browser im SAP-Fenster anzeigen
cl_abap_browser=>show_html( EXPORTING
                              title        = 'Daten zur Bestellung'
                              html_string  = lv_html
                              container    = cl_gui_container=>default_screen ).
* cl_gui_container=>default_screen erzwingen
WRITE: space.

[ABAP] MM: BAPI_MATERIAL_SAVEDATA – Anlegen und Ändern von Materialstammdaten

TYPES: ty_it_materialdescription TYPE STANDARD TABLE OF bapi_makt WITH DEFAULT KEY.

* Kopfdaten
DATA(lv_headdata) = VALUE bapimathead( material      = '123456'             " MATNR
                                       ind_sector    = CONV mbrsh( 'A' )    " Branche (Tabelle T137)
                                       matl_type     = CONV mtart( 'VERP' ) " Materialart (Tabelle T134)

                                       basic_view    = abap_true            " Sicht Grundddaten anlegen
                                       purchase_view = abap_true            " Sicht Einkauf anlegen
                                       account_view  = abap_true            " Sicht Buchhaltung
                                     ).
* Mandantenebene (MARA)
* Daten
DATA(lv_clientdata) = VALUE bapi_mara( del_flag   = abap_false               " Löschvormerkung auf Mandantenebene
                                       matl_group = CONV matkl( '00100000' ) " Warengruppe (Tabelle T023)
                                       base_uom   = CONV meins( 'KG' )       " Basismengeneinheit (Tabelle T006)
                                     ).

* Speicherflags
DATA(lv_clientdatax) = VALUE bapi_marax( del_flag   = abap_true " Löschvormerkung auf Mandantenebene setzen
                                         matl_group = abap_true " Warengruppe setzen
                                         base_uom   = abap_true " Basismengeneinheit setzen
                                       ).
* Werksebene (MARC)
* Daten
DATA(lv_plantdata) = VALUE bapi_marc( plant     = CONV werks_d( '0001' ) " Werk (Tabelle T001W)
                                      del_flag  = abap_false             " Löschvormerkung auf Werksebene
                                      pur_group = CONV ekgrp( '001' )    " Einkäufergruppe (Tabelle T024)
                                    ).

* Speicherflags
DATA(lv_plantdatax) = VALUE bapi_marcx( plant     = CONV werks_d( '0001' ) " Werk (Tabelle T001W)
                                        del_flag  = abap_true              " Löschvormerkung auf Werksebene setzen
                                        pur_group = abap_true              " Einkäufergruppe setzen
                                      ).

* Materialbewertung (MBEW)
* Daten
DATA(lv_valuationdata) = VALUE bapi_mbew( val_area  = CONV bwkey( '0001' )  " Bewertungskreis (Tabelle T001K)
                                          val_type  = CONV bwtar_d( 'ABC' ) " Bewertungsarten (Tabelle T149D)
                                          val_class = CONV bklas( '1234' )  " Bewertungsklasse (Tabelle T025)
                                          del_flag  = abap_false            " Löschvormerkung für alle Materialdaten einer Bewertungsart
                                        ).

* Speicherflags
DATA(lv_valuationdatax) = VALUE bapi_mbewx( val_area = CONV bwkey( '0001' )  " Bewertungskreis (Tabelle T001K)
                                            val_type = CONV bwtar_d( 'ABC' ) " Bewertungsarten (Tabelle T149D)
                                            val_class = abap_true            " Bewertungsklasse setzen
                                            del_flag  = abap_true            " Löschvormerkung setzen
                                          ).

* Lagerort (MARD)
* Daten
DATA(lv_storagelocationdata) = VALUE bapi_mard( plant    = CONV werks_d( '0001' ) " Werk (Tabelle T001W)
                                                stge_loc = CONV lgort_d( '1000' ) " Lagerort (Tabelle T001L)
                                                del_flag = abap_false             " Material auf Lagerortebene zum Löschen vormerken
                                              ).

* Speicherflags
DATA(lv_storagelocationdatax) = VALUE bapi_mardx( plant    = CONV werks_d( '0001' ) " Werk (Tabelle T001W)
                                                  stge_loc = CONV lgort_d( '1000' ) " Lagerort (Tabelle T001L)
                                                  del_flag = abap_false             " Löschvormerkung setzen
                                                ).

* Materialkurztext
DATA(it_materialdescription) = VALUE ty_it_materialdescription( ( langu     = sy-langu
                                                                  langu_iso = cl_i18n_languages=>sap1_to_sap2( im_lang_sap1 = sy-langu )
                                                                  matl_desc = 'Schraube 70mm' ) ).

DATA: lv_return TYPE bapiret2.
DATA: it_returnmessages TYPE STANDARD TABLE OF bapi_matreturn2 WITH DEFAULT KEY.

CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
  EXPORTING
    headdata             = lv_headdata
    clientdata           = lv_clientdata
    clientdatax          = lv_clientdatax
    plantdata            = lv_plantdata
    plantdatax           = lv_plantdatax
    valuationdata        = lv_valuationdata
    valuationdatax       = lv_valuationdatax
    storagelocationdata  = lv_storagelocationdata
    storagelocationdatax = lv_storagelocationdatax
  IMPORTING
    return               = lv_return
  TABLES
    materialdescription  = it_materialdescription
    returnmessages       = it_returnmessages.

* Meldungen des Bausteins ausgeben
LOOP AT it_returnmessages ASSIGNING FIELD-SYMBOL(<m>).
  WRITE: / <m>-type,
           <m>-id,
           <m>-number,
           <m>-message,
           <m>-log_no,
           <m>-log_msg_no,
           <m>-message_v1,
           <m>-message_v2,
           <m>-message_v3,
           <m>-message_v4,
           <m>-parameter,
           <m>-row,
           <m>-field,
           <m>-system.
ENDLOOP.

* Fehlerauswertung
* Meldungstyp auswerten, Baustein gibt nur S - Success oder E - Error zurück
IF lv_return-type = 'E'.
* Fehler -> ROLLBACK
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
    IMPORTING
      return = lv_return.

  WRITE: / lv_return-type, lv_return-id, lv_return-message.
ELSE.
* kein Fehler -> COMMIT
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*      EXPORTING
*       wait   = abap_true
    IMPORTING
      return = lv_return.

  WRITE: / lv_return-type, lv_return-id, lv_return-message.
ENDIF.