BAPI_GOODSMVT_CREATE (561) - Stock/Inventory SAP Upload Tool.


Generally during cut-over activity stage, we need to upload initial inventory/stock from legacy. This program will help you to mass upload Inventory/Stock.

This program is built on BAPI 'BAPI_GOODSMVT_CREATE' for movement type 561.


SAMPLE PROGRAM:


REPORT zstock_upload.

TYPESBEGIN OF gty_upload,
         srno       TYPE sy-tabix,
         doc_date   TYPE char10,
         pstng_date TYPE char10,
         move_type  TYPE mseg-bwart,
         spec_stock TYPE mseg-sobkz,
         plant      TYPE mseg-werks,
         stge_loc   TYPE mseg-lgort,
         material   TYPE mseg-matnr,
         batch      TYPE mseg-charg,
         vendor     TYPE mseg-lifnr,
         entry_uom  TYPE mseg-meins,
         entry_qnt  TYPE char16,
         unit_uom   TYPE mseg-meins,
         amount_lc  TYPE char16,
         prod_date  TYPE char10,
       END OF gty_upload.

TYPES BEGIN OF gty_log,
          srno       TYPE sy-tabix,
          doc_date   TYPE bldat,
          pstng_date TYPE budat,
          move_type  TYPE mseg-bwart,
          spec_stock TYPE mseg-sobkz,
          plant      TYPE mseg-werks,
          stge_loc   TYPE mseg-lgort,
          material   TYPE mseg-matnr,
          batch      TYPE mseg-charg,
          vendor     TYPE mseg-lifnr,
          entry_uom  TYPE mseg-meins,
          entry_qnt  TYPE erfmg,
          unit_uom   TYPE erfme,
          amount_lc  TYPE bapi_exbwr,
          prod_date  TYPE hsdat,
          matdoc_no  LIKE bapi2017_gm_head_ret,
          type       TYPE bapiret2-type,
          message    TYPE bapiret2-message,
        END OF gty_log.

TYPESBEGIN OF gty_mch1,
         matnr TYPE mch1-matnr,
         charg TYPE mch1-charg,
         vfdat TYPE mch1-vfdat,
         hsdat TYPE mch1-hsdat,
       END OF gty_mch1.

DATAgt_log     TYPE TABLE OF gty_log,
      gwa_log    TYPE gty_log,
      gt_upload  TYPE TABLE OF gty_upload,
      gwa_upload TYPE gty_upload,
      gt_mch1    TYPE TABLE OF gty_mch1,
      gwa_mch1   TYPE gty_mch1.


SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERSp_file TYPE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM p_f4_help.

START-OF-SELECTION.
  PERFORM p_upload_file.
  PERFORM p_call_bapi.
  PERFORM p_display.

