SAP ABAP 物料主数据创建

函数:

BAPI_MATERIAL_SAVEDATA


BAPI_MATERIAL_GETINTNUMBER"创建物料号


BAPI_MATINSPCTRL_SAVEREPLICA"扩充质量视图


BAPI_OBJCL_CREATE"扩充分类视图


BAPI_MATERIAL_SAVEREPLICA"扩充库存地点

扩充视图:

基础视图、采购视图、MRP视图、一般工厂视图、销售视图、财务视图、仓储视图、扩展字段、质量视图、分类视图等。

说明:内部给号先调用函数BAPI_MATERIAL_GETINTNUMBER创建物料号

具体代码

Code listing for function: ZBPM_ISP003
Description: 物料主数据创建
FUNCTION zbpm_isp003.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(INPUT) TYPE  ZBPM_ISP003_INPUT
*"  EXPORTING
*"     VALUE(OUTPUT) TYPE  ZBPM_ISP003_OUTPUT
*"----------------------------------------------------------------------

*       Global data declarations

  DATA:
    ls_return               TYPE bapiret2,
    lt_return               TYPE TABLE OF bapiret2,
    ls_headdata             TYPE bapimathead,
    ls_plantdata            TYPE bapi_marc,
    ls_plantdatax           TYPE bapi_marcx,
    ls_salesdata            TYPE bapi_mvke,
    ls_salesdatax           TYPE bapi_mvkex,
    ls_cliendata            TYPE bapi_mara,
    ls_cliendatax           TYPE bapi_marax,
    ls_valuationdata        TYPE  bapi_mbew,
    ls_valuationdatax       TYPE  bapi_mbewx,
    ls_storagelocationdata  TYPE  bapi_mard,
    ls_storagelocationdatax TYPE  bapi_mardx,
    ls_warehousenumberdata  TYPE bapi_mlgn,
    ls_warehousenumberdatax TYPE bapi_mlgnx,
    ls_taxclassifications   TYPE bapi_mlan,
    ls_storagetypedata      TYPE bapi_mlgt,
    ls_storagetypedatax     TYPE bapi_mlgtx,
    ls_internationalartnos  TYPE bapi_mean,
    lt_internationalartnos  TYPE TABLE OF bapi_mean,
    lt_taxclassifications   TYPE TABLE OF bapi_mlan,
    ls_tq34                 TYPE tq34,
    lt_tq34                 TYPE TABLE OF tq34,
    ls_inspectionctrl       TYPE bapi1001004_qmat,
    lt_inspectionctrl       TYPE TABLE OF bapi1001004_qmat,
    lt_bapi_makt            TYPE TABLE OF bapi_makt,
    ls_bapi_makt            TYPE bapi_makt,
    lt_bapi_mltx            TYPE TABLE OF bapi_mltx,
    ls_bapi_mltx            TYPE bapi_mltx,
    ls_unitsofmeasure       TYPE bapi_marm,
    lt_unitsofmeasure       TYPE TABLE OF bapi_marm,
    ls_unitsofmeasurex      TYPE bapi_marmx,
    lt_unitsofmeasurex      TYPE TABLE OF bapi_marmx.

  DATA:objectkeynew LIKE  bapi1003_key-object,
       classnumnew  LIKE  bapi1003_key-classnum,
       classtypenew LIKE  bapi1003_key-classtype.

*扩展字段
  DATA:
    lv_valuepart(960),                          "扩展用
    ls_te_mara        TYPE bapi_te_mara,        "扩展字段
    ls_te_marax       TYPE bapi_te_marax,
    ls_extensionin    TYPE bapiparex,         "BAPI扩展结构
    lt_extensionin    TYPE TABLE OF bapiparex,         "BAPI扩展结构
    ls_extensioninx   TYPE bapiparexx,        "BAPI扩展结构标识位
    lt_extensioninx   TYPE TABLE OF bapiparexx.        "BAPI扩展结构标识位

  DATA:return_obj         LIKE  TABLE OF bapiret2,
       it_allocvalueschar TYPE TABLE OF  bapi1003_alloc_values_char,
       is_allocvalueschar TYPE bapi1003_alloc_values_char.

  DATA:material_number  LIKE  bapimatinr OCCURS 0 WITH HEADER LINE.
  DATA:ls_ret TYPE bapireturn1 .

  "扩充库存地点
  DATA : lt_head      TYPE TABLE OF bapie1matheader,
         ls_head      TYPE bapie1matheader,
         lt_strlgort  TYPE TABLE OF bapie1mard,
         ls_strlgort  TYPE bapie1mard,
         lt_strlgortx TYPE TABLE OF bapie1mardx,
         ls_strlgortx TYPE bapie1mardx,
         lv_return    TYPE bapiret2.

  MOVE-CORRESPONDING input-item[] TO output-item[] .
  "校验必输
  LOOP AT input-item INTO DATA(ls_initem) WHERE maktx IS INITIAL OR mbrsh IS INITIAL
                                             OR mtart IS INITIAL OR matkl IS INITIAL
                                             OR meins IS INITIAL .
    READ TABLE output-item INTO DATA(ls_outitem) WITH KEY id = ls_initem-id .
    IF sy-subrc = 0 .
      ls_outitem-type = 'E' .
      ls_outitem-message = '请输入所有必输字段' .
      MODIFY output-item FROM ls_outitem TRANSPORTING type message WHERE id = ls_initem-id  .
      CLEAR : ls_outitem .
    ENDIF .
    CLEAR : ls_initem .
  ENDLOOP .

  READ TABLE output-item INTO ls_outitem WITH KEY type = 'E' .
  IF sy-subrc = 0 .
    RETURN .
  ENDIF .

