0% found this document useful (0 votes)
12 views12 pages

abapcode

The document outlines the structure and logic for a report in ABAP that handles journal entry postings, including data types for headers and items, selection screen parameters, and various forms for uploading, processing, validating, and displaying data. It includes error handling for file selection and data validation against existing records in the database. The report ensures that credit and debit amounts balance and provides feedback on the validity of the entries.

Uploaded by

shanii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

abapcode

The document outlines the structure and logic for a report in ABAP that handles journal entry postings, including data types for headers and items, selection screen parameters, and various forms for uploading, processing, validating, and displaying data. It includes error handling for file selection and data validation against existing records in the database. The report ensures that credit and debit amounts balance and provides feedback on the validity of the entries.

Uploaded by

shanii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

*&---------------------------------------------------------------------*

*& Report Z500_JE_UPD


*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
report z500_je_upd.

* Data types
types: begin of ty_header,
rec_typ type c length 1,
doc_date type string,
curr_code type c length 3,
ref type c length 16,
header_txt type c length 25,
end of ty_header.

types: begin of ty_item,


rec_typ type c length 1,
crdr type c length 2,
gl_acc type c length 10,
vendor type c length 10,
tax type c length 2,
amount type p length 15 decimals 2,
cost_center type c length 10,
profit_center type c length 10,
assignment type c length 10,
item_txt type c length 50,
end of ty_item.

types: begin of ty_report,


company type bkpf-bukrs,
doc_typ type bkpf-blart,
post_dt type bkpf-budat,
doc_dt type bkpf-bldat,
curr_code type bkpf-waers,
ref type bkpf-xblnr,
header_txt type bkpf-bktxt,
cr_amt type bseg-wrbtr,
dr_amt type bseg-wrbtr,
check type string,
end of ty_report.

* Data declarations
data: gt_raw type table of string,
gt_header type table of ty_header,
gs_header type ty_header,
gt_item type table of ty_item,
gs_item type ty_item,
gt_report type table of ty_report,
gs_report type ty_report,
lv_last_index type i,
lv_cr_amount type bseg-wrbtr,
lv_dr_amount type bseg-wrbtr,
lv_crdr type c length 2,
lv_session type abap_bool value abap_false.

data: lv_valid type abap_bool,


lv_amount type string.
data: gv_valid type abap_bool.

data: bdcdata like bdcdata occurs 0 with header line.


data: gr_table type ref to cl_salv_table.

* Selection screen parameters


selection-screen begin of block b1 with frame title t1.
parameters: pa_bukrs type bkpf-bukrs obligatory,
pa_blart type bkpf-blart default 'AB' obligatory,
pa_budat type bkpf-budat default sy-datum obligatory.
selection-screen end of block b1.

selection-screen begin of block b2 with frame title t2.


parameters: pa_fname type string obligatory.
selection-screen end of block b2.

selection-screen begin of block b3 with frame title t3.


parameters: pa_sessn type bdc_sessn obligatory,
pa_dmode type ctu_params-dismode default 'N' obligatory,
pa_umode type ctu_params-updmode default 'S' obligatory.
selection-screen end of block b3.

* Initialization
initialization.
t1 = 'Journal Entry Posting - Default'.
t2 = 'Data Upload Parameter'.
t3 = 'Batch Input Parameters'.

if sy-batch = 'X'.
message e003(z500_message_class).
endif.

* Class definition for event handling


class lcl_je_event_handler definition.
public section.
class-methods: handle_custom_function
for event added_function of cl_salv_events_table
importing e_salv_function.
endclass.

* Class implementation for event handling


class lcl_je_event_handler implementation.
method handle_custom_function.
case e_salv_function.
when 'POST'.
perform generate.
endcase.
endmethod.
endclass.

* File selection
at selection-screen on value-request for pa_fname.
data: lt_file_table type filetable,
lv_rc type i,
lv_filename type string.

call method cl_gui_frontend_services=>file_open_dialog


exporting
window_title = 'Select File'
default_extension = 'txt'
file_filter = '(*.txt)|*.txt|All Files (.)|.'
changing
file_table = lt_file_table
rc = lv_rc
exceptions
others = 1.

