Monday, April 14, 2008

ALV Report using REUSE

his program creates an ALV report for Daily Cash Receipts for a selected date range.

REPORT sy-repid.

*-----------------------------------------------------------------------

*/ Description :This program creates an ALV report for Daily Cash

* Receipts for a selected date range.

*-----------------------------------------------------------------------

* Global data declaration

TYPE-POOLS: slis.

* Global structure of list

TYPES: BEGIN OF i_cust_invoices, "This is a temp-table where SELECT

"data is to be stored.

bkpf LIKE bkpf, "Acct Doc Header structure

bseg LIKE bseg, "Acct Doc Segment struc

kna1 LIKE kna1, "Customer Master struc

skat LIKE skat, "Chart of Accts struc

END OF i_cust_invoices.

TABLES: bkpf, "TABLES: def of Database tables.

bseg,

kna1,

skat.

*/ Selection and Input Parameters

SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME.

SELECT-OPTIONS: s_belnr FOR bseg-belnr, "Acct Doc number

s_budat FOR bkpf-budat, "Doc Posting Date

s_blart FOR bkpf-blart DEFAULT 'DZ'. "Document Type

SELECTION-SCREEN END OF BLOCK block1.

DATA: gt_fieldcat TYPE slis_t_fieldcat_alv.

DATA: gt_outtab TYPE i_cust_invoices OCCURS 0 WITH HEADER LINE.

DATA: g_repid LIKE sy-repid.

DATA: i_bkpf TYPE bkpf OCCURS 0 WITH HEADER LINE.

* Initialization fieldcatalog

INITIALIZATION.

g_repid = sy-repid.

PERFORM fieldcat_init USING gt_fieldcat[].

* Start of Selection

START-OF-SELECTION.

* Data selection

PERFORM select_data TABLES gt_outtab.

* Display list

END-OF-SELECTION.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

i_callback_program = g_repid

it_fieldcat = gt_fieldcat[]

TABLES

t_outtab = gt_outtab.

*-----------------------------------------------------------------------

* Forms

*-----------------------------------------------------------------------

* Initialization fieldcatalog DEFINITION OF HEADING.

* Fieldnames and ref_fieldnames MUST be UPPER CASE !!

FORM fieldcat_init

USING rt_fieldcat TYPE slis_t_fieldcat_alv.

DATA: ls_fieldcat TYPE slis_fieldcat_alv.

DATA: pos TYPE i VALUE 1.

CLEAR ls_fieldcat.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BKPF-BUDAT'. "Posting Date

ls_fieldcat-ref_fieldname = 'BUDAT'.

ls_fieldcat-ref_tabname = 'BKPF'.

ls_fieldcat-key = 'X'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BSEG-BELNR'. "Acct Doc Number

ls_fieldcat-ref_fieldname = 'BELNR'.

ls_fieldcat-ref_tabname = 'BSEG'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BSEG-HKONT'. "Acct number

ls_fieldcat-ref_fieldname = 'HKONT'.

ls_fieldcat-ref_tabname = 'BSEG'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BSEG-KOSTL'. "Cost Center

ls_fieldcat-ref_fieldname = 'KOSTL'.

ls_fieldcat-ref_tabname = 'BSEG'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'SKAT-TXT20'. "Acct Description

ls_fieldcat-ref_fieldname = 'TXT20'.

ls_fieldcat-ref_tabname = 'SKAT'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'SKAT-TXT50'. "Acct Description

ls_fieldcat-ref_fieldname = 'TXT50'.

ls_fieldcat-ref_tabname = 'SKAT'.

LS_FIELDCAT-NO_OUT = 'X'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

* ls_fieldcat-col_pos = pos.

* ls_fieldcat-fieldname = 'BSEG-AUGBL'. "Clearing Doc

* ls_fieldcat-ref_fieldname = 'AUGBL'.

* ls_fieldcat-ref_tabname = 'BSEG'.

* APPEND ls_fieldcat TO rt_fieldcat.

* CLEAR ls_fieldcat.

* pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BSEG-KUNNR'. "Cust number

ls_fieldcat-ref_fieldname = 'KUNNR'.

ls_fieldcat-ref_tabname = 'BSEG'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'KNA1-NAME1'. "Customer name

ls_fieldcat-ref_fieldname = 'NAME1'.

ls_fieldcat-ref_tabname = 'KNA1'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BKPF-XBLNR'. "Ref Doc (Check) Number

ls_fieldcat-ref_fieldname = 'XBLNR'.

ls_fieldcat-ref_tabname = 'BKPF'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BSEG-SHKZG'. "DR/CR ind.

ls_fieldcat-ref_fieldname = 'SHKZG'.

ls_fieldcat-ref_tabname = 'BSEG'.

LS_FIELDCAT-NO_OUT = 'X'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BSEG-WRBTR'. "Amt in Doc Currency

