OpenSAP A4h1 Week 4 Source Code
OpenSAP A4h1 Week 4 Source Code
OpenSAP A4h1 Week 4 Source Code
https://open.sap.com/courses/a4h1
Version 1 - 15.10.2014
Only Demo content
Without any warranty
Contents
CDS DDL Sources ........................................................................................................................................ 3
ZDDLS_CDS_40_ANNOTATION .................................................................................................................. 3
ABAP Classes .............................................................................................................................................. 3
ZCL_AMDP_SIMPLE_00 .............................................................................................................................. 3
ZCL_A4H1_AMDP_BADI .............................................................................................................................. 4
ZCL_A4H1_BADI_IMPL_01 .......................................................................................................................... 5
ZCL_A4H1_BADI_IMPL_02 .......................................................................................................................... 6
ZCL_A4H1_CALL_AMDP_BADI ................................................................................................................... 7
ZCL_A4H1_E2E_CUST_OPEN_INVOICE ................................................................................................... 8
ZCL_ZA4H1_E2E_CUST_INF_DPC_EXT.................................................................................................. 12
ABAP Programs ......................................................................................................................................... 15
ZR_ADBC_PROC_CALL............................................................................................................................. 15
ZR_ADBC_SIMPLE ..................................................................................................................................... 17
ZR_AMDP_01_SIMPLE_CALL ................................................................................................................... 18
ZR_AMDP_BADI_CALL .............................................................................................................................. 19
ZR_AMDP_CALL ......................................................................................................................................... 19
ABAP Interfaces ......................................................................................................................................... 19
ZIF_A4H1_AMDP_BADI.............................................................................................................................. 19
@AbapCatalog.sqlViewName: 'ZDDLS_CDS_40'
@ClientDependent: true
@AbapCatalog.Buffering.status: #SWITCHED_OFF
define view zcdsv_annotation_simple as select from snwd_so
{
key so_id as customer_id,
@Semantics.currencyCode: true
currency_code,
@Semantics.amount.currencyCode: 'currency_code'
gross_amount
}
ABAP CLASSES
ZCL_AMDP_SIMPLE_00
METHOD get_customer_infos
by DATABASE PROCEDURE
FOR HDB
LANGUAGE SQLSCRIPT
options read-ONLY
using snwd_bpa snwd_so.
declare lv_zero integer;
lv_zero := 0;
et_customer_info =
SELECT BP_ID as customer_id,
COMPANY_NAME as customer_name,
SO.CURRENCY_CODE,
SUM( SO.GROSS_AMOUNT )
as TOTAL_GROSS_AMOUNT
,
( 1 / :lv_zero ) as undefined
FROM SNWD_BPA AS BPA
INNER JOIN SNWD_SO AS SO
ON SO.BUYER_GUID = BPA.NODE_KEY
GROUP BY BP_ID, COMPANY_NAME, SO.CURRENCY_CODE
ORDER BY BP_ID;
ENDMETHOD.
ENDCLASS.
ZCL_A4H1_AMDP_BADI
INTERFACES if_badi_interface .
INTERFACES zif_a4h1_amdp_badi .
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
METHOD zif_a4h1_amdp_badi~get_customer_infos
BY DATABASE PROCEDURE
FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING snwd_so snwd_bpa.
et_customer_info =
SELECT BP_ID as customer_id,
COMPANY_NAME as customer_name,
SO.CURRENCY_CODE,
SUM( SO.GROSS_AMOUNT )
as TOTAL_GROSS_AMOUNT
FROM SNWD_BPA AS BPA
INNER JOIN SNWD_SO AS SO
ON SO.BUYER_GUID = BPA.NODE_KEY
GROUP BY BP_ID, COMPANY_NAME, SO.CURRENCY_CODE
ORDER BY BP_ID;
ENDMETHOD.
ENDCLASS.
ZCL_A4H1_BADI_IMPL_01
PRIVATE SECTION.
ENDCLASS.
METHOD zif_a4h1_amdp_badi~get_customer_infos
BY DATABASE PROCEDURE
FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING snwd_so snwd_bpa.
et_customer_info =
SELECT BP_ID as customer_id,
COMPANY_NAME as customer_name,
SO.CURRENCY_CODE,
SUM( SO.GROSS_AMOUNT )
as TOTAL_GROSS_AMOUNT
FROM SNWD_BPA AS BPA
INNER JOIN SNWD_SO AS SO
ON SO.BUYER_GUID = BPA.NODE_KEY
GROUP BY BP_ID, COMPANY_NAME, SO.CURRENCY_CODE
HAVING SUM( SO.GROSS_AMOUNT ) > 10000000
ORDER BY BP_ID;
ENDMETHOD.
ENDCLASS.
ZCL_A4H1_CALL_AMDP_BADI
ENDCLASS.
METHOD call_badi
by DATABASE PROCEDURE
FOR HDB
LANGUAGE SQLSCRIPT
options read-only
using ZA4H1_AMDP_BADI=>get_customer_infos.
call "ZA4H1_AMDP_BADI=>GET_CUSTOMER_INFOS"(
et_customer_info => et_customer_info );
ENDMETHOD.
ENDCLASS.
ZCL_A4H1_E2E_CUST_OPEN_INVOICE
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING snwd_bpa snwd_so_inv_head snwd_so_inv_item.
-- declare a local variable
declare lv_today date;
-- get current date for conversion
select current_date into lv_today from dummy;
-- select relevant invoice items
lt_relevant_items =
select
i.client as client,
i.node_key as inv_i_guid,
h.node_key as inv_guid,
bpa.node_key as buyer_guid,
bpa.bp_id as customer_id,
h.created_at as invoice_date,
i.gross_amount,
i.currency_code
from snwd_so_inv_item as i
join snwd_so_inv_head as h
on i.client = h.client
and i.parent_key = h.node_key
join snwd_bpa as bpa
on h.client = bpa.client
and h.buyer_guid = bpa.node_key
where h.client = :iv_client
and bpa.bp_id = :iv_bupaid
and h.payment_status = '';
--convert gross amount of items to currency 'USD'
et_conv_items = ce_conversion( :lt_relevant_items,
[ family = 'currency',
method = 'ERP',
steps =
'shift,convert,shift_back',
source_unit_column =
"CURRENCY_CODE" ,
output_unit_column =
"CURRENCY_CODE_CONV",
target_unit = 'USD',
10
reference_date =
:lv_today,
client = :iv_client ],
[gross_amount] ) ;
ENDMETHOD.
call
"ZCL_A4H1_E2E_CUST_OPEN_INVOICE=>GET_CURR_CONV_RELEVANT_ITEMS"
(
iv_client => :iv_client,
iv_bupaid => :iv_bupaid,
et_conv_items => :lt_converted_items );
--aggregated gross amounts per sales order invoice
et_invinfo =
select
customer_id,
so_id as order_id,
invoice_date,
currency_code_conv as currency_code,
sum( conv_items.gross_amount ) as sum_gross_amount
from :lt_converted_items as conv_items
join snwd_so_inv_head as h
on h.client = conv_items.client
and h.node_key = conv_items.inv_guid
join snwd_so as so
on so.client = h.client
and so.node_key = h.so_guid
group by customer_id, so_id, invoice_date,
currency_code_conv
order by so_id asc;
ENDMETHOD.
11
ENDCLASS.
ZCL_ZA4H1_E2E_CUST_INF_DPC_EXT
13
et_invinfo = DATA(lt_entityset)
).
"provide the resultset of the AMDP to the tabular
"tabular output parameter
et_entityset = lt_entityset.
ENDMETHOD.
METHOD customerinfos_get_entity.
DATA ls_cust_classification TYPE
zcl_za4h1_e2e_cust_inf_mpc=>ts_customerclassification.
"Entity can only be accessed via the navigation property
ToCustomerInfo
"of entity type CustomerClassification and if the key
CustomerId is provided
IF iv_source_name <>
zcl_za4h1_e2e_cust_inf_mpc=>gc_customerclassification.
RAISE EXCEPTION TYPE /iwbep/cx_mgw_tech_exception.
ENDIF.
io_tech_request_context->get_converted_source_keys(
IMPORTING
es_key_values = ls_cust_classification ).
14
REPORT zr_adbc_proc_call.
"lot of type definitions
TYPES:
BEGIN OF ty_overview,
value TYPE string,
table TYPE string,
END OF ty_overview,
BEGIN OF ty_invoice_header,
invoice_guid TYPE snwd_so_inv_head-node_key,
created_at
TYPE snwd_so_inv_head-created_at,
paid_at
TYPE snwd_so_inv_head-changed_at,
buyer_guid
TYPE snwd_so_inv_head-buyer_guid,
END OF ty_invoice_header .
TYPES:
BEGIN OF ty_invoice_item,
item_guid
TYPE snwd_so_inv_item-node_key,
invoice_guid TYPE snwd_so_inv_head-node_key,
product_guid TYPE snwd_so_inv_item-product_guid,
gross_amount TYPE snwd_so_inv_item-gross_amount,
currency_code TYPE snwd_so_inv_item-currency_code,
END OF ty_invoice_item .
TYPES:
BEGIN OF ty_customer_info,
customer_guid TYPE snwd_bpa-node_key,
customer_id
TYPE snwd_bpa-bp_id,
customer_name TYPE snwd_bpa-company_name,
15
country
TYPE snwd_ad-country,
postal_code
TYPE snwd_ad-postal_code,
city
TYPE snwd_ad-city,
END OF ty_customer_info .
TYPES:
tt_invoice_header TYPE STANDARD TABLE OF ty_invoice_header
WITH KEY invoice_guid .
TYPES:
tt_invoice_item TYPE STANDARD TABLE OF ty_invoice_item .
TYPES:
tt_customer_info TYPE STANDARD TABLE OF ty_customer_info .
DATA
DATA
DATA
DATA
DATA
DATA
DATA
lv_stmt = | CALL
"SAPHANAABAP"."ZCL_DEMO_PAID_ON_DATE_AMDP=>PAID_ON_DATE" | &&
|
( '20140912', NULL, NULL, NULL
) WITH OVERVIEW |.
TRY.
lo_stmt = NEW cl_sql_statement( ).
lo_res = lo_stmt->execute_query( lv_stmt ).
"get a reference of the overview table and prepare the
result set
lo_res->set_param_table( REF #( lt_overview ) ).
"retrieve the overview table
lo_res->next_package( ).
LOOP AT lt_overview INTO DATA(ls_overview).
"select from the corresponding DB table listed in the
overview table
DATA(lo_res_tab) = lo_stmt->execute_query( | select *
from { ls_overview-table }| ).
IF ls_overview-value CS 'ET_INVOICE_ITEM'.
"prepare the result set
16
value =
ZR_ADBC_SIMPLE
REPORT zr_adbc_simple.
TYPES:
BEGIN OF ty_res,
bp_id
company_name
currency_code
total_gross_amount
END OF ty_res.
DATA
DATA
DATA
DATA
lv_stmt
lo_stmt
lo_res
lt_result
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
snwd_bpa-bp_id,
snwd_bpa-company_name,
snwd_so-currency_code,
snwd_so-gross_amount,
string.
REF TO cl_sql_statement.
REF TO cl_sql_result_set.
STANDARD TABLE OF ty_res WITH EMPTY KEY.
ON SO.BUYER_GUID = BPA.NODE_KEY
| &&
| GROUP BY BP_ID, COMPANY_NAME, SO.CURRENCY_CODE
|.
TRY.
"1. create statement object
lo_stmt = NEW cl_sql_statement( ).
"2. execute the query
lo_res = lo_stmt->execute_query( lv_stmt ).
"3. set the output parameter
lo_res->set_param_table( REF #( lt_result ) ).
"4. fetch the result
lo_res->next_package( ).
"5. release the resources
lo_res->close( ).
CATCH cx_sql_exception INTO DATA(lx).
"do some meaningful error handling
WRITE: lx->sql_message.
ENDTRY.
cl_demo_output=>display_data( lt_result ).
ZR_AMDP_01_SIMPLE_CALL
REPORT zr_amdp_01_simple_call.
DATA(lo_amdp) = NEW zcl_amdp_simple_00( ).
TRY.
lo_amdp->get_customer_infos(
IMPORTING
et_customer_info = DATA(lt_result) ).
cl_demo_output=>display_data( lt_result ).
CATCH cx_amdp_execution_failed INTO DATA(lx).
"do some meaningful error handling
WRITE: 'Error occurred during execution of an AMDP - do
something!',
/ 'SQL Error Message:',
18
/ |{ lx->sql_message }|.
ENDTRY.
ZR_AMDP_BADI_CALL
REPORT zr_amdp_badi_call.
data lt_cust_info type zif_a4h1_amdp_badi=>tt_customer_info.
"call the AMDP BAdI
zcl_a4h1_call_amdp_badi=>call_badi( IMPORTING et_customer_info
= lt_cust_info ).
value =
ZR_AMDP_CALL
REPORT zr_amdp_call.
DATA(lo_info_list) = NEW zcl_demo_paid_on_date_amdp( ).
lo_info_list->paid_on_date(
EXPORTING
iv_payment_date = '20140912'
IMPORTING
et_customer_info = DATA(lt_cust_info_amdp)
et_invoice_header = DATA(lt_inv_head_amdp)
et_invoice_item
= DATA(lt_inv_item_amdp) ).
cl_demo_output=>display_data( name = 'Customer Info'
value = lt_cust_info_amdp ).
ABAP INTERFACES
ZIF_A4H1_AMDP_BADI
INTERFACE zif_a4h1_amdp_badi
PUBLIC .
INTERFACES if_badi_interface .
TYPES:
BEGIN OF ty_customer_info,
customer_id
TYPE snwd_bpa-bp_id,
customer_name
TYPE snwd_bpa-company_name,
currency_code
TYPE snwd_so-currency_code,
total_gross_amount TYPE snwd_so-gross_amount,
END OF ty_customer_info ,
19
ENDINTERFACE.
20
www.sap.com