0% found this document useful (0 votes)
55 views

Include Include Include OF Perform Perform END OF Perform: //////////DATA DECLARATION

This report extracts data from an Excel file, creates an internal table, and calls a BAPI to create purchase orders. It includes forms to get data from Excel, create an internal table by parsing the column values, and call the BAPI. Any error messages or return codes from the BAPI call are formatted and written to the log.

Uploaded by

Niloy Biswas
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Include Include Include OF Perform Perform END OF Perform: //////////DATA DECLARATION

This report extracts data from an Excel file, creates an internal table, and calls a BAPI to create purchase orders. It includes forms to get data from Excel, create an internal table by parsing the column values, and call the BAPI. Any error messages or return codes from the BAPI call are formatted and written to the log.

Uploaded by

Niloy Biswas
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

*&---------------------------------------------------------------------

*
*& Report  ZREPORT_BAPI_PO_CREATE
*&
*&---------------------------------------------------------------------
*
*&
*&
*&---------------------------------------------------------------------
*

REPORT  ZREPORT_BAPI_PO_CREATE.

INCLUDE zdata_dec_bapi.

INCLUDE zsel_screen_bapi.

include zdata_fetch_bapi.

START-OF-SELECTION.

perform get_data_from_excel.

PERFORM CREATE_INT_TABLE.

END-OF-SELECTION.

PERFORM CALL_BAPI.

//////////DATA DECLARATION:///////////

*&---------------------------------------------------------------------
*
*&  Include           ZDATA_DEC_BAPI
*&---------------------------------------------------------------------
*

DATA : i_excel TYPE STANDARD TABLE OF alsmex_tabline,
       w_excel TYPE alsmex_tabline.

TYPES : BEGIN OF ty_tab,
        comp_code    TYPE bukrs,
        doc_type    TYPE esart,
        vendor      TYPE  elifn,
        purch_org   TYPE  ekorg,
        pur_group    TYPE bkgrp,
        po_item      TYPE ebelp,
        material    TYPE  matnr,
        plant        TYPE ewerk,
        matl_group  TYPE  matkl,
        quantity    TYPE  bstmg,
        net_price    TYPE bapicurext,
        price_unit  TYPE  epein,
        END OF ty_tab.

DATA : i_tab TYPE STANDARD TABLE OF ty_tab,
       w_tab TYPE ty_tab.
//////////////////SELECTION SCREEN:////////////////

*&---------------------------------------------------------------------
*
*&  Include           ZSEL_SCREEN_BAPI
*&---------------------------------------------------------------------
*

PARAMETERS : P_FILE TYPE LOCALFILE,
       P_BEG TYPE I,
       P_END TYPE I.

at SELECTION-SCREEN on VALUE-REQUEST FOR p_file.

  CALL FUNCTION 'F4_FILENAME'
   EXPORTING
     PROGRAM_NAME        = SYST-CPROG
     DYNPRO_NUMBER       = SYST-DYNNR
*     FIELD_NAME          = ' '
   IMPORTING
     FILE_NAME           = p_file.

/////////DATA FETCH://////////

*&---------------------------------------------------------------------
*
*&  Include           ZDATA_FETCH_BAPI
*&---------------------------------------------------------------------
*
*&---------------------------------------------------------------------
*
*&      Form  get_data_from_excel
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
FORM get_data_from_excel .

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                      = p_file
      i_begin_col                   = 1
      i_begin_row                   = p_beg
      i_end_col                     = 11
      i_end_row                     = p_end
    TABLES
      intern                        = i_excel