ls_fieldcat-ref_fieldname = 'WRBTR'.

ls_fieldcat-ref_tabname = 'BSEG'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BKPF-WAERS'. "Currency Key

ls_fieldcat-ref_fieldname = 'WAERS'.

ls_fieldcat-ref_tabname = 'BKPF'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BKPF-KURSF'. "Exchange Rate

ls_fieldcat-ref_fieldname = 'KURSF'.

ls_fieldcat-ref_tabname = 'BKPF'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BSEG-DMBTR'. "Amt in Local Currency

ls_fieldcat-ref_fieldname = 'DMBTR'.

ls_fieldcat-ref_tabname = 'BSEG'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BSEG-GJAHR'. "Fiscal Year

ls_fieldcat-ref_fieldname = 'GJAHR'.

ls_fieldcat-ref_tabname = 'BSEG'.

LS_FIELDCAT-NO_OUT = 'X'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BSEG-BUZEI'. "Doc Line #

ls_fieldcat-ref_fieldname = 'BUZEI'.

ls_fieldcat-ref_tabname = 'BSEG'.

LS_FIELDCAT-NO_OUT = 'X'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BSEG-BSCHL'. "Posting Key

ls_fieldcat-ref_fieldname = 'BSCHL'.

ls_fieldcat-ref_tabname = 'BSEG'.

LS_FIELDCAT-NO_OUT = 'X'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

* Cost Center is not needed for cash posting - only expenses.

*

* ls_fieldcat-col_pos = pos.

* ls_fieldcat-fieldname = 'BSEG-KOSTL'. "Cost Center

* ls_fieldcat-ref_fieldname = 'KOSTL'.

* ls_fieldcat-ref_tabname = 'BSEG'.

* APPEND ls_fieldcat TO rt_fieldcat.

* CLEAR ls_fieldcat.

* pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BKPF-BUKRS'. "Company Code

ls_fieldcat-ref_fieldname = 'BUKRS'.

ls_fieldcat-ref_tabname = 'BKPF'.

LS_FIELDCAT-NO_OUT = 'X'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BKPF-BLART'. "Document Type

ls_fieldcat-ref_fieldname = 'BLART'.

ls_fieldcat-ref_tabname = 'BKPF'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BSEG-SGTXT'. "Item Text

ls_fieldcat-ref_fieldname = 'SGTXT'.

ls_fieldcat-ref_tabname = 'BSEG'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

pos = pos + 1.

ls_fieldcat-col_pos = pos.

ls_fieldcat-fieldname = 'BKPF-USNAM'. "User name

ls_fieldcat-ref_fieldname = 'USNAM'.

ls_fieldcat-ref_tabname = 'BKPF'.

LS_FIELDCAT-NO_OUT = 'X'.

APPEND ls_fieldcat TO rt_fieldcat.

CLEAR ls_fieldcat.

ENDFORM. "fieldcat_init

* Data selection

FORM select_data TABLES rt_outtab LIKE gt_outtab[].

DATA: l_name LIKE tline-tdline.

* IF s_hkont IS INITIAL.

SELECT bukrs belnr blart budat usnam xblnr waers kursf INTO

CORRESPONDING FIELDS OF TABLE

i_bkpf FROM bkpf WHERE belnr IN s_belnr

AND blart IN s_blart

AND budat IN s_budat.

LOOP AT i_bkpf.

SELECT hkont kostl belnr gjahr buzei bschl shkzg dmbtr wrbtr sgtxt

kunnr INTO (bseg-hkont, bseg-kostl, bseg-belnr, bseg-gjahr,

bseg-buzei, bseg-bschl, bseg-shkzg, bseg-dmbtr,

bseg-wrbtr, bseg-sgtxt, bseg-kunnr) FROM bseg

WHERE belnr EQ i_bkpf-belnr.

CLEAR: KNA1, skat.

SELECT SINGLE * FROM skat WHERE SPRAS EQ SY-LANGU

AND KTOPL EQ '7100'

AND SAKNR = bseg-hkont.

"ska1, skb1 account # stored in SAKNR.

"in BSEG, account # stored in HKONT.

if bseg-SHKZG eq 'H'.

multiply : bseg-wrbtr by -1,

bseg-dmbtr by -1.

endif.

MOVE-CORRESPONDING: i_bkpf TO rt_outtab-bkpf,

bseg TO rt_outtab-bseg,

skat TO rt_outtab-skat.

SELECT SINGLE name1 INTO rt_outtab-kna1-name1 FROM KNA1

WHERE kunnr = rt_outtab-bseg-kunnr.

APPEND rt_outtab.

CLEAR: rt_outtab.

ENDSELECT.

ENDLOOP.

ENDFORM. " select_data

No comments:

Blog Archive