生成报表是odoo里面必备的功能
前置条件
第一步 安装wkhtmltopdf
下载链接(https://wkhtmltopdf.org/downloads.html)
第二步 添加系统路径
第三步 odoo配置
在odoo.conf下,加入如下内容,让odoo识别这个插件
bin_path = C:\Program Files\wkhtmltopdf\bin
运行后,如果后台出现这么一句话,就说明环境配置完成
第四步 创建report文件夹 配置报表action
在report定义 不然是不会显示打印的 就跟定义一个菜单动作差不多的意思(个人理解)
<odoo>
<record id="action_fixed_assets_detail_report"
model="ir.actions.report">
<field name="name">固定资产转移单(PDF)</field>
<field name="model">fixed.assets.transfer.form</field>
<field name="report_type">qweb-html</field>
<field name="report_name">workform.fixed_assets_transfer_form_template</field>
<field name="report_file">workform.fixed_assets_transfer_form_template</field>
<field name="binding_model_id"
ref="model_fixed_assets_transfer_form"/>
<field name="binding_type">report</field>
</record>
<odoo/>
- 基本属性 :
- model=“ir.actions.report” :表示这是一个报表动作
- name=“固定资产转移单(PDF)” :报表显示名称
- model=“fixed.assets.transfer.form” :报表关联的数据模型
- 报表配置 :
- report_type=“qweb-html” :使用QWeb模板引擎生成HTML格式报表
- qweb-html:点击会先预览 qweb-pdf:直接下载 不会预览
- report_name 和 report_file :指定报表模板路径为 workform.fixed_assets_transfer_form_template(这两个数据一致 我也不知道为啥要一样 教程上写得)
- 绑定设置 :
binding_model_id :通过ref属性绑定到固定资产转移单模型
binding_type=“report” :指定为报表类型的绑定 - 实现原理 :这个报表动作会从 fixed.assets.transfer.form 模型获取数据使用 workform.fixed_assets_transfer_form_template 模板渲染HTML自动转换为PDF格式输出
- 使用方式 :在固定资产转移单的表单或列表视图中,会出现"打印"按钮,点击后即可生成PDF报表。
配置报表模板
一般格式都是这样
<odoo>
<template id="fixed_assets_transfer_form_template">
<t t-call="web.html_container">
<t t-call="web.internal_layout">
<t t-foreach="docs" t-as="doc">
<div class="page">
</div>
</t>
</t>
</t>
</template>
</odoo>
-
template id="fixed_assets_transfer_form_template id是report.xml里面
-
t-call=“web.html_container” :调用标准HTML容器模板
-
t-call=“web.internal_layout” :使用Odoo内置的报表布局
-
t-foreach=“docs” t-as=“doc” :循环遍历文档集(个人理解:拿到记录集 然后在遍历 得到一条记录 doc就跟self差不多 因为在tree视图里面可以勾选多条数据一起打印)
t标签 里面的属性
- t-name 用于指明模板的名称
- t-if 用于指明元素在页面产生的条件
- t-foreach用于指明一个循环调用
- t-as 用于取得循环中的单个值 一般与t-foreach搭配使用 后面带的是一个变量名
- t-esc 用与一个文字的输出
- t-call 用于调用另外模板,后面带一个模板名称
- t-set 用于设定一个变量 t-value 用于指定某个元素或者变量的值
实战使用
字段显示
<span t-field="doc.department_name"/>
跟 <t t-esc='doc.department_name'/>
效果是一样都是显示字段的值
但是t-esc是不可以翻译的 t-field是可以翻译的 方便有多语言业务 t-field不可以嵌套在t标签里
循环 one2many字段可以这样用
判断
因为我的值是012 但是我打印模版想显示中文
方法1:
<td>
<t t-if="doc.recruitment_method == '0'">内招</t>
<t t-elif="doc.recruitment_method == '1'">外招</t>
<t t-elif="doc.recruitment_method == '2'">不限</t>
</td>
方法2:
t-set="reason_mapping" #定义一个名为 reason_mapping 的变量。
t-value="dict([('0', '内招'), ('1', '外招'), ('2', '储备人才'), ('3', '不限')])" :将 reason 字段的值和显示内容构建成一个字典。
t-esc="reason_mapping.get(doc.reason, '')" :从字典里获取 doc.reason 对应的值,若未找到则返回空字符串。
根据条件显示字段
<t t-if="doc.reason_other">
<td>原因</td>
<td style="width:1000px;"><t t-esc="doc.reason_other"/></td>
</t>
显示条数