Sei sulla pagina 1di 9

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

*& Report ZRICH_002


*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
report zepm_data_uploader1.
data: ls_tab type edids,
lv_times type timestamp,
lt_tab type table of edids.

parameters : gv_adr type c as checkbox,


gv_dat type c as checkbox,
gv_prod type c as checkbox,
gv_bp type c as checkbox,
gvinvbp type c as checkbox,
gvinvpr type c as checkbox,
gv_inv type c as checkbox.
types:
begin of ty_snwd_bp,
client type string, node_key type string, bp_role type string, email_address type
string, phone_number type string, fax_number type string, web_address type string,
address_guid type string, bp_id type string, company_name type string, legal_form
type
string, created_by type string, created_at type string, changed_by type string,
changed_at type string, currency_code type string, dummy_field_bpa type string,
approval_status type string, test_fan type string, test_fan_2 type string,
test_fan_3 type
string,

end of ty_snwd_bp,
begin of ty_snwd_pd,
client type string, node_key type string, product_id type string, type_code
type string,
category type string, created_by type string, created_at type string,
changed_by type string,
changed_at type string, name_guid type string, desc_guid type string,
supplier_guid type string, tax_tarif_code type string, measure_unit type
string,
weight_measure type string, weight_unit type string, currency_code type
string,
price type string, product_pic_url type string, width type string, depth
type string,
height type string, dim_unit type string, dummy_field_pd type string,

end of ty_snwd_pd,
begin of ty_snwd_texts,
client type string, node_key type string, parent_key type string, language
type string, text type string, _dataaging type string,

end of ty_snwd_texts,
begin of ty_snwd_so,
client type string, node_key type string, so_id type string, created_by type
string, created_at type string, changed_by type string,
changed_at type string, created_by_bp type string, changed_by_bp type string,
note_guid type string, buyer_guid type string,
currency_code type string, gross_amount type string, net_amount type string,
tax_amount type string, lifecycle_status type string,
billing_status type string, delivery_status type string, op_id type string,
_dataaging type string, dummy type string,
overall_status type string, buy_contact_guid type string, ship_to_adr_guid
type string, bill_to_adr_guid type string, payment_method type string,
payment_terms type string,

end of ty_snwd_so,
begin of ty_snwd_so_inv_head,
client type string, node_key type string, so_guid type string, dly_note_number
type string, created_by type string, created_at type string,
changed_by type string, changed_at type string, gross_amount type string,
net_amount type string, tax_amount type string,
currency_code type string, buyer_guid type string, dunning_level type string,
payment_status type string,
_dataaging type string,

end of ty_snwd_so_inv_head,
begin of ty_snwd_so_inv_item,
client type string, node_key type string, parent_key type string, so_item_guid
type string,
quantity type string, quantity_unit type string, gross_amount type string,
net_amount type string, tax_amount type string, currency_code type string,
product_guid type string, inv_item_pos type string, _dataaging type string,

end of ty_snwd_so_inv_item.

data: t_snwd_pd type standard table of ty_snwd_pd.


data: t_snwd_texts type standard table of ty_snwd_texts.
data: t_snwd_so type standard table of ty_snwd_so.
data: t_snwd_so_inv type standard table of ty_snwd_so_inv_head.
data: t_snwd_so_inv_itm type standard table of ty_snwd_so_inv_item.
data: t_snwd_bpa type standard table of ty_snwd_bp.
*

class lcl_excel_uploader definition.


public section.
data: header_rows_count type i.
data: max_rows type i.
data: filename type localfile.
methods:
constructor.
methods:
upload changing ct_data type any table.
private section.
data: lv_tot_components type i.
methods:
do_upload
importing
iv_begin type i
iv_end type i
exporting
rv_empty type flag
changing
ct_data type standard table.

endclass. "lcl_excel_uploader DEFINITION

data: lo_uploader type ref to lcl_excel_uploader.


field-symbols <fs> type any.
create object lo_uploader.

* delete from snwd_so.

if gv_prod = 'X'.
delete from snwd_pd.
delete from snwd_texts.
data: lt_prd type table of snwd_pd,
ls_prd type snwd_pd.

lo_uploader->max_rows = 125.
lo_uploader->filename = 'C:\epmdata\snwd_pd.xls'.
lo_uploader->header_rows_count = 2.
lo_uploader->upload( changing ct_data = t_snwd_pd ).
* delete from edids.
loop at t_snwd_pd assigning field-symbol(<str>).
assign component 'CLIENT' of structure <str> to <fs>.
<fs> = sy-mandt.
get time stamp field lv_times.
<str>-changed_at = lv_times.
<str>-created_at = lv_times.
move-corresponding <str> to ls_prd.
append ls_prd to lt_prd.
endloop.

modify snwd_pd from table lt_prd.

data: lt_prdt type table of snwd_texts,


ls_prdt type snwd_texts.

