春节刚过,还没得心情来上班。就接到了新任务修改一下报表ZFI002财务凭证查询报表。当然对于老手来说这样的事情是小菜一碟了。也确实我是新手都2个小时搞定。所以清闲的很,用来记录以下过程。给需要的童鞋学习。任务主要是要新增加栏位:借方发生额,贷方发生额,文本,参照,用户名。并且用户名可以输入。不管它啦。先看看之前的是什么样子。呵呵~~
输入(查询条件):公司代码:BKPF-BUKRS 凭证编号:BKPF-BELNR 会计年度:BKPF- GJAHR 凭证类型:BKPF- BLART 凭证日期:BKPF-BLDAT 过账日期:BKPF-BUDAT 用户名:BKPF-USNAM(新增加哦~)
表有:T_BKPF,T_BSEG,T_LFA1,T_SKAT
处理过程有:GET_BKPF()-->CHECK_BKPF()-->GET_BSEG()-->PROCESS_BSEG()-->EVENTS_BUILD()-->LAYOUT_BUILD()-->FIELDS_BUILD()-->DISPLAY_BUILD().
是不是很啰嗦啊,其实就是那么简单嘛,分析要求:取数据-->处理数据-->alv显示 其实很多ABAP的报表都是这样的一个套路。好了,其他的也就不多说了,直接上Code.
*&---------------------------------------------------------------------*
*& Report ZFI002
*&---------------------------------------------------------------------*
* Author : RobertLee
* Date : 2008.05.02
* Purpose: FI Doc Report using ALV For Finance
*
*&---------------------------------------------------------------------*
* Change : Jasson.Lee
* Date : 2012.02.14
* Purpose: FI Doc Report using ALV For Finance Add col
*
*----------------------------------------------------------------------
REPORT ZFI002.
TABLES: BKPF,BSEG,LFA1,SKAT.
*--------------------------------
* Global Types
* Essential Declaration for ALV Display
*--------------------------------
TYPE-POOLS: slis.
*--------------------------------
* Global Internal Tables
*--------------------------------
DATA:
i_fieldcat_alv TYPE slis_t_fieldcat_alv ,
i_layout TYPE slis_layout_alv,
i_fieldcat TYPE slis_fieldcat_alv,
i_events TYPE slis_t_event,
w_events LIKE LINE OF i_events,
i_list_comments TYPE slis_t_listheader,
w_list_comments LIKE LINE OF i_list_comments,
w_repid LIKE sy-repid.
DATA: BEGIN OF T_BKPF OCCURS 0,
BUKRS LIKE BKPF-BUKRS, "CoCode
BELNR LIKE BKPF-BELNR, "FI Doc
GJAHR LIKE BKPF-GJAHR, "Fiscal Year
BLART LIKE BKPF-BLART, "Doc Type
BLDAT LIKE BKPF-BLDAT, "Doc Date
BUDAT LIKE BKPF-BUDAT, "Posting Date
TCODE LIKE BKPF-TCODE, "Transaction Code
AWKEY LIKE BKPF-AWKEY, "Reference Doc
BSTAT LIKE BKPF-BSTAT, "Document Status
BKTXT LIKE BKPF-BKTXT, " txt
XBLNR LIKE BKPF-XBLNR, " ref
USNAM LIKE BKPF-USNAM, " user name
END OF T_BKPF.
DATA: BEGIN OF T_BSEG OCCURS 0,
BUKRS LIKE BSEG-BUKRS, "CoCode
BELNR LIKE BSEG-BELNR, "FI Doc
GJAHR LIKE BSEG-GJAHR, "Fiscal Year
BUZEI LIKE BSEG-BUZEI, "Item
SHKZG LIKE BSEG-SHKZG, "Credit or Debit
PSWSL LIKE BSEG-PSWSL, "Cur
DMBTR like BSEG-DMBTR, "Local Amount
PSWBT LIKE BSEG-PSWBT, "Amount
SAKNR LIKE BSEG-SAKNR, "G/L Account Number
HKONT LIKE BSEG-HKONT, "General Ledger Account
LIFNR LIKE BSEG-LIFNR, "Account Number of Vendor or Creditor
BSCHL LIKE BSEG-BSCHL, "Posting Key
BLART LIKE BKPF-BLART, "Doc Type
BLDAT LIKE BKPF-BLDAT, "Doc Date
BUDAT LIKE BKPF-BUDAT, "Posting Date
TCODE LIKE BKPF-TCODE, "Transaction Code
AWKEY LIKE BKPF-AWKEY, "Reference Doc
BSTAT LIKE BKPF-BSTAT, "Document status
TXT20 LIKE SKAT-TXT20, "G/L Acccount Name
NAME1 LIKE LFA1-NAME1, "Vendor Name
PSWBT1 LIKE BSEG-PSWBT, "Amount
PSWBT2 LIKE BSEG-PSWBT, "Amount
BKTXT LIKE BKPF-BKTXT, " txt
XBLNR LIKE BKPF-XBLNR, " ref
USNAM LIKE BKPF-USNAM, " user name
END OF T_BSEG.
DATA: BEGIN OF T_LFA1 OCCURS 0,
LIFNR LIKE LFA1-LIFNR,
NAME1 LIKE LFA1-NAME1,
END OF T_LFA1.
DATA: BEGIN OF T_SKAT OCCURS 0,
SAKNR LIKE SKAT-SAKNR,
TXT20 LIKE SKAT-TXT20,
END OF T_SKAT.
DATA W_LEN TYPE I.
*--------------------------------
* Selection Screen
*--------------------------------
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS:
S_BUKRS FOR BKPF-BUKRS Default 5000, "CoCode
S_BELNR FOR BKPF-BELNR, "FI Doc
S_GJAHR FOR BKPF-GJAHR Default 2008, "Fiscal Year
S_BLART FOR BKPF-BLART, "Doc Type
S_BLDAT FOR BKPF-BLDAT, "Doc Date
S_BUDAT FOR BKPF-BUDAT, "Posting Date
S_USNAM FOR BKPF-USNAM.
SELECTION-SCREEN END OF BLOCK b1.
*--------------------------------
* Initialization
*--------------------------------
INITIALIZATION.
*--------------------------------
* At Selection Screen PBO
*--------------------------------
AT SELECTION-SCREEN OUTPUT.
*--------------------------------
* Start of Selection
*--------------------------------
START-OF-SELECTION.
PERFORM GET_BKPF.
PERFORM CHECK_BKPF.
PERFORM GET_BSEG.
PERFORM Process_BSEG.
PERFORM Events_build.
PERFORM Layout_build.
PERFORM Fields_bulid.
PERFORM Display_data.
END-OF-SELECTION.
*--------------------------------
* Top of Page
*--------------------------------
TOP-OF-PAGE.
*--------------------------------
* At User Command
*--------------------------------
AT USER-COMMAND.
*--------------------------------
* At Line Selection
*--------------------------------
AT LINE-SELECTION.
*&---------------------------------------------------------------------*
*& Form display_data
*&---------------------------------------------------------------------*
FORM display_data.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_user_command = 'USER_COMMAND' "用户触发事件
* i_callback_pf_status_set = 'PF_STATUS_SET'(001) "调用用户事件和按钮事件
i_callback_program = w_repid "当前程序
is_layout = i_layout "子函数layout_build填充的格式定义
it_fieldcat = i_fieldcat_alv[] "子函数fields填充的各列
it_events = i_events[]
i_save = 'A' "保存变式
TABLES
t_outtab = T_BSEG.
ENDFORM. "display_data
*---------------------------------------------------------------------*
* FORM user_command *
*---------------------------------------------------------------------*
FORM USER_COMMAND USING i_ucomm LIKE sy-ucomm
selfield TYPE slis_selfield.
CASE i_ucomm.
WHEN '&IC1'.
CASE selfield-sel_tab_field.
WHEN '1-BELNR'.
SET PARAMETER ID 'BLN' FIELD selfield-value.
SET PARAMETER ID 'BUK' FIELD T_BSEG-BUKRS.
SET PARAMETER ID 'GJR' FIELD T_BSEG-GJAHR.
CALL TRANSACTION 'FB03' AND SKIP FIRST SCREEN.
ENDCASE.
WHEN OTHERS.
ENDCASE.
ENDFORM. "callback_ucomm
*&---------------------------------------------------------------------*
*& Form GET_BKPF
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_BKPF .
*Get Data From BKPF
SELECT BUKRS BELNR GJAHR BLART BLDAT BUDAT TCODE AWKEY BSTAT BKTXT XBLNR USNAM
INTO TABLE T_BKPF
FROM BKPF
WHERE BUKRS IN S_BUKRS AND BELNR IN S_BELNR AND BLDAT IN S_BLDAT
and GJAHR in S_GJAHR and BLART in S_BLART AND BUDAT IN S_BUDAT AND USNAM IN S_USNAM .
ENDFORM. " GET_BKPF
*&---------------------------------------------------------------------*
*& Form CHECK_BKPF
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM CHECK_BKPF .
IF T_BKPF[] IS INITIAL.
MESSAGE I000(ZMsg) with '没找到对应的数据,请更改查询条件'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " CHECK_BKPF
*&---------------------------------------------------------------------*
*& Form GET_BSEG
*&---------------------------------------------------------------------*
FORM GET_BSEG.
*Get Data From BSEG
SELECT BUKRS BELNR GJAHR BUZEI SHKZG PSWSL DMBTR PSWBT SAKNR HKONT LIFNR BSCHL
INTO CORRESPONDING FIELDS OF TABLE T_BSEG
FROM BSEG
FOR ALL ENTRIES IN T_BKPF
WHERE BUKRS = T_BKPF-BUKRS AND BELNR = T_BKPF-BELNR AND GJAHR = T_BKPF-GJAHR.
*Get G/L Account name from skat
SELECT SAKNR TXT20
INTO TABLE T_SKAT
FROM SKAT
WHERE SPRAS = SY-LANGU AND KTOPL = 'INT'.
*Get Vendor Info from LFA1
SELECT LIFNR NAME1
INTO TABLE T_LFA1
FROM LFA1
FOR ALL ENTRIES IN T_BSEG
WHERE LIFNR = T_BSEG-LIFNR.
ENDFORM. "GET_BSEG
*&---------------------------------------------------------------------*
*& Form events_build
*&---------------------------------------------------------------------*
FORM events_build.
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
i_list_type = 0
IMPORTING
et_events = i_events.
* READ TABLE i_events WITH KEY name = 'TOP_OF_PAGE' INTO w_events.
* IF sy-subrc = 0.
* MOVE 'ALV_TOP_OF_PAGE' TO w_events-form.
* MODIFY i_events FROM w_events INDEX sy-tabix.
* ENDIF.
*
* READ TABLE i_events WITH KEY name = 'END_OF_LIST' INTO w_events.
* IF sy-subrc = 0.
* MOVE 'ALV_END_OF_LIST' TO w_events-form.
* MODIFY i_events FROM w_events INDEX sy-tabix.
* ENDIF.
READ TABLE i_events WITH KEY name = 'USER_COMMAND' INTO w_events.
IF sy-subrc = 0.
MOVE 'USER_COMMAND' TO w_events-form.
MODIFY i_events FROM w_events INDEX sy-tabix.
ENDIF.
ENDFORM. "events_build
*&---------------------------------------------------------------------*
*& Form layout_build
*&---------------------------------------------------------------------*
FORM layout_build.
* i_layout-detail_popup = 'X'. "弹出详细信息窗口
* i-layout-no_vline = 'X'. "设置列间隔线
i_layout-info_fieldname = 'COLOR'. "颜色值
i_layout-colwidth_optimize = 'X'. "优化列宽选项
i_layout-detail_initial_lines = 'X'.
i_layout-detail_titlebar = '凭证查询报表'.
i_layout-no_colhead =' '.
w_repid = sy-repid.
ENDFORM. "layout_build
*&---------------------------------------------------------------------*
*& Form Fields_Bulid
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM fields_bulid .
DATA tmp_pos TYPE i.
REFRESH i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'BLART'.
i_fieldcat-seltext_l = '凭证类型'.
i_fieldcat-key = 'X'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'BUKRS'.
i_fieldcat-seltext_l = '公司代码'.
i_fieldcat-outputlen = '15'.
i_fieldcat-no_zero = 'X'.
i_fieldcat-key = 'X'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'BELNR'.
i_fieldcat-seltext_l = '会计凭证号'.
i_fieldcat-outputlen = '10'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'BLDAT'.
i_fieldcat-seltext_l = '凭证日期'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'BUDAT'.
i_fieldcat-seltext_l = '过账日期'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'GJAHR'.
i_fieldcat-seltext_l = '年度'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'BUZEI'.
i_fieldcat-seltext_l = '项目'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'BSCHL'.
i_fieldcat-seltext_l = 'PK'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'SHKZG'.
i_fieldcat-seltext_l = '借方/贷方'.
i_fieldcat-no_out = 'X'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'HKONT'.
i_fieldcat-seltext_l = '科目'.
i_fieldcat-No_Zero = 'X'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'SAKNR'.
i_fieldcat-seltext_l = '总帐科目'.
i_fieldcat-No_Zero = 'X'.
i_fieldcat-no_out = 'X'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'TXT20'.
i_fieldcat-seltext_l = '科目名'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'LIFNR'.
i_fieldcat-seltext_l = 'Vendor'.
i_fieldcat-no_out = 'X'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'NAME1'.
i_fieldcat-seltext_l = '厂商名'.
i_fieldcat-no_out = 'X'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'PSWSL'.
i_fieldcat-seltext_l = '币别'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'PSWBT'.
i_fieldcat-seltext_l = '金额'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'DMBTR'.
i_fieldcat-seltext_l = '本币金额'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'AWKEY'.
i_fieldcat-seltext_l = '原始凭证'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'TCODE'.
i_fieldcat-seltext_l = '原始凭证交易码'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'BSTAT'.
i_fieldcat-seltext_l = '凭证状态'.
i_fieldcat-no_Out = 'X'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'PSWBT1'.
i_fieldcat-seltext_l = '借方发生额'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'PSWBT2'.
i_fieldcat-seltext_l = '贷方发生额'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'BKTXT'.
i_fieldcat-seltext_l = '文本'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'XBLNR'.
i_fieldcat-seltext_l = '参照'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
tmp_pos = tmp_pos + 1.
i_fieldcat-col_pos = tmp_pos.
i_fieldcat-fieldname = 'USNAM'.
i_fieldcat-seltext_l = '用户名'.
APPEND i_fieldcat TO i_fieldcat_alv.
CLEAR i_fieldcat.
ENDFORM. " Fields_Bulid
*&---------------------------------------------------------------------*
*& Form Process_BSEG
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM process_BSEG .
DATA l_loop LIKE sy-tabix.
DATA L_LEN TYPE I.
SORT T_BSEG BY BUKRS GJAHR BELNR.
SORT T_BKPF BY BUKRS GJAHR BELNR.
LOOP AT T_BSEG.
L_LOOP = SY-TABIX.
IF T_BSEG-SHKZG = 'H'.
T_BSEG-DMBTR = T_BSEG-DMBTR * ( -1 ).
T_BSEG-PSWBT = T_BSEG-PSWBT * ( -1 ).
T_BSEG-PSWBT2 = T_BSEG-PSWBT * ( -1 ).
ENDIF.
IF T_BSEG-SHKZG = 'S'.
T_BSEG-PSWBT1 = T_BSEG-PSWBT .
ENDIF.
* Get G/L Account Name
READ TABLE T_SKAT WITH KEY SAKNR = T_BSEG-HKONT BINARY SEARCH.
IF SY-SUBRC = 0.
T_BSEG-TXT20 = T_SKAT-TXT20.
ENDIF.
* Get Vendor Name
IF T_BSEG-SAKNR <> '' AND T_BSEG-LIFNR <> ''.
T_BSEG-HKONT = T_BSEG-LIFNR.
READ TABLE T_LFA1 WITH KEY LIFNR = T_BSEG-LIFNR BINARY SEARCH.
IF SY-SUBRC = 0.
T_BSEG-NAME1 = T_LFA1-NAME1.
T_BSEG-TXT20 = T_LFA1-NAME1.
ENDIF.
ENDIF.
*Move Doc Header to Item
READ TABLE T_BKPF WITH KEY BUKRS = T_BSEG-BUKRS GJAHR = T_BSEG-GJAHR
BELNR = T_BSEG-BELNR BINARY SEARCH.
IF SY-SUBRC = 0.
T_BSEG-BLART = T_BKPF-BLART .
T_BSEG-BLDAT = T_BKPF-BLDAT .
T_BSEG-BUDAT = T_BKPF-BUDAT .
T_BSEG-TCODE = T_BKPF-TCODE .
T_BSEG-AWKEY = T_BKPF-AWKEY .
T_BSEG-BKTXT = T_BKPF-BKTXT .
T_BSEG-XBLNR = T_BKPF-XBLNR .
T_BSEG-USNAM = T_BKPF-USNAM .
ENDIF.
IF T_BSEG-AWKEY <>' '.
W_LEN = STRLEN( T_BSEG-AWKEY ) .
IF W_LEN = 10 OR W_LEN = 14 OR W_LEN = 18.
T_BSEG-AWKEY = T_BSEG-AWKEY(10).
ENDIF.
ENDIF.
MODIFY T_BSEG.
SY-TABIX = L_LOOP.
ENDLOOP.
ENDFORM. " Process_BSEG