解决了bom拷贝不能跨工厂问题
第一步是找到了cs01里面 如何进行跨工厂拷贝的方法,从业务上可行
第二步在此基础上做bdc 难点是 在 翻页时候 pagedown 这个关键地方,否则多行的会丢掉信息
report zcs01 .
tables : mast , cdhdr ,makt , mara , cdpos , stas , stpo , stko,marc.
data:bdcdata like bdcdata occurs 0 with header line.
data: msg type table of bdcmsgcoll.
data:msgs type table of bdcmsgcoll with header line.
data : itab_x type standard table of stpox with header line."多层展开
data : itab_s type standard table of stpox with header line."单层展开
data : itab_tmp like table of itab_x with header line.
data flag.
data: begin of m_itab occurs 0,
matnr like mast-matnr, "物料代码
* werks LIKE mast-werks, "工厂
* stlan LIKE mast-stlan, "BOM用途
* stlal LIKE mast-stlal, "可选的BOM
* ttidx LIKE itab_x-ttidx,
stufe like itab_x-stufe,
* altst LIKE itab_x-altst,
end of m_itab.
data:wam like m_itab .
data:exp_tab like table of m_itab with header line.
data:tmp_tab like table of m_itab with header line.
data: begin of tmsg occurs 0,
msgtext(72),
end of tmsg.
data lmsg like line of tmsg.
data:error like table of m_itab with header line.
*DATA: BEGIN OF itab OCCURS 0,
* matnr LIKE mast-matnr, "物料代码
* werks LIKE mast-werks, "工厂
* stlan LIKE mast-stlan, "BOM用途
* stlal LIKE mast-stlal, "可选的BOM
* andat LIKE mast-andat, "选择时间段
* mandt LIKE mast-mandt, "集团
* stlnr LIKE mast-stlnr, "BOM
* END OF itab.
************ 定义查询界面
*SELECT-OPTIONS:
* s_matnr FOR mast-matnr , "选择物料
* s_andat FOR mast-andat , "日期
* s_werks FOR mast-werks DEFAULT '2000' NO-EXTENSION NO INTERVALS,
*"工厂
* s_stlan FOR mast-stlan NO-EXTENSION NO INTERVALS , "BOM用途
* s_stlal FOR mast-stlal NO-EXTENSION NO INTERVALS . "可选的BOM
*
*PARAMETERS : check1 AS CHECKBOX.
*
*
selection-screen :begin of block blk3 with frame title text-007.
parameter p_matnr like mast-matnr obligatory .
selection-screen :end of block blk3 .
selection-screen :begin of block blk1 with frame title text-005.
selection-screen begin of line.
selection-screen comment (8) text-001 .
parameter p_werksf like mast-werks obligatory .
selection-screen comment 20(8) text-003 .
selection-screen position 28.
parameter p_stlanf like mast-stlan obligatory .
selection-screen comment 32(8) text-002 .
selection-screen position 40.
parameter p_stlalf like mast-stlal obligatory .
selection-screen end of line.
selection-screen: end of block blk1.
selection-screen :begin of block blk2 with frame title text-006.
selection-screen begin of line.
selection-screen comment (8) text-004 .
parameter p_werkst like mast-werks obligatory .
selection-screen comment 20(8) text-003 .
selection-screen position 28.
parameter p_stlant like mast-stlan obligatory .
selection-screen comment 32(8) text-002 .
selection-screen position 40.
parameter p_stlalt like mast-stlal obligatory .
selection-screen end of line.
selection-screen: end of block blk2.
*
**
**SELECTION-SCREEN :BEGIN OF BLOCK blk3 WITH FRAME TITLE text-007.
*PARAMETER p_matnr LIKE mast-matnr.
**SELECTION-SCREEN COMMENT 10(8) text-003 .
*PARAMETER p_stlan LIKE mast-stlan.
**SELECTION-SCREEN COMMENT 32(8) text-003 .
**SELECTION-SCREEN POSITION 40.
*PARAMETER p_stlal LIKE mast-stlal.
*
**SELECTION-SCREEN :END OF BLOCK blk3 .
*SELECTION-SCREEN :BEGIN OF BLOCK blk1 WITH FRAME TITLE text-008.
*SELECTION-SCREEN BEGIN OF LINE.
*SELECTION-SCREEN COMMENT (8) text-001 .
*SELECTION-SCREEN POSITION 10.
*PARAMETER p_werksf LIKE mast-werks.
*
*SELECTION-SCREEN COMMENT 20(8) text-004 .
*SELECTION-SCREEN POSITION 28.
*PARAMETER p_werkst LIKE mast-werks.
*SELECTION-SCREEN END OF LINE.
*SELECTION-SCREEN: END OF BLOCK blk1.
at selection-screen.
perform check_screen.
start-of-selection.
* PERFORM check_screen.
perform procdata .
*&---------------------------------------------------------------------*
*& Form CHECK
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form check_screen .
select single * from marc where matnr = p_matnr and werks = p_werksf .
if sy-subrc <> 0.
message '输入的物料在复制源工厂不存在...' type 'E'.
endif.
select single * from marc where matnr = p_matnr and werks = p_werkst .
if sy-subrc <> 0.
message '输入的物料在目标工厂不存在...' type 'E'.
endif.
select single * from mast where matnr = p_matnr and stlal = p_stlalf and stlan = p_stlanf and werks = p_werksf .
if sy-subrc <> 0.
message '输入的复制源工厂此物料BOM不存在...' type 'E'.
endif.
select single * from mast where matnr = p_matnr and stlal = p_stlalt and stlan = p_stlant and werks = p_werkst .
if sy-subrc = 0.
message '输入的目标工厂此物料BOM已经存在...' type 'E'.
endif.
data msgtext(72).
* MESSAGE ID '29' TYPE 'E' NUMBER '162' WITH '29' into msgtext.
* MESSAGE ID '29' TYPE 'E' NUMBER '162' WITH '29'.
endform. " CHECK
*&---------------------------------------------------------------------*
*& Form procdata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form procdata.
refresh itab_tmp.
call function 'CS_BOM_EXPL_MAT_V2'
exporting
ftrel = 'X'
capid = 'PP01'
datuv = sy-datum
* emeng = 1
mehrs = 'X'
ehndl = '1'
stlan = p_stlanf
stlal = p_stlalf
mtnrv = p_matnr
werks = p_werksf
tables
stb = itab_x
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
conversion_error = 8
others = 9.
loop at itab_x ."解决物料没有创建问题
move-corresponding itab_x to m_itab.
m_itab-matnr = itab_x-idnrk .
append m_itab to tmp_tab.
collect m_itab.
endloop.
loop at m_itab.
select single * from marc where matnr = m_itab-matnr and werks = p_werkst.
if sy-subrc <> 0.
append m_itab to error.
endif.
endloop.
if error[] is not initial.
write:/' 请在目标工厂',p_werkst,'维护以下物料'.
loop at error .
write:/ error-matnr.",' ',sy-tabix.
select single maktx from makt into makt-maktx where matnr = error-matnr and spras = '1' .
if sy-subrc = 0.
write: ' ', makt-maktx.
endif.
write: ' ',sy-tabix.
endloop.
else.
perform copybom .
endif.
endform. " procdata
*&---------------------------------------------------------------------*
*& Form COPYBOM
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form copybom .
* wam-stufe = 1.
* LOOP AT m_itab."
*
* IF wam-stufe < m_itab-stufe.
* SELECT SINGLE * FROM mast WHERE matnr = wam-matnr AND werks = p_werkst AND stlan = '1' AND stlal = '01' .
* IF sy-subrc <> 0.
* PERFORM bdc_data USING wam-matnr '1' '1' '01' '01' p_werksf p_werkst .
* ENDIF.
* ENDIF.
*
* wam = m_itab .
*
*
* ENDLOOP.
*分成两部分写代码,调试比较清晰
wam-stufe = 1.
loop at tmp_tab."
if wam-stufe < tmp_tab-stufe.
clear wam-stufe.
collect wam into exp_tab.
endif.
wam = tmp_tab .
endloop.
loop at exp_tab.
select single * from mast where matnr = exp_tab-matnr and werks = p_werkst and stlan = '1' and stlal = '01' .
if sy-subrc <> 0.
* PERFORM bdc_data USING exp_tab-matnr '1' '1' '01' '01' p_werksf p_werkst .
*form bdc_data using matnr stlanf stlant stlalf stlalt werksf werkst .
*备注,这个地方改变从1改变到参数目的是 bom用途STLAN是界面上的,结果改成可选bom了-----一定注意
* PERFORM bdc_data USING exp_tab-matnr '1' '1' p_stlant '01' p_werksf p_werkst .
perform bdc_data using exp_tab-matnr '1' p_stlant '01' '01' p_werksf p_werkst .
clear msgs."清空表头
append msgs."增加一个空行 以区分
endif.
endloop.
* IF msgs[] IS INITIAL.
* PERFORM bdc_data USING p_matnr p_stlant p_stlalt p_werksf p_werkst.
* IF flag <> '1'." 防止底层有问题时候顶层创建,否则问题处理完会提示顶层bom存在
* PERFORM bdc_data USING p_matnr p_stlanf p_stlant p_stlalf p_stlalt p_werksf p_werkst .
* if flag <> '1'.
* write / ' 恭喜,创建成功!' .write / .
* endif.
* LOOP AT msg into msgs ."注意,最后一个只给最后提示
* MESSAGE ID msgs-msgid TYPE msgs-msgtyp NUMBER msgs-msgnr
* WITH msgs-msgv1 msgs-msgv2 msgs-msgv3 msgs-msgv4 INTO lmsg.
* WRITE / lmsg.
* APPEND lmsg TO tmsg.
* endloop.
** LOOP AT msg .
** MESSAGE ID msg-msgid TYPE msg-msgtyp NUMBER msg-msgnr
** WITH msg-msgv1 msgs-msgv2 msg-msgv3 msg-msgv4 INTO lmsg.
** WRITE / lmsg.
** APPEND lmsg TO tmsg.
** endloop.
*
* ELSE.
* write / ' 创建中出现问题,请修正!' .write / .
*
* LOOP AT msgs WHERE msgtyp = 'E' .
* MESSAGE ID msgs-msgid TYPE msgs-msgtyp NUMBER msgs-msgnr
* WITH msgs-msgv1 msgs-msgv2 msgs-msgv3 msgs-msgv4 INTO lmsg.
* WRITE / lmsg.
* APPEND lmsg TO tmsg.
* ENDLOOP.
* ENDIF.
if flag <> '1'." 防止底层有问题时候顶层创建,否则问题处理完会提示顶层bom存在
perform bdc_data using p_matnr p_stlanf p_stlant p_stlalf p_stlalt p_werksf p_werkst .
if flag <> '1'.
write / ' 恭喜,创建成功!' .write / .
else.
write / ' 创建顶层bom时出现问题,请修正!' .write / .
endif.
loop at msgs ."注意,给出所有提示。
if msgs is not initial.
message id msgs-msgid type msgs-msgtyp number msgs-msgnr
with msgs-msgv1 msgs-msgv2 msgs-msgv3 msgs-msgv4 into lmsg.
write / lmsg.
append lmsg to tmsg.
else.
write /.
endif.
endloop.
* LOOP AT msg .
* MESSAGE ID msg-msgid TYPE msg-msgtyp NUMBER msg-msgnr
* WITH msg-msgv1 msgs-msgv2 msg-msgv3 msg-msgv4 INTO lmsg.
* WRITE / lmsg.
* APPEND lmsg TO tmsg.
* endloop.
else.
write / ' 创建中出现问题,请修正!' .write / .
loop at msgs ."WHERE msgtyp = 'E' .
if msgs is not initial.
message id msgs-msgid type msgs-msgtyp number msgs-msgnr
with msgs-msgv1 msgs-msgv2 msgs-msgv3 msgs-msgv4 into lmsg.
write / lmsg.
append lmsg to tmsg.
endif.
endloop.
endif.
endform. " COPYBOM
*&---------------------------------------------------------------------*
*& Form bdc_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form bdc_data using matnr stlanf stlant stlalf stlalt werksf werkst .
* PERFORM bdc_dynpro USING 'SAPLCSAL' '100'.
**perform bdc_field using 'BDC_CURSOR' 'RC29N-ZWERK'.
* PERFORM bdc_field USING 'BDC_OKCODE' '/00'.
* PERFORM bdc_field USING 'RC29N-MATNR' matnr ." '100001'.
* PERFORM bdc_field USING 'RC29N-WERKS' werksf." '2101'.
* PERFORM bdc_field USING 'RC29N-STLAN' stlan." '1'.
* PERFORM bdc_field USING 'RC29N-STLAL' stlal." '4'.
* PERFORM bdc_field USING 'RC29N-ZWERK' werkst."'2107'.
* PERFORM bdc_dynpro USING 'SAPLCSAL' '120'.
* PERFORM bdc_field USING 'BDC_OKCODE' '=FCBU'.
* PERFORM bdc_field USING 'RC29K-AUSKZ(01)' 'X'.
data: all type i, count type i value 1,balance type i.
data text(15) ."RC29P-AUSKZ(01)
text = 'RC29P-AUSKZ(01)'.
call function 'CS_BOM_EXPL_MAT_V2'
exporting
ftrel = 'X'
capid = 'PP01'
datuv = sy-datum
* emeng = 1
* mehrs = 'X'
ehndl = '1'
stlan = stlanf
stlal = stlalf
mtnrv = matnr
werks = werksf
tables
stb = itab_s
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
conversion_error = 8
others = 9.
all = lines( itab_s ).
perform bdc_dynpro using 'SAPLCSDI' '0100'.
perform bdc_field using 'BDC_CURSOR'
'RC29N-STLAL'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_field using 'RC29N-MATNR'
matnr . " '100001'.
perform bdc_field using 'RC29N-WERKS'
werkst . "'2109'.
perform bdc_field using 'RC29N-STLAN'
stlant . "'1'.
perform bdc_field using 'RC29N-STLAL'
stlalt . "'6'.
* PERFORM bdc_field USING 'RC29N-DATUV'
* sy-datum."'2009.02.26'.
perform bdc_dynpro using 'SAPLCSDI' '0110'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
*perform bdc_field using 'RC29K-BMENG'
* '1,000'.
perform bdc_field using 'RC29K-STLST'
'1'.
perform bdc_field using 'BDC_CURSOR'
'RC29K-EXSTL'.
perform bdc_field using 'RC29K-EXSTL'
''.
perform bdc_dynpro using 'SAPLCSDI' '0111'.
perform bdc_field using 'BDC_CURSOR'
'RC29K-LABOR'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_dynpro using 'SAPLCSDI' '0140'.
perform bdc_field using 'BDC_CURSOR'
'RC29P-POSNR(01)'.
perform bdc_field using 'BDC_OKCODE'
'=FCEW'.
perform bdc_dynpro using 'SAPLCSDI' '0105'.
perform bdc_field using 'BDC_CURSOR'
'RC29N-STLAL'.
perform bdc_field using 'BDC_OKCODE'
'=CLWI'.
perform bdc_field using 'RC29N-MATNR'
matnr . "|'100001'.
perform bdc_field using 'RC29N-WERKS'
werksf. "'2101'.
perform bdc_field using 'RC29N-STLAN'
stlanf. "'1'.
perform bdc_field using 'RC29N-STLAL'
stlalf. "'2'.
* PERFORM bdc_field USING 'RC29N-DATUV'
* sy-datum."'2009.02.26'.
* PERFORM bdc_dynpro USING 'SAPLCSDI' '0156'.
**perform bdc_field using 'BDC_CURSOR'
** 'RC29P-AUSKZ(02)'.
* PERFORM bdc_field USING 'BDC_OKCODE'
* '=FCUE'.
*
**
** PERFORM bdc_field USING 'RC29P-AUSKZ(01)'
** 'X'.
** PERFORM bdc_field USING 'RC29P-AUSKZ(02)'
** 'X'.
*
*
* LOOP AT itab_s.
* IF sy-tabix < 10 .
* text+13(1) = sy-tabix.
* ELSE.
* text+12(2) = sy-tabix.
* ENDIF.
* PERFORM bdc_field USING text 'X'.
* ENDLOOP.
*
*
*
* PERFORM bdc_dynpro USING 'SAPLCSDI' '0150'.
**perform bdc_field using 'BDC_CURSOR'
** 'RC29P-AUSKZ(02)'.
* PERFORM bdc_field USING 'BDC_OKCODE'
* '=FCBU'.
**perform bdc_field using 'RC29P-AUSKZ(01)'
** 'X'.
**perform bdc_field using 'RC29P-AUSKZ(02)'
** 'X'.
*
* LOOP AT itab_s.
* IF sy-tabix < 10 .
* text+13(1) = sy-tabix.
* ELSE.
* text+12(2) = sy-tabix.
* ENDIF.
* PERFORM bdc_field USING text 'X'.
* ENDLOOP.
*
* CLEAR msg.
**'A' Display screen
**'E' Display only if an error occurs
**'N' Do not display
**'P' Do not display; debugging possible
*data : test type CTU_PARAMS .
*test-DEFSIZE = 'X'.
*test-DISMODE = 'N'.
*test-UPDMODE = 'N'.
* CALL TRANSACTION 'CS01' USING bdcdata OPTIONS FROM test MESSAGES INTO msg.
** CALL TRANSACTION 'CS01' USING bdcdata MODE 'N' MESSAGES INTO msg.
perform bdc_dynpro using 'SAPLCSDI' '0156'.
perform bdc_field using 'BDC_OKCODE'
'=MALL'.
*perform bdc_dynpro using 'SAPLCSDI' '0156'.
*perform bdc_field using 'BDC_OKCODE'
* '=P+'.
perform bdc_dynpro using 'SAPLCSDI' '0156'.
perform bdc_field using 'BDC_OKCODE'
'=FCUE'.
*perform bdc_dynpro using 'SAPLCSDI' '0150'.
**perform bdc_field using 'BDC_CURSOR'
** 'RC29P-AUSKZ(05)'.
*perform bdc_field using 'BDC_OKCODE'
* '=P+'.
*perform bdc_field using 'RC29P-AUSKZ(01)'
* record-auskz_01_013.
*perform bdc_field using 'RC29P-AUSKZ(02)'
* record-auskz_02_014.
*perform bdc_field using 'RC29P-AUSKZ(03)'
* record-auskz_03_015.
*perform bdc_field using 'RC29P-AUSKZ(04)'
* record-auskz_04_016.
*perform bdc_field using 'RC29P-AUSKZ(05)'
* record-auskz_05_017.
perform bdc_dynpro using 'SAPLCSDI' '0150'.
*perform bdc_field using 'BDC_CURSOR'
* 'RC29P-AUSKZ(04)'.
*perform bdc_field using 'BDC_OKCODE'
* '=FCBU'.
*perform bdc_field using 'RC29P-AUSKZ(02)'
* record-auskz_02_018.
*perform bdc_field using 'RC29P-AUSKZ(03)'
* record-auskz_03_019.
*perform bdc_field using 'RC29P-AUSKZ(04)'
* record-auskz_04_020.
*
* do.
* if count < 5.
* balance = count mod 5 .
* elseif count = 5.
* balance = 5.
* else.
* balance = count mod 4 .
* if balance = 0.
* balance = 4.
* endif.
* if balance = 1.
* balance = 5.
* endif.
* endif.
*
* text+13(1) = balance.
* PERFORM bdc_field USING text 'X'.
*
*
* if balance = 5 and count <> all.
* perform bdc_dynpro using 'SAPLCSDI' '0150'.
*
* perform bdc_field using 'BDC_OKCODE'
* '=P+'.
* endif.
* if all = count.
* exit.
* endif.
* count = count + 1.
* enddo.
perform bdc_field using 'BDC_OKCODE'
'=FCBU'.
clear msg.
*'A' Display screen
*'E' Display only if an error occurs
*'N' Do not display
*'P' Do not display; debugging possible
*data : test type ctu_params .
*test-defsize = 'X'.
*test-dismode = 'N'.
*test-updmode = 'N'.
* call transaction 'CS01' using bdcdata options from test messages into msg.
call transaction 'CS01' using bdcdata mode 'N' messages into msg.
if sy-subrc <> 0.
flag = 1.
endif.
clear bdcdata.
refresh bdcdata.
commit work and wait.
* MESSAGE ID '29' TYPE 'E' NUMBER '162' WITH '29' into msgtext.
* append lines of msg to msgs.
* MESSAGE ID '29' TYPE 'E' NUMBER '162' WITH '29' into msgtext.
append lines of msg to msgs.
endform. " PROCESS_DATA
*----------------------------------------------------------------------
*
* Start new screen
*
*----------------------------------------------------------------------
form bdc_dynpro using program dynpro.
clear bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
append bdcdata.
endform. "BDC_DYNPRO
*----------------------------------------------------------------------
*
* Insert field
*
*----------------------------------------------------------------------
*
form bdc_field using fnam fval.
clear bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
append bdcdata.
endform. "BDC_FIELD