lo_uploader->max_rows = 125.
lo_uploader->filename = 'C:\epmdata\SNWD_TEXTS.xls'.
lo_uploader->header_rows_count = 2.
lo_uploader->upload( changing ct_data = t_snwd_texts ).
* delete from edids.
loop at t_snwd_texts assigning field-symbol(<strt>).
assign component 'MANDT' of structure <strt> to <fs>.
<fs> = sy-mandt.
move-corresponding <strt> to ls_prdt.
append ls_prdt to lt_prdt.
endloop.

modify snwd_texts from table lt_prdt.


endif.
if gv_bp = 'X'.
delete from snwd_bpa.
data: lt_bp type table of snwd_bpa,
ls_bp type snwd_bpa.

lo_uploader->max_rows = 95.
lo_uploader->filename = 'C:\epmdata\snwd_bpa.xls'.
lo_uploader->header_rows_count = 2.
lo_uploader->upload( changing ct_data = t_snwd_bpa ).
* delete from edids.
loop at t_snwd_bpa assigning field-symbol(<strb>).
assign component 'CLIENT' of structure <strb> to <fs>.
<fs> = sy-mandt.
get time stamp field lv_times.
<strb>-changed_at = lv_times.
<strb>-created_at = lv_times.
move-corresponding <strb> to ls_bp.
append ls_bp to lt_bp.
endloop.

modify snwd_bpa from table lt_bp.


endif.

if gv_inv = 'X'.
* delete from snwd_so_inv_head.
* delete from snwd_so_inv_item.
* data: lt_prdi type table of snwd_so_inv_head,
* ls_prdi type snwd_so_inv_head.
*
* lo_uploader->max_rows = 1000.
* lo_uploader->filename = 'C:\epmdata\SNWD_SO_INV_HEAD.xls'.
* lo_uploader->header_rows_count = 2.
* lo_uploader->upload( changing ct_data = t_snwd_so_inv ).
** delete from edids.
* loop at t_snwd_so_inv assigning field-symbol(<strin>).
* assign component 'CLIENT' of structure <strin> to <fs>.
* <fs> = sy-mandt.
* get time stamp field lv_times.
* <strin>-changed_at = lv_times.
* <strin>-created_at = lv_times.
* move-corresponding <strin> to ls_prdi.
* append ls_prdi to lt_prdi.
* endloop.
*
* modify snwd_so_inv_head from table lt_prdi.

data: lt_prditm type table of snwd_so_inv_item,


ls_prditm type snwd_so_inv_item.

lo_uploader->max_rows = 5880.
lo_uploader->filename = 'C:\epmdata\snwd_so_inv_item.xls'.
lo_uploader->header_rows_count = 2.
lo_uploader->upload( changing ct_data = t_snwd_so_inv_itm ).
* delete from edids.
loop at t_snwd_so_inv_itm assigning field-symbol(<stritm>).
assign component 'CLIENT' of structure <stritm> to <fs>.
<fs> = sy-mandt.
move-corresponding <stritm> to ls_prditm.
append ls_prditm to lt_prditm.
endloop.

modify snwd_so_inv_item from table lt_prditm.

endif.
IF gvinvpr = 'X'.
data: BEGIN OF lt_products occurs 0,
fromp type snwd_so-node_key,
top type snwd_so-node_key,
END OF lt_products,
ls_products like LINE OF lt_products.

data: lV_guid type snwd_so-node_key.


data: lv_high type i.
data: lv_idx type i.

select * from snwd_so_inv_item into TABLE @data(lt_orderss).

SELECT * FROM snwd_pd INTO TABLE @data(lt_ords).

DESCRIBE TABLE lt_ords LINES lv_high.

LOOP AT lt_orderss ASSIGNING FIELD-SYMBOL(<orders>).


* read TABLE lt_products INTO ls_products with key fromp = <order>-buyer_guid.
CALL FUNCTION 'QF05_RANDOM_INTEGER'
EXPORTING
RAN_INT_MAX = lv_high
RAN_INT_MIN = 1
IMPORTING
RAN_INT = lv_idx
* EXCEPTIONS
* INVALID_INPUT = 1
* OTHERS = 2
.

READ TABLE lt_ords INTO data(ls_prodc) INDEX lv_idx.

<orders>-product_guid = ls_prodc-node_key.
ENDLOOP.

MODIFY snwd_so_inv_item FROM TABLE lt_orderss.

ENDIF.
if gvinvbp = 'X'.

select * from snwd_so_inv_head into TABLE @data(lt_orders).

select DISTINCT buyer_guid as fromp from snwd_so_inv_head into CORRESPONDING


FIELDS OF TABLE lt_products.

* delete ADJACENT DUPLICATES FROM lt_orders COMPARING parent_key.

SELECT * FROM snwd_bpa INTO TABLE @data(lt_ord) WHERE bp_role = '1' or bp_role
= '01'.

DESCRIBE TABLE lt_ord LINES lv_high.

LOOP AT lt_orders ASSIGNING FIELD-SYMBOL(<order>).