if sy-subrc = 0 and lv_rc > 0.


read table lt_file_table into lv_filename index 1.
if lv_filename cp '*.txt'.
pa_fname = lv_filename.
else.
message 'Only .txt files are allowed. Select a .txt file.' type 'E'.
endif.
else.
message 'File selection cancelled or failed' type 'E'.
return.
endif.

* Main logic
start-of-selection.
perform upload.
if gt_raw is not initial.
perform process_data.
perform report.
perform display_data.
else.
message 'No data loaded' type 'E'.
endif.

* Form to upload data


form upload.
call function 'GUI_UPLOAD'
exporting
filename = pa_fname
filetype = 'ASC'
tables
data_tab = gt_raw
exceptions
file_open_error = 1
file_read_error = 2
others = 17.
if sy-subrc <> 0.
message e004(z500_message_class).
endif.
endform.

* Form to process data


form process_data.
loop at gt_raw assigning field-symbol(<lv_line>).
data: lt_fields type table of string.

split <lv_line> at cl_abap_char_utilities=>horizontal_tab into table lt_fields.

field-symbols: <lv_rec_typ> type string,


<lv_date_raw> type string,
<lv_curr_code> type string,
<lv_ref> type string,
<lv_header_txt> type string,
<lv_crdr> type string,
<lv_gl_acc> type string,
<lv_vendor> type string,
<lv_tax> type string,
<lv_amount> type string,
<lv_cost_center> type string,
<lv_profit_center> type string,
<lv_assignment> type string,
<lv_item_txt> type string.

" Assign field symbols with checks


assign lt_fields[ 1 ] to <lv_rec_typ>.
if <lv_rec_typ> is assigned.
case <lv_rec_typ>.
when 'H'.
clear: gs_header.
gs_header-rec_typ = <lv_rec_typ>.
assign lt_fields[ 2 ] to <lv_date_raw>.
if <lv_date_raw> is assigned.
" Convert DDMMYYYY to YYYYMMDD
data(lv_day) = <lv_date_raw>+0(2).
data(lv_month) = <lv_date_raw>+3(2).
data(lv_year) = <lv_date_raw>+6(4).
concatenate lv_year lv_month lv_day into gs_header-doc_date.
endif.
assign lt_fields[ 3 ] to <lv_curr_code>.
if <lv_curr_code> is assigned.
gs_header-curr_code = <lv_curr_code>.
endif.
assign lt_fields[ 4 ] to <lv_ref>.
if <lv_ref> is assigned.
gs_header-ref = <lv_ref>.
endif.
assign lt_fields[ 5 ] to <lv_header_txt>.
if <lv_header_txt> is assigned.
gs_header-header_txt = <lv_header_txt>.
endif.
append gs_header to gt_header.
lv_last_index = sy-tabix.

when 'I'.
clear: gs_item.
gs_item-rec_typ = <lv_rec_typ>.
assign lt_fields[ 2 ] to <lv_crdr>.
if <lv_crdr> is assigned.
gs_item-crdr = <lv_crdr>.
endif.
assign lt_fields[ 3 ] to <lv_gl_acc>.
if <lv_gl_acc> is assigned.
gs_item-gl_acc = <lv_gl_acc>.
endif.
assign lt_fields[ 4 ] to <lv_vendor>.
if <lv_vendor> is assigned.
gs_item-vendor = <lv_vendor>.
endif.
assign lt_fields[ 5 ] to <lv_tax>.
if <lv_tax> is assigned.
gs_item-tax = <lv_tax>.
endif.
assign lt_fields[ 6 ] to <lv_amount>.
if <lv_amount> is assigned.
gs_item-amount = <lv_amount>.
endif.
assign lt_fields[ 7 ] to <lv_cost_center>.
if <lv_cost_center> is assigned.
gs_item-cost_center = <lv_cost_center>.
endif.
assign lt_fields[ 8 ] to <lv_profit_center>.
if <lv_profit_center> is assigned.
gs_item-profit_center = <lv_profit_center>.
endif.
assign lt_fields[ 9 ] to <lv_assignment>.
if <lv_assignment> is assigned.
gs_item-assignment = <lv_assignment>.
endif.
assign lt_fields[ 10 ] to <lv_item_txt>.
if <lv_item_txt> is assigned.
gs_item-item_txt = <lv_item_txt>.
endif.
append gs_item to gt_item.
endcase.
endif.
endloop.
endform.

