在sap开发中常常需要开发台账表,台账的统计时间是变动的, 我们通常会用临时程序来处理。我这里介绍一个简单的动态台账表(统计月份,周等在有限范围内)的实现方法。
1、申明内表
DATA: BEGIN OF gt_list OCCURS 0,
pernr LIKE pa0001-pernr, "员工编号
ename LIKE pa0001-ename, "员工姓名
gjj1 TYPE p DECIMALS 2, "计算周期一字段
gjj2 TYPE p DECIMALS 2, "
gjj3 TYPE p DECIMALS 2, "
gjj4 TYPE p DECIMALS 2, "
gjj5 TYPE p DECIMALS 2, "
gjj6 TYPE p DECIMALS 2, "
gjj7 TYPE p DECIMALS 2, "
gjj8 TYPE p DECIMALS 2, "
gjj9 TYPE p DECIMALS 2, "
gjj10 TYPE p DECIMALS 2, "
gjj11 TYPE p DECIMALS 2, "
gjj12 TYPE p DECIMALS 2, "计算周期12字段
gjj TYPE p DECIMALS 2, "计算合计字段
END OF gt_list.
计算的周期字段由自己需求最大来定义,本例子是12个月的台账统计报表,所以只定义12个字段;
2、定义统计周期参数
PARAMETERS: p_spmons TYPE s031-spmon OBLIGATORY MEMORY ID per DEFAULT sy-datum+0(6) .
PARAMETERS: p_spmone TYPE s031-spmon OBLIGATORY MEMORY ID per DEFAULT sy-datum+0(6) .
3、在AT SELECTION-SCREEN.事件中检查台账期间的合法性
AT SELECTION-SCREEN.
CONCATENATE p_spmons+0(6) '01' INTO g_sdate.
CONCATENATE p_spmone+0(6) '01' INTO g_edate.
CALL FUNCTION 'LAST_DAY_OF_MONTHS'
EXPORTING
day_in = g_edate
IMPORTING
last_day_of_month = g_edate.
IF g_sdate > g_edate.
MESSAGE '截止日期必须大于起始日期' TYPE 'E'.
ENDIF.
CALL FUNCTION 'DURATION_DETERMINE'
EXPORTING
unit = 'MON'
IMPORTING
duration = g_count
CHANGING
start_date = g_sdate
end_date = g_edate.
IF g_count > 12.
MESSAGE '台帐统计不能超过12个月份' TYPE 'E'.
ENDIF.
4、如何定义显示Grid字段格式(参见红色部分代码)
FORM f_fieldcat_init USING im_fieldcat TYPE slis_t_fieldcat_alv
im_sort TYPE slis_t_sortinfo_alv.
DATA: ls_fieldcat TYPE slis_fieldcat_alv,
ls_sort TYPE slis_sortinfo_alv.
DATA: s TYPE d.
DATA: month(5).
DATA: field(20).
DATA: n TYPE i.
* "字段名要大写
s = g_sdate.
REFRESH im_fieldcat.
REFRESH im_sort.
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = 'ENAME'.
ls_fieldcat-seltext_l = '员工姓名'.
ls_fieldcat-outputlen = '10'.
APPEND ls_fieldcat TO im_fieldcat.
*根据输入确定显示统计输出的字段
n = 1.
WHILE s+0(6) <= g_edate+0(6).
WRITE n TO month LEFT-JUSTIFIED.
CONCATENATE 'GJJ' month INTO month.
CONCATENATE s+0(6) '月' '金额' INTO field.
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = month.
ls_fieldcat-seltext_m = field.
ls_fieldcat-outputlen = 15.
ls_fieldcat-do_sum = 'X'.
ls_fieldcat-no_zero = 'X'.
APPEND ls_fieldcat TO im_fieldcat.
n = n + 1.
CALL FUNCTION 'START_TIME_DETERMINE'
EXPORTING
duration = -1
unit = 'MON'
IMPORTING
start_date = s
CHANGING
end_date = s.
ENDWHILE.
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = 'GJJ'.
ls_fieldcat-seltext_m = '合计'.
ls_fieldcat-outputlen = 15.
ls_fieldcat-do_sum = 'X'.
ls_fieldcat-no_zero = 'X'.
APPEND ls_fieldcat TO im_fieldcat.
endform.
5、如何在计算时填写内表的合适字段
data: l_monthdur type i,
l_date type d.
l_date = sdate."数据的日期
*计算数据的日期和起始日期的月份差
CALL FUNCTION 'DURATION_DETERMINE'
EXPORTING
unit = 'MON'
IMPORTING
duration = l_monthdur
CHANGING
start_date = g_sdate
end_date = l_date.
IF l_monthdur = 0.
l_monthdur = 1.
ENDIF.
*根据月份差将数据算到相应字段
CASE l_monthdur.
WHEN 1.
gt_list-gjj1 = gt_list-gjj1 + wa_rt_header-betrg / 2.
WHEN 2.
gt_list-gjj2 = gt_list-gjj2 + wa_rt_header-betrg / 2.
WHEN 3.
gt_list-gjj3 = gt_list-gjj3 + wa_rt_header-betrg / 2.
WHEN 4.
gt_list-gjj4 = gt_list-gjj4 + wa_rt_header-betrg / 2.
WHEN 5.
gt_list-gjj5 = gt_list-gjj5 + wa_rt_header-betrg / 2.
WHEN 6.
gt_list-gjj6 = gt_list-gjj6 + wa_rt_header-betrg / 2.
WHEN 7.
gt_list-gjj7 = gt_list-gjj7 + wa_rt_header-betrg / 2.
WHEN 8.
gt_list-gjj8 = gt_list-gjj8 + wa_rt_header-betrg / 2.
WHEN 9.
gt_list-gjj9 = gt_list-gjj9 + wa_rt_header-betrg / 2.
WHEN 10.
gt_list-gjj10 = gt_list-gjj10 + wa_rt_header-betrg / 2.
WHEN 11.
gt_list-gjj11 = gt_list-gjj11 + wa_rt_header-betrg / 2.
WHEN 12.
gt_list-gjj12 = gt_list-gjj12 + wa_rt_header-betrg / 2.
ENDCASE.