- 程 序 名:ZMD04
- 程序描述:库存需求清单报表
- 事务代码:ZMD04
- 修改日志
- 日期 版本 修改人 描述
-
- 20231202 1.0 XFL 创建程序
REPORT zmd04.
- Tables Definitions
TABLES: marc.
- Data Definitions 定义数据
TYPES: BEGIN OF ty_data,
werks TYPE marc-werks,
matnr TYPE marc-matnr,
meins TYPE mara-meins,
beskz TYPE marc-beskz,
sobsl TYPE marc-sobsl,
groes TYPE mara-groes,
maktx TYPE makt-maktx,
END OF ty_data,
BEGIN OF ty_dd07v,
domvalue_l TYPE dd07v-domvalue_l,
ddtext TYPE dd07v-ddtext,
END OF ty_dd07v,
BEGIN OF ty_mrp_element,
delkz TYPE delkz,
END OF ty_mrp_element,
BEGIN OF ty_mrp_data,
matnr TYPE marc-matnr,
delkz TYPE delkz,
menge TYPE mseg-menge,
END OF ty_mrp_data.
DATA: gt_data TYPE TABLE OF ty_data,
gt_dd07v TYPE TABLE OF ty_dd07v,
gt_mrp_element TYPE TABLE OF ty_mrp_element,
gt_mrp_data TYPE TABLE OF ty_mrp_data.
DATA: gs_layout TYPE lvc_s_layo, "布局
gt_fieldcat TYPE lvc_t_fcat. "字段
FIELD-SYMBOLS:<gt_data> TYPE STANDARD TABLE.
DATA: gt_table TYPE REF TO data.
- Includes Module 包含模块
- Selection Screen 选择屏幕
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-t01.
PARAMETERS: p_werks LIKE marc-werks OBLIGATORY,
p_berid LIKE mdlv-berid,
p_date LIKE sy-datum DEFAULT ‘99991231’.
SELECT-OPTIONS: s_matnr FOR marc-matnr,
s_dispo FOR marc-dispo,
s_beskz FOR marc-beskz,
s_sobsl FOR marc-sobsl.
SELECTION-SCREEN END OF BLOCK b1.
- Initialization 初始化事件
INITIALIZATION.
- At Selection Screen PAI事件
AT SELECTION-SCREEN.
- At Selection Screen Output PBO事件
AT SELECTION-SCREEN OUTPUT.
- Report Format 报表格式
TOP-OF-PAGE.
END-OF-PAGE.
- Main Process 主要逻辑
START-OF-SELECTION.
"获取数据
PERFORM frm_get_data.
"显示数据
PERFORM frm_display_data.
END-OF-SELECTION.
&---------------------------------------------------------------------
*& Form FRM_GET_DATA
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_get_data .
"物料工厂数据
SELECT marc~werks
marc~matnr
marc~beskz
marc~sobsl
mara~meins
mara~groes
makt~maktx
INTO CORRESPONDING FIELDS OF TABLE gt_data
FROM mara
INNER JOIN marc ON mara~matnr = marc~matnr
LEFT JOIN makt ON mara~matnr = makt~matnr AND spras = sy-langu
WHERE marc~werks = p_werks
AND mara~matnr IN s_matnr
AND marc~dispo IN s_dispo.
"MRP元素说明
SELECT domvalue_l
ddtext
INTO TABLE gt_dd07v
FROM dd07v
WHERE domname = ‘DELKZ’
AND ddlanguage = sy-langu.
SORT: gt_data BY matnr,
gt_dd07v BY domvalue_l.
IF gt_data IS NOT INITIAL.
"获取物料MD04数据
PERFORM frm_get_md04_data.
ENDIF.
ENDFORM.
&---------------------------------------------------------------------
*& Form frm_get_md04_data
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_get_md04_data .
DATA: lt_berid TYPE pph_matnr_werks_berid_tab,
lt_mdps TYPE pph_mdps_abap_ext_tab,
ls_mrp_data TYPE ty_mrp_data,
ls_mrp_element TYPE ty_mrp_element.
"需要查询MD04的料号
lt_berid = VALUE #(
FOR ls_data IN gt_data WHERE ( matnr NE ‘’ )
( werks = p_werks matnr = ls_data-matnr berid = p_berid )
).
CALL FUNCTION ‘MD_MDPSX_READ_API’
EXPORTING
it_matnr_werks_berid = lt_berid
IMPORTING
et_mdpsx = lt_mdps.
LOOP AT lt_mdps ASSIGNING FIELD-SYMBOL(<ls_mdps>) WHERE dat00 <= p_date OR plumi = ‘B’.
ls_mrp_data-matnr = <ls_mdps>-matnr.
ls_mrp_data-delkz = <ls_mdps>-delkz.
CASE <ls_mdps>-plumi.
WHEN ‘B’ OR ‘+’. "库存和供给
ls_mrp_data-menge = <ls_mdps>-mng01.
WHEN ‘-’. "需求
ls_mrp_data-menge = <ls_mdps>-mng01 * -1.
ENDCASE.
COLLECT ls_mrp_data INTO gt_mrp_data.
ls_mrp_element-delkz = <ls_mdps>-delkz.
COLLECT ls_mrp_element INTO gt_mrp_element.
CLEAR:ls_mrp_data,ls_mrp_element.
ENDLOOP.
SORT gt_mrp_data BY matnr delkz.
ENDFORM.
&---------------------------------------------------------------------
*& Form FRM_DISPLAY_DATA
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_display_data .
"栏位最适宽度
gs_layout-cwidth_opt = ‘X’.
"ALV条纹
gs_layout-zebra = ‘X’.
"构建ALV的栏位
PERFORM frm_create_field.
"创建动态表
PERFORM frm_create_dynamic_table.
"处理数据
PERFORM frm_process_alv_data.
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY_LVC’
EXPORTING
i_callback_program = sy-repid "当前程序名
is_layout_lvc = gs_layout "Layout
it_fieldcat_lvc = gt_fieldcat "Fieldcat
i_save = ‘A’
TABLES
t_outtab = <gt_data>
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM.
&---------------------------------------------------------------------
*& Form FRM_CREATE_FIELD
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_create_field .
DATA: ls_fieldcat TYPE lvc_s_fcat,
lv_field TYPE lvc_fname,
lv_fieldname TYPE string,
lv_count TYPE i.
DEFINE fieldcat.
ADD 1 TO lv_count.
"栏位显示顺序
ls_fieldcat-col_pos = lv_count.
"内表栏位
ls_fieldcat-fieldname = &1.
"参考栏位
ls_fieldcat-ref_field = &2.
"参考表
ls_fieldcat-ref_table = &3.
"单位
ls_fieldcat-qfieldname = &4.
"栏位标题(长文或中等文或短文)
ls_fieldcat-scrtext_s = ls_fieldcat-scrtext_m =
ls_fieldcat-scrtext_l = &5.
"显示长文或中等文或短文
ls_fieldcat-colddictxt = &6.
APPEND ls_fieldcat TO gt_fieldcat.
CLEAR ls_fieldcat.
END-OF-DEFINITION.
fieldcat ‘WERKS’ ‘WERKS’ ‘MARC’ ‘’ ‘’ ‘’.
fieldcat ‘MATNR’ ‘MATNR’ ‘MARC’ ‘’ ‘’ ‘’.
fieldcat ‘MEINS’ ‘MEINS’ ‘MARA’ ‘’ ‘’ ‘’.
fieldcat ‘BESKZ’ ‘BESKZ’ ‘MARC’ ‘’ ‘’ ‘’.
fieldcat ‘SOBSL’ ‘SOBSL’ ‘MARC’ ‘’ ‘’ ‘’.
fieldcat ‘GROES’ ‘GROES’ ‘MARA’ ‘’ ‘’ ‘’.
fieldcat ‘MAKTX’ ‘MAKTX’ ‘MAKT’ ‘’ ‘’ ‘’.
fieldcat ‘AV_QTY’ ‘LABST’ ‘MARD’ ‘MEINS’ TEXT-f01 ‘L’.
LOOP AT gt_mrp_element ASSIGNING FIELD-SYMBOL(<ls_mrp_element>).
"栏位
lv_field = ‘MD04_’ && <ls_mrp_element>-delkz.
"栏位说明
READ TABLE gt_dd07v INTO DATA(ls_dd07v) WITH KEY
domvalue_l = <ls_mrp_element>-delkz
BINARY SEARCH.
IF sy-subrc = 0.
lv_fieldname = ls_dd07v-ddtext.
ENDIF.
fieldcat lv_field ‘LABST’ ‘MARD’ ‘MEINS’ lv_fieldname ‘L’.
ENDLOOP.
ENDFORM.
&---------------------------------------------------------------------
*& Form FRM_CREATE_DYNAMIC_TABLE
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_create_dynamic_table .
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = gt_fieldcat
IMPORTING
ep_table = gt_table.
IF gt_table IS BOUND.
ASSIGN gt_table->* TO <gt_data>.
ENDIF.
ENDFORM.
&---------------------------------------------------------------------
*& Form FRM_PROCESS_ALV_DATA
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_process_alv_data .
FIELD-SYMBOLS:
TYPE any.
DATA: line TYPE REF TO data,
lv_field TYPE lvc_fname,
lv_av_qty TYPE mseg-menge.
ASSIGN gt_table->* TO <gt_data>.
CREATE DATA line LIKE LINE OF <gt_data>.
ASSIGN line->* TO
LOOP AT gt_data INTO DATA(ls_data).
CLEAR lv_av_qty.
ASSIGN COMPONENT 'WERKS' OF STRUCTURE <line> TO <field>.
<field> = ls_data-werks.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <line> TO <field>.
<field> = ls_data-matnr.
ASSIGN COMPONENT 'MEINS' OF STRUCTURE <line> TO <field>.
<field> = ls_data-meins.
ASSIGN COMPONENT 'BESKZ' OF STRUCTURE <line> TO <field>.
<field> = ls_data-beskz.
ASSIGN COMPONENT 'SOBSL' OF STRUCTURE <line> TO <field>.
<field> = ls_data-sobsl.
ASSIGN COMPONENT 'GROES' OF STRUCTURE <line> TO <field>.
<field> = ls_data-groes.
ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <line> TO <field>.
<field> = ls_data-maktx.
LOOP AT gt_mrp_element INTO DATA(ls_mrp_element).
READ TABLE gt_mrp_data INTO DATA(ls_mrp_data) WITH KEY
matnr = ls_data-matnr
delkz = ls_mrp_element-delkz
BINARY SEARCH.
IF sy-subrc = 0.
"栏位
lv_field = 'MD04_' && ls_mrp_element-delkz.
ASSIGN COMPONENT lv_field OF STRUCTURE <line> TO <field>.
<field> = ls_mrp_data-menge.
lv_av_qty = lv_av_qty + ls_mrp_data-menge.
ENDIF.
ENDLOOP.
ASSIGN COMPONENT 'AV_QTY' OF STRUCTURE <line> TO <field>.
<field> = lv_av_qty.
APPEND <line> TO <gt_data>.
CLEAR <line>.
ENDLOOP.
ENDFORM.