[FIX] hr_contract: Reset current contract on stage modification
Purpose ======= Let's say we have 2 contracts A and B: A --> Running B --> Cancelled contract_id : A B is set to Running Contract_id : B B is set to Cancelled contract_id : B In this case, the current contract should be reset to A. X-original-commit: f736264881cbb3086430ea8cd7dba7c4f5a351f4 Part-of: odoo/odoo#92040
This commit is contained in:
@@ -215,9 +215,23 @@ class Contract(models.Model):
|
||||
return 'wage'
|
||||
|
||||
def write(self, vals):
|
||||
old_state = {c.id: c.state for c in self}
|
||||
res = super(Contract, self).write(vals)
|
||||
new_state = {c.id: c.state for c in self}
|
||||
if vals.get('state') == 'open':
|
||||
self._assign_open_contract()
|
||||
today = fields.Date.today()
|
||||
for contract in self:
|
||||
if contract == contract.employee_id.contract_id \
|
||||
and old_state[contract.id] == 'open' \
|
||||
and new_state[contract.id] != 'open':
|
||||
running_contract = self.env['hr.contract'].search([
|
||||
('employee_id', '=', contract.employee_id.id),
|
||||
('company_id', '=', contract.company_id.id),
|
||||
('state', '=', 'open'),
|
||||
]).filtered(lambda c: c.date_start <= today and (not c.date_end or c.date_end >= today))
|
||||
if running_contract:
|
||||
contract.employee_id.contract_id = running_contract[0]
|
||||
if vals.get('state') == 'close':
|
||||
for contract in self.filtered(lambda c: not c.date_end):
|
||||
contract.date_end = max(date.today(), contract.date_start)
|
||||
|
||||
@@ -2,10 +2,13 @@
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from datetime import datetime, date
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
from odoo.exceptions import ValidationError
|
||||
from odoo.addons.hr_contract.tests.common import TestContractCommon
|
||||
from odoo.tests import tagged
|
||||
|
||||
|
||||
@tagged('test_contracts')
|
||||
class TestHrContracts(TestContractCommon):
|
||||
|
||||
@classmethod
|
||||
@@ -101,3 +104,15 @@ class TestHrContracts(TestContractCommon):
|
||||
# New contract, with gap
|
||||
self.create_contract('open', 'normal', date(2016, 1, 1), date(2016, 1, 31))
|
||||
self.assertEqual(self.employee.first_contract_date, date(2017, 1, 1))
|
||||
|
||||
def test_current_contract_stage_change(self):
|
||||
today = date.today()
|
||||
contract = self.create_contract('open', 'normal', today + relativedelta(day=1), today + relativedelta(day=31))
|
||||
self.assertEqual(self.employee.contract_id, contract)
|
||||
|
||||
draft_contract = self.create_contract('draft', 'normal', today + relativedelta(months=1, day=1), today + relativedelta(months=1, day=31))
|
||||
draft_contract.state = 'open'
|
||||
self.assertEqual(self.employee.contract_id, draft_contract)
|
||||
|
||||
draft_contract.state = 'draft'
|
||||
self.assertEqual(self.employee.contract_id, contract)
|
||||
|
||||
Reference in New Issue
Block a user