前言
在日常中有很多审批场景,例如请销假。审批拒绝的时候应该给出原因,此时,在form界面点击拒绝的时候应该弹出输入窗口。如下图所示。
编写模型
模块的目录下,新建wizard文件夹,然后直接创建一个models.py
和views.py
参考代码如下:
class RejectReasonWizard(models.TransientModel):
_name = 'reject.reason.wizard'
vehicle_id = fields.Many2one('vehicle', string='车辆')
reason = fields.Text(string='拒绝原因')
def action_confirm(self):
self.vehicle_id.write({
'status': 'rejected',
'reason_for_rejection': self.reason,
'approval_time': fields.Datetime.now(),
'approver': self.env.user.name + '-' + self.env.user.login,
'qrcode': g_qrcode(self.vehicle_id.id), # 生成二维码
})
return {'type': 'ir.actions.act_window_close'}
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record model="ir.ui.view" id="view_reject_reason_wizard">
<field name="name">view.reject.reason.wizard</field>
<field name="model">reject.reason.wizard</field>
<field name="arch" type="xml">
<form>
<group col="4">
<field name="vehicle_id" options="{'no_create':True,'no_create_edit': True,'no_open':True}" readonly='1'/>
<newline/>
<field name="reason"/>
</group>
<group col="4">
<footer>
<button string='确定' name="action_confirm" type="object" class="oe_highlight"/>
或
<button string="取消" class="oe_highlight" special="cancel"/>
</footer>
</group>
</form>
</field>
</record>
<record model="ir.actions.act_window" id="action_reject_reason_wizard">
<field name="name">填写拒绝原因</field>
<field name="res_model">reject.reason.wizard</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_reject_reason_wizard"/>
<field name="target">new</field>
</record>
</odoo>
<header>
<button name="action_approve" type="object" string="审核通过" status="pending"
class="btn-primary" attrs="{'invisible': [('status', 'in', ['approved', 'rejected'])]}"/>
<button name="%(ev_01.action_reject_reason_wizard)d"
type="action" icon="fa-user-plus" class="oe_stat_button" string="审核拒绝"
attrs="{'invisible': [('status', 'in', ['approved', 'rejected'])]}"
context="{'default_vehicle_id': id}"
/>
<field name="status" widget="statusbar" statusbar_visible="pending"
statusbar_groups="pending_group,approved_group"/>
<statusbar name="pending_group" statusbar_visible="pending"/>
<statusbar name="approved_group" statusbar_visible="approved,rejected"/>
</header>