"BAPI赋值
  LOOP AT input-item INTO ls_initem .

    IF ls_initem-matnr IS INITIAL .

      IF ls_initem-mbrsh IS INITIAL .
        ls_initem-mbrsh = 'M' .
      ENDIF .

      CALL FUNCTION 'BAPI_MATERIAL_GETINTNUMBER'
        EXPORTING
          material_type   = ls_initem-mtart
          industry_sector = ls_initem-mbrsh
        IMPORTING
          return          = ls_ret
        TABLES
          material_number = material_number.

      READ TABLE material_number INTO DATA(ls_number) INDEX 1 .
      IF sy-subrc = 0 .
        ls_initem-matnr = ls_number-material .
      ELSE .
        ls_outitem-type = 'E' .
        ls_outitem-message = '物料创建失败' .
        MODIFY output-item FROM ls_outitem TRANSPORTING type message matnr WHERE id = ls_initem-id  .
        CONTINUE .
      ENDIF .

    ELSE .
      CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
        EXPORTING
          input        = ls_initem-matnr
        IMPORTING
          output       = ls_initem-matnr
        EXCEPTIONS
          length_error = 1
          OTHERS       = 2.
      IF sy-subrc <> 0.
* Implement suitable error handling here
      ENDIF.

    ENDIF .

    ls_headdata-material       = ls_initem-matnr ."物料号
    IF ls_initem-mbrsh IS INITIAL .               "行业领域默认M
      ls_initem-mbrsh = 'M' .
    ENDIF .
    ls_headdata-ind_sector     = ls_initem-mbrsh ."行业领域
    ls_headdata-matl_type      = ls_initem-mtart ."物料类型
*    CLEAR : ls_initem .

*基础视图
    ls_headdata-basic_view     = 'X'.

    ls_cliendata-matl_group    = ls_initem-matkl ."物料组
    ls_cliendatax-matl_group   = 'X' .

    ls_cliendata-old_mat_no    = ls_initem-bismt. "旧料号
    ls_cliendatax-old_mat_no  = 'X'.

    CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'   "基本计量单位
      EXPORTING
        input          = ls_initem-meins
        language       = sy-langu
      IMPORTING
        output         = ls_initem-meins
      EXCEPTIONS
        unit_not_found = 1
        OTHERS         = 2.
    IF sy-subrc <> 0.
    ENDIF.
    ls_cliendata-base_uom     = ls_initem-meins .
    ls_cliendatax-base_uom    = 'X'.

    IF ls_initem-mstae IS INITIAL .
      ls_initem-mstae = 'Z0' .
    ENDIF .
    ls_cliendata-pur_status    = ls_initem-mstae ."物料状态
    ls_cliendatax-pur_status   = 'X' .

    ls_cliendata-net_weight    = ls_initem-ntgew ."净重
    ls_cliendatax-net_weight   = 'X' .

    ls_cliendata-allowed_wt    = ls_initem-brgew ."毛重
    ls_cliendatax-allowed_wt   = 'X' .

    IF ls_initem-gewei IS NOT INITIAL .
      ls_cliendata-unit_of_wt    = ls_initem-gewei ."重量单位
      ls_cliendatax-unit_of_wt   = 'X' .
    ENDIF .

    CALL FUNCTION 'CONVERSION_EXIT_EAN11_INPUT'
      EXPORTING
        input  = ls_initem-ean11
      IMPORTING
        output = ls_initem-ean11.
    ls_internationalartnos-ean_upc = ls_initem-ean11 ."商品EAN号
    IF ls_initem-ean11 IS NOT INITIAL .
      ls_initem-numtp = 'XQ' .
    ENDIF .
    ls_internationalartnos-ean_cat = ls_initem-numtp ."商品EAN类别

    ls_internationalartnos-unit    = ls_initem-meins .

    CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
      EXPORTING
        input          = ls_initem-meins
        language       = sy-langu
      IMPORTING
        output         = ls_internationalartnos-unit_iso
      EXCEPTIONS
        unit_not_found = 1
        OTHERS         = 2.
    IF sy-subrc <> 0.
* Implement suitable error handling here
    ENDIF.

    APPEND ls_internationalartnos TO lt_internationalartnos .

    ls_bapi_makt-langu         = sy-langu .
    ls_bapi_makt-matl_desc     = ls_initem-maktx .
    APPEND ls_bapi_makt TO lt_bapi_makt .

