2 SAP & E-cology Integration ABAP Program

1. Query Region

FUNCTION ZMMIP004.
*"----------------------------------------------------------------------
*"*"Local Interface
*"  EXPORTING
*"     VALUE(EV_MSGTY) TYPE  BAPI_MTYPE
*"     VALUE(EV_MESSAGE) TYPE  BAPI_MSG
*"  TABLES
*"      ET_OUTPUT STRUCTURE  T005U
*"----------------------------------------------------------------------
  SELECT *
    FROM T005U

    INTO TABLE et_output
    WHERE  SPRAS = 1 AND land1 = 'CN'.

  IF et_output[] IS NOT INITIAL.
    ev_msgty = 'S'.
  ELSE.
    ev_msgty = 'E'.
    ev_message = 'No Data'.
    RETURN.
  ENDIF.
ENDFUNCTION.

2. Query Counry

FUNCTION zmmip005 .
*"----------------------------------------------------------------------
*"*"Local Interface
*"  IMPORTING
*"     VALUE(IV_LAND1) TYPE  LAND1 OPTIONAL
*"     VALUE(IV_LANDX) TYPE  LANDX OPTIONAL
*"  EXPORTING
*"     VALUE(EV_MSGTY) TYPE  BAPI_MTYPE
*"     VALUE(EV_MESSAGE) TYPE  BAPI_MSG
*"  TABLES
*"      ET_OUTPUT STRUCTURE  ZSMMIP005
*"----------------------------------------------------------------------
  DATA: lv_where TYPE string.
  DATA: con_name TYPE landx.
  IF iv_land1 IS NOT INITIAL.
    CONCATENATE lv_where 'land1 = iv_land1' 'AND' INTO lv_where SEPARATED BY space.
  ENDIF.

  IF iv_landx IS NOT INITIAL.
    CONCATENATE '%' iv_landx '%' INTO con_name.
    CONCATENATE lv_where 'landx LIKE con_name'  'AND' INTO lv_where SEPARATED BY space.
  ENDIF.

  IF lv_where IS NOT INITIAL.
    SHIFT lv_where RIGHT DELETING TRAILING 'AND'.
  ENDIF.

  SELECT LAND1 LANDX
    FROM t005t

    INTO TABLE et_output
    WHERE (lv_where).

  IF et_output[] IS NOT INITIAL.
    ev_msgty = 'S'.
  ELSE.
    ev_msgty = 'E'.
    ev_message = 'No data'.
    RETURN.
  ENDIF.
ENDFUNCTION.

3. Query Language

FUNCTION zmmip006.
*"----------------------------------------------------------------------
*"*"Local Interface
*"  IMPORTING
*"     VALUE(IV_SPRSL) TYPE  SPRAS OPTIONAL
*"     VALUE(IV_SPTXT) TYPE  SPTXT OPTIONAL
*"  EXPORTING
*"     VALUE(EV_MSGTY) TYPE  BAPI_MTYPE
*"     VALUE(EV_MESSAGE) TYPE  BAPI_MSG
*"  TABLES
*"      ET_OUTPUT STRUCTURE  ZSMMIP006
*"----------------------------------------------------------------------

  DATA: lv_where TYPE string.
  DATA: con_name TYPE sptxt.
  data: ty_data type TABLE OF t002t.

  IF iv_sprsl IS NOT INITIAL.
    CONCATENATE lv_where 'sprsl = iv_sprsl' 'AND' INTO lv_where SEPARATED BY space.
  ENDIF.

  IF iv_sptxt IS NOT INITIAL.
    CONCATENATE '%' iv_sptxt '%' INTO con_name.
    CONCATENATE lv_where 'sptxt LIKE con_name'  'AND' INTO lv_where SEPARATED BY space.
  ENDIF.

  IF  lv_where IS NOT INITIAL.
    SHIFT lv_where RIGHT DELETING TRAILING 'AND'.
  ENDIF.
  select SPRSL sptxt from  t002t into table ty_data[] where spras = 1.
  
  SELECT b~laiso , a~sptxt
    FROM t002t AS a
    LEFT JOIN t002 AS b  ON b~spras = a~SPRSL
    INTO TABLE @et_output
    WHERE (lv_where) and a~spras = 1 .

  IF et_output[] IS NOT INITIAL.
    ev_msgty = 'S'.
  ELSE.
    ev_msgty = 'E'.
    ev_message = 'No data'.
    RETURN.
  ENDIF.
ENDFUNCTION.

4. Query G/L account master (company code)

FUNCTION zmmip007.
*"----------------------------------------------------------------------
*"*"Local Interface
*"  IMPORTING
*"     VALUE(IV_SAKNR) TYPE  STRING OPTIONAL
*"  EXPORTING
*"     VALUE(EV_MESSAGE) TYPE  BAPI_MSG
*"     VALUE(EV_MSGTY) TYPE  BAPI_MTYPE
*"  TABLES
*"      ET_OUTPUT STRUCTURE  ZSMMIP007
*"----------------------------------------------------------------------
  DATA: lv_where TYPE string.
  DATA: con_name TYPE string.
  " IF iv_saknr IS NOT INITIAL.
  "    CONCATENATE lv_where 'saknr = iv_saknr' 'AND' INTO lv_where SEPARATED BY space.
  "  ENDIF.
  CONCATENATE lv_where  'skb1~bukrs = ''1100'' or skb1~bukrs = ''1200'' AND'   INTO lv_where SEPARATED BY space.
  IF iv_saknr IS NOT INITIAL.
    CONCATENATE '%' iv_saknr '%' INTO con_name.
    CONCATENATE lv_where 'skb1~saknr LIKE con_name'  'AND' INTO lv_where SEPARATED BY space.
  ENDIF.

  IF  lv_where IS NOT INITIAL.
    SHIFT lv_where RIGHT DELETING TRAILING 'AND'.
  ENDIF.

  SELECT
          skb1~bukrs
          skb1~saknr"G/L Account Number
          skat~txt50 "G/L Account Long Text
        INTO CORRESPONDING FIELDS OF TABLE et_output
        FROM skb1
      JOIN skat ON skb1~saknr = skat~saknr
   WHERE (lv_where).
  IF et_output[] IS NOT INITIAL.
    ev_msgty = 'S'.
  ELSE.
    ev_msgty = 'E'.
    ev_message = 'No data'.
    RETURN.
  ENDIF.
