[FIX] hr_payroll_gamification: modernize input line

This commit is contained in:
Jared Kipe
2022-05-23 19:22:51 +00:00
parent a63681a820
commit 7c2a7bc2c3

View File

@@ -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