Powered By

Free XML Skins for Blogger

Powered by Blogger

Wednesday, February 4, 2009

SAP ABAP program OR implementation: Accounting Docs Automatically Created from MM

Some poor souls in your finance department might use this report for auditing...

This program lists accounting documents that were automatically created from MM, and also lists the MM source documents. It can take several minutes to run, depending on the amount of data in the database, because it has to access (slow) cluster tables.

Accounting Docs Automatically Created from MM


*&---------------------------------------------------------------------*
*& Report ZFIMMDOC *
*& Listing of accounting docs and the material docs from which they *
*& were automatically created. *
*&---------------------------------------------------------------------*
REPORT ZFIMMDOC MESSAGE-ID Z1 LINE-COUNT 65 NO STANDARD PAGE HEADING.
TABLES: BKPF, MSEG, MKPF, T001, BSIM, T001K, MARA, BSEG, SKAT,
MAKT, T156T.
INCLUDE: ZFIMMTOP. "types mymseg_type

PARAMETERS: COMPANYC LIKE BKPF-BUKRS OBLIGATORY, " default '1101',
YEAR LIKE BKPF-GJAHR OBLIGATORY, " default '1998'.
POSTDATE LIKE BKPF-BUDAT OBLIGATORY.
SELECT-OPTIONS: VAL_AREA FOR BSIM-BWKEY NO-DISPLAY,
S_MBLNR FOR MSEG-MBLNR NO-DISPLAY,
S_BELNR FOR BSEG-BELNR NO-DISPLAY.

DATA: MYBUTXT LIKE T001-BUTXT,
LINES TYPE I,
PREVIOUS_BELNR LIKE BKPF-BELNR,
PREVIOUS_AWKEY LIKE BKPF-AWKEY,
MYTXT50 LIKE SKAT-TXT50,
BEGIN OF X001K OCCURS 20.
INCLUDE STRUCTURE T001K.
DATA: END OF X001K.
DATA: BEGIN OF MYBKPF OCCURS 100,
BLART LIKE BKPF-BLART, "doc type
BELNR LIKE BKPF-BELNR, "doc no
WAERS LIKE BKPF-WAERS, "currency key
AWKEY LIKE BKPF-AWKEY,
END OF MYBKPF.
DATA: BEGIN OF MYBSIM OCCURS 500.
INCLUDE TYPE MYBSIM_TYPE.
DATA: END OF MYBSIM.
DATA: BEGIN OF MYBSEG OCCURS 1200,
BELNR LIKE BSEG-BELNR,
BUZEI LIKE BSEG-BUZEI,
BSCHL LIKE BSEG-BSCHL,
HKONT LIKE BSEG-HKONT,
WRBTR LIKE BSEG-WRBTR,
SHKZG LIKE BSEG-SHKZG, "debit/credit
END OF MYBSEG.
DATA: BEGIN OF MYMSEG OCCURS 500.
INCLUDE TYPE MYMSEG_TYPE.
DATA: END OF MYMSEG.

PERFORM GET_BKPF.
PERFORM GET_BSIM.
PERFORM COLLECT_BELNR.
PERFORM GET_BSEG.
PERFORM COLLECT_MBLNR.
PERFORM GET_MSEG.

SORT MYBSIM BY BELNR.
SORT MYMSEG BY MBLNR ZEILE.
SORT MYBSEG BY BELNR BUZEI.
LOOP AT MYBSIM.
IF MYBSIM-SHKZG = 'H'. "negative value
MYBSIM-DMBTR = ( -1 ) * MYBSIM-DMBTR.
ENDIF.
PERFORM WRITE_BSIM_DATA USING MYBSIM.
AT END OF BELNR. "new belnr record: get bseg recs
PERFORM WRITE_BSEG_DATA USING MYBSIM-BELNR.
ENDAT.
AT END OF AWKEY.
PERFORM WRITE_MSEG_DATA USING MYBSIM-AWKEY(10).
ENDAT.
WRITE: SY-ULINE.
ENDLOOP.
WRITE: / 'number of records accessed'.
DESCRIBE TABLE MYBKPF LINES LINES.
WRITE: / 'bkpf:', LINES.
DESCRIBE TABLE MYBSIM LINES LINES.
WRITE: / 'bsim:', LINES.
DESCRIBE TABLE MYMSEG LINES LINES.
WRITE: / 'mseg:', LINES.
DESCRIBE TABLE MYBSEG LINES LINES.
WRITE: / 'bseg:', LINES.

