如何通过点击按钮在 OEv7 中进行计算?

44 阅读2分钟

有一个问题需要解决,即在菜单下“假期”部分的分配请求中进行一些简单的计算。

在视图 xml 文件中添加了一个按钮,如下所示:

<record model="ir.ui.view" id="view_edit_holiday_allocation_form">
            <field name="name">allocation</field>
            <field name="model">hr.holidays</field>
            <field name="inherit_id" ref="hr_holidays.allocation_leave_new"/>
            <field name="arch" type="xml">
                <data>
                        <field name="department_id" position="after">
                <field name="monthly_quota"/>
                        <field name="refusal_date"/>
                        <button name="calc_monthly_quota" type="object" string="Calculate Monthly Quota"/>
                        </field>
            </data>
        </field>
    </record>

在 .py 文件中

class hr_holidays(osv.osv):
    _inherit = "hr.holidays"
def calc_monthly_quota(self, cr, uid, ids, context=None):

    for record in self.browse(cr, uid, ids):
        if record.state :
            self.write(cr, uid, [record.id],{'monthly_quota':\
    record.number_of_days_temp/12})
    return True

_columns = {
        "monthly_quota": fields.float("Monthly Quota", readonly=True,
     states={'draft':[('readonly',False)]}, help="If monthly leave \
     limit is more then xero then employee can not take leave more \
     then monthly allocation in total allocation. If monthly quota \
     is zero user can take leave as per his allocation limit."),
        "refusal_date" : fields.date('Date of Refusal'),
        "create_date" : fields.date('Create Date'),
         }

只要在点击按钮时计算出请假的月度配额即可。比如在分配(number_of_days_temp)中输入 12,那么月度配额应该为 1。

除了记录状态外,其他一切运行正常。点击按钮后,记录状态从“提交”即草稿变为“批准”即确认。在保存表单之前,表单的状态会自行更改,理想情况下,表单的状态应该在点击保存按钮之后才更改。

已经阅读了 Odoo 7.0 文档,其中提到:按钮被点击后,记录应该总是重新加载。

但现在还不清楚需要做什么才能在不保存的情况下更改表单的状态。

2. 解决方案

当点击按钮时,需要使用 Odoo 的 onchange 方法来重新加载记录而无需保存它。这意味着当按钮被点击时,onchange 方法将被调用,该方法将重新加载记录并计算月度配额。

以下是在 .py 文件中添加 onchange 方法的示例:

class hr_holidays(osv.osv):
    _inherit = "hr.holidays"

    @api.onchange('number_of_days_temp')
    def _onchange_number_of_days_temp(self):
        if self.number_of_days_temp:
            self.monthly_quota = self.number_of_days_temp / 12

    def calc_monthly_quota(self, cr, uid, ids, context=None):

        for record in self.browse(cr, uid, ids):
            if record.state:
                self.write(cr, uid, [record.id], {'monthly_quota': \
                record.number_of_days_temp / 12})
        return True

    _columns = {
        "monthly_quota": fields.float("Monthly Quota", readonly=True,
                                     states={'draft': [('readonly', False)]},
                                     help="If monthly leave "
                                          "limit is more then xero then employee can not take leave more "
                                          "then monthly allocation in total allocation. If monthly quota "
                                          "is zero user can take leave as per his allocation limit."),
        "refusal_date": fields.date('Date of Refusal'),
        "create_date": fields.date('Create Date'),
    }

使用 onchange 方法后,就可以在不保存的情况下重新加载记录并计算月度配额。