2020.01.11 ABAP随笔获取标准报表数据(MB52)数据进行客制ALV

Posted jxzhu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2020.01.11 ABAP随笔获取标准报表数据(MB52)数据进行客制ALV相关的知识,希望对你有一定的参考价值。

获取标准报表数据(MB52)数据进行客制ALV

之前有一个需求,客户使用的单位大多为采购单位,但是MB52中是物料主数据中的基本单位,所以需要对MB52进行处理。

相比于增强来说,使用submit去获取MB52的数据,然后在自己的报表中再去展示更好一点,也更加灵活。这里不仅仅限于MB52还有任何其他ALV报表都可以使用这个方法来做到客制的目的。

关键的代码是:

 1 DATA lw_mb52 TYPE ty_mb52.
 2   DATA lt_mb52 TYPE TABLE OF ty_mb52.
 3   DATA ls_mb52 TYPE REF TO data.
 4 
 5 “设置执行时不显示,只获取ALV数据
 6 cl_salv_bs_runtime_info=>set(
 7     display = abap_false
 8     metadata = abap_false
 9     data  = abap_true ).
10 
11   RANGES:lr_mtart FOR mara-mtart,
12          lr_matkl FOR mara-matkl,
13          lr_ekgrp FOR marc-ekgrp.
14 
15 “MB52的程序
16   SUBMIT rm07mlbs
17   WITH matnr IN s_matnr
18   WITH werks IN s_werks
19   WITH lgort IN s_lgort
20   WITH charg IN s_charg
21   WITH mtart IN lr_mtart
22   WITH matkl IN lr_matkl
23   WITH ekgrp IN lr_ekgrp
24   WITH negativ = ‘‘
25   WITH pa_sond = ‘‘
26   WITH xmchb = X
27   WITH nozero = X
28   WITH novalues = ‘‘
29   WITH pa_hsq = ‘‘
30   WITH pa_flt = X
31   WITH p_vari = ‘‘
32   AND RETURN.
33 
34   TRY .“可以获取MB52
35       cl_salv_bs_runtime_info=>get_data_ref(
36         IMPORTING r_data = ls_mb52 ).
37 
38       ASSIGN ls_mb52->* TO <lt_mb52>.
39 
40     CATCH  cx_salv_bs_sc_runtime_info.
41       MESSAGE 无法获取ALV数据  TYPE  E.
42   ENDTRY.
43 
44   cl_salv_bs_runtime_info=>clear_all( ).

我们可以通过DEBUG看一下:

<lt_mb52>为我们获取的内表数据,这个就是MB52非层次显示时候展示的数据

 技术图片

技术图片

 

 

 技术图片

这样就可以将MB52获取的数据转移到内表中,供自己写ALV展示出来

通过MARM来获取物料的库存单位和采购单位

 1   IF  lt_mawk_tmp[] IS NOT INITIAL.
 2     SELECT a~matnr a~bstme b~umrez AS umrez1 b~umren AS umren1 m~ausme AS wausm c~umrez AS umrez2 c~umren AS umren2
 3      INTO TABLE lt_marm
 4      FROM mara AS a
 5      INNER JOIN marc AS m ON m~matnr = a~matnr
 6      LEFT OUTER JOIN marm AS b ON b~matnr = a~matnr AND b~meinh = a~bstme
 7      LEFT OUTER JOIN marm AS c ON c~matnr = m~matnr AND c~meinh = m~ausme
 8      FOR ALL ENTRIES IN lt_mawk_tmp[]
 9      WHERE a~matnr = lt_mawk_tmp-matnr
10        AND m~werks = lt_mawk_tmp-werks .
11   ENDIF.

然后用基本单位数量 * 分母 / 分子来计算采购单位数量

效果如下图所示:根据不同单位显示不同数量

技术图片

 

 

 注意:

因为MB52不输入任何条件直接执行时,会报警告

若我们在自己的界面不加以限制输入,则执行之后会停留在MB52的选择界面

 技术图片

 

技术图片

所以我们加上输入限制,这个点需要注意一下。

1   IF s_werks IS INITIAL AND s_matnr IS INITIAL AND s_lgort IS INITIAL AND s_charg IS INITIAL.
2     MESSAGE s000(zmm) WITH 请输入任一条件 DISPLAY LIKE E.
3     RETURN.
4   ENDIF.