*采购视图
    IF ls_initem-zcgbs IS NOT INITIAL .

      ls_headdata-purchase_view = 'X'.

      ls_plantdata-plant  = ls_initem-werks.      "工厂
      ls_plantdatax-plant = ls_initem-werks.

      ls_plantdata-pur_group  = ls_initem-ekgrp.  "采购组
      ls_plantdatax-pur_group = 'X'.

      CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
        EXPORTING
          input          = ls_initem-bstme
        IMPORTING
          output         = ls_initem-bstme
        EXCEPTIONS
          unit_not_found = 1
          OTHERS         = 2.

      IF ls_initem-bstme IS NOT INITIAL .
        ls_cliendata-po_unit    = ls_initem-bstme.  "采购订单的计量单位
        ls_cliendatax-po_unit   = 'X'.
      ENDIF .

      IF ls_initem-bstme IS NOT INITIAL .

        CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
          EXPORTING
            input          = ls_initem-bstme
            language       = sy-langu
          IMPORTING
            output         = ls_unitsofmeasure-alt_unit_iso
          EXCEPTIONS
            unit_not_found = 1
            OTHERS         = 2.
        IF sy-subrc <> 0.
* Implement suitable error handling here
        ENDIF.

        ls_unitsofmeasurex-alt_unit_iso = ls_unitsofmeasure-alt_unit_iso .

        CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
          EXPORTING
            input          = ls_initem-bstme
            language       = sy-langu
          IMPORTING
            output         = ls_initem-bstme
          EXCEPTIONS
            unit_not_found = 1
            OTHERS         = 2.
        IF sy-subrc <> 0.
* Implement suitable error handling here
        ENDIF.

        ls_unitsofmeasure-alt_unit  = ls_initem-bstme .

        ls_unitsofmeasurex-alt_unit = ls_unitsofmeasure-alt_unit .

      ELSE .

        CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
          EXPORTING
            input          = ls_initem-meins
            language       = sy-langu
          IMPORTING
            output         = ls_unitsofmeasure-alt_unit_iso
          EXCEPTIONS
            unit_not_found = 1
            OTHERS         = 2.
        IF sy-subrc <> 0.
* Implement suitable error handling here
        ENDIF.

        ls_unitsofmeasurex-alt_unit_iso = ls_unitsofmeasure-alt_unit_iso .

        CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
          EXPORTING
            input          = ls_initem-meins
            language       = sy-langu
          IMPORTING
            output         = ls_initem-meins
          EXCEPTIONS
            unit_not_found = 1
            OTHERS         = 2.
        IF sy-subrc <> 0.
* Implement suitable error handling here
        ENDIF.

        ls_unitsofmeasure-alt_unit  = ls_initem-meins .

        ls_unitsofmeasurex-alt_unit = ls_unitsofmeasure-alt_unit .

      ENDIF .


      ls_unitsofmeasure-alt_unit_iso = ls_unitsofmeasure-alt_unit_iso .
      ls_unitsofmeasurex-alt_unit_iso = ls_unitsofmeasure-alt_unit_iso .





      IF ls_initem-umrez IS NOT INITIAL .
        ls_unitsofmeasure-numerator  = ls_initem-umrez.  "转换分子
        ls_unitsofmeasurex-numerator = 'X'.
      ENDIF  .


      IF ls_initem-umren IS NOT INITIAL .
        ls_unitsofmeasure-denominatr  = ls_initem-umren.  "转换分母
        ls_unitsofmeasurex-denominatr = 'X'.
      ENDIF .

      ls_unitsofmeasure-ean_upc = ls_initem-ean11 .
      ls_unitsofmeasure-ean_cat = ls_initem-numtp .

      ls_unitsofmeasurex-ean_upc = 'X' .
      ls_unitsofmeasurex-ean_cat = 'X' .

      ls_unitsofmeasure-gross_wt  = ls_initem-brgew .
      ls_unitsofmeasurex-gross_wt = 'X' .

      APPEND ls_unitsofmeasure TO lt_unitsofmeasure .
      APPEND ls_unitsofmeasurex TO lt_unitsofmeasurex .
    ENDIF .

