You are on page 1of 6

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

*& Report zaug_oia_simple


*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zaug_oia_simple.

types : BEGIN OF ty_oia,


bp_id type snwd_bpa-bp_id,
company_name type snwd_bpa-company_name,
open_days type int4,
gross_amount type snwd_so_inv_head-gross_amount,
currency_code type snwd_so_inv_head-currency_code,
tagging type flag,
END OF TY_OIA,
BEGIN OF ty_avg_days,
bp_id type snwd_bpa-bp_id,
company_name type snwd_bpa-company_name,
no_days type int4,
inv_count type int4,
END OF ty_avg_days,
BEGIN OF ty_amount,
bp_id type snwd_bpa-bp_id,
gross_amount type snwd_so_inv_head-gross_amount,
currency_code type snwd_so_inv_head-currency_code,
END OF ty_amount.

data: itab type table of ty_oia,


wa type ty_oia,
lt_avg_days type table of ty_avg_days,
ls_avg_days type ty_avg_days,
lt_amount type table of ty_amount,
ls_amount type ty_amount.

data(lo_timer) = cl_abap_runtime=>create_hr_timer( ).
data(t1) = lo_timer->get_runtime( ).

*Creating a customizing table to maintain user specific threshold values

select single * from zdp_cust into @data(ls_customizing) where usrid = @sy-uname.

*Since how long on average basis the invoices are due


*Collect all the invoices which are open SNWD_SO_INV_HEAD where PAYMENT_STATUS = ‘’
select * from snwd_so_inv_head as so_hdr inner join snwD_bpa as bpa
on so_hdr~buyer_guid = bpa~node_key
into @data(ls_due_day) where payment_status = ''.
MOVE-CORRESPONDING ls_due_day-bpa to ls_avg_days.
* Calculate the average number of days invoices are due. For Each invoice find
how many days its due
CONVERT TIME STAMP ls_due_day-so_hdr-changed_at TIME ZONE sy-zonlo into date
data(lv_day).
* TODAY – WHEN_WAS_THE_INVOICE Changed = X
ls_avg_days-no_days = sy-datum - lv_day.
* Total number of days / Count of invoice
ls_avg_days-inv_count = 1.
collect ls_avg_days into lt_avg_days.
* Combine this data with SNWD_BPA (Business partners) to get for each customer
the due days average
ENDSELECT.

*3. Check the total open amount of invoice which is due in a common currency
*Invoice header table SNWD_SO_INV_HEAD to find open invoice PAYMENT_STATUS = ‘’
select * from snwd_so_inv_item as items inner join snwd_so_inv_head as inv_head on
items~parent_key = inv_head~node_key
inner join snwd_bpa as bpa on
inv_head~buyer_guid = bpa~node_key
into @data(ls_amt)
WHERE inv_head~payment_status = ''.

MOVE-CORRESPONDING ls_amt-bpa to ls_amount.


*Join the open invoices to find their items from SNWD_SO_INV_ITEM in different
currency
call FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
EXPORTING
* client = SY-MANDT
date = sy-datum
foreign_amount = ls_amt-items-gross_amount
foreign_currency = ls_amt-items-currency_code
local_currency = ls_customizing-currency_code
IMPORTING
local_amount = ls_amount-gross_amount
EXCEPTIONS
no_rate_found = 1
overflow = 2
no_factors_found = 3
no_spread_found = 4
derived_2_times = 5
others = 6
.

ls_amount-currency_code = ls_customizing-currency_code.
*Perform currency conversion to bring amount in common currency and total

*Join the data with SNWD_BPA with business partners


collect ls_amount into lt_amount.

ENDSELECT.

*4. Classify the customers based on their ability to pay pending bills. Comparing
with a threshold limit.
loop at lt_amount into ls_amount.

read table lt_avg_days into ls_avg_days with key bp_id = ls_amount-bp_id.

if sy-subrc = 0.

MOVE-CORRESPONDING ls_amount to wa.


wa-company_name = ls_avg_days.
wa-open_days = ls_avg_days-no_days / ls_avg_days-inv_count.

if wa-open_days > ls_customizing-max_open_days and ls_amount-gross_amount >


ls_customizing-max_gross_amount .
wa-tagging = abap_true.
else.
wa-tagging = abap_false.
ENDIF.
append wa to itab.

ENDIF.

endloop.

data(t2) = lo_timer->get_runtime( ).
data(t3) = ( t2 - t1 ) / 1000.

cl_demo_output=>display_text( text = |Time takes was { t3 } ms| ).


cl_demo_output=>display(
EXPORTING
data = itab
* name =
* exclude =
* include =
).

"Ctrl+7 to comment and un comment the code


*wa-company_name = 'IBM'.
*wa-gross_amount = 100.
*collect wa into itab.
*
*wa-company_name = 'SAP'.
*wa-gross_amount = 200.
*collect wa into itab.
*
*wa-company_name = 'SAP'.
*wa-gross_amount = 300.
*collect wa into itab.
*
*wa-company_name = 'IBM'.
*wa-gross_amount = 500.
*collect wa into itab.
*
*wa-company_name = 'IBM'.
*wa-gross_amount = 900.
*collect wa into itab.
*
*
*loop at itab into wa.
*write: / wa-company_name, wa-gross_amount.
*endloop.