* read TABLE lt_products INTO ls_products with key fromp = <order>-buyer_guid.
CALL FUNCTION 'QF05_RANDOM_INTEGER'
EXPORTING
RAN_INT_MAX = lv_high
RAN_INT_MIN = 1
IMPORTING
RAN_INT = lv_idx
* EXCEPTIONS
* INVALID_INPUT = 1
* OTHERS = 2
.

READ TABLE lt_ord INTO data(ls_prod) INDEX lv_idx.

<order>-buyer_guid = ls_prod-node_key.
ENDLOOP.

MODIFY snwd_so_inv_head FROM TABLE lt_orders.

endif.

if gv_adr = 'X'.
**Adjust Address and BPA table GUIDS
data: itabx type table of snwd_ad.
select * from snwd_ad into table @data(itab) order by city.

delete adjacent duplicates from itab comparing city.

select * from snwd_bpa into table @data(xtab).

loop at xtab into data(x).


data(lv_index) = sy-tabix.
read table itab assigning field-symbol(<s>) index lv_index.
<s>-node_key = x-address_guid.
append <s> to itabx.
endloop.

*delete FROM snwd_ad.


MODIFY snwd_ad from table itabx.
endif.

if gv_dat = 'X'.
** set some date in invoice header as old
data : lv_date type datum,
lv_val type snwd_so_inv_head-created_at.

select * from snwd_so_inv_head into table @data(xitab).

loop at xitab assigning field-symbol(<xfs>).


perform get_random_date.
<xfs>-created_at = lv_val.
<xfs>-changed_at = lv_val.
endloop.

modify snwd_so_inv_head from table xitab.

endif.
*

select * from snwd_so_inv_head as inv INNER JOIN snwd_bpa as bpa


on inv~buyer_guid = bpa~node_key INTO table @data(lt_test1).
DESCRIBE TABLE lt_test1.
IF sy-tfill > 0.
WRITE : / 'Test for bpa and inv header successful'.
else.
WRITE : 'test failed'.
ENDIF.

form get_random_date.
data:
o_rand type ref to cl_abap_random_int,
n type i,
seed type i.

seed = cl_abap_random=>seed( ).

cl_abap_random_int=>create(
exporting
seed = seed
min = 0
max = 100
receiving
prng = o_rand
).

n = o_rand->get_next( ).

lv_date = sy-datum - 365 - n.

convert date lv_date


into time stamp lv_val time zone 'EST'.

endform.

class lcl_excel_uploader implementation.


method constructor.
max_rows = 9999.
endmethod. "constructor
method upload.
data: lo_struct type ref to cl_abap_structdescr,
lo_table type ref to cl_abap_tabledescr,
lt_comp type cl_abap_structdescr=>component_table.

lo_table ?= cl_abap_structdescr=>describe_by_data( ct_data ).


lo_struct ?= lo_table->get_table_line_type( ).
lt_comp = lo_struct->get_components( ).
*
lv_tot_components = lines( lt_comp ).
*
data: lv_empty type flag,
lv_begin type i,
lv_end type i.
*
lv_begin = header_rows_count + 1.
lv_end = max_rows.
* WHILE lv_empty IS INITIAL.
do_upload(
exporting
iv_begin = lv_begin
iv_end = lv_end
importing
rv_empty = lv_empty
changing
ct_data = ct_data
).
lv_begin = lv_end + 1.
lv_end = lv_begin + max_rows.
* ENDWHILE.
endmethod. "upload
*

method do_upload.

data: li_exceldata type standard table of alsmex_tabline.


data: ls_exceldata like line of li_exceldata.
data: lv_tot_rows type i.
data: lv_packet type i.
field-symbols: <struc> type any,
<field> type any.

* Upload this packet


call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
exporting
filename = filename
i_begin_col = 1
i_begin_row = iv_begin
i_end_col = lv_tot_components
i_end_row = iv_end
tables
intern = li_exceldata
exceptions
inconsistent_parameters = 1
upload_ole = 2
others = 3.
* something wrong, exit
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
rv_empty = 'X'.
exit.
endif.

* No rows uploaded, exit


if li_exceldata is initial.
rv_empty = 'X'.
exit.
endif.

* Move from Row, Col to Flat Structure


loop at li_exceldata into ls_exceldata.
" Append new row
at new row.
append initial line to ct_data assigning <struc>.
endat.
" component and its value
assign component ls_exceldata-col of structure <struc> to <field>.
if sy-subrc eq 0.
<field> = ls_exceldata-value.
endif.

" add the row count


at end of row.
if <struc> is not initial.
lv_tot_rows = lv_tot_rows + 1.
endif.
endat.
endloop.

* packet has more rows than uploaded rows,


* no more packet left. Thus exit
lv_packet = iv_end - iv_begin.
if lv_tot_rows lt lv_packet.
rv_empty = 'X'.
endif.

endmethod. "do_upload
endclass. "lcl_excel_uploader IMPLEMENTATION
*

Potrebbero piacerti anche