You are on page 1of 20

ABAP Development for SAP HANA

https://open.sap.com/courses/a4h1

Source Code of Week 1

Version 1 - 15.10.2014

Only Demo content


Without any warranty

Contents
CDS DDL Sources............................................................................................................................................ 3
ZDDLS_CDS_40_ANNOTATION ...................................................................................................................... 4
ABAP Classes.................................................................................................................................................. 5
ZCL_DEMO_PAID_ON_DATE ......................................................................................................................... 6
ZCL_DEMO_PAID_ON_DATE_AMDP ............................................................................................................. 9
ABAP Programs ............................................................................................................................................ 11
ZR_HELLO_WORLD ...................................................................................................................................... 12
ZR_EPM_SET_INVOICE_PAID....................................................................................................................... 13
ZR_PAID_ON_DATE ..................................................................................................................................... 18
ZR_PAID_ON_DATE_AMDP ......................................................................................................................... 19

CDS DDL Sources

ZDDLS_CDS_40_ANNOTATION
@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_DEMO_PAID_ON_DATE
CLASS zcl_demo_paid_on_date DEFINITION
PUBLIC
CREATE PUBLIC .
PUBLIC SECTION.
TYPES:
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,
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
.
METHODS paid_on_date

IMPORTING
VALUE(iv_payment_date)
EXPORTING
VALUE(et_invoice_header)
VALUE(et_invoice_item)
VALUE(et_customer_info)

TYPE d
TYPE tt_invoice_header
TYPE tt_invoice_item
TYPE tt_customer_info .

ENDCLASS.

CLASS zcl_demo_paid_on_date IMPLEMENTATION.

METHOD paid_on_date.
"! selection of invoices paid on a specified date
"! plus business partner and product information
DATA ls_invoice_head TYPE ty_invoice_header.
DATA lt_invoice_item TYPE tt_invoice_item.
DATA lt_customer_info TYPE tt_customer_info.
DATA lv_payment_date_min TYPE timestamp.
DATA lv_payment_date_max TYPE timestamp.
CONVERT DATE iv_payment_date TIME '0000' INTO TIME STAMP
lv_payment_date_min TIME ZONE 'UTC'.
CONVERT DATE iv_payment_date TIME '2359' INTO TIME STAMP
lv_payment_date_max TIME ZONE 'UTC'.
" First we retrieve all invoice header
" which were paid on the requested date
SELECT
node_key
AS invoice_guid
created_at
AS created_at
changed_at
AS paid_at
buyer_guid
FROM
snwd_so_inv_head
INTO ls_invoice_head
WHERE
payment_status = 'P'

AND changed_at BETWEEN lv_payment_date_min AND


lv_payment_date_max.
CLEAR lt_invoice_item.
CLEAR lt_customer_info.
"get items of invoice
SELECT
node_key
AS item_guid
parent_key AS invoice_guid
product_guid
gross_amount
currency_code
FROM snwd_so_inv_item
INTO TABLE lt_invoice_item
WHERE parent_key = ls_invoice_head-invoice_guid.
"get information about the customers
SELECT
bpa~node_key
AS customer_guid
bpa~bp_id
AS customer_id
bpa~company_name AS customer_name
ad~country
ad~postal_code
ad~city
FROM snwd_bpa AS bpa
JOIN snwd_ad AS ad ON ad~node_key = bpa~address_guid
INTO TABLE lt_customer_info
WHERE bpa~node_key = ls_invoice_head-buyer_guid.
APPEND ls_invoice_head
TO et_invoice_header.
APPEND LINES OF lt_invoice_item TO et_invoice_item.
APPEND LINES OF lt_customer_info TO et_customer_info.
ENDSELECT.
"remove duplicates
SORT et_customer_info BY customer_name.
DELETE ADJACENT DUPLICATES FROM et_customer_info.
ENDMETHOD.
ENDCLASS.

