iT邦幫忙

2021 iThome 鐵人賽

DAY 16
0

Odoo提供建立report的功能,透過wkhtmltopdf來輸出pdf,我們來寫一個簡單的範例,能夠印出學生的姓名、綽號、所屬學校。

首先增加檔案 /reports/res_student_report.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="report_student_id_card">
        <t t-call="web.html_container">
            <t t-foreach="docs" t-as="o">
                <t t-call="web.external_layout">
                    <div class="page">
                        <h2>Student Report</h2>
                        <div>
                            <p>Name: <span t-field="o.name"/></p>
                        </div>
                        <div>
                            <p>Nickname: <span t-field="o.nickname"/></p>
                        </div>
                        <div>
                            <p>School: <span t-field="o.school_id.name"/></p>
                        </div>
                    </div>
                </t>
            </t>
        </t>
    </template>
    
    <report
        id="report_student"
        model="res.student"
        string="Student report"
        report_type="qweb-pdf"
        name="student.report_student_id_card"
        file="student.report_student_id_card"
        print_report_name="'學生資訊 %s' % (object.name)"
    />
    
</odoo>

我們分為兩個部分來解釋

Template

Odoo 的Template代表畫面,是用Qweb撰寫,將xml轉譯成html

    <template id="report_student_id">
        <t t-call="web.html_container">
            <t t-foreach="docs" t-as="o">
                <t t-call="web.external_layout">
                    <div class="page">
                        <h2>Student Report</h2>
                        <div>
                            <p>Name: <span t-field="o.name"/></p>
                        </div>
                        <div>
                            <p>Nickname: <span t-field="o.nickname"/></p>
                        </div>
                        <div>
                            <p>School: <span t-field="o.school_id.name"/></p>
                        </div>
                    </div>
                </t>
            </t>
        </t>
    </template>

id:自定義,不重複即可

t:以t開頭的為Qweb寫法,基本上一開始寫的是一樣的

t-call="web.external_layout :提供我們基本的header與footer
<t t-foreach="docs" t-as="o"> :代表遍歷整個records,像是python的 for o in records:

t-as 是幫你的model的名稱

t-field :配上之前設定的model object可以拿到底下的field

只要知道Qweb代表的意義,可以寫出基本的畫面,另外也有t-if 等邏輯判斷幫助report更加完善

Report

    <report
        id="report_student"
        model="res.student"
        string="Student report"
        report_type="qweb-pdf"
        name="res_student.report_student_id"
        file="res_student.report_student_id"
        print_report_name="'學生資訊 %s' % (object.name) +'.pdf'"
    />

report就是匯出檔案的設定

id:自定義,不重複即可

model:關聯的model name

string:顯示的Action button name

report_type :輸出模式,預設為qweb-pdf ,另外還有qweb-html ,會以網頁的方式呈現

name file:串連template的設定,格式為model.template_id

print_report_name :輸出檔案名稱

設定好之後我們將reports加入__manifest__.py

'data': [
        'reports/student_report.xml'
		...
    ],

重啟之後我們便可以在頁面看到多了一個print button:

https://ithelp.ithome.com.tw/upload/images/20211001/20130896xt4pm05bLC.png

點選之後即會下載pdf到電腦中,而我們印出其中三個field,檔名也與設定的一致:

https://ithelp.ithome.com.tw/upload/images/20211001/20130896SfVOvUozij.png

同樣Odoo也提供介面去操作Reports,當然也可以設定權限,或是修改相關資訊

https://ithelp.ithome.com.tw/upload/images/20211001/2013089610xELVgHpP.png

今天就介紹到這邊了,如果要客製化Report,可以利用一些base template可以減少花的時間,這部分建議多看source code的寫法比較可以掌握Qweb的用法。


上一篇
Day15 Let's ODOO: Menu
下一篇
Day17 Let's ODOO: Data Files
系列文
Let's ODOO 開發與應用30天挑戰30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言