QWeb 报表

报表以 HTML/QWeb 编写,类似于 Odoo 中的网站视图。您可以使用常规的 QWeb 控制流工具 。PDF 渲染本身由 wkhtmltopdf 执行。

报表通过 报表动作 声明,并为该动作指定一个 报表模板

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

报表模板

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

time

Python 标准库中 time 的引用

user

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

res_company

当前用户所属公司的记录

website

当前网站对象(如果有;此项目可能存在但为 None

web_base_url

Web 服务器的基本 URL

context_timestamp

一个函数,接受 UTC 时间 1datetime.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 设置为语言代码(例如 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 属性从主模板调用它。

条形码

条形码是由控制器返回的图像,得益于 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_topmargin_bottommargin_leftmargin_right

边距尺寸(毫米)

page_heightpage_width

页面尺寸(毫米)

orientation

横向或纵向

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

警告

当使用自定义报表时,默认的文档相关项( 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>

即使您已在其他资源包(而非 web.reports_assets_common )中使用过,也需要在此 less 文件中定义您的 @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;
}

将 less 添加到您的资源包后,您可以在此示例中使用类(如 h1-title-big )在自定义 QWeb 报表中。

报表是网页

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

例如,您可以通过访问 http://<服务器地址>/report/html/sale.report_saleorder/38 以 HTML 模式查看销售订单报表。

或者,您可以通过访问 http://<服务器地址>/report/pdf/sale.report_saleorder/38 查看 PDF 版本。

1

无论 python:datetime 对象实际处于哪个时区(包括无时区),其时区都会无条件被 设置 为 UTC,然后再调整为用户的时区。