整体代码如下:

创建好 STATUS就可以使用了

  1 *&---------------------------------------------------------------------*
  2 *& Report ZTEST_JXZHU_MB52
  3 *&---------------------------------------------------------------------*
  4 *&
  5 *&---------------------------------------------------------------------*
  6 REPORT ztest_jxzhu_mb52.
  7 
  8 TABLES:mard,mcha .
  9 
 10 "类型
 11 TYPES:BEGIN OF ty_data,
 12         light         TYPE icon_d,
 13         werks         TYPE marc-werks,
 14         lgort         TYPE mard-lgort,
 15         matnr         TYPE marc-matnr,
 16         maktx         TYPE makt-maktx,
 17         meins         TYPE mara-meins, "
 18         menge         TYPE mard-labst, "基本单位数量
 19         bstme         TYPE mara-bstme, "采购订单位
 20         bstmg         TYPE mseg-bstmg, "采购单位数量
 21         wausm         TYPE maw1-wausm, "发货单位
 22         lfimg         TYPE lips-lfimg,
 23         menge_quality TYPE mard-labst,
 24         bstmg_quality TYPE mseg-bstmg,
 25         lfimg_quality TYPE lips-lfimg,
 26         charg         TYPE mchb-charg,
 27         lifnr         TYPE mcha-lifnr,
 28         name1         TYPE lfa1-name1,
 29         hsdat         TYPE mcha-hsdat,
 30         mhdhb         TYPE mara-mhdhb, "总货架寿命--保质期
 31         mhdrz         TYPE mara-mhdrz, "剩余货架寿命
 32         vfdat         TYPE mcha-vfdat, "到期日
 33       END OF ty_data.
 34 
 35 TYPES:BEGIN OF ty_mb52,
 36         matnr TYPE mara-matnr,
 37         maktx TYPE makt-maktx,
 38         werks TYPE marc-werks,
 39         lgort TYPE mard-lgort,
 40         lvorm TYPE mard-lvorm,
 41         charg TYPE mchb-charg,
 42         meins TYPE mara-meins,
 43         labst TYPE mard-labst, "非限制使用的估价的库存
 44         umlme TYPE mard-umlme, "在运库存 (从一库存地到另一库存地)
 45         insme TYPE mard-insme, "在检库存
 46       END OF ty_mb52.
 47 
 48 DATA:gt_data TYPE TABLE OF ty_data.
 49 
 50 FIELD-SYMBOLS:<lt_mb52> TYPE ANY TABLE,
 51               <l_mb52>  TYPE any.
 52 
 53 
 54 "alv
 55 DATA:gt_fieldcat TYPE lvc_t_fcat, "定义列标题(属性信息)
 56      gs_layout   TYPE lvc_s_layo,
 57      gs_fieldcat TYPE lvc_s_fcat, "定义对象
 58 
 59      wa_repid    LIKE sy-repid, "当前程序
 60      gs_title    TYPE lvc_title.
 61 DATA : lv_jg TYPE vtbbewe-atage.
 62 DATA :lv_lqts TYPE c LENGTH 3.
 63 
 64 SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE TEXT-t01.
 65 SELECT-OPTIONS:s_werks FOR mard-werks,
 66                s_matnr FOR mard-matnr,
 67                s_lgort FOR mard-lgort,
 68                s_charg FOR mcha-charg.
 69 SELECTION-SCREEN END OF BLOCK blk1.
 70 
 71 
 72 
 73 
 74 START-OF-SELECTION.
 75   "
 76   IF s_werks IS INITIAL AND s_matnr IS INITIAL AND s_lgort IS INITIAL AND s_charg IS INITIAL.
 77     MESSAGE s000(zmm) WITH 请输入任一条件 DISPLAY LIKE E.
 78     RETURN.
 79   ENDIF.
 80 
 81   PERFORM start_of_selection.
 82 
 83 
 84 END-OF-SELECTION.
 85   PERFORM end_of_selection.
 86 
 87 *&---------------------------------------------------------------------*
 88 *& Form START_OF_SELECTION
 89 *&---------------------------------------------------------------------*
 90 *& text
 91 *&---------------------------------------------------------------------*
 92 *& -->  p1        text
 93 *& <--  p2        text
 94 *&---------------------------------------------------------------------*
 95 FORM start_of_selection .
 96   DATA lw_mb52 TYPE ty_mb52.
 97   DATA lt_mb52 TYPE TABLE OF ty_mb52.
 98   DATA ls_mb52 TYPE REF TO data.
 99 
