diff --git a/hr_payroll_attendance/__manifest__.py b/hr_payroll_attendance/__manifest__.py index e8cf0a9b..6b18d0a3 100755 --- a/hr_payroll_attendance/__manifest__.py +++ b/hr_payroll_attendance/__manifest__.py @@ -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', ], diff --git a/hr_payroll_attendance/models/hr_attendance.py b/hr_payroll_attendance/models/hr_attendance.py index de4a0d3b..7c005f72 100644 --- a/hr_payroll_attendance/models/hr_attendance.py +++ b/hr_payroll_attendance/models/hr_attendance.py @@ -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() diff --git a/hr_payroll_attendance/models/hr_payslip.py b/hr_payroll_attendance/models/hr_payslip.py index df811cf4..d000ce79 100755 --- a/hr_payroll_attendance/models/hr_payslip.py +++ b/hr_payroll_attendance/models/hr_payslip.py @@ -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 diff --git a/hr_payroll_attendance/tests/test_payroll_attendance.py b/hr_payroll_attendance/tests/test_payroll_attendance.py index 80472a52..b94df0ff 100644 --- a/hr_payroll_attendance/tests/test_payroll_attendance.py +++ b/hr_payroll_attendance/tests/test_payroll_attendance.py @@ -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) diff --git a/hr_payroll_attendance/views/hr_attendance_views.xml b/hr_payroll_attendance/views/hr_attendance_views.xml new file mode 100644 index 00000000..c0da03f6 --- /dev/null +++ b/hr_payroll_attendance/views/hr_attendance_views.xml @@ -0,0 +1,17 @@ + + + + + hr.attendance.tree.inherit + hr.attendance + + + + + + + + + + + diff --git a/hr_payroll_attendance/views/hr_payslip_views.xml b/hr_payroll_attendance/views/hr_payslip_views.xml index 292f413f..90a390d9 100644 --- a/hr_payroll_attendance/views/hr_payslip_views.xml +++ b/hr_payroll_attendance/views/hr_payslip_views.xml @@ -12,7 +12,7 @@ - +