ENDFUNCTION.

5. Query Rules for 'Allocation' Field Layout data

FUNCTION zmmip008.
*"----------------------------------------------------------------------
*"*"Local Interface
*"  IMPORTING
*"     VALUE(IV_ZUAWA) TYPE  DZUAWA OPTIONAL
*"     VALUE(IV_NAME1) TYPE  NAME1_ZUN OPTIONAL
*"  EXPORTING
*"     VALUE(EV_MSGTY) TYPE  BAPI_MTYPE
*"     VALUE(EV_MESSAGE) TYPE  BAPI_MSG
*"  TABLES
*"      ET_OUTPUT STRUCTURE  ZSMMIP008
*"----------------------------------------------------------------------
  DATA: lv_where TYPE string.
  DATA: con_name TYPE NAME1_ZUN.

  IF iv_zuawa IS NOT INITIAL.
    CONCATENATE lv_where 'zuawa = iv_zuawa' 'AND' INTO lv_where SEPARATED BY space.
  ENDIF.

  IF iv_name1 IS NOT INITIAL.
    CONCATENATE '%' iv_name1  '%' INTO con_name.
    CONCATENATE lv_where 'name1 LIKE con_name'  'AND' INTO lv_where SEPARATED BY space.
  ENDIF.

  IF  lv_where IS NOT INITIAL.
    SHIFT lv_where RIGHT DELETING TRAILING 'AND'.
  ENDIF.

  SELECT zuawa name1 FELD1
    FROM TZUN
    INTO CORRESPONDING FIELDS OF TABLE et_output
    WHERE (lv_where).

  IF et_output[] IS NOT INITIAL.
    ev_msgty = 'S'.
  ELSE.
    ev_msgty = 'E'.
    ev_message = 'No data'.
    RETURN.
  ENDIF.
ENDFUNCTION.

6. Query Own Explanations for Terms of Payment

FUNCTION zmmip009.
*"----------------------------------------------------------------------
*"*"Local Interface
*"  IMPORTING
*"     VALUE(IV_ZTERM) TYPE  DZTERM OPTIONAL
*"     VALUE(IV_VTEXT) TYPE  DZTERM_BEZ OPTIONAL
*"  EXPORTING
*"     VALUE(EV_MSGTY) TYPE  BAPI_MTYPE
*"     VALUE(EV_MESSAGE) TYPE  BAPI_MSG
*"  TABLES
*"      ET_OUTPUT STRUCTURE  TVZBT
*"----------------------------------------------------------------------


  DATA: lv_where TYPE string.
  DATA: con_name TYPE  dzterm_bez.


  IF iv_zterm IS NOT INITIAL.
    CONCATENATE lv_where 'zterm = iv_zterm' 'AND' INTO lv_where SEPARATED BY space.
  ENDIF.

  IF iv_vtext IS NOT INITIAL.
    CONCATENATE '%' iv_vtext '%' INTO con_name.
    CONCATENATE lv_where 'vtext LIKE con_name'  'AND' INTO lv_where SEPARATED BY space.
  ENDIF.

  IF  lv_where IS NOT INITIAL.
    SHIFT lv_where RIGHT DELETING TRAILING 'AND'.
  ENDIF.

  SELECT
    t052u~zterm,
    t052u~TEXT1 as VTEXT
    FROM t052u
    join T052 on t052~zterm = t052u~zterm and KOART = 'K'
    WHERE spras = 1

    INTO CORRESPONDING FIELDS OF  TABLE @et_output.

  IF et_output[] IS NOT INITIAL.
    ev_msgty = 'S'.
  ELSE.
    ev_msgty = 'E'.
    ev_message = 'No Data'.
    RETURN.
  ENDIF.
ENDFUNCTION.

7. Create new vendor

