qweb report introduction

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-field, t-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.

Refs

Elico Corp (shenzhen) is hiring

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末茎匠,一起剝皮案震驚了整個濱河市惶傻,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機宽闲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來握牧,“玉大人容诬,你說我怎么就攤上這事⊙匮” “怎么了览徒?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長颂龙。 經(jīng)常有香客問我习蓬,道長纽什,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任躲叼,我火速辦了婚禮芦缰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘枫慷。我一直安慰自己让蕾,他們只是感情好,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布流礁。 她就那樣靜靜地躺著涕俗,像睡著了一般罗丰。 火紅的嫁衣襯著肌膚如雪神帅。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天萌抵,我揣著相機與錄音找御,去河邊找鬼。 笑死绍填,一個胖子當著我的面吹牛霎桅,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播讨永,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼滔驶,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了卿闹?” 一聲冷哼從身側(cè)響起揭糕,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎锻霎,沒想到半個月后著角,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡旋恼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年吏口,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片冰更。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡产徊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蜀细,到底是詐尸還是另有隱情囚痴,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布审葬,位于F島的核電站深滚,受9級特大地震影響奕谭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜痴荐,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一血柳、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧生兆,春花似錦难捌、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至合蔽,卻和暖如春击敌,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背拴事。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工沃斤, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人刃宵。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓衡瓶,卻偏偏與公主長得像,于是被迫代替她去往敵國和親牲证。 傳聞我的和親對象是個殘疾皇子哮针,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

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