* EXCEPTIONS
*   INCONSISTENT_PARAMETERS       = 1
*   UPLOAD_OLE                    = 2
*   OTHERS                        = 3
            .
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDFORM.                    " get_data_from_excel
*&---------------------------------------------------------------------
*
*&      Form  CREATE_INT_TABLE
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
FORM create_int_table .

  LOOP AT i_excel INTO w_excel.

    CASE w_excel-col.

      WHEN '0001'.
        w_tab-comp_code = w_excel-value.

      WHEN '0002'.
        w_tab-doc_type = w_excel-value.

      WHEN '0003'.
        w_tab-vendor = w_excel-value.

      WHEN '0004'.
        w_tab-purch_org = w_excel-value.

      WHEN '0005'.
        w_tab-pur_group = w_excel-value.

      WHEN '0006'.
        w_tab-po_item = w_excel-value.

      WHEN '0007'.
        w_tab-material = w_excel-value.

      WHEN '0008'.
        w_tab-plant = w_excel-value.

      WHEN '0009'.
        w_tab-matl_group = w_excel-value.

      WHEN '0010'.
        w_tab-quantity = w_excel-value.

      WHEN '0011'.
        w_tab-net_price = w_excel-value.

      WHEN '0012'.
        w_tab-price_unit = w_excel-value.
    ENDCASE.

    AT END OF row.
      APPEND w_tab TO i_tab.
      CLEAR w_tab.
    ENDAT.

  ENDLOOP.

ENDFORM.                    " CREATE_INT_TABLE
*&---------------------------------------------------------------------
*
*&      Form  CALL_BAPI
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
FORM call_bapi .

  DATA : w_header TYPE bapimepoheader,
         w_headerx TYPE bapimepoheaderx,
         i_item TYPE STANDARD TABLE OF bapimepoitem,
         w_item TYPE bapimepoitem,
         i_itemx TYPE STANDARD TABLE OF bapimepoitemx,
         w_itemx TYPE bapimepoitemx,
         i_return TYPE STANDARD TABLE OF bapiret2,
         w_return TYPE bapiret2,
         lv_text TYPE char255.

  LOOP AT i_tab INTO w_tab.

    CLEAR : w_header,
            w_headerx,
            w_item,
            w_itemx.
    FREE : i_item,i_itemx,i_return.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    input         = w_tab-vendor
 IMPORTING
   OUTPUT        = w_tab-vendor.
          .

*  FILL HEADER DATA
    w_header-comp_code = w_tab-comp_code.
    w_header-doc_type  = w_tab-doc_type.
    w_header-vendor    = w_tab-vendor.
    w_header-purch_org = w_tab-purch_org.
    w_header-pur_group = w_tab-pur_group.
    w_headerx-comp_code =  'X'.
    w_headerx-doc_type  =  'X'.
    w_headerx-vendor    =  'X'.
    w_headerx-purch_org =  'X'.
    w_headerx-pur_group =  'X'.

* FILL ITEM DATA
    w_item-po_item    = w_tab-po_item.
    w_item-material   = w_tab-material.
    w_item-plant      = w_tab-plant.
    w_item-matl_group = w_tab-matl_group.
    w_item-quantity   = w_tab-quantity.
    w_item-net_price  = w_tab-net_price.
    w_item-price_unit = w_tab-price_unit.
    APPEND w_item TO i_item.
    CLEAR w_item.

    w_itemx-po_item    = w_tab-po_item.
    w_itemx-po_itemx   = 'X'.
    w_itemx-material   = 'X'.
    w_itemx-plant      = 'X'.
    w_itemx-matl_group = 'X'.
    w_itemx-quantity   = 'X'.
    w_itemx-net_price  = 'X'.
    w_itemx-price_unit = 'X'.
    APPEND w_itemx TO i_itemx.
    CLEAR w_itemx.

    CALL FUNCTION 'BAPI_PO_CREATE1'
      EXPORTING
        poheader  = w_header
        poheaderx = w_headerx
      TABLES
        return    = i_return
        poitem    = i_item
        poitemx   = i_itemx.

    READ TABLE i_return INTO w_return WITH KEY type = 'E'.
    IF sy-subrc NE 0.

      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
       EXPORTING
         wait          = 'X'
