关于生产订单修改,相信大家网上一搜,一大堆,最后发现,差不多都是一样,我们公司也是自2020年3月10日,直接引用网络一字不改,使用了两年多,才发现有一些记录不正确,为什么没有开发人员或业务顾问仔细研究一下,明显的错误都没有发现吗?无语。。。。。。。
言归正传,首先用SMOD,打开(或新增)增强,
找组件
然后,写代码(以下代码引用网络稍加调整,在S4 Hana SAP上验证,红色部分已经取消或替代替原网上代码):
*&---------------------------------------------------------------------*
*& 包含 ZXCO1U01
*&---------------------------------------------------------------------*
TABLES:caufv,resb.
DATA: BEGIN OF old_resb OCCURS 0, "更改后未保存之RESB.
aufnr LIKE resb-aufnr,
posnr LIKE resb-posnr,
matnr LIKE resb-matnr,
rsnum LIKE resb-rsnum,
rspos LIKE resb-rspos,
xloek LIKE resb-xloek,
bdmng LIKE resb-bdmng,
objnr LIKE resb-objnr,
END OF old_resb.
DATA: BEGIN OF new_resb OCCURS 0. "数据库中的RESB.
INCLUDE STRUCTURE old_resb.
DATA: END OF new_resb.
DATA: BEGIN OF obj_resb OCCURS 0. "插入的RESB
INCLUDE STRUCTURE old_resb.
DATA: END OF obj_resb.
DATA: i_zpco02 LIKE zpco002 OCCURS 0 WITH HEADER LINE.
DATA: i_temp LIKE zpco002 OCCURS 0 WITH HEADER LINE.
DATA: iporg LIKE msxxlist-hostadr,
ipdec(16) TYPE c,
host(18) TYPE c.
DATA:char1(20) TYPE c,
char2(20) TYPE c.
DATA: inst_flg TYPE c VALUE 'N',
chan_had TYPE c VALUE 'N'.
' CHECK sy-tcode EQ 'CO02'.''取消。怕其他事务码也可能引起生产订单变更CO40、CO41等
** Get user IP,hostname
CALL FUNCTION 'TH_USER_INFO' " Get user IP,hostname
EXPORTING
client = sy-mandt
user = sy-uname
IMPORTING
hostaddr = iporg
terminal = host
EXCEPTIONS
OTHERS = 1.
**"Conv.IP addr to format "xxx.xxx.xxx.xxx"
CALL FUNCTION 'GWY_IPADR2STRING' "Conv.IP addr
EXPORTING
ipadr = iporg
IMPORTING
string = ipdec.
***Common var.
MOVE: sy-mandt TO i_temp-mandt,
sy-uname TO i_temp-aenam,
sy-datum TO i_temp-laeda,
sy-uzeit TO i_temp-times,
sy-tcode TO i_temp-tcode,
ipdec TO i_temp-hostip,
host TO i_temp-host,
header_table-aufnr TO i_temp-aufnr.
SELECT SINGLE gamng gstrp INTO (caufv-gamng, caufv-gstrp)
FROM caufv WHERE aufnr EQ header_table-aufnr.
IF header_table-gamng NE caufv-gamng.
MOVE: caufv-gamng TO char1, header_table-gamng TO char2.
MOVE-CORRESPONDING i_temp TO i_zpco02.
MOVE: '更改工单总数' TO i_zpco02-filed,
'U' TO i_zpco02-chnid,
char1 TO i_zpco02-value_old,
char2 TO i_zpco02-value_new.
APPEND i_zpco02.
inst_flg = 'Y'.
' chan_had = 'Y'.
ENDIF.
IF header_table-gstrp NE caufv-gstrp.
MOVE-CORRESPONDING i_temp TO i_zpco02.
MOVE: '更改开工日期' TO i_zpco02-filed,
'U' TO i_zpco02-chnid,
caufv-gstrp TO i_zpco02-value_old,
header_table-gstrp TO i_zpco02-value_new.
APPEND i_zpco02.
inst_flg = 'Y'.
ENDIF.
IF inst_flg = 'Y'.
INSERT zpco002 FROM TABLE i_zpco02 ACCEPTING DUPLICATE KEYS.
inst_flg = 'N'.
ENDIF.
LOOP AT component_table WHERE vbkz EQ 'U'
OR vbkz EQ 'I'
OR vbkz EQ 'D'.
CASE component_table-vbkz.
WHEN 'I'.
MOVE-CORRESPONDING i_temp TO i_zpco02.
MOVE: '新增组件' TO i_zpco02-filed,
'I' TO i_zpco02-chnid,
'' TO i_zpco02-value_old, "old_resb-matnr
component_table-matnr TO i_zpco02-value_new,
'新增的组件' TO i_zpco02-matnr,
component_table-alpos TO i_zpco02-alpos,
sy-tabix TO i_zpco02-loopid.
APPEND i_zpco02.
inst_flg = 'Y'.
WHEN 'D'.
MOVE-CORRESPONDING i_temp TO i_zpco02.
MOVE: '删除组件' TO i_zpco02-filed,
'D' TO i_zpco02-chnid,
resb-xloek TO i_zpco02-value_old,
component_table-xloek TO i_zpco02-value_new,
component_table-matnr TO i_zpco02-matnr,
component_table-alpos TO i_zpco02-alpos,
sy-tabix TO i_zpco02-loopid.
APPEND i_zpco02.
inst_flg = 'Y'.
WHEN 'U'.
SELECT SINGLE matnr bdmng INTO (resb-matnr, resb-bdmng)
FROM resb
WHERE
'aufnr EQ component_table-aufnr
'AND posnr EQ component_table-posnr
'AND matnr EQ component_table-matnr
'AND ''RESB 表的主键就是预留单号、行号,没有必要这么多查询条件,条件多了有可能查不出来想要的值。
rsnum EQ component_table-rsnum
AND rspos EQ component_table-rspos.
IF component_table-matnr NE resb-matnr.
MOVE-CORRESPONDING i_temp TO i_zpco02.
MOVE: '更改组件' TO i_zpco02-filed,
'U' TO i_zpco02-chnid,
resb-matnr TO i_zpco02-value_old,
component_table-matnr TO i_zpco02-value_new,
'更改的工单组件' TO i_zpco02-matnr,
component_table-alpos TO i_zpco02-alpos,
sy-tabix TO i_zpco02-loopid.
APPEND i_zpco02.
'*当组件变更,数量也变更时 "新加逻辑 可能当初没有考虑到
IF component_table-bdmng NE resb-bdmng.
MOVE: component_table-bdmng TO char1, resb-bdmng TO char2.
MOVE-CORRESPONDING i_temp TO t_zpco02.
MOVE: '需求数量' TO t_zpco02-filed,
'U' TO t_zpco02-chnid,
char2 TO t_zpco02-value_old,
char1 TO t_zpco02-value_new,
component_table-matnr TO t_zpco02-matnr,
component_table-alpos TO t_zpco02-alpos,
sy-tabix TO t_zpco02-loopid.
APPEND t_zpco02.
ENDIF.
inst_flg = 'Y'.
ELSEIF component_table-bdmng NE resb-bdmng.
CHECK chan_had NE 'Y'.
MOVE: component_table-bdmng TO char1, resb-bdmng TO char2.
MOVE-CORRESPONDING i_temp TO i_zpco02.
MOVE: '需求数量' TO i_zpco02-filed,
'U' TO i_zpco02-chnid,
char2 TO i_zpco02-value_old,
char1 TO i_zpco02-value_new,
component_table-matnr TO i_zpco02-matnr,
component_table-alpos TO i_zpco02-alpos,
sy-tabix TO i_zpco02-loopid.
APPEND i_zpco02.
inst_flg = 'Y'.
ENDIF.
ENDCASE.
ENDLOOP.
IF inst_flg = 'Y'.
INSERT zpco002 FROM TABLE i_zpco02 ACCEPTING DUPLICATE KEYS.
IF sy-subrc EQ 0.
inst_flg = 'N'.
"chan_had = 'N'.
'* MESSAGE ID "ZX" TYPE "W"
'* NUMBER "000" WITH "SAP系统已记录您的更改!".
ENDIF.
ENDIF.
最后就是关于表的定义:时间也设定为主键,怕同一天更改多次。