昨天下午和今天早上主要研究了如何进行展BOM与反查BOM表。以下程序演示了4种方式,分别为:
1、展开单层BOM
2、展开多层BOM
3、反查单层BOM
4、反查多层BOM
主要用到2个BAPI函数,分别为:
1、CS_BOM_EXPL_MAT_V2 展BOM表
2、CS_WHERE_USED_MAT 反查BOM表
参考了资料:http://blog.csdn.net/reacleliu/article/details/6836193
代码:
*&---------------------------------------------------------------------*
*& Report Z03_30_CS03_BAPI_HX
*&
*&---------------------------------------------------------------------*
*& BOM表展开单层、展开多层、反查一层、反查多层的测试
*&
*&---------------------------------------------------------------------*
REPORT Z03_30_CS03_BAPI_HX.
TABLES MARA.
DATA: WA_CONTAINER TYPE SCRFNAME VALUE 'ALVDATA',
ALV_GRID TYPE REF TO CL_GUI_ALV_GRID,
WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
DATA: STB TYPE STANDARD TABLE OF STPOX WITH HEADER LINE.
DATA: OK_CODE TYPE SY-UCOMM.
DATA: CSCMAT TYPE STANDARD TABLE OF CSCMAT WITH HEADER LINE.
DATA: R1 TYPE C,
R2 TYPE C,
R3 TYPE C,
R4 TYPE C,
MEHRS TYPE C.
* 反查物料清单要用到的明细
DATA: IT_WULTB LIKE STPOV OCCURS 0 WITH HEADER LINE,
WULTB LIKE STANDARD TABLE OF STPOV WITH HEADER LINE,
IT_EQUICAT LIKE CSCEQUI OCCURS 0 WITH HEADER LINE,
IT_KNDCAT LIKE CSCKND OCCURS 0 WITH HEADER LINE,
IT_MATCAT LIKE CSCMAT OCCURS 0 WITH HEADER LINE,
IT_STDCAT LIKE CSCSTD OCCURS 0 WITH HEADER LINE,
IT_TPLCAT LIKE CSCTPL OCCURS 0 WITH HEADER LINE,
IT_PRJCAT LIKE CSCPRJ OCCURS 0 WITH HEADER LINE.
CALL SCREEN 0100.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'STATUS'.
* SET TITLEBAR 'xxx'.
IF WA_CUSTOM_CONTAINER IS INITIAL.
CREATE OBJECT WA_CUSTOM_CONTAINER
EXPORTING
CONTAINER_NAME = WA_CONTAINER.
CREATE OBJECT ALV_GRID
EXPORTING
I_PARENT = WA_CUSTOM_CONTAINER.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
CASE OK_CODE.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
WHEN 'QUIT'.
LEAVE PROGRAM.
WHEN 'CAN'.
LEAVE PROGRAM.
WHEN 'BTOK'.
IF R1 = 'X' OR R2 = 'X'."展开单层或多层BOM表
IF R1 = 'X'.
MEHRS = ''.
ELSE.
MEHRS = 'X'.
ENDIF.
PERFORM EXPANDBOM.
CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'STPOX'
CHANGING
IT_OUTTAB = STB[].
ELSEIF R3 = 'X'."反查单层BOM表
PERFORM where_user_bom.
CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'STPOV'
CHANGING
IT_OUTTAB = IT_WULTB[].
ELSEIF R4 = 'X'."反查多层BOM表
PERFORM WHERE_USER_BOM_MULIT.
CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'STPOV'
CHANGING
IT_OUTTAB = WULTB[].
ENDIF.
WHEN OTHERS.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Form EXPANDBOM
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
*1) 虚拟件 MDMPS -
* 多层 MEHRS X
* 结果: 全展(显示包含虚拟件)
*
*2)虚拟件 MDMPS X
* 多层 MEHRS X
* 结果: 展1或2层(下层遇虚拟件则展开至其下一层,显示包含虚拟件)
*
*3) 虚拟件 MDMPS -
* 多层 MEHRS -
* 结果: 展一层(下层为虚拟件,不再向下展开)
*
*4) 虚拟件 MDMPS X
* 多层 MEHRS -
* 结果:展一层 (同3,下层为虚拟件,不再向下展开)
* 即:MEHRS置空,不论MDMPS如何设置,都只展一层,并且如果下层就是虚拟件,不展开虚拟件至其更下一层,与2)要区别开来
FORM EXPANDBOM .
CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'
EXPORTING
FTREL = 'X'
* ALEKZ = ' '
* ALTVO = ' '
* AUFSW = ' '
* AUMGB = ' '
* AUMNG = 0
* AUSKZ = ' '
* AMIND = ' '
* BAGRP = ' '
* BEIKZ = ' '
* BESSL = ' '
* BGIXO = ' '
* BREMS = ' '
CAPID = 'PP01' "表示查询的是生产BOM,
* CHLST = ' '
* COSPR = ' '
* CUOBJ = 000000000000000
* CUOVS = 0
* CUOLS = ' '
DATUV = SY-DATUM "BOM的有效起始日期,一般指定为当前日,表示读取当前有效的BOM表,如果一个物料有多个版本的BOM表,不同的日期会查出不同版本的BOM
* DELNL = ' '
* DRLDT = ' '
EHNDL = '1'
EMENG = 2 "如果这里填写数量,到时候会用这个数*BOM定额作为组件的数量。,即如果定额为2,传入10,则展开时组件的定额为20
* ERSKZ = ' '
* ERSSL = ' '
* FBSTP = ' '
* KNFBA = ' '
* KSBVO = ' '
* MBWLS = ' '
* MKTLS = 'X'
* MDMPS = 'X' "X表示展开虚拟件,为空表示不展开虚拟件
MEHRS = MEHRS "X表示展开多层,' '表示展开单层,其中还受参数MDMPS影响,MEHRS与MDMPS4种组合见上面所述
* MKMAT = ' '
* MMAPS = ' '
* SALWW = ' '
* SPLWW = ' '
* MMORY = ' ' “是否使用缓存
MTNRV = MARA-MATNR "要展开BOM的物料号码
* NLINK = ' '
* POSTP = ' '
* RNDKZ = ' '
* RVREL = ' '
* SANFR = ' '
* SANIN = ' '
* SANKA = ' '
* SANKO = ' '
* SANVS = ' '
* SCHGT = ' '
* STKKZ = ' '
STLAL = '1' "可选BOM的编号
* STLAN = ' '
* STPST = 0
* SVWVO = 'X'
WERKS = '3000' "工厂
* NORVL = ' '
* MDNOT = ' '
* PANOT = ' '
* QVERW = ' '
* VERID = ' '
* VRSVO = 'X'
* IMPORTING
* TOPMAT =
* DSTST =
TABLES
STB = STB
MATCAT = CSCMAT
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.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. " EXPANDBOM
*&---------------------------------------------------------------------*
*& Form WHERE_USER_BOM
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM WHERE_USER_BOM .
CLEAR:IT_WULTB,IT_WULTB[].
CALL FUNCTION 'CS_WHERE_USED_MAT'
EXPORTING
DATUB = SY-DATUM
DATUV = SY-DATUM
MATNR = MARA-MATNR
* POSTP = ' '
* RETCODE_ONLY = ' '
* STLAN = ' '
MCLMT = '00000000'
WERKS = '3000'
* IMPORTING
* TOPMAT =
TABLES
WULTB = IT_WULTB
EQUICAT = IT_EQUICAT
KNDCAT = IT_KNDCAT
MATCAT = IT_MATCAT
STDCAT = IT_STDCAT
TPLCAT = IT_TPLCAT
EXCEPTIONS
CALL_INVALID = 1
MATERIAL_NOT_FOUND = 2
NO_WHERE_USED_REC_FOUND = 3
NO_WHERE_USED_REC_SELECTED = 4
NO_WHERE_USED_REC_VALID = 5
OTHERS = 6.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. " WHERE_USER_BOM
*&---------------------------------------------------------------------*
*& Form WHERE_USER_BOM_MULIT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM WHERE_USER_BOM_MULIT .
DATA: INDEX TYPE I.
DESCRIBE TABLE WULTB LINES INDEX.
IF INDEX <> 0.
READ TABLE WULTB INDEX INDEX.
MARA-MATNR = WULTB-MATNR.
ENDIF.
CLEAR IT_WULTB[].
CALL FUNCTION 'CS_WHERE_USED_MAT'
EXPORTING
DATUB = SY-DATUM
DATUV = SY-DATUM
MATNR = MARA-MATNR
* POSTP = ' '
* RETCODE_ONLY = ' '
* STLAN = ' '
MCLMT = '00000000'
WERKS = '3000'
* IMPORTING
* TOPMAT =
TABLES
WULTB = IT_WULTB
EQUICAT = IT_EQUICAT
KNDCAT = IT_KNDCAT
MATCAT = IT_MATCAT
STDCAT = IT_STDCAT
TPLCAT = IT_TPLCAT
EXCEPTIONS
CALL_INVALID = 1
MATERIAL_NOT_FOUND = 2
NO_WHERE_USED_REC_FOUND = 3
NO_WHERE_USED_REC_SELECTED = 4
NO_WHERE_USED_REC_VALID = 5
OTHERS = 6.
IF SY-SUBRC = 0."如果展开到最顶层,该值为3,不继续展开
IT_WULTB-LEVEL = INDEX + 1.
APPEND IT_WULTB TO WULTB.
PERFORM WHERE_USER_BOM_MULIT.
ENDIF.
ENDFORM. " WHERE_USER_BOM_MULIT