*        IMPORTING
*          RETURN        =
                .

    ENDIF.

    LOOP AT i_return INTO w_return.

      CALL FUNCTION 'FORMAT_MESSAGE'
        EXPORTING
          id        = w_return-id
          lang      = sy-langu
          no        = w_return-number
          v1        = w_return-message_v1
          v2        = w_return-message_v2
          v3        = w_return-message_v3
          v4        = w_return-message_v4
        IMPORTING
          msg       = lv_text
        EXCEPTIONS
          not_found = 1
          OTHERS    = 2.
      IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.

      WRITE:/ lv_text.
    ENDLOOP.
    FREE : i_return.

  ENDLOOP.

ENDFORM.                    " CALL_BAPI

///////////////CREATE INTERNAL TABLE:///////////////

*&---------------------------------------------------------------------
*
*&  Include           ZDATA_FETCH_BAPI
*&---------------------------------------------------------------------
*
*&---------------------------------------------------------------------
*
*&      Form  get_data_from_excel
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
FORM get_data_from_excel .

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                      = p_file
      i_begin_col                   = 1
      i_begin_row                   = p_beg
      i_end_col                     = 11
      i_end_row                     = p_end
    TABLES
      intern                        = i_excel
* EXCEPTIONS
*   INCONSISTENT_PARAMETERS       = 1
*   UPLOAD_OLE                    = 2
*   OTHERS                        = 3
            .
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDFORM.                    " get_data_from_excel
*&---------------------------------------------------------------------
*
*&      Form  CREATE_INT_TABLE
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
FORM create_int_table .

  LOOP AT i_excel INTO w_excel.

    CASE w_excel-col.

      WHEN '0001'.
        w_tab-comp_code = w_excel-value.

      WHEN '0002'.
        w_tab-doc_type = w_excel-value.

      WHEN '0003'.
        w_tab-vendor = w_excel-value.

      WHEN '0004'.
        w_tab-purch_org = w_excel-value.

      WHEN '0005'.
        w_tab-pur_group = w_excel-value.

      WHEN '0006'.
        w_tab-po_item = w_excel-value.

      WHEN '0007'.
        w_tab-material = w_excel-value.

      WHEN '0008'.
        w_tab-plant = w_excel-value.

      WHEN '0009'.
        w_tab-matl_group = w_excel-value.

      WHEN '0010'.
        w_tab-quantity = w_excel-value.

      WHEN '0011'.
        w_tab-net_price = w_excel-value.

      WHEN '0012'.
        w_tab-price_unit = w_excel-value.
    ENDCASE.

    AT END OF row.
      APPEND w_tab TO i_tab.
      CLEAR w_tab.
    ENDAT.

  ENDLOOP.

ENDFORM.                    " CREATE_INT_TABLE
*&---------------------------------------------------------------------
*
*&      Form  CALL_BAPI
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
FORM call_bapi .

  DATA : w_header TYPE bapimepoheader,
         w_headerx TYPE bapimepoheaderx,
         i_item TYPE STANDARD TABLE OF bapimepoitem,
         w_item TYPE bapimepoitem,
         i_itemx TYPE STANDARD TABLE OF bapimepoitemx,
         w_itemx TYPE bapimepoitemx,
         i_return TYPE STANDARD TABLE OF bapiret2,
         w_return TYPE bapiret2,
         lv_text TYPE char255.

  LOOP AT i_tab INTO w_tab.

    CLEAR : w_header,
            w_headerx,
            w_item,
            w_itemx.
    FREE : i_item,i_itemx,i_return.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    input         = w_tab-vendor
 IMPORTING
   OUTPUT        = w_tab-vendor.
          .

