SAP 增加委外采购订单BOM变更记录
增强点:ME_PROCESS_PO_CUST
IF_EX_ME_PROCESS_PO_CUST~POST
DATA:ls_item TYPE mepoitem,
ls_schedules TYPE meposchedule.
DATA:items TYPE purchase_order_items,
schedules TYPE purchase_order_schedules.
DATA:item_obj TYPE purchase_order_item,
schedules_obj TYPE purchase_order_schedule.
ls_poheader = im_header->get_data( ).
items = im_header->get_items( ).
***ZCPO–委外采购订单组件更改记录表
MANDT MANDT CLNT 3 0 0 集团
EBELN EBELN CHAR 10 0 0 采购凭证编号
EBELP EBELP NUMC 5 0 0 采购凭证的项目编号
ETENR EETEN NUMC 4 0 0 交货计划行计数器
ZITEM ZITEM NUMC 6 0 0 序号
RSNUM RSNUM NUMC 10 0 0 预留/相关需求的编号
RSPOS RSPOS NUMC 4 0 0 预留 / 相关需求的项目编号
IDNRK IDNRK CHAR 40 0 0 BOM 组件
STYPE FIELDNAME CHAR 30 0 0 字段名
RANEW CHAR 30 0 0 新值
RAOLD CHAR 30 0 0 旧值
CHNGIND CDCHNGIND CHAR 1 0 0 更改类型
EDATE CDDATUM DATS 8 0 0 创建修改文档的数据
AEZEI CDUZEIT TIMS 6 0 0 时间已更改
AENAM AENAM CHAR 12 0 0 更改对象用户的名称
ENAME CDUSERNAME CHAR 12 0 0 修改文档中的个人负责的用户名
BANCI NUMC 4 0 0 更改版本次数
TCODE CDTCODE CHAR 20 0 0 已在其中进行更改的事务
DATA:lt_bom TYPE mmpur_t_mdpm.
DATA:ls_bom TYPE mdpm.
DATA:winuser TYPE string.
DATA:lt_resb TYPE TABLE OF resb.
DATA:ls_resb TYPE resb.
DATA:lt_resb2 TYPE TABLE OF resb.
DATA:ls_resb2 TYPE resb.
DATA: fieldname(30) VALUE '(SAPLMEPO)TRTYP'.
DATA: id TYPE i.
DATA: row TYPE i.
DATA: wa_zcpo TYPE zcpo.
DATA: trtyp. " H 创建 V 修改 A 显示
DATA: lv_tcode TYPE sy-tcode.
FIELD-SYMBOLS: TYPE any.
********************user_name
CALL METHOD cl_gui_frontend_services=>get_user_name
CHANGING
user_name = winuser
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
cntl_system_error = 1
cntl_error = 2
OTHERS = 3.
********************user_name
IF sy-tcode = ‘ME21N’ OR sy-tcode = ‘ME22N’ OR sy-tcode = ‘ME23N’.
ASSIGN (fieldname) TO .
trtyp = .
CASE trtyp.
WHEN ‘H’.
lv_tcode = ‘ME21N’.
WHEN ‘V’.
lv_tcode = ‘ME22N’.
WHEN ‘A’.
lv_tcode = ‘ME23N’.
WHEN OTHERS.
lv_tcode = sy-tcode.
ENDCASE.
ELSE.
lv_tcode = sy-tcode.
ENDIF.
LOOP AT items INTO item_obj.
CLEAR ls_item.
ls_item = item_obj-item->get_data( ).
IF ls_item-pstyp = '3'. " 委外采购订单
REFRESH:lt_eket.
SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_eket
FROM eket WHERE ebeln = ls_item-ebeln
AND ebelp = ls_item-ebelp.
CLEAR:schedules.
schedules = item_obj-item->get_schedules( ).
LOOP AT schedules INTO schedules_obj.
CLEAR:ls_schedules,lt_bom,lt_resb.
ls_schedules = schedules_obj-schedule->get_data( ). "计划行
CALL METHOD schedules_obj-schedule->get_bom
IMPORTING
et_bom = lt_bom. "计划行对应预留bom
IF lt_bom IS NOT INITIAL.
id = 1. " 先计算好修改版本
row = 0.
SELECT * INTO wa_zcpo FROM zcpo WHERE ebeln = ls_schedules-ebeln AND ebelp = ls_schedules-ebelp AND etenr = ls_schedules-etenr ORDER BY banci DESCENDING.
id = wa_zcpo-banci + 1.
EXIT.
ENDSELECT.
SELECT * INTO wa_zcpo FROM zcpo WHERE ebeln = ls_schedules-ebeln AND ebelp = ls_schedules-ebelp AND etenr = ls_schedules-etenr ORDER BY zitem DESCENDING.
row = wa_zcpo-zitem.
EXIT.
ENDSELECT.
CLEAR wa_zcpo.
wa_zcpo-mandt = sy-mandt.
wa_zcpo-ebeln = ls_schedules-ebeln.
wa_zcpo-ebelp = ls_schedules-ebelp.
wa_zcpo-etenr = ls_schedules-etenr.
wa_zcpo-edate = sy-datum.
wa_zcpo-aezei = sy-uzeit.
wa_zcpo-aenam = sy-uname.
wa_zcpo-ename = winuser.
wa_zcpo-banci = id.
wa_zcpo-tcode = lv_tcode.
SELECT * INTO TABLE lt_resb FROM resb WHERE ebeln = ls_schedules-ebeln
AND ebelp = ls_schedules-ebelp
AND ebele = ls_schedules-etenr.
IF lt_bom IS INITIAL AND lt_resb IS INITIAL.
CONTINUE.
ENDIF.
"1先循环新数据lt_bom 与 旧数据对比lt_resb
LOOP AT lt_bom INTO ls_bom.
READ TABLE lt_resb INTO ls_resb WITH KEY rsnum = ls_bom-rsnum
rspos = ls_bom-rspos.
IF sy-subrc = 0.
wa_zcpo-rsnum = ls_bom-rsnum.
wa_zcpo-rspos = ls_bom-rspos.
wa_zcpo-idnrk = ls_bom-matnr.
IF ls_bom-kzear <> ls_resb-kzear.
wa_zcpo-stype = '最后发货标识'.
wa_zcpo-ranew = ls_bom-matnr.
wa_zcpo-raold = ls_resb-matnr.
wa_zcpo-chngind = 'U'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
ENDIF.
IF ls_bom-matnr <> ls_resb-matnr.
wa_zcpo-stype = '组件编号'.
wa_zcpo-ranew = ls_bom-matnr.
wa_zcpo-raold = ls_resb-matnr.
wa_zcpo-chngind = 'U'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
ENDIF.
IF ls_bom-erfmg <> ls_resb-erfmg.
wa_zcpo-stype = '需求数量'.
wa_zcpo-ranew = ls_bom-erfmg.
wa_zcpo-raold = ls_resb-erfmg.
wa_zcpo-chngind = 'U'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
ENDIF.
IF ls_bom-erfme <> ls_resb-erfme.
wa_zcpo-stype = '单位'.
wa_zcpo-ranew = ls_bom-erfme.
wa_zcpo-raold = ls_resb-erfme.
wa_zcpo-chngind = 'U'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
ENDIF.
IF ls_bom-fmeng <> ls_resb-fmeng.
wa_zcpo-stype = '数量固定'.
wa_zcpo-ranew = ls_bom-fmeng.
wa_zcpo-raold = ls_resb-fmeng.
wa_zcpo-chngind = 'U'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
ENDIF.
IF ls_bom-werks <> ls_resb-werks.
wa_zcpo-stype = '工厂'.
wa_zcpo-ranew = ls_bom-werks.
wa_zcpo-raold = ls_resb-werks.
wa_zcpo-chngind = 'U'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
ENDIF.
IF ls_bom-bdter <> ls_resb-bdter.
wa_zcpo-stype = '需求日期'.
wa_zcpo-ranew = ls_bom-bdter.
wa_zcpo-raold = ls_resb-bdter.
wa_zcpo-chngind = 'U'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
ENDIF.
IF ls_bom-posnr <> ls_resb-posnr.
wa_zcpo-stype = '项目'.
wa_zcpo-ranew = ls_bom-posnr.
wa_zcpo-raold = ls_resb-posnr.
wa_zcpo-chngind = 'U'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
ENDIF.
IF ls_bom-shkzg <> ls_resb-shkzg.
wa_zcpo-stype = '借贷标识'.
wa_zcpo-ranew = ls_bom-shkzg.
wa_zcpo-raold = ls_resb-shkzg.
wa_zcpo-chngind = 'U'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
ENDIF.
DELETE lt_resb WHERE rsnum = ls_bom-rsnum AND rspos = ls_bom-rspos.
ELSE.
wa_zcpo-rsnum = ls_bom-rsnum.
wa_zcpo-rspos = ls_bom-rspos.
wa_zcpo-idnrk = ls_bom-matnr.
wa_zcpo-stype = '组件编号'.
wa_zcpo-ranew = ls_bom-matnr.
wa_zcpo-chngind = 'I'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '需求数量'.
wa_zcpo-ranew = ls_bom-erfmg.
wa_zcpo-chngind = 'I'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '单位'.
wa_zcpo-ranew = ls_bom-erfme.
wa_zcpo-chngind = 'I'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '工厂'.
wa_zcpo-ranew = ls_bom-werks.
wa_zcpo-chngind = 'I'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '需求日期'.
wa_zcpo-ranew = ls_bom-bdter.
wa_zcpo-chngind = 'I'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '项目'.
wa_zcpo-ranew = ls_bom-posnr.
wa_zcpo-chngind = 'I'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '借贷标识'.
wa_zcpo-ranew = ls_bom-shkzg.
wa_zcpo-chngind = 'I'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
ENDIF.
ENDLOOP.
"2再循环旧数据lt_resb剩下行.
LOOP AT lt_resb INTO ls_resb.
wa_zcpo-rsnum = ls_resb-rsnum.
wa_zcpo-rspos = ls_resb-rspos.
wa_zcpo-idnrk = ls_resb-matnr.
wa_zcpo-stype = '组件编号'.
wa_zcpo-ranew = ls_resb-matnr.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '需求数量'.
wa_zcpo-ranew = ls_resb-erfmg.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '单位'.
wa_zcpo-ranew = ls_resb-erfme.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '工厂'.
wa_zcpo-ranew = ls_resb-werks.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '需求日期'.
wa_zcpo-ranew = ls_resb-bdter.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '项目'.
wa_zcpo-ranew = ls_resb-posnr.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '借贷标识'.
wa_zcpo-ranew = ls_resb-shkzg.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
ENDLOOP.
ENDIF.
READ TABLE lt_eket INTO ls_eket WITH KEY ebeln = ls_schedules-ebeln
ebelp = ls_schedules-ebelp
etenr = ls_schedules-etenr.
IF sy-subrc = 0.
DELETE lt_eket WHERE ebeln = ls_schedules-ebeln
AND ebelp = ls_schedules-ebelp
AND etenr = ls_schedules-etenr.
ENDIF.
ENDLOOP.
"删除的计划行
IF lt_eket IS NOT INITIAL.
REFRESH:lt_resb,lt_resb2.
SELECT * INTO TABLE lt_resb
FROM resb
FOR ALL ENTRIES IN lt_eket
WHERE ebeln = lt_eket-ebeln
AND ebelp = lt_eket-ebelp
AND ebele = lt_eket-etenr.
IF sy-subrc = 0.
APPEND LINES OF lt_resb TO lt_resb2.
SORT lt_resb2 BY ebeln ebelp ebele.
DELETE ADJACENT DUPLICATES FROM lt_resb2 COMPARING ebeln ebelp ebele.
LOOP AT lt_resb2 INTO ls_resb2.
id = 1. " 先计算好修改版本
row = 0.
SELECT * INTO wa_zcpo FROM zcpo WHERE ebeln = ls_resb2-ebeln AND ebelp = ls_resb2-ebelp AND etenr = ls_resb2-ebele ORDER BY banci DESCENDING.
id = wa_zcpo-banci + 1.
EXIT.
ENDSELECT.
SELECT * INTO wa_zcpo FROM zcpo WHERE ebeln = ls_resb2-ebeln AND ebelp = ls_resb2-ebelp AND etenr = ls_resb2-ebele ORDER BY zitem DESCENDING.
row = wa_zcpo-zitem.
EXIT.
ENDSELECT.
CLEAR wa_zcpo.
wa_zcpo-mandt = sy-mandt.
wa_zcpo-ebeln = ls_resb2-ebeln.
wa_zcpo-ebelp = ls_resb2-ebelp.
wa_zcpo-etenr = ls_resb2-ebele.
wa_zcpo-edate = sy-datum.
wa_zcpo-aezei = sy-uzeit.
wa_zcpo-aenam = sy-uname.
wa_zcpo-ename = winuser.
wa_zcpo-banci = id.
wa_zcpo-tcode = lv_tcode.
LOOP AT lt_resb INTO ls_resb WHERE ebeln = ls_resb2-ebeln
AND ebelp = ls_resb2-ebelp
AND ebele = ls_resb2-ebele.
wa_zcpo-rsnum = ls_resb-rsnum.
wa_zcpo-rspos = ls_resb-rspos.
wa_zcpo-idnrk = ls_resb-matnr.
wa_zcpo-stype = '组件编号'.
wa_zcpo-ranew = ls_resb-matnr.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '需求数量'.
wa_zcpo-ranew = ls_resb-erfmg.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '单位'.
wa_zcpo-ranew = ls_resb-erfme.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '工厂'.
wa_zcpo-ranew = ls_resb-werks.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '需求日期'.
wa_zcpo-ranew = ls_resb-bdter.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '项目'.
wa_zcpo-ranew = ls_resb-posnr.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
wa_zcpo-stype = '借贷标识'.
wa_zcpo-ranew = ls_resb-shkzg.
wa_zcpo-chngind = 'D'.
row = row + 1.
wa_zcpo-zitem = row.
INSERT INTO zcpo VALUES wa_zcpo.
ENDLOOP.
ENDLOOP.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.