FUNCTION zmmip010.
*"----------------------------------------------------------------------
*"*"Local Interface
*"  IMPORTING
*"     VALUE(IV_BUKRS) TYPE  LFB1-BUKRS
*"     VALUE(IV_AKONT) TYPE  LFB1-AKONT
*"     VALUE(IV_ZUAWA) TYPE  LFB1-ZUAWA
*"     VALUE(IV_ZTERM) TYPE  LFB1-ZTERM
*"     VALUE(IV_BU_SORT1_TXT) TYPE  BUS000FLDS-BU_SORT1_TXT
*"     VALUE(IV_BU_SORT2_TXT) TYPE  BUS000FLDS-BU_SORT2_TXT OPTIONAL
*"     VALUE(IV_COUNTRY) TYPE  ADDR2_DATA-COUNTRY
*"     VALUE(IV_REGION) TYPE  ADDR2_DATA-REGION
*"     VALUE(IV_STREET) TYPE  ADDR2_DATA-STREET
*"     VALUE(IV_LANGU) TYPE  T002-LAISO
*"     VALUE(IV_POST_CODE1) TYPE  ADDR2_DATA-POST_CODE1
*"     VALUE(IV_NO) TYPE  CHAR8 OPTIONAL
*"     VALUE(IV_NAME_ORG1) TYPE  BU_NAMEOR1
*"     VALUE(IV_NAME_ORG2) TYPE  BU_NAMEOR2 OPTIONAL
*"     VALUE(IV_CITY) TYPE  ADDR2_DATA-CITY1
*"     VALUE(IV_CREATION_GROUP) TYPE  BAPIBUS1006_HEAD-PARTN_GRP
*"       DEFAULT 'MM05'
*"  EXPORTING
*"     VALUE(EV_MSGTY) TYPE  BAPI_MTYPE
*"     VALUE(EV_MESSAGE) TYPE  BAPI_MSG
*"     VALUE(EV_SAP_NO) TYPE  BAPIBUS1006_HEAD-BPARTNER
*"----------------------------------------------------------------------


  lw_data-bukrs = iv_bukrs.
  lw_data-akont  = iv_akont.
  lw_data-zuawa = iv_zuawa.
  lw_data-termtj = iv_zterm.
  lw_data-bu_sort1_txt = iv_bu_sort1_txt.
  lw_data-bu_sort2_txt = iv_bu_sort2_txt.
  lw_data-country = iv_country.
  lw_data-region = iv_region.
  lw_data-street  = iv_street.
  lw_data-langu = iv_langu.
  lw_data-post_code1 = iv_post_code1.
  lw_data-name_org1  = iv_name_org1.
  lw_data-name_org2  = iv_name_org2.
  lw_data-city1 = iv_city.
  lw_data-creation_group = iv_creation_group.

  APPEND lw_data TO gt_data.

  SELECT * INTO TABLE lt_tb003 FROM tb003.
  lt_comp = gt_data.
  SORT lt_comp BY  bukrs.
  DELETE ADJACENT DUPLICATES FROM lt_comp COMPARING bukrs."CREATION_NUMBER BUKRS.
*
  lt_org = gt_data.
  SORT lt_org BY  ekorg.
  DELETE ADJACENT DUPLICATES FROM lt_org COMPARING ekorg."CREATION_NUMBER EKORG.

  PERFORM frm_check_data.

**Check wheter BP is existing or not
  IF lw_data-creation_number IS INITIAL.
    PERFORM frm_check_bp_exit USING lw_data-name_org1
    CHANGING lw_data-exist
      lw_data-creation_number.
  ELSE.
    DATA: lw_but000 LIKE but000.
    SELECT SINGLE * FROM but000 INTO lw_but000 WHERE partner = lw_data-creation_number AND bu_group = lw_data-creation_group  AND type = '2'.
    IF sy-subrc = 0.
*------Supplier exist, marked as X, update data
      lw_data-exist  = 'X'.
    ELSE.
*------Supplier not exist, mark as blank, create new data
      lw_data-exist = ''.
    ENDIF.

  ENDIF.

  IF lw_data-exist IS INITIAL.
**Create BP
    PERFORM frm_create_bp USING lw_data-creation_number
    CHANGING lw_data.

  ELSE.
**Create BP
    PERFORM frm_change_bp USING lw_data-creation_number
    CHANGING lw_data.

**Update address
    PERFORM frm_add_addr USING lw_data-creation_number
    CHANGING lw_data.
  ENDIF.

  IF lw_data-exist = 'X'.
**Supplier role
    READ TABLE lt_tb003 INTO lw_tb003 WITH KEY role = 'FLVN01'.
    IF sy-subrc = 0.

      PERFORM frm_add_role USING lw_data-creation_number
            lw_tb003-rolecategory
            'FLVN01'
      CHANGING lw_head.
    ENDIF.

**Finance Role
    READ TABLE lt_tb003 INTO lw_tb003 WITH KEY role = 'FLVN00'.
    IF sy-subrc = 0.

      PERFORM frm_add_role USING lw_data-creation_number
            lw_tb003-rolecategory
            'FLVN00'
      CHANGING lw_head.
    ENDIF.


**Submit data
    PERFORM frm_commit USING lw_data-creation_number.

***change address data
*      PERFORM frm_change_addr USING lw_head-creation_number
*      CHANGING lw_head.

**Supplier data
    CLEAR: lt_comp_temp.
    LOOP AT lt_comp INTO lw_comp WHERE bukrs = lw_data-bukrs.
      APPEND lw_comp TO lt_comp_temp.
    ENDLOOP.

    CLEAR: lt_org_temp.
    LOOP AT lt_org INTO lw_org WHERE ekorg = lw_data-ekorg.
      APPEND lw_org TO lt_org_temp.
    ENDLOOP.

    PERFORM frm_create_vendor TABLES lt_comp_temp lt_org_temp
    USING lw_data-creation_number
    CHANGING lw_data.

**Submit data
    PERFORM frm_commit USING lw_data-creation_number.

  ENDIF.

  ev_msgty = lw_data-type.
  ev_message = lw_data-message.
  ev_sap_no = lw_data-creation_number.
ENDFUNCTION.

8. Query Material Master Data

