Shared Top Border

Enterprise Resource
Planning Portal

 

Advertise | Founder BLOG

ERPGenie.COM ABAP Tips and Tricks Database

THE ultimate
ERP website

 

Forums | Vote for us |

Google    Other Search Options

Login

Login to view more content!!!





Lost Password?
No account yet? Register

Registered Access

Poll

What area of ABAP are you interested in?
 
Home arrow Sample Code arrow BDC arrow Batch Input: Post FI documents
Batch Input: Post FI documents PDF Print E-mail
User Rating: / 3
PoorBest 
Written by Anon.   
Tuesday, 20 March 2007
This report:
  1. Uploads an input file from presentation server (a tabs delimited text file);
  2. Processes all records with 'CALL TRANSACTION USING...' technique (transaction F-02);
  3. Creates a BDC session for records that were processed with an error;
  4. Displays statistics and errors report.
report z line-size 150
line-count 66
no standard page heading.
************************************************************************
* Program name: Post FI Documents
* Description : Upload input file from PC
* (1st line skipped as it is column headers)
* Post FI Documents via transaction F-02
************************************************************************

********* select-options and parameters ********************************
parameters:
p_bukrs like t001-bukrs obligatory,
p_budat like bkpf-budat obligatory,
p_blart like bkpf-blart obligatory,
p_file like rlgrap-filename lower case obligatory.
selection-screen skip.
parameters:
p_sess like d0100-mapn default sy-uname obligatory.

********* constants ****************************************************
constants: c_tcode like tstc-tcode value 'F-02'.

********* data: variables **********************************************
data: w_subrc like sy-subrc,
f_session,
f_session_created,
w_xblnr like bkpf-xblnr,
w_waers like bkpf-waers,
w_debit like bseg-wrbtr,
w_credit like bseg-wrbtr,
w_deb_tot like bseg-wrbtr,
w_cre_tot like bseg-wrbtr,
w_deb_bdc like bseg-wrbtr,
w_cre_bdc like bseg-wrbtr,
w_ndoc like sy-tabix,
w_ndoc_bdc like sy-tabix,
w_doc like bkpf-belnr.

********* data: internal tables, ranges ********************************
* input file
data: begin of it_file_up occurs 0,
bktxt(25) type c, "doc header text
xblnr(16) type c, "reference
bldat(10) type c, "doc date
waers(5) type c, "currency
newko(17) type c, "GL account
kostl(10) type c, "cost center
mwskz(2) type c, "tax code
deb_amt(16) type c, "debit amount
cre_amt(16) type c, "credit amount
end of it_file_up.
data: begin of it_file occurs 0,
bktxt like bkpf-bktxt, "doc header text
xblnr like bkpf-xblnr, "reference
bldat like bkpf-bldat, "doc date
waers like bkpf-waers, "currency
newko like rf05a-newko, "GL account
kostl like cobl-kostl, "cost center
mwskz like bseg-mwskz, "tax code
deb_amt like bseg-wrbtr, "debit amount
cre_amt like bseg-wrbtr, "credit amount
end of it_file.
data: it_doc like it_file occurs 0 with header line.


* BDC/Call transaction data.
data: begin of bdcdata occurs 0.
include structure bdcdata.
data: end of bdcdata.
data: messtab like bdcmsgcoll occurs 100 with header line.


************************************************
at selection-screen on value-request for p_file.
************************************************
perform get_file_name.

*******************
start-of-selection.
*******************
perform upload_file.
perform verify_file.
perform process_file.

*****************
end-of-selection.
*****************
skip.
write: / 'Total documents created:', 26 w_ndoc, 40
'Debit:', w_deb_tot currency w_waers,
'Credit:', w_cre_tot currency w_waers,
/ 'Total documents in BDC:', 26 w_ndoc_bdc, 40
'Debit:', w_deb_bdc currency w_waers,
'Credit:', w_cre_bdc currency w_waers.
add: w_ndoc_bdc to w_ndoc,
w_deb_bdc to w_deb_tot,
w_cre_bdc to w_cre_tot.
uline at (87).
write: / 'Grand total:', 26 w_ndoc, 40
'Debit:', w_deb_tot currency w_waers,
'Credit:', w_cre_tot currency w_waers.

