Odoo14使用hiPrint实现打印功能

使用hiPrint代替odoo原生的打印功能

可以实现快速自定义修改打印模板,无需每次都调整打印模板

无论是表单分页还是各种需求,都能满足

目录

1 使用命令创建新的模块,无用的demo文件可以删除掉

2 新建“打印模板”,用于保存打印模板

1)编写打印模板模型

2)增加打印模板权限

3)增加打印模板视图和菜单

3 首次运行先安装模块

4 增加按钮,定制并保存模板

1)增加自定义按钮“定制模板”xml

2)增加自定义按钮事件js,点击按钮实现打开新的页面

3)增加引入自定义按钮事件js的xml

4)列表视图增加自定义按钮

5)引入以上2个文件

6)引入js,bootstrap,hiprint并引用

7)编写接口,用于打开模板编辑界面,并实现修改保存打印模板

8)编写打开的打印模板页面,在打印模板页面调用接口,实现修改保存打印模板

5 编辑并保存打印模板

1)勾选打印模板记录,点击定制模板

2)在模板设计页面设计打印模板,并保存

6 新建“打印数据”,用于模拟测试打印功能(类似新建“打印模板”,此处省略说明)

1)增加模型,增加权限,增加视图,增加菜单,增加数据

7 在需要进行打印的列表处增加打印按钮

1)增加自定义按钮“打印”xml

2)增加自定义按钮事件js,点击按钮实现打印

3)增加引入自定义按钮事件js的xml

4)列表视图增加自定义按钮

5)引入以上2个文件

8 勾选数据记录,并点击打印按钮即可

9 插件下载


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">
        &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值