Sie sind auf Seite 1von 9

REPORT zfnclr_gl_extraction

NO STANDARD PAGE HEADING


MESSAGE-ID zprs.

*----------------------------------------------------------------------*
* Date:
12/08/2014
*
* Author:
Mahmood ul Haque
*
* Title:
Extraction of document balance (Open Item and
*
*
Balances) data.
*
* Transaction Code: ZFNCL_GLEXTRC
*
* ADR Name:
*
*------------|------------!-------------|------------------------------*
* Transport# | Date
| Responsible | Description
*------------|------------|-------------|------------------------------*
* D21K999017 | 12/08/2014 | HAQUEMA
| Initial Program Creation
*
*
|
|
|
*
*------------|------------|-------------|------------------------------*
*----------------------------------------------------------------------*
* Tables updated:
*
*
*
* External files: Download data to an excel file in prresentation
*
*
server.
*
* Input parameters: Company Code
*
*
Fiscal year
*
*
Posting Date
*
*
Document Type
*
*
GL Account Number
*
*
*
* Output parameters:
*
*
*
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* Include
*
*----------------------------------------------------------------------*
INCLUDE zfncln_gl_extrac_top.
*----------------------------------------------------------------------*
* Initialization
*
*----------------------------------------------------------------------*
INITIALIZATION.
* Check the Authorization to execute the Transaction Code
AUTHORITY-CHECK OBJECT 'S_TCODE'
ID 'TCD' FIELD 'ZFNCL_GLEXTRC'.
IF sy-subrc NE 0.
MESSAGE e000 WITH text-028 'ZFNCL_GLEXTRC'.
ENDIF.
*----------------------------------------------------------------------*
* At Selection Screen
*
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
* Validation check for Company Code
PERFORM f_validation_check.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM f_get_file_name.

*----------------------------------------------------------------------*
* Start-of-Selection
*
*----------------------------------------------------------------------*
START-OF-SELECTION.
* Fetch data from BSIS and BKPF table
PERFORM f_get_data CHANGING gt_bsis
gt_bkpf.
*----------------------------------------------------------------------*
* End-of-Selection
*
*----------------------------------------------------------------------*
END-OF-SELECTION.
* Populate the Final table
PERFORM f_populate_final USING
gt_bsis
gt_bkpf
CHANGING gt_final.
* Download the Final data populated in final table to an Excel file
PERFORM f_excel_download USING gt_final.
* Refresh the table used in this program
PERFORM f_refresh_table.
*----------------------------------------------------------------------*
* FORMS
*
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&
Form F_GET_DATA
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
<--P_IT_BSIS text
*
<--P_IT_BKPF text
*----------------------------------------------------------------------*
FORM f_get_data
CHANGING pc_gt_bsis TYPE tt_bsis
pc_gt_bkpf TYPE tt_bkpf.
* Fetch data from table BSIS for user input is selection screen
SELECT bukrs
"Company Code
hkont
"General Ledger Account
gjahr
"Fiscal Year
belnr
"Accounting Document Number
budat
"Posting Date in the Document
waers
"Currency Key
blart
"Document Type
monat
"Fiscal Period
bschl
"Posting key
shkzg
"Debit/Credit Indicator
dmbtr
"Amount in Local Currency
wrbtr
"Amount in document currency
sgtxt
"Item Text
aufnr
"Order Number
kostl
"Cost Center
vbund
"Company ID of trading partner
projk
"WBS Element
prctr
"Profit Center
dmbe2
"Amount in Second Local Currency
bewar
"Transaction Type
FROM bsis
INTO TABLE pc_gt_bsis
WHERE bukrs IN s_bukrs
AND hkont IN s_hkont

AND
AND
AND

gjahr = p_gjahr
budat IN s_budat
blart IN s_blart.