*  FILL HEADER DATA
    w_header-comp_code = w_tab-comp_code.
    w_header-doc_type  = w_tab-doc_type.
    w_header-vendor    = w_tab-vendor.
    w_header-purch_org = w_tab-purch_org.
    w_header-pur_group = w_tab-pur_group.
    w_headerx-comp_code =  'X'.
    w_headerx-doc_type  =  'X'.
    w_headerx-vendor    =  'X'.
    w_headerx-purch_org =  'X'.
    w_headerx-pur_group =  'X'.

* FILL ITEM DATA
    w_item-po_item    = w_tab-po_item.
    w_item-material   = w_tab-material.
    w_item-plant      = w_tab-plant.
    w_item-matl_group = w_tab-matl_group.
    w_item-quantity   = w_tab-quantity.
    w_item-net_price  = w_tab-net_price.
    w_item-price_unit = w_tab-price_unit.
    APPEND w_item TO i_item.
    CLEAR w_item.

    w_itemx-po_item    = w_tab-po_item.
    w_itemx-po_itemx   = 'X'.
    w_itemx-material   = 'X'.
    w_itemx-plant      = 'X'.
    w_itemx-matl_group = 'X'.
    w_itemx-quantity   = 'X'.
    w_itemx-net_price  = 'X'.
    w_itemx-price_unit = 'X'.
    APPEND w_itemx TO i_itemx.
    CLEAR w_itemx.

    CALL FUNCTION 'BAPI_PO_CREATE1'
      EXPORTING
        poheader  = w_header
        poheaderx = w_headerx
      TABLES
        return    = i_return
        poitem    = i_item
        poitemx   = i_itemx.

    READ TABLE i_return INTO w_return WITH KEY type = 'E'.
    IF sy-subrc NE 0.

      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
       EXPORTING
         wait          = 'X'
*        IMPORTING
*          RETURN        =
                .

    ENDIF.

    LOOP AT i_return INTO w_return.

      CALL FUNCTION 'FORMAT_MESSAGE'
        EXPORTING
          id        = w_return-id
          lang      = sy-langu
          no        = w_return-number
          v1        = w_return-message_v1
          v2        = w_return-message_v2
          v3        = w_return-message_v3
          v4        = w_return-message_v4
        IMPORTING
          msg       = lv_text
        EXCEPTIONS
          not_found = 1
          OTHERS    = 2.
      IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.

      WRITE:/ lv_text.
    ENDLOOP.
    FREE : i_return.

  ENDLOOP.

ENDFORM.                    " CALL_BAPI

*&---------------------------------------------------------------------
*
*&  Include           ZDATA_FETCH_BAPI
*&---------------------------------------------------------------------
*
*&---------------------------------------------------------------------
*
*&      Form  get_data_from_excel
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
FORM get_data_from_excel .

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                      = p_file
      i_begin_col                   = 1
      i_begin_row                   = p_beg
      i_end_col                     = 11
      i_end_row                     = p_end
    TABLES
      intern                        = i_excel
* EXCEPTIONS
*   INCONSISTENT_PARAMETERS       = 1
*   UPLOAD_OLE                    = 2
*   OTHERS                        = 3
            .
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDFORM.                    " get_data_from_excel
*&---------------------------------------------------------------------
*
*&      Form  CREATE_INT_TABLE
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
FORM create_int_table .

  LOOP AT i_excel INTO w_excel.

    CASE w_excel-col.

      WHEN '0001'.
        w_tab-comp_code = w_excel-value.

      WHEN '0002'.
        w_tab-doc_type = w_excel-value.

      WHEN '0003'.
        w_tab-vendor = w_excel-value.

      WHEN '0004'.
        w_tab-purch_org = w_excel-value.

      WHEN '0005'.
        w_tab-pur_group = w_excel-value.

      WHEN '0006'.
        w_tab-po_item = w_excel-value.

      WHEN '0007'.
        w_tab-material = w_excel-value.

      WHEN '0008'.
        w_tab-plant = w_excel-value.

      WHEN '0009'.
        w_tab-matl_group = w_excel-value.

      WHEN '0010'.
        w_tab-quantity = w_excel-value.
      WHEN '0011'.
        w_tab-net_price = w_excel-value.

      WHEN '0012'.
        w_tab-price_unit = w_excel-value.

    ENDCASE.

    AT END OF row.
      APPEND w_tab TO i_tab.
      CLEAR w_tab.
    ENDAT.

  ENDLOOP.

