REPORT ztest_copy_bom.
DATA: lit_stb TYPE TABLE OF stpox,
lwa_stb TYPE stpox,
lit_matcat TYPE TABLE OF cscmat,
lwa_vbap TYPE vbap,
lwa_stko TYPE stko_api01,
lwa_stpo TYPE stpo_api03,
lit_stpo TYPE TABLE OF stpo_api03 WITH HEADER LINE,
l_item_no TYPE stpo_api03-item_no.
DATA:git_stb TYPE STANDARD TABLE OF stpox,
gwa_stb TYPE stpox.
DATA:lit_line TYPE STANDARD TABLE OF csltx_line,
lwa_line TYPE csltx_line.
SELECTION-SCREEN BEGIN OF BLOCK b_kopvor
WITH FRAME TITLE text-001.
PARAMETERS: p_matnr1 LIKE mast-matnr MEMORY ID mat,
p_werks1 LIKE mast-werks OBLIGATORY MEMORY ID wrk, "plant
p_stlan1 LIKE mast-stlan DEFAULT '1' MODIF ID mod. "BOM Usage
SELECTION-SCREEN END OF BLOCK b_kopvor.
SELECTION-SCREEN BEGIN OF BLOCK b_aufstu
WITH FRAME TITLE text-002.
PARAMETERS: p_vbeln LIKE kdst-vbeln MEMORY ID aun
MATCHCODE OBJECT vmva,
p_vbpos LIKE kdst-vbpos MEMORY ID aup,
p_matnr2 LIKE kdst-matnr MEMORY ID ma2,
p_werks2 LIKE mast-werks OBLIGATORY MEMORY ID wrk, "Werk
p_stlan2 LIKE mast-stlan DEFAULT '1' MODIF ID mod.
SELECTION-SCREEN END OF BLOCK b_aufstu.
DATA:l_from TYPE csap_kbom-datuv.
* 物料BOM展开
CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'
EXPORTING
mtnrv = p_matnr1 "'AK47'
werks = p_werks1 "'1130'
stlan = p_stlan1 "'1'
datuv = sy-datum
ehndl = '1'
mmory = '1'
capid = 'PP01'
aufsw = ' '
mbwls = ' '
mehrs = ' '
TABLES
stb = git_stb
EXCEPTIONS
alt_not_found = 1
call_invalid = 2
material_not_found = 3
missing_authorization = 4
no_bom_found = 5
no_plant_data = 6
no_suitable_bom_found = 7.
LOOP AT git_stb INTO gwa_stb.
* BOM Item
ADD 10 TO l_item_no.
"Fill Bom Item to update
CLEAR lwa_stpo.
lwa_stpo-item_categ = gwa_stb-postp.
CALL FUNCTION 'CONVERSION_EXIT_NUMCV_INPUT'
EXPORTING
input = l_item_no
IMPORTING
output = lwa_stpo-item_no.
lwa_stpo-component = gwa_stb-idnrk.
* 将物料BOM中的class K转换成L,创建订单BOM
IF lwa_stpo-item_categ = 'K'.
lwa_stpo-item_categ = 'L'.
lwa_stpo-component = gwa_stb-ojtxp.
ENDIF.
" … Fill any fields you need
lwa_stpo-comp_qty = gwa_stb-menge.
CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
EXPORTING
input = gwa_stb-meins
language = sy-langu
IMPORTING
* LONG_TEXT =
output = lwa_stpo-comp_unit
* SHORT_TEXT =
EXCEPTIONS
unit_not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
lwa_stpo-item_text1 = gwa_stb-potx1.
lwa_stpo-item_text2 = gwa_stb-potx2.
lwa_stpo-bom_no = gwa_stb-stlnr.
lwa_stpo-item_node = gwa_stb-stlkn.
lwa_stpo-item_count = gwa_stb-stpoz.
lwa_stpo-bom_alt = gwa_stb-stlal.
* identifier用来标识长文本是保存在哪个BOM Item上
lwa_stpo-identifier = l_item_no.
APPEND lwa_stpo TO lit_stpo.
* 长文本
CLEAR:lwa_line.
* identifier用来标识长文本是保存在哪个BOM Item上
lwa_line-identifier = l_item_no.
lwa_line-tdformat = '*'.
lwa_line-tdline = 'line1'.
lwa_line-object_id = '2'. "2: STPO, 1:STKP 0:STZU
APPEND lwa_line TO lit_line.
lwa_line-tdline = 'line12'.
APPEND lwa_line TO lit_line.
lwa_line-tdline = 'line123'.
APPEND lwa_line TO lit_line.
lwa_line-tdline = 'line1234'.
APPEND lwa_line TO lit_line.
ENDLOOP.
CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
EXPORTING
date_internal = sy-datum
IMPORTING
date_external = l_from
EXCEPTIONS
date_internal_is_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
* 订单BOM维护
CALL FUNCTION 'CSAP_ORD_BOM_MAINTAIN'
EXPORTING
order = p_vbeln "'06900222222'
position = p_vbpos "'200'
material = p_matnr2 "'000000000050333339'
plant = p_werks2 "'1130'
bom_usage = p_stlan2 "'1'
valid_from = l_from
i_stko = lwa_stko
fl_new_item = 'X'
fl_commit_and_wait = 'X'
TABLES
t_stpo = lit_stpo
t_ltx_line = lit_line
EXCEPTIONS
error = 1
OTHERS = 2.