前一周 写了文章 思考了SAP 修改记录写入的思考-CSDN博客
紧接着 小家伙 感染了支原体 反反复复 ,代码没有落地,今天终于落地了,下面把代码贴出来。
FUNCTION ZRFC_MM_QUOTA_BAPI.
*"----------------------------------------------------------------------
*"*"本地接口:
*" TABLES
*" XEQUK STRUCTURE EQUKU OPTIONAL
*" XEQUP STRUCTURE UEQUP OPTIONAL
*" YEQUK STRUCTURE EQUKU OPTIONAL
*" YEQUP STRUCTURE UEQUP OPTIONAL
*"----------------------------------------------------------------------
*--------------------------------------------------------------------*
*function:
*changer:
*note:要考虑如下几种情形
* 1、原有数据20230101~20231231,新增 20220101-20221231 / 20240101-20241231 不相干不处理
* 2、原有数据20230101~20231231,新增 20220101-20231231 / 20231201-20241231 一半相干
* 3、原有数据20230101~20231231,新增 20230301-20230531 / 20221201-20241231 全相干
*--------------------------------------------------------------------*
DATA:LDATE TYPE SY-DATUM.
DATA:LQUNUM LIKE EQUK-QUNUM.
DATA:LSEQUKN LIKE EQUK.
DATA:LSEQUPN LIKE EQUP.
DATA:TIMESTAMP LIKE EQUK-CHANGEDON.
DATA:LOBJID LIKE CDHDR-OBJECTID.
GET TIME STAMP FIELD TIMESTAMP.
LOOP AT XEQUK INTO DATA(LSEQUK).
* 原有数据20230101~20231231 新增 20220101-20231031
LOBJID(18) = LSEQUK-MATNR.
LOBJID+40(4) = LSEQUK-WERKS.
IF LSEQUK-CHANGEDON IS INITIAL.
LSEQUK-CHANGEDON = TIMESTAMP.
MODIFY XEQUK FROM LSEQUK.
ENDIF.
SELECT * FROM EQUK INTO TABLE @DATA(LTEQUK)
WHERE EQUK~MATNR = @LSEQUK-MATNR AND EQUK~WERKS = @LSEQUK-WERKS
AND EQUK~BDATU > @LSEQUK-BDATU AND
EQUK~VDATU < @LSEQUK-BDATU AND
EQUK~VDATU > @LSEQUK-VDATU.
LOOP AT LTEQUK INTO DATA(LSEQUKO).
CLEAR LDATE.
PERFORM GETSPECIFICDATE(ZABAP_COMMON_PROGRAM) USING LSEQUK-BDATU 1 0 0 '+' CHANGING LDATE.
UPDATE EQUK SET VDATU = LDATE
WHERE EQUK~MATNR = LSEQUKO-MATNR AND EQUK~WERKS = LSEQUKO-WERKS AND BDATU = LSEQUKO-BDATU.
LSEQUKN = LSEQUKO.
LSEQUKN-VDATU = LDATE.
PERFORM ZQUOTE_WRITE_DOCUMENT USING LSEQUKO LSEQUKN 'U'.
ENDLOOP.
* 原有数据20230101~20231231 新增 20231001-20241231
SELECT * FROM EQUK INTO TABLE @LTEQUK
WHERE EQUK~MATNR = @LSEQUK-MATNR AND EQUK~WERKS = @LSEQUK-WERKS
AND EQUK~BDATU < @LSEQUK-BDATU AND EQUK~VDATU < @LSEQUK-VDATU
AND EQUK~BDATU > @LSEQUK-VDATU.
LOOP AT LTEQUK INTO LSEQUKO.
CLEAR LDATE.
PERFORM GETSPECIFICDATE(ZABAP_COMMON_PROGRAM) USING LSEQUKO-BDATU 1 0 0 '-' CHANGING LDATE.
PERFORM GET_SNRO_NO(ZABAP_COMMON_PROGRAM) USING 'QUOTIERUNG' '01' CHANGING LQUNUM.
SELECT * FROM EQUP WHERE EQUP~QUNUM = @LSEQUKO-QUNUM INTO TABLE @DATA(LTEQUPOSUB).
APPEND LSEQUKO TO YEQUK.
LSEQUKN = LSEQUKO.
LSEQUKN-BDATU = LDATE.
LSEQUKN-QUNUM = LQUNUM.
APPEND LSEQUKN TO XEQUK.
LOOP AT LTEQUPOSUB INTO LSEQUPN.
APPEND LSEQUPN TO YEQUP.
LSEQUPN-QUNUM = LQUNUM.
APPEND LSEQUPN TO XEQUP.
ENDLOOP.
* UPDATE EQUK SET BDATU = LDATE
* WHERE EQUK~MATNR = LSEQUKO-MATNR AND EQUK~WERKS = LSEQUKO-WERKS AND BDATU = LSEQUKO-BDATU.
ENDLOOP.
* 3、原有数据20230101~20231231,新增 20230301-20230531 / 20221201-20241231 全相干
SELECT * FROM EQUK INTO TABLE @LTEQUK
WHERE EQUK~MATNR = @LSEQUK-MATNR AND EQUK~WERKS = @LSEQUK-WERKS
AND EQUK~BDATU >= @LSEQUK-BDATU AND EQUK~VDATU <= @LSEQUK-VDATU.
* 分段处理
LOOP AT LTEQUK INTO LSEQUKO.
CLEAR LDATE.
IF LSEQUKO-BDATU > LSEQUK-BDATU.
* 后半段
PERFORM GETSPECIFICDATE(ZABAP_COMMON_PROGRAM) USING LSEQUK-BDATU 1 0 0 '+' CHANGING LDATE.
UPDATE EQUK SET VDATU = LDATE
WHERE EQUK~MATNR = LSEQUKO-MATNR AND EQUK~WERKS = LSEQUKO-WERKS
AND QUNUM = LSEQUKO-QUNUM AND BDATU = LSEQUKO-BDATU.
LSEQUKN = LSEQUKO.
LSEQUKN-VDATU = LDATE.
PERFORM ZQUOTE_WRITE_DOCUMENT USING LSEQUKO LSEQUKN 'U'.
ENDIF.
* 前半段
IF LSEQUKO-BDATU > LSEQUK-BDATU.
IF LSEQUKO-VDATU < LSEQUK-VDATU.
* 新增一个记录
SELECT * FROM EQUP WHERE QUNUM = @LSEQUKO-QUNUM INTO TABLE @DATA(LTEQUPO).
PERFORM GET_SNRO_NO(ZABAP_COMMON_PROGRAM) USING 'QUOTIERUNG' '01' CHANGING LQUNUM.
MOVE-CORRESPONDING LSEQUKO TO XEQUK.
XEQUK-QUNUM = LQUNUM.
PERFORM GETSPECIFICDATE(ZABAP_COMMON_PROGRAM) USING LSEQUK-VDATU 1 0 0 '-' CHANGING LDATE.
XEQUK-BDATU = LDATE.
XEQUK-KZ = 'I'.
LOOP AT LTEQUPO INTO DATA(LSEQUPO).
MOVE-CORRESPONDING LSEQUPO TO XEQUP.
XEQUP-QUNUM = LQUNUM.
XEQUP-KZ = 'I'.
APPEND XEQUP.
ENDLOOP.
APPEND XEQUK.
ELSEIF LSEQUKO-VDATU = LSEQUK-VDATU.
PERFORM GETSPECIFICDATE(ZABAP_COMMON_PROGRAM) USING LSEQUK-BDATU 1 0 0 '+' CHANGING LDATE.
UPDATE EQUK SET VDATU = LDATE
WHERE EQUK~MATNR = LSEQUKO-MATNR AND EQUK~WERKS = LSEQUKO-WERKS AND BDATU = LSEQUKO-BDATU.
LSEQUKN = LSEQUKO.
LSEQUKN-VDATU = LDATE.
PERFORM ZQUOTE_WRITE_DOCUMENT USING LSEQUKO LSEQUKN 'U'.
ENDIF.
ENDIF.
ENDLOOP.
SELECT * FROM EQUK INTO TABLE @LTEQUK
WHERE EQUK~MATNR = @LSEQUK-MATNR AND EQUK~WERKS = @LSEQUK-WERKS
AND EQUK~BDATU <= @LSEQUK-BDATU AND EQUK~VDATU >= @LSEQUK-VDATU.
* 替换 删除老的
LOOP AT LTEQUK INTO LSEQUKO .
SELECT * INTO TABLE @DATA(LTEQUPD)
FROM EQUP WHERE EQUP~QUNUM = @LSEQUKO-QUNUM.
LOOP AT LTEQUPD INTO DATA(LSEQUPD).
MOVE-CORRESPONDING LSEQUPD TO YEQUP.
APPEND YEQUP .
ENDLOOP.
MOVE-CORRESPONDING LSEQUKO TO YEQUK.
APPEND YEQUK.
ENDLOOP.
ENDLOOP.
CALL FUNCTION 'ME_UPDATE_QUOTA'
TABLES
XEQUK = XEQUK
XEQUP = XEQUP
YEQUK = YEQUK
YEQUP = YEQUP.
PERFORM ZQUOTE_WRITE_DOCU_EQUK TABLES XEQUK YEQUK USING LOBJID.
PERFORM ZQUOTE_WRITE_DOCU_EQUP TABLES XEQUP YEQUP USING LOBJID.
ENDFUNCTION.
FORM ZQUOTE_WRITE_DOCU_EQUP TABLES XTEQUP TYPE STANDARD TABLE YTEQUP TYPE STANDARD TABLE
USING LOBJID TYPE CDHDR-OBJECTID.
CALL FUNCTION 'CHANGEDOCUMENT_OPEN'
EXPORTING
OBJECTCLASS = 'QUOTEN2'
OBJECTID = LOBJID
* PLANNED_CHANGE_NUMBER = PLANNED_CHANGE_NUMBER
* PLANNED_OR_REAL_CHANGES = PLANNED_OR_REAL_CHANGES
EXCEPTIONS
SEQUENCE_INVALID = 1
OTHERS = 2.
CASE SY-SUBRC.
WHEN 1.
MESSAGE A001(F2) WITH 'SEQUENCE INVALID'.
WHEN 2.
MESSAGE A001(F2) WITH 'OPEN ERROR'.
ENDCASE.
CALL FUNCTION 'CHANGEDOCUMENT_MULTIPLE_CASE'
EXPORTING
TABLENAME = 'EQUP '
CHANGE_INDICATOR = 'U'
DOCU_DELETE = 'X'
TABLES
TABLE_OLD = YTEQUP
TABLE_NEW = XTEQUP
EXCEPTIONS
NAMETAB_ERROR = 1
OPEN_MISSING = 2
POSITION_INSERT_FAILED = 3
OTHERS = 4.
CASE SY-SUBRC.
WHEN 1.
MESSAGE A001(F2) WITH 'NAMETAB-ERROR'.
WHEN 2.
MESSAGE A001(F2) WITH 'OPEN MISSING'.
WHEN 3.
MESSAGE A001(F2) WITH 'INSERT ERROR'.
WHEN 4.
MESSAGE A001(F2) WITH 'SINGLE ERROR'.
ENDCASE.
CALL FUNCTION 'CHANGEDOCUMENT_CLOSE'
EXPORTING
OBJECTCLASS = 'QUOTEN2'
OBJECTID = LOBJID
DATE_OF_CHANGE = SY-DATUM
TIME_OF_CHANGE = SY-UZEIT
TCODE = 'ZRFC_MM_QUOTA_BAPI' "TCODE'
USERNAME = SY-UNAME
OBJECT_CHANGE_INDICATOR = 'U' "OBJECT_CHANGE_INDICATOR'
* NO_CHANGE_POINTERS = NO_CHANGE_POINTERS
EXCEPTIONS
HEADER_INSERT_FAILED = 1
OBJECT_INVALID = 2
OPEN_MISSING = 3
NO_POSITION_INSERTED = 4
OTHERS = 5.
CASE SY-SUBRC.
WHEN 1.
MESSAGE A001(F2) WITH 'INSERT HEADER FAILED'.
WHEN 2.
MESSAGE A001(F2) WITH 'OBJECT INVALID'.
WHEN 3.
MESSAGE A001(F2) WITH 'OPEN MISSING'.
WHEN 5.
MESSAGE A001(F2) WITH 'CLOSE ERROR'.
ENDCASE.
ENDFORM.
FORM ZQUOTE_WRITE_DOCU_EQUK TABLES XTEQUK TYPE STANDARD TABLE YTEQUK TYPE STANDARD TABLE
USING LOBJID TYPE CDHDR-OBJECTID.
CALL FUNCTION 'CHANGEDOCUMENT_OPEN'
EXPORTING
OBJECTCLASS = 'QUOTEN2'
OBJECTID = LOBJID
* PLANNED_CHANGE_NUMBER = PLANNED_CHANGE_NUMBER
* PLANNED_OR_REAL_CHANGES = PLANNED_OR_REAL_CHANGES
EXCEPTIONS
SEQUENCE_INVALID = 1
OTHERS = 2.
CASE SY-SUBRC.
WHEN 1.
MESSAGE A001(F2) WITH 'SEQUENCE INVALID'.
WHEN 2.
MESSAGE A001(F2) WITH 'OPEN ERROR'.
ENDCASE.
CALL FUNCTION 'CHANGEDOCUMENT_MULTIPLE_CASE'
EXPORTING
TABLENAME = 'EQUK '
CHANGE_INDICATOR = 'U'
DOCU_DELETE = 'X'
TABLES
TABLE_OLD = YTEQUK
TABLE_NEW = XTEQUK
EXCEPTIONS
NAMETAB_ERROR = 1
OPEN_MISSING = 2
POSITION_INSERT_FAILED = 3
OTHERS = 4.
CASE SY-SUBRC.
WHEN 1.
MESSAGE A001(F2) WITH 'NAMETAB-ERROR'.
WHEN 2.
MESSAGE A001(F2) WITH 'OPEN MISSING'.
WHEN 3.
MESSAGE A001(F2) WITH 'INSERT ERROR'.
WHEN 4.
MESSAGE A001(F2) WITH 'SINGLE ERROR'.
ENDCASE.
CALL FUNCTION 'CHANGEDOCUMENT_CLOSE'
EXPORTING
OBJECTCLASS = 'QUOTEN2'
OBJECTID = LOBJID
DATE_OF_CHANGE = SY-DATUM
TIME_OF_CHANGE = SY-UZEIT
TCODE = 'ZRFC_MM_QUOTA_BAPI' "TCODE'
USERNAME = SY-UNAME
OBJECT_CHANGE_INDICATOR = 'U' "OBJECT_CHANGE_INDICATOR'
* NO_CHANGE_POINTERS = NO_CHANGE_POINTERS
EXCEPTIONS
HEADER_INSERT_FAILED = 1
OBJECT_INVALID = 2
OPEN_MISSING = 3
NO_POSITION_INSERTED = 4
OTHERS = 5.
CASE SY-SUBRC.
WHEN 1.
MESSAGE A001(F2) WITH 'INSERT HEADER FAILED'.
WHEN 2.
MESSAGE A001(F2) WITH 'OBJECT INVALID'.
WHEN 3.
MESSAGE A001(F2) WITH 'OPEN MISSING'.
WHEN 5.
MESSAGE A001(F2) WITH 'CLOSE ERROR'.
ENDCASE.
ENDFORM.
FORM ZQUOTE_WRITE_DOCUMENT USING LSEQUKO LIKE EQUK LSEQUKN LIKE EQUK PUDI TYPE C.
DATA:LOBJID LIKE CDHDR-OBJECTID.
LOBJID(18) = LSEQUKO-MATNR.
LOBJID+40(4) = LSEQUKO-WERKS.
CALL FUNCTION 'CHANGEDOCUMENT_OPEN'
EXPORTING
OBJECTCLASS = 'QUOTEN2'
OBJECTID = LOBJID
* PLANNED_CHANGE_NUMBER = PLANNED_CHANGE_NUMBER
* PLANNED_OR_REAL_CHANGES = PLANNED_OR_REAL_CHANGES
EXCEPTIONS
SEQUENCE_INVALID = 1
OTHERS = 2.
CASE SY-SUBRC.
WHEN 1.
MESSAGE A001(F2) WITH 'SEQUENCE INVALID'.
WHEN 2.
MESSAGE A001(F2) WITH 'OPEN ERROR'.
ENDCASE.
CALL FUNCTION 'CHANGEDOCUMENT_SINGLE_CASE'
EXPORTING
TABLENAME = 'EQUK '
WORKAREA_OLD = LSEQUKO
WORKAREA_NEW = LSEQUKN
CHANGE_INDICATOR = 'U'
DOCU_DELETE = 'X'
EXCEPTIONS
NAMETAB_ERROR = 1
OPEN_MISSING = 2
POSITION_INSERT_FAILED = 3
OTHERS = 4.
CASE SY-SUBRC.
WHEN 1.
MESSAGE A001(F2) WITH 'NAMETAB-ERROR'.
WHEN 2.
MESSAGE A001(F2) WITH 'OPEN MISSING'.
WHEN 3.
MESSAGE A001(F2) WITH 'INSERT ERROR'.
WHEN 4.
MESSAGE A001(F2) WITH 'SINGLE ERROR'.
ENDCASE.
CALL FUNCTION 'CHANGEDOCUMENT_CLOSE'
EXPORTING
OBJECTCLASS = 'QUOTEN2'
OBJECTID = LOBJID
DATE_OF_CHANGE = SY-DATUM
TIME_OF_CHANGE = SY-UZEIT
TCODE = 'ZRFC_MM_QUOTA_BAPI' "TCODE'
USERNAME = SY-UNAME
OBJECT_CHANGE_INDICATOR = 'U' "OBJECT_CHANGE_INDICATOR'
* NO_CHANGE_POINTERS = NO_CHANGE_POINTERS
EXCEPTIONS
HEADER_INSERT_FAILED = 1
OBJECT_INVALID = 2
OPEN_MISSING = 3
NO_POSITION_INSERTED = 4
OTHERS = 5.
CASE SY-SUBRC.
WHEN 1.
MESSAGE A001(F2) WITH 'INSERT HEADER FAILED'.
WHEN 2.
MESSAGE A001(F2) WITH 'OBJECT INVALID'.
WHEN 3.
MESSAGE A001(F2) WITH 'OPEN MISSING'.
WHEN 5.
MESSAGE A001(F2) WITH 'CLOSE ERROR'.
ENDCASE.
ENDFORM.