FUNCTION ZMMIP011 .
*"----------------------------------------------------------------------
*"*"Local Interface
*"  IMPORTING
*"     VALUE(IV_WERKSTYPE  WERKS_D OPTIONAL
*"     VALUE(IV_MATNR) TYPE  TEXT18 OPTIONAL
*"  EXPORTING
*"     VALUE(EV_MSGTYTYPE  BAPI_MTYPE
*"     VALUE(EV_MESSAGE) TYPE  BAPI_MSG
*"  TABLES
*"      ET_OUTPUT STRUCTURE  ZSMMIP011OUT
*"----------------------------------------------------------------------
INCLUDE zmmip011top. "Global variable, screen variable, data type
  IF iv_matnr IS NOT INITIAL.
*Material code plus leading zero
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = IV_MATNR
    IMPORTING
      output = IV_MATNR.
   CONCATENATE lv_where 'mast~matnr = iv_matnr' 'AND' INTO lv_where SEPARATED BY space.
ELSE.
    ev_msgty = 'E'.
    ev_message = 'Please enter material code'.
     RETURN.
  ENDIF.

 IF lv_where IS NOT INITIAL.
    SHIFT lv_where RIGHT DELETING TRAILING 'AND'.  " Delete last AND
  ENDIF.

*Get all the secondary classifications and descriptions of the part number
 SELECT  atnam attxt
       INTO CORRESPONDING FIELDS OF TABLE gt_fldm2
          FROM ZTMM_OA_DATA
       WHERE fldm2 = IV_MATNR+6(3).

 IF gt_fldm2[] IS INITIAL.
    ev_msgty = 'E'.
    ev_message = 'No such data'.
    RETURN.
  ENDIF.

*Get the characteristics and description of the material
 SELECT cabn~atnam cabnt~atinn cabnt~atbez
   INTO TABLE it_cabn
   FROM cabn
   JOIN cabnt ON cabn~atinn = cabnt~atinn
   FOR ALL ENTRIES IN gt_fldm2
   WHERE
         cabn~atnam = gt_fldm2-atnam AND
         cabnt~spras = sy-langu AND cabn~lkenz = ''.

    SELECT ausp~objek ausp~atinn ausp~atwrt ausp~dec_value_from cabn~anzdz
      FROM ausp
      JOIN cabn ON cabn~atinn = ausp~atinn
      INTO TABLE gt_ausp
      WHERE ausp~objek = IV_MATNR  AND
            ausp~lkenz = '' AND
            ausp~klart = '001'."Class Type

SORT it_cabn BY atinn.
SORT gt_ausp BY atinn.

  LOOP AT gt_ausp INTO wa_ausp.
*   Convert scientific notation into numbers
    l_decf = wa_ausp-dec_value_from.
    l_digits = wa_ausp-digits.
    CALL FUNCTION 'QSS0_FLTP_TO_CHAR_CONVERSION'
      EXPORTING
        i_number_of_digits       = l_digits  "digits
        i_fltp_value             = l_decf
        i_value_not_initial_flag = 'X'
        i_screen_fieldlength     = 16
      IMPORTING
        e_char_field             = l_char.
    CONDENSE l_char NO-GAPS.
    wa_ausp-value = l_char.
    MODIFY gt_ausp FROM wa_ausp TRANSPORTING value.
    CLEAR wa_ausp.
  ENDLOOP.

  LOOP AT it_cabn INTO wa_cabn.
        READ TABLE gt_ausp INTO wa_ausp WITH KEY atinn = wa_cabn-atinn.
        IF sy-subrc = 0 AND wa_ausp-atwrt  IS NOT INITIAL.
            wa_out-z_value = wa_ausp-atwrt.
            ELSEIF sy-subrc = 0 AND wa_ausp-atwrt IS INITIAL..
              wa_out-z_value = wa_ausp-value.
           ELSE.
              wa_out-z_value = ''.
          ENDIF.
       wa_out-atbez =  wa_cabn-atbez.
       wa_out-atnam =  wa_cabn-atnam.
     APPEND wa_out TO et_output.
     CLEAR wa_cabn.
     CLEAR wa_ausp.
     ENDLOOP.

SORT et_output BY atnam.
   IF et_output[] IS NOT INITIAL.
    ev_msgty = 'S'.
   ELSE.
    ev_msgty = 'E'.
    ev_message = 'No data'.
    RETURN.
  ENDIF.
ENDFUNCTION.

9. Interactive-Material Master Data Classification Features

FUNCTION ZMMIP011A.
*"----------------------------------------------------------------------
*"*"Local Interface
*"  EXPORTING
*"     VALUE(EV_MSGTY) TYPE  BAPI_MTYPE
*"     VALUE(EV_MESSAGE) TYPE  BAPI_MSG
*"  TABLES
*"      IV_INPUT STRUCTURE  ZSMMIP011A
*"----------------------------------------------------------------------

INCLUDE ZMMIP011ATOP.

IF IV_INPUT[] IS INITIAL.
    ev_msgty = 'E'.
    ev_message = 'No changes'.
    RETURN.
  ELSE.
    SORT IV_INPUT BY  matnr.
    LOOP AT IV_INPUT INTO wa_input.
*Material code plus leading zero
      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          input  = wa_input-matnr
        IMPORTING
          output = wa_input-matnr.
    MODIFY IV_INPUT FROM wa_input.
    AT NEW matnr.
       APPEND wa_input TO gt_temp."Remove same sap material no.
     ENDAT.
    CLEAR wa_input.
    ENDLOOP.
   ENDIF.
*Query all feature value of material via ZMMIP011
LOOP AT gt_temp INTO wa_temp.
  CALL FUNCTION 'ZMMIP011'
  EXPORTING
    iv_werks  =  wa_temp-werks
    iv_matnr  =  wa_temp-matnr
    TABLES
      et_output = gt_output.
