这是在佛山好帮手时受启发而研究出来的,创建物料,带单位,类型组
经测试。。。。算了,不说了,有什么限制自己测去。。。今天心情不好。。。
FUNCTION ZLY_CREATE_PRODUCT_UNIT.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(IN_PRODUCT_ID) TYPE COMT_PRODUCT_ID
*" REFERENCE(IN_PRODUCT_TYPE) TYPE COMT_PRODUCT_TYPE DEFAULT '01'
*" REFERENCE(IN_SHORT_TEXT) TYPE COMT_PRSHTEXTX DEFAULT
*" 'CRM CREATE PRODUCT'
*" REFERENCE(IN_CATEGORY_ID) TYPE COMT_CATEGORY_ID DEFAULT
*" 'MAT_ROH'
*" EXPORTING
*" REFERENCE(RT_RETURN) TYPE BAPIRET2_TAB
*"----------------------------------------------------------------------
*--> Structures
DATA : ls_product TYPE comt_product_mat_maintain_api,
ls_comt_prod TYPE comt_product_maintain_api,
ls_prod TYPE comt_product,
ls_prod_categ2 TYPE comt_prod_cat_rel,
ls_srv_maintain TYPE comt_prod_mat_maintain_api,
ls_pr_unit TYPE comt_pr_unit_maintain,
ls_unit TYPE comt_pr_unit,
ls_shtext TYPE comt_pr_shtext_maintain,
ls_text TYPE comt_prshtext,
ls_ret_prod TYPE comt_product_mat_maintain_api,
ls_return TYPE bapiret2.
*--> Internal Tables
**----------------------------------------------------------------------
*----------
DATA : lt_product TYPE comt_product_mat_maintain_apit,
lt_ret_prod TYPE comt_product_mat_maintain_apit,
lt_prod_categ TYPE comt_prod_cat_rel_maintain_tab,
lt_pr_unit TYPE comt_pr_unit_maintain_tab,
lt_shtext_maint TYPE comt_pr_shtext_maintain_tab.
*-->logsys get
DATA iv_logsys TYPE TBDLS-LOGSYS.
CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'
IMPORTING
OWN_LOGICAL_SYSTEM = iv_logsys
EXCEPTIONS
OWN_LOGICAL_SYSTEM_NOT_DEFINED = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
ENDIF.
**--> Maintain Category relevant data
DATA:ls_category TYPE comm_category .
CLEAR ls_prod_categ2.
SELECT SINGLE * FROM comm_category INTO ls_category WHERE category_id = IN_CATEGORY_ID." 'ZMNFG'.
ls_prod_categ2-category_id = ls_category-category_id.
ls_prod_categ2-category_guid = ls_category-category_guid.
ls_prod_categ2-logsys = iv_logsys.
APPEND ls_prod_categ2 TO lt_prod_categ.
**--> Maintain Product-general data
ls_prod-product_id = in_product_id.
ls_prod-product_type = in_product_type.
ls_prod-client = sy-mandt.
ls_prod-logsys = iv_logsys.
MOVE ls_prod TO ls_comt_prod-com_product.
MOVE lt_prod_categ TO ls_comt_prod-categories.
**--> Maintain Units of measure data
* LOOP AT it_unit INTO ls_unit.
* ls_unit-logsys = iv_logsys.
* MOVE ls_unit TO ls_pr_unit-data.
* APPEND ls_pr_unit TO lt_pr_unit.
* ENDLOOP.
ls_unit-unit = 'PC'.
ls_unit-numerator = '1'.
ls_unit-denominator = '1'.
ls_unit-is_base_unit = 'X'.
ls_unit-logsys = iv_logsys.
MOVE ls_unit TO ls_pr_unit-data.
APPEND ls_pr_unit TO lt_pr_unit.
MOVE lt_pr_unit TO ls_srv_maintain-comm_pr_unit.
MOVE ls_srv_maintain TO ls_product-data.
**--> Maintain Short texts
ls_text-langu = sy-langu.
ls_text-short_text = in_short_text.
ls_text-logsys = iv_logsys.
MOVE ls_text TO ls_shtext-data.
APPEND ls_shtext TO lt_shtext_maint.
MOVE lt_shtext_maint TO ls_comt_prod-short_texts.
MOVE ls_comt_prod TO ls_product-header.
APPEND ls_product TO lt_product.
**--> Create Product
CALL FUNCTION 'COM_PRODUCT_MAT_MAINT_MULT_API'
EXPORTING
it_product = lt_product
iv_check_only = ' '
iv_suppress_inactive = ' '
iv_activate_inactive_products = ' '
iv_application = ' '
IMPORTING
et_product = lt_ret_prod
et_bapireturn = rt_return
EXCEPTIONS
internal_error = 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.
CALL FUNCTION 'BALW_BAPIRETURN_GET2'
EXPORTING
type = 'E'
cl = sy-msgid
number = sy-msgno
IMPORTING
return = ls_return.
APPEND ls_return TO rt_return.
CLEAR ls_return.
ENDIF.
**--> Save Product
DATA : lv_product_id TYPE symsgv.
CALL FUNCTION 'COM_PRODUCT_SAVE_API'
EXPORTING
iv_update_task = 'X'
EXCEPTIONS
internal_error = 1
save_not_allowed = 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.
* lv_product_id = in_product_id.
* CALL FUNCTION 'BALW_BAPIRETURN_GET2'
* EXPORTING
* type = 'E'
* cl = gc_msg_class
* number = '002'
* par1 = lv_product_id
* IMPORTING
* return = ls_return.
* APPEND ls_return TO xt_return.
* CLEAR ls_return.
ENDIF.
**--> Commit transaction
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
IMPORTING
return = ls_return.
IF ls_return IS NOT INITIAL.
APPEND ls_return TO rt_return.
ENDIF.
**--> Refresh buffer
CALL FUNCTION 'COM_PRODUCT_FREE_API'.
ENDFUNCTION.