Friday 12 July 2019

Currency and Unit conversion in ABAP CDS views

Recently, I’ve been checking some examples of unit and currency conversions based on ABAP CDS views and I noticed is quite common to find people applying manual conversions in the formulas.

A simple example of a days to years conversion is conceived with a multiplication of the original value by 365, for more complex situations like an expansion of this same conversion from days to months and years a CASE statement is implemented to adjust the output of the value.

ABAP CDS Views, SAP ABAP Study Materials, SAP ABAP Certifications, SAP ABAP Learning

But what happens when you have conversion of an amount to different currencies or a unit conversion based on dynamic values?

In these cases is impossible to apply a fixed calculation in the code due to the variations of the currency or unit, but what most part of the consultants don’t know is that ABAP CDS views provide support to set of functions conceived specifically for this kind of scenario:

◈ UNIT_CONVERSION
◈ CURRENCY_CONVERSION

But how these functions work?


Unit conversion has 3 mandatory parameters + 2 optional parameters to support client verification and error handling. The specification is available below:

Formal Parameter Optional  Data Type 
quantity QUAN, DEC, INT1, INT2, INT4, FLTP
source_unit  –  UNIT 
target_unit –  UNIT 
client  X, –  CLNT 
error_handling  CHAR with length 20

Currency conversion has 4 mandatory parameters + 6 optional parameters to support fine adjustments in the output, client verification and error handling. The specification is available below:

Formal Parameter Optional  Data Type 
amount CURR 
source_currency  –  CUKY 
target_currency  –  CUKY 
exchange_rate_date  –  DATS 
exchange_rate_type  CHAR with length 4 
client  X, –  CLNT 
round   CHAR 
decimal_shift  CHAR 
decimal_shift_back   CHAR 
error_handling  CHAR with length 20 

In this short post we are going to create a demo exploring both functions based on two different entities from the Flights demo tables:

◈ SAPLANE
◈ SFLIGHT

ABAP CDS Views, SAP ABAP Study Materials, SAP ABAP Certifications, SAP ABAP Learning

In the diagram above you can find the connection between both tables. Notice that SAPLANE holds the data of the planes and SFLIGHT stores data of the flights.

Example #1: Unit Conversion


For the unit conversion we will use one of the fields from SAPLANE table.

There are 3 valid options based on the Currency/Quantity Fields:

ABAP CDS Views, SAP ABAP Study Materials, SAP ABAP Certifications, SAP ABAP Learning

For this example, I am using a conversion based on Maximum fuel capacity (TANKCAP). The objective is to demonstrate multiple conversions based on the same dimension.

To check the valid options to convert this field you can open the table T006 and search units based on the Volume dimension.

ABAP CDS Views, SAP ABAP Study Materials, SAP ABAP Certifications, SAP ABAP Learning

Checking the data available in the table we can notice TANKCAP is usually referred by Liters (L). Let’s try to create conversions to Cubic Meters (M3) and Cubic Yards (YD3) via this standard function.

The code and delivery are pretty simple, create a new Data Definition via HANA Studio with the name of ZCDS_UNIT_CONVERSION and include the following code.

@AbapCatalog.sqlViewName: 'ZCDSUNITCONV'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'CDS Demo by Felipe Rodrigues'

define view ZCDS_UNIT_CONVERSION
  as select from saplane
{
      @EndUserText.label: 'Plane Type'
  key planetype as PlaneType,

      @EndUserText.label: 'Tank Cap'
      tankcap   as TankCapacity,

      @EndUserText.label: 'Tank Cap (UOM)'
      cap_unit  as CapacityUnit,

      @EndUserText.label: 'Tank Cap (in Cubic Meters)'
      unit_conversion(
        quantity    => tankcap,
        source_unit => cap_unit,
        target_unit => cast('M3' as abap.unit)
      )         as TankCapacityInM3,

      @EndUserText.label: 'Tank Cap (in Cubic Yard)'
      unit_conversion(
        quantity    => tankcap,
        source_unit => cap_unit,
        target_unit => cast('YD3' as abap.unit)
      )         as TankCapacityInYD3
}

Run the Data Preview and check the result:

ABAP CDS Views, SAP ABAP Study Materials, SAP ABAP Certifications, SAP ABAP Learning

Let’s re-conciliate the data from the first record via google assistant.

Liters (L) to Cubic Meters (M3)

ABAP CDS Views, SAP ABAP Study Materials, SAP ABAP Certifications, SAP ABAP Learning

Liters (L) to Cubic Yards (YD3)

ABAP CDS Views, SAP ABAP Study Materials, SAP ABAP Certifications, SAP ABAP Learning

Important Note: Due to a small difference in the number of decimal places in the conversion factor used by SAP and Google you can notice also a small difference between the results, just remember the conversion factor can be adjusted in SAP depending on your needs.

Example #2: Currency Conversion


For the currency conversion we are going to use some of the fields from SFLIGHT table.

ABAP CDS Views, SAP ABAP Study Materials, SAP ABAP Certifications, SAP ABAP Learning

For this example, I am using Airfare (PRICE) and converting the value to Australian Dollars (AUD).

This is a pretty interesting scenario because the prices usually vary depending depending on the flight and company, with the standard function we are able to convert the values dynamically.

To achieve this result let’s create a new Data Definition via HANA Studio with the name of ZCDS_CURRENCY_CONVERSION and include the following code.

@AbapCatalog.sqlViewName: 'ZCDSCURRCONV'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'CDS Demo by Felipe Rodrigues'

define view ZCDS_CURRENCY_CONVERSION
  as select from sflight
{
      @EndUserText.label: 'Airline Code'
  key carrid   as AirlineCode,

      @EndUserText.label: 'Connection No.'
  key connid   as ConnectionNumber,

      @EndUserText.label: 'Flight Date'
  key fldate   as FlightDate,

      @EndUserText.label: 'Price'
      price    as Price,

      @EndUserText.label: 'Currency'
      currency as Currency,

      @EndUserText.label: 'Price (in Australian Dollars)'
      currency_conversion(
        amount             => price,
        source_currency    => currency,
        target_currency    => cast('AUD' as abap.cuky),
        exchange_rate_date => fldate
      )        as PriceInAUD
}

Notice the CURRENCY_CONVERSION has a mandatory parameter named EXCHANGE_RATE_DATE, for this scenario we need to assign the Flight Date for the exchange date, the system is going to analyse the options available in the table TCURR automatically and provide the proper conversion rate based on the date the customer acquired the flight ticket.

Run the Data Preview and check the result below:

ABAP CDS Views, SAP ABAP Study Materials, SAP ABAP Certifications, SAP ABAP Learning

Important Note: The accuracy of your currency conversion will depend on how often you update the Exchange Rate in your SAP system. In my case the conversion is not accurate because this data is from a development environment but if you have updated conversion rates in your system you will find the proper values in the output of this CDS view.

No comments:

Post a Comment