Upload
crxcrvs
View
243
Download
0
Embed Size (px)
Citation preview
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 1/20
1
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
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 2/20
ContentsCDS 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
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 3/20
CDS DDL Sources
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 4/20
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
}
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 5/20
ABAP Classes
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 6/20
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
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 7/20
IMPORTING VALUE(iv_payment_date) TYPE d
EXPORTING VALUE(et_invoice_header) TYPE tt_invoice_headerVALUE(et_invoice_item) TYPE tt_invoice_item
VALUE(et_customer_info) 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 STAMPlv_payment_date_min TIME ZONE 'UTC'.
CONVERT DATE iv_payment_date TIME '2359' INTO TIME STAMPlv_payment_date_max TIME ZONE 'UTC'.
" First we retrieve all invoice header " which were paid on the requested date
SELECT node_key AS invoice_guidcreated_at AS created_atchanged_at AS paid_atbuyer_guid
FROM snwd_so_inv_head
INTO ls_invoice_headWHERE
payment_status = 'P'
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 8/20
AND changed_at BETWEEN lv_payment_date_min ANDlv_payment_date_max.
CLEAR lt_invoice_item. CLEAR lt_customer_info.
"get items of invoice SELECT node_key AS item_guidparent_key AS invoice_guidproduct_guidgross_amountcurrency_code
FROM snwd_so_inv_item
INTO TABLE lt_invoice_itemWHERE parent_key = ls_invoice_head-invoice_guid.
"get information about the customers SELECT bpa~node_key AS customer_guidbpa~bp_id AS customer_idbpa~company_name AS customer_name
ad~countryad~postal_codead~city
FROM snwd_bpa AS bpaJOIN snwd_ad AS ad ON ad~node_key = bpa~address_guidINTO TABLE lt_customer_infoWHERE 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.
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 9/20
ZCL_DEMO_PAID_ON_DATE_AMDP
CLASS zcl_demo_paid_on_date_amdp DEFINITION PUBLIC
INHERITING FROM zcl_demo_paid_on_dateFINAL 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 LANGUAGESQLSCRIPT 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
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 10/20
node_key AS item_guid, parent_key AS invoice_guid, product_guid, gross_amount, currency_code
FROM snwd_so_inv_itemWHERE 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 bpaJOIN snwd_ad AS ad ON ad.node_key = bpa.address_guidWHERE bpa.node_key in ( select buyer_guid from
:et_invoice_header ) ORDER BY company_name;
ENDMETHOD. ENDCLASS.
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 11/20
ABAP Programs
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 12/20
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 ABAPDevelopment for SAP HANA!|.
WRITE: lv_string.
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 13/20
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 executeIMPORTING iv_bupa_id TYPE snwd_bpa-bp_idiv_count TYPE i
EXPORTING ev_open TYPE iet_so TYPE if_epm_so_header=>tt_node_data
RAISING cx_epm_api_exceptioncx_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.
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 14/20
" 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_so-node_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_keyii_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(
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 15/20
iv_suppress_commit = mv_test_modeii_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_keyIMPORTING 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.
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 16/20
DATA lv_bpa TYPE snwd_bpa-bp_id.
SELECT SINGLE bpa~bp_idFROM 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_bpaWHERE head~payment_status = ''.
cl_abap_unit_assert=>assert_subrc( msg = 'no corresponding data in the DB' ##no_text
level = if_aunit_constants=>tolerablequit = if_aunit_constants=>method ).
lcl_set_invoice_paid=>execute( EXPORTING iv_bupa_id = lv_bpaiv_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.
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 17/20
START-OF-SELECTION. lcl_set_invoice_paid=>execute( EXPORTING iv_bupa_id = bupa
iv_count = numIMPORTING 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.
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 18/20
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 = p_date
IMPORTING et_invoice_header =
DATA(lt_invoice_head) et_invoice_item =
DATA(lt_invoice_item) et_customer_info =
DATA(lt_customer_info) ).
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( ).
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 19/20
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 = p_date
IMPORTING et_invoice_header =
DATA(lt_invoice_head) et_invoice_item =
DATA(lt_invoice_item) et_customer_info =
DATA(lt_customer_info) ).
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( ).
7/26/2019 OpenSAP a4h1 Week 1 Source Code
http://slidepdf.com/reader/full/opensap-a4h1-week-1-source-code 20/20
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 formor for any purpose without the express permission of SAP SE or an SAPaffiliate company.SAP and other SAP products and services mentioned herein as well as theirrespective logos are trademarks or registered trademarks of SAP SE (or anSAP affiliate company) in Germany and other countries. Please seehttp://www.sap.com/corporate-en/legal/copyright/index.epx#trademark foradditional trademark information and notices. Some software productsmarketed by SAP SE and its distributors contain proprietary softwarecomponents of other software vendors.National product specifications may vary.These materials are provided by SAP SE or an SAP affiliate company forinformational purposes only, without representation or warranty of any kind,and SAP SE or its affiliated companies shall not be liable for errors oromissions with respect to the materials. The only warranties for SAP SE orSAP affiliate company products and services are those that are set forth inthe express warranty statements accompanying such products and services,if any. Nothing herein should be construed as constituting an additionalwarranty.In particular, SAP SE or its affiliated companies have no obligation to pursueany 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 pr esentation, and SAP SE’s or its affiliated companies’
strategy and possible future developments, products, and/or platformdirections and functionality are all subject to change and may be changed bySAP SE or its affiliated companies at any time for any reason without notice.The information in this document is not a commitment, promise, or legalobligation to deliver any material, code, or functionality. All forward-lookingstatements are subject to various risks and uncertainties that could causeactual results to differ materially from expectations. Readers are cautionednot to place undue reliance on these forward-looking statements, whichspeak only as of their dates, and they should not be relied upon in making