[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.