[FIX] hr_payroll_attendance: Unlink behavior on attendances and remove "Work Calendar" work lines.

This commit is contained in:
Jared Kipe
2020-07-10 14:31:14 -07:00
parent cf399eb6c3
commit c07254ef2b
6 changed files with 80 additions and 40 deletions

View File

@@ -8,6 +8,7 @@
'category': 'Human Resources',
'data': [
'data/hr_payroll_attendance_data.xml',
'views/hr_attendance_views.xml',
'views/hr_contract_views.xml',
'views/hr_payslip_views.xml',
],

View File

@@ -4,4 +4,9 @@ from odoo import fields, models
class HrAttendance(models.Model):
_inherit = 'hr.attendance'
payslip_id = fields.Many2one('hr.payslip', string="Payslip", readonly=True)
payslip_id = fields.Many2one('hr.payslip', string="Payslip", readonly=True, ondelete='set null')
def unlink(self):
attn_with_payslip = self.filtered(lambda a: a.payslip_id)
attn_with_payslip.write({'payslip_id': False})
return super(HrAttendance, self - attn_with_payslip).unlink()

View File

@@ -6,7 +6,7 @@ class HrPayslip(models.Model):
_inherit = 'hr.payslip'
attendance_ids = fields.One2many('hr.attendance', 'payslip_id', string='Attendances',
help='Attendances represented by payslip.',
help='Attendances represented by payslip.', readonly=True,
states={'draft': [('readonly', False)], 'verify': [('readonly', False)]})
attendance_count = fields.Integer(compute='_compute_attendance_count')
@@ -15,53 +15,53 @@ class HrPayslip(models.Model):
for payslip in self:
payslip.attendance_count = len(payslip.attendance_ids)
@api.onchange('worked_days_line_ids')
def _onchange_worked_days_line_ids(self):
# super()._onchange_worked_days_line_ids()
attendance_type = self.env.ref('hr_payroll_attendance.work_input_attendance', raise_if_not_found=False)
if not self.worked_days_line_ids.filtered(lambda line: line.work_entry_type_id == attendance_type):
self.attendance_ids.write({'payslip_id': False})
def _get_worked_day_lines(self):
# Called at the end of _onchange_employee()
worked_day_lines = super()._get_worked_day_lines()
return self._attendance_get_worked_day_lines(worked_day_lines)
@api.onchange('employee_id', 'struct_id', 'contract_id', 'date_from', 'date_to')
def _onchange_employee(self):
res = super()._onchange_employee()
if self.state == 'draft' and self.contract_id.paid_hourly_attendance:
self.attendance_ids = self.env['hr.attendance'].search([
('employee_id', '=', self.employee_id.id),
('check_out', '<=', self.date_to),
'|', ('payslip_id', '=', False),
('payslip_id', '=', self.id),
])
self._onchange_attendance_ids()
return res
def _attendance_get_worked_day_lines(self, worked_day_lines):
"""
Filters out basic "Attendance"/"Work Calendar" entries as they would add to salary.
Note that this is during an onchange (probably).
:returns: a list of dict containing the worked days values that should be applied for the given payslip
"""
if not self.contract_id.paid_hourly_attendance:
return worked_day_lines
if not self.state == 'draft':
return worked_day_lines
attendance_to_keep = self.attendance_ids.filtered(lambda a: a.employee_id == self.employee_id
and a.check_out.date() <= self.date_to)
attendance_to_keep += self.env['hr.attendance'].search([
('employee_id', '=', self.employee_id.id),
('check_out', '<=', self.date_to),
('payslip_id', '=', False),
])
self.update({'attendance_ids': [(6, 0, attendance_to_keep.ids)]})
@api.onchange('attendance_ids')
def _onchange_attendance_ids(self):
original_work_type = self.env.ref('hr_work_entry.work_entry_type_attendance', raise_if_not_found=False)
attendance_type = self.env.ref('hr_payroll_attendance.work_input_attendance', raise_if_not_found=False)
if not attendance_type:
return
# return early, include the "work calendar lines"
return worked_day_lines
original_work_type = self.env.ref('hr_work_entry.work_entry_type_attendance', raise_if_not_found=False)
if original_work_type:
types_to_remove = original_work_type + attendance_type
else:
types_to_remove = attendance_type
# filter out "work calendar lines"
worked_day_lines = [w for w in worked_day_lines if w['work_entry_type_id'] != original_work_type.id]
work_data = self._pre_aggregate_attendance_data()
processed_data = self.aggregate_overtime(work_data)
lines_to_keep = self.worked_days_line_ids.filtered(lambda x: x.work_entry_type_id not in types_to_remove)
# Note that [(5, 0, 0)] + [(4, 999, 0)], will not work
work_lines_vals = [(3, line.id, False) for line in (self.worked_days_line_ids - lines_to_keep)]
work_lines_vals += [(4, line.id, False) for line in lines_to_keep]
work_lines_vals += [(0, 0, {
'number_of_days': data[0],
'number_of_hours': data[1],
'amount': data[1] * data[2] * self._wage_for_work_type(work_type),
'contract_id': self.contract_id.id,
'work_entry_type_id': work_type.id,
}) for work_type, data in processed_data.items()]
self.update({'worked_days_line_ids': work_lines_vals})
worked_day_lines += [{
'number_of_days': data[0],
'number_of_hours': data[1],
'amount': data[1] * data[2] * self._wage_for_work_type(work_type),
'contract_id': self.contract_id.id,
'work_entry_type_id': work_type.id,
} for work_type, data in processed_data.items()]
return worked_day_lines
def _wage_for_work_type(self, work_type):
# Override if you pay differently for different work types

View File

@@ -120,3 +120,20 @@ class TestUsPayslip(common.TransactionCase):
# (80 * 21.50) + (47.37 * 21.50 * 1.5) = 3247.6825
cats = self._getCategories()
self.assertAlmostEqual(cats['BASIC'], 3247.68, 2)
# ensure unlink behavior.
self.payslip.attendance_ids = self.env['hr.attendance'].browse()
self.payslip.state = 'draft'
self.payslip.flush()
self.payslip._onchange_employee()
self.payslip.compute_sheet()
cats = self._getCategories()
self.assertAlmostEqual(cats['BASIC'], 3247.68, 2)
self.payslip.write({'attendance_ids': [(5, 0, 0)]})
self.payslip.state = 'draft'
self.payslip.flush()
self.payslip._onchange_employee()
self.payslip.compute_sheet()
cats = self._getCategories()
self.assertAlmostEqual(cats['BASIC'], 3247.68, 2)

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_attendance_tree_inherit" model="ir.ui.view">
<field name="name">hr.attendance.tree.inherit</field>
<field name="model">hr.attendance</field>
<field name="inherit_id" ref="hr_attendance.view_attendance_tree"/>
<field name="arch" type="xml">
<data>
<xpath expr="//field[@name='worked_hours']" position="after">
<field name="payslip_id"/>
</xpath>
</data>
</field>
</record>
</odoo>

View File

@@ -12,7 +12,7 @@
</button>
</xpath>
<xpath expr="//field[@name='number']" position="after">
<field name="attendance_ids" invisible="1"/>
<field name="attendance_ids" invisible="1" widget="many2many"/>
</xpath>
</field>
</record>