ZCL_DEMO_PAID_ON_DATE_AMDP
CLASS zcl_demo_paid_on_date_amdp DEFINITION
PUBLIC
INHERITING FROM zcl_demo_paid_on_date
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb.
METHODS paid_on_date REDEFINITION.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.

CLASS zcl_demo_paid_on_date_amdp IMPLEMENTATION.

METHOD paid_on_date BY DATABASE PROCEDURE FOR HDB LANGUAGE


SQLSCRIPT OPTIONS READ-ONLY
USING snwd_so_inv_head snwd_so_inv_item snwd_bpa snwd_ad.
--sql script
-VALUE(et_invoice_header) TYPE tt_invoice_header
-VALUE(et_invoice_item)
TYPE tt_invoice_item
-VALUE(et_customer_info) TYPE tt_customer_info .
et_invoice_header = SELECT
node_key
AS invoice_guid,
created_at
AS created_at,
changed_at
AS paid_at,
buyer_guid
FROM
snwd_so_inv_head
WHERE
payment_status = 'P'
AND LEFT(changed_at,8) = :iv_payment_date;

et_invoice_item =
SELECT

node_key
AS item_guid,
parent_key AS invoice_guid,
product_guid,
gross_amount,
currency_code
FROM snwd_so_inv_item
WHERE parent_key in ( select invoice_guid from
:et_invoice_header );

--get information about the customers


et_customer_info = SELECT DISTINCT
bpa.node_key
AS customer_guid,
bpa.bp_id
AS customer_id,
bpa.company_name AS customer_name,
ad.country,
ad.postal_code,
ad.city
FROM snwd_bpa AS bpa
JOIN snwd_ad AS ad ON ad.node_key = bpa.address_guid
WHERE bpa.node_key in ( select buyer_guid from
:et_invoice_header )
ORDER BY company_name;
ENDMETHOD.
ENDCLASS.

ABAP Programs

ZR_HELLO_WORLD
REPORT zr_hello_world.
DATA lv_string TYPE string.
DATA lv_name TYPE sy-uname.
lv_name = cl_abap_syst=>get_user_name( ).
lv_string = |Hello { lv_name }, welcome to the ABAP
Development for SAP HANA!|.
WRITE: lv_string.

ZR_EPM_SET_INVOICE_PAID
REPORT zr_epm_set_invoice_paid.
CLASS lcl_set_invoice_paid DEFINITION CREATE PRIVATE.
PUBLIC SECTION.
CLASS-DATA mv_test_mode

TYPE abap_bool.

CLASS-METHODS execute
IMPORTING
iv_bupa_id TYPE snwd_bpa-bp_id
iv_count
TYPE i
EXPORTING
ev_open
TYPE i
et_so
TYPE if_epm_so_header=>tt_node_data
RAISING
cx_epm_api_exception
cx_epm_system_exception.
ENDCLASS.
CLASS lcl_set_invoice_paid IMPLEMENTATION.
METHOD execute.
DATA(mo_message_buffer) =
cl_epm_service_facade=>get_message_buffer( ).
DATA mo_invoice TYPE REF TO if_epm_so_invoice.
mo_invoice ?= cl_epm_service_facade=>get_bo(
if_epm_so_invoice=>gc_bo_name ).
" read the invoices for this bupa
mo_invoice->if_epm_so_invoice_header~query_by_header(
EXPORTING it_sel_par_buyer_ids = VALUE #( ( sign = 'I'
option = 'EQ' low = iv_bupa_id ) )
iv_max_rows = 99999
IMPORTING et_data = DATA(lt_invoices) ).

DATA lt_paid_invoice LIKE lt_invoices.

" find some unpaid invoices


DATA(lv_count) = iv_count.
LOOP AT lt_invoices ASSIGNING FIELD-SYMBOL(<ls_invoice>).
CHECK lv_count > 0.
DATA lt_invoice_key TYPE STANDARD TABLE OF snwd_sonode_key.