IF sy-subrc = 0.
SORT pc_gt_bsis BY bukrs belnr gjahr.
* Get the unique Company code, Doc No. and fiscla year to fetch the
* Accounting header data from BKPF
gt_bsis_t[] = pc_gt_bsis[].
SORT gt_bsis_t BY bukrs belnr gjahr.
DELETE ADJACENT DUPLICATES FROM gt_bsis_t
COMPARING bukrs
belnr
gjahr.
* Fetch data from table BKPF
SELECT bukrs
"Company Code
belnr
"Accounting Document Number
gjahr
"Fiscal Year
hwaer
"Local Currency
FROM bkpf
INTO TABLE pc_gt_bkpf
FOR ALL ENTRIES IN gt_bsis_t
WHERE bukrs = gt_bsis_t-bukrs
AND belnr = gt_bsis_t-belnr
AND gjahr = gt_bsis_t-gjahr.
IF sy-subrc = 0.
SORT pc_gt_bkpf BY bukrs belnr gjahr.
ENDIF.
REFRESH gt_bsis_t.
ELSE.
MESSAGE i000 WITH 'No Data Found'(025).
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM.
" F_GET_BSIS_DATA
*&---------------------------------------------------------------------*
*&
Form F_POPULATE_FINAL
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_IT_BSIS text
*
-->P_IT_BKPF text
*
<--P_IT_FINAL text
*----------------------------------------------------------------------*
FORM f_populate_final USING
pv_gt_bsis TYPE tt_bsis
pv_gt_bkpf TYPE tt_bkpf
CHANGING pc_gt_final TYPE tt_final.
TYPES: BEGIN OF lty_ska1,
saknr TYPE saknr, "G/L Account Number
ktopl TYPE ktopl, "Chart of Accounts
xbilk TYPE
xbilk, "Indicator:balance sheet account
END OF lty_ska1,
BEGIN OF lty_skb1,
bukrs TYPE
bukrs, "Company Code

saknr TYPE saknr, "G/L Account Number


xopvw TYPE
xopvw, "Indicator: Open item management?
END OF lty_skb1.
DATA: lt_ska1 TYPE STANDARD TABLE OF lty_ska1,
lt_skb1 TYPE STANDARD TABLE OF lty_skb1,
lt_skb1_t TYPE STANDARD TABLE OF lty_skb1.
DATA: ls_bsis TYPE ty_bsis,
ls_bkpf TYPE ty_bkpf,
ls_final TYPE ty_final,
ls_skb1 TYPE lty_skb1,
ls_ska1 TYPE lty_ska1.
CONSTANTS: lc_usd TYPE waers VALUE 'USD',
lc_coa1 TYPE ktopl VALUE 'COA1'.
* Pass the BSIS table data to temp table
gt_bsis_t[] = pv_gt_bsis[].
SORT gt_bsis_t BY bukrs hkont.
DELETE ADJACENT DUPLICATES FROM gt_bsis_t
COMPARING bukrs
hkont.
* Fetch data from table SKB1
SELECT bukrs
"Company Code
saknr
"G/L Account Number
xopvw
"Indicator: Open item management?
FROM skb1
INTO TABLE lt_skb1
FOR ALL ENTRIES IN gt_bsis_t
WHERE bukrs = gt_bsis_t-bukrs
AND saknr = gt_bsis_t-hkont.
IF sy-subrc = 0.
SORT lt_skb1 BY bukrs saknr.
lt_skb1_t[] = lt_skb1[].
SORT lt_skb1_t BY saknr.
DELETE ADJACENT DUPLICATES FROM lt_skb1_t
COMPARING saknr.
* Fetch chart of account data from table SKA1
SELECT saknr
"G/L Account Number
ktopl
"Chart of Accounts
xbilk
"Indicator:balance sheet account
FROM ska1
INTO TABLE lt_ska1
FOR ALL ENTRIES IN lt_skb1_t
WHERE saknr = lt_skb1_t-saknr.
IF sy-subrc = 0.
SORT lt_ska1 BY saknr ktopl.
ENDIF.
ENDIF.

LOOP AT pv_gt_bsis INTO ls_bsis.


Check teh GL account are open item managed or not
READ TABLE lt_skb1 INTO ls_skb1 WITH KEY
bukrs = ls_bsis-bukrs
saknr = ls_bsis-hkont

