QWeb报告

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

报告是使用 报告操作 声明的,并且使用 报告模板 进行操作。

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

报告模板

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

time

对 Python 标准库中的 time 的引用

user

res.user record for the user printing the report

res_company

当前 user 的公司的记录

website

当前网站对象(如果有)(此项可以存在但为 None

web_base_url

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

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

docs

当前报告的记录

doc_ids

list of ids for the docs records

doc_model

model for the docs records

如果您希望在模板中访问其他记录/模型,您将需要 自定义报表,然而在那种情况下,如果您需要它们,您将不得不提供上述项目。

可翻译的模板

如果您想要翻译报告(例如到合作伙伴的语言),您需要定义两个模板:

  • 主要报告模板

  • 可翻译的文档

您可以在主模板中调用可翻译的文档,将属性 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">

小技巧

请注意,这仅在调用外部模板时有效,您将无法通过在xml节点上设置 t-lang 属性来翻译文档的一部分,除非该节点是 t-call 。如果您希望翻译模板的一部分,您可以创建一个包含该部分模板的外部模板,并在主模板中使用 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报告缺少样式,请检查 这些说明

纸张格式

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

name (mandatory)

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

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

横向或纵向

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

警告

当使用自定义报表时,”default” 文档相关的项目 (doc_ids, doc_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;
}

After you’ve added the less into your assets bundle you can use the classes - in this example h1-title-big - in your custom QWeb report.

报告是网页

报告是由报告模块动态生成的,可以通过URL直接访问:

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

或者您可以通过以下链接访问PDF版本:http://<server-address>/report/pdf/sale.report_saleorder/38

1

无论:class:`python:datetime`对象实际处于什么时区(包括无时区),它的时区都将无条件地被设置为UTC,然后再调整为用户的