IF <ls_invoice>-payment_status = space.
APPEND <ls_invoice>-node_key TO lt_invoice_key.
INSERT <ls_invoice> INTO TABLE lt_paid_invoice.
lv_count = lv_count - 1.
ENDIF.
ENDLOOP.
ev_open = lv_count.
IF lv_count > 0.
RETURN.
ENDIF.
" mark selected as paid
mo_invoice->if_epm_so_invoice_header~action_mark_as_paid(
EXPORTING
it_node_keys
= lt_invoice_key
ii_message_buffer
= mo_message_buffer
IMPORTING
et_node_key_info
= DATA(lt_node_key_info)
).
DATA(lt_messages) = mo_message_buffer->get_messages( ).
LOOP AT lt_messages ASSIGNING FIELD-SYMBOL(<ls_message>).
DATA(lv_text) = <ls_message>->to_string( ).
WRITE: / lv_text.
ENDLOOP.

cl_epm_service_facade=>save(

iv_suppress_commit = mv_test_mode
ii_message_buffer = mo_message_buffer ).
lt_messages = mo_message_buffer->get_messages(
LOOP AT lt_messages ASSIGNING <ls_message>.
lv_text = <ls_message>->to_string( ).
WRITE: / lv_text.
ENDLOOP.

).

mo_invoice>if_epm_so_invoice_header~navigate_to_sales_order(
EXPORTING it_source_node_keys = lt_invoice_key
IMPORTING et_data
= et_so
).
ENDMETHOD.
ENDCLASS.

CLASS ltc_invoice_paid DEFINITION CREATE PRIVATE


FOR TESTING
DURATION MEDIUM
RISK LEVEL DANGEROUS.

PRIVATE SECTION.
METHODS setup.
METHODS teardown.
METHODS do_test FOR TESTING RAISING cx_epm_api_exception
cx_epm_system_exception.
ENDCLASS.
CLASS ltc_invoice_paid IMPLEMENTATION.
METHOD setup.
lcl_set_invoice_paid=>mv_test_mode = abap_true.
ENDMETHOD.

METHOD do_test.

DATA lv_bpa TYPE snwd_bpa-bp_id.


SELECT SINGLE bpa~bp_id
FROM snwd_bpa AS bpa
INNER JOIN snwd_so AS so ON so~buyer_guid =
bpa~node_key
INNER JOIN snwd_so_inv_head AS head ON head~so_guid =
so~node_key
INTO lv_bpa
WHERE head~payment_status = ''.
cl_abap_unit_assert=>assert_subrc(
msg = 'no corresponding data in the DB'
level = if_aunit_constants=>tolerable
quit = if_aunit_constants=>method ).

##no_text

lcl_set_invoice_paid=>execute(
EXPORTING
iv_bupa_id
= lv_bpa
iv_count
= 1
IMPORTING
ev_open
= DATA(lv_open)
et_so
= DATA(lt_so)
).
cl_abap_unit_assert=>assert_initial( act = lv_open ).
cl_abap_unit_assert=>assert_not_initial( act = lt_so ).
ENDMETHOD.
METHOD teardown.
lcl_set_invoice_paid=>mv_test_mode = abap_false.
ROLLBACK WORK.
ENDMETHOD.
ENDCLASS.

PARAMETER bupa TYPE snwd_bpa-bp_id.


PARAMETER num TYPE i DEFAULT 1.

START-OF-SELECTION.
lcl_set_invoice_paid=>execute(
EXPORTING
iv_bupa_id = bupa
iv_count
= num
IMPORTING
ev_open
= DATA(lv_open)
et_so
= DATA(lt_so)
).
IF lv_open > 0.
WRITE: / 'not enough unpaid invoices available'(001).
ELSE.
WRITE: / 'sales orders marked as paid:'(002).
LOOP AT lt_so ASSIGNING FIELD-SYMBOL(<ls_paid>).
WRITE: / <ls_paid>-so_id, <ls_paid>-gross_amount,
<ls_paid>-currency_code.
ENDLOOP.
ENDIF.

