QWeb 报表

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

报表通过 报表动作 以及供该动作使用的 报表模板 来声明。

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

报表模板

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

time

对 Python 标准库中 python:time 模块的引用

user

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

res_company

当前 user 所属公司的记录

website

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

web_base_url

Web 服务器的 base url

context_timestamp

一个接受 UTC1 的 python:datetime.datetime 并将其转换为打印报表的用户所在时区的函数

最小可用模板

一个最小模板如下所示:

<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 对象的所有字段。

默认情况下,渲染上下文还会暴露以下内容:

docs

当前报表的记录

doc_ids

docs 记录的 id 列表

doc_model

docs 记录的模型

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

可翻译的模板

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

  • 主报表模板

  • 可翻译的文档

然后,您可以在主模板中调用可翻译文档,将属性 t-lang 设置为语言代码(例如 fr 或 en_US)或某个记录字段。如果您使用了可翻译的字段(如国家名称、销售条件等),还需要使用正确的上下文重新浏览相关记录。

Warning

如果您的报表模板未使用可翻译的记录字段,则 没有 必要以其他语言重新浏览记录,而且这样做会影响性能。

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

<!-- 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">

Tip

请注意,这仅在调用外部模板时有效;除 t-call 以外,无法通过在某个 XML 节点上设置 t-lang 属性来翻译文档的一部分。如果想翻译模板的一部分,可以将该部分模板创建为外部模板,并在主模板中通过 t-lang 属性调用它。

条形码

条形码是控制器返回的图片,借助 QWeb 语法可以轻松嵌入报表中(例如参见 attributes):

<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 的记录,可以包含以下属性:

name (必填)

仅在某种列表中查找报表时用作助记/描述

description

对格式的简短描述

format

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

dpi

输出 DPI;默认为 90

margin_top, margin_bottom, margin_left, margin_right

以毫米为单位的边距大小

page_height, page_width

以毫米为单位的页面尺寸

orientation

Landscape 或 Portrait

header_line

布尔值,用于显示页眉线

header_spacing

以毫米为单位的页眉间距

示例:

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

Warning

使用自定义报表时,与文档相关的”默认”条目(doc_ids、doc_model 和 docs) 不会 被包含。如果需要它们,必须自行包含。

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

自定义字体

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

需要在这个 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。

报表即网页

报表由报表模块动态生成,可以直接通过 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,然后再调整为用户的时区