Skip to content

Commit 1c672af

Browse files
committed
[IMP] project,hr_timesheet: handle AA cases
When a project template is created not converted we should not create a AA. task-4700744
1 parent 11ec160 commit 1c672af

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

addons/hr_timesheet/models/project_project.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def _search_is_project_overtime(self, operator, value):
100100
@api.constrains('allow_timesheets', 'account_id')
101101
def _check_allow_timesheet(self):
102102
for project in self:
103-
if project.allow_timesheets and not project.account_id:
103+
if project.allow_timesheets and not project.account_id and not project.is_template:
104104
project_plan, _other_plans = self.env['account.analytic.plan']._get_all_plans()
105105
raise ValidationError(_(
106106
"To use the timesheets feature, you need an analytic account for your project. Please set one up in the plan '%(plan_name)s' or turn off the timesheets feature.",
@@ -135,12 +135,12 @@ def create(self, vals_list):
135135
""" Create an analytic account if project allow timesheet and don't provide one
136136
Note: create it before calling super() to avoid raising the ValidationError from _check_allow_timesheet
137137
"""
138-
defaults = self.default_get(['allow_timesheets', 'account_id'])
138+
defaults = self.default_get(['allow_timesheets', 'account_id', 'is_template'])
139139
analytic_accounts_vals = [
140140
vals for vals in vals_list
141141
if (
142142
vals.get('allow_timesheets', defaults.get('allow_timesheets')) and
143-
not vals.get('account_id', defaults.get('account_id'))
143+
not vals.get('account_id', defaults.get('account_id')) and not vals.get('is_template', defaults.get('is_template'))
144144
)
145145
]
146146

@@ -153,7 +153,7 @@ def create(self, vals_list):
153153
def write(self, values):
154154
# create the AA for project still allowing timesheet
155155
if values.get('allow_timesheets') and not values.get('account_id'):
156-
project_wo_account = self.filtered(lambda project: not project.account_id)
156+
project_wo_account = self.filtered(lambda project: not project.account_id and not project.is_template)
157157
if project_wo_account:
158158
project_wo_account._create_analytic_account()
159159
return super().write(values)
@@ -171,7 +171,7 @@ def _compute_display_name(self):
171171

172172
@api.model
173173
def _init_data_analytic_account(self):
174-
self.search([('account_id', '=', False), ('allow_timesheets', '=', True)])._create_analytic_account()
174+
self.search([('account_id', '=', False), ('allow_timesheets', '=', True), ('is_template', '=', False)])._create_analytic_account()
175175

176176
@api.ondelete(at_uninstall=False)
177177
def _unlink_except_contains_entries(self):
@@ -283,3 +283,8 @@ def action_view_tasks(self):
283283
action = super().action_view_tasks()
284284
action['context']['allow_timesheets'] = self.allow_timesheets
285285
return action
286+
287+
def action_create_from_template(self, values=None):
288+
project = super().action_create_from_template(values)
289+
project._create_analytic_account()
290+
return project

addons/hr_timesheet/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
from . import test_project_task_quick_create
66
from . import test_portal_timesheet
77
from . import test_project_project
8+
from . import test_project_template
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from odoo.tests import TransactionCase, tagged
2+
3+
4+
@tagged('post_install', '-at_install')
5+
class TestProjectProjectTemplate(TransactionCase):
6+
7+
def test_template_created_not_having_analytic_account(self):
8+
template_project, normal_project = self.env["project.project"].create(
9+
[
10+
{
11+
"name": "Template Project",
12+
"is_template": True,
13+
"allow_timesheets": True,
14+
},
15+
{
16+
"name": "Normal Project",
17+
"is_template": False,
18+
"allow_timesheets": True,
19+
},
20+
]
21+
)
22+
23+
self.assertFalse(template_project.account_id, "The template project shouldn't have analytic account")
24+
self.assertTrue(normal_project.account_id, "A normal project should have a analytic account")
25+
26+
def test_project_created_from_template_to_have_analytic_account(self):
27+
template_project = self.env['project.project'].create({
28+
"name": "Template Project",
29+
"is_template": True,
30+
"allow_timesheets": True,
31+
})
32+
33+
new_project = template_project.action_create_from_template()
34+
self.assertTrue(new_project.account_id, "A project created from template should have a analytic account")

0 commit comments

Comments
 (0)