form report.

loop at gt_header into gs_header.


clear: lv_cr_amount, lv_dr_amount, gs_report.
gs_report-company = pa_bukrs.
gs_report-doc_typ = pa_blart.
gs_report-post_dt = pa_budat.
gs_report-doc_dt = gs_header-doc_date.
gs_report-curr_code = gs_header-curr_code.
gs_report-ref = gs_header-ref.
gs_report-header_txt = gs_header-header_txt.

* gv_valid = abap_true.

loop at gt_item into gs_item where rec_typ = 'I'.


perform validate using gs_item changing gs_report-check.

case gs_item-crdr.
when 'CR'.
lv_cr_amount = lv_cr_amount + gs_item-amount.
when 'DR'.
lv_dr_amount = lv_dr_amount + gs_item-amount.
endcase.
endloop.

if lv_cr_amount <> lv_dr_amount.


gs_report-check = 'Posting must balance (CR = DR)'.
gv_valid = abap_false.
endif.
append gs_report to gt_report.
endloop.
if gv_valid = abap_true.
endif.
endform.

* Form to validate data


form validate using is_item type ty_item
changing cv_check type string.
data: ls_return type bapireturn,
ls_glacc type bapi3006_0-gl_account,
lv_kokrs type tka02-kokrs,
lv_prctr type cepc-prctr,
lv_lifnr type lfa1-lifnr,
lv_mwskz type t007a-mwskz.

" Default validation status


cv_check = 'Valid'.
gv_valid = abap_true.

" Ensure either GL Account OR Vendor is provided, not both


if is_item-gl_acc is not initial and is_item-vendor is not initial.
cv_check = 'Either GL Account OR Vendor should be filled per line item, not
both'.
gv_valid = abap_false.
exit.
endif.

if is_item-gl_acc is not initial.

call function 'CONVERSION_EXIT_ALPHA_INPUT'


exporting
input = is_item-gl_acc
importing
output = ls_glacc.

" Call BAPI to check G/L Account


*BREAK-POINT.
call function 'BAPI_GL_ACC_EXISTENCECHECK'
exporting
companycode = pa_bukrs
glacct = ls_glacc
importing
return = ls_return.

if ls_return-type = 'E'.
cv_check = ls_return-message.
gv_valid = abap_false.
exit.
endif.

endif.

"Validate Vendor
if is_item-vendor is not initial.
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = is_item-vendor
importing
output = is_item-vendor.

select single lifnr


into lv_lifnr
from lfa1
where lifnr = is_item-vendor.

if sy-subrc <> 0.
cv_check = |Vendor { is_item-vendor } does not exist|.
gv_valid = abap_false.
exit.
endif.
endif.
*break-point.
" Validate Profit Center
if is_item-profit_center is not initial.
select single kokrs
into lv_kokrs
from tka02
where bukrs = pa_bukrs.

if sy-subrc <> 0.
cv_check = 'Invalid Company Code'.
gv_valid = abap_false.
exit.
endif.

select single prctr


into lv_prctr
from cepc
where prctr = is_item-profit_center
and kokrs = lv_kokrs
and datbi >= pa_budat.

if sy-subrc <> 0.
cv_check = |Profit center { is_item-profit_center } is invalid|.
gv_valid = abap_false.
exit.
endif.
endif.

" Validate Tax Code


if is_item-tax is not initial.
select single mwskz
into lv_mwskz
from t007a
where mwskz = is_item-tax.

if sy-subrc <> 0.
cv_check = |Tax code { is_item-tax } does not exist|.
gv_valid = abap_false.
exit.
endif.
endif.

endform.

