1. 程式人生 > 其它 >如何使用 SAP CDS view 中的 currency conversion 功能

如何使用 SAP CDS view 中的 currency conversion 功能

例如,我開發了一個 CDS view,並自動生成了一個 Fiori Elements 應用,裡面顯示的金額單位是歐元:

我期望利用 CDS view 的貨幣轉換功能,currency conversion,將這些金額的單位轉換成美元。

可以使用這個文件裡提到的轉換函式。

使用如下高亮程式碼:

@AbapCatalog.sqlViewName: 'ZCTRAVELJERRY'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Consumption view from /DMO/I_TRAVEL_U'
@Metadata.allowExtensions: true
@Search.searchable: true
define view Z_C_TRAVEL_DATA_JERRY as select from /DMO/I_Travel_U 
association [1..1] to /DMO/I_Agency as _Agency on $projection.AgencyID = _Agency.AgencyID
association [1..1] to /DMO/I_Customer as _Customer on $projection.CustomerID = _Customer.CustomerID
{       
    key TravelID,

    @ObjectModel.text.association: '_Agency'
    AgencyID,
    CustomerID,
    concat_with_space(_Customer.Title, _Customer.LastName, 1) as Addressee,
    BeginDate,
    EndDate,
    BookingFee,
    TotalPrice,
    CurrencyCode,
    currency_conversion(
        amount => TotalPrice,
        source_currency => CurrencyCode,
        round => 'X',
        target_currency => cast('USD' as abap.cuky( 5 )),
        exchange_rate_date => cast('20200429' as abap.dats),
        error_handling => 'SET_TO_NULL' )
    as PriceInUSD,  
    @Search.defaultSearchElement: true
    @Search.fuzzinessThreshold: 0.90
    Memo,
    Status,
    LastChangedAt,
    /* Associations */
    _Agency,
    _Booking,
    _Currency,
    _Customer
}

最後的效果:

轉換效果如下:藍色是原始的歐元單位,紅色的是通過 currency_conversion 轉換之後的新值:

更多Jerry的原創文章,盡在:"汪子熙":