AT SELECTION-SCREEN.
* get company text
SELECT SINGLE BUTXT FROM T001 INTO MYBUTXT
WHERE BUKRS = COMPANYC.
* get valuation areas from company code
REFRESH VAL_AREA. CLEAR VAL_AREA.
SELECT * FROM T001K INTO TABLE X001K
WHERE BUKRS = COMPANYC.
LOOP AT X001K.
VAL_AREA-SIGN = 'I'. VAL_AREA-OPTION = 'EQ'.
VAL_AREA-LOW = X001K-BWKEY.
APPEND VAL_AREA.
ENDLOOP.
READ TABLE VAL_AREA INDEX 1.

TOP-OF-PAGE.
PERFORM WRITE_HEADER.

*&---------------------------------------------------------------------*
*& Form GET_BKPF
*&---------------------------------------------------------------------*
FORM GET_BKPF.
* get bkpf records
* should perform authority check here **
SELECT BLART BELNR WAERS AWKEY
INTO CORRESPONDING FIELDS OF TABLE MYBKPF
FROM BKPF
WHERE BUKRS = COMPANYC AND GJAHR = YEAR AND BUDAT = POSTDATE
ORDER BY BELNR.
ENDFORM. " GET_BKPF

*&---------------------------------------------------------------------*
*& Form GET_BSIM
*----------------------------------------------------------------------*
FORM GET_BSIM.
LOOP AT MYBKPF.
MYBSIM-BLART = MYBKPF-BLART.
MYBSIM-BELNR = MYBKPF-BELNR.
MYBSIM-WAERS = MYBKPF-WAERS.
MYBSIM-AWKEY = MYBKPF-AWKEY.
* should perform authority check here **
* select bsim records using conditions gjahr, bwkey, belnr
SELECT BELNR BUZEI DMBTR MATNR BUDAT MENGE MEINS
INTO CORRESPONDING FIELDS OF MYBSIM
FROM BSIM
WHERE GJAHR = YEAR AND
BELNR = MYBKPF-BELNR AND
BWKEY IN VAL_AREA.
APPEND MYBSIM.
ENDSELECT.
ENDLOOP.
ENDFORM. " GET_BSIM

*&---------------------------------------------------------------------*
*& Form get_bseg
*&---------------------------------------------------------------------*
FORM GET_BSEG.
* should perform authority check here **
* select bseg records using conditions bukrs, belnr, gjahr
SELECT BELNR BUZEI BSCHL HKONT WRBTR SHKZG
INTO CORRESPONDING FIELDS OF TABLE MYBSEG
FROM BSEG
WHERE GJAHR = YEAR AND
BUKRS = COMPANYC AND
BELNR IN S_BELNR.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form GET_MSEG
*&---------------------------------------------------------------------*
FORM GET_MSEG.
* should perform authority check here **
SELECT MBLNR
ZEILE BWART MATNR LGORT MENGE MEINS WAERS SHKZG DMBTR WERKS
KOSTL SOBKZ KZBEW KZZUG KZVBR
INTO CORRESPONDING FIELDS OF TABLE MYMSEG
FROM MSEG
WHERE MJAHR = YEAR AND MBLNR IN S_MBLNR.
WRITE: SY-ULINE.
ENDFORM. " GET_AND_WRITE_MATDOC
*&---------------------------------------------------------------------*
*& Form WRITE_HEADER
*----------------------------------------------------------------------*
FORM WRITE_HEADER.
DATA: MYOPTION(11) TYPE C, MYSIGN(7) TYPE C, "explain select criteria
SELECT_TEXT(60) TYPE C, SELECT_POSITION TYPE I.
WRITE: TEXT-000,
TEXT-001. "company and report header
IF SY-PAGNO = 1. "**** first page
WRITE: / 'Company Code', COMPANYC, MYBUTXT,
'Accounting Year', YEAR, 'Posting Date:', POSTDATE.
ENDIF. "**** first page
WRITE: / 'Generated', SY-DATUM, SY-UZEIT,
'Page', SY-PAGNO, SY-ULINE.
* format for accounting docs
FORMAT COLOR COL_GROUP.
WRITE: / 'DT',
4 'acct doc',
15 'material',
25 'item',
31 'Posted',
42(12) 'Quantity',
'Unit',
61 ' balance'.
FORMAT COLOR OFF.
WRITE: / 'item' UNDER 'item', 30 'Code', 35 'Account',
45 'Description',
61 ' balance'.
* format for material docs
FORMAT COLOR COL_TOTAL.
WRITE: / 'MatrialDoc' UNDER 'acct doc',
'Posted' UNDER 'Posted', 'Entered' UNDER 'Description',
76 ' '.
FORMAT COLOR OFF.
WRITE: /10 'item',
'Material' UNDER 'Material',
25 'Plant',
'Stor' UNDER 'Code',
'Mvmt' UNDER 'Account',
42(12) 'Quantity',
'Unit',
61 ' balance',
/11 'MaterialDesc',
'MovementType' UNDER 'Quantity',
60 'CostCtr'.
WRITE: SY-ULINE.
ENDFORM. " WRITE_HEADER
*&---------------------------------------------------------------------*
*& Form WRITE_MKPF_DATA
*&---------------------------------------------------------------------*
FORM WRITE_MKPF_DATA USING F_MSEG TYPE MYMSEG_TYPE
F_MAKTX LIKE MAKT-MAKTX
F_BTEXT LIKE T156T-BTEXT.
WRITE: / F_MSEG-ZEILE UNDER 'item',
F_MSEG-MATNR UNDER 'material',
F_MSEG-WERKS UNDER 'Plant',
F_MSEG-LGORT UNDER 'Stor',
F_MSEG-BWART UNDER 'Mvmt',
(12)F_MSEG-MENGE UNDER 'Quantity' DECIMALS 2,
F_MSEG-MEINS UNDER 'Unit',
F_MSEG-DMBTR UNDER ' balance'.
WRITE: / F_MAKTX UNDER 'MaterialDesc',
F_BTEXT UNDER 'MovementType',
F_MSEG-KOSTL UNDER 'CostCtr'.
ENDFORM. " WRITE_MKPF_DATA