*&---------------------------------------------------------------------*
*& Report zaug_oia_aggregate
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zaug_oia_aggregate.

types : BEGIN OF ty_oia,


bp_id type snwd_bpa-bp_id,
company_name type snwd_bpa-company_name,
open_days type int4,
gross_amount type snwd_so_inv_head-gross_amount,
currency_code type snwd_so_inv_head-currency_code,
tagging type flag,
END OF TY_OIA,
BEGIN OF ty_avg_days,
bp_id type snwd_bpa-bp_id,
company_name type snwd_bpa-company_name,
no_days type int4,
inv_count type int4,
END OF ty_avg_days,
BEGIN OF ty_amount,
bp_id type snwd_bpa-bp_id,
gross_amount type snwd_so_inv_head-gross_amount,
currency_code type snwd_so_inv_head-currency_code,
END OF ty_amount.

data: itab type table of ty_oia,


wa type ty_oia,
lt_avg_days type table of ty_avg_days,
ls_avg_days type ty_avg_days,
lt_amount type table of ty_amount,
ls_amount type ty_amount.

data(lo_timer) = cl_abap_runtime=>create_hr_timer( ).
data(t1) = lo_timer->get_runtime( ).

*Creating a customizing table to maintain user specific threshold values

select single * from zdp_cust into @data(ls_customizing) where usrid = @sy-uname.

*Since how long on average basis the invoices are due


*Collect all the invoices which are open SNWD_SO_INV_HEAD where PAYMENT_STATUS = ‘’
select bpa~bp_id, bpa~company_name,
so_hdr~changed_at, count( * ) as inv_count
from snwd_so_inv_head as so_hdr inner join snwD_bpa as bpa
on so_hdr~buyer_guid = bpa~node_key
into @data(ls_due_day) where payment_status = '' group by bpa~bp_id,
bpa~company_name,
so_hdr~changed_at.
MOVE-CORRESPONDING ls_due_day to ls_avg_days.
* Calculate the average number of days invoices are due. For Each invoice find
how many days its due
CONVERT TIME STAMP ls_due_day-changed_at TIME ZONE sy-zonlo into date
data(lv_day).
* TODAY – WHEN_WAS_THE_INVOICE Changed = X
ls_avg_days-no_days = ( sy-datum - lv_day ) * ls_due_day-inv_count.
* Total number of days / Count of invoice
ls_avg_days-inv_count = ls_due_day-inv_count.
collect ls_avg_days into lt_avg_days.
* Combine this data with SNWD_BPA (Business partners) to get for each customer
the due days average
ENDSELECT.

*3. Check the total open amount of invoice which is due in a common currency
*Invoice header table SNWD_SO_INV_HEAD to find open invoice PAYMENT_STATUS = ‘’
select bpa~bp_id, sum( items~gross_amount ) as gross_amount,
items~currency_code
from snwd_so_inv_item as items inner join snwd_so_inv_head as inv_head on
items~parent_key = inv_head~node_key
inner join snwd_bpa as bpa on
inv_head~buyer_guid = bpa~node_key
into @data(ls_amt)
WHERE inv_head~payment_status = '' group by bpa~bp_id, items~currency_code.

MOVE-CORRESPONDING ls_amt to ls_amount.


*Join the open invoices to find their items from SNWD_SO_INV_ITEM in different
currency
call FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
EXPORTING
* client = SY-MANDT
date = sy-datum
foreign_amount = ls_amt-gross_amount
foreign_currency = ls_amt-currency_code
local_currency = ls_customizing-currency_code
IMPORTING
local_amount = ls_amount-gross_amount
EXCEPTIONS
no_rate_found = 1
overflow = 2
no_factors_found = 3
no_spread_found = 4
derived_2_times = 5
others = 6
.

ls_amount-currency_code = ls_customizing-currency_code.
*Perform currency conversion to bring amount in common currency and total

*Join the data with SNWD_BPA with business partners


collect ls_amount into lt_amount.

ENDSELECT.

*4. Classify the customers based on their ability to pay pending bills. Comparing
with a threshold limit.
loop at lt_amount into ls_amount.

read table lt_avg_days into ls_avg_days with key bp_id = ls_amount-bp_id.

if sy-subrc = 0.

MOVE-CORRESPONDING ls_amount to wa.


wa-company_name = ls_avg_days.
wa-open_days = ls_avg_days-no_days / ls_avg_days-inv_count.

if wa-open_days > ls_customizing-max_open_days and ls_amount-gross_amount >


ls_customizing-max_gross_amount .
wa-tagging = abap_true.
else.
wa-tagging = abap_false.
ENDIF.

append wa to itab.

ENDIF.
endloop.

data(t2) = lo_timer->get_runtime( ).
data(t3) = ( t2 - t1 ) / 1000.

cl_demo_output=>display(
EXPORTING
data = itab
name = |Time takes was { t3 } ms|
* exclude =
* include =
).
*cl_demo_output=>display(
* EXPORTING
* data = itab
** name =
** exclude =
** include =
*).

You might also like