Skip to content

Commit 13758eb

Browse files
[MIG] hr_timesheet_holiday: Migration to 15.0
1 parent 8d040d1 commit 13758eb

File tree

12 files changed

+64
-71
lines changed

12 files changed

+64
-71
lines changed

hr_timesheet_holiday/README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Contributors
7575
* Tom Blauwendraat <tom@sunflowerweb.nl>
7676
* Terrence Nzaywa <terrence@sunflowerweb.nl>
7777
* Holger Brunn <hbrunn@therp.nl>
78-
* Aaron Henriquez <ahenriquez@eficent.com>
78+
* Aaron Henriquez <aaron.henriquez@forgeflow.com>
7979

8080
Maintainer
8181
----------

hr_timesheet_holiday/__manifest__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
{
55
"name": "HR Timesheet Holiday",
6-
"version": "10.0.1.0.1",
6+
"version": "15.0.1.0.0",
77
"category": "Generic Modules/Human Resources",
88
"summary": """When holidays are granted, add lines to the analytic account
99
that is linked to the Leave Type""",
@@ -14,6 +14,7 @@
1414
"hr_timesheet_sheet",
1515
"hr_holidays",
1616
"hr_holidays_settings",
17+
"project_timesheet_holidays",
1718
],
1819
"data": [
1920
"views/hr_holidays_view.xml",
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from . import analytic_account
22
from . import analytic_line
33
from . import company
4-
from . import hr_holidays
5-
from . import hr_holidays_status
4+
from . import hr_leave
5+
from . import hr_leave_type

hr_timesheet_holiday/models/analytic_account.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AnalyticAccount(models.Model):
1616
default=False,
1717
)
1818
holiday_status_ids = fields.One2many(
19-
comodel_name="hr.holidays.status",
19+
comodel_name="hr.leave.type",
2020
inverse_name="analytic_account_id",
2121
)
2222

@@ -26,7 +26,6 @@ def _trigger_project_creation(self, vals):
2626
return True
2727
return super(AnalyticAccount, self)._trigger_project_creation(vals)
2828

29-
@api.multi
3029
def project_create(self, vals):
3130
res = super(AnalyticAccount, self).project_create(vals)
3231
if isinstance(res, (int, float)):
@@ -37,7 +36,6 @@ def project_create(self, vals):
3736
return res
3837

3938
@api.constrains("is_leave_account", "project_ids")
40-
@api.multi
4139
def _check_account_allow_timesheet(self):
4240
for aa in self:
4341
if any(project.allow_timesheets is False for project in aa.project_ids):

hr_timesheet_holiday/models/analytic_line.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class AnalyticLine(models.Model):
1010

1111
_inherit = "account.analytic.line"
1212

13-
leave_id = fields.Many2one(comodel_name="hr.holidays", string="Leave id")
13+
leave_id = fields.Many2one(comodel_name="hr.leave", string="Leave id")
1414

