SD の伝票フロー「受注 → 出荷 → 請求」の最後。ここから請求書という紙 (PDF) が出ます。 作りは 受注伝票とほぼ同じで、違いは帳票を開くボタンを持っていることだけ。 そしてこのアプリは、7 本の中で唯一「アノテーションでは書けない要件」に当たった例でもあります (選択した複数件のまとめ出力)。その境界の話も含めて読んでください。
番号を押すと対応する反対側が光ります。 同じ「帳票を開く」リンクがヘッダ帯・基本情報・一覧の 3 箇所にあります。 同じ 1 行を 3 箇所に置いているだけで、リンクの実装は 1 つもありません。
1
2
3
4
5
6
@UI.DataPoint + @UI.HeaderFacetsCriticality: postingCriticalityFieldGroup #Report を HeaderFacets 経由で@UI.FacetsFieldGroup #General の先頭UI.DataFieldWithUrl をもう 1 箇所に置いただけitems/@UI.LineItem@UI.LineItem の 1 要素目
using ShowcaseService as srv from './showcase-service';
/**
* 請求伝票: List Report + Object Page。
*
* SD の伝票フロー「受注 → 出荷 → 請求」の最後で、ここから請求書という
* 紙 (PDF) が出る。受注伝票アプリ (annotations-salesorder.cds) と作りは同じで、
* 違うのは **帳票を開くボタンを持っている**ことだけ。
*
* そのボタンも画面のコードは書いていない。UI.DataFieldWithUrl を
* Identification (= Object Page のヘッダのアクション領域) に置くと、
* Fiori Elements がボタンとして描き、押すと URL を開く。
* 一覧からも 1 行ずつ開けるように、LineItem の **先頭** にも同じものを置いている
* (一覧の目的が「請求書を出す」ことなので、右端に置くと導線として使えない)。
*
* 一方で「選んだ複数件をまとめて 1 本の PDF にする」だけはアノテーションで書けない。
* どの行が選択されているかは画面側の状態で、CDS からは見えないため。そこだけ
* Fiori Elements の custom action を 1 本足してある (app/billing/webapp)。
*/
annotate srv.BillingDocs with @(
UI: {
HeaderInfo: {
TypeName : '請求伝票',
TypeNamePlural : '請求伝票',
Title : { Value: ID },
Description : { Value: payer.name }
},
SelectionFields: [ ID, payer_ID, billingDate, postingStatusText, salesOrder_ID ],
DataPoint #GrossValue: {
Value : grossValue,
Title : '請求金額 (税込)'
},
DataPoint #PostingStatus: {
Value : postingStatusText,
Title : '会計転記',
Criticality : postingCriticality
},
LineItem: [
/*
* 帳票リンクは **列の先頭** に置く。
* 請求伝票一覧の実務上の目的は「この伝票の請求書を出す」ことなので、
* それが右端まで横スクロールしないと見えない位置にあると導線として死ぬ。
* Fiori Elements は LineItem の並び順をそのまま列順にするので、
* 先頭に書けばそれだけで左端に固定される (画面のコードは書かない)。
*/
{ $Type: 'UI.DataFieldWithUrl', Value: pdfLinkText, Url: pdfUrl, Label: '帳票' },
{ Value: ID, Label: '請求伝票' },
{ Value: billingType, Label: '請求タイプ' },
{ Value: billingDate, Label: '請求日' },
{ Value: payer_ID, Label: '請求先' },
{ Value: payer.name, Label: '請求先名' },
{ Value: salesOrder_ID, Label: '参照受注' },
{ Value: netValue, Label: '正味額' },
{ Value: taxAmount, Label: '消費税額' },
{ Value: grossValue, Label: '請求金額' },
{ Value: paymentTermsText, Label: '支払条件' },
{ Value: dueDate, Label: '支払期日' },
{ Value: accountingDoc, Label: '会計伝票' },
{ Value: postingStatusText, Label: '会計転記', Criticality: postingCriticality }
],
// 帳票へのリンクはヘッダ帯に出す。
// Identification に DataFieldWithUrl を置いてもヘッダのボタンにはならない
// (Fiori Elements V4 がヘッダのアクションとして扱うのは DataFieldForAction と
// DataFieldForIntentBasedNavigation だけ) ので、HeaderFacets 経由で載せる。
FieldGroup #Report: { Data: [
{ $Type: 'UI.DataFieldWithUrl', Value: pdfLinkText, Url: pdfUrl, Label: '帳票' }
]},
HeaderFacets: [
{ $Type: 'UI.ReferenceFacet', ID: 'HeaderGross', Target: '@UI.DataPoint#GrossValue' },
{ $Type: 'UI.ReferenceFacet', ID: 'HeaderPosting', Target: '@UI.DataPoint#PostingStatus' },
{ $Type: 'UI.ReferenceFacet', ID: 'HeaderReport', Target: '@UI.FieldGroup#Report' }
],
Facets: [
{ $Type: 'UI.ReferenceFacet', ID: 'GeneralFacet', Label: '基本情報',
Target: '@UI.FieldGroup#General' },
{ $Type: 'UI.ReferenceFacet', ID: 'PaymentFacet', Label: '支払条件',
Target: '@UI.FieldGroup#Payment' },
{ $Type: 'UI.ReferenceFacet', ID: 'ItemsFacet', Label: '明細',
Target: 'items/@UI.LineItem' }
],
FieldGroup #General: { Data: [
{ $Type: 'UI.DataFieldWithUrl', Value: pdfLinkText, Url: pdfUrl, Label: '帳票' },
{ Value: ID },
{ Value: billingType },
{ Value: billingDate },
{ Value: salesOrder_ID },
{ Value: payer_ID },
{ Value: payer.name, Label: '請求先名' },
{ Value: netValue },
{ Value: taxAmount },
{ Value: grossValue }
]},
FieldGroup #Payment: { Data: [
{ Value: paymentTerms },
{ Value: paymentTermsText },
{ Value: dueDate },
{ Value: accountingDoc },
{ Value: postingStatusText, Criticality: postingCriticality }
]}
}
);
annotate srv.BillingDocItems with @(
UI: {
HeaderInfo: {
TypeName : '請求明細',
TypeNamePlural : '請求明細',
Title : { Value: itemNo },
Description : { Value: material.description }
},
LineItem: [
{ Value: itemNo, Label: '明細番号' },
{ Value: material_ID, Label: '品目' },
{ Value: material.description, Label: '品目テキスト' },
{ Value: quantity, Label: '数量' },
{ Value: unit, Label: '単位' },
{ Value: unitPrice, Label: '単価' },
{ Value: netValue, Label: '正味額' },
{ Value: taxRate, Label: '税率 (%)' },
{ Value: taxAmount, Label: '消費税額' }
],
Facets: [
{ $Type: 'UI.ReferenceFacet', ID: 'ItemDetail', Label: '明細詳細',
Target: '@UI.FieldGroup#ItemDetail' }
],
FieldGroup #ItemDetail: { Data: [
{ Value: itemNo },
{ Value: material_ID },
{ Value: material.description, Label: '品目テキスト' },
{ Value: quantity },
{ Value: unit },
{ Value: unitPrice },
{ Value: netValue },
{ Value: taxRate },
{ Value: taxAmount }
]}
}
);
annotate srv.BillingDocs with {
ID @title: '請求伝票';
billingType @title: '請求タイプ';
billingDate @title: '請求日';
netValue @title: '正味額' @Measures.ISOCurrency: currency_code;
taxAmount @title: '消費税額' @Measures.ISOCurrency: currency_code;
grossValue @title: '請求金額 (税込)' @Measures.ISOCurrency: currency_code;
paymentTerms @title: '支払条件コード';
paymentTermsText @title: '支払条件';
dueDate @title: '支払期日';
accountingDoc @title: '会計伝票';
postingStatus @title: '会計転記 (コード)';
postingStatusText @title: '会計転記';
salesOrder @title: '参照受注';
payer @title: '請求先' @(Common: {
Text : payer.name,
TextArrangement : #TextSeparate,
ValueList : {
CollectionPath : 'Customers',
Label : '請求先',
Parameters : [
{ $Type: 'Common.ValueListParameterInOut', LocalDataProperty: payer_ID, ValueListProperty: 'ID' },
{ $Type: 'Common.ValueListParameterDisplayOnly', ValueListProperty: 'name' },
{ $Type: 'Common.ValueListParameterDisplayOnly', ValueListProperty: 'city' },
{ $Type: 'Common.ValueListParameterDisplayOnly', ValueListProperty: 'segment' }
]
}
});
}
annotate srv.BillingDocItems with {
itemNo @title: '明細番号';
quantity @title: '数量';
unit @title: '単位';
unitPrice @title: '単価' @Measures.ISOCurrency: currency_code;
netValue @title: '正味額' @Measures.ISOCurrency: currency_code;
taxRate @title: '税率 (%)';
taxAmount @title: '消費税額' @Measures.ISOCurrency: currency_code;
material @title: '品目';
}
{
"_version": "1.60.0",
"sap.app": {
"id": "showcase.billing",
"type": "application",
"applicationVersion": { "version": "1.0.0" },
"title": "請求伝票",
"description": "請求 — List Report + Object Page。行とヘッダのボタンから請求書 (PDF) を開く",
"dataSources": {
"mainService": {
"uri": "/odata/v4/showcase/",
"type": "OData",
"settings": { "odataVersion": "4.0" }
}
}
},
"sap.ui": {
"technology": "UI5",
"deviceTypes": { "desktop": true, "tablet": true, "phone": true }
},
"sap.ui5": {
"dependencies": {
"minUI5Version": "1.120.0",
"libs": { "sap.m": {}, "sap.ui.core": {}, "sap.fe.templates": {} }
},
"models": {
"": {
"dataSource": "mainService",
"preload": true,
"settings": { "operationMode": "Server", "autoExpandSelect": true, "earlyRequests": true }
}
},
"routing": {
"routes": [
{ "pattern": ":?query:", "name": "BillingDocsList", "target": "BillingDocsList" },
{ "pattern": "BillingDocs({key}):?query:", "name": "BillingDocsObjectPage", "target": "BillingDocsObjectPage" },
{ "pattern": "BillingDocs({key})/items({key2}):?query:", "name": "BillingDocItemsObjectPage", "target": "BillingDocItemsObjectPage" }
],
"targets": {
"BillingDocsList": {
"type": "Component",
"id": "BillingDocsList",
"name": "sap.fe.templates.ListReport",
"options": {
"settings": {
"contextPath": "/BillingDocs",
"variantManagement": "Page",
"initialLoad": "Enabled",
"navigation": {
"BillingDocs": { "detail": { "route": "BillingDocsObjectPage" } }
},
"controlConfiguration": {
"@com.sap.vocabularies.UI.v1.LineItem": {
"tableSettings": { "selectionMode": "ForceMulti" },
"actions": {
"printInvoices": {
"text": "選択した請求書をまとめて出力",
"press": "showcase.billing.ext.InvoiceActions.onPrintSelected",
"requiresSelection": true,
"enabled": true,
"visible": true
}
}
}
}
}
}
},
"BillingDocsObjectPage": {
"type": "Component",
"id": "BillingDocsObjectPage",
"name": "sap.fe.templates.ObjectPage",
"options": {
"settings": {
"contextPath": "/BillingDocs",
"navigation": {
"items": { "detail": { "route": "BillingDocItemsObjectPage" } }
}
}
}
},
"BillingDocItemsObjectPage": {
"type": "Component",
"id": "BillingDocItemsObjectPage",
"name": "sap.fe.templates.ObjectPage",
"options": { "settings": { "contextPath": "/BillingDocs/items" } }
}
}
}
}
}
押すと PDF が開くボタンですが、押したときの処理はどこにも書いていません。 CDS の projection で行ごとの URL 文字列を作り、それをリンクとして置いているだけです。
@readonly
entity BillingDocs as projection on db.BillingDocuments {
*,
items : redirected to BillingDocItems,
'請求書 (PDF) を開く' as pdfLinkText : String(30),
'/report/billing/' || ID || '.pdf' as pdfUrl : String(60),
case postingStatus when 'C' then 3 else 2 end as postingCriticality : Integer,
case postingStatus when 'C' then '転記済' else '未転記' end as postingStatusText : String(10)
};
{ $Type: 'UI.DataFieldWithUrl', Value: pdfLinkText, Url: pdfUrl, Label: '帳票' }
Value がリンクの文字、Url が飛び先で、どちらも項目名です。
固定文字列ではなく項目にしてあるので、多言語化も行ごとの出し分けもデータ側で完結します。
受注一括取込のテンプレート配布リンクとまったく同じ手です。
HeaderFacets を経由する@UI.Identification (=個票のアクション領域) に置けばヘッダに出そうですが、
出ません。Fiori Elements V4 がヘッダのアクションとして扱うのは
DataFieldForAction と DataFieldForIntentBasedNavigation の 2 種類だけで、
DataFieldWithUrl は対象外だからです。
そこで FieldGroup #Report という 1 項目だけのグループを作り、
HeaderFacets から参照してヘッダ帯に載せています (画面マップの 3)。
エラーも警告も出ないので、知らないと「書いたのに出ない」で止まります。
帳票そのものはサーバ側で生成しています (srv/pdf-render.js)。
SAP でいう出力タイプ RD00 (請求書) にあたる位置づけで、
本番なら ADS (Adobe Document Services) が担当する層です。
税率と税額を明細ごとに持っているのは、適格請求書 (インボイス) が 「税率ごとに区分した対価の額」と「税率ごとの消費税額」を要求するためです。 伝票の合計だけ持つ設計にすると、軽減税率が 1 行でも混ざった瞬間に 適格請求書として成立しなくなります。帳票の要件がデータモデルを決めている例です。
srv/pdf-export.cds で
Capabilities.SupportedFormats に application/pdf を入れ、
PDF.Features で能力を申告すると、UI 側の設定 0 でボタンに現れます。
アノテーションは短縮名で書くのが必須で、
Org.OData.Capabilities.V1.… と完全名で書くと CSN には載るのに
$metadata から黙って消えます。
「一覧で選んだ複数件の請求書を、まとめて 1 本の PDF にしたい」—— SAP でいえば VF31 (請求文書の出力再発行) を範囲指定で流したときの動きです。 これはアノテーションでは書けません。理由は 1 つで、 「どの行が選択されているか」は画面側の状態であって、CDS からは見えないからです。
Fiori Elements はこの種の穴を custom action で塞ぐようにできています。 manifest に 1 ブロック足すとテンプレートのツールバーにボタンが増え、押すと自分の JavaScript が呼ばれます。
"controlConfiguration": {
"@com.sap.vocabularies.UI.v1.LineItem": {
"tableSettings": { "selectionMode": "ForceMulti" },
"actions": {
"printInvoices": {
"text": "選択した請求書をまとめて出力",
"press": "showcase.billing.ext.InvoiceActions.onPrintSelected",
"requiresSelection": true,
"enabled": true,
"visible": true
}
}
}
}
sap.ui.define([
"sap/m/MessageBox",
"sap/m/MessageToast"
], function (MessageBox, MessageToast) {
"use strict";
/**
* 請求伝票一覧 (List Report) のツールバーに足した custom action の実装。
*
* このアプリで画面のコードを書いているのはこのファイルだけで、あとは
* すべてアノテーション任せ (srv/annotations-billing.cds)。1 件ぶんの帳票は
* UI.DataFieldWithUrl だけで出せるが、**まとめ出力は「一覧で選んだ行」という
* 画面側の状態が要る**ので、アノテーションだけでは組み立てられない。
* Fiori Elements はこういう穴を custom action で塞ぐようにできていて、
* manifest.json の controlConfiguration に 1 ブロック足すとテンプレートの
* ツールバーにボタンが増え、押すとここが呼ばれる。
*
* 呼び出し規約は sap.fe が決めている (sap/fe/core/helpers/FPMHelper.js の
* actionWrapper):
* fn(bindingContext, selectedContexts, listBinding)
* this は ExtensionAPI に bind された状態で渡ってくる。
*/
/** 1 回のまとめ出力に載せる上限。URL 長と PDF 生成時間の両方に効く */
const MAX_DOCS = 50;
return {
/**
* 選択された請求伝票の請求書を 1 本の PDF にまとめて開く。
*
* SAP でいえば VF31 (請求文書の出力再発行) を範囲指定で流したときの動きで、
* 選んだ伝票ぶんの帳票が伝票ごとに改ページされて 1 つのスプールに出る。
* 束ねる処理はサーバ側 (srv/pdf-middleware.js) にあり、ここがやるのは
* 「選択行から伝票番号を拾って URL を組む」ことだけ。
*
* @param {sap.ui.model.odata.v4.Context} oBindingContext 一覧では undefined
* @param {sap.ui.model.odata.v4.Context[]} aSelectedContexts 選択された行
*/
onPrintSelected: function (oBindingContext, aSelectedContexts) {
const aContexts = aSelectedContexts || [];
// requiresSelection: true があるので通常は 0 件で押せないが、
// ボタンの有効化は画面側の都合なので、実処理側でも一応見る
if (!aContexts.length) {
MessageToast.show("請求伝票を選択してください。");
return;
}
if (aContexts.length > MAX_DOCS) {
MessageBox.warning(
"一度にまとめて出力できるのは " + MAX_DOCS + " 件までです " +
"(選択: " + aContexts.length + " 件)。絞り込んでから実行してください。"
);
return;
}
const aIds = aContexts.map(function (oContext) {
return oContext.getProperty("ID");
}).filter(Boolean);
if (!aIds.length) { return; }
// 伝票番号はカンマ区切りで 1 本の URL に載せる。番号そのものは
// 英数字とハイフンだけだが、将来コード体系が変わっても壊れないよう
// 1 件ずつエスケープしてから繋ぐ (区切りのカンマは素のまま残す)
const sUrl = "/report/billing/" +
aIds.map(encodeURIComponent).join(",") + ".pdf";
// 開き方は **window.open ではなく <a> のクリック**。
// window.open はクリック直後の同期呼び出しでもポップアップブロックの
// 対象になり (Chrome のサイト別設定「ポップアップとリダイレクト」が
// ブロックだと、ユーザー操作起点でも弾かれて null が返る)、実際
// 本番 (Vercel) ではトーストだけ出て PDF が開かない状態だった。
// target="_blank" のリンク遷移はポップアップ扱いされないので、
// 行の「請求書 (PDF) を開く」(UI.DataFieldWithUrl = 素の <a>) と
// 同じ経路に揃える。ランチパッドのタイルでも同じ罠を踏んでいる (README)。
const oLink = document.createElement("a");
oLink.href = sUrl;
oLink.target = "_blank";
oLink.rel = "noopener";
document.body.appendChild(oLink);
oLink.click();
oLink.remove();
MessageToast.show(aIds.length + " 件の請求書を出力しました。");
}
};
});
| 設定 | 効果 |
|---|---|
"selectionMode": "ForceMulti" | 行の左にチェックボックスを出し、複数選択を常に有効にする |
"press" | 呼ぶ関数をモジュールパスで指定する (showcase.billing.ext.InvoiceActions = webapp/ext/InvoiceActions.js) |
"requiresSelection": true | 1 件も選ばれていないときはボタンを無効にする (押せない状態の実装が不要になる) |
| 関数の引数 | sap.fe が決めている呼び出し規約 —— fn(bindingContext, selectedContexts, listBinding)。this は ExtensionAPI |
case 式で書ける (受注一括取込が全部やっている)srv/pdf-middleware.js にあり、JavaScript がやるのは URL の組み立てだけ| アノテーション / 設定 | 何を決めるか | 画面のどこ | ABAP なら |
|---|---|---|---|
UI.DataFieldWithUrl | リンクとして出す項目 | (3)(5)(8) | 同じ |
@UI.HeaderFacets + FieldGroup | ヘッダ帯にリンクを載せる回避策 | (3) | @UI.facet: purpose: #HEADER |
@UI.DataPoint | ヘッダの数値とステータス | (1)(2) | @UI.dataPoint |
@UI.Facets / FieldGroup | タブと項目グループ | (4) | @UI.facet |
Target: 'items/@UI.LineItem' | 明細テーブル | (6) | 同じ |
@Common.ValueList | 請求先の値ヘルプ | フィルタ | @Consumption.valueHelpDefinition |
Capabilities.SupportedFormats / PDF.Features | 一覧の PDF エクスポート | ツールバー | ADS が申告する能力 |
controlConfiguration.actions (manifest) | ツールバーに自作ボタン | (9) | 同じ (BSP 拡張) |
@readonly | 更新・削除を封じる | 編集ボタンが無い | behavior の read only |
| やること | 起きること | そこから分かること |
|---|---|---|
FieldGroup #Report を HeaderFacets から外し、
同じものを Identification に置く |
ヘッダからリンクが消える | V4 のヘッダアクションは action と intent の 2 種類だけ |
LineItem の帳票リンクを末尾に移す |
右端に行き、横スクロールしないと見えなくなる | 配列の順番が導線の質を決める |
| 一覧で 2 件選んで「まとめて出力」 | 2 伝票ぶんが改ページされた 1 本の PDF になる | custom action は選択行を受け取れる |
"requiresSelection" を false にする |
0 件選択でも押せてしまう | ボタンの有効判定も宣言で書ける (JS 側の防御は保険) |
/report/billing/BD-90000001,BD-90000002.pdf を直接開く |
まとめ出力と同じ PDF が出る | 画面がやっているのは URL の組み立てだけ |