SAP MD04相关报表开发详解编程语言

场景描述:MD04是MRP的运行结果,逻辑复杂,功能比较强大。平时可能会遇到业务人员想要将MD04里面某些字段取出来用ALV展示的需求,比如库存短缺报表等,可以用下面的方式来取MD04里面的数据,这个需求比较简单,有复杂需求可以在此基础上增加逻辑。

下面的例子是一个简单的库存短缺报表,将MD04中的部分字段直接取出来展示:

*&---------------------------------------------------------------------* 
*& Report  ZMMR034 
*& 
*&---------------------------------------------------------------------* 
*& 
*& 
*&---------------------------------------------------------------------* 
REPORT  ZMMR034. 
TYPE-POOLS:SLIS. 
TABLES:MARA,SSCRFIELDS,MARC. 
*----------------------------------------------------------------------* 
*       ALV定义 
*----------------------------------------------------------------------* 
DATA: GW_FIELDCAT TYPE SLIS_FIELDCAT_ALV, 
GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV, 
G_LAYOUT    TYPE SLIS_LAYOUT_ALV. 
*----------------------------------------------------------------------* 
* 内表和工作区定义 
*----------------------------------------------------------------------* 
TYPES:BEGIN OF TY_ALV, 
SEL    TYPE CHAR1, 
WERKS  TYPE MARC-WERKS, 
MATNR  TYPE MARC-MATNR, 
MAKTX  TYPE MAKT-MAKTX, 
MEINS  TYPE MARA-MEINS, 
DAT00  TYPE MDEZ-DAT00, 
DELB0  TYPE MDEZ-DELB0, 
EXTRA  TYPE MDEZ-EXTRA, 
UMDAT  TYPE MDEZ-UMDAT, 
AUSKT  TYPE MDEZ-AUSKT, 
MNG01  TYPE MDEZ-MNG01, 
MNG02  TYPE MDEZ-MNG02, 
END OF TY_ALV. 
DATA:GT_ALV TYPE TABLE OF TY_ALV, 
WA_ALV TYPE TY_ALV, 
GT_OUT TYPE TABLE OF TY_ALV, 
WA_OUT TYPE TY_ALV, 
LT_HEADINFO TYPE TABLE OF MT61D, 
LS_HEADINFO LIKE LINE OF LT_HEADINFO, 
LT_INNERINFO TYPE TABLE OF MDEZ, 
LS_INNERINFO LIKE LINE OF LT_INNERINFO. 
*----------------------------------------------------------------------* 
* 选择屏幕 
*----------------------------------------------------------------------* 
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001. 
SELECT-OPTIONS: 
"PARAMETERS: 
S_MATNR FOR MARA-MATNR,            "物料号 
S_WERKS FOR MARC-WERKS OBLIGATORY DEFAULT '8200'.            "工厂 
SELECTION-SCREEN END OF BLOCK B1. 
*----------------------------------------------------------------------* 
* Initialization 
*----------------------------------------------------------------------* 
INITIALIZATION. 
*----------------------------------------------------------------------* 
* at selection screen 
*----------------------------------------------------------------------* 
AT SELECTION-SCREEN OUTPUT. 
AT SELECTION-SCREEN. 
*----------------------------------------------------------------------* 
* Event top of page 
*----------------------------------------------------------------------* 
TOP-OF-PAGE. 
*----------------------------------------------------------------------* 
* event Start of Selection 
*----------------------------------------------------------------------* 
START-OF-SELECTION. 
PERFORM FRM_GET_DATA. 
PERFORM FRM_LAYOUT_BUILD. 
PERFORM FRM_FIELD_BUILD. 
PERFORM FRM_DATA_OUT. 
*----------------------------------------------------------------------* 
*EVENT End-of selection 
*----------------------------------------------------------------------* 
END-OF-SELECTION. 
*----------------------------------------------------------------------* 
*EVENT  End-of page 
*----------------------------------------------------------------------* 
END-OF-PAGE. 
*----------------------------------------------------------------------* 
*----------------------------------------------------------------------* 
** forms 
*----------------------------------------------------------------------* 
*&---------------------------------------------------------------------* 
*&      Form  FRM_CHECK_AUTHORITY 
*&---------------------------------------------------------------------* 
*       "  检查权限 
*----------------------------------------------------------------------* 
FORM FRM_CHECK_AUTHORITY. 
ENDFORM.                    "FRM_CHECK_AUTHORITY 
*----------------------------------------------------------------------* 
** forms 
*----------------------------------------------------------------------* 
*&---------------------------------------------------------------------* 
*&      Form  FRM_LAYOUT_BUILD 
*&---------------------------------------------------------------------* 
*       "  设置表格样式 
*----------------------------------------------------------------------* 
FORM FRM_LAYOUT_BUILD . 
G_LAYOUT-BOX_FIELDNAME        = 'SEL'.  " 选择模式,在最左端有选择按钮 
G_LAYOUT-COLWIDTH_OPTIMIZE    = 'X'.    " 自动调整列宽 
G_LAYOUT-ZEBRA                = 'X'.    " 表格斑马线 
"G_LAYOUT-EDIT = 'X'.                     "可编辑 
G_LAYOUT-INFO_FIELDNAME       = 'CLR'. 
ENDFORM.                    "FRM_LAYOUT_BUILD 
*&---------------------------------------------------------------------* 
*&      Form  FRM_FIELD_BUILD 
*&---------------------------------------------------------------------* 
*       "  建立表头数据 
*----------------------------------------------------------------------* 
FORM FRM_FIELD_BUILD . 
CLEAR GT_FIELDCAT. 
PERFORM FIELD_SET: 
USING 'WERKS'  '工厂'   '15' '' , 
USING 'MATNR'  '物料编码'   '15' 'X' , 
USING 'MAKTX'  '物料描述'   '15' '' , 
USING 'MEINS'  '单位'   '15' '' , 
USING 'DAT00'  '日期'   '15' '' , 
USING 'DELB0'  'MRP元素'   '15' '' , 
USING 'EXTRA'  'MRP元素数据'   '15' '' , 
USING 'UMDAT'  '再计划日期'   '15' '' , 
USING 'AUSKT'  '例外'   '15' '' , 
USING 'MNG01'  '收货/需求'   '15' '' , 
USING 'MNG02'  '可用数量'   '15' ''. 
ENDFORM.                    "FRM_FIELD_BUILD 
*&---------------------------------------------------------------------* 
*&      Form  FRM_DATA_OUT 
*&---------------------------------------------------------------------* 
*       输出数据 
*----------------------------------------------------------------------* 
FORM FRM_DATA_OUT. 
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' 
EXPORTING 
I_CALLBACK_PROGRAM       = SY-REPID 
IS_LAYOUT                = G_LAYOUT 
IT_FIELDCAT              = GT_FIELDCAT 
I_SAVE                   = 'X' 
I_CALLBACK_PF_STATUS_SET = 'FRM_STATE' 
I_CALLBACK_USER_COMMAND  = 'FRM_USER_COMMAND' 
TABLES 
T_OUTTAB                 = GT_OUT 
EXCEPTIONS 
PROGRAM_ERROR            = 1 
OTHERS                   = 2. 
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.                    "FRM_DATA_OUT 
*&---------------------------------------------------------------------* 
*&      Form  FRM_SET_PF_STATUS 
*&---------------------------------------------------------------------* 
FORM FRM_STATE USING RT_EXTAB TYPE SLIS_T_EXTAB. 
SET PF-STATUS 'STANDARD'. 
ENDFORM.                    "FRM_STATE 
*&---------------------------------------------------------------------* 
*&      Form  FRM_USER_COMMAND 
*&---------------------------------------------------------------------* 
FORM FRM_USER_COMMAND USING R_UCOMM LIKE SY-UCOMM 
RS_SELFIELD TYPE SLIS_SELFIELD. 
READ TABLE GT_ALV INTO WA_ALV INDEX RS_SELFIELD-TABINDEX. 
CHECK SY-SUBRC = 0. 
ENDFORM.                    "FRM_USER_COMMAND 
*&---------------------------------------------------------------------* 
*&      Form  FIELD_SET 
*&---------------------------------------------------------------------* 
FORM FIELD_SET USING P_FIELDNAME 
P_SELTEXT_L 
P_OUTPUTLEN 
P_HOTSPOT. 
GW_FIELDCAT-FIELDNAME     = P_FIELDNAME . 
GW_FIELDCAT-SELTEXT_L     = P_SELTEXT_L . 
GW_FIELDCAT-OUTPUTLEN     = P_OUTPUTLEN . 
GW_FIELDCAT-HOTSPOT   = P_HOTSPOT. 
APPEND GW_FIELDCAT TO GT_FIELDCAT. 
CLEAR GW_FIELDCAT. 
ENDFORM.                    "FIELD_SET 
*&---------------------------------------------------------------------* 
*&      Form  FRM_GET_DATA 
*&---------------------------------------------------------------------* 
*       text 
*----------------------------------------------------------------------* 
*  -->  p1        text 
*  <--  p2        text 
*----------------------------------------------------------------------* 
FORM FRM_GET_DATA . 
SELECT MATNR WERKS 
INTO CORRESPONDING FIELDS OF TABLE GT_ALV 
FROM MARC 
WHERE MATNR IN S_MATNR AND WERKS IN S_WERKS. 
IF GT_ALV IS INITIAL. 
MESSAGE '没有找到满足条件的数据' TYPE 'E'. 
ENDIF. 
LOOP AT GT_ALV INTO WA_ALV. 
CALL FUNCTION 'MD_STOCK_REQUIREMENTS_LIST_API' 
EXPORTING 
MATNR                    = WA_ALV-MATNR 
WERKS                    = WA_ALV-WERKS 
IMPORTING 
E_MT61D                  = LS_HEADINFO 
TABLES 
MDEZX                    = LT_INNERINFO 
EXCEPTIONS 
MATERIAL_PLANT_NOT_FOUND = 1 
PLANT_NOT_FOUND          = 2 
OTHERS                   = 3. 
LOOP AT LT_INNERINFO INTO LS_INNERINFO. 
WA_OUT-WERKS = WA_ALV-WERKS. 
WA_OUT-MATNR = WA_ALV-MATNR. 
WA_OUT-MAKTX = LS_HEADINFO-MAKTX. 
WA_OUT-MEINS = LS_HEADINFO-MEINS. 
WA_OUT-DAT00 = LS_INNERINFO-DAT00. 
WA_OUT-DELB0 = LS_INNERINFO-DELB0. 
WA_OUT-EXTRA = LS_INNERINFO-EXTRA. 
WA_OUT-UMDAT = LS_INNERINFO-UMDAT. 
WA_OUT-AUSKT = LS_INNERINFO-AUSKT. 
WA_OUT-MNG01 = LS_INNERINFO-MNG01. 
WA_OUT-MNG02 = LS_INNERINFO-MNG02. 
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' 
EXPORTING 
INPUT  = WA_OUT-MATNR 
IMPORTING 
OUTPUT = WA_OUT-MATNR. 
APPEND WA_OUT TO GT_OUT. 
CLEAR:WA_OUT,LS_INNERINFO. 
ENDLOOP. 
CLEAR:WA_ALV,LS_HEADINFO. 
ENDLOOP. 
SORT GT_OUT BY WERKS MATNR. 
ENDFORM.                    "FRM_GET_DATA 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/19579.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论