*MRP视图

    IF ls_initem-zmrpbs IS NOT INITIAL .
      ls_headdata-mrp_view = 'X' .


      ls_plantdata-mrp_type = ls_initem-dismm .          "MRP类型
      ls_plantdatax-mrp_type = 'X' .

      IF ls_initem-dispo IS NOT INITIAL .
        ls_plantdata-mrp_ctrler = ls_initem-dispo .        "MRP控制者
        ls_plantdatax-mrp_ctrler = 'X' .
      ENDIF .

      IF ls_initem-disls IS NOT INITIAL .
        ls_plantdata-lotsizekey = ls_initem-disls .        "批量大小
        ls_plantdatax-lotsizekey = 'X' .
      ENDIF .

      IF ls_initem-bstfe IS NOT INITIAL .
        ls_plantdata-fixed_lot = ls_initem-bstfe .         "固定批量大小
        ls_plantdatax-fixed_lot = 'X' .
      ENDIF .

      IF ls_initem-bstmi IS NOT INITIAL .
        ls_plantdata-minlotsize = ls_initem-bstmi .        "最小批量大小
        ls_plantdatax-minlotsize = 'X' .
      ENDIF .

      IF ls_initem-bstma IS NOT INITIAL .
        ls_plantdata-maxlotsize = ls_initem-bstma .        "最大批量大小
        ls_plantdatax-maxlotsize = 'X' .
      ENDIF .

      IF ls_initem-bstrf IS NOT INITIAL .
        ls_plantdata-round_val = ls_initem-bstrf .         "舍入值
        ls_plantdatax-round_val = 'X' .
      ENDIF .
      ls_plantdata-max_stock = ls_initem-mabst .         "最大库存水平
      ls_plantdatax-max_stock = 'X' .

      ls_plantdata-safety_stk = ls_initem-eisbe .        "安全库存
      ls_plantdatax-safety_stk = 'X' .
      IF ls_initem-sobsl IS NOT INITIAL .
        ls_plantdata-spproctype = ls_initem-sobsl .        "特殊采购类型
        ls_plantdatax-spproctype = 'X' .
      ENDIF .

      ls_plantdata-proc_type = ls_initem-beskz .         "采购类型
      ls_plantdatax-proc_type = 'X' .

      ls_plantdata-backflush = ls_initem-rgekz .         "反冲
      ls_plantdatax-backflush = 'X' .

      IF ls_initem-dzeit IS NOT INITIAL .
        ls_plantdata-inhseprodt = ls_initem-dzeit .        "自制生产时间
        ls_plantdatax-inhseprodt = 'X' .
      ENDIF .

      IF ls_initem-fhori IS INITIAL .
        ls_initem-fhori = '000' .
      ENDIF .
      ls_plantdata-sm_key = ls_initem-fhori .            "计划编辑码
      ls_plantdatax-sm_key = 'X' .

      ls_plantdata-plnd_delry = ls_initem-plifz .        "计划交货时间
      ls_plantdatax-plnd_delry = 'X' .

      ls_plantdata-gr_pr_time = ls_initem-webaz .        "收货处理时间
      ls_plantdatax-gr_pr_time = 'X' .

      ls_plantdata-sloc_exprc = ls_initem-lgfsb .        "外部采购的缺省存储地点
      ls_plantdatax-sloc_exprc = 'X' .

      ls_plantdata-iss_st_loc = ls_initem-lgpro .        "发货存储地点
      ls_plantdatax-iss_st_loc = 'X' .

      IF ls_initem-strgr IS INITIAL .
        ls_initem-strgr = '40' .
      ENDIF .
      ls_plantdata-plan_strgp = ls_initem-strgr .        "计划策略组
      ls_plantdatax-plan_strgp = 'X' .

      IF ls_initem-vrmod IS INITIAL .
        ls_initem-vrmod = '2' .
      ENDIF .
      ls_plantdata-consummode = ls_initem-vrmod .        "消耗模式
      ls_plantdatax-consummode = 'X' .

      IF ls_initem-vint1 IS NOT INITIAL .
        ls_initem-vint1 = '999' .
      ENDIF .
      ls_plantdata-bwd_cons = ls_initem-vint1 .          "消耗期间:逆向
      ls_plantdatax-bwd_cons = 'X' .

      IF ls_initem-vint2 IS INITIAL .
        ls_initem-vint2 = '999' .
      ENDIF .
      ls_plantdata-fwd_cons = ls_initem-vint2 .          "消耗时期-向前
      ls_plantdatax-fwd_cons = 'X' .

      IF ls_initem-mtvfp IS INITIAL .
        ls_initem-mtvfp = '02' .
      ENDIF .
      ls_plantdata-availcheck = ls_initem-mtvfp  .       "可用性检查的检查组
      ls_plantdatax-availcheck = 'X' .

      IF ls_initem-sbdkz IS INITIAL .
        ls_initem-sbdkz = '2' .
      ENDIF .
      ls_plantdata-dep_req_id = ls_initem-sbdkz  .       "对于独立和集中需求的相关需求标识
      ls_plantdatax-dep_req_id = 'X' .

      ls_plantdata-production_scheduler = ls_initem-fevor  .   "生产主管
      ls_plantdatax-production_scheduler = 'X' .

      IF ls_initem-sfcpf IS INITIAL .
        ls_initem-sfcpf = '000001' .
      ENDIF .
      ls_plantdata-prodprof = ls_initem-sfcpf  .         "生产计划参数文件
      ls_plantdatax-prodprof = 'X' .

    ENDIF .

*一般工厂视图
    IF ls_initem-zgcbs IS NOT INITIAL.

      ls_headdata-work_sched_view = 'X' .

*      ls_plantdata-stge_pd_un  = ls_initem-lzeih.     "最大库存期间单位
*      ls_plantdatax-stge_pd_un = 'X' .

      IF ls_initem-klart = '022' .
        ls_plantdata-batch_mgmt = 'X' .
      ELSE .
        ls_plantdata-batch_mgmt  = ls_initem-xchpf.     "批次管理(工厂)
      ENDIF .
      ls_plantdatax-batch_mgmt = 'X' .

      ls_plantdata-issue_unit  = ls_initem-ausme.     "发货单位
      ls_plantdatax-issue_unit = 'X' .

      ls_cliendata-minremlife  = ls_initem-mhdrz.     "最小剩余货架寿命
      ls_cliendatax-minremlife = 'X' .

      ls_cliendata-shelf_life  = ls_initem-mhdhb.     "总货架寿命
      ls_cliendatax-shelf_life = 'X' .

      ls_cliendata-period_ind_expiration_date  = ls_initem-iprkz.     "货架寿命期间标识
      ls_cliendatax-period_ind_expiration_date = 'X' .

    ENDIF .

