1.动态创建内表
1.1首先维护好一个子例程
FORM frm_add_fcat USING value1 value2 value3 value4.
wa_fcat-fieldname = value1.
wa_fcat-inttype = value2.
wa_fcat-reptext = value3.
wa_fcat-intlen = value4.
APPEND wa_fcat TO it_fcat.
CLEAR: wa_fcat.
ENDFORM. "frm_add_fcat
1.2调用子例程创建静态部分
"静态主表部分
PERFORM frm_add_fcat USING: 'AUART' afpo-dauat '订单类型' '10'.
PERFORM frm_add_fcat USING: 'PLNBEZ' afko-plnbez '物料' '18'.
PERFORM frm_add_fcat USING: 'MAKTX' makt-maktx '物料描述' '40'.
PERFORM frm_add_fcat USING: 'WGBEZ' t023t-wgbez '物料组' '10'.
PERFORM frm_add_fcat USING: 'WERKS' mard-werks '工厂' '10'.
PERFORM frm_add_fcat USING: 'LABST' mard-werks '当前库存' '10'.
PERFORM frm_add_fcat USING: 'MENGE_RJ' matdoc-menge '日均库存' '10'.
PERFORM frm_add_fcat USING: 'MENGE_WG' matdoc-menge '已完工' '10'.
PERFORM frm_add_fcat USING: 'MENGE_SALE' matdoc-menge '可销售' '10'.
PERFORM frm_add_fcat USING: 'MENGE_WWG' matdoc-menge '未完工' '10'.
1.3进行动态循环,创建对应的动态列
"动态日期部分
CLEAR:day_col.
day_col = s_gstrp-high.
DO col TIMES." 根据条件动态生成列
"从起始日期开始算
CALL FUNCTION 'FIMA_DATE_CREATE'
EXPORTING
i_date = day_col
i_days = '+1'
IMPORTING
e_date = day_col.
CONCATENATE 'COL' day_col+4(4) INTO colname.
CONCATENATE '未完工-' day_col+4(4) INTO colreptext.
PERFORM frm_add_fcat USING: colname 'I' colreptext '10'.
ENDDO.
1.4根据创建好的内表结构生成动态表
" 根据it_fact生成动态表
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fcat
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table> .
CREATE DATA dy_line LIKE LINE OF <dyn_table>.
ASSIGN dy_line->* TO <dyn_wa>.
2.相关的前导0处理
LOOP AT gt_logrt_now ASSIGNING FIELD-SYMBOL(<fs_gt_logrt_now1>) .
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = <fs_gt_logrt_now1>-plnbez
IMPORTING
output = <fs_gt_logrt_now1>-plnbez.
ENDLOOP.
3.注意一个语法的坑----AT END OF (此类的新语法)
3.1使用普通的循环可能出现,把内标赋值到工作区的时候出现*******
LOOP AT gt_logrt_complete_wg INTO gw_logrt_complete_wg .
menge_total_fin = gw_logrt_complete_wg-menge + menge_total_fin.
AT END OF aufnr .
gw_logrt_complete_wg-menge = menge_total_fin.
MODIFY gt_logrt_complete_wg FROM gw_logrt_complete_wg TRANSPORTING menge WHERE aufnr = gw_logrt_complete_wg-aufnr.
CLEAR: menge_total_fin .
ENDAT.
ENDLOOP.
3.2正确的写法应该是分配到指针来处理
LOOP AT gt_logrt_complete_mb1 ASSIGNING FIELD-SYMBOL(<fs_gt_logrt_complete_mb2>) .
READ TABLE gt_logrt_complete_wg INTO gw_logrt_complete_wg WITH KEY aufnr = <fs_gt_logrt_complete_mb2>-aufnr.
menge_total_tar = <fs_gt_logrt_complete_mb2>-gamng - gw_logrt_complete_wg-gamng.
AT END OF plnbez .
<fs_gt_logrt_complete_mb2>-gamng = menge_total_tar.
* MODIFY gt_logrt_complete_mb1 FROM gw_logrt_complete_mb1 TRANSPORTING gamng WHERE aufnr = gw_logrt_complete_mb1-aufnr.
CLEAR:menge_total_fin .
ENDAT.
CLEAR: gw_logrt_complete_mb1 ,menge_total_tar ,gw_logrt_complete_wg.
ENDLOOP.
4.注意点排序跟去重的字段要保持一致
SORT gt_logrt_complete_wg BY aufnr .
DELETE ADJACENT DUPLICATES FROM gt_logrt_complete_wg COMPARING aufnr .
5.怎么给动态表写入值呢?(高效率)
5.1 在其他表里面把值处理好,通过关键字来关联,例如:日期之类的?
LOOP AT gt_out INTO gw_out .
"把静态部分的数据复制到动态表里面
MOVE-CORRESPONDING gw_out TO <dyn_wa>.
********-未完工数量 = 目标数量 - 完工数量
"1.生成动态表部分
DO col TIMES.
* index = index + 1.
"从起始日期开始算
CALL FUNCTION 'FIMA_DATE_CREATE'
EXPORTING
i_date = day_col
i_days = '+1'
IMPORTING
e_date = day_col.
CONCATENATE 'COL' day_col+4(4) INTO colname.
ASSIGN COMPONENT colname OF STRUCTURE <dyn_wa> TO <dyn_field>.
READ TABLE gt_logrt_complete_mb1 INTO gw_logrt_complete_mb1 WITH KEY auart = gw_out-auart plnbez = gw_out-plnbez gstrp = day_col .
* "3.目标数据值获取
<dyn_field> = gw_logrt_complete_mb1-gamng .
gw_out-menge_wwg = gw_out-menge_wwg + <dyn_field>.
CLEAR: gt_logrt_complete ,gw_logrt_complete, menge_total_fin , menge_total_tar ,gw_logrt_complete_mb1 , gw_logrt_complete_wg1.
ENDDO.
PERFORM frm_set_stru_value USING 'MENGE_WWG' gw_out-menge_wwg CHANGING <dyn_wa>.
APPEND <dyn_wa> TO <dyn_table>.
CLEAR:gw_out.
day_col = s_gstrp-high.
ENDLOOP.
最后赋上全部的源码啦!
*&---------------------------------------------------------------------*
*& Report ZMMRP046
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zmmrp046.
TABLES:resb,afko,afpo,makt,mard,t023t,matdoc.
DATA :lt_return LIKE bapiret2 OCCURS 0 WITH HEADER LINE.
"动态列表
DATA: wa_layo TYPE lvc_s_layo.
DATA: wa_fcat TYPE lvc_s_fcat,
it_fcat TYPE lvc_t_fcat.
FIELD-SYMBOLS: <fs_table> TYPE STANDARD TABLE,
<fs_structure> TYPE any.
DATA it_scol TYPE lvc_t_scol .
DATA: g_grid TYPE REF TO cl_gui_alv_grid.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa>,
<dyn_field>.
DATA: dy_table TYPE REF TO data,
dy_line TYPE REF TO data.
DATA: colreptext(10).
DATA: colname(10),
index(2).
DATA :col TYPE i .
****ALV参数定义:
TYPE-POOLS: slis.
DATA: it_fieldcat TYPE slis_t_fieldcat_alv,
it_layout TYPE slis_layout_alv.
DATA: lv_repid LIKE sy-repid.
DATA: it_ls_fieldcat TYPE slis_fieldcat_alv.
DATA: lv_pos TYPE i VALUE 1.
TYPES:"主表
BEGIN OF ty_out,
auart TYPE afpo-dauat, "订单类型
plnbez TYPE afko-plnbez , "物料
maktx TYPE makt-maktx , "物料描述
labst TYPE mard-labst , " 当前库存
wgbez TYPE t023t-wgbez, "物料组
werks TYPE mard-werks,
menge_rj TYPE matdoc-menge, "日均库存
menge_wg TYPE matdoc-menge, "已完工
menge_wwg TYPE matdoc-menge, "未完工
menge_sale TYPE matdoc-menge, "可销售库存
gamng TYPE afko-gamng, "工单目标数量
it_fcat TYPE lvc_t_fcat,
END OF ty_out.
TYPES:"当前库存表
BEGIN OF ty_logrt,
auart TYPE aufk-auart , "订单类型
plnbez TYPE afko-plnbez , "物料
labst TYPE mard-labst , " 当前库存
werks TYPE afpo-dwerk, "工厂
lgort TYPE mard-lgort, "库位
END OF ty_logrt.
TYPES:"日均出库
BEGIN OF ty_logrt_aver,
auart TYPE aufk-auart , "订单类型
matbf TYPE matdoc-matbf , "物料
menge TYPE matdoc-menge , " 库存数量
werks TYPE matdoc-werks, "工厂
kunnr TYPE kna1-kunnr,
END OF ty_logrt_aver.
TYPES:"日均出库
BEGIN OF ty_logrt_complete,
auart TYPE aufk-auart , "订单类型
gstrp TYPE afko-gstrp,
plnbez TYPE afko-plnbez , "物料
menge TYPE matdoc-menge , " 库存数量
werks TYPE matdoc-werks, "工厂
gamng TYPE afko-gamng,
kunnr TYPE kna1-kunnr,
aufnr TYPE afko-aufnr,
END OF ty_logrt_complete.
DATA:gt_out TYPE TABLE OF ty_out WITH HEADER LINE,
gw_out TYPE ty_out.
DATA:gt_logrt_now TYPE TABLE OF ty_logrt WITH HEADER LINE,
gw_logrt_now TYPE ty_logrt.
DATA:gt_logrt_avger TYPE TABLE OF ty_logrt_aver WITH HEADER LINE,
gw_logrt_avger TYPE ty_logrt_aver.
DATA:gt_logrt_complete TYPE TABLE OF ty_logrt_complete WITH HEADER LINE,
gw_logrt_complete TYPE ty_logrt_complete.
DATA:gt_logrt_complete_mb TYPE TABLE OF ty_logrt_complete WITH HEADER LINE, "该结构体仅用于存储数据
gw_logrt_complete_mb TYPE ty_logrt_complete.
DATA:gt_logrt_complete_mb1 TYPE TABLE OF ty_logrt_complete WITH HEADER LINE, "该结构体仅用于存储数据
gw_logrt_complete_mb1 TYPE ty_logrt_complete.
DATA:gt_logrt_complete_wg TYPE TABLE OF ty_logrt_complete WITH HEADER LINE, "该结构体仅用于存储数据
gw_logrt_complete_wg TYPE ty_logrt_complete.
DATA:gt_logrt_complete_wg1 TYPE TABLE OF ty_logrt_complete WITH HEADER LINE, "该结构体仅用于存储数据
gw_logrt_complete_wg1 TYPE ty_logrt_complete.
DATA:gt_logrt_tar TYPE TABLE OF ty_logrt_complete WITH HEADER LINE,
gw_logrt_tar TYPE ty_logrt_complete.
DATA: lv_first_day TYPE afko-gstrp, "获取传入时间段的第一天
lv_last_day TYPE afko-gstrp. "获取传入时间段的最后一天
"前十天
DATA: lv_first_day_10 TYPE afko-gstrp, "获取传入时间段的第一天
lv_last_day_10 TYPE afko-gstrp. "获取传入时间段的最后一天
DATA:day_col TYPE afko-gstrp.
DATA :gt_afko TYPE TABLE OF afko.
DATA :i_matnr TYPE afko-plnbez.
"数量汇总--变量
DATA :menge_total TYPE matdoc-menge.
DATA :menge_total_1 TYPE matdoc-menge.
DATA :menge_total_fin TYPE matdoc-menge."当日已完工的数量
DATA :menge_total_tar TYPE matdoc-menge."当日工单汇总数量
SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS:
s_matnr FOR afko-plnbez ,
s_gstrp FOR afko-gstrp OBLIGATORY. "生产开始日期
SELECTION-SCREEN END OF BLOCK block1.
START-OF-SELECTION.
*** 数据上传
* PERFORM frm_upload_data.
* ***** 数据处理
PERFORM get_data.
** 数据输出
PERFORM frm_output_data.
FORM get_data .
"获取本月的第一天
CONCATENATE s_gstrp-low(6) '01' INTO lv_first_day.
"获取本月的最后一天
CALL FUNCTION 'FIMA_DATE_CREATE'
EXPORTING
i_date = s_gstrp-high
* i_months = '-1' "根据月份的加减可以推算需要年月的第一天和最后一天
* i_days = '+1'
i_set_last_day_of_month = 'X'
IMPORTING
e_date = lv_last_day.
"查询期间的前10天--开始日期
CALL FUNCTION 'FIMA_DATE_CREATE'
EXPORTING
i_date = s_gstrp-low
i_days = '-10'
IMPORTING
e_date = lv_first_day_10.
"查询期间的前10天--结束日期
CALL FUNCTION 'FIMA_DATE_CREATE'
EXPORTING
i_date = s_gstrp-high
i_days = '-10'
IMPORTING
e_date = lv_last_day_10.
"计算两个日期之间的差
CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
EXPORTING
i_date_from = s_gstrp-high
* I_KEY_DAY_FROM =
i_date_to = lv_last_day
* I_KEY_DAY_TO =
* I_FLG_SEPARATE = ' '
IMPORTING
e_days = col
* E_MONTHS =
* E_YEARS =
.
col = col - 1 .
"静态主表部分
PERFORM frm_add_fcat USING: 'AUART' afpo-dauat '订单类型' '10'.
PERFORM frm_add_fcat USING: 'PLNBEZ' afko-plnbez '物料' '18'.
PERFORM frm_add_fcat USING: 'MAKTX' makt-maktx '物料描述' '40'.
PERFORM frm_add_fcat USING: 'WGBEZ' t023t-wgbez '物料组' '10'.
PERFORM frm_add_fcat USING: 'WERKS' mard-werks '工厂' '10'.
PERFORM frm_add_fcat USING: 'LABST' mard-werks '当前库存' '10'.
PERFORM frm_add_fcat USING: 'MENGE_RJ' matdoc-menge '日均库存' '10'.
PERFORM frm_add_fcat USING: 'MENGE_WG' matdoc-menge '已完工' '10'.
PERFORM frm_add_fcat USING: 'MENGE_SALE' matdoc-menge '可销售' '10'.
PERFORM frm_add_fcat USING: 'MENGE_WWG' matdoc-menge '未完工' '10'.
"动态日期部分
CLEAR:day_col.
day_col = s_gstrp-high.
DO col TIMES." 根据条件动态生成列
"从起始日期开始算
CALL FUNCTION 'FIMA_DATE_CREATE'
EXPORTING
i_date = day_col
i_days = '+1'
IMPORTING
e_date = day_col.
CONCATENATE 'COL' day_col+4(4) INTO colname.
CONCATENATE '未完工-' day_col+4(4) INTO colreptext.
PERFORM frm_add_fcat USING: colname 'I' colreptext '10'.
ENDDO.
" 根据it_fact生成动态表
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fcat
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table> .
CREATE DATA dy_line LIKE LINE OF <dyn_table>.
ASSIGN dy_line->* TO <dyn_wa>.
"前导0处理
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = s_matnr-low
IMPORTING
output = i_matnr.
" 获取工单类型 物料 物料描述 物料组
SELECT *"afpo~dauat makt~maktx
FROM afko
* JOIN afpo ON afpo~aufnr = afko~aufnr "订单类型
JOIN aufk ON aufk~aufnr = afko~aufnr "订单类型
JOIN makt ON afko~plnbez = makt~matnr"物料描述
JOIN mara ON afko~plnbez = mara~matnr "物料组
JOIN t023t ON mara~matkl = t023t~matkl
INTO CORRESPONDING FIELDS OF TABLE gt_out
WHERE afko~gstrp >= lv_first_day AND afko~gstrp <= lv_last_day
AND ( afko~plnbez IN s_matnr OR afko~stlbez IN s_matnr )
AND aufk~auart IN ( 'GD02' ,'GD11', 'GD12' , 'GD32' ,'GD42' )
.
SORT gt_out BY auart plnbez .
DELETE ADJACENT DUPLICATES FROM gt_out.
LOOP AT gt_out ASSIGNING FIELD-SYMBOL(<fs_gr_out_in>).
* * "删除主数据的前导0
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = <fs_gr_out_in>-plnbez
IMPORTING
output = <fs_gr_out_in>-plnbez.
ENDLOOP.
* 去掉多余的前导0
* IF gt_out[] IS NOT INITIAL.
* CLEAR:gw_out.
* LOOP AT gt_out ASSIGNING FIELD-SYMBOL(<fs_gt_out_matnr>) .
* CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
* EXPORTING
* input = <fs_gt_out_matnr>-plnbez
* IMPORTING
* output = <fs_gt_out_matnr>-plnbez.
*
** MODIFY gt_out FROM gw_out TRANSPORTING plnbez WHERE auart = gw_out-auart AND plnbez = gw_out-plnbez .
*
* CLEAR:gw_out.
* ENDLOOP.
* ENDIF.
* "获取当前库存
SELECT aufk~auart , afko~plnbez, mard~labst ,mard~werks,mard~lgort
FROM mard
JOIN afko ON afko~plnbez = mard~matnr
JOIN aufk ON aufk~werks = mard~werks AND aufk~aufnr = afko~aufnr AND aufk~auart IN ( 'GD02' ,'GD11', 'GD12' , 'GD32' ,'GD42' )
JOIN ztsd007b ON ztsd007b~lgort = mard~lgort AND ztsd007b~werks = mard~werks
INTO CORRESPONDING FIELDS OF TABLE @gt_logrt_now
* FOR ALL ENTRIES IN @gt_out
WHERE ( mard~lgort IN ( '1128','1129','1122','1127','1212','1205','1305','1306','1503' ) OR ( ztsd007b~lgort = mard~lgort AND ztsd007b~werks = mard~werks ) )
AND afko~gstrp >= @lv_first_day
AND afko~gstrp <= @lv_last_day
AND mard~matnr IN @s_matnr
.
"处理当前库存--删除重复数据
SORT gt_logrt_now BY auart plnbez werks lgort.
DELETE ADJACENT DUPLICATES FROM gt_logrt_now.
LOOP AT gt_logrt_now ASSIGNING FIELD-SYMBOL(<fs_gt_logrt_now1>) .
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = <fs_gt_logrt_now1>-plnbez
IMPORTING
output = <fs_gt_logrt_now1>-plnbez.
ENDLOOP.
"赋值到主表里面---当前库存
SORT gt_logrt_now BY auart plnbez .
SORT gt_out BY auart plnbez .
CLEAR:menge_total.
LOOP AT gt_logrt_now INTO gw_logrt_now.
menge_total = gw_logrt_now-labst + menge_total.
AT END OF plnbez.
READ TABLE gt_out INTO gw_out WITH KEY auart = gw_logrt_now-auart plnbez = gw_logrt_now-plnbez .
gw_out-labst = menge_total.
MODIFY gt_out FROM gw_out TRANSPORTING plnbez labst WHERE auart = gw_logrt_now-auart AND plnbez = gw_logrt_now-plnbez .
CLEAR:menge_total.
ENDAT.
CLEAR: gw_logrt_now , gw_out.
ENDLOOP.
* "日均库存
SELECT * "afpo~dwerk ,afko~aufnr, afko~plnbez ,matdoc~menge
FROM matdoc
JOIN kna1 ON matdoc~kunnr = kna1~kunnr AND kna1~ktokd <> 'SD06'
* JOIN afko ON matdoc~matbf = afko~plnbez
* JOIN aufk ON aufk~aufnr = afko~aufnr
INTO CORRESPONDING FIELDS OF TABLE @gt_logrt_avger
WHERE matdoc~matbf IN @s_matnr
AND matdoc~budat > @lv_first_day_10
AND matdoc~budat < @s_gstrp-high
AND matdoc~vgart = 'WL'
AND matdoc~shkzg = 'H'
.
*
"赋值到主表里面---日均库存
SORT gt_logrt_avger BY auart matbf.
SORT gt_out BY auart plnbez .
CLEAR:menge_total.
LOOP AT gt_logrt_avger ASSIGNING FIELD-SYMBOL(<fs_gt_logrt_avger1>).
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = <fs_gt_logrt_avger1>-matbf
IMPORTING
output = <fs_gt_logrt_avger1>-matbf.
ENDLOOP.
LOOP AT gt_logrt_avger INTO gw_logrt_avger.
menge_total = gw_logrt_avger-menge + menge_total.
AT END OF matbf.
READ TABLE gt_out INTO gw_out WITH KEY plnbez = gw_logrt_avger-matbf .
IF menge_total IS NOT INITIAL.
gw_out-menge_rj = menge_total / 10.
ELSE.
gw_out-menge_rj = menge_total.
ENDIF.
"可销售
IF gw_out-labst IS NOT INITIAL AND menge_total IS NOT INITIAL..
gw_out-menge_sale = gw_out-labst / gw_out-menge_rj.
ELSE.
gw_out-menge_sale = 0.
ENDIF.
"**********
MODIFY gt_out FROM gw_out TRANSPORTING menge_rj menge_sale WHERE plnbez = gw_logrt_avger-matbf .
CLEAR:menge_total.
ENDAT.
CLEAR: gw_logrt_avger , gw_out.
ENDLOOP.
* "已完工
SELECT *
FROM matdoc
JOIN afko ON matdoc~aufnr = afko~aufnr AND afko~gstrp >= @lv_first_day AND afko~gstrp <= @lv_last_day
JOIN aufk ON aufk~aufnr = matdoc~aufnr
INTO CORRESPONDING FIELDS OF TABLE @gt_logrt_complete
WHERE matdoc~bwart = '101' OR matdoc~bwart = '102'
AND afko~plnbez IN @s_matnr
.
LOOP AT gt_logrt_complete ASSIGNING FIELD-SYMBOL(<fs_gt_logrt_complete1>) .
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = <fs_gt_logrt_complete1>-plnbez
IMPORTING
output = <fs_gt_logrt_complete1>-plnbez.
ENDLOOP.
"赋值到主表里面---已完工
SORT gt_logrt_complete BY auart plnbez.
SORT gt_out BY auart plnbez .
CLEAR:menge_total,menge_total_1.
LOOP AT gt_logrt_complete INTO gw_logrt_complete.
menge_total = gw_logrt_complete-menge + menge_total.
* menge_total_1 = ( gw_logrt_complete-gamng - gw_logrt_complete-menge ) + menge_total_1.
AT END OF plnbez.
READ TABLE gt_out INTO gw_out WITH KEY auart = gw_logrt_complete-auart plnbez = gw_logrt_complete-plnbez .
gw_out-menge_wg = menge_total.
* gw_out-menge_wwg = menge_total_1.
MODIFY gt_out FROM gw_out TRANSPORTING menge_wg menge_wwg WHERE auart = gw_logrt_complete-auart AND plnbez = gw_logrt_complete-plnbez .
CLEAR:menge_total.
ENDAT.
CLEAR: gw_logrt_avger , gw_out.
ENDLOOP.
****未完工
"1.获取剩余日期的全部天数
CALL FUNCTION 'FIMA_DATE_CREATE'
EXPORTING
i_date = s_gstrp-high
i_days = col
IMPORTING
e_date = lv_last_day_10.
*******目标数量
"1.查找目标数量
CLEAR:gt_logrt_complete.
SELECT *
FROM afko
JOIN aufk ON aufk~aufnr = afko~aufnr
INTO CORRESPONDING FIELDS OF TABLE @gt_logrt_complete_mb
WHERE afko~plnbez IN @s_matnr
AND afko~gstrp > @s_gstrp-high AND afko~gstrp <= @lv_last_day_10
.
LOOP AT gt_logrt_complete_mb ASSIGNING FIELD-SYMBOL(<fs_gt_logrt_complete_mb>).
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = <fs_gt_logrt_complete_mb>-plnbez
IMPORTING
output = <fs_gt_logrt_complete_mb>-plnbez.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = <fs_gt_logrt_complete_mb>-aufnr
IMPORTING
output = <fs_gt_logrt_complete_mb>-aufnr.
ENDLOOP.
APPEND LINES OF gt_logrt_complete_mb TO gt_logrt_complete_mb1. "拷贝一份数据出来-为了后续的在动态列表里面写入日期的值
*
".数据获取
"已完工
SELECT *
FROM matdoc
JOIN afko ON matdoc~aufnr = afko~aufnr AND afko~gstrp > @s_gstrp-high AND afko~gstrp <= @lv_last_day_10
JOIN aufk ON aufk~aufnr = matdoc~aufnr
INTO CORRESPONDING FIELDS OF TABLE @gt_logrt_complete_wg
WHERE ( matdoc~bwart = '101' OR matdoc~bwart = '102' )
AND afko~plnbez IN @s_matnr
.
LOOP AT gt_logrt_complete_wg ASSIGNING FIELD-SYMBOL(<fs_gt_logrt_complete_wg>).
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = <fs_gt_logrt_complete_wg>-plnbez
IMPORTING
output = <fs_gt_logrt_complete_wg>-plnbez.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = <fs_gt_logrt_complete_wg>-aufnr
IMPORTING
output = <fs_gt_logrt_complete_wg>-aufnr.
ENDLOOP.
APPEND LINES OF gt_logrt_complete_wg TO gt_logrt_complete_wg1. "拷贝一份数据出来-为了后续的在动态列表里面写入日期的值
*
*************************已完工
* SORT gt_logrt_complete_wg BY auart plnbez DESCENDING.
* CLEAR:menge_total,menge_total_1 ,gw_logrt_complete_wg,menge_total_fin.
*
*
* LOOP AT gt_logrt_complete_wg INTO gw_logrt_complete_wg .
* menge_total_fin = gw_logrt_complete_wg-menge + menge_total_fin.
*
* AT END OF plnbez .
* gw_logrt_complete_wg-menge = menge_total_fin.
* MODIFY gt_logrt_complete_wg FROM gw_logrt_complete_wg TRANSPORTING menge WHERE auart = gw_logrt_complete_wg-auart AND plnbez = gw_logrt_complete_wg-plnbez .
* CLEAR:menge_total_fin.
* ENDAT.
*
* CLEAR:gw_out,gw_logrt_complete_wg.
* ENDLOOP.
*
*
* "去重
* SORT gt_logrt_complete_wg BY auart plnbez DESCENDING .
* DELETE ADJACENT DUPLICATES FROM gt_logrt_complete_wg COMPARING auart plnbez .
*
* SORT gt_logrt_complete_mb BY auart plnbez.
* CLEAR:menge_total,gw_logrt_complete_mb.
*
* "2.汇总--目标数量
* SORT gt_logrt_complete_mb BY auart plnbez.
* CLEAR:menge_total,menge_total_1 ,gw_logrt_complete_mb,menge_total_fin.
*
*
* LOOP AT gt_logrt_complete_mb INTO gw_logrt_complete_mb .
*
* menge_total_fin = gw_logrt_complete_mb-gamng + menge_total_fin.
*
* AT END OF plnbez .
*
* gw_logrt_complete_mb-gamng = menge_total_fin.
* MODIFY gt_logrt_complete_mb FROM gw_logrt_complete_mb TRANSPORTING gamng WHERE auart = gw_logrt_complete_mb-auart AND plnbez = gw_logrt_complete_mb-plnbez .
* CLEAR:menge_total_fin .
* ENDAT.
*
* CLEAR: gw_logrt_complete_mb .
* ENDLOOP.
*
*
* "去重
* SORT gt_logrt_complete_mb BY auart plnbez .
* DELETE ADJACENT DUPLICATES FROM gt_logrt_complete_mb COMPARING auart plnbez .
***********未完工
* CLEAR: gw_logrt_complete_mb ,gw_logrt_complete_wg ,gw_out.
* SORT : gt_out , gt_logrt_complete_mb ,gt_logrt_complete_wg BY auart plnbez.
* LOOP AT gt_out INTO gw_out.
*
* READ TABLE gt_logrt_complete_mb INTO gw_logrt_complete_mb WITH KEY auart = gw_out-auart plnbez = gw_out-plnbez .
*
* READ TABLE gt_logrt_complete_wg INTO gw_logrt_complete_wg WITH KEY auart = gw_out-auart plnbez = gw_out-plnbez .
*
* gw_out-menge_wwg = gw_logrt_complete_mb-gamng - gw_logrt_complete_wg-menge .
*
* MODIFY gt_out FROM gw_out TRANSPORTING menge_wwg WHERE auart = gw_out-auart AND plnbez = gw_out-plnbez .
*
*
* CLEAR:gw_out , gw_logrt_complete_mb ,gw_logrt_complete_wg.
* ENDLOOP.
*******************----------------------动态数据处理
"已完工数量汇总
SORT gt_logrt_complete_wg BY aufnr.
CLEAR:gw_logrt_complete_wg ,menge_total_fin.
LOOP AT gt_logrt_complete_wg INTO gw_logrt_complete_wg .
menge_total_fin = gw_logrt_complete_wg-menge + menge_total_fin.
AT END OF aufnr .
gw_logrt_complete_wg-menge = menge_total_fin.
MODIFY gt_logrt_complete_wg FROM gw_logrt_complete_wg TRANSPORTING menge WHERE aufnr = gw_logrt_complete_wg-aufnr.
CLEAR: menge_total_fin .
ENDAT.
ENDLOOP.
SORT gt_logrt_complete_wg BY aufnr .
DELETE ADJACENT DUPLICATES FROM gt_logrt_complete_wg COMPARING aufnr .
CLEAR:gw_logrt_complete_wg ,menge_total_fin.
**---- 未完工数量 = 当天的工单的目标数量 - 该工单的完成数量---2024年4月19日
"计算未完工数量
SORT gt_logrt_complete_mb1 BY aufnr .
CLEAR:menge_total,menge_total_1 ,gw_logrt_complete_mb1,menge_total_fin , menge_total_tar.
LOOP AT gt_logrt_complete_mb1 ASSIGNING FIELD-SYMBOL(<fs_gt_logrt_complete_mb2>) .
READ TABLE gt_logrt_complete_wg INTO gw_logrt_complete_wg WITH KEY aufnr = <fs_gt_logrt_complete_mb2>-aufnr.
menge_total_tar = <fs_gt_logrt_complete_mb2>-gamng - gw_logrt_complete_wg-gamng.
AT END OF plnbez .
<fs_gt_logrt_complete_mb2>-gamng = menge_total_tar.
* MODIFY gt_logrt_complete_mb1 FROM gw_logrt_complete_mb1 TRANSPORTING gamng WHERE aufnr = gw_logrt_complete_mb1-aufnr.
CLEAR:menge_total_fin .
ENDAT.
CLEAR: gw_logrt_complete_mb1 ,menge_total_tar ,gw_logrt_complete_wg.
ENDLOOP.
"根据日期汇总未完工数量
SORT gt_logrt_complete_mb1 BY gstrp auart plnbez .
CLEAR:menge_total,menge_total_1 ,gw_logrt_complete_mb1,menge_total_fin , menge_total_tar.
LOOP AT gt_logrt_complete_mb1 INTO gw_logrt_complete_mb1 .
* READ TABLE gt_logrt_complete_wg INTO gw_logrt_complete_wg WITH KEY aufnr = gw_logrt_complete_mb1-aufnr.
* menge_total_tar = gw_logrt_complete_mb1-gamng - gw_logrt_complete_wg-gamng.
menge_total_fin = gw_logrt_complete_mb1-gamng + menge_total_fin.
AT END OF plnbez .
gw_logrt_complete_mb1-gamng = menge_total_fin.
MODIFY gt_logrt_complete_mb1 FROM gw_logrt_complete_mb1 TRANSPORTING gamng WHERE auart = gw_logrt_complete_mb1-auart
AND plnbez = gw_logrt_complete_mb1-plnbez AND gstrp = gw_logrt_complete_mb1-gstrp .
CLEAR:menge_total_fin .
ENDAT.
CLEAR: gw_logrt_complete_mb1 ,menge_total_tar ,gw_logrt_complete_wg.
ENDLOOP.
"去重
SORT gt_logrt_complete_mb1 BY gstrp auart plnbez .
DELETE ADJACENT DUPLICATES FROM gt_logrt_complete_mb1 COMPARING gstrp auart plnbez .
****--------完工数量
* SORT gt_logrt_complete_wg1 BY gstrp auart plnbez .
*
*
* CLEAR:menge_total, menge_total_1 ,gw_logrt_complete_wg1,menge_total_fin.
*
*
* LOOP AT gt_logrt_complete_wg1 INTO gw_logrt_complete_wg1 .
*
* menge_total_fin = gw_logrt_complete_wg1-menge + menge_total_fin.
*
* AT END OF plnbez .
* gw_logrt_complete_wg1-menge = menge_total_fin.
* MODIFY gt_logrt_complete_wg1 FROM gw_logrt_complete_wg1 TRANSPORTING menge
* WHERE auart = gw_logrt_complete_wg1-auart
* AND plnbez = gw_logrt_complete_wg1-plnbez
* AND gstrp = gw_logrt_complete_wg1-gstrp .
* CLEAR:menge_total_fin.
* ENDAT.
*
* CLEAR:gw_out,gw_logrt_complete_wg1.
* ENDLOOP.
*
*
* "去重
* SORT gt_logrt_complete_wg1 BY gstrp auart plnbez DESCENDING .
* DELETE ADJACENT DUPLICATES FROM gt_logrt_complete_wg1 COMPARING gstrp auart plnbez .
"动态日期部分
CLEAR:day_col.
CLEAR: gt_logrt_complete ,gw_logrt_complete, menge_total_fin , menge_total_tar .
day_col = s_gstrp-high.
"本文以 <dyn_table> 作为输出主表,GT_OUT是静态数据的合计,本循环是为了把静态舒服赋值到动态表里面
IF gt_out[] IS NOT INITIAL.
CLEAR:gw_out,index.
*新---2024年4月17日
CLEAR : gw_logrt_complete_mb1 ,gw_logrt_complete_wg1 ,gw_out .
LOOP AT gt_out INTO gw_out .
"把静态部分的数据复制到动态表里面
MOVE-CORRESPONDING gw_out TO <dyn_wa>.
********-未完工数量 = 目标数量 - 完工数量
"1.生成动态表部分
DO col TIMES.
* index = index + 1.
"从起始日期开始算
CALL FUNCTION 'FIMA_DATE_CREATE'
EXPORTING
i_date = day_col
i_days = '+1'
IMPORTING
e_date = day_col.
CONCATENATE 'COL' day_col+4(4) INTO colname.
ASSIGN COMPONENT colname OF STRUCTURE <dyn_wa> TO <dyn_field>.
READ TABLE gt_logrt_complete_mb1 INTO gw_logrt_complete_mb1 WITH KEY auart = gw_out-auart plnbez = gw_out-plnbez gstrp = day_col .
* "3.目标数据值获取
<dyn_field> = gw_logrt_complete_mb1-gamng .
gw_out-menge_wwg = gw_out-menge_wwg + <dyn_field>.
CLEAR: gt_logrt_complete ,gw_logrt_complete, menge_total_fin , menge_total_tar ,gw_logrt_complete_mb1 , gw_logrt_complete_wg1.
ENDDO.
PERFORM frm_set_stru_value USING 'MENGE_WWG' gw_out-menge_wwg CHANGING <dyn_wa>.
APPEND <dyn_wa> TO <dyn_table>.
CLEAR:gw_out.
day_col = s_gstrp-high.
ENDLOOP.
ENDIF.
ENDFORM.
FORM frm_get_stru_value USING pi_field pw_stru TYPE any CHANGING pc_value.
FIELD-SYMBOLS:<fs_temp>.
CLEAR: pc_value.
ASSIGN COMPONENT pi_field OF STRUCTURE pw_stru TO <fs_temp>.
IF sy-subrc = 0.
pc_value = <fs_temp>.
UNASSIGN <fs_temp>.
ENDIF.
ENDFORM. "frm_get_stru_value、
*输出字段定义
FORM frm_fieldcat_init.
DEFINE fieldcat.
CLEAR it_ls_fieldcat.
it_ls_fieldcat-col_pos = 1.
it_ls_fieldcat-fieldname = &1.
it_ls_fieldcat-seltext_l = &2.
it_ls_fieldcat-outputlen = &3.
it_ls_fieldcat-key = &4.
it_ls_fieldcat-seltext_m = it_ls_fieldcat-seltext_l.
it_ls_fieldcat-seltext_s = it_ls_fieldcat-seltext_l.
APPEND it_ls_fieldcat TO it_fieldcat.
CLEAR it_ls_fieldcat.
END-OF-DEFINITION.
fieldcat 'PLNBEZ' '物料编号' '18' 'X' .
fieldcat 'AUART' '订单类型' '10' 'X' .
fieldcat 'MAKTX' '物料描述' '40' 'X' .
fieldcat 'WGBEZ' '物料组' '20' '' .
fieldcat 'LABST' '当前库存' '10' '' .
fieldcat 'MENGE_RJ' '日均库存' '10' '' .
fieldcat 'MENGE_WG' '已完工' '10' '' .
fieldcat 'MENGE_SALE' '可销售' '10' '' .
fieldcat 'MENGE_WWG' '未完工' '10' '' .
DO 3 TIMES .
ENDDO.
ENDFORM. "frm_fieldcat_init
***数据输出
**********
FORM frm_output_data.
* lv_repid = sy-repid.
* it_layout-detail_popup = 'X'.
* it_layout-info_fieldname = 'LINECOLOR'.
* REFRESH :it_fieldcat[].
* PERFORM frm_fieldcat_init.
* CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
* EXPORTING
* i_callback_program = lv_repid
* it_fieldcat = it_fieldcat[]
* is_layout = it_layout
* i_default = 'X'
* i_save = 'A'
** i_grid_title = it_out
* TABLES
* t_outtab = gt_out.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = wa_layo
it_fieldcat_lvc = it_fcat
TABLES
t_outtab = <dyn_table>
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_output_data
FORM frm_add_fcat USING value1 value2 value3 value4.
wa_fcat-fieldname = value1.
wa_fcat-inttype = value2.
wa_fcat-reptext = value3.
wa_fcat-intlen = value4.
APPEND wa_fcat TO it_fcat.
CLEAR: wa_fcat.
ENDFORM. "frm_add_fcat
FORM frm_set_stru_value USING pi_field pi_value CHANGING pw_stru TYPE any.
FIELD-SYMBOLS:<fs_temp>.
ASSIGN COMPONENT pi_field OF STRUCTURE pw_stru TO <fs_temp>.
IF sy-subrc = 0.
<fs_temp> = pi_value.
UNASSIGN <fs_temp>.
ENDIF.
ENDFORM.
执行结果如下: