QWeb レポート

レポートは、Odoo のウェブサイトビューのように、HTML/QWebで記述されます。 通常の :ref:`QWebコントロールフローツール <reference/qweb>`を使用できます。PDFレンダリング自体はwkhtmltopdf_によって実行されます。

レポートは レポート アクション を使用して宣言され、アクションに レポート テンプレート を使用して宣言されます。

有用または必要であれば、レポートのレポートに 用紙の書式 を指定することができます。

レポート テンプレート

レポート テンプレートは、常に次の変数を提供します。

time

Python標準ライブラリからの :mod:`python:time`への参照

user

ユーザーがレポートを印刷するための res.user レコード

res_company

現在の user の会社のレコード

website

現在のウェブサイトオブジェクト。(このアイテムは None ですが)

web_base_url

ウェブサーバーのベース URL

context_timestamp

関数 datetime.datetime を UTC1 で取り、レポートを印刷するユーザーのタイムゾーンに変換します。

最小限の実行可能なテンプレート

最小限のテンプレートは以下のようになります:

<template id="report_invoice">
    <t t-call="web.html_container">
        <t t-foreach="docs" t-as="o">
            <t t-call="web.external_layout">
                <div class="page">
                    <h2>Report title</h2>
                    <p>This object's name is <span t-field="o.name"/></p>
                </div>
            </t>
        </t>
    </t>
</template>

external_layout を呼び出すと、レポートにデフォルトのヘッダーとフッターが追加されます。PDFの本文は、<div class="page">``内のコンテンツになります。 テンプレートの ``id は、レポート宣言で指定された名前でなければなりません。例えば、 account. 上記のレポートの eport_invoice です。これはQWeb テンプレートであるため、テンプレートから受け取った docs オブジェクトのすべてのフィールドにアクセスできます。

デフォルトでは、レンダリングコンテキストでは以下の項目も表示されます。

docs

現在のレポートのレコード

doc_ids

list of id for the docs records

doc_model

docs レコードのモデル

テンプレート内の他のレコード/モデルにアクセスしたい場合は、 :ref:`カスタムレポート <reference/reports/custom_reports>`が必要です。 しかしその場合は上記のアイテムが必要になるでしょう

翻訳可能なテンプレート

レポートを翻訳したい場合(たとえばパートナーの言語など)、2つのテンプレートを定義する必要があります。

  • メイン レポート テンプレート

  • 翻訳可能なドキュメント

その後、メインテンプレートから t-lang 属性を言語コード(例えば fren_US など)またはレコードフィールドに設定し、翻訳可能なドキュメントを呼び出すことができます。 翻訳可能な項目(国名など)を使用する場合は、関連するレコードを適切なコンテキストで再度参照する必要があります。 販売条件など)

警告

レポートテンプレートが翻訳可能なレコード項目を使用しない場合は、別の言語でレコードを再参照する必要はありません*。パフォーマンスに影響します。

例えば、format@@0のformat@@1レポートを見てみましょう。

<!-- Main template -->
<template id="report_saleorder">
    <t t-call="web.html_container">
        <t t-foreach="docs" t-as="doc">
            <t t-call="sale.report_saleorder_document" t-lang="doc.partner_id.lang"/>
        </t>
    </t>
</template>

<!-- Translatable template -->
<template id="report_saleorder_document">
    <!-- Re-browse of the record with the partner lang -->
    <t t-set="doc" t-value="doc.with_context(lang=doc.partner_id.lang)" />
    <t t-call="web.external_layout">
        <div class="page">
            <div class="oe_structure"/>
            <div class="row">
                <div class="col-6">
                    <strong t-if="doc.partner_shipping_id == doc.partner_invoice_id">Invoice and shipping address:</strong>
                    <strong t-if="doc.partner_shipping_id != doc.partner_invoice_id">Invoice address:</strong>
                    <div t-field="doc.partner_invoice_id" t-options="{&quot;no_marker&quot;: True}"/>
                <...>
            <div class="oe_structure"/>
        </div>
    </t>
</template>

メインテンプレートは、doc.partner_id.langt-lang パラメータとして翻訳可能なテンプレートを呼び出し、パートナーの言語でレンダリングされます。 この方法で、各販売注文は、対応する顧客の言語で印刷されます。 ドキュメントの本文のみを翻訳したいが、ヘッダーとフッターをデフォルトの言語にしておきます。 このようにレポートの外部レイアウトを呼び出すことができます。

<t t-call="web.external_layout" t-lang="en_US">

ちなみに

これは外部テンプレートを呼び出すときにのみ機能することに注意してください。 t-call 以外の xml ノードに t-lang 属性を設定することで、ドキュメントの一部を翻訳することはできません。 テンプレートの一部を翻訳したい場合 この部分的なテンプレートで外部テンプレートを作成し、メインテンプレートから t-lang 属性で呼び出すことができます。

バーコード

バーコードはコントローラから返される画像で、QWeb 構文のおかげで簡単にレポートに埋め込むことができます (例: 属性 を参照してください)。

<img t-att-src="'/report/barcode/QR/%s' % 'My text in qr code'"/>

クエリ文字列としてより多くのパラメータを渡すことができます

<img t-att-src="'/report/barcode/?
    barcode_type=%s&amp;value=%s&amp;width=%s&amp;height=%s'%('QR', 'text', 200, 200)"/>

便利な備考

  • Twitter BootstrapとFontAwesomeクラスはレポートテンプレートで使用できます

  • ローカル CSS はテンプレートに直接配置できます

  • グローバルCSSは、テンプレートを継承してCSSを挿入することで、メインレポートレイアウトに挿入できます。

    <template id="report_saleorder_style" inherit_id="report.style">
      <xpath expr=".">
        <t>
          .example-css-class {
            background-color: red;
          }
        </t>
      </xpath>
    </template>
    
  • PDFレポートにスタイルが不足している場合は、 :ref:`これらの手順 <reference/backend/reporting/printed-reports/pdf-without-styles> ` を確認してください。

用紙の書式

用紙フォーマットは report.paperformat のレコードで、次の属性を含めることができます。

name (必須)

何らかのリストで検索するときにレポートのニーモニック・記述としてのみ役に立ちます

description

あなたのフォーマットの簡単な説明です

format

定義済みのフォーマットのいずれか(A0からA9、B0からB10、法的、レター、Tabloid,... または custom; A4 をデフォルトで指定します。ページサイズを定義する場合は、カスタム形式ではない形式は使用できません。

dpi

出力DPI; 90 by default

margin_top, margin_bottom, margin_left, margin_right

マージンサイズ (mm)

page_height, page_width

ページサイズ (mm)

orientation

横長または縦長

header_line

ヘッダ行を表示するboolean

header_spacing

mmのヘッダ間隔

例:

<record id="paperformat_frenchcheck" model="report.paperformat">
    <field name="name">French Bank Check</field>
    <field name="default" eval="True"/>
    <field name="format">custom</field>
    <field name="page_height">80</field>
    <field name="page_width">175</field>
    <field name="orientation">Portrait</field>
    <field name="margin_top">3</field>
    <field name="margin_bottom">3</field>
    <field name="margin_left">3</field>
    <field name="margin_right">3</field>
    <field name="header_line" eval="False"/>
    <field name="header_spacing">3</field>
    <field name="dpi">80</field>
</record>

カスタムレポート

デフォルトでは、レポーティングシステムは model フィールドで指定されたターゲットモデルに基づいてレンダリング値をビルドします。

However, it will first look for a model named report.module.report_name and call that model's _get_report_values(doc_ids, data) in order to prepare the rendering data for the template.

テンプレートのレンダリング時に使用または表示する任意のアイテムを含めることができます (追加モデルのデータなど)。

from odoo import api, models

class ParticularReport(models.AbstractModel):
    _name = 'report.module.report_name'

    def _get_report_values(self, docids, data=None):
        # get the report action back as we will need its data
        report = self.env['ir.actions.report']._get_report_from_name('module.report_name')
        # get the records selected for this rendering of the report
        obj = self.env[report.model].browse(docids)
        # return a custom rendering context
        return {
            'lines': docids.get_lines()
        }

警告

カスタムレポートを使用する場合、"default" ドキュメント関連項目 (doc_ids, doc_modeldocs) は **含まれません。 あなたがそれらを望むならば、あなた自身でそれらを含める必要があります。

上の例では、レンダリングコンテキストには "global" の値と lines が含まれていますが、それ以外は何も含まれていません。

カスタムフォント

カスタムフォントを使用したい場合は、web.reports_assets_common アセットバンドルにカスタムフォントと関連するless/CSSを追加する必要があります。 web.assets_common または web.assets_backend にカスタムフォントを追加すると、フォントが QWeb レポートで使用できなくなります。

例:

<template id="report_assets_common_custom_fonts" name="Custom QWeb fonts" inherit_id="web.report_assets_common">
    <xpath expr="." position="inside">
        <link href="/your_module/static/src/less/fonts.less" rel="stylesheet" type="text/less"/>
    </xpath>
</template>

別のアセットバンドル (web.reports_assets_common 以外) で使用している場合でも、このファイルの中で @font-face を定義する必要があります。

例:

@font-face {
    font-family: 'MonixBold';
    src: local('MonixBold'), local('MonixBold'), url(/your_module/static/fonts/MonixBold-Regular.otf) format('opentype');
}

.h1-title-big {
    font-family: MonixBold;
    font-size: 60px;
    color: #3399cc;
}

アセットバンドルに少ないものを追加したら、カスタムQWebレポートでクラスを使用できます - この例では``h1-title-big`` - 。

レポートはWebページ

レポートはレポートモジュールによって動的に生成され、URL を介して直接アクセスできます。

例えば、 http://<server-address>/report/html/sale.report_saleorder/38 から html モードでの販売注文レポートにアクセスできます。

または、http://<server-address>/report/pdf/sale.report_saleorder/38 からPDF版にアクセスできます。

1

python:datetime オブジェクトが実際にどのタイムゾーンにあるかは関係ありません (タイムゾーンなし) そのタイムゾーンはユーザーに調整される前に無条件に 設定 を UTC に変更します