skip.
if not f_session is initial.
write: / 'BDC session created:', p_sess.
endif.

*&---------------------------------------------------------------------*
*& Form get_file_name
*&---------------------------------------------------------------------*
* get PC file name
*----------------------------------------------------------------------*
form get_file_name.
data w_file like p_file.
call function 'WS_FILENAME_GET'
exporting
mask = ',*.txt ,*.TXT.'
importing
filename = w_file
exceptions
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
others = 5.
if sy-subrc = 0.
p_file = w_file.
endif.
endform. " get_file_name

*&---------------------------------------------------------------------*
*& Form upload_file
*&---------------------------------------------------------------------*
* upload PC file
*----------------------------------------------------------------------*
form upload_file.
call function 'WS_UPLOAD'
exporting
filename = p_file
filetype = 'DAT'
tables
data_tab = it_file_up
exceptions
conversion_error = 1
file_open_error = 2
file_read_error = 3
invalid_type = 4
no_batch = 5
unknown_error = 6
invalid_table_width = 7
gui_refuse_filetransfer = 8
customer_error = 9
others = 10.
if sy-subrc <> 0.
w_subrc = sy-subrc.
message e398(00) with 'Cannot upload file:' p_file
'Return code=' w_subrc.
endif.
delete it_file_up index 1.
if sy-subrc <> 0 or it_file_up[] is initial.
stop.
endif.
loop at it_file_up.
it_file-bktxt = it_file_up-bktxt.
it_file-xblnr = it_file_up-xblnr.
it_file-bldat+0(4) = it_file_up-bldat+6(4).
it_file-bldat+4(2) = it_file_up-bldat+0(2).
it_file-bldat+6(2) = it_file_up-bldat+3(2).
it_file-waers = it_file_up-waers.
it_file-newko = it_file_up-newko.
it_file-kostl = it_file_up-kostl.
it_file-mwskz = it_file_up-mwskz.
it_file-deb_amt = it_file_up-deb_amt.
it_file-cre_amt = it_file_up-cre_amt.
append it_file.
clear it_file.
endloop.
endform. " upload_file

*&---------------------------------------------------------------------*
*& Form verify_file
*&---------------------------------------------------------------------*
* verify debit/credit balance etc.
*----------------------------------------------------------------------*
form verify_file.
data w_waers like it_file-waers.
loop at it_file.
w_waers = it_file-waers.
if it_file-deb_amt <> 0 and it_file-cre_amt <> 0.
message e398(00) with 'Line' sy-tabix
'Both debit and credit amounts'.
endif.
if it_file-deb_amt = 0 and it_file-cre_amt = 0.
message e398(00) with 'Line' sy-tabix
'Zero debit and credit amounts'.
endif.
at last.
sum.
write: / 'Records uploaded:', 25 sy-tabix,
/ 'Total debit amount:', 25 it_file-deb_amt
currency w_waers,
/ 'Total credit amount:', 25 it_file-cre_amt
currency w_waers.
skip.
endat.
endloop.
endform. " verify_file

*&---------------------------------------------------------------------*
*& Form process_file
*&---------------------------------------------------------------------*
* process file
*----------------------------------------------------------------------*
form process_file.
sort it_file by bktxt xblnr.
loop at it_file.
at new xblnr.
it_doc[] = it_file[].
delete it_doc where xblnr <> it_file-xblnr.
perform process_one_doc.
endat.
endloop.
if f_session_created = 'X'.
perform bdc_close_session.
endif.
endform. " process_file

*&---------------------------------------------------------------------*
*& Form process_one_doc
*&---------------------------------------------------------------------*
* process one doc
*----------------------------------------------------------------------*
form process_one_doc.
data: i_curr like sy-tabix,
i_next like sy-tabix,
i_last like sy-tabix,
w_doc like it_doc,
w_amount like bseg-wrbtr.
describe table it_doc lines i_last.
w_debit = w_credit = 0.
clear: w_doc, bdcdata, messtab.
refresh: bdcdata, messtab.

