odoo8-qweb report 制作pdf报表方法介绍

首先推荐一个qweb制作report的视频,比较直观。

http://v.youku.com/v_show/id_XMTI4OTU0MDQwOA==.html?from=s1.8-1-1.2


然后,转载一篇文章:http://www.jianshu.com/p/f33a236702cb


Before start of introduction, odoo (former openerp) is open source, the source code is the best tutorial.

Eg, you can check point_of_sale module to check out how to write qweb report.

qweb report introduction

openerp v7 use webkit and rml, openerp v6 uses rml.
qweb report is the new report engine of odoo V8, webkit and rml is depreciated from v8.

qweb is also rendering engine for odoo web server. Thus, odoo standardise the web engine.

Guide line

This article talks about

  • qweb report introduction
  • how to create a qweb report

qweb report

main elements

** report registration in database
** report translation registration in database (optional)
** report layout in database
** customize rendering functions (optional)

Qweb introduction

In general ,

  1. developer write xml code to create actions, report record, views, web layout.
  2. when installing modules, there actions, reports, views is saved in database.
  3. user click a button, open a new windows, it trigger the actions and Web javascript API. Web javascript will

    • find the view id and read layout code from databass render it into web
    • find record id and read record value from database render it into web
  4. when print report , odoo will trigger wkhtmltopdf library to render web into pdf document.

odoo qweb introduction

grammer
  • data
    t-fieldt-esc

  • loop, condition

      <p t-foreach="[1, 2, 3]" t-as="i">
          <t t-esc="i"/>
      </p>
      <t t-if="condition">
          <p>ok</p>
      </t>

how to create a qweb report

0. moduel structure

module
    | report
        |    customize_report.py
    | views
        |    report_layout_view.xml
    | report.xml
    | __init__.py
    | __openerp__.py
    | ...

1. create a report

  • if no 2nd step, the value of file and name 2nd step.
  • if 2nd step, the value of should be the template id in 2nd step
<report 
            id="report_sale_order_libiya_xxx"
            string="Sale Order Libiya"
            model="sale.order" 
            report_type="qweb-pdf"
            file="module.report_sale_order_xxx" 
name="module.report_sale_order_xxx" 
        />

2. create a report translation (optional)

<template id="report_sale_order_xxx">
    <t t-call="report.html_container">
        <t t-foreach="doc_ids" t-as="doc_id">
            <t t-raw="translate_doc(doc_id, doc_model, 'partner_id.lang', 'module.report_sale_order_xxx_document')"/>
        </t>
    </t>
</template>

create report layout

odoo is using bootstrap for web layout

http://www.w3cschool.cc/bootstrap/bootstrap-grid-system.html

<template id="report_sale_order_xxx_document">
    <t t-call="report.external_layout">
    <div class="page">
        <div class="oe_structure"/>
        <table class="dest_address">
        <tr>
            <td>
                <strong>Customer address:</strong>
                    <div t-field="o.partner_id" 
                        t-field-options='{"widget": "contact", "fields": ["address", "name", "phone", "fax","email","vat"], "no_marker": false}'/>
                    <p t-if="o.partner_id.vat">VAT: <span t-field="o.partner_id.vat"/></p>
            </td>
        </tr>
        </table>

            <div class="row mt32 mb32" id="informations">
                <div t-if="o.client_order_ref" class="col-xs-3">
                    <strong>Invoice:</strong>
                    <p t-field="o.client_order_ref"/>
                </div>
                <div t-if="o.user_id.name" class="col-xs-3">
                    <strong>Salesperson:</strong>
                    <p t-field="o.user_id.name"/>
                </div>
                <div t-if="o.payment_term" class="col-xs-3">
                    <strong>Payment Term:</strong>
                    <p t-field="o.payment_term"/>
                </div>
            </div>

</template>

create customize report rending function

There are two ways to registrating

method 1

odoo use this way to reuse the code of v7.

import time
from openerp.report import report_sxw
from openerp.osv import osv

class sale_report_xxx(report_sxw.rml_parse):
    def _print_test(self):
        return "good"

    def __init__(self, cr, uid, name, context):
        super(sale_report_libiya, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
            'time': time,
            'cr':cr,
            'uid': uid,
            'curr_rec': self.curr_rec,
            'compute_currency': self.compute_currency,
            'print_test': self._print_test,
            'print_test2': "good2",
            'other_methods'self._other_methods,
        })

class report_pos_details(osv.AbstractModel):
    _name = 'report.sale_webkit_report_libiya.report_sale_order_xxx'
    _inherit = 'report.abstract_report'
    _template = 'module.report_sale_order_xxx'
    _wrapped_report_class = sale_report_xxx
Method 2

code taken from odoo documentation

from openerp import api, models


class ParticularReport(models.AbstractModel):
    _name = 'report.<<module.reportname>>'
    @api.multi
    def render_html(self, data=None):
        report_obj = self.env['report']
        report = report_obj._get_report_from_name('<<module.reportname>>')
        docargs = {
            'doc_ids': self._ids,
            'doc_model': report.model,
            'docs': self,
        }
        return report_obj.render('<<module.reportname>>', docargs)

tips

website editor

After this module installed, website manager can edit report online once report type is set in backend to html.

After the online edit, the report can be changed back to pdf to print pdf document later.

output image on Qweb report

除了以上问题外,还有一个经常遇到的问题就是在报表中输出image,这里也来介绍个方法:

Now I'm printing the image in the QWeb report this way:

<span t-field="product_id.image_small" t-field-options="{"widget": "image", "class": "img-rounded"}"/>


说是原创,实在无语,多多见凉。
Odoo 是一款企业级开源应用程序,它提供了一套完整的业务应用套件。在 Odoo 中,QWEB报表是基于 XML 模板的报表生成工具,它允许开发者使用 XML 语法结合特定的 QWeb 表达式来设计报表。从 Odoo 13 开始,QWEB报表能够直接使用数据库中的数据,这涉及到 SQL 语句的使用。 要使用 SQL 生成 QWEB 报表,首先需要在 Odoo 的模型中定义 SQL 视图。然后,在 QWEB 报表模板中,可以通过特定的指令调用这些视图的数据。以下是一般步骤: 1. 定义 SQL 视图: 在 Odoo 模型中,可以通过 `sql_view` 属性定义一个 SQL 视图,例如: ```python class MyModel(models.Model): _name = 'my.model' _sql_constraints = [('unique_name', 'unique(name)', 'Name must be unique')] _sql_views = { 'my_sql_view': """ CREATE VIEW my_sql_view AS SELECT name, COUNT(*) as cnt FROM my_model GROUP BY name """ } ``` 2. 在 QWEB 报表中使用 SQL 数据: 在 QWEB 模板文件(XML)中,可以使用 `<t t-call="web.sql_template">` 来调用定义好的 SQL 视图。例如: ```xml <template id="report_my_sql_data" name="Report SQL Data"> <t t-call="web.sql_template" t-prepare="{'model': 'my.model', 'view_id': ref('my_sql_view')}"> <div> <h2>My SQL Data</h2> <table> <thead> <tr> <th>Name</th> <th>Count</th> </tr> </thead> <tbody> <t t-foreach="rows" t-as="row"> <tr> <td t-esc="row.name"/> <td t-esc="row.cnt"/> </tr> </t> </tbody> </table> </div> </t> </template> ``` 3. 在报表生成时,Odoo 将执行 SQL 视图定义的查询,并将结果以变量的形式提供给 QWEB 模板进行渲染。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值