*&---------------------------------------------------------------------*
*& Form WRITE_MKPF_HEADER
*&---------------------------------------------------------------------*
FORM WRITE_MKPF_HEADER USING P_MBLNR LIKE MKPF-MBLNR.
DATA: MYBUDAT LIKE MKPF-BUDAT, MYCPUDT LIKE MKPF-CPUDT.
SELECT SINGLE BUDAT CPUDT FROM MKPF
INTO (MYBUDAT, MYCPUDT)
WHERE MBLNR = P_MBLNR AND MJAHR = YEAR.
IF SY-SUBRC = 0. "got record
FORMAT COLOR COL_TOTAL.
WRITE: / P_MBLNR UNDER 'acct doc',
MYBUDAT UNDER 'Posted',
MYCPUDT UNDER 'Entered',
76 ' '.
FORMAT COLOR OFF.
ENDIF.
ENDFORM. " WRITE_MKPF_HEADER
*&---------------------------------------------------------------------*
*& Form WRITE_BSIM_DATA
*&---------------------------------------------------------------------*
FORM WRITE_BSIM_DATA USING F_BSIM TYPE MYBSIM_TYPE.
FORMAT COLOR COL_GROUP.
WRITE: / F_BSIM-BLART UNDER 'DT',
F_BSIM-BELNR UNDER 'acct doc',
F_BSIM-MATNR UNDER 'material',
F_BSIM-BUZEI UNDER 'Plant', "item
F_BSIM-BUDAT UNDER 'Posted',
(12)F_BSIM-MENGE UNDER 'Quantity' DECIMALS 2,
F_BSIM-MEINS UNDER 'Unit',
F_BSIM-DMBTR UNDER ' balance' RIGHT-JUSTIFIED.
FORMAT COLOR OFF.
ENDFORM. " WRITE_BSIM_DATA

