summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjem-odoo <jem@openerp.com>2018-01-09 09:47:32 +0100
committerjem-odoo <jem@openerp.com>2018-01-15 15:15:45 +0100
commitaa8a39fc4f2dab278a0cf42a58656527ce1efe19 (patch)
tree408b4e98d6b2a3949534d1f1923ab8720c49a797
parent628cddca24ca9d6576092ed8d98d12d39dc88e97 (diff)
[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
-rw-r--r--addons/hr_timesheet/project_timesheet.py15
-rw-r--r--addons/hr_timesheet/tests/test_timesheet.py41
2 files changed, 55 insertions, 1 deletions
diff --git a/addons/hr_timesheet/project_timesheet.py b/addons/hr_timesheet/project_timesheet.py
index 8a50f3c1ef4..20ef3b873db 100644
--- a/addons/hr_timesheet/project_timesheet.py
+++ b/addons/hr_timesheet/project_timesheet.py
@@ -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
diff --git a/addons/hr_timesheet/tests/test_timesheet.py b/addons/hr_timesheet/tests/test_timesheet.py
index e744d4341f7..ee93d9e0905 100644
--- a/addons/hr_timesheet/tests/test_timesheet.py
+++ b/addons/hr_timesheet/tests/test_timesheet.py
@@ -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
+ })