100   DATA:BEGIN OF lt_mara OCCURS 0,
101          matnr TYPE mara-matnr,
102          mhdhb TYPE mara-mhdhb, "总货架寿命
103          mhdrz TYPE mara-mhdrz,  "剩余货架寿命
104        END OF lt_mara.
105   DATA lt_mara_tmp LIKE TABLE OF lt_mara WITH HEADER LINE.
106 
107   DATA:BEGIN OF lt_mcha OCCURS 0,
108          matnr TYPE mcha-matnr,
109          werks TYPE mcha-werks,
110          charg TYPE mcha-charg,
111          lifnr TYPE mcha-lifnr,
112          name1 TYPE lfa1-name1,
113          hsdat TYPE mcha-hsdat,
114          vfdat TYPE mcha-vfdat,
115        END OF lt_mcha.
116   DATA lt_mcha_tmp LIKE TABLE OF lt_mcha WITH HEADER LINE.
117 
118   DATA :BEGIN OF lt_mawk OCCURS 0,
119           werks TYPE mcha-werks,
120           matnr TYPE mcha-matnr,
121         END OF lt_mawk.
122   DATA lt_mawk_tmp LIKE TABLE OF lt_mcha WITH HEADER LINE.
123 
124   DATA:BEGIN OF lt_marm OCCURS 0,
125          matnr  TYPE marm-matnr,
126          bstme  TYPE mara-bstme,
127          umrez1 TYPE marm-umrez,
128          umren1 TYPE marm-umren,
129          wausm  TYPE maw1-wausm,
130          umrez2 TYPE marm-umrez,
131          umren2 TYPE marm-umren,
132        END OF lt_marm.
133 
134 
135   cl_salv_bs_runtime_info=>set(
136     display = abap_false
137     metadata = abap_false
138     data  = abap_true ).
139 
140   RANGES:lr_mtart FOR mara-mtart,
141          lr_matkl FOR mara-matkl,
142          lr_ekgrp FOR marc-ekgrp.
143 
144   SUBMIT rm07mlbs
145   WITH matnr IN s_matnr
146   WITH werks IN s_werks
147   WITH lgort IN s_lgort
148   WITH charg IN s_charg
149   WITH mtart IN lr_mtart
150   WITH matkl IN lr_matkl
151   WITH ekgrp IN lr_ekgrp
152   WITH negativ = ‘‘
153   WITH pa_sond = ‘‘
154   WITH xmchb = X
155   WITH nozero = X
156   WITH novalues = ‘‘
157   WITH pa_hsq = ‘‘
158   WITH pa_flt = X
159   WITH p_vari = ‘‘
160   AND RETURN.
161 
162   TRY .
163       cl_salv_bs_runtime_info=>get_data_ref(
164         IMPORTING r_data = ls_mb52 ).
165 
166       ASSIGN ls_mb52->* TO <lt_mb52>.
167 
168     CATCH  cx_salv_bs_sc_runtime_info.
169       MESSAGE 无法获取ALV数据  TYPE  E.
170   ENDTRY.
171 
172   cl_salv_bs_runtime_info=>clear_all( ).
173 
174   IF <lt_mb52> IS ASSIGNED.
175     LOOP AT <lt_mb52> ASSIGNING <l_mb52>.
176       MOVE-CORRESPONDING <l_mb52> TO lw_mb52.
177       "物料工厂
178       lt_mawk_tmp-matnr = lw_mb52-matnr.
179       lt_mawk_tmp-werks = lw_mb52-werks.
180       APPEND lt_mawk_tmp.
181       CLEAR lt_mawk_tmp.
182       "物料
183       lt_mara_tmp-matnr = lw_mb52-matnr.
184       APPEND lt_mara_tmp.
185       CLEAR lt_mara_tmp.
186       "批次mcha
187       IF lw_mb52-charg IS NOT INITIAL.
188         lt_mcha_tmp-matnr = lw_mb52-matnr.
189         lt_mcha_tmp-werks = lw_mb52-werks.
190         lt_mcha_tmp-charg = lw_mb52-charg.
191         APPEND lt_mcha_tmp.
192         CLEAR lt_mcha_tmp.
193       ENDIF.
194       APPEND lw_mb52 TO lt_mb52.
195       CLEAR lw_mb52.
196     ENDLOOP.
197   ENDIF.
198 
199   "取 物料描述  供应商 和 供应商描述
200   SORT lt_mara_tmp[] BY matnr.
201   SORT lt_mawk_tmp[] BY werks matnr.
202   SORT lt_mcha_tmp[] BY werks matnr charg.
203 
204   DELETE ADJACENT DUPLICATES FROM lt_mara_tmp COMPARING matnr.
205   DELETE ADJACENT DUPLICATES FROM lt_mawk_tmp COMPARING werks matnr.
206   DELETE ADJACENT DUPLICATES FROM lt_mcha_tmp COMPARING werks matnr charg.
207 
208   IF lt_mara_tmp[] IS NOT INITIAL.
209     SELECT a~matnr a~mhdhb a~mhdrz INTO TABLE lt_mara
210       FROM mara AS a
211       FOR ALL ENTRIES IN lt_mara_tmp[]
212       WHERE a~matnr = lt_mara_tmp-matnr.
213   ENDIF.
214 
215   IF  lt_mawk_tmp[] IS NOT INITIAL.
216     SELECT a~matnr a~bstme b~umrez AS umrez1 b~umren AS umren1 m~ausme AS wausm c~umrez AS umrez2 c~umren AS umren2
217      INTO TABLE lt_marm
218      FROM mara AS a
219      INNER JOIN marc AS m ON m~matnr = a~matnr
220      LEFT OUTER JOIN marm AS b ON b~matnr = a~matnr AND b~meinh = a~bstme
221      LEFT OUTER JOIN marm AS c ON c~matnr = m~matnr AND c~meinh = m~ausme
222      FOR ALL ENTRIES IN lt_mawk_tmp[]
223      WHERE a~matnr = lt_mawk_tmp-matnr
224        AND m~werks = lt_mawk_tmp-werks .
225   ENDIF.
226 
227   IF lt_mcha_tmp[] IS NOT INITIAL.
228     SELECT a~matnr a~werks a~charg a~lifnr b~name1 a~hsdat a~vfdat INTO TABLE lt_mcha
229       FROM mcha AS a
230       LEFT OUTER JOIN lfa1 AS b  ON b~lifnr = a~lifnr
231       FOR ALL ENTRIES IN lt_mcha_tmp[]
232       WHERE a~matnr = lt_mcha_tmp-matnr AND a~werks = lt_mcha_tmp-werks AND a~charg = lt_mcha_tmp-charg.
233   ENDIF.
234 
235   DATA ls_data TYPE ty_data.
236   SORT lt_mara[] BY matnr.
237   SORT lt_mcha[] BY werks matnr charg.
238   SORT lt_marm[] BY matnr.
239 
240   LOOP AT lt_mb52 ASSIGNING FIELD-SYMBOL(<fs1>).
241     ls_data-werks = <fs1>-werks.
242     ls_data-lgort = <fs1>-lgort.
243     ls_data-matnr = <fs1>-matnr.
244     ls_data-maktx = <fs1>-maktx.
245     READ TABLE lt_mara WITH KEY matnr = <fs1>-matnr BINARY SEARCH.
246     IF sy-subrc EQ 0.
247 *      ls_data-maktx = lt_mara-maktx.
248       ls_data-mhdhb = lt_mara-mhdhb.
249       ls_data-mhdrz = lt_mara-mhdrz.
250     ENDIF.
251     ls_data-meins = <fs1>-meins.
252     ls_data-menge = <fs1>-labst + <fs1>-umlme.
253     ls_data-menge_quality = <fs1>-insme.
254     "采购订单
255     READ TABLE lt_marm WITH KEY matnr = <fs1>-matnr BINARY SEARCH.
256     IF sy-subrc EQ 0.
257       IF lt_marm-bstme IS NOT INITIAL.
258         ls_data-bstme = lt_marm-bstme.
259         ls_data-bstmg = ls_data-menge * lt_marm-umren1 / lt_marm-umrez1.
260         ls_data-bstmg_quality = ls_data-menge_quality  * lt_marm-umren1 / lt_marm-umrez1.
261       ELSE.
262         ls_data-bstme = ls_data-meins.
263         ls_data-bstmg = ls_data-menge.
264         ls_data-bstmg_quality = ls_data-menge_quality.
265       ENDIF.
266       "交货
267       IF lt_marm-wausm IS NOT INITIAL.
268         ls_data-wausm = lt_marm-wausm.
269         ls_data-lfimg = ls_data-menge * lt_marm-umren2 / lt_marm-umrez2.
270         ls_data-lfimg_quality = ls_data-menge_quality * lt_marm-umren2 / lt_marm-umrez2.
271       ELSE.
272         ls_data-wausm = ls_data-meins.
273         ls_data-lfimg = ls_data-menge.
274         ls_data-lfimg_quality = ls_data-menge_quality.
275       ENDIF.
276     ENDIF.
277 
278     READ TABLE lt_mcha WITH KEY werks = <fs1>-werks matnr = <fs1>-matnr charg = <fs1>-charg BINARY SEARCH.
279     IF sy-subrc EQ 0.
280       ls_data-charg = <fs1>-charg.
281       ls_data-lifnr = lt_mcha-lifnr.
282       ls_data-name1 = lt_mcha-name1.
283       ls_data-hsdat = lt_mcha-hsdat.
284       ls_data-vfdat = lt_mcha-vfdat.
285     ENDIF.
286 
287 
288     IF ls_data-vfdat IS NOT INITIAL.
289       IF ls_data-vfdat <= sy-datum.
290         ls_data-light = icon_red_light.
291       ENDIF.
292     ELSE.
293       ls_data-light = icon_green_light.
294     ENDIF.
295 
296     APPEND ls_data TO  gt_data.
297     CLEAR ls_data.
298   ENDLOOP.
299 
300 
301 ENDFORM.
302 *&---------------------------------------------------------------------*
303 *& Form END_OF_SELECTION
304 *&---------------------------------------------------------------------*
305 *& text
306 *&---------------------------------------------------------------------*
307 *& -->  p1        text
308 *& <--  p2        text
309 *&---------------------------------------------------------------------*
310 FORM end_of_selection .
311   gs_layout-zebra = X. "ALV表格按斑马线条纹显示
312   gs_layout-cwidth_opt = X. "将ALV字段宽度设置为最优化
313   gs_layout-no_merging = X.
314   wa_repid    = sy-repid.
315 
316   PERFORM frm_fcat_build.
317 
318   CALL FUNCTION REUSE_ALV_GRID_DISPLAY_LVC
319     EXPORTING
320 *     I_INTERFACE_CHECK        = ‘ ‘
321 *     I_BYPASSING_BUFFER       =
322 *     I_BUFFER_ACTIVE          =
323       i_callback_program       = wa_repid
324       i_callback_pf_status_set = PF_STATUS_SET "USER_COMMAND
325 *     i_callback_user_command  = ‘USER_COMMAND‘
326 *     I_CALLBACK_TOP_OF_PAGE   = ‘ ‘
327 *     I_CALLBACK_html_TOP_OF_PAGE       = ‘ ‘
328 *     I_CALLBACK_HTML_END_OF_LIST       = ‘ ‘
329 *     I_STRUCTURE_NAME         =
330 *     I_BACKGROUND_ID          = ‘ ‘
331       i_grid_title             = gs_title
332 *     I_GRID_SETTINGS          =
333       is_layout_lvc            = gs_layout
334       it_fieldcat_lvc          = gt_fieldcat[]
335 *     IT_EXCLUDING             =
336 *     IT_SPECIAL_GROUPS_LVC    =
337 *     IT_SORT_LVC              =
338 *     IT_FILTER_LVC            =
339 *     IT_HYPERLINK             =
340 *     IS_SEL_HIDE              =
341       i_default                = ‘‘
342       i_save                   = U
343 *     IS_VARIANT               =
344 *     IT_EVENTS                =
345 *     IT_EVENT_EXIT            =
346 *   IMPORTING
347 *     E_EXIT_CAUSED_BY_CALLER  =
348 *     ES_EXIT_CAUSED_BY_USER   =
349     TABLES
350       t_outtab                 = gt_data[]
351     EXCEPTIONS
352       program_error            = 1
353       OTHERS                   = 2.
354   IF sy-subrc <> 0.
355     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
356     WITH sy-msgv1 sy-msgv2.
357     EXIT.
358   ENDIF.
359 ENDFORM.
360 *&---------------------------------------------------------------------*
361 *&      Form  PF_STATUS_SET
362 *&---------------------------------------------------------------------*
363 *      用户命令
364 *----------------------------------------------------------------------*
365 *  -->  p1        text
366 *  <--  p2        text
367 *----------------------------------------------------------------------*
368 FORM pf_status_set USING pi_rt_extab TYPE slis_t_extab.
369   SET PF-STATUS STANDARD_FULLSCREEN.
370 ENDFORM.
371 
372 *&---------------------------------------------------------------------*
373 *& Form FRM_FCAT_BUILD
374 *&---------------------------------------------------------------------*
375 *& text
376 *&---------------------------------------------------------------------*
377 *& -->  p1        text
378 *& <--  p2        text
379 *&---------------------------------------------------------------------*
380 FORM frm_fcat_build .
381   DATA lv_pos TYPE i .
382   REFRESH gt_fieldcat.
383   CLEAR gs_fieldcat.
384   CLEAR lv_pos.
385   DEFINE %%append_fieldcat.
386     lv_pos = lv_pos + 1.
387     gs_fieldcat-col_pos = lv_pos.
388     gs_fieldcat-fieldname = &1."字段名
389     gs_fieldcat-coltext = &2."文本
390     gs_fieldcat-no_zero = &3."去掉前导零
391     gs_fieldcat-ref_field = &4.
392     gs_fieldcat-ref_table = &5.
393     APPEND gs_fieldcat TO gt_fieldcat.
394     CLEAR gs_fieldcat.
395   END-OF-DEFINITION.
396   %%append_fieldcat:
397   LIGHT 指示灯 ‘‘   ICON_D,
398   WERKS 工厂 ‘‘ WERKS MARC,
399   LGORT 仓库地点 ‘‘ LGORT MARD,
400   MATNR 物料编号 X MARD MATNR,
401   MAKTX 物料描述 ‘‘ MAKT MAKTX,
402   MEINS 基本单位 ‘‘ MEINS MARA,
403   MENGE 基本单位数量-非限制 ‘‘ MENGE MSEG,
404   MENGE_QUALITY 基本单位数量-质检 ‘‘ MENGE MSEG,
405   BSTME 采购单位 ‘‘ BSTME MARA,
406   BSTMG 采购单位数量-非限制 ‘‘ BSTMG MSEG,
407   BSTMG_QUALITY 采购单位数量-质检 ‘‘ BSTMG MSEG,
408   WAUSM 发货单位 ‘‘ WAUSM MAW1,
409   LFIMG 发货单位数量-非限制 ‘‘ LFIMG LIPS,
410   LFIMG_QUALITY 发货单位数量-质检 ‘‘ LFIMG LIPS,
411   CHARG 批次 ‘‘ CHARG MCHA,
412   LIFNR 供应商 ‘‘ LIFNR LFA1,
413   NAME1 名称 ‘‘ NAME1 LFA1,
414   HSDAT 生产日期 ‘‘ HSDAT MCHA,
415   VFDAT 到期日 ‘‘ VFDAT MCHA,
416   MHDHB 总货架寿命 ‘‘ MHDHB MARA,
417   MHDRZ 剩余货架寿命 ‘‘ MHDRZ MARA.
418 
419 ENDFORM.

 

技术图片

以上是关于2020.01.11 ABAP随笔获取标准报表数据(MB52)数据进行客制ALV的主要内容,如果未能解决你的问题,请参考以下文章

abap 获取生产订单标准工时和实际报工工时的报表

ABAP 取标准报表测试程序

ABAP供应商进项税额查询报表开发

abap简单报表,alv显示,按钮事件

abap开发中,在屏幕上怎样显示alv

sap abap报表显示的问题