From 7c2a7bc2c391045b476646f44a79f8c1f259d966 Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Mon, 23 May 2022 19:22:51 +0000 Subject: [PATCH] [FIX] hr_payroll_gamification: modernize input line --- hr_payroll_gamification/models/payroll.py | 58 +++++++++++++++-------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/hr_payroll_gamification/models/payroll.py b/hr_payroll_gamification/models/payroll.py index 06aecb0b..eee89a13 100644 --- a/hr_payroll_gamification/models/payroll.py +++ b/hr_payroll_gamification/models/payroll.py @@ -4,28 +4,46 @@ from odoo import api, Command, fields, models class Payslip(models.Model): _inherit = 'hr.payslip' - @api.depends('employee_id', 'contract_id', 'struct_id', 'date_from', 'date_to', 'struct_id') - def _compute_input_line_ids(self): - res = super()._compute_input_line_ids() - badge_type = self.env.ref('hr_payroll_gamification.badge_other_input', raise_if_not_found=False) - if not badge_type: - return res - for slip in self: - amount = slip._get_input_badges() - if not amount: - continue - slip.update({ - 'input_line_ids': [ - Command.create({ - 'name': badge_type.name or 'Badges', - 'amount': amount, - 'input_type_id': badge_type.id, - }), - ], - }) + @api.model_create_multi + def create(self, vals_list): + payslips = super().create(vals_list) + draft_slips = payslips.filtered(lambda p: p.employee_id and p.state == 'draft') + if not draft_slips: + return payslips + + draft_slips._update_badges() + return payslips + + def write(self, vals): + res = super().write(vals) + if 'date_to' in vals or 'date_from' in vals: + self._update_badges() return res - def _get_input_badges(self): + def _update_badges(self): + badge_type = self.env.ref('hr_payroll_gamification.badge_other_input', raise_if_not_found=False) + if not badge_type: + return + for payslip in self: + amount = payslip._get_input_badge_amount() + line = payslip.input_line_ids.filtered(lambda l: l.input_type_id == badge_type) + command = [] + if line and not amount: + command.append((2, line.id, False)) + elif line and amount: + command.append((1, line.id, { + 'amount': amount, + })) + elif amount: + command.append((0, 0, { + 'name': badge_type.name, + 'amount': amount, + 'input_type_id': badge_type.id, + })) + if command: + payslip.update({'input_line_ids': command}) + + def _get_input_badge_amount(self): amount = 0.0 for bu in self.employee_id.badge_ids.filtered(lambda bu: bu.badge_id.payroll_type == 'fixed'): amount += bu.badge_id.payroll_amount