*销售视图
    IF ls_initem-zxsbs IS NOT INITIAL.
      ls_headdata-sales_view = 'X' .

      ls_salesdata-sales_org  = ls_initem-vkorg .        "销售组织
      ls_salesdatax-sales_org = ls_initem-vkorg .

      ls_salesdata-distr_chan  = ls_initem-vtweg .       "分销渠道
      ls_salesdatax-distr_chan = ls_initem-vtweg .

      ls_salesdata-acct_assgt  = ls_initem-ktgrm .       "物料的科目分配组
      ls_salesdatax-acct_assgt = 'X' .

      ls_salesdata-delyg_plnt  = ls_initem-dwerk .       "交货工厂
      ls_salesdatax-delyg_plnt = 'X' .

      IF ls_initem-mtpos IS INITIAL .
        ls_initem-mtpos = 'NORM' .
      ENDIF .
      ls_salesdata-item_cat  = ls_initem-mtpos .         "来自物料主数据的项目类别组
      ls_salesdatax-item_cat = 'X' .

      IF ls_initem-versg IS INITIAL .
        ls_initem-versg = '1' .
      ENDIF .
      ls_salesdata-matl_stats  = ls_initem-versg .       "物料统计组
      ls_salesdatax-matl_stats = 'X' .

      IF ls_initem-spart IS INITIAL .
        ls_initem-spart = '00' .
      ENDIF .
      ls_cliendata-division  = ls_initem-spart .         "产品组
      ls_cliendatax-division = 'X' .

      IF ls_initem-tragr IS INITIAL .
        ls_initem-tragr = '0001' .
      ENDIF .
      ls_cliendata-trans_grp  = ls_initem-tragr .        "运输组
      ls_cliendatax-trans_grp = 'X' .

      ls_cliendata-item_cat  = ls_initem-mtpos_mara .    "常规项目类别组
      ls_cliendatax-item_cat = 'X' .

      IF ls_initem-ladgr IS INITIAL .
        ls_initem-ladgr = '0001' .
      ENDIF .
      ls_plantdata-loadinggrp = ls_initem-ladgr .        "装载组
      ls_plantdatax-loadinggrp = 'X' .

      IF ls_initem-aland IS INITIAL .
        ls_initem-aland = 'CN' .
      ENDIF .
      ls_taxclassifications-depcountry = ls_initem-aland."国家/地区

      IF ls_initem-tatyp IS INITIAL .
        ls_initem-tatyp = 'MWST' .
      ENDIF .
      ls_taxclassifications-tax_type_1 = ls_initem-tatyp."税收类别
      ls_taxclassifications-taxclass_1 = ls_initem-taxkm."税
      APPEND ls_taxclassifications TO lt_taxclassifications .

    ENDIF .

*财务视图
    IF ls_initem-zcwbs IS NOT INITIAL .

      ls_headdata-account_view = 'X' .
      ls_headdata-cost_view = 'X' .

      ls_valuationdata-val_area = ls_initem-werks .
      ls_valuationdatax-val_area = ls_initem-werks .

      ls_valuationdata-val_class  = ls_initem-bklas .     "评估类
      ls_valuationdatax-val_class = 'X' .                 "
      "
      ls_valuationdata-ml_settle  = ls_initem-mlast .     "价格确定
      ls_valuationdatax-ml_settle = 'X' .                 "
      "
      ls_valuationdata-price_unit  = ls_initem-peinh .    "价格单位
      ls_valuationdatax-price_unit = 'X' .                "
      "
      ls_valuationdata-price_ctrl  = ls_initem-vprsv .    "价格控制
      ls_valuationdatax-price_ctrl = 'X' .                "
      "
      IF ls_initem-ekalr IS INITIAL .
        ls_initem-ekalr = 'X' .
      ENDIF .
      ls_valuationdata-qty_struct  = ls_initem-ekalr .    "用QS成本估算
      ls_valuationdatax-qty_struct = 'X' .                "
      "
      IF ls_initem-hkmat IS INITIAL .
        ls_initem-hkmat = 'X' .
      ENDIF .
      ls_valuationdata-orig_mat  = ls_initem-hkmat .      "物料来源
      ls_valuationdatax-orig_mat = 'X' .                  "

    ENDIF .

*仓储视图
    IF ls_initem-zccbs IS NOT INITIAL.
      ls_headdata-warehouse_view = 'X' .
      ls_headdata-storage_view   = 'X' .

      ls_storagelocationdata-plant = ls_initem-werks .
      ls_storagelocationdatax-plant = ls_initem-werks .

      ls_storagelocationdata-stge_loc = ls_initem-lgort .
      ls_storagelocationdatax-stge_loc = ls_initem-lgort .

      ls_storagetypedata-whse_no  = ls_initem-lgnum .
      ls_storagetypedatax-whse_no = ls_initem-lgnum ."'X' .

      ls_storagetypedata-stge_type  = ls_initem-lgtyp .
      ls_storagetypedatax-stge_type = ls_initem-lgtyp ."'X' .

      ls_warehousenumberdata-whse_no  = ls_initem-lgnum .
      ls_warehousenumberdatax-whse_no = ls_initem-lgnum ."'X' .

      ls_warehousenumberdata-withdrawal  = ls_initem-ltkza .
      ls_warehousenumberdatax-withdrawal = 'X' ."'X' .

      ls_warehousenumberdata-placement  = ls_initem-ltkze .
      ls_warehousenumberdatax-placement = 'X' ."'X' .

      ls_warehousenumberdata-stgesector  = ls_initem-lgbkz .
      ls_warehousenumberdatax-stgesector = 'X' ."'X' .

    ENDIF .