1515
@api.depends(
1616
"date",
@@ -25,12 +25,12 @@ def _compute_sheet(self):
2525
"""Links the timesheet line to the corresponding sheet"""
2626
for ts_line in self:
2727
if not ts_line.account_id.is_leave_account:
28-
super(AnalyticLine, ts_line)._compute_sheet()
28+
return super(AnalyticLine, ts_line)._compute_sheet()
2929
else:
30-
sheets = self.env["hr_timesheet_sheet.sheet"].search(
30+
sheets = self.env["hr_timesheet.sheet"].search(
3131
[
32-
("date_to", ">=", ts_line.date),
33-
("date_from", "<=", ts_line.date),
32+
("date_start", ">=", ts_line.date),
33+
("date_end", "<=", ts_line.date),
3434
("employee_id.user_id.id", "=", ts_line.user_id.id),
3535
("state", "in", ["draft", "new"]),
3636
]
@@ -39,7 +39,6 @@ def _compute_sheet(self):
3939
ts_line.sheet_id_computed = sheets[0]
4040
ts_line.sheet_id = sheets[0]
4141

42-
@api.multi
4342
def write(self, vals):
4443
if not self.env.context.get("force_write", False):
4544
for rec in self:
@@ -51,4 +50,4 @@ def write(self, vals):
5150
"Please edit the leave request instead."
5251
)
5352
)
54-
super(AnalyticLine, self).write(vals)
53+
return super(AnalyticLine, self).write(vals)

hr_timesheet_holiday/models/company.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@ class ResCompany(models.Model):
99

1010
_inherit = "res.company"
1111

12-
timesheet_hours_per_day = fields.Float(
13-
string="Timesheet Hours Per Day", digits=(2, 2), default=8.0
14-
)
12+
timesheet_hours_per_day = fields.Float(digits=(2, 2), default=8.0)

hr_timesheet_holiday/models/hr_holidays.py hr_timesheet_holiday/models/hr_leave.py

+6-15
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from odoo.exceptions import Warning as UserError
88

99

10-
class HrHolidays(models.Model):
10+
class HrLeave(models.Model):
1111
"""Update analytic lines on status change of Leave Request"""
1212

13-
_inherit = "hr.holidays"
13+
_inherit = "hr.leave"
1414

1515
# Timesheet entry linked to this leave request
1616
analytic_line_ids = fields.One2many(
@@ -19,10 +19,6 @@ class HrHolidays(models.Model):
1919
string="Analytic Lines",
2020
)
2121

22-
def check_no_duplicate_leaves_hook(self, user, date, hours):
23-
return
24-
25-
@api.multi
2622
def add_timesheet_line(self, description, date, hours, account):
2723
"""Add a timesheet line for this leave"""
2824
self.ensure_one()
@@ -31,9 +27,6 @@ def add_timesheet_line(self, description, date, hours, account):
3127
raise UserError(_("No active projects for this Analytic Account"))
3228
# User exists because already checked during the action_approve
3329
user = self.employee_id.user_id
34-
public = self.check_no_duplicate_leaves_hook(user, date, hours)
35-
if public:
36-
return
3730
self.sudo().with_context(force_write=True).write(
3831
{
3932
"analytic_line_ids": [
@@ -66,15 +59,14 @@ def _get_hours_per_day(self, company, employee):
6659
)
6760
return hours_per_day
6861

69-
@api.multi
7062
def action_approve(self):
7163
"""On grant of leave, add timesheet lines"""
72-
res = super(HrHolidays, self).action_approve()
64+
res = super(HrLeave, self).action_approve()
7365

7466
# Postprocess Leave Types that have an analytic account configured
7567
for leave in self:
7668
account = leave.holiday_status_id.analytic_account_id
77-
if not account or leave.type != "remove":
69+
if not account:
7870
# we only work on leaves (type=remove, type=add is allocation)
7971
# which have an account set
8072
continue
@@ -92,7 +84,7 @@ def action_approve(self):
9284
)
9385

9486
# Add analytic lines for these leave hours
95-
leave.analytic_line_ids.sudo(user.id).unlink() # to be sure
87+
leave.analytic_line_ids.with_user(user.id).unlink() # to be sure
9688
dt_from = fields.Datetime.from_string(leave.date_from)
9789
dt_current = dt_from
9890
at_least_one_complete_day = False
@@ -122,9 +114,8 @@ def action_approve(self):
122114
)
123115
return res
124116

125-
@api.multi
126117
def action_refuse(self):
127118
"""On refusal of leave, delete timesheet lines"""
128-
res = super(HrHolidays, self).action_refuse()
119+
res = super(HrLeave, self).action_refuse()
129120
self.mapped("analytic_line_ids").with_context(force_write=True).unlink()
130121
return res

hr_timesheet_holiday/models/hr_holidays_status.py hr_timesheet_holiday/models/hr_leave_type.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
from odoo import fields, models
55

66

7-
class HrHolidaysStatus(models.Model):
7+
class HrLeaveType(models.Model):
88
"""Add project to holiday status"""
99

10-
_inherit = "hr.holidays.status"
10+
_inherit = "hr.leave.type"
1111

1212
analytic_account_id = fields.Many2one(
1313
comodel_name="account.analytic.account",
1414
string="Analytic Account",
15-
related="project_id.analytic_account_id",
16-
)
17-
project_id = fields.Many2one(
18-
comodel_name="project.project",
19-
string="Project",
15+
related="timesheet_project_id.analytic_account_id",
2016
)

hr_timesheet_holiday/tests/test_all.py

+37-24
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,29 @@
88

99
from odoo.exceptions import UserError, ValidationError
1010

11-
from odoo.addons.hr_holidays.tests.common import TestHrHolidaysBase
11+
from odoo.addons.hr_holidays.tests.common import TestHrHolidaysCommon
12+
from odoo.addons.mail.tests.common import mail_new_test_user
1213

1314

14-
class TimesheetHolidayTest(TestHrHolidaysBase):
15+
class TimesheetHolidayTest(TestHrHolidaysCommon):
1516
def setUp(self):
1617
super(TimesheetHolidayTest, self).setUp()
17-
self.leave = self.env["hr.holidays"]
18+
self.leave = self.env["hr.leave"]
1819
self.project = self.env["project.project"]
19-
self.sheet = self.env["hr_timesheet_sheet.sheet"]
20-
self.employee = self.env.ref("hr.employee_qdp")
20+
self.sheet = self.env["hr_timesheet.sheet"]
21+
# grant analytic account access
22+
self.user_hruser.groups_id += self.env.ref("analytic.group_analytic_accounting")
23+
24+
self.employee_user = mail_new_test_user(
25+
self.env, login="Test Emp", groups="base.group_user"
26+
)
27+
self.employee = self.env["hr.employee"].create(
28+
{
29+
"name": "Test Employee",
30+
"user_id": self.employee_user.id,
31+
"department_id": self.rd_dept.id,
32+
}
33+
)
2134
self.sl = self.env.ref("hr_holidays.holiday_status_sl")
2235

2336
def _create_timesheet(self, employee, date_from, date_to):
@@ -40,6 +53,7 @@ def test_all(self):
4053
"allow_timesheets": False,
4154
}
4255
)
56+
project._create_analytic_account()
4357
account = project.analytic_account_id
4458
with self.assertRaises(ValidationError):
4559
# Create analytic account
@@ -48,30 +62,29 @@ def test_all(self):
4862
account.write({"is_leave_account": True})
4963
# Link sick leave to analytic account
5064
sl = self.sl
51-
sl.write({"project_id": project.id})
65+
sl.write({"timesheet_project_id": project.id})
5266
# Confirm leave and check hours added to account
5367
hours_before = sum(account.line_ids.mapped("amount"))
54-
# Holidays.sudo(self.user_employee_id)
55-
hol_empl_grp = self.leave.sudo(self.user_hruser_id)
68+
# Holidays.with_user(self.user_employee_id)
69+
hol_empl_grp = self.leave.with_user(self.user_hruser_id)
5670
leave = hol_empl_grp.create(
5771
{
5872
"name": "One week sick leave",
5973
"employee_id": self.employee_emp_id,
6074
"holiday_status_id": self.sl.id,
6175
"date_from": (datetime.today() - relativedelta(days=7)),
6276
"date_to": datetime.today(),
63-
"number_of_days_temp": 7.0,
6477
}
6578
)
6679
self.assertEqual(
6780
leave.state,
6881
"confirm",
6982
"hr_holidays: newly created leave request should be in " "confirm state",
7083
)
71-
leave.sudo(self.user_hruser_id).action_approve()
84+
leave.with_user(self.user_hruser_id).action_approve()
7285

