abapcode
abapcode
* 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.
* 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.
* 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.
* 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.
* 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.
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.
* gv_valid = abap_true.
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 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.
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.
if sy-subrc <> 0.
cv_check = |Profit center { is_item-profit_center } is invalid|.
gv_valid = abap_false.
exit.
endif.
endif.
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 ).
gr_table->display( ).
form generate.
if lv_session = abap_true.
message e006(z500_message_class) with pa_sessn.
return.
endif.
perform open_bdc.
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.
form process_item.
data: lv_amt type p length 15 decimals 2.
case is_item-crdr.
when 'CR'.
lv_crdr = '31'.
when 'DR'.
lv_crdr = '21'.
endcase.
case is_item-crdr.
when 'CR'.
lv_crdr = '50'.
when 'DR'.
lv_crdr = '40'.
endcase.
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.
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.