ZR_PAID_ON_DATE
PROGRAM zr_paid_on_date.
DATA p_date TYPE d VALUE '20140912'.
DATA(lo_timer) = cl_abap_runtime=>create_hr_timer( ).
DATA(lo_info_list) = NEW zcl_demo_paid_on_date( ).
DATA(t1) = lo_timer->get_runtime( ).
lo_info_list->paid_on_date(
EXPORTING
iv_payment_date
IMPORTING
et_invoice_header
DATA(lt_invoice_head)
et_invoice_item
DATA(lt_invoice_item)
et_customer_info
DATA(lt_customer_info) ).

= p_date
=
=
=

DATA(t2) = lo_timer->get_runtime( ).
DATA(elapsed_time) = ( t2 - t1 ) / 1000.
cl_demo_output=>next_section( title = |Runtime (ABAP): {
elapsed_time } ms.| ).
cl_demo_output=>write_data( name = 'Customer Info' value =
lt_customer_info ).
cl_demo_output=>display( ).

ZR_PAID_ON_DATE_AMDP
PROGRAM zr_paid_on_date_amdp.
DATA p_date TYPE d VALUE '20140912'.
DATA(lo_timer) = cl_abap_runtime=>create_hr_timer( ).
DATA(lo_info_list) = NEW zcl_demo_paid_on_date_amdp( ).
DATA(t1) = lo_timer->get_runtime( ).
lo_info_list->paid_on_date(
EXPORTING
iv_payment_date
IMPORTING
et_invoice_header
DATA(lt_invoice_head)
et_invoice_item
DATA(lt_invoice_item)
et_customer_info
DATA(lt_customer_info) ).

= p_date
=
=
=

DATA(t2) = lo_timer->get_runtime( ).
DATA(elapsed_time) = ( t2 - t1 ) / 1000.
cl_demo_output=>next_section( title = |Runtime (AMDP): {
elapsed_time } ms.| ).
cl_demo_output=>write_data( name = 'Customer Info' value =
lt_customer_info ).
cl_demo_output=>display( ).

www.sap.com

2014 SAP SE or an SAP affiliate company. All rights reserved.


No part of this publication may be reproduced or transmitted in any form
or for any purpose without the express permission of SAP SE or an SAP
affiliate company.
SAP and other SAP products and services mentioned herein as well as their
respective logos are trademarks or registered trademarks of SAP SE (or an
SAP affiliate company) in Germany and other countries. Please see
http://www.sap.com/corporate-en/legal/copyright/index.epx#trademark for
additional trademark information and notices. Some software products
marketed by SAP SE and its distributors contain proprietary software
components of other software vendors.
National product specifications may vary.
These materials are provided by SAP SE or an SAP affiliate company for
informational purposes only, without representation or warranty of any kind,
and SAP SE or its affiliated companies shall not be liable for errors or
omissions with respect to the materials. The only warranties for SAP SE or
SAP affiliate company products and services are those that are set forth in
the express warranty statements accompanying such products and services,
if any. Nothing herein should be construed as constituting an additional
warranty.
In particular, SAP SE or its affiliated companies have no obligation to pursue
any course of business outlined in this document or any related presentation,
or to develop or release any functionality mentioned therein. This document,
or any related presentation, and SAP SEs or its affiliated companies
strategy and possible future developments, products, and/or platform
directions and functionality are all subject to change and may be changed by
SAP SE or its affiliated companies at any time for any reason without notice.
The information in this document is not a commitment, promise, or legal
obligation to deliver any material, code, or functionality. All forward-looking
statements are subject to various risks and uncertainties that could cause
actual results to differ materially from expectations. Readers are cautioned
not to place undue reliance on these forward-looking statements, which
speak only as of their dates, and they should not be relied upon in making
purchasing decisions.

You might also like