ABAP程式示例3

mmccking發表於2007-07-30

一個很好的ALV示例。

[@more@]

*&---------------------------------------------------------------------*
*& Report Z_TEST01
*&
*&---------------------------------------------------------------------*
REPORT Z_TEST01 NO STANDARD PAGE HEADING.
*--------------------------定義資料------------------------------------*
*WERKS=工廠,NAME1=工廠名稱,BUDAT=憑證日期,MBLNR=物料憑證編號,
*MJAHR=物料憑證年度,VGART=憑證型別----------*
DATA: BEGIN OF IT_RESULT OCCURS 0,
WERKS LIKE T001W-WERKS,
NAME1 LIKE T001W-NAME1,
BUDAT LIKE MKPF-BUDAT,
DMBTR LIKE MSEG-DMBTR,
ZWZXH TYPE P DECIMALS 3,
END OF IT_RESULT.
DATA: BEGIN OF IT_T001W OCCURS 0,
WERKS LIKE T001W-WERKS,
NAME1 LIKE T001W-NAME1,
END OF IT_T001W.
DATA: BEGIN OF IT_MKPF OCCURS 0,
MBLNR LIKE MKPF-MBLNR,
MJAHR LIKE MKPF-MJAHR,
BUDAT LIKE MKPF-BUDAT,
END OF IT_MKPF.
DATA: BEGIN OF IT_MSEG OCCURS 0,
MBLNR LIKE MSEG-MBLNR,
MJAHR LIKE MSEG-MJAHR,
ZEILE LIKE MSEG-ZEILE,
WERKS LIKE MSEG-WERKS,
DMBTR LIKE MSEG-DMBTR,
SHKZG LIKE MSEG-SHKZG,
END OF IT_MSEG.

TYPE-POOLS: SLIS.

DATA: GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
IS_LAYOUT TYPE SLIS_LAYOUT_ALV.
DATA: TMP_TITLE(70),
TMP_REPID LIKE SY-REPID.
*----------------初始選擇螢幕--------------------*
SELECT-OPTIONS: S_WERKS FOR IT_RESULT-WERKS,
S_BUDAT FOR IT_RESULT-BUDAT.

PARAMETERS: R1 RADIOBUTTON GROUP RAD1,
R2 RADIOBUTTON GROUP RAD1 DEFAULT 'X'.

*-------------------TOP-OF-PAGE------------------*
TOP-OF-PAGE.
PERFORM FRM_HEAD_OUTPUT .
*------------------START-OF-SELECTION-------------*
START-OF-SELECTION.
PERFORM FRM_GETDATA.
PERFORM FRM_PROCESS.
*------------------END-OF-SELECTION---------------*
END-OF-SELECTION.
PERFORM FRM_OUTPUT.
*&---------------------------------------------------------------------*
*& Form FRM_GETDATA
*&---------------------------------------------------------------------*
* 獲取資料
*----------------------------------------------------------------------*
* --&gt p1 text
* *----------------------------------------------------------------------*
FORM FRM_GETDATA .
SELECT * FROM T001W
INTO CORRESPONDING FIELDS OF TABLE IT_T001W
WHERE WERKS IN S_WERKS.

SELECT * FROM MKPF
INTO CORRESPONDING FIELDS OF TABLE IT_MKPF
WHERE VGART EQ 'WA' AND BUDAT IN S_BUDAT.

SELECT * FROM MSEG
INTO CORRESPONDING FIELDS OF TABLE IT_MSEG
WHERE WERKS IN S_WERKS
AND BWART IN ('201','202','261','262').


