在处理BOM数据的过程中,写了一个校验对比的程序,与大家共享。
注:校验的双方必须有一方的数据是正确的,这里程序是以标准BOM为准,校验生产订单的BOM中的错误数据。
下面是CODE:
TABLES: RESB,AUFK,AFKO,MARA.
TYPES: BEGIN OF TY_ALV,
AUFNR TYPE RESB-AUFNR,
MATNR TYPE MARA-MATNR,
RSPOS TYPE RESB-RSPOS,
MATNR_BOM TYPE RESB-MATNR,
WERKS TYPE RESB-WERKS,
BAUGR TYPE RESB-BAUGR,
POSTP TYPE RESB-POSTP,
SORTF TYPE RESB-SORTF,
DUMPS TYPE RESB-DUMPS,
* LMENG TYPE RESB-LMENG,
BDMNG TYPE RESB-BDMNG,
ENMNG TYPE RESB-ENMNG,
BWART TYPE RESB-BWART,
LGORT TYPE RESB-LGORT,
AUFLD TYPE AFKO-AUFLD,
GAMNG TYPE AFKO-GAMNG,
STLNR TYPE RESB-STLNR,
RGEKZ TYPE RESB-RGEKZ,
STLKN TYPE RESB-STLKN,
LIGHT TYPE ICON_D,
MES TYPE STRING,
SLBOX TYPE C,
END OF TY_ALV.
DATA: GT_ALV TYPE TABLE OF TY_ALV,
GS_ALV TYPE TY_ALV.
DATA: GT_DATA TYPE TABLE OF TY_ALV,
GS_DATA TYPE TY_ALV.
DATA: GS_LAYO TYPE LVC_S_LAYO.
DATA: GS_FCAT TYPE LVC_S_FCAT.
DATA: GT_FCAT TYPE LVC_T_FCAT.
DATA: GV_COL TYPE I.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS: S_ERDAT FOR AUFK-ERDAT. "创建日期
SELECT-OPTIONS: S_AUFNR FOR AUFK-AUFNR NO INTERVALS. "生产订单
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR NO INTERVALS. "物料编码
SELECT-OPTIONS: S_WERKS FOR RESB-WERKS NO INTERVALS. "工厂
SELECTION-SCREEN END OF BLOCK B1 .
INITIALIZATION.
START-OF-SELECTION.
PERFORM FRM_SELECT_DATA.
PERFORM FRM_DEAL_DATA.
END-OF-SELECTION.
PERFORM FRM_DISPALY_ALV.
*&---------------------------------------------------------------------*
*& Form FRM_SELECT_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_SELECT_DATA .
"获取生产订单下BOM的数据
SELECT
RESB~AUFNR
AFKO~STLBEZ AS MATNR
RESB~RSPOS
RESB~MATNR AS MATNR_BOM
RESB~WERKS
RESB~BAUGR
RESB~POSTP
RESB~SORTF
RESB~DUMPS
* resb~lmeng
RESB~BDMNG
RESB~ENMNG
RESB~BWART
RESB~LGORT
AFKO~AUFLD
AFKO~GAMNG
RESB~STLNR
RESB~RGEKZ
RESB~STLKN
INTO CORRESPONDING FIELDS OF TABLE GT_DATA
FROM RESB
INNER JOIN AUFK ON RESB~AUFNR EQ AUFK~AUFNR
INNER JOIN AFKO ON AUFK~AUFNR EQ AFKO~AUFNR
WHERE AUFK~ERDAT IN S_ERDAT AND
AUFK~AUFNR IN S_AUFNR AND
AFKO~STLBEZ IN S_MATNR AND
RESB~WERKS IN S_WERKS .
SORT GT_DATA BY AUFNR MATNR STLNR STLKN.
* DATA: LT_STB TYPE TABLE OF STPOX.
* CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'
* EXPORTING
* CAPID = 'PP01'
* DATUV = SY-DATUM
* EMENG = 1
* MEHRS = 'X'
* MTNRV = 'BT3A-0A2-01-B1-Q '
* WERKS = '6002'
* TABLES
* STB = LT_STB
** MATCAT =
* 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.
* IF SY-SUBRC <> 0.
** Implement suitable error handling here
* ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CREATE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_CHECK .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPALY_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_DISPALY_ALV .
PERFORM FRM_SET_LAYOUT.
PERFORM FRM_BUILD_FCAT.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
* I_CALLBACK_PF_STATUS_SET = 'FRM_STATUS_SET'
* I_CALLBACK_USER_COMMAND = 'FRM_USER_COMMAND'
IS_LAYOUT_LVC = GS_LAYO
IT_FIELDCAT_LVC = GT_FCAT
I_DEFAULT = 'X'
I_SAVE = 'A'
TABLES
T_OUTTAB = GT_ALV
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*FORM FRM_STATUS_SET USING P_EXTAB TYPE SLIS_T_EXTAB.
* SET PF-STATUS 'PF_ZPPR0027'.
*ENDFORM.
*&---------------------------------------------------------------------*
*FORM FRM_USER_COMMAND USING P_UCOMM TYPE SY-UCOMM
* RS_SELFIELD TYPE SLIS_SELFIELD.
*
* DATA: LR_GRID TYPE REF TO CL_GUI_ALV_GRID.
* DATA: LS_STABLE TYPE LVC_S_STBL.
* LS_STABLE-ROW = 'X' .
* LS_STABLE-COL = 'X' .
*
* CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
* IMPORTING
* E_GRID = LR_GRID.
* CALL METHOD LR_GRID->CHECK_CHANGED_DATA.
* RS_SELFIELD-REFRESH = 'X'.
*
* CASE P_UCOMM.
** WHEN 'ZCHECK'.
** READ TABLE GT_ALV TRANSPORTING NO FIELDS WITH KEY SLBOX = 'X'.
** IF SY-SUBRC <> 0.
** MESSAGE '请选择需要更新的数据!' TYPE 'S' DISPLAY LIKE 'I'.
** RETURN.
** ENDIF.
**
** PERFORM FRM_CHECK.
* WHEN 'DSEALL'.
* PERFORM FRM_SELECT_ALL USING '' .
*
* WHEN 'SELALL'.
* PERFORM FRM_SELECT_ALL USING 'X' .
* WHEN OTHERS.
* ENDCASE.
*
* "刷新屏幕
* CALL METHOD LR_GRID->REFRESH_TABLE_DISPLAY
* EXPORTING
* IS_STABLE = LS_STABLE.
*
*ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_SET_LAYOUT .
CLEAR GS_LAYO.
GS_LAYO-ZEBRA = 'X'.
GS_LAYO-CWIDTH_OPT = 'X'.
GS_LAYO-BOX_FNAME = 'SLBOX'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_BUILD_FACT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_BUILD_FCAT .
DEFINE M_FEILDCAT.
CLEAR gs_fcat.
gs_fcat-fieldname = &1.
gs_fcat-ref_table = &2.
gs_fcat-ref_field = &3.
gs_fcat-scrtext_l = &4.
gs_fcat-key = &5.
gv_col = gv_col + 1.
gs_fcat-col_pos = gv_col.
APPEND gs_fcat TO gt_fcat.
END-OF-DEFINITION.
REFRESH GT_FCAT.
CLEAR GV_COL.
* M_FEILDCAT 'SLBOX' '' '' '复选框' 'X' .
M_FEILDCAT 'LIGHT' '' '' '状态灯' 'X' .
M_FEILDCAT 'MES' '' '' '消息' 'X' .
M_FEILDCAT 'RSPOS' 'RESB' 'RSPOS' '项目编号' '' .
M_FEILDCAT 'MATNR' 'MARA' 'MATNR' '订单生产物料' '' .
M_FEILDCAT 'MATNR_BOM' 'RESB' 'MATNR' '订单BOM物料' '' .
M_FEILDCAT 'WERKS' 'RESB' 'WERKS' '工厂' '' .
M_FEILDCAT 'AUFNR' 'RESB' 'AUFNR' '生产订单' '' .
M_FEILDCAT 'BAUGR' 'RESB' 'BAUGR' '追溯需求' '' .
M_FEILDCAT 'POSTP' 'RESB' 'POSTP' '条目类别' '' .
M_FEILDCAT 'SORTF' 'RESB' 'SORTF' '排序字符串' '' .
M_FEILDCAT 'DUMPS' 'RESB' 'DUMPS' '虚拟项目' '' .
M_FEILDCAT 'BDMNG' 'RESB' 'BDMNG' '需求数量' '' .
M_FEILDCAT 'ENMNG' 'RESB' 'ENMNG' '提货数' '' .
M_FEILDCAT 'BWART' 'RESB' 'BWART' '移动类型' '' .
M_FEILDCAT 'LGORT' 'RESB' 'LGORT' '库位' '' .
M_FEILDCAT 'AUFLD' 'AFKO' 'AUFLD' 'BOM展开日期' '' .
M_FEILDCAT 'GAMNG' 'AFKO' 'GAMNG' '目标数量' '' .
M_FEILDCAT 'STLNR' 'RESB' 'STLNR' '物料单' '' .
M_FEILDCAT 'RGEKZ' 'RESB' 'RGEKZ' '反冲' '' .
M_FEILDCAT 'STLKN' 'RESB' 'STLKN' ' 项目节点' '' .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SELECT_ALL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> P_
*&---------------------------------------------------------------------*
FORM FRM_SELECT_ALL USING PX TYPE C.
LOOP AT GT_ALV INTO GS_ALV.
GS_ALV-SLBOX = PX .
MODIFY GT_ALV FROM GS_ALV .
ENDLOOP .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DEAL_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_DEAL_DATA .
DATA: LT_STB TYPE TABLE OF STPOX.
DATA: LS_STB TYPE STPOX.
TYPES: BEGIN OF TY_STB_MORE.
INCLUDE STRUCTURE STPOX.
TYPES: FLG TYPE C,
END OF TY_STB_MORE.
DATA: LT_STB_MORE TYPE TABLE OF TY_STB_MORE.
DATA: LS_STB_MORE TYPE TY_STB_MORE.
LOOP AT GT_DATA ASSIGNING FIELD-SYMBOL(<FW_DATA>).
AT NEW MATNR.
"一个生产订单取一次BOM
CLEAR LT_STB.
CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'
EXPORTING
CAPID = 'PP01'
DATUV = <FW_DATA>-AUFLD "展开时间
EMENG = <FW_DATA>-GAMNG "目标数量
BREMS = 'X'
MEHRS = 'X'
MTNRV = <FW_DATA>-MATNR "物料编码
WERKS = <FW_DATA>-WERKS "工厂
TABLES
STB = LT_STB
* MATCAT =
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.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
SORT LT_STB BY IDNRK STLNR STLKN ."MENGE.
"数据存入到自定义内表,方便进行数据处理
CLEAR LT_STB_MORE.
MOVE-CORRESPONDING LT_STB TO LT_STB_MORE.
ENDAT.
CLEAR GS_ALV.
MOVE-CORRESPONDING <FW_DATA> TO GS_ALV.
READ TABLE GT_ALV TRANSPORTING NO FIELDS WITH KEY AUFNR = <FW_DATA>-AUFNR MATNR = <FW_DATA>-MATNR
STLNR = <FW_DATA>-STLNR STLKN = <FW_DATA>-STLKN.
IF SY-SUBRC EQ 0 .
GS_ALV-LIGHT = ICON_RED_LIGHT.
GS_ALV-MES = '订单BOM重复'.
"数据插入到前台展示内表
APPEND GS_ALV TO GT_ALV.
CONTINUE.
ENDIF.
* CLEAR LS_STB_MORE.
READ TABLE LT_STB_MORE ASSIGNING FIELD-SYMBOL(<LW_STB_MORE>)
WITH KEY IDNRK = <FW_DATA>-MATNR_BOM STLNR = <FW_DATA>-STLNR
STLKN = <FW_DATA>-STLKN BINARY SEARCH.
IF SY-SUBRC EQ 0.
"组件用料和生产订单下子物料的用料数量不一致,报错
IF <LW_STB_MORE>-MNGKO NE <FW_DATA>-BDMNG.
GS_ALV-LIGHT = ICON_RED_LIGHT.
GS_ALV-MES = '数量不一致'.
ELSE.
GS_ALV-LIGHT = ICON_GREEN_LIGHT.
GS_ALV-MES = '正常'.
ENDIF.
"对比过的数据打上标识
<LW_STB_MORE>-FLG = 'X'.
ELSE.
"对比未取到值,说明是在标准BOM中没有这条数据
GS_ALV-LIGHT = ICON_RED_LIGHT.
GS_ALV-MES = '标准BOM不存在'.
ENDIF.
"数据插入到前台展示内表
APPEND GS_ALV TO GT_ALV.
AT END OF MATNR.
"删除已经进行对比过的数据
DELETE LT_STB_MORE WHERE FLG EQ 'X'.
IF LT_STB_MORE IS NOT INITIAL.
CLEAR LS_STB_MORE .
"将没有对比过也就是说生产订单下没有的数据展示到前台提示
LOOP AT LT_STB_MORE INTO LS_STB_MORE.
"当有重复物料BOM问题的时候,1对1设置为正常,并且不把当前条数据插入到alv
READ TABLE GT_ALV ASSIGNING FIELD-SYMBOL(<FW_ALV>) WITH KEY AUFNR = <FW_DATA>-AUFNR
MATNR = <FW_DATA>-MATNR
MATNR_BOM = LS_STB_MORE-IDNRK
STLNR = LS_STB_MORE-STLNR
LIGHT = ICON_RED_LIGHT
.
IF SY-SUBRC EQ 0.
<FW_ALV>-LIGHT = ICON_GREEN_LIGHT.
<FW_ALV>-MES = '正常'.
CONTINUE.
ENDIF.
CLEAR GS_ALV.
GS_ALV-AUFNR = <FW_DATA>-AUFNR.
GS_ALV-MATNR = <FW_DATA>-MATNR.
GS_ALV-WERKS = <FW_DATA>-WERKS.
GS_ALV-LGORT = <FW_DATA>-LGORT.
GS_ALV-MATNR_BOM = LS_STB_MORE-IDNRK.
GS_ALV-BDMNG = LS_STB_MORE-MNGKO.
GS_ALV-DUMPS = LS_STB_MORE-DUMPS. "虚拟项目
GS_ALV-RGEKZ = LS_STB_MORE-RGEKZ. "反冲
GS_ALV-SORTF = LS_STB_MORE-SORTF. "排序字符串
GS_ALV-STLNR = LS_STB_MORE-STLNR. "物料单
GS_ALV-LIGHT = ICON_RED_LIGHT.
GS_ALV-MES = '订单缺失物料或标准BOM物料重复'.
APPEND GS_ALV TO GT_ALV.
ENDLOOP.
ENDIF.
ENDAT.
ENDLOOP.
ENDFORM.
OVER…