QWeb 报表

报表使用 HTML/QWeb 编写,就像 Odoo 中的网站视图一样。您可以使用通常的 QWeb 控制流工具。PDF 渲染本身由 wkhtmltopdf 完成。

报表是通过 报表动作 声明的,并且需要一个 引用/报表/模板 供该动作使用。

如果有必要或适用,可以为报表指定一个 纸张格式

报表模板

报表模板始终会提供以下变量:

时间

对 Python 标准库中 time 的引用

用户

res.user 记录,即打印报表的用户

公司

当前用户所属公司的记录

网站

当前网站对象(如果有的话,此条目可能存在但为 None

web_base_url

Web 服务器的基准网址

上下文时间戳

一个函数,接收以 datetime.datetime 表示的 UTC 时间 1,并将其转换为打印报表的用户的时区。

最小可行模板

一个最小的模板看起来像:

<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.report_invoice。由于这是一个 QWeb 模板,您可以访问模板接收到的 docs 对象的所有字段。

默认情况下,渲染上下文还将公开以下项:

文档

当前报表的记录

文档ID

docs 记录的 ID 列表

文档模型

用于 docs 记录的模型

如果您希望在模板中访问其他记录/模型,您将需要 自定义报表,但在此情况下,如果您需要上述内容,则必须提供它们。

可翻译模板

如果您希望翻译报表(例如,翻译成业务伙伴的语言),您需要定义两个模板:

  • 主要报表模板

  • 可翻译的文档

你可以通过将属性 t-lang 设置为语言代码(例如 fren_US)或设置为记录字段,从主模板中调用可翻译文档。如果你使用的是可翻译字段(如国家名称、销售条件等),还需要在适当上下文中重新浏览相关记录。

警告

如果您的报表模板不使用可翻译的记录字段,则在另一种语言中重新浏览记录是*不必要的*,并且会影响性能。

例如,我们来看一下销售模块中的销售订单报表:

<!-- 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.lang 作为 t-lang 参数调用可翻译模板,因此它将使用合作伙伴的语言进行渲染。这样,每个销售订单都将使用对应客户的语言打印。如果您希望仅翻译文档正文,但保持页眉和页脚使用默认语言,可以这样调用报表的外部布局::

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

小技巧

请注意,此功能仅在调用外部模板时有效,您无法通过在非 t-call 的 XML 节点上设置 t-lang 属性来翻译文档的一部分。如果您希望翻译模板的一部分,可以创建一个包含该部分模板的外部模板,并通过 t-lang 属性从主模板中调用它。

条形码

条形码是通过控制器返回的图像, thanks to the QWeb syntax(例如,请参阅 属性)可以轻松嵌入到报表中:

<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 报表缺少样式,请查看 这些说明

纸张格式

纸张格式是 report.paperformat 的记录,可以包含以下属性:

名称 (必填)

仅在查找某种列表中的报表时作为记忆/描述用途

描述

一个格式的简要描述

格式

可以是预定义的格式(A0 到 A9、B0 到 B10、Legal、Letter、Tabloid 等),或者 custom;默认为 A4。如果您定义了页面尺寸,则不能使用非自定义的格式。

dpi

输出 DPI;默认为 90

上边距下边距左边距右边距

毫米的边距尺寸

页面高度页面宽度

页面尺寸(毫米)

方向

横向或纵向

标题行

布尔值,用于显示标题行

页眉间距

页眉间距(毫米)

示例:

<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 字段指定的目标模型生成渲染值。

然而,它会首先查找名为 report.module.report_name 的模型,并调用该模型的 _get_report_values(doc_ids, data) 方法,以准备模板的渲染数据。

这可用于在渲染模板时包含任意项目以供使用或显示,例如来自其他模型的数据:

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()
        }

警告

使用自定义报表时,”默认” 的与文档相关的项(doc_idsdoc_modeldocs)将 不会 被包含。如果您需要这些项,需要自行包含它们。

在上面的示例中,渲染上下文将包含“全局”值以及我们放入其中的 lines,但不会有其他内容。

自定义字体

如果您想使用自定义字体,需要将您的自定义字体以及关联的 less/CSS 文件添加到 web.reports_assets_common 资源包中。将您的自定义字体添加到 web.assets_commonweb.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>

你需要在此 less 文件中定义你的 @font-face,即使你已在其他资源包(除 web.reports_assets_common 之外)中使用过它。

示例:

@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;
}

在将 less 文件添加到你的资源包后,你可以在自定义 QWeb 报表中使用这些类——例如 h1-title-big

报表是网页

报表由报表模块动态生成,可以通过网址直接访问:

例如,您可以通过访问 http://<server-address>/report/html/sale.report_saleorder/38 来以 HTML 模式查看销售订单报表。

或者您可以在 http://<server-address>/report/pdf/sale.report_saleorder/38 访问 PDF 版本。

1

它实际上处于什么时区(包括没有时区)并不重要,该 python:datetime 对象的时区将在调整为用户时区之前无条件地被 设置 为 UTC。