loop at it_doc.
i_curr = sy-tabix.
w_doc = it_doc.
at first.
w_waers = w_doc-waers.
w_xblnr = w_doc-xblnr.
* doc header:
perform bdc_dynpro using 'SAPMF05A' '0100'.
perform bdc_field_w using 'BKPF-BLDAT' w_doc-bldat.
perform bdc_field using 'BKPF-BLART' p_blart.
perform bdc_field using 'BKPF-BUKRS' p_bukrs.
perform bdc_field_w using 'BKPF-BUDAT' p_budat.
perform bdc_field using 'BKPF-WAERS' w_doc-waers.
perform bdc_field using 'BKPF-XBLNR' w_doc-xblnr.
perform bdc_field using 'BKPF-BKTXT' w_doc-bktxt.
if w_doc-deb_amt <> 0.
perform bdc_field using 'RF05A-NEWBS' '40'.
else.
perform bdc_field using 'RF05A-NEWBS' '50'.
endif.
perform bdc_field using 'RF05A-NEWKO' w_doc-newko.
perform bdc_field using 'BDC_OKCODE' '/00'.
endat.
* doc item:
perform bdc_dynpro using 'SAPMF05A' '0300'.
if it_doc-deb_amt <> 0.
w_amount = abs( it_doc-deb_amt ).
add it_doc-deb_amt to w_debit.
else.
w_amount = abs( it_doc-cre_amt ).
add it_doc-cre_amt to w_credit.
endif.
perform bdc_field_s using 'BSEG-WRBTR' w_amount.
if not it_doc-mwskz is initial.
perform bdc_field using 'BSEG-MWSKZ' it_doc-mwskz.
endif.
if i_curr <> i_last.
* nex doc item - prepare account:
i_next = i_curr + 1.
clear w_doc.
read table it_doc into w_doc index i_next.
if sy-subrc = 0.
if w_doc-deb_amt <> 0.
perform bdc_field using 'RF05A-NEWBS' '40'.
else.
perform bdc_field using 'RF05A-NEWBS' '50'.
endif.
perform bdc_field using 'RF05A-NEWKO' w_doc-newko.
endif.
endif.
* more data:
perform bdc_field using 'BDC_OKCODE' '/00'.
perform bdc_dynpro using 'SAPLKACB' '0002'.
if it_doc-deb_amt <> 0.
perform bdc_field using 'COBL-KOSTL' it_doc-kostl.
endif.
perform bdc_field using 'BDC_OKCODE' '=ENTE'.

* last item => save:
if i_curr = i_last.
perform bdc_dynpro using 'SAPMF05A' '0300'.
perform bdc_field using 'BDC_OKCODE' '=BU'.
perform bdc_dynpro using 'SAPLKACB' '0002'.
perform bdc_field using 'BDC_OKCODE' '=ENTE'.
endif.
endloop.

perform call_transaction.
endform. " process_one_doc

*&---------------------------------------------------------------------*
*& Form call_transaction
*&---------------------------------------------------------------------*
* call transaction
*----------------------------------------------------------------------*
form call_transaction.
call transaction c_tcode using bdcdata
mode 'N' update 'S'
messages into messtab.

w_subrc = sy-subrc.
clear w_doc.
perform process_messages.

if w_doc ne space.
write: / 'Document created:', 25 w_doc, 40
'Debit:', w_debit currency w_waers,
'Credit:', w_credit currency w_waers.
add: w_debit to w_deb_tot,
w_credit to w_cre_tot,
1 to w_ndoc.
else.
write: / 'Document not created:', 25 w_doc, 40
'Debit:', w_debit currency w_waers,
'Credit:', w_credit currency w_waers.
add: w_debit to w_deb_bdc,
w_credit to w_cre_bdc,
1 to w_ndoc_bdc.
endif.

*error on transaction call, create BDC session if name entered:
if w_subrc <> 0 and not p_sess is initial.
perform bdc_create_session.
endif.
endform. " call_transaction

*----------------------------------------------------------------------*
* Start new screen *
*----------------------------------------------------------------------*
form bdc_dynpro using program dynpro.
clear bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
append bdcdata.
clear bdcdata.
endform.

*----------------------------------------------------------------------*
* Insert field *
*----------------------------------------------------------------------*
form bdc_field using fnam fval.
clear bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
append bdcdata.
clear bdcdata.
endform.

*----------------------------------------------------------------------*
* Insert field and shift left *
*----------------------------------------------------------------------*
form bdc_field_s using fnam fval.
clear bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
shift bdcdata-fval left deleting leading space.
append bdcdata.
clear bdcdata.
endform.

