0
点赞
收藏
分享

微信扫一扫

13、odoo打印报表

东言肆语 2022-01-31 阅读 56


打印报表(Reporting)

点我下载完整odoo的demo

从odoo8.0开始使用基于Qweb,Bootstrap和Wkhtmltopdf的全新报表引擎。

一份报表由两个元素构成:

1 在ir.actions.report.xml中定义报表记录,使用定义报表生成的参数。

<report
id="account_invoices"
model="account.invoice"
string="Invoices"
report_type="qweb-pdf"
name="account.report_invoice"
file="account.report_invoice"
attachment_use="True"
attachment="(object.state in ('open','paid')) and
('INV'+(object.number or '').replace('/','')+'.pdf')"
/>

2 Qweb view定义报表样式

<t t-call="report.html_container">
<t t-foreach="docs" t-as="o">
<t t-call="report.external_layout">
<div class="page">
<h2>Report title</h2>
</div>
</t>
</t>
</t>

这里docs是从context发送过来的变量,代表报表内容记录。

docs的作用其实是 ,比如你在界面上选定了几条相关模型的记录,然后可以通过docs访问这些记录数据,相当于模型方法中的recordset赋值给 docs = self,然后可以在qweb里读取。

另外还有user代表打印此报表的人

注意

生成pdf需要服务端安装wkhtmltopdf程序, 有时候打印的报表样式丢失,需要正确设置相关参数。

report.url = http://localhost:8069 如果你的odoo有使用反向代理,你可以设置url路径为本地访问。

web.base.url.freeze=True 样式路径通过web.base.url参数取得,但是有时在不同用户登录导致其发生变化,取到不一样的值,可以锁住设定。

为session制作报表, 包括 session名称, 开始结束时间, 出席学生。

在myproject/my_first_app/下创建一个report文件夹,里面创建四个文件​​__init__.py、myproject_report.xml、myproject_report_templates.py、myproject_report_templates.xml​

在​​myproject/my_first_app/report/__init__.py​​中导入:

from . import myproject_report_templates

​myproject/my_first_app/__init__.py​

from . import controllers
from . import models
from . import wizard
from . import report

​myproject/my_first_app/__manifest__.py​

'data': [
'security/myproject_security.xml',
'security/ir.model.access.csv',
'data/course_data.xml',
'data/session_data.xml',
'views/menu_views.xml',
'views/course_views.xml',
'views/session_views.xml',
'views/res_partner_views.xml',
'wizard/myproject_wizard_view.xml',
'wizard/actions_menu.xml',
'report/myproject_report.xml',
'report/myproject_report_templates.xml',
],

myproject/my_first_app/report/myproject_report.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<report
id="report_session"
model="myproject.session"
string="Session Report"
name="my_first_app.report_myproject_report_templates"
file="my_first_app.report_myproject_report_templates"
report_type="qweb-pdf" />
</odoo>

myproject/report/myproject_report_templates.py

# -*- coding: utf-8 -*-
from odoo import api, models, fields

class SessionViewReport(models.AbstractModel):
_name = 'report.my_first_app.report_myproject_report_templates'

@api.model
def _get_report_values(self, docids, data=None):
ssession_ids = self.env['myproject.session'].browse(docids)

return {
'doc_ids': docids,
'doc_model': self.env.context.get('active_model'),
'data': data,
'docs': ssession_ids,
}

myproject/report/myproject_report_templates.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="report_myproject_report_templates">
<t t-call="web.html_container">
<t t-foreach="docs" t-as="doc">
<t t-call="web.external_layout">
<div class="page">
<h2 t-field="doc.name"/>
<p>From
<span t-field="doc.start_date"/>
to
<span t-field="doc.end_date"/>
</p>
<h3>Attendees:</h3>
<ul>
<t t-foreach="doc.attendee_ids" t-as="attender">
<li>
<span t-field="attender.name"/>
</li>
</t>
</ul>
</div>
</t>
</t>
</t>
</template>
</odoo>

更新应用后,效果如下:

13、odoo打印报表_html

点我下载完整odoo的demo

参考

<https://www.odoo.com/documentation/10.0/reference/qweb.html

https://www.odoo.com/documentation/10.0/reference/reports.html

后记

【后记】为了让大家能够轻松学编程,我创建了一个公众号【轻松学编程】,里面有让你快速学会编程的文章,当然也有一些干货提高你的编程水平,也有一些编程项目适合做一些课程设计等课题。

如果文章对您有帮助,请我喝杯咖啡吧!

公众号

13、odoo打印报表_html_02


关注我,我们一起成长~~



举报

相关推荐

0 条评论