*扩展字段
    IF ls_initem-zzxbz  IS NOT INITIAL
    OR ls_initem-zwmsx  IS NOT INITIAL
    OR ls_initem-zzsbs  IS NOT INITIAL
    OR ls_initem-zcbjbs IS NOT INITIAL
    OR ls_initem-zgxpbs IS NOT INITIAL .

      ls_extensionin-structure  = 'BAPI_TE_MARA'.   "扩展结构
      ls_extensioninx-structure = 'BAPI_TE_MARAX'.  "扩展结构

      ls_te_mara-material  = ls_initem-matnr.
      ls_te_marax-material = ls_initem-matnr.

      ls_te_mara-zzxbz     = ls_initem-zzxbz.
      ls_te_marax-zzxbz    = 'X'.

      ls_te_mara-zwmsx     = ls_initem-zwmsx.
      ls_te_marax-zwmsx    = 'X'.

      ls_te_mara-zzsbs     = ls_initem-zzsbs.
      ls_te_marax-zzsbs    = 'X'.

      ls_te_mara-zcbjbs    = ls_initem-zcbjbs.
      ls_te_marax-zcbjbs   = 'X'.

      ls_te_mara-zgxpbs    = ls_initem-zgxpbs.
      ls_te_marax-zgxpbs   = 'X'.

      ls_te_mara-zkdpbs    = ls_initem-zkdpbs.
      ls_te_marax-zkdpbs   = 'X'.

      ls_te_mara-zedpbs    = ls_initem-zedpbs.
      ls_te_marax-zedpbs   = 'X'.

*      ls_te_mara-zkdpbs    = ls_initem-zkdpbs.
*      ls_te_marax-zkdpbs   = 'X'.
*
*      ls_te_mara-zedpbs    = ls_initem-zedpbs.
*      ls_te_marax-zedpbs   = 'X'.

      lv_valuepart = ls_te_mara .

      ls_extensionin-valuepart1 = lv_valuepart(240).
      ls_extensionin-valuepart2 = lv_valuepart+240(240).
      ls_extensionin-valuepart3 = lv_valuepart+480(240).
      ls_extensionin-valuepart4 = lv_valuepart+720(240).
      APPEND ls_extensionin TO lt_extensionin.

      CLEAR lv_valuepart.
      lv_valuepart = ls_te_marax.
      ls_extensioninx-valuepart1 = lv_valuepart(240).
      ls_extensioninx-valuepart2 = lv_valuepart+240(240).
      ls_extensioninx-valuepart3 = lv_valuepart+480(240).
      ls_extensioninx-valuepart4 = lv_valuepart+720(240).
      APPEND ls_extensioninx TO lt_extensioninx.
      CLEAR ls_extensioninx.
      CLEAR lv_valuepart.

    ENDIF .

    IF ls_initem-zzjbs IS NOT INITIAL .
      ls_headdata-quality_view = 'X'.
    ENDIF .

    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
      EXPORTING
        headdata             = ls_headdata
        clientdata           = ls_cliendata
        clientdatax          = ls_cliendatax
        plantdata            = ls_plantdata
        plantdatax           = ls_plantdatax
        warehousenumberdata  = ls_warehousenumberdata
        warehousenumberdatax = ls_warehousenumberdatax
        storagelocationdata  = ls_storagelocationdata
        storagelocationdatax = ls_storagelocationdatax
        salesdata            = ls_salesdata
        salesdatax           = ls_salesdatax
        storagetypedata      = ls_storagetypedata
        storagetypedatax     = ls_storagetypedatax
        valuationdata        = ls_valuationdata
        valuationdatax       = ls_valuationdatax
      IMPORTING
        return               = ls_return
      TABLES
        materialdescription  = lt_bapi_makt
        internationalartnos  = lt_internationalartnos
*       materiallongtext     = lt_bapi_mltx
        unitsofmeasure       = lt_unitsofmeasure
        unitsofmeasurex      = lt_unitsofmeasurex
        extensionin          = lt_extensionin
        extensioninx         = lt_extensioninx
        taxclassifications   = lt_taxclassifications.

    IF ls_return-type <> 'E' AND ls_return-type <> 'A' .

      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.

      READ TABLE output-item INTO ls_outitem WITH KEY id = ls_initem-id .
      IF sy-subrc = 0 .
        ls_outitem-type = 'S' .
        ls_outitem-message = '创建成功' .
        ls_outitem-matnr = ls_initem-matnr .

        MODIFY output-item FROM ls_outitem TRANSPORTING type message matnr WHERE id = ls_initem-id  .
      ENDIF .