lv_key = wa_temp-matnr.
lv_class = wa_temp-matnr+6(6).
*CALL FUNCTION 'BAPI_OBJCL_GETDETAIL'
*  EXPORTING
*    objectkey              = lv_key
*    objecttable            = lv_table
*    classnum               = lv_class
*    classtype              = lv_class_type
*  tables
*    allocvaluesnum         = lt_alloc_num
*    allocvalueschar        = lt_alloc_char
*    allocvaluescurr        = lt_alloc_curr
*    return                 = lt_return.
*Get the character format of each characteristic value
SELECT atnam atfor FROM cabn INTO TABLE lt_cabn
   FOR ALL ENTRIES IN gt_output
       WHERE atnam = gt_output-atnam.

LOOP AT gt_output INTO wa_output.
 DATA:lv_atnam TYPE atnam.
 DATA:lv_value TYPE TXT50.
 READ TABLE lt_cabn INTO lw_cabn WITH KEY atnam = wa_output-atnam ."Check the character format of the characteristic value one by one
        CASE lw_cabn-atfor.
        WHEN 'CHAR'.
           READ TABLE IV_INPUT INTO wa_input  WITH KEY  atnam = wa_output-atnam matnr = wa_temp-matnr.
          IF  wa_output-z_value IS INITIAL AND ( wa_input-z_value = '' OR  wa_input-z_value IS INITIAL )."Skip if there is no such characteristic value
            CONTINUE.
          ENDIF.
             IF sy-subrc = 0 AND  wa_input-z_value IS NOT INITIAL."Indicates that the characteristic value has changed
               lw_alloc_char-charact = wa_input-atnam .
               lw_alloc_char-value_char = wa_input-z_value .
               lw_alloc_char-value_char_long = wa_input-z_value .
               ELSEIF  sy-subrc = 0 AND wa_input-z_value IS INITIAL."Modify the characteristic value to blank
                 CONTINUE.
                 ELSE."The characteristic value has no changed
                 lw_alloc_char-charact = wa_output-atnam .
                 lw_alloc_char-value_char = wa_output-z_value .
                 lw_alloc_char-value_char_long = wa_output-z_value .
              ENDIF.
          APPEND lw_alloc_char TO lt_alloc_char.
        WHEN 'NUM'.
            READ TABLE IV_INPUT INTO wa_input  WITH KEY  atnam = wa_output-atnam matnr = wa_temp-matnr.
            IF  wa_output-z_value IS INITIAL AND wa_input-z_value IS INITIAL.
            CONTINUE.
           ENDIF.
             IF sy-subrc = 0.
               lv_atnam = wa_input-atnam.
               lv_value = wa_input-z_value.
               ELSE.
               lv_atnam = wa_output-atnam.
               lv_value =  wa_output-z_value.
               ENDIF.
               IF lv_value CN '0123456789.- '."Check if the data is in digital format
                 ev_msgty = 'E'.
                 ev_message = 'Characteristic' && wa_input-atnam && ' value is not digit'.
                  RETURN.
                  ELSE.
                     lw_alloc_num-charact = lv_atnam  .
                    CALL FUNCTION 'CHAR_FLTP_CONVERSION'
                     EXPORTING
                       string             = lv_value
                       IMPORTING
                         flstr              = lw_alloc_num-value_from
                         EXCEPTIONS
                      exponent_too_big   = 1
                      exponent_too_small = 2
                      string_not_fltp    = 3
                      too_many_decim     = 4
                      OTHERS             = 5.
                    ENDIF.
                      APPEND lw_alloc_num TO lt_alloc_num.
                WHEN OTHERS.
            ENDCASE.
      CLEAR:lw_alloc_char,lw_alloc_num,lw_cabn,wa_input.
   ENDLOOP.
*
    CALL FUNCTION 'BAPI_OBJCL_CHANGE'
      EXPORTING
        objectkey    = lv_key
        objecttable  = lv_table
        classnum     = lv_class        "lv_class
        classtype    = lv_class_type
*        status       = '1'
*        keydate         = sy-datum
*        NO_DEFAULT_VALUES       = ' '
*    IMPORTING
*        CLASSIF_STATUS  =
      TABLES
        allocvaluesnumnew  = lt_alloc_num
        allocvaluescharnew = lt_alloc_char
        allocvaluescurrnew = lt_alloc_curr
        return          = lt_return.

    READ TABLE lt_return INTO lw_return  WITH KEY type = 'E'..
*  IF lw_return-type = 'S' .
    IF sy-subrc <> 0.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
     wait = 'X'.
     LOOP AT lt_return INTO lw_return WHERE type = 'S' AND number = '737'.
       IF  ev_msgty <> 'E'.
       ev_msgty  = 'S'.
       ENDIF.
      IF  EV_MESSAGE IS INITIAL.
         EV_MESSAGE = lv_key+6 && ' modified successfully'.
      ELSE.
        CONCATENATE  EV_MESSAGE '/' lv_key+6 ' modified successfully' INTO  EV_MESSAGE.
      ENDIF.
    ENDLOOP.
  ELSE.
    LOOP AT lt_return INTO lw_return WHERE type = 'E'.
      EV_MSGTY = 'E'.
      IF  EV_MESSAGE IS INITIAL.
         EV_MESSAGE = lv_key+6 && lw_return-message.
      ELSE.
        CONCATENATE  EV_MESSAGE '/' lv_key+6 lw_return-message INTO  EV_MESSAGE.
      ENDIF.
    ENDLOOP.
  ENDIF.

IF ev_msgty  = 'S'.