*----------------------------------------------------------------------*
* write field *
*----------------------------------------------------------------------*
form bdc_field_w using fnam fval.
clear bdcdata.
bdcdata-fnam = fnam.
write fval to bdcdata-fval.
append bdcdata.
clear bdcdata.
endform.

*&---------------------------------------------------------------------*
*& Form bdc_create_session
*&---------------------------------------------------------------------*
form bdc_create_session.
if f_session_created is initial.
call function 'BDC_OPEN_GROUP'
exporting
group = p_sess
keep = 'X'
user = sy-uname
exceptions
client_invalid = 1
destination_invalid = 2
group_invalid = 3
group_is_locked = 4
holddate_invalid = 5
internal_error = 6
queue_error = 7
running = 8
system_lock_error = 9
user_invalid = 10
others = 11.
if sy-subrc <> 0.
w_subrc = sy-subrc.
message i398(00) with 'Cannot open session' p_sess
'Return code=' w_subrc.
else.
f_session_created = 'X'.
endif.
endif.

if f_session_created = 'X'.
call function 'BDC_INSERT'
exporting
tcode = c_tcode
tables
dynprotab = bdcdata
exceptions
internal_error = 1
not_open = 2
queue_error = 3
tcode_invalid = 4
printing_invalid = 5
posting_invalid = 6
others = 7.
if sy-subrc <> 0.
w_subrc = sy-subrc.
message i398(00) with 'Cannot insert BDC into session' p_sess
'Return code=' w_subrc.
else.
f_session = 'X'.
endif.
endif.
endform. " bdc_create_session

*&---------------------------------------------------------------------*
*& Form bdc_close_session
*&---------------------------------------------------------------------*
form bdc_close_session.
call function 'BDC_CLOSE_GROUP'
exceptions
not_open = 1
queue_error = 2
others = 3.
if sy-subrc <> 0.
w_subrc = sy-subrc.
message i398(00) with 'Cannot close session' p_sess
'Return code=' w_subrc.
endif.
endform. " bdc_close_session

*&---------------------------------------------------------------------*
*& Form process_messages
*&---------------------------------------------------------------------*
form process_messages.
define prepare_message_text.
call function 'MESSAGE_TEXT_BUILD'
exporting
msgid = messtab-msgid
msgnr = messtab-msgnr
msgv1 = messtab-msgv1
msgv2 = messtab-msgv2
msgv3 = messtab-msgv3
msgv4 = messtab-msgv4
importing
message_text_output = w_msg.
end-of-definition.
define write_message_text.
if f_first is initial.
write: / 'Document Reference:', w_xblnr.
f_first = 'X'.
endif.
write / w_msg color &1.
end-of-definition.
data: w_msg(250),
f_first.

loop at messtab.
if messtab-msgtyp = 'E' or messtab-msgtyp = 'A'.
prepare_message_text.
write_message_text col_negative.
elseif messtab-msgtyp = 'W'.
prepare_message_text.
write_message_text col_total.
elseif messtab-msgtyp = 'S'.
if w_subrc <> 0.
prepare_message_text.
write_message_text col_normal.
endif.
* find document number:
if messtab-msgid = 'F5' and
messtab-msgnr = 312.
w_doc = messtab-msgv1.
endif.
endif.
endloop.
endform. " process_messages

Related Items:

 
Next >

Google Search

Statistics

Contribution Activity
Utilities: 38
Tips and Tricks: 333
Sample Code: 164
Total Contributions: 550

Member Activity
Members: 6197 since 2/1/2007!
New: 5 since yesterday!
Visitors: 1023813
We have 1 guest online

Newest Members

Welcome our newest members:

Google Ads

Shared Bottom Border

Contact Us | Polls | Add URL | Contribute | Privacy | Terms | Feedback

Discussion Forum | BLOG | Consultants: Post your resume | Companies: Advertise on ERPGenie.COM | Post Job
Financials Consultant | Consultant Review | Gallia Consulting | Supply Chain Project | SAP Financials Forum
GenieHoldings.COM, Inc. | Genie Press | WorkflowGenie | ESAGenie | ERPTopSites | ABAP Tips and Tricks | SAP Solutions Database

EDIGenie | Searching Survivor