QWeb報表簡易教程

正文之前, 先說兩件事:
一. 強調(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

基本上 ,

  1. 程序員在 xml 代碼中編寫 動作,報表炕泳,視圖纵诞,樣式,在python 代碼中編寫

  2. 模塊安裝培遵,上面編寫中的動作浙芙,報表,視圖會存在數(shù)據(jù)庫中籽腕。

  3. 當用戶點擊視圖上的按鈕嗡呼,或打開一個新網(wǎng)頁,它會驅(qū)動動作皇耗,調(diào)用 網(wǎng)頁 javascript API. 網(wǎng)頁 javascript 腳本會

    • 找到對應的視圖id 和數(shù)據(jù)庫中的視圖代碼南窗,渲染到網(wǎng)頁上。
    • 找到對應的數(shù)據(jù)id和數(shù)據(jù)庫中的值郎楼,渲染到網(wǎng)頁上万伤。
  4. 打應報表時,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放妈。

索引

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市荐操,隨后出現(xiàn)的幾起案子芜抒,更是在濱河造成了極大的恐慌,老刑警劉巖托启,帶你破解...
    沈念sama閱讀 222,946評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宅倒,死亡現(xiàn)場離奇詭異,居然都是意外死亡屯耸,警方通過查閱死者的電腦和手機拐迁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評論 3 399
  • 文/潘曉璐 我一進店門蹭劈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人线召,你說我怎么就攤上這事铺韧。” “怎么了缓淹?”我有些...
    開封第一講書人閱讀 169,716評論 0 364
  • 文/不壞的土叔 我叫張陵哈打,是天一觀的道長。 經(jīng)常有香客問我讯壶,道長料仗,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,222評論 1 300
  • 正文 為了忘掉前任伏蚊,我火速辦了婚禮立轧,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘躏吊。我一直安慰自己氛改,他們只是感情好,可當我...
    茶點故事閱讀 69,223評論 6 398
  • 文/花漫 我一把揭開白布颜阐。 她就那樣靜靜地躺著平窘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪凳怨。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,807評論 1 314
  • 那天是鬼,我揣著相機與錄音肤舞,去河邊找鬼。 笑死均蜜,一個胖子當著我的面吹牛李剖,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播囤耳,決...
    沈念sama閱讀 41,235評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼篙顺,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了充择?” 一聲冷哼從身側(cè)響起德玫,我...
    開封第一講書人閱讀 40,189評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎椎麦,沒想到半個月后宰僧,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,712評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡观挎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,775評論 3 343
  • 正文 我和宋清朗相戀三年琴儿,在試婚紗的時候發(fā)現(xiàn)自己被綠了段化。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,926評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡造成,死狀恐怖显熏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情晒屎,我是刑警寧澤佃延,帶...
    沈念sama閱讀 36,580評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站夷磕,受9級特大地震影響履肃,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜坐桩,卻給世界環(huán)境...
    茶點故事閱讀 42,259評論 3 336
  • 文/蒙蒙 一尺棋、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧绵跷,春花似錦膘螟、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至净当,卻和暖如春内斯,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背像啼。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評論 1 274
  • 我被黑心中介騙來泰國打工俘闯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人忽冻。 一個月前我還...
    沈念sama閱讀 49,368評論 3 379
  • 正文 我出身青樓真朗,卻偏偏與公主長得像,于是被迫代替她去往敵國和親僧诚。 傳聞我的和親對象是個殘疾皇子遮婶,可洞房花燭夜當晚...
    茶點故事閱讀 45,930評論 2 361

推薦閱讀更多精彩內(nèi)容