PERFORM bdc_init.
***************************************************************
perform bdc_dynpro      using 'SAPLMGMM'              '0060'.
perform bdc_field       using 'BDC_CURSOR'            'RMMG1-MATNR'.
perform bdc_field       using 'BDC_OKCODE'            '=AUSW'. "ENTR
perform bdc_field       using 'RMMG1-MATNR'            lv_key+6(12).

perform bdc_dynpro      using 'SAPLMGMM'              '0070'.
perform bdc_field       using 'BDC_CURSOR'            'MSICHTAUSW-DYTXT(03)'.
perform bdc_field       using 'BDC_OKCODE'            '=ENTR'.
perform bdc_field       using 'MSICHTAUSW-KZSEL(03)'  'X'.

perform bdc_dynpro      using 'SAPLCLCA'               '0602'.
perform bdc_field       using 'BDC_CURSOR'             'RMCLF-KLART'.
perform bdc_field       using 'BDC_OKCODE'             '=ENTE'.
perform bdc_field       using 'RMCLF-KLART'            lv_class_type."=001

perform bdc_dynpro      using 'SAPLCLFM'               '0500'.
perform bdc_field       using 'BDC_CURSOR'             'RMCLF-CLASS(01)'.
perform bdc_field       using 'BDC_OKCODE'             '=AUSW'.
perform bdc_field       using 'RMCLF-PAGPOS'           1.

perform bdc_dynpro      using 'SAPLCTMS'                '0109'.
perform bdc_field       using 'BDC_CURSOR'              'RCTMS-MWERT(03)'.
perform bdc_field       using 'BDC_OKCODE'              '=BACK'.

perform bdc_dynpro      using 'SAPLCLFM'                '0500'.
perform bdc_field       using 'BDC_CURSOR'              'RMCLF-CLASS(01)'.
perform bdc_field       using 'BDC_OKCODE'              '=SAVE'.
perform bdc_field       using 'RMCLF-PAGPOS'            1.

*********************************************************
 CLEAR itmsg.
 CALL TRANSACTION 'MM02' USING itbdc MODE 'E' UPDATE 'S' MESSAGES INTO itmsg.
*      DATA: msg TYPE bdcmsgcoll.
*     LOOP AT itmsg INTO msg.
*      CALL FUNCTION 'MESSAGE_TEXT_BUILD'
*        EXPORTING
*          msgid               = msg-MSGID
*          msgnr               = msg-MSGNR
*          msgv1               = msg-MSGV1
*          msgv2               = msg-MSGV2
*          msgv3               = msg-MSGV3
*          msgv4               = msg-MSGV4
*        IMPORTING
*           MESSAGE_TEXT_OUTPUT = lv_txt.
*      ENDLOOP.
*----------------------Edit material long text----------------------
*SELECT clint class FROM klah INTO TABLE lt_klah
*       WHERE class = lv_class.
*
* READ TABLE lt_klah INTO lw_klah  WITH KEY class = lv_class.
*
*lw_allkssk-objek = lv_key.
*lw_allkssk-clint = lw_klah-clint.
*lw_allkssk-klart = lv_class_type. "=001
*lw_allkssk-class = lv_class.
*lw_allkssk-kschl = 'PZ'.
*  APPEND lw_allkssk TO lt_allkssk.
*
*l_rmclf-matnr = lv_key.
*l_rmclf-objek = lv_key.
*l_rmclf-class = lv_class.
*l_rmclf-statu = '1'.
*l_rmclf-zaehl = 10.
*l_rmclf-klart = lv_class_type.
*
*  SELECT
*       objek
*       atinn
*       atzhl
*       klart
*       mafid
*       atwrt
*       atflv
*   FROM ausp INTO TABLE lt_allausp
*       WHERE objek = lv_key.
*
*  CALL FUNCTION 'EXIT_SAPLCLFM_002'
*   EXPORTING
*    I_RMCLF  =  l_rmclf
*    I_APPL  = ''
*    I_CALLED = '1'
*    TABLES
*      t_allkssk = lt_allkssk
*      t_allausp = lt_allausp
*      t_delcl = lt_delcl
*      t_delob = lt_delob.

*����������������������������������������������������������������������������*
  ENDIF.

   CLEAR : lw_cabn,lw_alloc_curr,lw_alloc_num,lw_alloc_char,lw_return.
   REFRESH:lt_cabn,lt_alloc_curr,lt_alloc_num,lt_alloc_char,lt_return.
ENDLOOP.
  CLEAR : wa_temp,wa_input ,wa_output.
  REFRESH :gt_temp,gt_input ,gt_output.
ENDFUNCTION.

10. Query Material Master Data (Basic)

FUNCTION ZMMIP012 .
*"----------------------------------------------------------------------
*"*"Local Interface
*"  IMPORTING
*"     VALUE(IV_WERKS) TYPE  WERKS_D
*"     VALUE(IV_MATNR) TYPE  TEXT18
*"  EXPORTING
*"     VALUE(EV_MSGTY) TYPE  BAPI_MTYPE
*"     VALUE(EV_MESSAGE) TYPE  BAPI_MSG
*"  TABLES
*"      ET_OUTPUT STRUCTURE  ZSMMIP012
*"----------------------------------------------------------------------
INCLUDE zmmip012top.

IF IV_MATNR IS NOT INITIAL.
*Material code plus leading zero
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = IV_MATNR
    IMPORTING
      output = IV_MATNR.
    CONCATENATE lv_where  '  a~matnr = IV_MATNR ' 'AND' INTO lv_where SEPARATED BY space.
 ELSE.
    ev_msgty = 'E'.
    ev_message = 'Please enter materail no.'.
    RETURN.
  ENDIF.