form display_data.
data: gr_table type ref to cl_salv_table,
lr_functions type ref to cl_salv_functions,
lr_columns type ref to cl_salv_columns_table,
lr_column type ref to cl_salv_column,
lr_events type ref to cl_salv_events_table,
gr_event_handler type ref to lcl_je_event_handler.

try.
call method cl_salv_table=>factory
importing
r_salv_table = gr_table
changing
t_table = gt_report.

lr_events = gr_table->get_event( ).
create object gr_event_handler.

if gv_valid = abap_true.
set handler gr_event_handler->handle_custom_function for lr_events.
endif.

gr_table->set_screen_status(
pfstatus = 'SALV_STANDARD'
report = syst-repid
set_functions = gr_table->c_functions_all ).

lr_columns = gr_table->get_columns( ).
lr_column = lr_columns->get_column( 'COMPANY' ).
lr_column->set_long_text( 'Company Code' ).
lr_column->set_output_length( 12 ).
lr_column = lr_columns->get_column( 'DOC_TYP' ).
lr_column->set_long_text( 'Document Type' ).
lr_column->set_output_length( 12 ).

lr_column = lr_columns->get_column( 'POST_DT' ).


lr_column->set_long_text( 'Posting Date' ).
lr_column->set_output_length( 12 ).

lr_column = lr_columns->get_column( 'DOC_DT' ).


lr_column->set_long_text( 'Document Date' ).
lr_column->set_output_length( 12 ).

lr_column = lr_columns->get_column( 'CURR_CODE' ).


lr_column->set_long_text( 'Currency Code' ).
lr_column->set_output_length( 12 ).

lr_column = lr_columns->get_column( 'REF' ).


lr_column->set_long_text( 'Reference' ).
lr_column->set_output_length( 12 ).

lr_column = lr_columns->get_column( 'HEADER_TXT' ).


lr_column->set_long_text( 'Header Text' ).
lr_column->set_output_length( 15 ).

lr_column = lr_columns->get_column( 'CR_AMT' ).


lr_column->set_long_text( 'Credit Amount' ).
lr_column->set_output_length( 17 ).

lr_column = lr_columns->get_column( 'DR_AMT' ).


lr_column->set_long_text( 'Debit Amount' ).
lr_column->set_output_length( 14 ).

lr_column = lr_columns->get_column( 'CHECK' ).


lr_column->set_long_text( 'Check Result' ).
lr_column->set_output_length( 25 ).

gr_table->display( ).

catch cx_salv_msg into data(lx_salv).


message lx_salv->get_text( ) type 'E'.
catch cx_root into data(lx_root).
message lx_root->get_text( ) type 'E'.
endtry.
endform.

form generate.
if lv_session = abap_true.
message e006(z500_message_class) with pa_sessn.
return.
endif.

perform open_bdc.

loop at gt_header into gs_header.


perform process_header.
perform process_item.
perform insert_bdc.
endloop.

perform close_bdc.
endform.

form open_bdc.
call function 'BDC_OPEN_GROUP'
exporting
client = sy-mandt
group = pa_sessn
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.
message e006(z500_message_class) with pa_sessn.
endif.
endform.

form process_header.
data: lv_dt type c length 10,
lv_pdt type c length 10.

lv_pdt = |{ pa_budat+6(2) }.{ pa_budat+4(2) }.{ pa_budat+0(4) }|.


lv_dt = |{ pa_budat+6(2) }{ pa_budat+4(2) }{ pa_budat+0(4) }|.

loop at gt_report into data(ls_report).


lv_dt = |{ ls_report-doc_dt+6(2) }.{ ls_report-doc_dt+4(2) }.{ ls_report-
doc_dt+0(4) }|.
endloop.

perform bdc_dynpro using 'SAPMF05A' '0100'.


perform bdc_field using 'BDC_OKCODE' '/00'.
perform bdc_field using 'BKPF-BLDAT' lv_dt.
perform bdc_field using 'BKPF-BLART' pa_blart.
perform bdc_field using 'BKPF-BUKRS' pa_bukrs.
perform bdc_field using 'BKPF-BUDAT' lv_pdt.
perform bdc_field using 'BKPF-WAERS' gs_header-curr_code.
perform bdc_field using 'BKPF-BKTXT' gs_header-header_txt.
endform.

