科目发生额查询表
一、需求文档
选择屏幕:
输出科目汇总金额屏幕显示字段:
表与表之间的关联条件:
ALV显示样式及功能要求:
执行查看明细后,出现明细的alv,此时双击凭证号跳转到FB03。
二、效果图
选择屏幕界面:
选择条件执行后alv的页面效果:
查看明细的界面:(注意明细金额的借贷方合计是否与总金额相等)
双击凭证号进入FB03:
三、代码
主程序部分:
REPORT ZFY21TLL_ACCO.
INCLUDE ZFY21TLL_ACCO_TOP.
INCLUDE ZFY21TLL_ACCO_S01.
INCLUDE ZFY21TLL_ACCO_F01.
INITIALIZATION."初始化
START-OF-SELECTION.
PERFORM FRM_GET_DATA."获取数据
END-OF-SELECTION.
* IF GT_OUTPUT IS NOT INITIAL.
PERFORM FRM_DISPLAY_DATA."数据展示界面
* ELSE.
* MESSAGE '请选择有效数据!' type 'I'.
* ENDIF.
结构:ZFY21TLL_ACCO_TOP.
TYPE-POOLS: SLIS,ICON.
* 定义结构
*总的数据表
TYPES: BEGIN OF GY_OUTPUT,
HKONT TYPE BSEG-HKONT, "科目
PSWSL TYPE BSEG-PSWSL, "货币
SEL TYPE C, "选择框
STATUS TYPE CHAR4, "状态
TXT20 TYPE SKAT-TXT20, "科目描述
DMBTR TYPE BSEG-DMBTR, "总金额
BUKRS TYPE BKPF-BUKRS, "公司代码
GJAHR TYPE BKPF-GJAHR, "会计年度
BLART TYPE BKPF-BLART, "凭证类型
BELNR TYPE BSEG-BELNR, "凭证号
BUZEI TYPE BSEG-BUZEI, "行号
SHKZG TYPE BSEG-SHKZG, "借贷标识 S
END OF GY_OUTPUT.
TYPES:TT_OUTPUT TYPE STANDARD TABLE OF GY_OUTPUT."定义一个表类型,方便以后将内表作为参数传入进去。
*可以直接根据表类型
*内表和工作区
DATA:GT_OUTPUT TYPE TT_OUTPUT.
DATA:GS_OUTPUT TYPE GY_OUTPUT.
DATA:GT_OUTPUT3 TYPE TT_OUTPUT."第一个alv
DATA:GS_OUTPUT3 TYPE GY_OUTPUT.
DATA:GT_OUTPUT4 TYPE TT_OUTPUT."第二个alv
DATA:GS_OUTPUT4 TYPE GY_OUTPUT.
选择屏幕:ZFY21TLL_ACCO_S01.
*引用表
TABLES:BKPF,BSEG."引用表 "因为下面的屏幕选择器用到了这两个表,所以需要定义
*定义屏幕选择器
SELECTION-SCREEN BEGIN OF BLOCK TLL WITH FRAME TITLE TEXT-T01."屏幕选择器
PARAMETERS P_P1 TYPE BKPF-BUKRS OBLIGATORY DEFAULT '1000'."公司代码
PARAMETERS P_P2 TYPE BKPF-GJAHR OBLIGATORY."会计年度
SELECT-OPTIONS S_S1 FOR BKPF-BLART."凭证类型
SELECT-OPTIONS S_S2 FOR BSEG-HKONT."科目
SELECTION-SCREEN END OF BLOCK TLL.
函数模块:ZFY21TLL_ACCO_F01
FORM FRM_GET_DATA .
SELECT BSEG~HKONT,"科目
* SKAT~TXT20,"科目描述
BSEG~DMBTR,"总金额
BSEG~PSWSL,"货币
BKPF~BUKRS,"公司代码
BKPF~GJAHR,"会计年度
BKPF~BLART,"凭证类型
BSEG~BELNR,"凭证号
BSEG~BUZEI,"行号
BSEG~SHKZG"借贷标识
FROM BKPF
INNER JOIN BSEG ON BKPF~BUKRS = BSEG~BUKRS
AND BKPF~BELNR = BSEG~BELNR
AND BKPF~GJAHR = BSEG~GJAHR
INTO CORRESPONDING FIELDS OF TABLE @GT_OUTPUT
WHERE BKPF~BUKRS = @P_P1"公司代码
AND BKPF~GJAHR = @P_P2"会计年度
AND BKPF~BLART IN @S_S1"凭证类型
AND BSEG~HKONT IN @S_S2."科目
IF SY-SUBRC <> 0.
* MESSAGE S001(00) WITH 'TTT' DISPLAY LIKE 'E'.
** LEAVE LIST-PROCESSING.
*判断是否能够成功取数,如果不能,退出后续操作
MESSAGE S000(ZTLL) DISPLAY LIKE 'E'.
ENDIF.
*放入科目描述:将SKAT~TXT20的数据放入GT_OUTPUT
DATA(LT_OUTPUT2) = GT_OUTPUT. "内表间的赋值
SORT LT_OUTPUT2 BY HKONT.
DELETE ADJACENT DUPLICATES FROM LT_OUTPUT2 COMPARING HKONT. "删除相邻行的重复数据,只保留相邻重复行的第一行数据
IF NOT LT_OUTPUT2 IS INITIAL.
SELECT SAKNR,
TXT20
FROM SKAT
INTO TABLE @DATA(GT_SKAT) "将TXT20放入内表:GT_SKAT
FOR ALL ENTRIES IN @LT_OUTPUT2
WHERE SAKNR = @LT_OUTPUT2-HKONT
AND SKAT~KTOPL = 'INT'
AND SKAT~SPRAS = @SY-LANGU."文本字段取系统当前语言,便于翻译和避免取不同语言下的重复数据
SORT GT_SKAT BY SAKNR.
ENDIF.
SORT LT_OUTPUT2 BY HKONT.
LOOP AT GT_OUTPUT INTO GS_OUTPUT.
READ TABLE GT_SKAT INTO DATA(GS_SKAT)
WITH KEY SAKNR = GS_OUTPUT-HKONT BINARY SEARCH.
IF SY-SUBRC EQ 0.
GS_OUTPUT-TXT20 = GS_SKAT-TXT20.
ENDIF.
*处理总数据中的每笔金额的正负
IF GS_OUTPUT-SHKZG = 'H'.
GS_OUTPUT-DMBTR = ( -1 ) * GS_OUTPUT-DMBTR.
ENDIF.
MODIFY GT_OUTPUT FROM GS_OUTPUT.
CLEAR GS_OUTPUT.
ENDLOOP.
*处理总数据中的每笔金额的正负
* LOOP AT GT_OUTPUT INTO GS_OUTPUT.
* IF GS_OUTPUT-SHKZG = 'S'.
* GS_OUTPUT-DMBTR = GS_OUTPUT-DMBTR.
* ELSEIF GS_OUTPUT-SHKZG = 'H'.
* GS_OUTPUT-DMBTR = ( -1 ) * GS_OUTPUT-DMBTR.
* ENDIF.
* MODIFY GT_OUTPUT FROM GS_OUTPUT.
* CLEAR GS_OUTPUT.
* ENDLOOP.
*获取第一个alv的数据:汇总金额-GT_OUTPUT3
DATA GS_TXT TYPE GY_OUTPUT."临时存储,避免在使用at end of 后非数值字段变成星号
SORT GT_OUTPUT BY HKONT PSWSL.
LOOP AT GT_OUTPUT INTO GS_OUTPUT.
GS_TXT = GS_OUTPUT.
GS_OUTPUT3-DMBTR = GS_OUTPUT3-DMBTR + GS_OUTPUT-DMBTR.
AT END OF PSWSL.
GS_OUTPUT3-HKONT = GS_OUTPUT-HKONT.
GS_OUTPUT3-PSWSL = GS_OUTPUT-PSWSL.
GS_OUTPUT3-TXT20 = GS_TXT-TXT20.
APPEND GS_OUTPUT3 TO GT_OUTPUT3.
CLEAR GS_OUTPUT3.
ENDAT.
ENDLOOP.
*获取指示灯状态
LOOP AT GT_OUTPUT3 INTO GS_OUTPUT3.
IF GS_OUTPUT3-DMBTR > 0.
GS_OUTPUT3-STATUS = ICON_GREEN_LIGHT.
ELSEIF GS_OUTPUT3-DMBTR = 0.
GS_OUTPUT3-STATUS = ICON_YELLOW_LIGHT.
ELSEIF GS_OUTPUT3-DMBTR < 0.
GS_OUTPUT3-STATUS = ICON_RED_LIGHT.
ENDIF.
MODIFY GT_OUTPUT3 FROM GS_OUTPUT3.
CLEAR GS_OUTPUT3.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_DATA
*&---------------------------------------------------------------------*
* text 显示数据
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
*显示第一个alv的数据 (数据存在gt_output3中)
FORM FRM_DISPLAY_DATA .
DATA: LT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV."打印的表格最开头,显示字段名称
DATA: LS_LAYOUT TYPE SLIS_LAYOUT_ALV."布局
DATA: LS_GRID_SETTINGS TYPE LVC_S_GLAY."单元格设置
* DATA:LV_REPID TYPE SY-REPID.
PERFORM FRM_SET_LAYOUT CHANGING LS_LAYOUT."layout格式设置
PERFORM FRM_SET_FIELDCATALOG CHANGING LT_FIELDCAT."FIELDCAT设置
LS_GRID_SETTINGS-EDT_CLL_CB = 'X'."设置单元格
* ALV显示函数
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID "当前字段的名字
I_CALLBACK_PF_STATUS_SET = 'FRM_SET_STATUS' "cns_status gui按钮
I_CALLBACK_USER_COMMAND = 'FRM_PROCESS_USER_COMMAND' "捕获操作命令
I_GRID_SETTINGS = LS_GRID_SETTINGS
I_SAVE = 'A'
IS_LAYOUT = LS_LAYOUT
IT_FIELDCAT = LT_FIELDCAT
TABLES
T_OUTTAB = GT_OUTPUT3.
ENDFORM.
FORM FRM_SET_STATUS USING EXTAB TYPE SLIS_T_EXTAB.
SET PF-STATUS 'STANDARD' EXCLUDING EXTAB. "设置gui:STANDARD
ENDFORM.
FORM FRM_SET_LAYOUT CHANGING CS_LAYOUT TYPE SLIS_LAYOUT_ALV."form格式
CLEAR CS_LAYOUT.
CS_LAYOUT-ZEBRA = 'X'.
* CS_LAYOUT-DETAIL_POPUP = 'X'.
* CS_LAYOUT-F2CODE = '&ETA'.
CS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
CS_LAYOUT-DETAIL_TITLEBAR = SY-TITLE.
ENDFORM.
FORM FRM_SET_FIELDCATALOG CHANGING CT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
DATA: LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
DATA: LV_COL_POS TYPE SYCUCOL.
DEFINE MAC_SET_FIELDCATALOG.
lv_col_pos = lv_col_pos + 1.
ls_fieldcat-col_pos = lv_col_pos.
ls_fieldcat-fieldname = &1.
ls_fieldcat-seltext_l = &2.
LS_FIELDCAT-no_zero = &3."如果取值为零,则为空,既不输出零。lzero输出前导零(X或空),仅NUMC类型字段有效
LS_FIELDCAT-just = &4.
IF &1 EQ 'SEL'.
ls_fieldcat-checkbox = 'X'.
ls_fieldcat-edit = 'X'.
ENDIF.
APPEND ls_fieldcat TO ct_fieldcat. CLEAR ls_fieldcat.
END-OF-DEFINITION.
CLEAR CT_FIELDCAT.
MAC_SET_FIELDCATALOG 'SEL' TEXT-001 '' ''."选择
MAC_SET_FIELDCATALOG 'STATUS' TEXT-002 '' 'C'. "状态
MAC_SET_FIELDCATALOG 'HKONT' TEXT-003 'X' ''."科目
MAC_SET_FIELDCATALOG 'TXT20' TEXT-004 '' ''."科目描述
MAC_SET_FIELDCATALOG 'DMBTR' TEXT-005 '' ''."总金额
MAC_SET_FIELDCATALOG 'PSWSL' TEXT-006 '' ''."货币
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_selectall
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM FRM_SELECTALL .
DATA: LS_OUT TYPE GY_OUTPUT.
LS_OUT-SEL = ABAP_TRUE.
MODIFY GT_OUTPUT3 FROM LS_OUT TRANSPORTING SEL WHERE SEL = ABAP_FALSE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_deselectall
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM FRM_DESELECTALL .
DATA: LS_OUT TYPE GY_OUTPUT.
LS_OUT-SEL = ABAP_FALSE.
MODIFY GT_OUTPUT3 FROM LS_OUT TRANSPORTING SEL WHERE SEL = ABAP_TRUE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DETAILS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_DETAILS ."查看明细:显示第二个alv
DATA ROW TYPE I.
*获取第二个alv的数据,从总数据表 gt_output中取数更好
FIELD-SYMBOLS <FS_OUTPUT4> TYPE GY_OUTPUT.
LOOP AT GT_OUTPUT3 INTO GS_OUTPUT3.
IF GS_OUTPUT3-SEL = 'X'.
ROW = ROW + 1.
ENDIF.
ENDLOOP.
IF ROW > 1.
MESSAGE S001(ZTLL) DISPLAY LIKE 'E'.
ELSEIF ROW < 1.
MESSAGE S002(ZTLL) DISPLAY LIKE 'E'.
ELSEIF ROW = 1.
SORT GT_OUTPUT3 BY HKONT.
READ TABLE GT_OUTPUT3 ASSIGNING <FS_OUTPUT4> WITH KEY SEL = 'X' BINARY SEARCH.
IF SY-SUBRC EQ 0.
SELECT BSEG~HKONT,"科目
BSEG~DMBTR,"金额
BSEG~PSWSL,"货币
BSEG~BELNR,"凭证号
BSEG~BUZEI,"行号
BSEG~SHKZG,"借贷标识
BKPF~BUKRS,"公司代码
BKPF~GJAHR"会计年度
FROM BKPF
INNER JOIN BSEG ON BKPF~BUKRS = BSEG~BUKRS
AND BKPF~BELNR = BSEG~BELNR
AND BKPF~GJAHR = BSEG~GJAHR
INTO CORRESPONDING FIELDS OF TABLE @GT_OUTPUT4
WHERE BSEG~HKONT = @<FS_OUTPUT4>-HKONT
AND BSEG~PSWSL = @<FS_OUTPUT4>-PSWSL
AND BKPF~BUKRS = @P_P1"公司代码
AND BKPF~GJAHR = @P_P2"会计年度
AND BKPF~BLART IN @S_S1."凭证类型
ENDIF.
*处理明细数据中的每笔金额的正负
LOOP AT GT_OUTPUT4 INTO GS_OUTPUT4.
IF GS_OUTPUT4-SHKZG = 'H'.
GS_OUTPUT4-DMBTR = ( -1 ) * GS_OUTPUT4-DMBTR.
ENDIF.
MODIFY GT_OUTPUT4 FROM GS_OUTPUT4.
CLEAR GS_OUTPUT4.
ENDLOOP.
PERFORM FRM_DISPALY_DATA2 ."显示第二个alv的信息
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPALY_DATA2
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_DISPALY_DATA2 .
DATA: LT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV."打印的表格最开头,显示字段名称
DATA: LS_LAYOUT TYPE SLIS_LAYOUT_ALV."布局
* DATA:LV_REPID TYPE SY-REPID.
PERFORM FRM_SET_LAYOUT CHANGING LS_LAYOUT."layout格式设置
PERFORM FRM_SET_FIELDCATALOG2 CHANGING LT_FIELDCAT."FIELDCAT设置
* ALV显示函数
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID "当前字段的名字
I_SAVE = 'A'
IS_LAYOUT = LS_LAYOUT
I_CALLBACK_USER_COMMAND = 'FRM_PROCESS_USER_COMMAND2' "捕获操作命令
IT_FIELDCAT = LT_FIELDCAT
TABLES
T_OUTTAB = GT_OUTPUT4.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_FIELDCATALOG2
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_LT_FIELDCAT text
*----------------------------------------------------------------------*
FORM FRM_SET_FIELDCATALOG2 CHANGING CT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
DATA: LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
DATA: LV_COL_POS TYPE SYCUCOL.
DEFINE MAC_SET_FIELDCATALOG.
lv_col_pos = lv_col_pos + 1.
ls_fieldcat-col_pos = lv_col_pos.
ls_fieldcat-fieldname = &1.
ls_fieldcat-seltext_l = &2.
LS_FIELDCAT-no_zero = &3.
IF &1 EQ 'SEL'.
ls_fieldcat-checkbox = 'X'.
ls_fieldcat-edit = 'X'.
ENDIF.
APPEND ls_fieldcat TO ct_fieldcat. CLEAR ls_fieldcat.
END-OF-DEFINITION.
CLEAR CT_FIELDCAT.
MAC_SET_FIELDCATALOG 'BELNR' TEXT-007 'X'."凭证号
MAC_SET_FIELDCATALOG 'BUZEI' TEXT-008 ''."行号
MAC_SET_FIELDCATALOG 'HKONT' TEXT-009 'X'."科目
MAC_SET_FIELDCATALOG 'SHKZG' TEXT-010 ''."借贷标识
MAC_SET_FIELDCATALOG 'DMBTR' TEXT-011 ''."金额
MAC_SET_FIELDCATALOG 'PSWSL' TEXT-012 ''."货币
ENDFORM.
* 为按钮添加新功能
FORM FRM_PROCESS_USER_COMMAND USING R_UCOMM TYPE SY-UCOMM "sy-ucomm系统字段:引发输入后处理的功能代码
RS_SELFIELD TYPE SLIS_SELFIELD.
CASE R_UCOMM.
WHEN 'SELECTALL'.
PERFORM FRM_SELECTALL.
WHEN 'DESELECTAL'.
PERFORM FRM_DESELECTALL.
WHEN 'DETAILS'.
PERFORM FRM_DETAILS.
ENDCASE.
RS_SELFIELD-REFRESH = ABAP_TRUE.
ENDFORM.
FORM FRM_PROCESS_USER_COMMAND2 USING R_UCOMM TYPE SY-UCOMM "sy-ucomm系统字段:引发输入后处理的功能代码
RS_SELFIELD TYPE SLIS_SELFIELD.
FIELD-SYMBOLS <FS_OUTPUT> TYPE GY_OUTPUT.
CASE R_UCOMM.
WHEN '&IC1'.
IF RS_SELFIELD-FIELDNAME = 'BELNR'.
READ TABLE GT_OUTPUT4 ASSIGNING <FS_OUTPUT> INDEX RS_SELFIELD-TABINDEX.
IF SY-SUBRC = 0.
SET PARAMETER ID: 'BLN' FIELD <FS_OUTPUT>-BELNR.
SET PARAMETER ID: 'BUK' FIELD <FS_OUTPUT>-BUKRS.
SET PARAMETER ID: 'GJR' FIELD <FS_OUTPUT>-GJAHR.
CALL TRANSACTION 'FB03' AND SKIP FIRST SCREEN.
ENDIF.
ENDIF.
ENDCASE.
RS_SELFIELD-REFRESH = ABAP_TRUE.
ENDFORM.