ENDFORM. " FRM_GETDATA
*&---------------------------------------------------------------------*
*& Form FRM_PROCESS
*&---------------------------------------------------------------------*
* 對資料進行處理
*----------------------------------------------------------------------*
* --&gt p1 text
* *----------------------------------------------------------------------*
FORM FRM_PROCESS .
LOOP AT IT_MSEG.
CLEAR IT_RESULT.
READ TABLE IT_MKPF WITH KEY MBLNR = IT_MSEG-MBLNR
MJAHR = IT_MSEG-MJAHR.
CHECK SY-SUBRC EQ 0.
READ TABLE IT_T001W WITH KEY WERKS = IT_MSEG-WERKS.
IT_RESULT-WERKS = IT_T001W-WERKS.
IT_RESULT-NAME1 = IT_T001W-NAME1.
IT_RESULT-BUDAT = IT_MKPF-BUDAT.
CASE IT_MSEG-SHKZG.
WHEN 'H'.
IT_RESULT-DMBTR = IT_MSEG-DMBTR.
WHEN 'S'.
IT_RESULT-DMBTR = 0 - IT_MSEG-DMBTR.
ENDCASE.
COLLECT IT_RESULT.
ENDLOOP.
ENDFORM. " FRM_PROCESS
*&---------------------------------------------------------------------*
*& Form FRM_OUTPUT
*&---------------------------------------------------------------------*
* 輸出資料
*----------------------------------------------------------------------*
* --&gt p1 text
* *----------------------------------------------------------------------*
FORM FRM_OUTPUT .
IF R2 = 'X' .
GT_FIELDCAT-FIELDNAME = 'WERKS'.
GT_FIELDCAT-SELTEXT_M = '物料號'.
APPEND GT_FIELDCAT.
CLEAR GT_FIELDCAT.
GT_FIELDCAT-FIELDNAME = 'NAME1'.
GT_FIELDCAT-SELTEXT_M = '物料名稱'.
GT_FIELDCAT-OUTPUTLEN = 30.
APPEND GT_FIELDCAT.
CLEAR GT_FIELDCAT.
GT_FIELDCAT-FIELDNAME = 'BUDAT'.
GT_FIELDCAT-SELTEXT_M = '記帳日期'.
GT_FIELDCAT-OUTPUTLEN = 10.
APPEND GT_FIELDCAT.
CLEAR GT_FIELDCAT.
GT_FIELDCAT-FIELDNAME = 'DMBTR'.
GT_FIELDCAT-SELTEXT_M = '本位幣金額'.
GT_FIELDCAT-OUTPUTLEN = 20.
APPEND GT_FIELDCAT.
CLEAR GT_FIELDCAT.
GT_FIELDCAT-FIELDNAME = 'ZWZXH'.
GT_FIELDCAT-SELTEXT_M = '物資消耗(萬元/kwh)'.
GT_FIELDCAT-OUTPUTLEN = 30.
APPEND GT_FIELDCAT.
CLEAR GT_FIELDCAT.
TMP_TITLE = '庫存資金佔用額'.
TMP_REPID = SY-REPID.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = TMP_REPID
IT_FIELDCAT = GT_FIELDCAT[]
I_GRID_TITLE = TMP_TITLE
TABLES
T_OUTTAB = IT_RESULT.
ENDIF.
IF R1 = 'X'.
LOOP AT IT_RESULT.
WRITE: '|',(10) IT_RESULT-WERKS CENTERED,
'|',(40) IT_RESULT-NAME1,
'|',(15) IT_RESULT-BUDAT CENTERED,
'|',(21) IT_RESULT-DMBTR,
'|',(22) IT_RESULT-ZWZXH ,
'|'.
ULINE /.
ENDLOOP.
ENDIF.
ENDFORM. " FRM_OUTPUT
*&---------------------------------------------------------------------*
*& Form FRM_HEAD_OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --&gt p1 text
* *----------------------------------------------------------------------*
FORM FRM_HEAD_OUTPUT .

ULINE .
WRITE: '|',(10) '物料號' CENTERED,
'|',(40) '物料名稱' CENTERED,
'|',(15) '記賬期間' CENTERED,
'|',(21) '本位幣金額' CENTERED ,
'|',(22) '物資消耗(萬元/kwh)' CENTERED ,
'|'.
ULINE /.

ENDFORM. " FRM_HEAD_OUTPUT

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8356764/viewspace-929419/,如需轉載,請註明出處,否則將追究法律責任。

相關文章