BINARY SEARCH.
IF sy-subrc = 0.
IF ls_skb1-xopvw = abap_true.
Check the Gl account are balance sheet account or not
READ TABLE lt_ska1 INTO ls_ska1 WITH KEY
saknr = ls_bsis-hkont
ktopl = lc_coa1
BINARY SEARCH.
IF sy-subrc = 0.
ls_final-bukrs
ls_final-belnr
ls_final-gjahr
ls_final-budat
ls_final-monat
ls_final-blart
ls_final-hkont
ls_final-ktopl
ls_final-vbund
ls_final-prctr
ls_final-projk
ls_final-aufnr
ls_final-kostl
ls_final-bewar
ls_final-sgtxt
ls_final-waers
ls_final-wrbtr
ls_final-gr_curr
ls_final-dmbe2
ls_final-dmbtr
ls_final-shkzg
ls_final-bschl

=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=

ls_bsis-bukrs .
ls_bsis-belnr .
ls_bsis-gjahr .
ls_bsis-budat .
ls_bsis-monat .
ls_bsis-blart .
ls_bsis-hkont .
ls_ska1-ktopl.
ls_bsis-vbund .
ls_bsis-prctr .
ls_bsis-projk .
ls_bsis-aufnr .
ls_bsis-kostl .
ls_bsis-bewar .
ls_bsis-sgtxt .
ls_bsis-waers .
ls_bsis-wrbtr .
lc_usd.
ls_bsis-dmbe2 .
ls_bsis-dmbtr .
ls_bsis-shkzg .
ls_bsis-bschl .

READ TABLE pv_gt_bkpf INTO ls_bkpf WITH KEY


bukrs = ls_bsis-bukrs
belnr = ls_bsis-belnr
gjahr = ls_bsis-gjahr
BINARY SEARCH.
IF sy-subrc = 0.
ls_final-hwaer
= ls_bkpf-hwaer.
ENDIF.
APPEND ls_final TO pc_gt_final.
ENDIF.
ENDIF.
ENDIF.
CLEAR: ls_bsis,
ls_bkpf,
ls_final,
ls_ska1,
ls_skb1.
ENDLOOP.
IF pc_gt_final[] IS INITIAL.
MESSAGE i000 WITH 'No Data Found'(025).
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM.

" F_POPULATE_FINAL

*&---------------------------------------------------------------------*
*&
Form F_EXCEL_DOWNLOAD
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_IT_FINAL text
*----------------------------------------------------------------------*
FORM f_excel_download USING
pv_gt_final TYPE tt_final.
TYPES: BEGIN
txt1
txt2
txt3
txt4
txt5
txt6
txt7
txt8
txt9
txt10
txt11
txt12
txt13
txt14
txt15
txt16
txt17
txt18
txt19
txt20
txt21
txt22
txt23
END OF

OF lty_excel,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
TYPE string,
lty_excel.

DATA: lt_body TYPE STANDARD TABLE OF lty_excel,


ls_body TYPE lty_excel,
ls_final TYPE ty_final.
*
*
*
*

DATA: lv_fullpath TYPE string.


lv_filename TYPE string,
lv_path
TYPE string,
lv_pat
TYPE string,
lv_result TYPE i.

" Full File path


" File Name
" File Path
" File Path
" Result

* CONSTANTS: lc_xls TYPE string VALUE 'XLS',


*
lc_temp TYPE string VALUE 'c:\temp\'.
* Create the file header
ls_body-txt1 = 'Company Code'(001).
ls_body-txt2 = 'Document Number'(002).
ls_body-txt3 = 'Year'(003).
ls_body-txt4 = 'Posting Date'(004).
ls_body-txt5 = 'Period'(005).
ls_body-txt6 = 'Document Type'(006).
ls_body-txt7 = 'Currency'(007).
ls_body-txt8 = 'Posting Key'(008).
ls_body-txt9 = 'GL Account Number'(009).
ls_body-txt10 = 'Dr Cr Indicator'(010).
ls_body-txt11 = 'Trading Partner'(011).

ls_body-txt12
ls_body-txt13
ls_body-txt14
ls_body-txt15
ls_body-txt16
ls_body-txt17
ls_body-txt18
ls_body-txt19
ls_body-txt20
ls_body-txt21
ls_body-txt22
ls_body-txt23

=
=
=
=
=
=
=
=
=
=
=
=

'Profit Center'(012).
'WBS Element'(013).
'Internal Order'(014).
'Cost Center'(015).
'Transaction Type'(016).
'Short Text'(017).
'Amount'(018).
'Group Currency'(019).
'Amount in Group Currency'(020).
'Local Currency Code'(021).
'Local Currency Amount'(022).
'Chart of Account'(026).