form process_item.
data: lv_amt type p length 15 decimals 2.

loop at gt_item into gs_item.


lv_amt = gs_item-amount.
if gs_item-vendor is not initial.
perform process_vendor using gs_item lv_amt.
else.
perform process_gl using gs_item lv_amt.
endif.

if gs_item-profit_center is not initial or gs_item-cost_center is not initial.


perform bdc_dynpro using 'SAPLKACB' '0002'.
perform bdc_field using 'BDC_OKCODE' '=ENTE'.
perform bdc_field using 'COBL-PRCTR' gs_item-profit_center.
perform bdc_field using 'COBL-KOSTL' gs_item-cost_center.
perform bdc_field using 'BDC_OKCODE' '=ENTE'.
perform bdc_dynpro using 'SAPMF05A' '0300'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
endif.
endloop.
endform.

form process_vendor using is_item type ty_item


iv_amount type p.
data: lv_crdr type c length 2,
lv_amount_str type c length 10.

write iv_amount to lv_amount_str decimals 2.

case is_item-crdr.
when 'CR'.
lv_crdr = '31'.
when 'DR'.
lv_crdr = '21'.
endcase.

perform bdc_field using 'RF05A-NEWBS' lv_crdr.


perform bdc_field using 'RF05A-NEWKO' is_item-vendor.

perform bdc_dynpro using 'SAPMF05A' '0302'.


perform bdc_field using 'BDC_OKCODE' '/00'.
perform bdc_field using 'BSEG-WRBTR' lv_amount_str.
perform bdc_field using 'BSEG-ZUONR' is_item-assignment.
perform bdc_field using 'BSEG-SGTXT' is_item-item_txt.
endform.

form process_gl using is_item type ty_item


iv_amount type p.
data: lv_crdr type c length 2,
lv_amount_str type c length 10.

write iv_amount to lv_amount_str decimals 2.

case is_item-crdr.
when 'CR'.
lv_crdr = '50'.
when 'DR'.
lv_crdr = '40'.
endcase.

perform bdc_field using 'RF05A-NEWBS' lv_crdr.


perform bdc_field using 'RF05A-NEWKO' is_item-gl_acc.

perform bdc_dynpro using 'SAPMF05A' '0300'.


perform bdc_field using 'BDC_OKCODE' '/00'.
perform bdc_field using 'BSEG-WRBTR' lv_amount_str.
perform bdc_field using 'BSEG-ZUONR' is_item-assignment.
perform bdc_field using 'BSEG-SGTXT' is_item-item_txt.
endform.

*form process_clickmore using is_item type ty_item.


* if is_item-profit_center is not initial or is_item-cost_center is not initial.
* perform bdc_dynpro using 'SAPLKACB' '0002'.
* perform bdc_field using 'BDC_OKCODE' '=ENTE'.
* if is_item-profit_center is not initial.
* perform bdc_field using 'COBL-PRCTR' is_item-profit_center.
* endif.
* if is_item-cost_center is not initial.
* perform bdc_field using 'COBL-KOSTL' is_item-cost_center.
* endif.
* perform bdc_field using 'BDC_OKCODE' '=ENTE'.
* endif.
*endform.

form insert_bdc.
call function 'BDC_INSERT'
exporting
tcode = 'FB01'
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.
message e006(z500_message_class) with pa_sessn.
endif.

refresh bdcdata.
endform.

form close_bdc.
data: lr_functions type ref to cl_salv_functions,
lr_function type ref to cl_salv_function.

call function 'BDC_CLOSE_GROUP'


exceptions
not_open = 1
queue_error = 2
others = 3.

if sy-subrc = 0.
lv_session = abap_true.
message s007(z500_message_class) with pa_sessn.
else.
message e006(z500_message_class) with pa_sessn.
endif.
endform.

form bdc_dynpro using program dynpro.


clear bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
append bdcdata.
endform.

form bdc_field using fnam fval.


clear bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
shift bdcdata-fval left deleting leading space.
append bdcdata.
endform.

You might also like