ENDFORM.                    " CREATE_INT_TABLE
*&---------------------------------------------------------------------
*
*&      Form  CALL_BAPI
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
FORM call_bapi .

  DATA : w_header TYPE bapimepoheader,
         w_headerx TYPE bapimepoheaderx,
         i_item TYPE STANDARD TABLE OF bapimepoitem,
         w_item TYPE bapimepoitem,
         i_itemx TYPE STANDARD TABLE OF bapimepoitemx,
         w_itemx TYPE bapimepoitemx,
         i_return TYPE STANDARD TABLE OF bapiret2,
         w_return TYPE bapiret2,
         lv_text TYPE char255.

  LOOP AT i_tab INTO w_tab.

    CLEAR : w_header,
            w_headerx,
            w_item,
            w_itemx.
    FREE : i_item,i_itemx,i_return.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    input         = w_tab-vendor
 IMPORTING
   OUTPUT        = w_tab-vendor.
          .
*  FILL HEADER DATA
    w_header-comp_code = w_tab-comp_code.
    w_header-doc_type  = w_tab-doc_type.
    w_header-vendor    = w_tab-vendor.
    w_header-purch_org = w_tab-purch_org.
    w_header-pur_group = w_tab-pur_group.

    w_headerx-comp_code =  'X'.
    w_headerx-doc_type  =  'X'.
    w_headerx-vendor    =  'X'.
    w_headerx-purch_org =  'X'.
    w_headerx-pur_group =  'X'.

* FILL ITEM DATA
    w_item-po_item    = w_tab-po_item.
    w_item-material   = w_tab-material.
    w_item-plant      = w_tab-plant.
    w_item-matl_group = w_tab-matl_group.
    w_item-quantity   = w_tab-quantity.
    w_item-net_price  = w_tab-net_price.
    w_item-price_unit = w_tab-price_unit.
    APPEND w_item TO i_item.
    CLEAR w_item.

    w_itemx-po_item    = w_tab-po_item.
    w_itemx-po_itemx   = 'X'.
    w_itemx-material   = 'X'.
    w_itemx-plant      = 'X'.
    w_itemx-matl_group = 'X'.
    w_itemx-quantity   = 'X'.
    w_itemx-net_price  = 'X'.
    w_itemx-price_unit = 'X'.
    APPEND w_itemx TO i_itemx.
    CLEAR w_itemx.

    CALL FUNCTION 'BAPI_PO_CREATE1'
      EXPORTING
        poheader  = w_header
        poheaderx = w_headerx
      TABLES
        return    = i_return
        poitem    = i_item
        poitemx   = i_itemx.

    READ TABLE i_return INTO w_return WITH KEY type = 'E'.
    IF sy-subrc NE 0.

      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
       EXPORTING
         wait          = 'X'
*        IMPORTING
*          RETURN        =
                .

    ENDIF.

    LOOP AT i_return INTO w_return.

      CALL FUNCTION 'FORMAT_MESSAGE'
        EXPORTING
          id        = w_return-id
          lang      = sy-langu
          no        = w_return-number
          v1        = w_return-message_v1
          v2        = w_return-message_v2
          v3        = w_return-message_v3
          v4        = w_return-message_v4
        IMPORTING
          msg       = lv_text
        EXCEPTIONS
          not_found = 1
          OTHERS    = 2.
      IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.

      WRITE:/ lv_text.
    ENDLOOP.
    FREE : i_return.

  ENDLOOP.

ENDFORM.                    " CALL_BAPI

You might also like