今天研究Odoo中選中一條數據進行打印,如何創建對應的打印按鈕,以及點擊相應的按鈕顯示相應的要打印的界面,具體如下圖所示:
首先需要在模塊中創建report目錄,例如官方文件所示:
其實點擊看看官方的模塊式怎麼寫的就大概明白怎麼回事了。這裏我用自己的模塊大概說一下,也方便以後忘記了可以隨時回來看到,report目錄結構都基本類似。在bn_oracle_report_pzqweb.py中寫了個測試的
其中get_report_values()方法是必須寫的,因爲在對應的xml文件中需要用到報表所需的數據,方法中的docids就是你選擇需要打印的數據的id集合。
用self.env.cr.execute()這個方法執行了原生的SQL語句。
return self._cr.dictfetchall() #返回字典型結果集。
return self._cr.fetchall() #返回列表型結果集。
bn_oracle_report_pzqweb.xml中
<!--測試-->
<report
id="action_bn_oracle_report_pzqweb3"
string="憑證報表測試"
model="bn.oracle.interface.pz.master"
report_type="qweb-html"
name="bn_oracle.report03"
file="bn_oracle.report03"/>
string: 在打印按鈕那裏的顯示名稱
id:外id標識
name: 完整主模板名稱 模塊名.主板模名稱 用於管理和調用
file: 完整模板文件名 模塊名.模板文件名 用於更好地關聯模板
model:相關的模型顯示在那個模塊進行打印報表 通常在各視圖中的打印那裏會出現
report_type: 報表類型 是 qweb-pdf 或 qweb-html
還要有一個對應的報表模板,即點擊憑證報表測試後顯示的要打印的界面。可以看到其中用到head.xxx,lines_data.xxx都是同過之前py文件獲得並返回的數據。
<!--測試-->
<template id="report03">
<t t-call="web.html_container">
<t t-call="web.external_layout">
<div class="page">
<table width="80%">
<tbody>
<tr>
<td>憑證編號:
<t t-esc="heard.pz_sequence"/>
</td>
<td>日期:
<t t-esc="heard.throw_date" t-options='{"widget": "date"}'/>
</td>
</tr>
<tr>
<td>Oracle憑證號:
<t t-esc="heard.strjournalname"/>
</td>
<td>拋轉狀態:
<t t-esc="heard.oracle_state"/>
</td>
</tr>
</tbody>
</table>
<br/>
<table class="table table-condensed" width="100%">
<thead>
<th width="15%">科目</th>
<th width="12%">部門</th>
<th width="10%">人員</th>
<th width="8%">項目</th>
<th width="5%">借方</th>
<th width="5%">貸方</th>
<th width="45%">摘要</th>
</thead>
<tbody>
<t t-foreach="lines_data" t-as="o">
<tr>
<td>
<t t-esc="o[2]"/>
</td>
<td>
<t t-esc="o[3]"/>
</td>
<td>
<t t-esc="o[4]"/>
</td>
<td>
<t t-esc="o[5]"/>
</td>
<td>
<t t-esc="o[6]" t-options='{"widget": "float", "precision": 2}'/>
</td>
<td>
<t t-esc="o[7]" t-options='{"widget": "float", "precision": 2}'/>
</td>
<td>
<t t-esc="o[8]"/>
</td>
</tr>
</t>
<tr>
<td>
</td>
<td>
<strong>合 計:</strong>
</td>
<td>
</td>
<td>
</td>
<td>
<strong>
<t t-esc="sum(line[6] for line in lines_data)"
t-options='{"widget": "float", "precision": 2}'/>
</strong>
</td>
<td>
<strong>
<t t-esc="sum(line[7] for line in lines_data)"
t-options='{"widget": "float", "precision": 2}'/>
</strong>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
</t>
</t>
</template>
到這裏基本就ok了,可能寫的比較模糊,剛開始寫博客,有什麼疑問可以大家討論~
對了,還可以設置打印的紙張,這個可以再odoo運行後用管理員登錄,然後選擇激活開發者模式,在設置中報告中設置。
可以在紙張類型中選擇想打印的紙張格式,紙張格式也可以用自帶的也可以自己設置,但只有在報表類型選擇爲PDF時才顯示,html類型想用A4橫向的話可以先切換爲PDF類型,選擇了對應的紙張格式後再把報表類型改回html也可生效。