IF IV_WERKS IS NOT INITIAL.
 CONCATENATE lv_where  '  b~werks = IV_WERKS ' 'AND' INTO lv_where SEPARATED BY space.
ENDIF.

 IF lv_where IS NOT INITIAL.
    SHIFT lv_where RIGHT DELETING TRAILING 'AND'.  "Delete last AND
  ENDIF.

 SELECT

    a~matnr        "Material Number
    a~matkl        "Material Group
    a~mtart        "Material type
    a~bismt        "Old material number
    a~extwg        "External material group
    a~mtpos_mara   "General item category group
    a~brgew        "Gross weight
    a~ntgew        "Net weight
    a~gewei        "Weight Unit
    a~spart        "Division
    a~taklv        "Tax Classification of Material

    a~tragr        "Transportation Group
    b~ladgr        "Loading Group
    b~ekgrp        "Purchasing Group
    a~bstme        "Order unit
    b~insmk        "Post to Inspection Stock
    b~kordb        "Indicator: Source list requirement

    a~mprof        "Mfr part profile
    b~dismm        "MRP Type
    b~dispo        "MRP controller
    b~disgr        "MRP Group
    b~disls        "Lot size (materials planning)
    b~bstmi        "Minimum lot size
    b~maabc        "ABC indicator
    b~ausss        "Assembly scrap in percent(%)
    b~bstrf        "Rounding value for purchase order quantity
    b~beskz        "Procurement Type
    b~sobsl        "Special procurement type
    b~lgpro        "Issue storage location
    b~lgfsb        "Default storage location for external procurement
    b~rgekz        "Indicator: Backflush
    b~schgt        "Indicator: bulk material
    b~dzeit        "In-house production time
    b~eisbe        "Safety stock
    b~fhori        "Scheduling Margin Key for Floats
    b~plifz        "Planned delivery time in days
    b~webaz        "Goods receipt processing time in days
    b~strgr        "Planning strategy group
    b~vrmod        "Consumption mode
    b~vint1        "Consumption period: backward
    b~vint2        "Consumption period: forward
    b~miskz        "Mixed MRP indicator

    b~mtvfp        "Checking Group for Availability Check
    b~sbdkz        "Dependent requirements ind. for individual and coll. reqmts
    b~kausf        "Component scrap in percent%
    b~kzaus        "Discontinuation indicator
    b~ausdt        "Effective-out date
    b~nfmat        "Follow-up material
    b~ausme        "Unit of issue
*    umren type marm-umren,        "Denominator for conversion to base units of measure
*    umrez type marm-umrez,        "Numerator for Conversion to Base Units of Measure
    b~fevor        "Production Supervisor
    b~sfcpf        "Production Scheduling Profile
    b~xchpf        "Batch management requirement indicator
    a~mhdrz        "Minimum Remaining Shelf Life
    a~mhdhb        "Total shelf life
    a~iprkz        "Period Indicator for Shelf Life Expiration Date(SLED)
    b~ncost        "Do Not Cost
    b~losgr        "Costing lot size

    b~prctr        "Profit center
******Extra field********
    b~uneto         "Underdelivery tolerance limit
    b~ueeto         "Overdelivery tolerance limit
    b~ueetk         "Indicator: Unlimited overdelivery allowed
    a~laeda        "Date of Last Change
    a~ersda        "Created On
    a~ernam        "Name of Person who Created the Object
    INTO CORRESPONDING FIELDS OF TABLE gt_data
    FROM mara AS a INNER JOIN marc AS b ON a~matnr = b~matnr
    WHERE (lv_where).

  SORT gt_data BY matnr.

  IF gt_data[] IS NOT INITIAL.
*get marm
     SELECT
       matnr                   "Material Number
       umren as umrena         "Denominator for conversion for Order
       umrez as umreza         "Numerator for Conversion for Order
       INTO CORRESPONDING FIELDS OF TABLE gt_marmdata
       FROM marm FOR ALL ENTRIES IN gt_data
     WHERE matnr = gt_data-MATNR and meinh = gt_data-bstme.
     SELECT
       matnr                   "Material Number
       umren as umrenb         "Denominator for conversion for delivery
       umrez as umrezb         "Numerator for Conversion for delivery
       INTO CORRESPONDING FIELDS OF TABLE gt_marmdata2
       FROM marm FOR ALL ENTRIES IN gt_data
     WHERE matnr = gt_data-MATNR and meinh = gt_data-ausme.
*Get mbew
    SELECT
      matnr         "Material Number
      bwkey         "Valuation area
      bklas         "Valuation Class
      vprsv         "Price control indicator
      mlast         "Material Price Determination: Control
      peinh         "Price unit
      ekalr         "Material Is Costed with Quantity Structure
      hkmat         "Material-related origin
      INTO CORRESPONDING FIELDS OF TABLE gt_mbewdata
      FROM mbew
    WHERE matnr = IV_MATNR .
*Get mvke
     SELECT
       matnr         "Material Number
       vkorg         "Sales Organization
       vtweg         "Distribution Channel
       dwerk         "Delivering Plant (Own or External)
       aumng         "Minimum order quantity in base unit of measure
       ktgrm         "Account assignment group for this material
       mtpos         "Item category group from material master
       INTO CORRESPONDING FIELDS OF TABLE gt_mvkedata
       FROM mvke
     WHERE matnr = IV_MATNR .