*质量视图

      IF ls_initem-zzjbs IS NOT INITIAL .

        ls_inspectionctrl-material_long             = ls_initem-matnr ."物料
        ls_inspectionctrl-plant                     = ls_initem-werks ."工厂
        ls_inspectionctrl-insptype                  = ls_initem-art. "检验类型
        ls_inspectionctrl-ind_insptype_mat_active   = ls_initem-aktiv. "活动的
        ls_inspectionctrl-ave_insp_duration         = ls_initem-mpdau. "检验周期
        ls_inspectionctrl-ind_insp_with_tsk_list    = 'X'. "
        ls_inspectionctrl-ind_auto_assign           = 'X'. "
        ls_inspectionctrl-ind_insp_by_charac        = 'X'. "
        ls_inspectionctrl-ind_post_to_insp_stock    = 'X'. "
        ls_inspectionctrl-ind_automatic_ud          = 'X'. "
        ls_inspectionctrl-ind_skips_allowed         = 'X'. "
        ls_inspectionctrl-ind_single_units_possible = 'X'. "
        ls_inspectionctrl-qual_score_procedure      = '06'. "
        ls_inspectionctrl-preferred_insptype        = 'X'. "

        APPEND ls_inspectionctrl TO lt_inspectionctrl .

        CALL FUNCTION 'BAPI_MATINSPCTRL_SAVEREPLICA'
          TABLES
            return         = lt_return
            inspectionctrl = lt_inspectionctrl.

        LOOP AT lt_return INTO ls_return WHERE type = 'A' OR type = 'E'.
          EXIT.
        ENDLOOP.
        IF sy-subrc = 0 .
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

          READ TABLE output-item INTO ls_outitem WITH KEY id = ls_initem-id .
          IF sy-subrc = 0 .
            ls_outitem-type = 'E' .
            ls_outitem-message = '质量视图扩展失败' .
            MODIFY output-item FROM ls_outitem TRANSPORTING type message WHERE id = ls_initem-id  .
          ENDIF .
        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = 'X'.
        ENDIF.

      ENDIF .

*分类视图
      IF ls_initem-class IS NOT INITIAL  .

        classnumnew = ls_initem-class.
        classtypenew = ls_initem-klart.

        IF ls_initem-class = 'Z_ZXQ01' AND ls_initem-klart = '001' .
          CLEAR : is_allocvalueschar .
          is_allocvalueschar-charact    = 'Z_CLCZ' .
          is_allocvalueschar-value_char = ls_initem-z_clcz .
          APPEND is_allocvalueschar TO it_allocvalueschar .
          CLEAR : is_allocvalueschar .

          is_allocvalueschar-charact    = 'Z_CPGG' .
          is_allocvalueschar-value_char = ls_initem-z_cpgg .
          APPEND is_allocvalueschar TO it_allocvalueschar .
          CLEAR : is_allocvalueschar .

          is_allocvalueschar-charact    = 'Z_BZXS' .
          is_allocvalueschar-value_char = ls_initem-z_bzxs .
          APPEND is_allocvalueschar TO it_allocvalueschar .
          CLEAR : is_allocvalueschar .

          is_allocvalueschar-charact    = 'Z_ZXCC' .
          is_allocvalueschar-value_char = ls_initem-z_zxcc .
          APPEND is_allocvalueschar TO it_allocvalueschar .
          CLEAR : is_allocvalueschar .

          is_allocvalueschar-charact    = 'Z_DXCC' .
          is_allocvalueschar-value_char = ls_initem-z_dxcc .
          APPEND is_allocvalueschar TO it_allocvalueschar .
          CLEAR : is_allocvalueschar .

          is_allocvalueschar-charact    = 'Z_ZXSL' .
          is_allocvalueschar-value_char = ls_initem-z_zxsl .
          APPEND is_allocvalueschar TO it_allocvalueschar .
          CLEAR : is_allocvalueschar .

          is_allocvalueschar-charact    = 'Z_DXSL' .
          is_allocvalueschar-value_char = ls_initem-z_dxsl .
          APPEND is_allocvalueschar TO it_allocvalueschar .
          CLEAR : is_allocvalueschar .

          is_allocvalueschar-charact    = 'Z_DXJZ' .
          is_allocvalueschar-value_char = ls_initem-z_dxjz .
          APPEND is_allocvalueschar TO it_allocvalueschar .
          CLEAR : is_allocvalueschar .

          is_allocvalueschar-charact    = 'Z_DXMZ' .
          is_allocvalueschar-value_char = ls_initem-z_dxmz .
          APPEND is_allocvalueschar TO it_allocvalueschar .
          CLEAR : is_allocvalueschar .

          is_allocvalueschar-charact    = 'Z_ZXJZ' .
          is_allocvalueschar-value_char = ls_initem-z_zxjz .
          APPEND is_allocvalueschar TO it_allocvalueschar .
          CLEAR : is_allocvalueschar .

          is_allocvalueschar-charact    = 'Z_ZXMZ' .
          is_allocvalueschar-value_char = ls_initem-z_zxmz .
          APPEND is_allocvalueschar TO it_allocvalueschar .
          CLEAR : is_allocvalueschar .
        ENDIF .
        objectkeynew = ls_initem-matnr .

        CALL FUNCTION 'BAPI_OBJCL_CREATE'
          EXPORTING
            objectkeynew    = objectkeynew
            objecttablenew  = 'MARA'
            classnumnew     = classnumnew
            classtypenew    = classtypenew
          TABLES
            allocvalueschar = it_allocvalueschar
            return          = return_obj[].

        LOOP AT return_obj INTO ls_return WHERE type = 'A' OR type = 'E'.
          EXIT.
        ENDLOOP.
        IF sy-subrc = 0 .
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
          READ TABLE output-item INTO ls_outitem WITH KEY id = ls_initem-id .
          IF sy-subrc = 0 .
            ls_outitem-type = 'E' .
            ls_outitem-message = ls_outitem-message && '分类视图扩展失败' .
            MODIFY output-item FROM ls_outitem TRANSPORTING type message WHERE id = ls_initem-id  .
          ENDIF .
        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = 'X'.
        ENDIF.
      ENDIF .

      "扩充同一工厂下所有库存地点
      IF ls_initem-lgort IS NOT INITIAL AND ls_initem-werks IS NOT INITIAL .

        SELECT werks ,lgort FROM t001l
          INTO TABLE @DATA(lt_lgort)
          WHERE werks = @ls_initem-werks .

        IF lt_lgort IS NOT INITIAL .
          DELETE lt_lgort WHERE lgort = ls_initem-lgort .

          ls_head-function     = '009'.
          ls_head-material     = ls_initem-matnr.
          ls_head-storage_view = 'X'.
          APPEND ls_head TO lt_head.
          CLEAR ls_head.

          LOOP AT lt_lgort INTO DATA(ls_lgort) .

            ls_strlgort-function = '009' .
            ls_strlgort-material = ls_initem-matnr .
            ls_strlgort-plant    = ls_initem-werks .
            ls_strlgort-stge_loc = ls_lgort-lgort .
            APPEND ls_strlgort TO lt_strlgort .
            CLEAR : ls_strlgort .

            ls_strlgortx-function = '009' .
            ls_strlgortx-material = ls_initem-matnr .
            ls_strlgortx-plant    = ls_initem-werks .
            ls_strlgortx-stge_loc = ls_lgort-lgort .
            APPEND ls_strlgortx TO lt_strlgortx .
            CLEAR : ls_strlgortx .

            CLEAR : ls_lgort .
          ENDLOOP.

          CALL FUNCTION 'BAPI_MATERIAL_SAVEREPLICA'
            EXPORTING
              noappllog            = 'X'
              nochangedoc          = ''
              testrun              = ''
              inpfldcheck          = ''
