使用hiPrint代替odoo原生的打印功能
可以实现快速自定义修改打印模板,无需每次都调整打印模板
无论是表单分页还是各种需求,都能满足
目录
8)编写打开的打印模板页面,在打印模板页面调用接口,实现修改保存打印模板
6 新建“打印数据”,用于模拟测试打印功能(类似新建“打印模板”,此处省略说明)
1 使用命令创建新的模块,无用的demo文件可以删除掉
python odoo-bin scaffold hiprint
2 新建“打印模板”,用于保存打印模板
1)编写打印模板模型
2)增加打印模板权限
3)增加打印模板视图和菜单
class PrintTemp(models.Model):
_name = 'print.temp'
_order = 'name DESC'
_description = '打印模板'
_sql_constraints = [
('unique_code_print_temp', 'unique(code)', '编码需要唯一.'),
]
name = fields.Char(u'名称', copy=False, index=True, required=True)
code = fields.Char(u'编码', copy=False, index=True, default='/')
type = fields.Char('类型')
template = fields.Text('模板')
remark = fields.Char(string='备注')
def copy(self, default=None):
default = dict(default or {})
copied_count = self.search_count(
[('name', '=like', u"副本 {}%".format(self.name))])
if not copied_count:
new_name = u"副本 {}".format(self.name)
else:
new_name = u"副本 {} ({})".format(self.name, copied_count)
default['name'] = new_name
return super(PrintTemp, self).copy(default)
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
print_temp,print_temp,model_print_temp,,1,1,1,1
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record model="ir.ui.view" id="print_temp_tree_view">
<field name="name">print.temp.tree</field>
<field name="model">print.temp</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
<field name="code"/>
<field name="type"/>
<field name="remark"/>
</tree>
</field>
</record>
<record model="ir.ui.view" id="print_temp_form_view">
<field name="name">print.temp.form</field>
<field name="model">print.temp</field>
<field name="arch" type="xml">
<form delete="0">
<sheet>
<group>
<group>
<field name="code"/>
<field name="name" />
</group>
<group>
<field name="template"/>
<field name="type"/>
<field name="remark"/>
</group>
</group>
</sheet>
</form>
</field>
</record>
<record model="ir.actions.act_window" id="action_print_temp">
<field name="name">打印模板</field>
<field name="res_model">print.temp</field>
<field name="view_mode">tree,form</field>
</record>
</odoo>
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<menuitem id="hiprint_menu" name="hiprint" web_icon="hiprint,static/description/icon.png" sequence="10"/>
<menuitem id="hiprint_print_temp" name="打印模板" parent="hiprint_menu" sequence="10" action="action_print_temp"/>
</data>
</odoo>
3 首次运行先安装模块
python odoo-bin -c odoo.conf -i hiprint
4 增加按钮,定制并保存模板
1)增加自定义按钮“定制模板”xml
2)增加自定义按钮事件js,点击按钮实现打开新的页面
3)增加引入自定义按钮事件js的xml
4)列表视图增加自定义按钮
5)引入以上2个文件
6)引入js,bootstrap,hiprint并引用
7)编写接口,用于打开模板编辑界面,并实现修改保存打印模板
8)编写打开的打印模板页面,在打印模板页面调用接口,实现修改保存打印模板
<?xml version="1.0" encoding="UTF-8"?>
<templates id="print_temp_button_template" xml:space="preserve">
<t t-extend="ListView.buttons" t-name="hiprint_print_temp_tree_view.buttons">
<t t-jquery="button.o_list_export_xlsx" t-operation="before">
<t t-if="widget.modelName=='print.temp'">
<button type="button" class="btn btn-primary o_list_button_custom">
定制模板
</button>
</t>
</t>
</t>
</templates>
odoo.define('add_print_temp_button', function (require) {
"use strict";
var ListController = require('web.ListController');
var ListView = require('web.ListView');
var viewRegistry = require('web.view_registry');
function renderGenerateButton() {
if (this.$buttons) {
var self = this;
this.$buttons.on('click', '.o_list_button_custom', function () {
console.info('click');
var actived_ids = []
var state = self.model.get(self.handle, {raw: true});
for (var i = 0; i < $('tbody .o_list_record_selector input').length; i++) {
if ($('tbody .o_list_record_selector input')[i].checked === true) {
actived_ids.push(state.res_ids[i]);
}
}
var ctx = state.context;
if(actived_ids.length > 0){
window.open("pt_custom/" + actived_ids[0], '_blank');
}
});
}
}
var ModelListController = ListController.extend({
willStart: function () {
var self = this;
//设置管理员权限组才看到按钮,可以按需修改
var ready = this.getSession().user_has_group('base.group_no_one')
.then(function () {
if (true) {
self.buttons_template = 'hiprint_print_temp_tree_view.buttons';
}
});
return Promise.all([this._super.apply(this, arguments), ready]);
},
renderButtons: function () {
this._super.apply(this, arguments);
renderGenerateButton.apply(this, arguments);
}
});
var ModelListView = ListView.extend({
config: _.extend({}, ListView.prototype.config, {
Controller: ModelListController,
}),
});
//将tree视图的按钮注册到视图
viewRegistry.add('print_temp_tree', ModelListView);
});
<odoo>
<data>
<template id="button_js" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/hiprint/static/src/js/print_temp_button.js"/>
</xpath>
</template>
</data>
</odoo>
<!--增加js_class="print_temp_tree"-->
<record model="ir.ui.view" id="print_temp_tree_view">
&