*&---------------------------------------------------------------------*
*& Form P_F4_HELP
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM p_f4_help .
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  syst-cprog
      dynpro_number syst-dynnr
      field_name    ' '
    IMPORTING
      file_name     p_file.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form P_UPLOAD_FILE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM p_upload_file .

  DATAgt_raw TYPE truxs_t_text_data.

  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
      i_field_seperator    'X'
      i_line_header        'X'
      i_tab_raw_data       gt_raw
      i_filename           p_file
    TABLES
      i_tab_converted_data gt_upload
    EXCEPTIONS
      conversion_failed    1
      OTHERS               2.
  IF sy-subrc <> 0.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form P_CALL_BAPI
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM p_call_bapi .

  DATAgwa_header TYPE bapi2017_gm_head_01,
        gt_item    TYPE TABLE OF bapi2017_gm_item_create,
        gwa_item   TYPE bapi2017_gm_item_create,
        lv_gmcode  TYPE bapi2017_gm_code,
        lv_doc     LIKE bapi2017_gm_head_ret,
        gt_return  TYPE TABLE OF bapiret2,
        gwa_return TYPE bapiret2.

  SELECT matnr charg vfdat hsdat
    FROM mch1
    INTO TABLE gt_mch1
    FOR ALL ENTRIES IN gt_upload
    WHERE matnr gt_upload-material
      AND charg gt_upload-batch.

  SORT gt_mch1 BY matnr charg.

  lv_gmcode '05'.
  LOOP AT gt_upload INTO gwa_upload.

    CALL FUNCTION 'CONVERSION_EXIT_SDATE_INPUT'
      EXPORTING
        input  gwa_upload-pstng_date
      IMPORTING
        output gwa_header-pstng_date.

    CALL FUNCTION 'CONVERSION_EXIT_SDATE_INPUT'
      EXPORTING
        input  gwa_upload-doc_date
      IMPORTING
        output gwa_header-doc_date.

    gwa_item-move_type gwa_upload-move_type.
    gwa_item-spec_stock gwa_upload-spec_stock.
    gwa_item-material =  |{ gwa_upload-material  ALPHA IN }|.
    gwa_item-vendor =  |{ gwa_upload-vendor  ALPHA IN }|.
    gwa_item-plant gwa_upload-plant.
    gwa_item-stge_loc gwa_upload-stge_loc.
    gwa_item-entry_qnt gwa_upload-entry_qnt.
    gwa_item-entry_uom gwa_upload-entry_uom.
    gwa_item-amount_lc gwa_upload-amount_lc.
    gwa_item-batch gwa_upload-batch.


    CLEARgwa_mch1.
    READ TABLE gt_mch1 INTO gwa_mch1 WITH KEY matnr gwa_upload-material
                                              charg gwa_upload-batch BINARY SEARCH.
    IF sy-subrc EQ 0.
      gwa_item-expirydate gwa_mch1-vfdat.
      gwa_item-prod_date gwa_mch1-hsdat.
    ENDIF.

    CALL FUNCTION 'CONVERSION_EXIT_SDATE_INPUT'
      EXPORTING
        input  gwa_upload-prod_date
      IMPORTING
        output gwa_item-prod_date.

    APPEND gwa_item TO gt_item.
    CLEARgwa_item.

    CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
      EXPORTING
        goodsmvt_header  gwa_header
        goodsmvt_code    lv_gmcode
      IMPORTING
        goodsmvt_headret lv_doc
      TABLES
        goodsmvt_item    gt_item
        return           gt_return.

    IF lv_doc IS NOT INITIAL.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait abap_true.
    ENDIF.

    gwa_log-srno       gwa_upload-srno.
    gwa_log-doc_date   gwa_upload-doc_date.
    gwa_log-pstng_date gwa_upload-pstng_date.
    gwa_log-move_type  gwa_upload-move_type.
    gwa_log-spec_stock gwa_upload-spec_stock.
    gwa_log-plant      gwa_upload-plant.
    gwa_log-stge_loc   gwa_upload-stge_loc.
    gwa_log-material   gwa_upload-material.
    gwa_log-batch      gwa_upload-batch.
    gwa_log-vendor     gwa_upload-vendor.
    gwa_log-entry_uom  gwa_upload-entry_uom.
    gwa_log-entry_qnt  gwa_upload-entry_qnt.
    gwa_log-unit_uom   gwa_upload-unit_uom.
    gwa_log-amount_lc  gwa_upload-amount_lc.
    gwa_log-prod_date  gwa_upload-prod_date.

    IF gt_return IS INITIAL.
      gwa_log-type   'S'.
      gwa_log-message '311 Movement Successful.'.
      APPEND gwa_log TO gt_log.
    ELSE.
      LOOP AT gt_return INTO gwa_return.
        gwa_log-message gwa_return-message.
        gwa_log-type   gwa_return-type.
        APPEND gwa_log TO gt_log.
      ENDLOOP.
    ENDIF.
    CLEARgt_itemgwa_itemgwa_headergt_returngwa_returngwa_loglv_doc.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form P_DISPLAY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM p_display .
  cl_salv_table=>factoryIMPORTING r_salv_table DATA(gr_table)
                          CHANGING t_table gt_log ).

  DATA(columnsgr_table->get_columns).
  columns->set_optimize).

  DATA(functionsgr_table->get_functions).
  functions->set_all).

  gr_table->display).
ENDFORM.

Comments

Also Read...