12 ноября 2018

Создание спецификации (BOM)

Пример использования BAPI_MATERIAL_BOM_GROUP_CREATE.




TYPES:
  BEGIN OF ty_s_data,
    werks TYPE werks_d, " Завод
    matnr_p TYPE matnr, " Производимый материал
    matnr TYPE matnr,   " Компонент
    menge TYPE menge_d, " Кол-во
    meins TYPE meins,   " ЕИ
    valid_from TYPE datuv, " Дата начала действия
    aennr TYPE aennr,   " Номер изменения
  END OF ty_s_data,
  
  ty_t_data TYPE TABLE OF ty_s_data.

FORM process_data  USING    ut_data TYPE ty_t_data
                            uv_testrun TYPE char01
                   CHANGING ct_return TYPE bapiret2_tab.

  DATA:
    lv_werks TYPE werks_d,
    lv_matnr_p TYPE mara-matnr,
    lv_aennr TYPE aennr,
    lv_valid_from TYPE d,
    lv_stlan TYPE mast-stlan VALUE '1',

    ls_testrun TYPE bapiflag,
    lt_bomgroup TYPE TABLE OF bapi1080_bgr_c,
    lt_variants TYPE TABLE OF bapi1080_bom_c,
    lt_items TYPE TABLE OF bapi1080_itm_c,
    lt_relations TYPE TABLE OF bapi1080_mbm_c,
    lt_assigments TYPE TABLE OF bapi1080_rel_itm_bom_c,
    lt_texts TYPE TABLE OF bapi1080_txt_c,
    lt_return TYPE TABLE OF bapiret2,

    lv_stlal_pr TYPE mast-stlal,
    lv_stlal TYPE mast-stlal,
    lv_n2(2) TYPE n,
    lv_stlnr TYPE mast-stlnr,
    lv_exstl TYPE stzu-exstl,
    lv_group(20),
    lv_item_no(4) TYPE n,
    lv_object_id TYPE cs_object_id.

  FIELD-SYMBOLS:
    <lfs_data> LIKE LINE OF ut_data,

    <lfs_bomgroup> LIKE LINE OF lt_bomgroup,
    <lfs_variants> LIKE LINE OF lt_variants,
    <lfs_items> LIKE LINE OF lt_items,
    <lfs_relations> LIKE LINE OF lt_relations,
    <lfs_assigments> LIKE LINE OF lt_assigments,
    <lfs_texts> LIKE LINE OF lt_texts,
    <lfs_return> LIKE LINE OF lt_return.

  " Создание BOM

  READ TABLE ut_data ASSIGNING <lfs_data> INDEX 1.
  CHECK sy-subrc EQ 0.

  lv_werks = <lfs_data>-werks.
  lv_matnr_p = <lfs_data>-matnr_p.
  lv_aennr = <lfs_data>-aennr.
  lv_valid_from = <lfs_data>-valid_from.

  IF lv_matnr_p IS INITIAL.
    EXIT.
  ENDIF.

  " Определение номера альтернативы

  lv_stlal = '01'.

  SELECT SINGLE MAX( stlal )
    FROM mast
    INTO lv_stlal_pr
    WHERE matnr EQ lv_matnr_p
      AND werks EQ lv_werks
      AND stlan EQ lv_stlan.
  IF sy-subrc NE 0.
    lv_stlal = '01'.
  ELSE.
    TRY .
        lv_n2 = lv_stlal_pr.
      CATCH cx_root.
        lv_n2 = '00'.
    ENDTRY.
    ADD 1 TO lv_n2.
    lv_stlal = lv_n2.
  ENDIF.

  " Определение группы спецификаций

  SELECT SINGLE stlnr
    FROM mast
    INTO lv_stlnr
    WHERE matnr EQ lv_matnr_p
      AND werks EQ lv_werks
      AND stlan EQ lv_stlan.
  IF sy-subrc EQ 0.
    SELECT SINGLE exstl
      FROM stzu
      INTO lv_exstl
      WHERE stlty EQ 'M'
        AND stlnr EQ lv_stlnr.
    IF sy-subrc EQ 0.
      lv_group = lv_exstl.
    ELSE.
      CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
        EXPORTING
          input  = lv_matnr_p
        IMPORTING
          output = lv_group.
    ENDIF.
  ELSE.
    CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
      EXPORTING
        input  = lv_matnr_p
      IMPORTING
        output = lv_group.
  ENDIF.

  INSERT INITIAL LINE INTO TABLE lt_bomgroup ASSIGNING <lfs_bomgroup>.
  <lfs_bomgroup>-bom_group_identification = lv_group.
  <lfs_bomgroup>-object_type = 'BGR'.
  <lfs_bomgroup>-object_id = lv_group.
  <lfs_bomgroup>-technical_type = 'M'.
  <lfs_bomgroup>-bom_usage = lv_stlan.
  <lfs_bomgroup>-bom_group = lv_group.
  <lfs_bomgroup>-created_in_plant = lv_werks.
  <lfs_bomgroup>-ltxt_lang = 'RU'.
  <lfs_bomgroup>-bom_text = ''.

  INSERT INITIAL LINE INTO TABLE lt_variants ASSIGNING <lfs_variants>.
  <lfs_variants>-bom_group_identification = lv_group.
  <lfs_variants>-object_type = 'BOM'.
  <lfs_variants>-object_id = lv_group.
  <lfs_variants>-alternative_bom = lv_stlal.
  <lfs_variants>-bom_status = '03'.
  <lfs_variants>-base_qty = 1.
  <lfs_variants>-base_unit = 'ST'.
  <lfs_variants>-ltxt_lang = 'RU'.
  <lfs_variants>-alt_text = ''.
  <lfs_variants>-valid_from_date = lv_valid_from.
  <lfs_variants>-change_no = lv_aennr.
  <lfs_variants>-function = 'NEW'.

  CLEAR:
    lv_item_no.

  LOOP AT ut_data ASSIGNING <lfs_data>.

    ADD 10 TO lv_item_no.

    CONCATENATE lv_group lv_item_no INTO lv_object_id SEPARATED BY '-'.

    INSERT INITIAL LINE INTO TABLE lt_items ASSIGNING <lfs_items>.
    <lfs_items>-bom_group_identification = lv_group.
    <lfs_items>-object_type = 'ITM'.
    <lfs_items>-object_id = lv_object_id.
    <lfs_items>-item_no = lv_item_no.
    <lfs_items>-item_cat = 'L'.
    <lfs_items>-component = <lfs_data>-matnr.
    <lfs_items>-comp_qty = <lfs_data>-menge.
    <lfs_items>-comp_unit = <lfs_data>-meins.
    <lfs_items>-ltxt_lang = 'R'.
    <lfs_items>-valid_from_date = lv_valid_from.
    <lfs_items>-change_no = lv_aennr.

    INSERT INITIAL LINE INTO TABLE lt_assigments ASSIGNING <lfs_assigments>.
    <lfs_assigments>-bom_group_identification = lv_group.
    <lfs_assigments>-sub_object_type = 'ITM'.
    <lfs_assigments>-sub_object_id = lv_object_id.
    <lfs_assigments>-super_object_type = 'BOM'.
    <lfs_assigments>-super_object_id = lv_group.
    <lfs_assigments>-valid_from_date = lv_valid_from.
    <lfs_assigments>-change_no = lv_aennr.
    <lfs_assigments>-function = 'NEW'.

  ENDLOOP.

  INSERT INITIAL LINE INTO TABLE lt_relations ASSIGNING <lfs_relations>.
  <lfs_relations>-bom_group_identification = lv_group.
  <lfs_relations>-material = lv_matnr_p.
  <lfs_relations>-plant = lv_werks.
  <lfs_relations>-bom_usage = lv_stlan.
  <lfs_relations>-alternative_bom = lv_stlal.
  <lfs_relations>-lot_size_from = 0.
  <lfs_relations>-lot_size_to = 0.

  ls_testrun-bapiflag = uv_testrun.

  CALL FUNCTION 'BAPI_MATERIAL_BOM_GROUP_CREATE'
    EXPORTING
      testrun                  = ls_testrun
*     all_error                = ' '
    TABLES
      bomgroup                 = lt_bomgroup
      variants                 = lt_variants
      items                    = lt_items
*     subitems                 =
      materialrelations        = lt_relations
      itemassignments          = lt_assigments
*     subitemassignments       =
      texts                    = lt_texts
      return                   = lt_return.

  IF uv_testrun IS INITIAL.

    LOOP AT lt_return ASSIGNING <lfs_return>
      WHERE type CA 'EAX'.
      EXIT.
    ENDLOOP.

    IF sy-subrc EQ 0.
      " Ошибка
      PERFORM bapi_rollback.
      INSERT LINES OF lt_return INTO TABLE ct_return.
    ELSE.
      " Успешно
      PERFORM bapi_commit.
      INSERT LINES OF lt_return INTO TABLE ct_return.
    ENDIF.

  ELSE.

    IF lt_return[] IS INITIAL.
      " Нет ошибок
    ELSE.
      " Есть ошибки
      INSERT LINES OF lt_return INTO TABLE ct_return.
    ENDIF.

  ENDIF.

ENDFORM.                    " PROCESS_DATA


Комментариев нет:

Отправить комментарий