7386
hours_after = sum(account.line_ids.mapped("unit_amount"))
74-
self.assertEqual(hours_after - hours_before, 35.0)
87+
self.assertEqual(hours_after - hours_before, 28.0)
7588

7689
# Test editing of lines forbidden
7790
self.assertRaises(
@@ -81,7 +94,7 @@ def test_all(self):
8194
# Test force editing of lines allowed
8295
account.line_ids[0].with_context(force_write=True).write({"unit_amount": 5.0})
8396
hours_after = sum(account.line_ids.mapped("unit_amount"))
84-
self.assertEqual(hours_after - hours_before, 33.0)
97+
self.assertEqual(hours_after - hours_before, 26.0)
8598
# Ensure the user_id defined on generated analytic lines is the user
8699
# set on the employee
87100
user_employee = self.env["hr.employee"].browse(self.employee_emp_id).user_id
@@ -100,21 +113,21 @@ def test_timesheet(self):
100113
"allow_timesheets": True,
101114
}
102115
)
116+
project._create_analytic_account()
103117
account = project.analytic_account_id
104118
account.write({"is_leave_account": True})
105119
# Link sick leave to analytic account
106120
sl = self.sl
107121
sl.write({"analytic_account_id": account.id})
108122

109-
hol_empl_grp = self.leave.sudo(self.user_employee_id)
123+
hol_empl_grp = self.leave.with_user(self.user_employee_id)
110124
leave = hol_empl_grp.create(
111125
{
112126
"name": "One week sick leave",
113127
"employee_id": self.employee_emp_id,
114128
"holiday_status_id": self.sl.id,
115129
"date_from": time.strftime("1900-01-06"),
116130
"date_to": time.strftime("1900-01-12"),
117-
"number_of_days_temp": 7.0,
118131
}
119132
)
120133
with self.assertRaises(UserError):
@@ -128,6 +141,7 @@ def test_allocation(self):
128141
"allow_timesheets": True,
129142
}
130143
)
144+
project._create_analytic_account()
131145
account = project.analytic_account_id
132146
account.write({"is_leave_account": True})
133147
# Link sick leave to analytic account
@@ -139,9 +153,7 @@ def test_allocation(self):
139153
"holiday_status_id": self.sl.id,
140154
"date_from": time.strftime("%Y-%m-06"),
141155
"date_to": time.strftime("%Y-%m-12"),
142-
"number_of_days_temp": 7.0,
143156
"employee_id": self.employee.id,
144-
"type": "add",
145157
}
146158
)
147159
leave.action_approve()
@@ -160,26 +172,27 @@ def test_timesheet_half_day(self):
160172
"allow_timesheets": False,
161173
}
162174
)
175+
project._create_analytic_account()
163176
account = project.analytic_account_id
164177
project.write({"allow_timesheets": True})
165178
account.write({"is_leave_account": True})
166179
# Link sick leave to analytic account
167180
sl = self.sl
168-
sl.write({"project_id": project.id})
181+
sl.write({"timesheet_project_id": project.id})
169182
# Confirm leave and check hours added to account
170183
hours_before = sum(account.line_ids.mapped("amount"))
171-
# Holidays.sudo(self.user_employee_id)
172-
hol_empl_grp = self.leave.sudo(self.user_hruser_id)
184+
# Holidays.with_user(self.user_employee_id)
185+
hol_empl_grp = self.leave.with_user(self.user_hruser_id)
186+
leave_date = datetime.today() + (relativedelta(days=10))
173187
leave = hol_empl_grp.create(
174188
{
175189
"name": "One day and a half sick leave",
176190
"employee_id": self.employee_emp_id,
177191
"holiday_status_id": self.sl.id,
178-
"date_from": (datetime.today() - relativedelta(hours=10.5)),
179-
"date_to": datetime.today(),
180-
"number_of_days_temp": 1.5,
192+
"date_from": leave_date - relativedelta(hours=10.5),
193+
"date_to": leave_date,
181194
}
182195
)
183-
leave.sudo(self.user_hruser_id).action_approve()
196+
leave.with_user(self.user_hruser_id).action_approve()
184197
hours_after = sum(account.line_ids.mapped("unit_amount"))
185-
self.assertEqual(hours_after - hours_before, 10.5)
198+
self.assertEqual(hours_after - hours_before, 3.5)

0 commit comments

Comments
 (0)