*Get material description
     SELECT matnr spras maktx
       INTO CORRESPONDING FIELDS OF TABLE gt_maktx
       FROM makt
     WHERE matnr = IV_MATNR AND spras = '1'.
       .
*Get long description text
     SELECT matnr spras makt1
       INTO CORRESPONDING FIELDS OF TABLE gt_makt1
       FROM ZTMM0001
     WHERE matnr = IV_MATNR AND spras = '1'.
*end



     LOOP AT gt_data INTO wa_data.
       case wa_data-iprkz.
         when ''.
           wa_data-eprkz = 'D'.        " Base on differetn iprkz value decide different data type of eprkz
         when '1'.
           wa_data-eprkz = 'W'.
         when '2'.
           wa_data-eprkz = 'M'.
         when '3'.
           wa_data-eprkz = 'Y'.
       ENDCASE.


       CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
         EXPORTING
           input    = wa_data-meins
*              language = sy-langu
         IMPORTING
           output   = wa_data-meins.
*          CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
*            EXPORTING
*              input    = gt_data-ausme
*               language = sy-langu
*            IMPORTING
*              output   = gt_data-ausme.

       READ TABLE gt_marmdata WITH KEY matnr = IV_MATNR.
       IF sy-subrc = 0.
          wa_data-umrena = gt_marmdata-umrena.        
          wa_data-umreza = gt_marmdata-umreza.        
       ENDIF.
       READ TABLE gt_marmdata2 WITH KEY matnr = IV_MATNR.
       IF sy-subrc = 0.
          wa_data-umrenb = gt_marmdata2-umrenb.        
          wa_data-umrezb = gt_marmdata2-umrezb.        
       ENDIF.
       READ TABLE gt_mbewdata WITH KEY matnr = IV_MATNR bwkey = IV_WERKS.
       IF sy-subrc = 0.
          wa_data-bklas = gt_mbewdata-bklas.        
          wa_data-vprsv = gt_mbewdata-vprsv.         
          wa_data-mlast = gt_mbewdata-mlast.        
          wa_data-peinh = gt_mbewdata-peinh.         
          wa_data-ekalr = gt_mbewdata-ekalr.        
          wa_data-hkmat = gt_mbewdata-hkmat.         
       ENDIF.
       if IV_WERKS = '1101' or IV_WERKS = '1102' or IV_WERKS = '1103' or IV_WERKS = '1104' or IV_WERKS = '1105' or IV_WERKS = '1106' or IV_WERKS = '1199'.
         READ TABLE gt_mvkedata WITH KEY matnr = IV_MATNR dwerk = '1199'.
         IF sy-subrc = 0.
            wa_data-vkorg = gt_mvkedata-vkorg.         
            wa_data-vtweg = gt_mvkedata-vtweg.         
            wa_data-dwerk = gt_mvkedata-dwerk.         
            wa_data-aumng = gt_mvkedata-aumng.         
            wa_data-ktgrm = gt_mvkedata-ktgrm.         
            wa_data-mtpos = gt_mvkedata-mtpos.         
         ENDIF.
       elseif IV_WERKS = '1201' or IV_WERKS = '1501' or IV_WERKS = '1601' .
         READ TABLE gt_mvkedata WITH KEY matnr = IV_MATNR dwerk = IV_WERKS.
         IF sy-subrc = 0.
            wa_data-vkorg = gt_mvkedata-vkorg.         
            wa_data-vtweg = gt_mvkedata-vtweg.         
            wa_data-dwerk = gt_mvkedata-dwerk.         
            wa_data-aumng = gt_mvkedata-aumng.         
            wa_data-ktgrm = gt_mvkedata-ktgrm.         
            wa_data-mtpos = gt_mvkedata-mtpos.         
         ENDIF.
       else.
         READ TABLE gt_mvkedata WITH KEY matnr = IV_MATNR dwerk = IV_WERKS.
         IF sy-subrc = 0.
            wa_data-vkorg = gt_mvkedata-vkorg.         
            wa_data-vtweg = gt_mvkedata-vtweg.         
            wa_data-dwerk = gt_mvkedata-dwerk.         
            wa_data-aumng = gt_mvkedata-aumng.         
            wa_data-ktgrm = gt_mvkedata-ktgrm.         
            wa_data-mtpos = gt_mvkedata-mtpos.         
         ENDIF.
       endif.

       READ TABLE gt_maktx WITH KEY matnr = IV_MATNR.
       IF sy-subrc = 0.
          wa_data-maktx = gt_maktx-maktx.
       ENDIF.
       READ TABLE gt_maktx_e WITH KEY matnr = IV_MATNR.
       IF sy-subrc = 0.
          wa_data-maktx_en = gt_maktx_e-maktx.
       ENDIF.
       READ TABLE gt_makt1 WITH KEY matnr = IV_MATNR.
       IF sy-subrc = 0.
          wa_data-makt1 = gt_makt1-makt1.
       ENDIF.
       READ TABLE gt_makt1 WITH KEY matnr = IV_MATNR.
       IF sy-subrc = 0.
          wa_data-makt1_en = gt_makt1-makt1.
       ENDIF.

      MODIFY gt_data  FROM  wa_data.
      CLEAR wa_data.
     ENDLOOP.
   ENDIF.
*Export table
LOOP AT gt_data INTO wa_data.
********basic data1**********************
 wa_out-z_view = 'Basic data 1'.
 wa_out-z_text = 'Material group'.
 wa_out-z_value = wa_data-MATKL.
 wa_out-z_isref = 1.
* wa_out-z_item = 'MATKL'.
 APPEND wa_out TO et_output.
 CLEAR wa_out.