Introduction to the currency_conversion function in SAP CDS view

SAP ABAP CDS ViewThe function of currency_conversionis a function for performing currency conversion in CDS (Core Data Services) view. In SAP systems, it is often necessary to convert between different currencies in order to present consistent currency amounts in reports and analysis. currency_conversionFunctions allow defining currency conversion rules in CDS views to convert values ​​from one currency to another in query results. This is useful in multi-country or multi-currency business environments.

currency_conversionThe syntax of the function is as follows:

currency_conversion(
  amount       = source_amount,
  source_cur   = source_currency,
  target_cur   = target_currency,
  validitydate = date,
  factor       = factor_type,
  decimal      = rounding_mode
) as converted_amount

Let's elaborate on the meaning and function of these parameters:

  1. source_amount: The original amount that needs currency conversion. It can be a field or a constant value in a CDS view.

  2. source_currency: The currency code of the original amount. It can be a field or a constant value in a CDS view.

  3. target_currency: The target currency code, which is the currency to convert the amount to. It can be a field or a constant value in a CDS view.

  4. validitydate: An optional parameter that specifies the effective date for the conversion rate. If this parameter is not provided, the system will use the current date.

  5. factor_type: Optional parameter to specify which type of exchange rate factor to use for conversion. For example, you can choose to use average rates, end-of-month rates, yearly rates, etc. If this parameter is not provided, the system will use the default exchange rate type.

  6. rounding_mode: An optional parameter that specifies how the converted amount is rounded. For example, you can specify rounding or truncation. If this parameter is not provided, the system will use the default rounding method.

  7. converted_amount: As a result, this name is used to store the transformed amount, which will become part of the CDS view and returned in the query results.

Now, let us illustrate the usage and effect of the function through an example currency_conversion. Suppose we have a SAP system that contains a SalesOrderCDS view named CDS that displays information about a sales order, including the order amount and order currency. We want to convert the order amount from the order currency to the local currency so we can view the order amount in the local currency.

Suppose we have the following data:

order number order amount order currency
1001 1000 USD
1002 1500 EUR
1003 2000 GBP

We want to convert these order amounts into local currency (assume local currency is CNY), and assume the conversion rates are 1 USD = 6.5 CNY, 1 EUR = 7.8 CNY, 1 GBP = 8.5 CNY.

currency_conversionFirst, to use a function for currency conversion in a CDS view , we can define the view like this:

@AbapCatalog.sqlViewName: 'ZSALESORDER'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Sales Order View'
define view ZSalesOrder as select from sflights as flights {
  key flights.carrid,
  key flights.connid,
  flights.fldate,
  currency_conversion(
    amount = flights.price,
    source_cur = flights.currency,
    target_cur = 'CNY',
    validitydate = flights.fldate
  ) as local_amount
} where flights.price is not null;

In the CDS view above, we used currency_conversiona function to convert a field flightsin the table price(OrderAmount) from currencya field (OrderCurrency) to the local currency CNYand stored the result in local_amounta new field called .

Now, when we query this CDS view:

select carrid, connid, fldate, price, currency, local_amount
from ZSalesOrder;

The query results will look like this:

carrid connid fldate price currency local_amount
AA 0017 2023-01-12 1000 USD 6500
LH 0400 2023-02-28 1500 EUR 11700
BA 0080 2023-03-15 2000 GBP 17000

In the query results, local_amountthe field shows the value of the order amount in local currency (CNY), converted using the corresponding exchange rate. For example, the order amount in the first line is converted from USD to CNY, and the result is 6500 CNY.

Summarize

SAP ABAP CDS ViewThe currency_conversionfunction is a powerful currency conversion tool that allows flexible currency conversion rules to be defined in CDS views, ensuring accurate presentation of data in a multi-currency environment. By judicious use of currency_conversionfunctions, enterprises can better manage multi-country business and get consistent currency amounts in reports and analysis.

Guess you like

Origin blog.csdn.net/i042416/article/details/132019216