正文之前, 先說兩件事:
一. 強調(diào)下odoo (原 openerp) 是開源軟件, 源碼是最好的老師呻拌,關(guān)于如何開發(fā)qweb報表,請多看源碼睦焕。
例子藐握,point_of_sale模塊有多個qweb報表的例子酿箭。
二. 貼個招聘鏈接。
Elico Corp (深圳) 正要招聘odoo技術(shù)工程師
qweb report 介紹
openerp 7版 使用 webkit 和 rml 報表引擎趾娃。 6版 用 rml缭嫡。
qweb 是8版采用的新報表引擎,webkit and rml 已在8版中棄用抬闷。
qweb 也是odoo web服務器的網(wǎng)頁渲染引擎妇蛀。也就是說,8版中笤成,odoo統(tǒng)一中網(wǎng)頁模板和報表模塊的渲染技術(shù)评架。
本文結(jié)構(gòu)
- qweb
- 如何創(chuàng)建一個qweb報表
qweb
基本上 ,
程序員在 xml 代碼中編寫 動作,報表炕泳,視圖纵诞,樣式,在python 代碼中編寫
模塊安裝培遵,上面編寫中的動作浙芙,報表,視圖會存在數(shù)據(jù)庫中籽腕。
-
當用戶點擊視圖上的按鈕嗡呼,或打開一個新網(wǎng)頁,它會驅(qū)動動作皇耗,調(diào)用 網(wǎng)頁 javascript API. 網(wǎng)頁 javascript 腳本會
- 找到對應的視圖id 和數(shù)據(jù)庫中的視圖代碼南窗,渲染到網(wǎng)頁上。
- 找到對應的數(shù)據(jù)id和數(shù)據(jù)庫中的值郎楼,渲染到網(wǎng)頁上万伤。
打應報表時,odoo 使用 wkhtmltopdf 把網(wǎng)頁樣式轉(zhuǎn)換到pdf格式.
qweb 語法介紹
數(shù)據(jù)
t-field
,t-esc
-
循環(huán), 條件
<p t-foreach="[1, 2, 3]" t-as="i"> <t t-esc="i"/> </p>
<t t-if="condition"> <p>ok</p> </t>
如何創(chuàng)建一個qweb報表
0. 模塊結(jié)構(gòu)
| report
| customize_report.py
| views
| report_layout_view.xml
| report.xml
| __init__.py
| __openerp__.py
| ...
1. 創(chuàng)建一個 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. 創(chuàng)建一個可翻譯的報表記錄 (可選)
<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>
3. 創(chuàng)建報表樣式
odoo 使用 bootstrap 作為網(wǎng)頁樣式:
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>
4. 創(chuàng)建自定義的渲染函數(shù)
有兩個方法
方法 1
odoo 使用這個方法重用7版代碼
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
方法 2
( odoo 官方文檔的代碼例子)
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)
小工具
報表網(wǎng)頁編輯工具
安裝 website_editor模塊 , 在后臺修改報表類型為HTML后呜袁,website manager 用戶可以在線修改報表樣式敌买。
網(wǎng)頁編輯完后,報表類型調(diào)整回pdf傅寡,即可再次答應pdf放妈。