APPEND ls_body TO lt_body.


CLEAR ls_body.
* Populate the body of the excel file
LOOP AT pv_gt_final INTO ls_final.
ls_body-txt1 = ls_final-bukrs .
ls_body-txt2 = ls_final-belnr .
ls_body-txt3 = ls_final-gjahr .
ls_body-txt4 = ls_final-budat .
ls_body-txt5 = ls_final-monat .
ls_body-txt6 = ls_final-blart .
ls_body-txt7 = ls_final-waers .
ls_body-txt8 = ls_final-bschl .
ls_body-txt9 = ls_final-hkont .
ls_body-txt10 = ls_final-shkzg .
ls_body-txt11 = ls_final-vbund .
ls_body-txt12 = ls_final-prctr .
ls_body-txt13 = ls_final-projk .
ls_body-txt14 = ls_final-aufnr .
ls_body-txt15 = ls_final-kostl .
ls_body-txt16 = ls_final-bewar .
ls_body-txt17 = ls_final-sgtxt .
ls_body-txt18 = ls_final-wrbtr .
ls_body-txt19 = ls_final-gr_curr .
ls_body-txt20 = ls_final-dmbe2 .
ls_body-txt21 = ls_final-hwaer .
ls_body-txt22 = ls_final-dmbtr .
ls_body-txt23 = ls_final-ktopl.
APPEND ls_body TO lt_body.
CLEAR: ls_body,
ls_final.
ENDLOOP.
* CONCATENATE 'GL Balance'(023) sy-datum sy-uzeit
*
INTO lv_path.
*
** Display save dialog window
* CALL METHOD cl_gui_frontend_services=>file_save_dialog
*
EXPORTING
*
default_extension = lc_xls
*
default_file_name = lv_path
*
initial_directory = lc_temp
*
CHANGING
*
filename
= lv_filename
*
path
= lv_path
*
fullpath
= lv_fullpath

*
user_action
= lv_result.
*
* CHECK lv_result = 0.
* Pass the file path to a string
lv_fullpath = p_file.
* Download the file into excel
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename
= lv_fullpath "lv_pat
filetype
= 'ASC'
write_field_separator = abap_true
TABLES
data_tab
= lt_body
EXCEPTIONS
file_write_error
= 1
no_batch
= 2
gui_refuse_filetransfer = 3
invalid_type
= 4
no_authority
= 5
unknown_error
= 6
header_not_allowed
= 7
separator_not_allowed = 8
filesize_not_allowed
= 9
header_too_long
= 10
dp_error_create
= 11
dp_error_send
= 12
dp_error_write
= 13
unknown_dp_error
= 14
access_denied
= 15
dp_out_of_memory
= 16
disk_full
= 17
dp_timeout
= 18
file_not_found
= 19
dataprovider_exception = 20
control_flush_error
= 21
OTHERS
= 22.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
MESSAGE s000 WITH 'Data loaded sucessfully'(024).
ENDIF.
REFRESH: lt_body.
ENDFORM.
" F_EXCEL_DOWNLOAD
*&---------------------------------------------------------------------*
*&
Form F_VALIDATION_CHECK
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_validation_check .
DATA: lv_bukrs TYPE bukrs. "Company Code
* Validate Company code
SELECT SINGLE bukrs

INTO lv_bukrs
FROM t001
WHERE bukrs IN s_bukrs.
IF lv_bukrs IS INITIAL.
MESSAGE e000 WITH 'Please Provide a valid Company Code'(027).
ENDIF.
CLEAR lv_bukrs.
ENDFORM.
" F_VALIDATION_CHECK
*&---------------------------------------------------------------------*
*&
Form F_REFRESH_TABLE
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_refresh_table .
REFRESH: gt_bsis,
gt_bkpf,
gt_final,
gt_bsis_t.
ENDFORM.
" F_REFRESH_TABLE
*&---------------------------------------------------------------------*
*&
Form F_GET_FILE_NAME
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_get_file_name .
* Call Function module to get the File name
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
*
FIELD_NAME
= ' '
IMPORTING
file_name
= p_file.
ENDFORM.

" F_GET_FILE_NAME