Tuesday, September 3, 2019

BAPI Goods Movement Create

We can do many standard functionality by using BAPI. In the following example we are transferring a stock from one WBS element to another WBS element. The requirement is that there will be two movements of 411 and 412. When the system is sending a stock (special stock) from a WBS element, then it will hit the movement type of 411. Then at the time of receiving that stock to another WBS element, the system will hit the movement of 412. Here we are passing the quantity equals to the number of serial numbers.

TABLES objk.
DATA it_fcat TYPE slis_t_fieldcat_alv.

INITIALIZATION.
  SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
  PARAMETERSp_matnr TYPE mara-matnr,
              p_menge TYPE ekpo-menge,
              p_lgort TYPE mard-lgort,
              p_werks TYPE marc-werks,
              p_swbs  TYPE prps-pspnr,
              p_rwbs  TYPE prps-pspnr,
              p_charg TYPE mch1-charg,
              p_bldat TYPE sy-datum,
              p_budat TYPE sy-datum,
              p_text  TYPE char100.
  SELECT-OPTIONS s_sernr FOR objk-sernr NO INTERVALS.
  SELECTION-SCREEN END OF BLOCK b1.

CLASS cl_wbs DEFINITION.
  PUBLIC SECTION.
    TYPESBEGIN OF ty_out,
             bwart TYPE mseg-bwart,
             mblnr TYPE string,
             mjahr TYPE mseg-mjahr,
             ztext TYPE string,
           END OF ty_out.
    DATAit_out TYPE TABLE OF ty_out,
          wa_out TYPE ty_out.

    METHODS:
      constructor,
      m_goods_movement,
      m_alv_grid.
ENDCLASS.