*&---------------------------------------------------------------------*
*& Form WRITE_BSEG_DATA
*&---------------------------------------------------------------------*
FORM WRITE_BSEG_DATA USING P_BELNR.
LOOP AT MYBSEG WHERE BELNR = P_BELNR.
SELECT SINGLE TXT50 INTO MYTXT50 FROM SKAT
WHERE SPRAS = '1' AND KTOPL = 'HFCA' AND
SAKNR = MYBSEG-HKONT.
IF MYBSEG-SHKZG = 'H'. "negative value
MYBSEG-WRBTR = ( -1 ) * MYBSEG-WRBTR.
ENDIF.
WRITE: / MYBSEG-BUZEI UNDER 'Plant', "item
MYBSEG-BSCHL UNDER 'Code',
MYBSEG-HKONT UNDER 'Account',
MYTXT50 UNDER 'Description',
MYBSEG-WRBTR UNDER ' balance'.
ENDLOOP.
ENDFORM. " WRITE_BSEG_DATA
*&---------------------------------------------------------------------*
*& Form COLLECT_MBLNR
*&---------------------------------------------------------------------*
FORM COLLECT_MBLNR.
SORT MYBSIM BY AWKEY.
S_MBLNR-SIGN = 'I'.
S_MBLNR-OPTION = 'EQ'.
LOOP AT MYBSIM.
AT NEW AWKEY.
S_MBLNR-LOW = MYBSIM-AWKEY(10).
APPEND S_MBLNR.
ENDAT.
ENDLOOP.
READ TABLE S_MBLNR INDEX 1.
ENDFORM. " COLLECT_MBLNR
*&---------------------------------------------------------------------*
*& Form COLLECT_BELNR
*&---------------------------------------------------------------------*
FORM COLLECT_BELNR.
S_BELNR-SIGN = 'I'.
S_BELNR-OPTION = 'EQ'.
SORT MYBSIM BY BELNR.
LOOP AT MYBSIM.
AT NEW BELNR.
S_BELNR-LOW = MYBSIM-BELNR.
APPEND S_BELNR.
ENDAT.
ENDLOOP.
READ TABLE S_BELNR INDEX 1.
ENDFORM. " COLLECT_BELNR
*&---------------------------------------------------------------------*
*& Form WRITE_MSEG_DATA
*&---------------------------------------------------------------------*
FORM WRITE_MSEG_DATA USING P_MBLNR LIKE MSEG-MBLNR.
DATA: MYBTEXT LIKE T156T-BTEXT,
MYMAKTX LIKE MAKT-MAKTX.
PERFORM WRITE_MKPF_HEADER USING P_MBLNR.
LOOP AT MYMSEG WHERE MBLNR = P_MBLNR.
SELECT SINGLE BTEXT INTO MYBTEXT FROM T156T
WHERE SPRAS = '1' AND
BWART = MYMSEG-BWART AND
SOBKZ = MYMSEG-SOBKZ AND
KZBEW = MYMSEG-KZBEW AND
KZZUG = MYMSEG-KZZUG AND
KZVBR = MYMSEG-KZVBR.
SELECT SINGLE MAKTX INTO MYMAKTX FROM MAKT
WHERE SPRAS = '1' AND MATNR = MYMSEG-MATNR.
IF MYMSEG-SHKZG = 'H'.
MYMSEG-DMBTR = ( -1 ) * MYMSEG-DMBTR.
ENDIF.
PERFORM WRITE_MKPF_DATA USING MYMSEG MYMAKTX MYBTEXT.
ENDLOOP.
ENDFORM. " WRITE_MSEG_DATA


* end of ZFIMMDOC program *

* begin of ZFIMMTOP include program *
*----------------------------------------------------------------------*
* INCLUDE ZFIMMTOP *
*----------------------------------------------------------------------*
TYPES: BEGIN OF MYMSEG_TYPE,
MBLNR LIKE MSEG-MBLNR,
ZEILE LIKE MSEG-ZEILE,
BWART LIKE MSEG-BWART,
MATNR LIKE MSEG-MATNR,
WERKS LIKE MSEG-WERKS,
LGORT LIKE MSEG-LGORT,
MENGE LIKE MSEG-MENGE,
MEINS LIKE MSEG-MEINS,
WAERS LIKE MSEG-WAERS,
SHKZG LIKE MSEG-SHKZG,
DMBTR LIKE MSEG-DMBTR,
KOSTL LIKE MSEG-KOSTL,
SOBKZ LIKE MSEG-SOBKZ, " for goods movement text
KZBEW LIKE MSEG-KZBEW, " for goods movement text
KZZUG LIKE MSEG-KZZUG, " for goods movement text
KZVBR LIKE MSEG-KZVBR, " for goods movement text
END OF MYMSEG_TYPE.
TYPES: BEGIN OF MYBSIM_TYPE,
BLART LIKE BKPF-BLART, "doc type
BELNR LIKE BKPF-BELNR, "doc no
WAERS LIKE BKPF-WAERS,
AWKEY LIKE BKPF-AWKEY,
BUZEI LIKE BSIM-BUZEI, "doc item
MATNR LIKE BSIM-MATNR,
BUDAT LIKE BSIM-BUDAT,
MENGE LIKE BSIM-MENGE, "quantity
MEINS LIKE BSIM-MEINS, "unit of measure
SHKZG LIKE BSIM-SHKZG, "debit/credit
DMBTR LIKE BSIM-DMBTR, "balance amount
END OF MYBSIM_TYPE.
* end of ZFIMMDOC include *

No comments:

Archives