*             FLAG_CAD_CALL        = ' '
*             NO_ROLLBACK_WORK     = ' '
*             FLAG_ONLINE          = ' '
            IMPORTING
              return               = lv_return
            TABLES
              headdata             = lt_head
              storagelocationdata  = lt_strlgort
              storagelocationdatax = lt_strlgortx.

          IF lv_return <> 'E' AND lv_return <> 'A' .

            CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
              EXPORTING
                wait = 'X'.

          ELSE .

            ls_outitem-type = 'E' .
            ls_outitem-message = ls_outitem-message && '库存地点扩充失败:' && lv_return-message .
            MODIFY output-item FROM ls_outitem TRANSPORTING type message WHERE id = ls_initem-id  .
            CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

          ENDIF .

        ENDIF .

      ENDIF .

    ELSE .
      ls_outitem-type = 'E' .
      ls_outitem-message = ls_outitem-message && '物料创建失败:' && ls_return-message .
      MODIFY output-item FROM ls_outitem TRANSPORTING type message WHERE id = ls_initem-id  .
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

    ENDIF .

    CLEAR : ls_return ,lt_return ,ls_headdata ,ls_plantdata ,ls_plantdatax ,ls_salesdata ,ls_salesdatax ,ls_cliendata ,ls_cliendatax ,
            ls_valuationdata ,ls_valuationdatax ,ls_storagelocationdata ,ls_storagelocationdatax ,ls_warehousenumberdata ,
            ls_warehousenumberdatax ,ls_taxclassifications ,ls_storagetypedata ,ls_storagetypedatax ,ls_internationalartnos ,
            lt_internationalartnos ,lt_taxclassifications ,ls_tq34 ,lt_tq34 ,ls_inspectionctrl ,lt_inspectionctrl ,lt_bapi_makt,
            ls_bapi_makt ,lt_bapi_mltx ,ls_bapi_mltx ,ls_unitsofmeasure ,lt_unitsofmeasure ,ls_unitsofmeasurex ,lt_unitsofmeasurex ,
            objectkeynew ,classnumnew ,classtypenew ,lv_valuepart ,ls_te_mara ,ls_te_marax ,ls_extensionin ,lt_extensionin ,ls_extensioninx ,
            lt_extensioninx ,material_number[] ,ls_initem .

  ENDLOOP .

  READ TABLE output-item INTO ls_outitem WITH KEY type = 'S' .
  IF sy-subrc = 0 .
    output-return-msgty = 'S' .
    output-return-msgtx = '执行成功' .
  ELSE .
    output-return-msgty = 'E' .
    output-return-msgtx = '执行失败' .
  ENDIF .

ENDFUNCTION.
Extracted by Direct Download Enterprise version 1.3.1 - E.G.Mellodew. 1998-2005 UK. Sap Release 756

你可能感兴趣的:(SAP,ABAP,笔记,经验分享)