onchange方法不适用于新记录

时间:2019-01-14 11:29:26

标签: python odoo odoo-12

我有python函数,我希望它为所有薪水单记录填充one2many字段。 Onchange方法仅在更改已创建的记录的employee_id时有效,而在创建新记录时不起作用。我需要创建,保存然后进行编辑。

如何使它也适用于新记录(工资单)?

all_project_hours = fields.One2many('hr.payslip.projects', 'slip_id', "Project Hours")

@api.onchange('employee_id')
def _calculate_project_hours(self):
    all_project_hours = []
    value = {}
    projects = self.env['project.project'].search([])
    for project in projects:
        domain = [('employee_id', '=', self.employee_id.name), ('date', '>=', self.date_from),
                  ('date', '<=', self.date_to),
                  ('validated', '=', True), ('is_bonus_eligible', '=', True), ('project_id', '=', project.name)]
        domain_ot = [('employee_id', '=', self.employee_id.name), ('date', '>=', self.date_from),
                     ('date', '<=', self.date_to),
                     ('validated', '=', True), ('is_bonus_eligible', '=', True), ('project_id', '=', project.name),
                     ('task_id', '=', 'Overtime')]
        all_timesheets = self.env["account.analytic.line"].search(domain)
        ot_timesheets = self.env["account.analytic.line"].search(domain_ot)
        sum_all = 0.0
        sum_ot = 0.0
        for unit in all_timesheets:
            sum_all += unit.unit_amount
        for ot in ot_timesheets:
            sum_ot += ot.unit_amount
        if self.total_project_hours > 0.0:
            split = sum_all / self.total_project_hours * 100
        else:
            split = 0.0
            return split
        all_project_hours.append(
            (0, 0, {'project_id': project.id, 'project_hours': sum_all, 'overtime_hours': sum_ot, 'project_split': split}))
    value.update(all_project_hours=all_project_hours)
    return {'value': value}

2 个答案:

答案 0 :(得分:0)

覆盖创建和写入函数,并在两者中调用_calculate_project_hours函数

答案 1 :(得分:0)

@Akshay的建议很好。

  

重写createwrite函数并在两个函数中都调用_calculate_project_hours函数。

有关如何执行此操作的示例:

@api.multi
@api.onchange('employee_id')
def _calculate_project_hours(self):
    projects = self.env['project.project'].search([])
    # I changed this to @api.multi, so it must loop through all records in self
    # Any references to `self` inside the loop are now changed to `record`
    for record in self:
        all_project_hours = []
        value = {}
        for project in projects:
            domain = [('employee_id', '=', record.employee_id.name), ('date', '>=', record.date_from),
                      ('date', '<=', record.date_to),
                      ('validated', '=', True), ('is_bonus_eligible', '=', True), ('project_id', '=', project.name)]
            domain_ot = [('employee_id', '=', record.employee_id.name), ('date', '>=', record.date_from),
                         ('date', '<=', record.date_to),
                         ('validated', '=', True), ('is_bonus_eligible', '=', True), ('project_id', '=', project.name),
                         ('task_id', '=', 'Overtime')]
            all_timesheets = record.env["account.analytic.line"].search(domain)
            ot_timesheets = record.env["account.analytic.line"].search(domain_ot)
            sum_all = 0.0
            sum_ot = 0.0
            for unit in all_timesheets:
                sum_all += unit.unit_amount
            for ot in ot_timesheets:
                sum_ot += ot.unit_amount
            if record.total_project_hours > 0.0:
                split = sum_all / record.total_project_hours * 100
            else:
                split = 0.0
                return split
            all_project_hours.append(
                (0, 0, {'project_id': project.id, 'project_hours': sum_all, 'overtime_hours': sum_ot, 'project_split': split}))
        # Assign the result to the record instead of returning it as a `value` dict
        record.all_project_hours = all_project_hours

@api.model
def create(self, vals):
    res = super().create(vals)
    res._calculate_project_hours()
    return res

@api.multi
def write(self, vals):
    res = super().write(vals)
    self._calculate_project_hours()
    return res

有关详细信息,请参见Odoo ORM Documentation