CLASS cl_wbs IMPLEMENTATION.
  METHOD constructor.
    IF p_matnr IS INITIAL OR
       p_menge IS INITIAL OR
       p_lgort IS INITIAL OR
       p_werks IS INITIAL OR
       p_swbs  IS INITIAL OR
       p_rwbs  IS INITIAL OR
       p_charg IS INITIAL OR
       p_bldat IS INITIAL OR
       p_budat IS INITIAL OR
       p_text  IS INITIAL.

      MESSAGE 'Each & every field is mandatory'
      TYPE 'S' DISPLAY LIKE 'E'.
      LEAVE LIST-PROCESSING.
      LEAVE TO SCREEN sy-dynnr.
    ENDIF.

    IF p_swbs p_rwbs.
      MESSAGE 'Sending & receiving WBS must be different'
      TYPE 'S' DISPLAY LIKE 'E'.
      LEAVE LIST-PROCESSING.
      LEAVE TO SCREEN sy-dynnr.
    ENDIF.

    DESCRIBE TABLE s_sernr[] LINES DATA(lv_lines).
    IF p_menge NE lv_lines.
      MESSAGE 'Quantity & Serial numbers must be same'
      TYPE 'S' DISPLAY LIKE 'E'.
      LEAVE LIST-PROCESSING.
      LEAVE TO SCREEN sy-dynnr.
    ENDIF.
  ENDMETHOD.

  METHOD m_goods_movement.
    DATAgoodsmvt_header       TYPE bapi2017_gm_head_01,
          goodsmvt_code         TYPE bapi2017_gm_code,
          mat_doc_411           TYPE bapi2017_gm_head_ret-mat_doc,
          mat_doc_412           TYPE bapi2017_gm_head_ret-mat_doc,
          mat_yr_411            TYPE bapi2017_gm_head_ret-doc_year,
          mat_yr_412            TYPE bapi2017_gm_head_ret-doc_year,
          goodsmvt_item         TYPE TABLE OF bapi2017_gm_item_create,
          goodsmvt_serialnumber TYPE TABLE OF bapi2017_gm_serialnumber,
          lw_item               TYPE bapi2017_gm_item_create,
          lw_ser                TYPE bapi2017_gm_serialnumber,
          return_411            TYPE TABLE OF bapiret2,
          return_412            TYPE TABLE OF bapiret2.

    "--Header Level Information
    goodsmvt_header-pstng_date p_bldat.
    goodsmvt_header-doc_date   p_budat.
    goodsmvt_code-gm_code      '04'.

    "--Item Level Information
    lw_item-material   p_matnr.
    lw_item-plant      p_werks.
    lw_item-stge_loc   p_lgort.
    lw_item-batch      p_charg.
    lw_item-stck_type  'L'.
    lw_item-spec_stock 'Q'.
    lw_item-entry_qnt  p_menge.

    SELECT SINGLE meins FROM mara
      INTO lw_item-entry_uom
      WHERE matnr p_matnr.

    lw_item-move_plant p_werks.
    lw_item-move_stloc p_lgort.
    lw_item-move_type  '411'.
    lw_item-move_batch p_charg.

    "--Sending WBS
    SELECT SINGLE posid FROM prps
      INTO lw_item-val_wbs_elem
      WHERE pspnr p_swbs.

    APPEND lw_item TO goodsmvt_item.

    "--Serial Numbers into BAPI
    LOOP AT s_sernr ASSIGNING FIELD-SYMBOL(<l_sernr>).
      lw_ser-matdoc_itm '0001'.
      lw_ser-serialno <l_sernr>-low.
      APPEND lw_ser TO goodsmvt_serialnumber.
      CLEAR  lw_ser.
    ENDLOOP.

    "--Goods Movement 411
    CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
      EXPORTING
        goodsmvt_header       goodsmvt_header
        goodsmvt_code         goodsmvt_code
      IMPORTING
        materialdocument      mat_doc_411
        matdocumentyear       mat_yr_411
      TABLES
        goodsmvt_item         goodsmvt_item
        goodsmvt_serialnumber goodsmvt_serialnumber
        return                return_411.

    IF mat_doc_411 IS NOT INITIAL.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait 'X'.

      READ TABLE goodsmvt_item ASSIGNING FIELD-SYMBOL(<l_item>INDEX 1.
      IF sy-subrc 0.
        <l_item>-move_type '412'.

        "--Receiving WBS
        SELECT SINGLE posid FROM prps
          INTO <l_item>-val_wbs_elem
          WHERE pspnr p_rwbs.
      ENDIF.

      "--Goods Movement 412
      CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
        EXPORTING
          goodsmvt_header       goodsmvt_header
          goodsmvt_code         goodsmvt_code
        IMPORTING
          materialdocument      mat_doc_412
          matdocumentyear       mat_yr_412
        TABLES
          goodsmvt_item         goodsmvt_item
          goodsmvt_serialnumber goodsmvt_serialnumber
          return                return_412.

      IF mat_doc_412 IS INITIAL.
        READ TABLE return_412 ASSIGNING FIELD-SYMBOL(<ls_412>INDEX 1.
        IF sy-subrc 0.
          IF <ls_412>-type 'E'.
            wa_out-bwart '412'.
            wa_out-mblnr <ls_412>-message.
            wa_out-ztext 'Not Created - Error'.
            APPEND wa_out TO it_out.
            CLEAR  wa_out.
          ENDIF.
        ENDIF.

      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait 'X'.
      ENDIF.

    ELSE.
      READ TABLE return_411 ASSIGNING FIELD-SYMBOL(<ls_411>INDEX 1.
      IF sy-subrc 0.
        IF <ls_411>-type 'E'.
          wa_out-bwart '411'.
          wa_out-mblnr <ls_411>-message.
          wa_out-ztext 'Not Created - Error'.
          APPEND wa_out TO it_out.
          CLEAR  wa_out.
        ENDIF.
      ENDIF.
    ENDIF.

    IF mat_doc_411 IS NOT INITIAL.
      wa_out-bwart '411'.
      wa_out-mblnr mat_doc_411.
      wa_out-mjahr mat_yr_411.
      wa_out-ztext 'Created Successfully'.
      APPEND wa_out TO it_out.
      CLEAR  wa_out.
    ENDIF.

    IF mat_doc_412 IS NOT INITIAL.
      wa_out-bwart '412'.
      wa_out-mblnr mat_doc_412.
      wa_out-mjahr mat_yr_412.
      wa_out-ztext 'Created Successfully'.
      APPEND wa_out TO it_out.
      CLEAR  wa_out.
    ENDIF.
  ENDMETHOD.

  METHOD m_alv_grid.
    DATA lw_lay TYPE slis_layout_alv.
    lw_lay-colwidth_optimize 'X'.

    PERFORM field_catalog USING:
          'BWART'  'Movement'          '',
          'MBLNR'  'Material Document' 'X',
          'MJAHR'  'Year'              'X',
          'ZTEXT'  'Status'            ''.

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        i_callback_program sy-repid
        is_layout          lw_lay
        it_fieldcat        it_fcat
      TABLES
        t_outtab           it_out
      EXCEPTIONS
        program_error      1
        OTHERS             2.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA obj_wbs TYPE REF TO cl_wbs.
  CREATE OBJECT obj_wbs.
  CALL METHOD:  obj_wbs->m_goods_movement,
                obj_wbs->m_alv_grid.

*&---------------------------------------------------------------------*
*& Form FIELD_CATALOG
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM field_catalog  USING  p_fname   TYPE string
                           p_text    TYPE string
                           p_no_zero TYPE c.

  DATA lw_fcat TYPE slis_fieldcat_alv.
  lw_fcat-fieldname p_fname.
  lw_fcat-seltext_l p_text.
  lw_fcat-no_zero   p_no_zero.
  APPEND lw_fcat TO it_fcat.
  CLEAR  lw_fcat.
ENDFORM.


Selection Screen:

Output:

No comments: