| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
|
| 4 |
from lxml import etree |
| 5 |
from unittest.mock import patch |
| 6 |
|
| 7 |
from odoo import fields |
| 8 |
from odoo.fields import Command |
| 9 |
from odoo.tests import Form, TransactionCase, new_test_user |
| 10 |
from odoo.exceptions import AccessError, RedirectWarning, UserError, ValidationError |
| 11 |
|
| 12 |
|
| 13 |
class TestCommonTimesheet(TransactionCase): |
| 14 |
|
| 15 |
@classmethod |
| 16 |
def setUpClass(cls): |
| 17 |
super(TestCommonTimesheet, cls).setUpClass() |
| 18 |
|
| 19 |
# Crappy hack to disable the rule from timesheet grid, if it exists |
| 20 |
# The registry doesn't contain the field timesheet_manager_id. |
| 21 |
# but there is an ir.rule about it, crashing during its evaluation |
| 22 |
rule = cls.env.ref('timesheet_grid.hr_timesheet_rule_approver_update', raise_if_not_found=False) |
| 23 |
if rule: |
| 24 |
rule.active = False |
| 25 |
|
| 26 |
# customer partner |
| 27 |
cls.partner = cls.env['res.partner'].create({ |
| 28 |
'name': 'Customer Task', |
| 29 |
'email': '[email protected]', |
| 30 |
'phone': '42', |
| 31 |
}) |
| 32 |
|
| 33 |
cls.analytic_plan = cls.env['account.analytic.plan'].create({ |
| 34 |
'name': 'Timesheet Plan Test', |
| 35 |
}) |
| 36 |
cls.analytic_account = cls.env['account.analytic.account'].create({ |
| 37 |
'name': 'Analytic Account for Test Customer', |
| 38 |
'partner_id': cls.partner.id, |
| 39 |
'plan_id': cls.analytic_plan.id, |
| 40 |
'code': 'TEST' |
| 41 |
}) |
| 42 |
|
| 43 |
# project and tasks |
| 44 |
cls.project_customer = cls.env['project.project'].create({ |
| 45 |
'name': 'Project X', |
| 46 |
'allow_timesheets': True, |
| 47 |
'partner_id': cls.partner.id, |
| 48 |
'account_id': cls.analytic_account.id, |
| 49 |
}) |
| 50 |
cls.task1 = cls.env['project.task'].create({ |
| 51 |
'name': 'Task One', |
| 52 |
'priority': '0', |
| 53 |
'state': '01_in_progress', |
| 54 |
'project_id': cls.project_customer.id, |
| 55 |
'partner_id': cls.partner.id, |
| 56 |
}) |
| 57 |
cls.task2 = cls.env['project.task'].create({ |
| 58 |
'name': 'Task Two', |
| 59 |
'priority': '1', |
| 60 |
'state': '1_done', |
| 61 |
'project_id': cls.project_customer.id, |
| 62 |
}) |
| 63 |
# users |
| 64 |
cls.user_employee = cls.env['res.users'].create({ |
| 65 |
'name': 'User Employee', |
| 66 |
'login': 'user_employee', |
| 67 |
'email': '[email protected]', |
| 68 |
'group_ids': [(6, 0, [cls.env.ref('hr_timesheet.group_hr_timesheet_user').id])], |
| 69 |
}) |
| 70 |
cls.user_employee2 = cls.env['res.users'].create({ |
| 71 |
'name': 'User Employee 2', |
| 72 |
'login': 'user_employee2', |
| 73 |
'email': '[email protected]', |
| 74 |
'group_ids': [(6, 0, [cls.env.ref('hr_timesheet.group_hr_timesheet_user').id])], |
| 75 |
}) |
| 76 |
cls.user_manager = cls.env['res.users'].create({ |
| 77 |
'name': 'User Officer', |
| 78 |
'login': 'user_manager', |
| 79 |
'email': '[email protected]', |
| 80 |
'group_ids': [(6, 0, [cls.env.ref('hr_timesheet.group_timesheet_manager').id])], |
| 81 |
}) |
| 82 |
# employees |
| 83 |
cls.empl_employee = cls.env['hr.employee'].create({ |
| 84 |
'name': 'User Empl Employee', |
| 85 |
'user_id': cls.user_employee.id, |
| 86 |
'employee_type': 'freelance', # Avoid searching the contract if hr_contract module is installed before this module. |
| 87 |
}) |
| 88 |
cls.empl_employee2 = cls.env['hr.employee'].create({ |
| 89 |
'name': 'User Empl Employee 2', |
| 90 |
'user_id': cls.user_employee2.id, |
| 91 |
'employee_type': 'freelance', |
| 92 |
}) |
| 93 |
cls.empl_manager = cls.env['hr.employee'].create({ |
| 94 |
'name': 'User Empl Officer', |
| 95 |
'user_id': cls.user_manager.id, |
| 96 |
'employee_type': 'freelance', |
| 97 |
}) |
| 98 |
cls.project = cls.env['project.project'].create({ |
| 99 |
'name': 'Test Project', |
| 100 |
'privacy_visibility': 'followers', |
| 101 |
'task_ids': [Command.create({ |
| 102 |
'name': 'Test Task', |
| 103 |
})], |
| 104 |
}) |
| 105 |
cls.timesheet = cls.env['account.analytic.line'].create({ |
| 106 |
'name': 'Test Timesheet', |
| 107 |
'project_id': cls.project.id, |
| 108 |
'task_id': cls.project.task_ids[0].id, |
| 109 |
'employee_id': cls.empl_employee.id, |
| 110 |
}) |
| 111 |
cls.timesheet_manager_no_project_user = new_test_user( |
| 112 |
cls.env, |
| 113 |
login='no_project_user', |
| 114 |
groups='hr_timesheet.group_timesheet_manager' |
| 115 |
) |
| 116 |
|
| 117 |
|
| 118 |
class TestTimesheet(TestCommonTimesheet): |
| 119 |
|
| 120 |
def setUp(self): |
| 121 |
super().setUp() |
| 122 |
# Make sure to clean the plan fields |
| 123 |
self.env.registry._setup_models__(self.env.cr) |
| 124 |
|
| 125 |
# Crappy hack to disable the rule from timesheet grid, if it exists |
| 126 |
# The registry doesn't contain the field timesheet_manager_id. |
| 127 |
# but there is an ir.rule about it, crashing during its evaluation |
| 128 |
rule = self.env.ref('timesheet_grid.timesheet_line_rule_user_update-unlink', raise_if_not_found=False) |
| 129 |
if rule: |
| 130 |
rule.active = False |
| 131 |
|
| 132 |
def test_log_timesheet(self): |
| 133 |
""" Test when log timesheet: check analytic account, user and employee are correctly set. """ |
| 134 |
Timesheet = self.env['account.analytic.line'] |
| 135 |
timesheet_uom = self.project_customer.account_id.company_id.project_time_mode_id |
| 136 |
# employee 1 log some timesheet on task 1 |
| 137 |
timesheet1 = Timesheet.with_user(self.user_employee).create({ |
| 138 |
'project_id': self.project_customer.id, |
| 139 |
'task_id': self.task1.id, |
| 140 |
'name': 'my first timesheet', |
| 141 |
'unit_amount': 4, |
| 142 |
}) |
| 143 |
self.assertEqual(timesheet1.account_id, self.project_customer.account_id, 'Analytic account should be the same as the project') |
| 144 |
self.assertEqual(timesheet1.employee_id, self.empl_employee, 'Employee should be the one of the current user') |
| 145 |
self.assertEqual(timesheet1.partner_id, self.task1.partner_id, 'Customer of task should be the same of the one set on new timesheet') |
| 146 |
self.assertEqual(timesheet1.product_uom_id, timesheet_uom, "The UoM of the timesheet should be the one set on the company of the analytic account.") |
| 147 |
|
| 148 |
# employee 1 cannot log timesheet for employee 2 |
| 149 |
with self.assertRaises(AccessError): |
| 150 |
timesheet2 = Timesheet.with_user(self.user_employee).create({ |
| 151 |
'project_id': self.project_customer.id, |
| 152 |
'task_id': self.task1.id, |
| 153 |
'name': 'a second timesheet but for employee 2', |
| 154 |
'unit_amount': 3, |
| 155 |
'employee_id': self.empl_employee2.id, |
| 156 |
}) |
| 157 |
|
| 158 |
# manager log timesheet for employee 2 |
| 159 |
timesheet3 = Timesheet.with_user(self.user_manager).create({ |
| 160 |
'project_id': self.project_customer.id, |
| 161 |
'task_id': self.task1.id, |
| 162 |
'name': 'a second timesheet but for employee 2', |
| 163 |
'unit_amount': 7, |
| 164 |
'employee_id': self.empl_employee2.id, |
| 165 |
}) |
| 166 |
self.assertEqual(timesheet3.user_id, self.user_employee2, 'Timesheet user should be the one linked to the given employee') |
| 167 |
self.assertEqual(timesheet3.product_uom_id, timesheet_uom, "The UoM of the timesheet 3 should be the one set on the company of the analytic account.") |
| 168 |
|
| 169 |
# employee 1 log some timesheet on project (no task) |
| 170 |
timesheet4 = Timesheet.with_user(self.user_employee).create({ |
| 171 |
'project_id': self.project_customer.id, |
| 172 |
'name': 'my first timesheet', |
| 173 |
'unit_amount': 4, |
| 174 |
}) |
| 175 |
self.assertEqual(timesheet4.partner_id, self.project_customer.partner_id, 'Customer of new timesheet should be the same of the one set project (since no task on timesheet)') |
| 176 |
|
| 177 |
def test_log_access_rights(self): |
| 178 |
""" Test access rights: user can update its own timesheets only, and manager can change all """ |
| 179 |
# employee 1 log some timesheet on task 1 |
| 180 |
Timesheet = self.env['account.analytic.line'] |
| 181 |
timesheet1 = Timesheet.with_user(self.user_employee).create({ |
| 182 |
'project_id': self.project_customer.id, |
| 183 |
'task_id': self.task1.id, |
| 184 |
'name': 'my first timesheet', |
| 185 |
'unit_amount': 4, |
| 186 |
}) |
| 187 |
# then employee 2 try to modify it |
| 188 |
with self.assertRaises(AccessError): |
| 189 |
timesheet1.with_user(self.user_employee2).write({ |
| 190 |
'name': 'i try to update this timesheet', |
| 191 |
'unit_amount': 2, |
| 192 |
}) |
| 193 |
# manager can modify all timesheet |
| 194 |
timesheet1.with_user(self.user_manager).write({ |
| 195 |
'unit_amount': 8, |
| 196 |
'employee_id': self.empl_employee2.id, |
| 197 |
}) |
| 198 |
self.assertEqual(timesheet1.user_id, self.user_employee2, 'Changing timesheet employee should change the related user') |
| 199 |
|
| 200 |
def test_create_unlink_project(self): |
| 201 |
""" Check project creation, and if necessary the analytic account generated when project should track time. """ |
| 202 |
# create project wihtout tracking time, nor provide AA |
| 203 |
non_tracked_project = self.env['project.project'].create({ |
| 204 |
'name': 'Project without timesheet', |
| 205 |
'allow_timesheets': False, |
| 206 |
'partner_id': self.partner.id, |
| 207 |
}) |
| 208 |
self.assertFalse(non_tracked_project.account_id, "A non time-tracked project shouldn't generate an analytic account") |
| 209 |
|
| 210 |
# create a project tracking time |
| 211 |
tracked_project = self.env['project.project'].create({ |
| 212 |
'name': 'Project with timesheet', |
| 213 |
'allow_timesheets': True, |
| 214 |
'partner_id': self.partner.id, |
| 215 |
}) |
| 216 |
self.assertTrue(tracked_project.account_id, "A time-tracked project should generate an analytic account") |
| 217 |
self.assertTrue(tracked_project.account_id.active, "A time-tracked project should generate an active analytic account") |
| 218 |
self.assertEqual(tracked_project.partner_id, tracked_project.account_id.partner_id, "The generated AA should have the same partner as the project") |
| 219 |
self.assertEqual(tracked_project.name, tracked_project.account_id.name, "The generated AA should have the same name as the project") |
| 220 |
self.assertEqual(tracked_project.account_id.project_count, 1, "The generated AA should be linked to the project") |
| 221 |
|
| 222 |
# create a project without tracking time, but with analytic account |
| 223 |
analytic_project = self.env['project.project'].create({ |
| 224 |
'name': 'Project without timesheet but with AA', |
| 225 |
'allow_timesheets': True, |
| 226 |
'partner_id': self.partner.id, |
| 227 |
'account_id': tracked_project.account_id.id, |
| 228 |
}) |
| 229 |
self.assertNotEqual(analytic_project.name, tracked_project.account_id.name, "The name of the associated AA can be different from the project") |
| 230 |
self.assertEqual(tracked_project.account_id.project_count, 2, "The AA should be linked to 2 project") |
| 231 |
|
| 232 |
# analytic linked to projects containing tasks can not be removed |
| 233 |
task = self.env['project.task'].create({ |
| 234 |
'name': 'task in tracked project', |
| 235 |
'project_id': tracked_project.id, |
| 236 |
}) |
| 237 |
with self.assertRaises(UserError): |
| 238 |
tracked_project.account_id.unlink() |
| 239 |
|
| 240 |
# task can be removed, as there is no timesheet |
| 241 |
task.unlink() |
| 242 |
|
| 243 |
# since both projects linked to the same analytic account are empty (no task), it can be removed |
| 244 |
tracked_project.account_id.unlink() |
| 245 |
|
| 246 |
def test_transfert_project(self): |
| 247 |
""" Transfert task with timesheet to another project. """ |
| 248 |
Timesheet = self.env['account.analytic.line'] |
| 249 |
Task = self.env['project.task'].with_context(default_project_id=self.task1.project_id.id) |
| 250 |
|
| 251 |
# create nested subtasks |
| 252 |
task_child = Task.create({ |
| 253 |
'name': 'Task Child', |
| 254 |
'parent_id': self.task1.id, |
| 255 |
}) |
| 256 |
|
| 257 |
task_grandchild = Task.create({ |
| 258 |
'name': 'Task Grandchild', |
| 259 |
'parent_id': task_child.id, |
| 260 |
}) |
| 261 |
|
| 262 |
# create a second project |
| 263 |
self.project_customer2 = self.env['project.project'].create({ |
| 264 |
'name': 'Project NUMBER DEUX', |
| 265 |
'allow_timesheets': True, |
| 266 |
}) |
| 267 |
# employee 1 log some timesheet on task 1 and its subtasks |
| 268 |
Timesheet.create([{ |
| 269 |
'project_id': self.project_customer.id, |
| 270 |
'task_id': self.task1.id, |
| 271 |
'name': 'my first timesheet', |
| 272 |
'unit_amount': 4, |
| 273 |
'employee_id': self.empl_employee.id, |
| 274 |
}, { |
| 275 |
'project_id': self.project_customer.id, |
| 276 |
'task_id': task_child.id, |
| 277 |
'name': 'my second timesheet', |
| 278 |
'unit_amount': 4, |
| 279 |
'employee_id': self.empl_employee.id, |
| 280 |
}, { |
| 281 |
'project_id': self.project_customer.id, |
| 282 |
'task_id': task_grandchild.id, |
| 283 |
'name': 'my third timesheet', |
| 284 |
'unit_amount': 4, |
| 285 |
'employee_id': self.empl_employee.id, |
| 286 |
}]) |
| 287 |
|
| 288 |
timesheet_count1 = Timesheet.search_count([('project_id', '=', self.project_customer.id)]) |
| 289 |
timesheet_count2 = Timesheet.search_count([('project_id', '=', self.project_customer2.id)]) |
| 290 |
self.assertEqual(timesheet_count1, 3, "3 timesheets should be linked to Project1") |
| 291 |
self.assertEqual(timesheet_count2, 0, "No timesheets should be linked to Project2") |
| 292 |
self.assertEqual(len(self.task1.timesheet_ids), 1, "The timesheet should be linked to task1") |
| 293 |
self.assertEqual(len(task_child.timesheet_ids), 1, "The timesheet should be linked to task_child") |
| 294 |
self.assertEqual(len(task_grandchild.timesheet_ids), 1, "The timesheet should be linked to task_grandchild") |
| 295 |
|
| 296 |
# change project of task 1 from form to trigger onchange |
| 297 |
with Form(self.task1) as task_form: |
| 298 |
task_form.project_id = self.project_customer2 |
| 299 |
|
| 300 |
timesheet_count1 = Timesheet.search_count([('project_id', '=', self.project_customer.id)]) |
| 301 |
timesheet_count2 = Timesheet.search_count([('project_id', '=', self.project_customer2.id)]) |
| 302 |
self.assertEqual(timesheet_count1, 0, "No timesheets should be linked to Project1") |
| 303 |
self.assertEqual(timesheet_count2, 3, "3 timesheets should be linked to Project2") |
| 304 |
self.assertEqual(len(self.task1.timesheet_ids), 1, "The timesheet still should be linked to task1") |
| 305 |
self.assertEqual(len(task_child.timesheet_ids), 1, "The timesheet still should be linked to task_child") |
| 306 |
self.assertEqual(len(task_grandchild.timesheet_ids), 1, "The timesheet still should be linked to task_grandchild") |
| 307 |
|
| 308 |
# It is forbidden to unset the project of a task with timesheet |
| 309 |
with self.assertRaises(UserError): |
| 310 |
self.task1.write({ |
| 311 |
'project_id': False |
| 312 |
}) |
| 313 |
|
| 314 |
def test_favorite_project_id(self): |
| 315 |
""" Test that user without previous timesheets and without |
| 316 |
access to the internal project has no favorite project. """ |
| 317 |
|
| 318 |
# make internal project accessible to invited internal users only |
| 319 |
self.env.company.internal_project_id.privacy_visibility = 'followers' |
| 320 |
|
| 321 |
favorite_project = self.env['account.analytic.line'].with_user(self.user_employee)._get_favorite_project_id() |
| 322 |
self.assertFalse(favorite_project, "A user without timesheet and without access to the internal project should have no favorite project.") |
| 323 |
|
| 324 |
def test_recompute_amount_for_multiple_timesheets(self): |
| 325 |
""" Check that amount is recomputed correctly when setting unit_amount for multiple timesheets at once. """ |
| 326 |
Timesheet = self.env['account.analytic.line'] |
| 327 |
self.empl_employee.hourly_cost = 5.0 |
| 328 |
self.empl_employee2.hourly_cost = 6.0 |
| 329 |
# create a timesheet for each employee |
| 330 |
timesheet_1 = Timesheet.with_user(self.user_employee).create({ |
| 331 |
'project_id': self.project_customer.id, |
| 332 |
'task_id': self.task1.id, |
| 333 |
'name': '/', |
| 334 |
'unit_amount': 1, |
| 335 |
}) |
| 336 |
timesheet_2 = Timesheet.with_user(self.user_employee2).create({ |
| 337 |
'project_id': self.project_customer.id, |
| 338 |
'task_id': self.task1.id, |
| 339 |
'name': '/', |
| 340 |
'unit_amount': 1, |
| 341 |
}) |
| 342 |
timesheets = timesheet_1 + timesheet_2 |
| 343 |
|
| 344 |
with self.assertRaises(AccessError): |
| 345 |
# should raise since employee 1 doesn't have the access rights to update employee's 2 timesheet |
| 346 |
timesheets.with_user(self.empl_employee.user_id).write({ |
| 347 |
'unit_amount': 2, |
| 348 |
}) |
| 349 |
|
| 350 |
timesheets.with_user(self.user_manager).write({ |
| 351 |
'unit_amount': 2, |
| 352 |
}) |
| 353 |
|
| 354 |
# since timesheet costs are different for both employees, we should get different amounts |
| 355 |
self.assertRecordValues(timesheets.with_user(self.user_manager), [{ |
| 356 |
'amount': -10.0, |
| 357 |
}, { |
| 358 |
'amount': -12.0, |
| 359 |
}]) |
| 360 |
|
| 361 |
def test_recompute_partner_from_task_customer_change(self): |
| 362 |
partner2 = self.env['res.partner'].create({ |
| 363 |
'name': 'Customer Task 2', |
| 364 |
'email': '[email protected]', |
| 365 |
'phone': '43', |
| 366 |
}) |
| 367 |
|
| 368 |
timesheet_entry = self.env['account.analytic.line'].create({ |
| 369 |
'project_id': self.project_customer.id, |
| 370 |
'task_id': self.task1.id, |
| 371 |
'name': 'my only timesheet', |
| 372 |
'unit_amount': 4, |
| 373 |
'user_id': self.user_employee.id, |
| 374 |
}) |
| 375 |
|
| 376 |
self.assertEqual(timesheet_entry.partner_id, self.partner, "The timesheet entry's partner should be equal to the task's partner/customer") |
| 377 |
|
| 378 |
self.task1.write({'partner_id': partner2}) |
| 379 |
|
| 380 |
self.assertEqual(timesheet_entry.partner_id, partner2, "The timesheet entry's partner should still be equal to the task's partner/customer, after the change") |
| 381 |
|
| 382 |
def test_task_with_timesheet_project_change(self): |
| 383 |
'''This test checks that no error is raised when moving a task that contains timesheet to another project. |
| 384 |
''' |
| 385 |
|
| 386 |
project_manager = self.env['res.users'].create({ |
| 387 |
'name': 'user_project_manager', |
| 388 |
'login': 'user_project_manager', |
| 389 |
'group_ids': [(6, 0, [self.ref('project.group_project_manager')])], |
| 390 |
}) |
| 391 |
|
| 392 |
project = self.env['project.project'].create({ |
| 393 |
'name': 'Project With Timesheets', |
| 394 |
'privacy_visibility': 'employees', |
| 395 |
'allow_timesheets': True, |
| 396 |
'user_id': project_manager.id, |
| 397 |
}) |
| 398 |
second_project = self.env['project.project'].create({ |
| 399 |
'name': 'Project w/ timesheets', |
| 400 |
'privacy_visibility': 'employees', |
| 401 |
'allow_timesheets': True, |
| 402 |
'user_id': project_manager.id, |
| 403 |
}) |
| 404 |
|
| 405 |
task_1 = self.env['project.task'].create({ |
| 406 |
'name': 'First task', |
| 407 |
'user_ids': self.user_employee2, |
| 408 |
'project_id': project.id |
| 409 |
}) |
| 410 |
|
| 411 |
timesheet = self.env['account.analytic.line'].create({ |
| 412 |
'name': 'FirstTimeSheet', |
| 413 |
'project_id': project.id, |
| 414 |
'task_id': task_1.id, |
| 415 |
'unit_amount': 2, |
| 416 |
'employee_id': self.empl_employee2.id |
| 417 |
}) |
| 418 |
|
| 419 |
task_1.with_user(project_manager).write({ |
| 420 |
'project_id': second_project.id |
| 421 |
}) |
| 422 |
|
| 423 |
self.assertEqual(timesheet.project_id, second_project, 'The project_id of non-validated timesheet should have changed') |
| 424 |
|
| 425 |
def test_compute_display_name(self): |
| 426 |
self.timesheet.with_user(self.timesheet_manager_no_project_user)._compute_display_name() |
| 427 |
self.assertEqual( |
| 428 |
self.timesheet.display_name, |
| 429 |
"Test Project - Test Task", |
| 430 |
"Display name should be correctly computed without raising AccessError." |
| 431 |
) |
| 432 |
|
| 433 |
def test_create_timesheet_employee_not_in_company(self): |
| 434 |
''' ts.employee_id only if the user has an employee in the company or one employee for all companies. |
| 435 |
''' |
| 436 |
company_2 = self.env['res.company'].create({'name': 'Company 2'}) |
| 437 |
company_3 = self.env['res.company'].create({'name': 'Company 3'}) |
| 438 |
|
| 439 |
analytic_plan = self.env['account.analytic.plan'].create({ |
| 440 |
'name': 'Plan Test', |
| 441 |
}) |
| 442 |
analytic_account = self.env['account.analytic.account'].create({ |
| 443 |
'name': 'Aa Aa', |
| 444 |
'plan_id': analytic_plan.id, |
| 445 |
'company_id': company_3.id, |
| 446 |
}) |
| 447 |
project = self.env['project.project'].create({ |
| 448 |
'name': 'Aa Project', |
| 449 |
'company_id': company_3.id, |
| 450 |
'account_id': analytic_account.id, |
| 451 |
}) |
| 452 |
task = self.env['project.task'].create({ |
| 453 |
'name': 'Aa Task', |
| 454 |
'project_id': project.id, |
| 455 |
}) |
| 456 |
|
| 457 |
Timesheet = self.env['account.analytic.line'].with_context(allowed_company_ids=[company_3.id, company_2.id, self.env.company.id]) |
| 458 |
timesheet = Timesheet.create({ |
| 459 |
'name': 'Timesheet', |
| 460 |
'project_id': project.id, |
| 461 |
'task_id': task.id, |
| 462 |
'unit_amount': 2, |
| 463 |
'user_id': self.user_manager.id, |
| 464 |
'company_id': company_3.id, |
| 465 |
}) |
| 466 |
self.assertEqual(timesheet.employee_id, self.user_manager.employee_id, 'As there is a unique employee for this user, it must be found') |
| 467 |
|
| 468 |
self.env['hr.employee'].with_company(company_2).create({ |
| 469 |
'name': 'Employee 2', |
| 470 |
'user_id': self.user_manager.id, |
| 471 |
}) |
| 472 |
with self.assertRaises(ValidationError): |
| 473 |
# As there are several employees for this user, but none of them in this company, none must be found |
| 474 |
Timesheet.create({ |
| 475 |
'name': 'Timesheet', |
| 476 |
'project_id': project.id, |
| 477 |
'task_id': task.id, |
| 478 |
'unit_amount': 2, |
| 479 |
'user_id': self.user_manager.id, |
| 480 |
'company_id': company_3.id, |
| 481 |
}) |
| 482 |
|
| 483 |
def test_create_timesheet_with_multi_company(self): |
| 484 |
""" Always set the current company in the timesheet, not the employee company """ |
| 485 |
company_4 = self.env['res.company'].create({'name': 'Company 4'}) |
| 486 |
empl_employee, archived_employee = self.env['hr.employee'].with_company(company_4).create([ |
| 487 |
{'name': 'Employee 3'}, |
| 488 |
{'name': 'Employee 4', 'active': False}, |
| 489 |
]) |
| 490 |
|
| 491 |
Timesheet = self.env['account.analytic.line'].with_context(allowed_company_ids=[company_4.id, self.env.company.id]) |
| 492 |
|
| 493 |
timesheet = Timesheet.create({ |
| 494 |
'project_id': self.project_customer.id, |
| 495 |
'task_id': self.task1.id, |
| 496 |
'name': 'my first timesheet', |
| 497 |
'unit_amount': 4, |
| 498 |
'employee_id': empl_employee.id, |
| 499 |
}) |
| 500 |
self.assertEqual(timesheet.company_id.id, self.env.company.id) |
| 501 |
|
| 502 |
with self.assertRaises(UserError, msg="The employee must be active to encode a timesheet"): |
| 503 |
Timesheet.create({ |
| 504 |
'project_id': self.project_customer.id, |
| 505 |
'task_id': self.task1.id, |
| 506 |
'name': 'my first timesheet', |
| 507 |
'unit_amount': 4, |
| 508 |
'employee_id': archived_employee.id, |
| 509 |
}) |
| 510 |
|
| 511 |
def test_subtask_log_timesheet(self): |
| 512 |
""" Test parent task takes into account the timesheets of its sub-tasks. |
| 513 |
Test Case: |
| 514 |
---------- |
| 515 |
1) Create parent task |
| 516 |
2) Create child/subtask task |
| 517 |
3) Enter the 8 hour timesheet in the child task |
| 518 |
4) Check subtask Effective hours in parent task |
| 519 |
""" |
| 520 |
subtask_1, subtask_2 = self.env['project.task'].create([ |
| 521 |
{ |
| 522 |
'name': 'Subtask 1', |
| 523 |
'project_id': self.project_customer.id, |
| 524 |
}, |
| 525 |
{ |
| 526 |
'name': 'Subtask 2', |
| 527 |
'project_id': self.project_customer.id, |
| 528 |
'child_ids': [Command.create({'name': 'Subsubtask', 'project_id': self.project_customer.id})], |
| 529 |
}, |
| 530 |
]) |
| 531 |
subsubtask = subtask_2.child_ids |
| 532 |
self.task1.child_ids = subtask_1 + subtask_2 |
| 533 |
self.assertTrue(self.project_customer.allow_timesheets, 'The project should be timesheetable') |
| 534 |
self.assertEqual(subtask_1.allow_timesheets, self.project_customer.allow_timesheets, 'The subtask should follow the settings of its project linked.') |
| 535 |
Timesheet = self.env['account.analytic.line'] |
| 536 |
Timesheet.create({ |
| 537 |
'name': 'FirstTimeSheet', |
| 538 |
'project_id': self.project_customer.id, |
| 539 |
'task_id': subtask_1.id, |
| 540 |
'unit_amount': 8.0, |
| 541 |
'employee_id': self.empl_employee2.id, |
| 542 |
}) |
| 543 |
self.assertEqual(self.task1.subtask_effective_hours, 8, 'Hours Spent on Sub-tasks should be 8 hours in Parent Task') |
| 544 |
|
| 545 |
Timesheet.create([ |
| 546 |
{ |
| 547 |
'name': '/', |
| 548 |
'task_id': subtask_2.id, |
| 549 |
'unit_amount': 1.0, |
| 550 |
'employee_id': self.empl_employee2.id, |
| 551 |
}, |
| 552 |
{ |
| 553 |
'name': '/', |
| 554 |
'task_id': subsubtask.id, |
| 555 |
'unit_amount': 1.0, |
| 556 |
'employee_id': self.empl_employee2.id, |
| 557 |
}, |
| 558 |
]) |
| 559 |
self.assertEqual(self.task1.subtask_effective_hours, 10) |
| 560 |
|
| 561 |
def test_ensure_product_uom_set_in_timesheet(self): |
| 562 |
self.assertFalse(self.project_customer.timesheet_ids, 'No timesheet should be recorded in this project') |
| 563 |
self.assertFalse(self.project_customer.total_timesheet_time, 'The total time recorded should be equal to 0 since no timesheet is recorded.') |
| 564 |
|
| 565 |
timesheet1, timesheet2 = self.env['account.analytic.line'].with_user(self.user_employee).create([ |
| 566 |
{'unit_amount': 1.0, 'project_id': self.project_customer.id}, |
| 567 |
{'unit_amount': 3.0, 'project_id': self.project_customer.id, 'product_uom_id': False}, |
| 568 |
]) |
| 569 |
self.assertEqual( |
| 570 |
timesheet1.product_uom_id, |
| 571 |
self.project_customer.account_id.company_id.timesheet_encode_uom_id, |
| 572 |
'The default UoM set on the timesheet should be the one set on the company of AA.' |
| 573 |
) |
| 574 |
self.assertEqual( |
| 575 |
timesheet2.product_uom_id, |
| 576 |
self.project_customer.account_id.company_id.timesheet_encode_uom_id, |
| 577 |
'Even if the product_uom_id field is empty in the vals, the product_uom_id should have a UoM by default,' |
| 578 |
' otherwise the `total_timesheet_time` in project should not included the timesheet.' |
| 579 |
) |
| 580 |
self.assertEqual(self.project_customer.timesheet_ids, timesheet1 + timesheet2) |
| 581 |
self.assertEqual( |
| 582 |
self.project_customer.total_timesheet_time, |
| 583 |
timesheet1.unit_amount + timesheet2.unit_amount, |
| 584 |
'The total timesheet time of this project should be equal to 4.' |
| 585 |
) |
| 586 |
def test_create_timesheet_with_archived_employee(self): |
| 587 |
''' the timesheet can be created or edited only with an active employee |
| 588 |
''' |
| 589 |
self.empl_employee2.active = False |
| 590 |
batch_vals = { |
| 591 |
'project_id': self.project_customer.id, |
| 592 |
'task_id': self.task1.id, |
| 593 |
'name': 'archived employee timesheet', |
| 594 |
'unit_amount': 3, |
| 595 |
'employee_id': self.empl_employee2.id |
| 596 |
} |
| 597 |
|
| 598 |
self.assertRaises(UserError, self.env['account.analytic.line'].create, batch_vals) |
| 599 |
|
| 600 |
batch_vals["employee_id"] = self.empl_employee.id |
| 601 |
timesheet = self.env['account.analytic.line'].create(batch_vals) |
| 602 |
|
| 603 |
with self.assertRaises(UserError): |
| 604 |
timesheet.employee_id = self.empl_employee2 |
| 605 |
|
| 606 |
def test_create_timesheet_with_companyless_analytic_account(self): |
| 607 |
""" This test ensures that a timesheet can be created on an analytic account whose company_id is set to False""" |
| 608 |
self.project_customer.account_id.company_id = False |
| 609 |
timesheet_with_project = self.env['account.analytic.line'].with_user(self.user_employee).create( |
| 610 |
{'unit_amount': 1.0, 'project_id': self.project_customer.id}) |
| 611 |
self.assertEqual(timesheet_with_project.product_uom_id, self.project_customer.company_id.project_time_mode_id, |
| 612 |
"The product_uom_id of the timesheet should be equal to the project's company uom " |
| 613 |
"if the project's analytic account has no company_id and no task_id is defined in the vals") |
| 614 |
timesheet_with_task = self.env['account.analytic.line'].with_user(self.user_employee).create({ |
| 615 |
'unit_amount': 1.0, 'task_id': self.task1.id |
| 616 |
}) |
| 617 |
self.assertEqual(timesheet_with_task.product_uom_id, self.task1.company_id.project_time_mode_id, |
| 618 |
"The product_uom_id of the timesheet should be equal to the task's company uom " |
| 619 |
"if the project's analytic account has no company_id") |
| 620 |
# Remove the company also on the project to be sure we find a UoM |
| 621 |
self.project_customer.company_id = False |
| 622 |
timesheet_with_project.with_user(self.user_employee).write( |
| 623 |
{'unit_amount': 2.0, 'project_id': self.project_customer.id}) |
| 624 |
self.assertEqual(timesheet_with_project.product_uom_id, self.env.company.project_time_mode_id, |
| 625 |
"The product_uom_id of the timesheet should be equal to the company uom " |
| 626 |
"if the project's analytic account and the project have no company_id") |
| 627 |
|
| 628 |
|
| 629 |
def test_create_timesheet_with_default_employee_in_context(self): |
| 630 |
timesheet = self.env['account.analytic.line'].with_context(default_employee_id=self.empl_employee.id).create({ |
| 631 |
'project_id': self.project_customer.id, |
| 632 |
'task_id': self.task1.id, |
| 633 |
'name': 'Timesheet with default employee in context', |
| 634 |
'unit_amount': 3, |
| 635 |
}) |
| 636 |
self.assertEqual(timesheet.employee_id, self.empl_employee) |
| 637 |
|
| 638 |
def test_uom_change_timesheet(self): |
| 639 |
""" |
| 640 |
We check that we don't over transform the timesheet unit amount when changing |
| 641 |
the company encoding timesheet uom, we keep it in the project as hours. |
| 642 |
So it will be transformed only once when encoding the timesheet. |
| 643 |
""" |
| 644 |
Timesheet = self.env['account.analytic.line'] |
| 645 |
project = self.env['project.project'].create({ |
| 646 |
'name': 'Project', |
| 647 |
'allow_timesheets': True, |
| 648 |
'partner_id': self.partner.id, |
| 649 |
}) |
| 650 |
project.allocated_hours = 40.0 |
| 651 |
|
| 652 |
Timesheet.create({ |
| 653 |
'name': 'FirstTimeSheet', |
| 654 |
'project_id': project.id, |
| 655 |
'unit_amount': 8, |
| 656 |
'employee_id': self.empl_employee2.id |
| 657 |
}) |
| 658 |
project_update_hrs = self.env['project.update'].create({ |
| 659 |
'name': 'Project update 1', |
| 660 |
'project_id': project.id, |
| 661 |
'status': 'on_track', |
| 662 |
}) |
| 663 |
self.assertEqual(project_update_hrs.timesheet_time, 8, "Timesheet time should be 8 hours for new project update") |
| 664 |
# Clear cached computed project values before the UoM change |
| 665 |
self.env['project.project'].invalidate_model() |
| 666 |
self.env.company.timesheet_encode_uom_id = self.env.ref('uom.product_uom_day') |
| 667 |
self.assertEqual(project.total_timesheet_time, 1, "Total timesheet time should be 1 day") |
| 668 |
project.allocated_hours = 0.0 |
| 669 |
panel_data = project.get_panel_data() |
| 670 |
self.assertEqual(panel_data['buttons'][1]['number'], "1 Days", "Should display '1 Days'") |
| 671 |
self.assertEqual(project.timesheet_encode_uom_id, self.env.company.timesheet_encode_uom_id, "Timesheet encode uom should be the one from the company of the env, since the project has no company.") |
| 672 |
project_update_days = self.env['project.update'].create({ |
| 673 |
'name': 'Project update 2', |
| 674 |
'project_id': project.id, |
| 675 |
'status': 'on_track', |
| 676 |
}) |
| 677 |
self.assertEqual(project_update_days.timesheet_time, 1, "Timesheet time should be 1 day for new project update") |
| 678 |
|
| 679 |
def test_unlink_task_with_timesheet(self): |
| 680 |
self.env['account.analytic.line'].create({ |
| 681 |
'project_id': self.project_customer.id, |
| 682 |
'task_id': self.task1.id, |
| 683 |
'name': 'timesheet', |
| 684 |
'unit_amount': 4, |
| 685 |
'employee_id': self.empl_employee.id, |
| 686 |
}) |
| 687 |
self.task2.unlink() |
| 688 |
with self.assertRaises(RedirectWarning): |
| 689 |
self.task1.unlink() |
| 690 |
|
| 691 |
def test_cannot_convert_task_with_timesheets_in_private_task(self): |
| 692 |
self.env['account.analytic.line'].create({ |
| 693 |
'name': '/', |
| 694 |
'unit_amount': 1, |
| 695 |
'project_id': self.project_customer.id, |
| 696 |
'task_id': self.task1.id, |
| 697 |
'employee_id': self.empl_employee.id, |
| 698 |
}) |
| 699 |
with self.assertRaises(UserError): |
| 700 |
self.task1.project_id = False |
| 701 |
|
| 702 |
self.task1.parent_id = self.task2 |
| 703 |
self.task1.project_id = self.project_customer |
| 704 |
|
| 705 |
with self.assertRaises(UserError): |
| 706 |
self.task1.project_id = False |
| 707 |
|
| 708 |
def test_percentage_of_allocated_hours(self): |
| 709 |
""" Test the percentage of allocated hours on a task. """ |
| 710 |
self.task1.allocated_hours = 11/60 |
| 711 |
self.assertEqual(self.task1.effective_hours, 0, 'No timesheet should be created yet.') |
| 712 |
self.assertEqual(self.task1.progress, 0, 'No timesheet should be created yet.') |
| 713 |
self.env['account.analytic.line'].create([ |
| 714 |
{ |
| 715 |
'name': 'Timesheet', |
| 716 |
'project_id': self.project_customer.id, |
| 717 |
'task_id': self.task1.id, |
| 718 |
'unit_amount': 3/60, |
| 719 |
'employee_id': self.empl_employee.id, |
| 720 |
}, { |
| 721 |
'name': 'Timesheet', |
| 722 |
'project_id': self.project_customer.id, |
| 723 |
'task_id': self.task1.id, |
| 724 |
'unit_amount': 4/60, |
| 725 |
'employee_id': self.empl_employee.id, |
| 726 |
}, { |
| 727 |
'name': 'Timesheet', |
| 728 |
'project_id': self.project_customer.id, |
| 729 |
'task_id': self.task1.id, |
| 730 |
'unit_amount': 4/60, |
| 731 |
'employee_id': self.empl_employee.id, |
| 732 |
}, |
| 733 |
]) |
| 734 |
self.assertEqual(self.task1.progress, 1, 'The progress of allocated hours should be 1.') |
| 735 |
|
| 736 |
def test_timesheet_update_user_on_employee(self): |
| 737 |
timesheet = self.env['account.analytic.line'].create({ |
| 738 |
'project_id': self.project_customer.id, |
| 739 |
'task_id': self.task1.id, |
| 740 |
'name': 'my first timesheet', |
| 741 |
'employee_id': self.empl_employee.id, |
| 742 |
}) |
| 743 |
self.assertEqual(timesheet.user_id, self.empl_employee.user_id) |
| 744 |
new_user = self.env['res.users'].create({ |
| 745 |
'name': 'Test user', |
| 746 |
'login': 'test', |
| 747 |
}) |
| 748 |
self.empl_employee.user_id = new_user |
| 749 |
self.assertEqual(timesheet.user_id, new_user) |
| 750 |
|
| 751 |
def test_analytic_plan_timesheet_creation(self): |
| 752 |
child_analytic_plan = self.env['account.analytic.plan'].create({ |
| 753 |
'name': 'Child Analytic Plan', |
| 754 |
'parent_id': self.analytic_plan.id, |
| 755 |
}) |
| 756 |
child_analytic_account = self.env['account.analytic.account'].create({ |
| 757 |
'name': 'Analytic Account for Child Analytic Plan', |
| 758 |
'partner_id': self.partner.id, |
| 759 |
'plan_id': child_analytic_plan.id, |
| 760 |
'code': 'TEST', |
| 761 |
}) |
| 762 |
plan_name = f'{self.analytic_plan._column_name()}' |
| 763 |
self.project_customer[plan_name] = child_analytic_account |
| 764 |
timesheet = self.env['account.analytic.line'].create({ |
| 765 |
'name': 'Timesheet', |
| 766 |
'unit_amount': 1, |
| 767 |
'project_id': self.project_customer.id, |
| 768 |
'employee_id': self.empl_employee.id, |
| 769 |
}) |
| 770 |
self.assertEqual(self.project_customer.account_id, timesheet.account_id) |
| 771 |
self.assertEqual(self.project_customer[plan_name], timesheet[plan_name]) |
| 772 |
|
| 773 |
def test_analytic_plan_timesheet_change_project(self): |
| 774 |
timesheet = self.env['account.analytic.line'].create({ |
| 775 |
'name': 'Timesheet', |
| 776 |
'unit_amount': 1, |
| 777 |
'project_id': self.project_customer.id, |
| 778 |
'employee_id': self.empl_employee.id, |
| 779 |
}) |
| 780 |
analytic_plan = self.env['account.analytic.plan'].create({ |
| 781 |
'name': 'Analytic Plan' |
| 782 |
}) |
| 783 |
analytic_account = self.env['account.analytic.account'].create({ |
| 784 |
'name': 'Analytic Account', |
| 785 |
'partner_id': self.partner.id, |
| 786 |
'plan_id': analytic_plan.id, |
| 787 |
'code': 'TEST', |
| 788 |
}) |
| 789 |
plan_name = f'{analytic_plan._column_name()}' |
| 790 |
other_project = self.env['project.project'].create({ |
| 791 |
'name': 'Other Project', |
| 792 |
'allow_timesheets': True, |
| 793 |
plan_name: analytic_account.id, |
| 794 |
}) |
| 795 |
timesheet.project_id = other_project |
| 796 |
self.assertEqual(other_project.account_id, timesheet.account_id) |
| 797 |
self.assertEqual(other_project[plan_name], timesheet[plan_name]) |
| 798 |
|
| 799 |
def test_mandatory_plan_timesheet_applicability(self): |
| 800 |
self.env['account.analytic.applicability'].create({ |
| 801 |
'business_domain': 'timesheet', |
| 802 |
'applicability': 'mandatory', |
| 803 |
'analytic_plan_id': self.analytic_plan.id, |
| 804 |
}) |
| 805 |
with self.assertRaises(ValidationError): |
| 806 |
# The analytic plan 'self.analytic_plan' is mandatory on the project linked to the timesheet |
| 807 |
self.env['account.analytic.line'].create({ |
| 808 |
'name': 'Timesheet', |
| 809 |
'unit_amount': 1, |
| 810 |
'project_id': self.project_customer.id, |
| 811 |
'employee_id': self.empl_employee.id, |
| 812 |
}) |
| 813 |
|
| 814 |
def test_timesheet_unactive_analytic_account(self): |
| 815 |
self.analytic_account.active = False |
| 816 |
with self.assertRaises(ValidationError): |
| 817 |
# The account_id given to the timesheet must be active |
| 818 |
self.env['account.analytic.line'].create({ |
| 819 |
'name': 'Timesheet', |
| 820 |
'unit_amount': 1, |
| 821 |
'project_id': self.project_customer.id, |
| 822 |
'employee_id': self.empl_employee.id, |
| 823 |
}) |
| 824 |
|
| 825 |
def test_project_update_reflects_task_allocated_hours_and_timesheet(self): |
| 826 |
""" |
| 827 |
Check if the project update is according to the project's allocated hours and timesheet. |
| 828 |
Step: |
| 829 |
1) set 10 allocated hours in the project_customer |
| 830 |
2) add timesheet 2 hour in task1 |
| 831 |
3) create project update and verfity |
| 832 |
4) repeat step 1 but allocated hour 12 |
| 833 |
5) add timesheet 3 hour in task2 |
| 834 |
6) repeat step 4 |
| 835 |
8) add timesheet 10 hour in task2 |
| 836 |
9) repeat step 4 |
| 837 |
""" |
| 838 |
def create_timesheet(task, hours): |
| 839 |
self.env['account.analytic.line'].create({ |
| 840 |
'name': 'Timesheet', |
| 841 |
'task_id': task.id, |
| 842 |
'unit_amount': hours, |
| 843 |
'employee_id': self.empl_employee.id, |
| 844 |
}) |
| 845 |
|
| 846 |
def create_project_update(): |
| 847 |
update_form = Form(self.env['project.update'].with_context({'default_project_id': self.project_customer.id})) |
| 848 |
update_form.name = "Test" |
| 849 |
update_project = update_form.save() |
| 850 |
|
| 851 |
return [update_project.allocated_time, update_project.timesheet_time, update_project.timesheet_percentage] |
| 852 |
|
| 853 |
self.project_customer.allocated_hours = 10 |
| 854 |
create_timesheet(self.task1, 2) |
| 855 |
project_update_vals_list = create_project_update() |
| 856 |
self.assertListEqual(project_update_vals_list, [10, 2, 20]) |
| 857 |
|
| 858 |
self.project_customer.allocated_hours = 12 |
| 859 |
create_timesheet(self.task2, 3) |
| 860 |
project_update_vals_list = create_project_update() |
| 861 |
self.assertListEqual(project_update_vals_list, [12, 5, 42]) |
| 862 |
|
| 863 |
create_timesheet(self.task2, 10) |
| 864 |
project_update_vals_list = create_project_update() |
| 865 |
self.assertListEqual(project_update_vals_list, [12, 15, 125]) |
| 866 |
|
| 867 |
def test_calendar_display_name(self): |
| 868 |
timesheet = self.env['account.analytic.line'].create({ |
| 869 |
'name': 'Timesheet', |
| 870 |
'unit_amount': 1, |
| 871 |
'project_id': self.project_customer.id, |
| 872 |
'employee_id': self.empl_employee.id, |
| 873 |
}) |
| 874 |
self.assertEqual(timesheet.calendar_display_name, f"{self.project_customer.name} (1h)") |
| 875 |
timesheet.unit_amount = 1.5 |
| 876 |
timesheet.invalidate_recordset(['calendar_display_name']) |
| 877 |
self.assertEqual(timesheet.calendar_display_name, f"{self.project_customer.name} (1h30)") |
| 878 |
timesheet.unit_amount = 8 |
| 879 |
timesheet.company_id.timesheet_encode_uom_id = self.env.ref('uom.product_uom_day') |
| 880 |
timesheet.invalidate_recordset(['calendar_display_name']) |
| 881 |
self.assertEqual(timesheet.calendar_display_name, f"{self.project_customer.name} (1d)") |
| 882 |
timesheet.unit_amount = 12 |
| 883 |
timesheet.invalidate_recordset(['calendar_display_name']) |
| 884 |
self.assertEqual(timesheet.calendar_display_name, f"{self.project_customer.name} (1.5d)") |
| 885 |
|
| 886 |
def test_multi_create_timesheets_from_calendar(self): |
| 887 |
""" |
| 888 |
Simulate creating timesheets using the multi-create feature in the calendar view |
| 889 |
""" |
| 890 |
HrTimesheet = self.env['account.analytic.line'].with_context(timesheet_calendar=True) |
| 891 |
|
| 892 |
timesheet = HrTimesheet.create({ |
| 893 |
'project_id': self.project_customer.id, |
| 894 |
'unit_amount': 1, |
| 895 |
'employee_id': self.empl_employee.id, |
| 896 |
'date': '2025-05-26', |
| 897 |
}) |
| 898 |
self.assertTrue(timesheet, "The timesheet should be created without errors") |
| 899 |
self.assertEqual(timesheet.employee_id, self.empl_employee) |
| 900 |
self.assertEqual(fields.Date.to_string(timesheet.date), '2025-05-26') |
| 901 |
|
| 902 |
timesheet = HrTimesheet.create({ |
| 903 |
'project_id': self.project_customer.id, |
| 904 |
'unit_amount': 1, |
| 905 |
'employee_id': self.empl_employee.id, |
| 906 |
'date': '2025-05-25', # Sunday |
| 907 |
}) |
| 908 |
self.assertFalse(timesheet, "The timesheet should not get created on a weekend") |
| 909 |
|
| 910 |
calendar = self.env['resource.calendar'].with_company(self.env.company).create({ |
| 911 |
'name': "part time", |
| 912 |
'hours_per_day': 8.0, |
| 913 |
'attendance_ids': [ |
| 914 |
(0, 0, {'name': "Monday Morning", 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}), |
| 915 |
(0, 0, {'name': "Monday Lunch", 'dayofweek': '0', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}), |
| 916 |
(0, 0, {'name': "Monday Evening", 'dayofweek': '0', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}), |
| 917 |
(0, 0, {'name': "Thursday Morning", 'dayofweek': '3', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}), |
| 918 |
(0, 0, {'name': "Thursday Lunch", 'dayofweek': '3', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}), |
| 919 |
(0, 0, {'name': "Thursday Evening", 'dayofweek': '3', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}), |
| 920 |
(0, 0, {'name': "Friday Morning", 'dayofweek': '4', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}), |
| 921 |
(0, 0, {'name': "Friday Lunch", 'dayofweek': '4', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}), |
| 922 |
(0, 0, {'name': "Friday Evening", 'dayofweek': '4', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}), |
| 923 |
], |
| 924 |
}) |
| 925 |
|
| 926 |
self.empl_employee.resource_calendar_id = calendar |
| 927 |
self.empl_employee2.resource_calendar_id = calendar |
| 928 |
with patch.object(self.env.registry['bus.bus'], '_sendone') as mock_send: |
| 929 |
def create_timesheet_records(days): |
| 930 |
return HrTimesheet.create([{ |
| 931 |
'project_id': self.project_customer.id, |
| 932 |
'unit_amount': 1, |
| 933 |
'date': f'2025-05-{day}', |
| 934 |
'employee_id': employee.id, |
| 935 |
} for day in days for employee in (self.empl_employee, self.empl_employee2)]) |
| 936 |
|
| 937 |
def assert_notification(mock_send, notification_type, message): |
| 938 |
mock_send.assert_called_with(self.env.user.partner_id, 'simple_notification', { |
| 939 |
'type': notification_type, |
| 940 |
'message': message, |
| 941 |
}) |
| 942 |
mock_send.reset_mock() |
| 943 |
|
| 944 |
timesheets = create_timesheet_records(('25', '26', '27')) |
| 945 |
self.assertEqual(len(timesheets), 2) |
| 946 |
self.assertEqual(fields.Date.to_string(timesheets[0].date), '2025-05-26', "The timesheet creation should skip non-working days") |
| 947 |
self.assertEqual(fields.Date.to_string(timesheets[1].date), '2025-05-26', "The timesheet creation should skip non-working days") |
| 948 |
assert_notification(mock_send, 'danger', "Some timesheets were not created: employees aren’t working on the selected days") |
| 949 |
|
| 950 |
timesheets = create_timesheet_records(('18', '25')) |
| 951 |
self.assertEqual(len(timesheets), 0) |
| 952 |
assert_notification(mock_send, 'danger', "No timesheets created: employees aren’t working on the selected days") |
| 953 |
|
| 954 |
timesheets = create_timesheet_records(('8', '9')) |
| 955 |
self.assertEqual(len(timesheets), 4) |
| 956 |
assert_notification(mock_send, 'success', "Timesheets successfully created") |
| 957 |
|
| 958 |
def test_keep_create_account_values_at_timesheet_creation(self): |
| 959 |
field_name = self.analytic_plan._column_name() |
| 960 |
analytic_account, another_account = self.env['account.analytic.account'].create([ |
| 961 |
{ |
| 962 |
'name': 'Analytic Account', |
| 963 |
'partner_id': self.partner.id, |
| 964 |
'plan_id': self.analytic_plan.id, |
| 965 |
'code': 'TEST', |
| 966 |
}, |
| 967 |
{ |
| 968 |
'name': 'Another Analytic Account', |
| 969 |
'partner_id': self.partner.id, |
| 970 |
'plan_id': self.analytic_plan.id, |
| 971 |
'code': 'TEST2', |
| 972 |
}, |
| 973 |
]) |
| 974 |
|
| 975 |
self.project_customer.write({ |
| 976 |
field_name: another_account.id, |
| 977 |
}) |
| 978 |
|
| 979 |
line = self.env['account.analytic.line'].create({ |
| 980 |
'name': 'Timesheet', |
| 981 |
'unit_amount': 1, |
| 982 |
'project_id': self.project_customer.id, |
| 983 |
field_name: analytic_account.id, |
| 984 |
'employee_id': self.empl_employee.id, |
| 985 |
}) |
| 986 |
self.assertEqual(line[field_name].id, analytic_account.id, f"The value of {field_name} shouldn't get overwritten by the project's account") |
| 987 |
|
| 988 |
def test_keep_write_account_values_at_timesheet_update(self): |
| 989 |
field_name = self.analytic_plan._column_name() |
| 990 |
analytic_account, another_account = self.env['account.analytic.account'].create([ |
| 991 |
{ |
| 992 |
'name': 'Analytic Account', |
| 993 |
'partner_id': self.partner.id, |
| 994 |
'plan_id': self.analytic_plan.id, |
| 995 |
'code': 'TEST', |
| 996 |
}, |
| 997 |
{ |
| 998 |
'name': 'Another Analytic Account', |
| 999 |
'partner_id': self.partner.id, |
| 1000 |
'plan_id': self.analytic_plan.id, |
| 1001 |
'code': 'TEST2', |
| 1002 |
}, |
| 1003 |
]) |
| 1004 |
|
| 1005 |
self.project_customer.write({ |
| 1006 |
field_name: another_account.id, |
| 1007 |
}) |
| 1008 |
|
| 1009 |
line = self.env['account.analytic.line'].create({ |
| 1010 |
'name': 'Timesheet', |
| 1011 |
'unit_amount': 1, |
| 1012 |
'employee_id': self.empl_employee.id, |
| 1013 |
}) |
| 1014 |
|
| 1015 |
line.write({ |
| 1016 |
'project_id': self.project_customer.id, |
| 1017 |
field_name: analytic_account.id |
| 1018 |
}) |
| 1019 |
self.assertEqual(line[field_name].id, analytic_account.id, f"The value of {field_name} shouldn't get overwritten by the project's account") |
| 1020 |
|
| 1021 |
def test_split_analytic_dynamic_update(self): |
| 1022 |
self.empl_employee.hourly_cost = 10.0 |
| 1023 |
another_account = self.project.account_id.copy() |
| 1024 |
|
| 1025 |
line = self.env['account.analytic.line'].create({ |
| 1026 |
'name': 'Timesheet', |
| 1027 |
'unit_amount': 1, |
| 1028 |
'project_id': self.project_customer.id, |
| 1029 |
'account_id': self.project.account_id.id, |
| 1030 |
'employee_id': self.empl_employee.id, |
| 1031 |
}) |
| 1032 |
self.assertEqual(line.amount, -10) |
| 1033 |
line.analytic_distribution = { |
| 1034 |
f"{self.project.account_id.id}": 50, |
| 1035 |
f"{another_account.id}": 50, |
| 1036 |
} |
| 1037 |
self.assertEqual(line.amount, -5) # the line is split in 2 |
| 1038 |
|
| 1039 |
def test_log_timesheet_with_user_has_two_employees_from_different_companies(self): |
| 1040 |
company_2 = self.env['res.company'].create({'name': 'Company 2'}) |
| 1041 |
self.env['hr.employee'].with_company(company_2).create({ |
| 1042 |
'name': 'Employee 2', |
| 1043 |
'user_id': self.user_manager.id, |
| 1044 |
}) |
| 1045 |
timesheet = self.env['account.analytic.line'].create({ |
| 1046 |
'project_id': self.project.id, |
| 1047 |
'user_id': self.user_manager.id, |
| 1048 |
}) |
| 1049 |
self.assertEqual(timesheet.company_id, self.env.company) |