[FIX] hr_timesheet: change project of timesheet when changing task

When we change the project of a task, we want the
related timesheets to be linked to the new project.
However, since a timesheet is an analytic line with
a project, a task with timesheet can not be without
project. In that case, an error is raise to prevent
this case.

opw-803811
This commit is contained in:
jem-odoo
2018-01-15 15:15:45 +01:00
parent 628cddca24
commit aa8a39fc4f
2 changed files with 55 additions and 1 deletions
+15
View File
@@ -76,3 +76,18 @@ class Task(models.Model):
vals['parent_id'] = context.pop('default_parent_id', None)
task = super(Task, self.with_context(context)).create(vals)
return task
@api.multi
def write(self, values):
result = super(Task, self).write(values)
# reassign project_id on related timesheet lines
if 'project_id' in values:
project_id = values.get('project_id')
# a timesheet must have an analytic account (and a project)
if not project_id:
raise UserError(_('This task must have a project since they are linked to timesheets.'))
self.sudo().mapped('timesheet_ids').write({
'project_id': project_id,
'account_id': self.env['project.project'].browse(project_id).sudo().analytic_account_id.id
})
return result
+40 -1
View File
@@ -2,7 +2,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.tests.common import TransactionCase
from odoo.exceptions import AccessError
from odoo.exceptions import AccessError, UserError
class TestTimesheet(TransactionCase):
@@ -116,3 +116,42 @@ class TestTimesheet(TransactionCase):
'employee_id': self.empl_employee2.id,
})
self.assertEquals(timesheet1.user_id, self.user_employee2, 'Changing timesheet employee should change the related user')
def test_transfert_project(self):
""" Test transfert task with timesheet to another project """
Timesheet = self.env['account.analytic.line']
# create a second project
self.project_customer2 = self.env['project.project'].create({
'name': 'Project NUMBER DEUX',
'allow_timesheets': True,
})
# employee 1 log some timesheet on task 1
Timesheet.create({
'project_id': self.project_customer.id,
'task_id': self.task1.id,
'name': 'my first timesheet',
'unit_amount': 4,
})
timesheet_count1 = Timesheet.search_count([('project_id', '=', self.project_customer.id)])
timesheet_count2 = Timesheet.search_count([('project_id', '=', self.project_customer2.id)])
self.assertEquals(timesheet_count1, 1, "One timesheet in project 1")
self.assertEquals(timesheet_count2, 0, "No timesheet in project 2")
self.assertEquals(len(self.task1.timesheet_ids), 1, "The timesheet should be linked to task 1")
# change project of task 1
self.task1.write({
'project_id': self.project_customer2.id
})
timesheet_count1 = Timesheet.search_count([('project_id', '=', self.project_customer.id)])
timesheet_count2 = Timesheet.search_count([('project_id', '=', self.project_customer2.id)])
self.assertEquals(timesheet_count1, 0, "No timesheet in project 1")
self.assertEquals(timesheet_count2, 1, "One timesheet in project 2")
self.assertEquals(len(self.task1.timesheet_ids), 1, "The timesheet should be linked to task 1")
# it is forbidden to set a task with timesheet without project
with self.assertRaises(UserError):
self.task1.write({
'project_id': False
})