BAPI_MATPHYSINV_CREATE_MULT - Physical Inventory (MI01) Upload SAP Tool.

This upload Program can help you to mass upload physical inventory documents at one go. This program uses  BAPI 'BAPI_MATPHYSINV_CREATE_MULT' to achieve the same.

CODE:

REPORT zmm_physical_inv_upload" Physical Inventory Upload

SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE TEXT-001.
PARAMETERS p_fname TYPE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN END OF BLOCK blk1.

TYPESBEGIN OF gty_doc,
         doc_date   TYPE sy-datum,    " Document Date / Posting Date
         werks      TYPE werks_d,     " Plant
         lgort      TYPE lgort_d,     " Storage Location
         post_block TYPE c,           " Posting Block
         spec_stock TYPE sobkz,       " Special Stock Indicator
         lifnr      TYPE lifnr,       " Vendor
         matnr      TYPE matnr,       " Material No.
         charg      TYPE charg_d,     " Batch
         stock_type TYPE c,           " Stock Type
         key        TYPE string,      " Key
       END OF gty_doc.

TYPESBEGIN OF gty_log,
         type       TYPE bapi_mtype,
         id         TYPE symsgid,
         number     TYPE symsgno,
         message    TYPE bapi_msg,
         werks      TYPE werks_d,
         lgort      TYPE lgort_d,
         spec_stock TYPE sobkz,
         iblnr      TYPE iblnr,
       END OF gty_log.

DATA:  lv_filename TYPE ibipparms-path.

DATAgt_doc   TYPE STANDARD TABLE OF gty_doc,
      gt_items TYPE STANDARD TABLE OF bapi_physinv_create_items,
      gt_log   TYPE STANDARD TABLE OF gty_log.

DATAgwa_doc    TYPE gty_doc,
      gwa_log    TYPE gty_log,
      gwa_items  TYPE bapi_physinv_create_items,
      gwa_header TYPE bapi_physinv_create_head.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.
  PERFORM p_f4_filename.

START-OF-SELECTION.
  PERFORM p_uploadfile.
  PERFORM p_processdata.
  PERFORM p_displaylog.

*&---------------------------------------------------------------------*
*& Form P_F4_FILENAME
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM p_f4_filename .
  CLEARlv_filename.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  syst-cprog
      dynpro_number syst-dynnr
      field_name    'P_FILE'
    IMPORTING
      file_name     lv_filename.

  p_fname lv_filename.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form P_UPLOADFILE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM p_uploadfile .
  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_fname
    TABLES
      i_tab_converted_data gt_doc
    EXCEPTIONS
      conversion_failed    1
      OTHERS               2.
  IF sy-subrc <> 0.
    MESSAGE e398(00WITH TEXT-002.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form P_PROCESSDATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM p_processdata .
  DATAgt_unique  TYPE STANDARD TABLE OF gty_doc,
        gwa_unique TYPE gty_doc.

  SORT gt_doc BY werks lgort spec_stock lifnr matnr.

  LOOP AT gt_doc ASSIGNING FIELD-SYMBOL(<fs_doc>).
    CONCATENATE <fs_doc>-werks <fs_doc>-lgort
                <fs_doc>-spec_stock <fs_doc>-lifnr
                INTO <fs_doc>-key.
  ENDLOOP.

  gt_unique gt_doc.
  DELETE ADJACENT DUPLICATES FROM gt_unique COMPARING key.

  SORT gt_doc BY key.

  LOOP AT gt_unique INTO gwa_unique.
    CLEARgwa_items,gt_items,gwa_header,gwa_doc.
    gwa_header-plant gwa_unique-werks.
    gwa_header-stge_loc gwa_unique-lgort.
    gwa_header-doc_date gwa_unique-doc_date.
    gwa_header-plan_date gwa_unique-doc_date.
    gwa_header-post_block gwa_unique-post_block.
    gwa_header-spec_stock gwa_unique-spec_stock.

    IF gwa_unique-spec_stock IS NOT INITIAL AND gwa_unique-lifnr IS NOT INITIAL.
      CLEARgwa_doc.
      READ TABLE gt_doc INTO gwa_doc WITH KEY key gwa_unique-key BINARY SEARCH.
      IF sy-subrc EQ 0.
        LOOP AT gt_doc INTO gwa_doc FROM sy-tabix.
          IF gwa_unique-key EQ gwa_doc-key.
            CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
              EXPORTING
                input  gwa_doc-matnr
              IMPORTING
                output gwa_items-material.

            CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
              EXPORTING
                input  gwa_doc-lifnr
              IMPORTING
                output gwa_items-vendor.

            gwa_items-batch gwa_doc-charg.
            gwa_items-stock_type 1.
            APPEND gwa_items TO gt_items.
            CLEARgwa_items.
          ELSE.
            EXIT.
          ENDIF.
        ENDLOOP.
      ENDIF.

    ELSE.

      CLEARgwa_doc.
      READ TABLE gt_doc INTO gwa_doc WITH KEY key gwa_unique-key BINARY SEARCH.
      IF sy-subrc EQ 0.
        LOOP AT gt_doc INTO gwa_doc FROM sy-tabix.
          IF gwa_unique-key EQ gwa_doc-key.
            CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
              EXPORTING
                input  gwa_doc-matnr
              IMPORTING
                output gwa_items-material.

            gwa_items-stock_type gwa_doc-stock_type.
            gwa_items-batch gwa_doc-charg.
            APPEND gwa_items TO gt_items.
            CLEARgwa_items.
          ELSE.
            EXIT.
          ENDIF.
        ENDLOOP.
      ENDIF.
    ENDIF.

    PERFORM p_bapicall.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form P_BAPICALL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM p_bapicall .
  DATAlt_return  TYPE STANDARD TABLE OF bapiret2,
        lwa_return TYPE bapiret2.

  CALL FUNCTION 'BAPI_MATPHYSINV_CREATE_MULT'
    EXPORTING
      head   gwa_header
    TABLES
      items  gt_items
      return lt_return.

  READ TABLE lt_return INTO lwa_return WITH KEY type 'E'.
  IF sy-subrc NE 0.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait 'X'.
  ENDIF.

  LOOP AT lt_return INTO lwa_return.
    gwa_log-type lwa_return-type.
    gwa_log-id lwa_return-id.
    gwa_log-number lwa_return-number.
    gwa_log-message lwa_return-message.
    gwa_log-lgort gwa_header-stge_loc.
    gwa_log-werks gwa_header-plant.
    gwa_log-spec_stock gwa_header-spec_stock.
    IF lwa_return-type 'S' and lwa_return-id 'M7' and lwa_return-number '710'.
      gwa_log-iblnr lwa_return-message_v1.
    ENDIF.
    APPEND gwa_log TO gt_log.
    CLEARlwa_return.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form P_DISPLAYLOG
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM p_displaylog .
  CALL FUNCTION 'ZDISPLAY_ALV'
    EXPORTING
      i_title   'Log Report'
      i_program sy-repid
    TABLES
      t_alv     gt_log.
ENDFORM.


Comments

Also Read...