Initial commit: Hr packages

This commit is contained in:
Ernad Husremovic 2025-08-29 15:20:50 +02:00
commit 62531cd146
2820 changed files with 1432848 additions and 0 deletions

View file

@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import hr_timesheet
from . import ir_http
from . import ir_ui_menu
from . import res_company
from . import res_config_settings
from . import project
from . import project_collaborator
from . import uom

View file

@ -0,0 +1,395 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from collections import defaultdict
from lxml import etree
import re
from odoo import api, Command, fields, models, _, _lt
from odoo.exceptions import UserError, AccessError, ValidationError
from odoo.osv import expression
class AccountAnalyticLine(models.Model):
_inherit = 'account.analytic.line'
def _get_favorite_project_id_domain(self, employee_id=False):
employee_id = employee_id or self.env.user.employee_id.id
return [
('employee_id', '=', employee_id),
('project_id', '!=', False),
]
@api.model
def _get_favorite_project_id(self, employee_id=False):
last_timesheet_ids = self.search(self._get_favorite_project_id_domain(employee_id), limit=5)
if len(last_timesheet_ids.project_id) == 1:
return last_timesheet_ids.project_id.id
return False
@api.model
def default_get(self, field_list):
result = super(AccountAnalyticLine, self).default_get(field_list)
if not self.env.context.get('default_employee_id') and 'employee_id' in field_list and result.get('user_id'):
result['employee_id'] = self.env['hr.employee'].search([('user_id', '=', result['user_id']), ('company_id', '=', result.get('company_id', self.env.company.id))], limit=1).id
if not self._context.get('default_project_id') and self._context.get('is_timesheet'):
employee_id = result.get('employee_id', self.env.context.get('default_employee_id', False))
favorite_project_id = self._get_favorite_project_id(employee_id)
if favorite_project_id:
result['project_id'] = favorite_project_id
return result
def _domain_project_id(self):
domain = [('allow_timesheets', '=', True)]
if not self.user_has_groups('hr_timesheet.group_timesheet_manager'):
return expression.AND([domain,
['|', ('privacy_visibility', '!=', 'followers'), ('message_partner_ids', 'in', [self.env.user.partner_id.id])]
])
return domain
def _domain_employee_id(self):
if not self.user_has_groups('hr_timesheet.group_hr_timesheet_approver'):
return [('user_id', '=', self.env.user.id)]
return []
task_id = fields.Many2one(
'project.task', 'Task', index='btree_not_null',
compute='_compute_task_id', store=True, readonly=False,
domain="[('allow_timesheets', '=', True), ('project_id', '=?', project_id)]")
ancestor_task_id = fields.Many2one('project.task', related='task_id.ancestor_id', store=True, index='btree_not_null')
project_id = fields.Many2one(
'project.project', 'Project', domain=_domain_project_id, index=True,
compute='_compute_project_id', store=True, readonly=False)
user_id = fields.Many2one(compute='_compute_user_id', store=True, readonly=False)
employee_id = fields.Many2one('hr.employee', "Employee", domain=_domain_employee_id, context={'active_test': False},
help="Define an 'hourly cost' on the employee to track the cost of their time.")
job_title = fields.Char(related='employee_id.job_title')
department_id = fields.Many2one('hr.department', "Department", compute='_compute_department_id', store=True, compute_sudo=True)
manager_id = fields.Many2one('hr.employee', "Manager", related='employee_id.parent_id', store=True)
encoding_uom_id = fields.Many2one('uom.uom', compute='_compute_encoding_uom_id')
partner_id = fields.Many2one(compute='_compute_partner_id', store=True, readonly=False)
def name_get(self):
result = super().name_get()
timesheets_read = self.env[self._name].search_read([('project_id', '!=', False), ('id', 'in', self.ids)], ['id', 'project_id', 'task_id'])
if not timesheets_read:
return result
def _get_display_name(project_id, task_id):
""" Get the display name of the timesheet based on the project and task
:param project_id: tuple containing the id and the display name of the project
:param task_id: tuple containing the id and the display name of the task if a task exists in the timesheet
otherwise False.
:returns: the display name of the timesheet
"""
if task_id:
return '%s - %s' % (project_id[1], task_id[1])
return project_id[1]
timesheet_dict = {res['id']: _get_display_name(res['project_id'], res['task_id']) for res in timesheets_read}
return list({**dict(result), **timesheet_dict}.items())
def _compute_encoding_uom_id(self):
for analytic_line in self:
analytic_line.encoding_uom_id = analytic_line.company_id.timesheet_encode_uom_id
@api.depends('task_id.partner_id', 'project_id.partner_id')
def _compute_partner_id(self):
for timesheet in self:
if timesheet.project_id:
timesheet.partner_id = timesheet.task_id.partner_id or timesheet.project_id.partner_id
@api.depends('task_id', 'task_id.project_id')
def _compute_project_id(self):
for line in self:
if not line.task_id.project_id or line.project_id == line.task_id.project_id:
continue
line.project_id = line.task_id.project_id
@api.depends('project_id')
def _compute_task_id(self):
for line in self.filtered(lambda line: not line.project_id):
line.task_id = False
@api.onchange('project_id')
def _onchange_project_id(self):
# TODO KBA in master - check to do it "properly", currently:
# This onchange is used to reset the task_id when the project changes.
# Doing it in the compute will remove the task_id when the project of a task changes.
if self.project_id != self.task_id.project_id:
self.task_id = False
@api.depends('employee_id')
def _compute_user_id(self):
for line in self:
line.user_id = line.employee_id.user_id if line.employee_id else self._default_user()
@api.depends('employee_id')
def _compute_department_id(self):
for line in self:
line.department_id = line.employee_id.department_id
@api.model_create_multi
def create(self, vals_list):
# Before creating a timesheet, we need to put a valid employee_id in the vals
default_user_id = self._default_user()
user_ids = []
employee_ids = []
# 1/ Collect the user_ids and employee_ids from each timesheet vals
for vals in vals_list:
vals.update(self._timesheet_preprocess(vals))
if not vals.get('project_id'):
continue
if not vals.get('name'):
vals['name'] = '/'
employee_id = vals.get('employee_id')
user_id = vals.get('user_id', default_user_id)
if employee_id and employee_id not in employee_ids:
employee_ids.append(employee_id)
elif user_id not in user_ids:
user_ids.append(user_id)
# 2/ Search all employees related to user_ids and employee_ids, in the selected companies
employees = self.env['hr.employee'].sudo().search([
'&', '|', ('user_id', 'in', user_ids), ('id', 'in', employee_ids), ('company_id', 'in', self.env.companies.ids)
])
# ┌───── in search results = active/in companies ────────> was found with... ─── employee_id ───> (A) There is nothing to do, we will use this employee_id
# 3/ Each employee └──── user_id ──────> (B)** We'll need to select the right employee for this user
# └─ not in search results = archived/not in companies ──> (C) We raise an error as we can't create a timesheet for an archived employee
# ** We can rely on the user to get the employee_id if
# he has an active employee in the company of the timesheet
# or he has only one active employee for all selected companies
valid_employee_per_id = {}
employee_id_per_company_per_user = defaultdict(dict)
for employee in employees:
if employee.id in employee_ids:
valid_employee_per_id[employee.id] = employee
else:
employee_id_per_company_per_user[employee.user_id.id][employee.company_id.id] = employee.id
# 4/ Put valid employee_id in each vals
error_msg = _lt('Timesheets must be created with an active employee in the selected companies.')
for vals in vals_list:
if not vals.get('project_id'):
continue
employee_in_id = vals.get('employee_id')
if employee_in_id:
if employee_in_id in valid_employee_per_id:
vals['user_id'] = valid_employee_per_id[employee_in_id].sudo().user_id.id # (A) OK
continue
else:
raise ValidationError(error_msg) # (C) KO
else:
user_id = vals.get('user_id', default_user_id) # (B)...
# ...Look for an employee, with ** conditions
employee_per_company = employee_id_per_company_per_user.get(user_id)
employee_out_id = False
if employee_per_company:
company_id = list(employee_per_company)[0] if len(employee_per_company) == 1\
else vals.get('company_id', self.env.company.id)
employee_out_id = employee_per_company.get(company_id, False)
if employee_out_id:
vals['employee_id'] = employee_out_id
vals['user_id'] = user_id
else: # ...and raise an error if they fail
raise ValidationError(error_msg)
# 5/ Finally, create the timesheets
lines = super(AccountAnalyticLine, self).create(vals_list)
for line, values in zip(lines, vals_list):
if line.project_id: # applied only for timesheet
line._timesheet_postprocess(values)
return lines
def write(self, values):
# If it's a basic user then check if the timesheet is his own.
if not (self.user_has_groups('hr_timesheet.group_hr_timesheet_approver') or self.env.su) and any(self.env.user.id != analytic_line.user_id.id for analytic_line in self):
raise AccessError(_("You cannot access timesheets that are not yours."))
values = self._timesheet_preprocess(values)
if values.get('employee_id'):
employee = self.env['hr.employee'].browse(values['employee_id'])
if not employee.active:
raise UserError(_('You cannot set an archived employee to the existing timesheets.'))
if 'name' in values and not values.get('name'):
values['name'] = '/'
result = super(AccountAnalyticLine, self).write(values)
# applied only for timesheet
self.filtered(lambda t: t.project_id)._timesheet_postprocess(values)
return result
@api.model
def _get_view_cache_key(self, view_id=None, view_type='form', **options):
"""The override of _get_view changing the time field labels according to the company timesheet encoding UOM
makes the view cache dependent on the company timesheet encoding uom"""
key = super()._get_view_cache_key(view_id, view_type, **options)
return key + (self.env.company.timesheet_encode_uom_id,)
@api.model
def _get_view(self, view_id=None, view_type='form', **options):
""" Set the correct label for `unit_amount`, depending on company UoM """
arch, view = super()._get_view(view_id, view_type, **options)
arch = self.sudo()._apply_timesheet_label(arch, view_type=view_type)
return arch, view
@api.model
def _apply_timesheet_label(self, view_node, view_type='form'):
doc = view_node
encoding_uom = self.env.company.timesheet_encode_uom_id
# Here, we select only the unit_amount field having no string set to give priority to
# custom inheretied view stored in database. Even if normally, no xpath can be done on
# 'string' attribute.
for node in doc.xpath("//field[@name='unit_amount'][@widget='timesheet_uom'][not(@string)]"):
node.set('string', _('%s Spent') % (re.sub(r'[\(\)]', '', encoding_uom.name or '')))
return doc
@api.model
def _apply_time_label(self, view_node, related_model):
doc = view_node
Model = self.env[related_model]
# Just fetch the name of the uom in `timesheet_encode_uom_id` of the current company
encoding_uom_name = self.env.company.timesheet_encode_uom_id.with_context(prefetch_fields=False).sudo().name
for node in doc.xpath("//field[@widget='timesheet_uom'][not(@string)] | //field[@widget='timesheet_uom_no_toggle'][not(@string)]"):
name_with_uom = re.sub(_('Hours') + "|Hours", encoding_uom_name or '', Model._fields[node.get('name')]._description_string(self.env), flags=re.IGNORECASE)
node.set('string', name_with_uom)
return doc
def _timesheet_get_portal_domain(self):
if self.env.user.has_group('hr_timesheet.group_hr_timesheet_user'):
# Then, he is internal user, and we take the domain for this current user
return self.env['ir.rule']._compute_domain(self._name)
return [
'|',
'&',
'|',
('task_id.project_id.message_partner_ids', 'child_of', [self.env.user.partner_id.commercial_partner_id.id]),
('task_id.message_partner_ids', 'child_of', [self.env.user.partner_id.commercial_partner_id.id]),
('task_id.project_id.privacy_visibility', '=', 'portal'),
'&',
('task_id', '=', False),
'&',
('project_id.message_partner_ids', 'child_of', [self.env.user.partner_id.commercial_partner_id.id]),
('project_id.privacy_visibility', '=', 'portal')
]
def _timesheet_preprocess(self, vals):
""" Deduce other field values from the one given.
Overrride this to compute on the fly some field that can not be computed fields.
:param values: dict values for `create`or `write`.
"""
project = self.env['project.project'].browse(vals.get('project_id', False))
task = self.env['project.task'].browse(vals.get('task_id', False))
# task implies project
if task and not project:
project = task.project_id
if not project:
raise ValidationError(_('You cannot create a timesheet on a private task.'))
vals['project_id'] = project.id
# task implies analytic account and tags
if task and not vals.get('account_id'):
task_analytic_account_id = task._get_task_analytic_account_id()
vals['account_id'] = task_analytic_account_id.id
vals['company_id'] = task_analytic_account_id.company_id.id or task.company_id.id
if not task_analytic_account_id.active:
raise UserError(_('You cannot add timesheets to a project or a task linked to an inactive analytic account.'))
# project implies analytic account
elif project and not vals.get('account_id'):
vals['account_id'] = project.analytic_account_id.id
vals['company_id'] = project.analytic_account_id.company_id.id or project.company_id.id
if not project.analytic_account_id.active:
raise UserError(_('You cannot add timesheets to a project linked to an inactive analytic account.'))
# force customer partner, from the task or the project
if project and not vals.get('partner_id'):
partner_id = task.partner_id.id if task else project.partner_id.id
if partner_id:
vals['partner_id'] = partner_id
# set timesheet UoM from the AA company (AA implies uom)
if not vals.get('product_uom_id') and all(v in vals for v in ['account_id', 'project_id']): # project_id required to check this is timesheet flow
analytic_account = self.env['account.analytic.account'].sudo().browse(vals['account_id'])
uom_id = analytic_account.company_id.project_time_mode_id.id
if not uom_id:
company_id = vals.get('company_id', False)
if not company_id:
project = self.env['project.project'].browse(vals.get('project_id'))
company_id = project.analytic_account_id.company_id.id or project.company_id.id
uom_id = self.env['res.company'].browse(company_id).project_time_mode_id.id
vals['product_uom_id'] = uom_id
return vals
def _timesheet_postprocess(self, values):
""" Hook to update record one by one according to the values of a `write` or a `create`. """
sudo_self = self.sudo() # this creates only one env for all operation that required sudo() in `_timesheet_postprocess_values`override
values_to_write = self._timesheet_postprocess_values(values)
for timesheet in sudo_self:
if values_to_write[timesheet.id]:
timesheet.write(values_to_write[timesheet.id])
return values
def _timesheet_postprocess_values(self, values):
""" Get the addionnal values to write on record
:param dict values: values for the model's fields, as a dictionary::
{'field_name': field_value, ...}
:return: a dictionary mapping each record id to its corresponding
dictionary values to write (may be empty).
"""
result = {id_: {} for id_ in self.ids}
sudo_self = self.sudo() # this creates only one env for all operation that required sudo()
# (re)compute the amount (depending on unit_amount, employee_id for the cost, and account_id for currency)
if any(field_name in values for field_name in ['unit_amount', 'employee_id', 'account_id']):
for timesheet in sudo_self:
cost = timesheet._hourly_cost()
amount = -timesheet.unit_amount * cost
amount_converted = timesheet.employee_id.currency_id._convert(
amount, timesheet.account_id.currency_id or timesheet.currency_id, self.env.company, timesheet.date)
result[timesheet.id].update({
'amount': amount_converted,
})
return result
def _is_timesheet_encode_uom_day(self):
company_uom = self.env.company.timesheet_encode_uom_id
return company_uom == self.env.ref('uom.product_uom_day')
def _is_updatable_timesheet(self):
return True
@api.model
def _convert_hours_to_days(self, time):
uom_hour = self.env.ref('uom.product_uom_hour')
uom_day = self.env.ref('uom.product_uom_day')
return round(uom_hour._compute_quantity(time, uom_day, raise_if_failure=False), 2)
def _get_timesheet_time_day(self):
return self._convert_hours_to_days(self.unit_amount)
def _hourly_cost(self):
self.ensure_one()
return self.employee_id.hourly_cost or 0.0
def _get_report_base_filename(self):
task_ids = self.task_id
if len(task_ids) == 1:
return _('Timesheets - %s', task_ids.name)
return _('Timesheets')
def _default_user(self):
return self.env.context.get('user_id', self.env.user.id)
@api.model
def _ensure_uom_hours(self):
uom_hours = self.env.ref('uom.product_uom_hour', raise_if_not_found=False)
if not uom_hours:
uom_hours = self.env['uom.uom'].create({
'name': "Hours",
'category_id': self.env.ref('uom.uom_categ_wtime').id,
'factor': 8,
'uom_type': "smaller",
})
self.env['ir.model.data'].create({
'name': 'product_uom_hour',
'model': 'uom.uom',
'module': 'uom',
'res_id': uom_hours.id,
'noupdate': True,
})

View file

@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, models
class Http(models.AbstractModel):
_inherit = 'ir.http'
def session_info(self):
""" The widget 'timesheet_uom' needs to know which UoM conversion factor and which javascript
widget to apply, depending on the current company.
"""
result = super(Http, self).session_info()
if self.env.user._is_internal():
company_ids = self.env.user.company_ids
for company in company_ids:
result["user_companies"]["allowed_companies"][company.id].update({
"timesheet_uom_id": company.timesheet_encode_uom_id.id,
"timesheet_uom_factor": company.project_time_mode_id._compute_quantity(
1.0,
company.timesheet_encode_uom_id,
round=False
),
})
result["uom_ids"] = self.get_timesheet_uoms()
return result
@api.model
def get_timesheet_uoms(self):
company_ids = self.env.user.company_ids
uom_ids = company_ids.mapped('timesheet_encode_uom_id') | \
company_ids.mapped('project_time_mode_id')
return {
uom.id:
{
'id': uom.id,
'name': uom.name,
'rounding': uom.rounding,
'timesheet_widget': uom.timesheet_widget,
} for uom in uom_ids
}

View file

@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models
class IrUiMenu(models.Model):
_inherit = 'ir.ui.menu'
def _load_menus_blacklist(self):
res = super()._load_menus_blacklist()
time_menu = self.env.ref('hr_timesheet.timesheet_menu_activity_user', raise_if_not_found=False)
if time_menu and self.env.user.has_group('hr_timesheet.group_hr_timesheet_approver'):
res.append(time_menu.id)
return res

View file

@ -0,0 +1,454 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from collections import defaultdict
from odoo import models, fields, api, _
from odoo.tools.float_utils import float_compare
from odoo.exceptions import UserError, ValidationError, RedirectWarning
from odoo.addons.rating.models.rating_data import OPERATOR_MAPPING
PROJECT_TASK_READABLE_FIELDS = {
'allow_subtasks',
'allow_timesheets',
'analytic_account_active',
'effective_hours',
'encode_uom_in_days',
'planned_hours',
'progress',
'overtime',
'remaining_hours',
'subtask_effective_hours',
'subtask_planned_hours',
'timesheet_ids',
'total_hours_spent',
}
class Project(models.Model):
_inherit = "project.project"
allow_timesheets = fields.Boolean(
"Timesheets", compute='_compute_allow_timesheets', store=True, readonly=False,
default=True)
analytic_account_id = fields.Many2one(
# note: replaces ['|', ('company_id', '=', False), ('company_id', '=', company_id)]
domain="""[
'|', ('company_id', '=', False), ('company_id', '=', company_id),
('partner_id', '=?', partner_id),
]"""
)
timesheet_ids = fields.One2many('account.analytic.line', 'project_id', 'Associated Timesheets')
timesheet_count = fields.Integer(compute="_compute_timesheet_count", groups='hr_timesheet.group_hr_timesheet_user')
timesheet_encode_uom_id = fields.Many2one('uom.uom', related='company_id.timesheet_encode_uom_id')
total_timesheet_time = fields.Integer(
compute='_compute_total_timesheet_time', groups='hr_timesheet.group_hr_timesheet_user',
help="Total number of time (in the proper UoM) recorded in the project, rounded to the unit.")
encode_uom_in_days = fields.Boolean(compute='_compute_encode_uom_in_days')
is_internal_project = fields.Boolean(compute='_compute_is_internal_project', search='_search_is_internal_project')
remaining_hours = fields.Float(compute='_compute_remaining_hours', string='Remaining Invoiced Time', compute_sudo=True)
is_project_overtime = fields.Boolean('Project in Overtime', compute='_compute_remaining_hours', search='_search_is_project_overtime', compute_sudo=True)
allocated_hours = fields.Float(string='Allocated Hours')
def _compute_encode_uom_in_days(self):
self.encode_uom_in_days = self.env.company.timesheet_encode_uom_id == self.env.ref('uom.product_uom_day')
@api.depends('analytic_account_id')
def _compute_allow_timesheets(self):
without_account = self.filtered(lambda t: not t.analytic_account_id and t._origin)
without_account.update({'allow_timesheets': False})
@api.depends('company_id')
def _compute_is_internal_project(self):
for project in self:
project.is_internal_project = project == project.company_id.internal_project_id
@api.model
def _search_is_internal_project(self, operator, value):
if not isinstance(value, bool):
raise ValueError(_('Invalid value: %s', value))
if operator not in ['=', '!=']:
raise ValueError(_('Invalid operator: %s', operator))
query = """
SELECT C.internal_project_id
FROM res_company C
WHERE C.internal_project_id IS NOT NULL
"""
if (operator == '=' and value is True) or (operator == '!=' and value is False):
operator_new = 'inselect'
else:
operator_new = 'not inselect'
return [('id', operator_new, (query, ()))]
@api.model
def _get_view_cache_key(self, view_id=None, view_type='form', **options):
"""The override of _get_view changing the time field labels according to the company timesheet encoding UOM
makes the view cache dependent on the company timesheet encoding uom"""
key = super()._get_view_cache_key(view_id, view_type, **options)
return key + (self.env.company.timesheet_encode_uom_id,)
@api.model
def _get_view(self, view_id=None, view_type='form', **options):
arch, view = super()._get_view(view_id, view_type, **options)
if view_type in ['tree', 'form'] and self.env.company.timesheet_encode_uom_id == self.env.ref('uom.product_uom_day'):
arch = self.env['account.analytic.line']._apply_time_label(arch, related_model=self._name)
return arch, view
@api.depends('allow_timesheets', 'timesheet_ids')
def _compute_remaining_hours(self):
timesheets_read_group = self.env['account.analytic.line']._read_group(
[('project_id', 'in', self.ids)],
['project_id', 'unit_amount'],
['project_id'],
lazy=False)
timesheet_time_dict = {res['project_id'][0]: res['unit_amount'] for res in timesheets_read_group}
for project in self:
project.remaining_hours = project.allocated_hours - timesheet_time_dict.get(project.id, 0)
project.is_project_overtime = project.remaining_hours < 0
@api.model
def _search_is_project_overtime(self, operator, value):
if not isinstance(value, bool):
raise ValueError(_('Invalid value: %s') % value)
if operator not in ['=', '!=']:
raise ValueError(_('Invalid operator: %s') % operator)
query = """
SELECT Project.id
FROM project_project AS Project
JOIN project_task AS Task
ON Project.id = Task.project_id
WHERE Project.allocated_hours > 0
AND Project.allow_timesheets = TRUE
AND Task.parent_id IS NULL
AND Task.is_closed IS FALSE
GROUP BY Project.id
HAVING Project.allocated_hours - SUM(Task.effective_hours) < 0
"""
if (operator == '=' and value is True) or (operator == '!=' and value is False):
operator_new = 'inselect'
else:
operator_new = 'not inselect'
return [('id', operator_new, (query, ()))]
@api.constrains('allow_timesheets', 'analytic_account_id')
def _check_allow_timesheet(self):
for project in self:
if project.allow_timesheets and not project.analytic_account_id:
raise ValidationError(_('You cannot use timesheets without an analytic account.'))
@api.depends('timesheet_ids')
def _compute_total_timesheet_time(self):
timesheets_read_group = self.env['account.analytic.line'].read_group(
[('project_id', 'in', self.ids)],
['project_id', 'unit_amount', 'product_uom_id'],
['project_id', 'product_uom_id'],
lazy=False)
timesheet_time_dict = defaultdict(list)
uom_ids = set(self.timesheet_encode_uom_id.ids)
for result in timesheets_read_group:
uom_id = result['product_uom_id'] and result['product_uom_id'][0]
if uom_id:
uom_ids.add(uom_id)
timesheet_time_dict[result['project_id'][0]].append((uom_id, result['unit_amount']))
uoms_dict = {uom.id: uom for uom in self.env['uom.uom'].browse(uom_ids)}
for project in self:
# Timesheets may be stored in a different unit of measure, so first
# we convert all of them to the reference unit
# if the timesheet has no product_uom_id then we take the one of the project
total_time = 0.0
for product_uom_id, unit_amount in timesheet_time_dict[project.id]:
factor = uoms_dict.get(product_uom_id, project.timesheet_encode_uom_id).factor_inv
total_time += unit_amount * (1.0 if project.encode_uom_in_days else factor)
# Now convert to the proper unit of measure set in the settings
total_time *= project.timesheet_encode_uom_id.factor
project.total_timesheet_time = int(round(total_time))
@api.depends('timesheet_ids')
def _compute_timesheet_count(self):
timesheet_read_group = self.env['account.analytic.line']._read_group(
[('project_id', 'in', self.ids)],
['project_id'],
['project_id']
)
timesheet_project_map = {project_info['project_id'][0]: project_info['project_id_count'] for project_info in timesheet_read_group}
for project in self:
project.timesheet_count = timesheet_project_map.get(project.id, 0)
@api.model_create_multi
def create(self, vals_list):
""" Create an analytic account if project allow timesheet and don't provide one
Note: create it before calling super() to avoid raising the ValidationError from _check_allow_timesheet
"""
defaults = self.default_get(['allow_timesheets', 'analytic_account_id'])
for vals in vals_list:
allow_timesheets = vals.get('allow_timesheets', defaults.get('allow_timesheets'))
analytic_account_id = vals.get('analytic_account_id', defaults.get('analytic_account_id'))
if allow_timesheets and not analytic_account_id:
analytic_account = self._create_analytic_account_from_values(vals)
vals['analytic_account_id'] = analytic_account.id
return super().create(vals_list)
def write(self, values):
# create the AA for project still allowing timesheet
if values.get('allow_timesheets') and not values.get('analytic_account_id'):
for project in self:
if not project.analytic_account_id:
project._create_analytic_account()
return super(Project, self).write(values)
def name_get(self):
res = super().name_get()
if len(self.env.context.get('allowed_company_ids', [])) <= 1:
return res
name_mapping = dict(res)
for project in self:
if project.is_internal_project:
name_mapping[project.id] = f'{name_mapping[project.id]} - {project.company_id.name}'
return list(name_mapping.items())
@api.model
def _init_data_analytic_account(self):
self.search([('analytic_account_id', '=', False), ('allow_timesheets', '=', True)])._create_analytic_account()
@api.ondelete(at_uninstall=False)
def _unlink_except_contains_entries(self):
"""
If some projects to unlink have some timesheets entries, these
timesheets entries must be unlinked first.
In this case, a warning message is displayed through a RedirectWarning
and allows the user to see timesheets entries to unlink.
"""
projects_with_timesheets = self.filtered(lambda p: p.timesheet_ids)
if projects_with_timesheets:
if len(projects_with_timesheets) > 1:
warning_msg = _("These projects have some timesheet entries referencing them. Before removing these projects, you have to remove these timesheet entries.")
else:
warning_msg = _("This project has some timesheet entries referencing it. Before removing this project, you have to remove these timesheet entries.")
raise RedirectWarning(
warning_msg, self.env.ref('hr_timesheet.timesheet_action_project').id,
_('See timesheet entries'), {'active_ids': projects_with_timesheets.ids})
def _convert_project_uom_to_timesheet_encode_uom(self, time):
uom_from = self.company_id.project_time_mode_id
uom_to = self.env.company.timesheet_encode_uom_id
return round(uom_from._compute_quantity(time, uom_to, raise_if_failure=False), 2)
def action_project_timesheets(self):
action = self.env['ir.actions.act_window']._for_xml_id('hr_timesheet.act_hr_timesheet_line_by_project')
action['display_name'] = _("%(name)s's Timesheets", name=self.name)
return action
class Task(models.Model):
_name = "project.task"
_inherit = "project.task"
analytic_account_active = fields.Boolean("Active Analytic Account", compute='_compute_analytic_account_active', compute_sudo=True)
allow_timesheets = fields.Boolean(
"Allow timesheets",
compute='_compute_allow_timesheets', compute_sudo=True,
search='_search_allow_timesheets', readonly=True,
help="Timesheets can be logged on this task.")
remaining_hours = fields.Float("Remaining Hours", compute='_compute_remaining_hours', store=True, readonly=True, help="Number of allocated hours minus the number of hours spent.")
remaining_hours_percentage = fields.Float(compute='_compute_remaining_hours_percentage', search='_search_remaining_hours_percentage')
effective_hours = fields.Float("Hours Spent", compute='_compute_effective_hours', compute_sudo=True, store=True)
total_hours_spent = fields.Float("Total Hours", compute='_compute_total_hours_spent', store=True, help="Time spent on this task and its sub-tasks (and their own sub-tasks).")
progress = fields.Float("Progress", compute='_compute_progress_hours', store=True, group_operator="avg")
overtime = fields.Float(compute='_compute_progress_hours', store=True)
subtask_effective_hours = fields.Float("Sub-tasks Hours Spent", compute='_compute_subtask_effective_hours', recursive=True, store=True, help="Time spent on the sub-tasks (and their own sub-tasks) of this task.")
timesheet_ids = fields.One2many('account.analytic.line', 'task_id', 'Timesheets')
encode_uom_in_days = fields.Boolean(compute='_compute_encode_uom_in_days', default=lambda self: self._uom_in_days())
@property
def SELF_READABLE_FIELDS(self):
return super().SELF_READABLE_FIELDS | PROJECT_TASK_READABLE_FIELDS
def _uom_in_days(self):
return self.env.company.timesheet_encode_uom_id == self.env.ref('uom.product_uom_day')
def _compute_encode_uom_in_days(self):
self.encode_uom_in_days = self._uom_in_days()
@api.depends('project_id.allow_timesheets')
def _compute_allow_timesheets(self):
for task in self:
task.allow_timesheets = task.project_id.allow_timesheets
def _search_allow_timesheets(self, operator, value):
query = self.env['project.project'].sudo()._search([
('allow_timesheets', operator, value),
])
return [('project_id', 'in', query)]
@api.depends('analytic_account_id.active', 'project_id.analytic_account_id.active')
def _compute_analytic_account_active(self):
""" Overridden in sale_timesheet """
for task in self:
task.analytic_account_active = task._get_task_analytic_account_id().active
@api.depends('timesheet_ids.unit_amount')
def _compute_effective_hours(self):
if not any(self._ids):
for task in self:
task.effective_hours = sum(task.timesheet_ids.mapped('unit_amount'))
return
timesheet_read_group = self.env['account.analytic.line'].read_group([('task_id', 'in', self.ids)], ['unit_amount', 'task_id'], ['task_id'])
timesheets_per_task = {res['task_id'][0]: res['unit_amount'] for res in timesheet_read_group}
for task in self:
task.effective_hours = timesheets_per_task.get(task.id, 0.0)
@api.depends('effective_hours', 'subtask_effective_hours', 'planned_hours')
def _compute_progress_hours(self):
for task in self:
if (task.planned_hours > 0.0):
task_total_hours = task.effective_hours + task.subtask_effective_hours
task.overtime = max(task_total_hours - task.planned_hours, 0)
if float_compare(task_total_hours, task.planned_hours, precision_digits=2) >= 0:
task.progress = 100
else:
task.progress = round(100.0 * task_total_hours / task.planned_hours, 2)
else:
task.progress = 0.0
task.overtime = 0
@api.depends('planned_hours', 'remaining_hours')
def _compute_remaining_hours_percentage(self):
for task in self:
if task.planned_hours > 0.0:
task.remaining_hours_percentage = task.remaining_hours / task.planned_hours
else:
task.remaining_hours_percentage = 0.0
def _search_remaining_hours_percentage(self, operator, value):
if operator not in OPERATOR_MAPPING:
raise NotImplementedError(_('This operator %s is not supported in this search method.', operator))
query = f"""
SELECT id
FROM {self._table}
WHERE remaining_hours > 0
AND planned_hours > 0
AND remaining_hours / planned_hours {operator} %s
"""
return [('id', 'inselect', (query, (value,)))]
@api.depends('effective_hours', 'subtask_effective_hours', 'planned_hours')
def _compute_remaining_hours(self):
for task in self:
task.remaining_hours = task.planned_hours - task.effective_hours - task.subtask_effective_hours
@api.depends('effective_hours', 'subtask_effective_hours')
def _compute_total_hours_spent(self):
for task in self:
task.total_hours_spent = task.effective_hours + task.subtask_effective_hours
@api.depends('child_ids.effective_hours', 'child_ids.subtask_effective_hours')
def _compute_subtask_effective_hours(self):
for task in self.with_context(active_test=False):
task.subtask_effective_hours = sum(child_task.effective_hours + child_task.subtask_effective_hours for child_task in task.child_ids)
def action_view_subtask_timesheet(self):
self.ensure_one()
task_ids = self.with_context(active_test=False)._get_subtask_ids_per_task_id().get(self.id, [])
action = self.env["ir.actions.actions"]._for_xml_id("hr_timesheet.timesheet_action_all")
graph_view_id = self.env.ref("hr_timesheet.view_hr_timesheet_line_graph_by_employee").id
new_views = []
for view in action['views']:
if view[1] == 'graph':
view = (graph_view_id, 'graph')
new_views.insert(0, view) if view[1] == 'tree' else new_views.append(view)
action.update({
'display_name': _('Timesheets'),
'context': {'default_project_id': self.project_id.id, 'grid_range': 'week'},
'domain': [('project_id', '!=', False), ('task_id', 'in', task_ids)],
'views': new_views,
})
return action
def _get_timesheet(self):
# Is override in sale_timesheet
return self.timesheet_ids
def write(self, values):
# a timesheet must have an analytic account (and a project)
if 'project_id' in values and not values.get('project_id') and self._get_timesheet():
raise UserError(_('This task must be part of a project because there are some timesheets linked to it.'))
res = super(Task, self).write(values)
if 'project_id' in values:
project = self.env['project.project'].browse(values.get('project_id'))
if project.allow_timesheets:
# We write on all non yet invoiced timesheet the new project_id (if project allow timesheet)
self._get_timesheet().write({'project_id': values.get('project_id')})
return res
def name_get(self):
if self.env.context.get('hr_timesheet_display_remaining_hours'):
name_mapping = dict(super().name_get())
for task in self:
if task.allow_timesheets and task.planned_hours > 0 and task.encode_uom_in_days:
days_left = _("(%s days remaining)") % task._convert_hours_to_days(task.remaining_hours)
name_mapping[task.id] = name_mapping.get(task.id, '') + u"\u00A0" + days_left
elif task.allow_timesheets and task.planned_hours > 0:
hours, mins = (str(int(duration)).rjust(2, '0') for duration in divmod(abs(task.remaining_hours) * 60, 60))
hours_left = _(
"(%(sign)s%(hours)s:%(minutes)s remaining)",
sign='-' if task.remaining_hours < 0 else '',
hours=hours,
minutes=mins,
)
name_mapping[task.id] = name_mapping.get(task.id, '') + u"\u00A0" + hours_left
return list(name_mapping.items())
return super().name_get()
@api.model
def _get_view_cache_key(self, view_id=None, view_type='form', **options):
"""The override of _get_view changing the time field labels according to the company timesheet encoding UOM
makes the view cache dependent on the company timesheet encoding uom"""
key = super()._get_view_cache_key(view_id, view_type, **options)
return key + (self.env.company.timesheet_encode_uom_id,)
@api.model
def _get_view(self, view_id=None, view_type='form', **options):
""" Set the correct label for `unit_amount`, depending on company UoM """
arch, view = super()._get_view(view_id, view_type, **options)
# Use of sudo as the portal user doesn't have access to uom
arch = self.env['account.analytic.line'].sudo()._apply_timesheet_label(arch)
if view_type in ['tree', 'pivot', 'graph', 'form'] and self.env.company.timesheet_encode_uom_id == self.env.ref('uom.product_uom_day'):
arch = self.env['account.analytic.line']._apply_time_label(arch, related_model=self._name)
return arch, view
@api.ondelete(at_uninstall=False)
def _unlink_except_contains_entries(self):
"""
If some tasks to unlink have some timesheets entries, these
timesheets entries must be unlinked first.
In this case, a warning message is displayed through a RedirectWarning
and allows the user to see timesheets entries to unlink.
"""
timesheet_data = self.env['account.analytic.line'].sudo()._read_group(
[('task_id', 'in', self.ids)],
['task_id'],
['task_id'],
)
task_with_timesheets_ids = [res['task_id'][0] for res in timesheet_data]
if task_with_timesheets_ids:
if len(task_with_timesheets_ids) > 1:
warning_msg = _("These tasks have some timesheet entries referencing them. Before removing these tasks, you have to remove these timesheet entries.")
else:
warning_msg = _("This task has some timesheet entries referencing it. Before removing this task, you have to remove these timesheet entries.")
raise RedirectWarning(
warning_msg, self.env.ref('hr_timesheet.timesheet_action_task').id,
_('See timesheet entries'), {'active_ids': task_with_timesheets_ids})
@api.model
def _convert_hours_to_days(self, time):
uom_hour = self.env.ref('uom.product_uom_hour')
uom_day = self.env.ref('uom.product_uom_day')
return round(uom_hour._compute_quantity(time, uom_day, raise_if_failure=False), 2)

View file

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, models
class ProjectCollaborator(models.Model):
_inherit = 'project.collaborator'
@api.model
def _toggle_project_sharing_portal_rules(self, active):
super()._toggle_project_sharing_portal_rules(active)
# ir.model.access
access_timesheet_portal = self.env.ref('hr_timesheet.access_account_analytic_line_portal_user').sudo()
if access_timesheet_portal.active != active:
access_timesheet_portal.write({'active': active})
# ir.rule
timesheet_portal_ir_rule = self.env.ref('hr_timesheet.timesheet_line_rule_portal_user').sudo()
if timesheet_portal_ir_rule.active != active:
timesheet_portal_ir_rule.write({'active': active})

View file

@ -0,0 +1,82 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError
class ResCompany(models.Model):
_inherit = 'res.company'
@api.model
def _default_project_time_mode_id(self):
uom = self.env.ref('uom.product_uom_hour', raise_if_not_found=False)
wtime = self.env.ref('uom.uom_categ_wtime')
if not uom:
uom = self.env['uom.uom'].search([('category_id', '=', wtime.id), ('uom_type', '=', 'reference')], limit=1)
if not uom:
uom = self.env['uom.uom'].search([('category_id', '=', wtime.id)], limit=1)
return uom
@api.model
def _default_timesheet_encode_uom_id(self):
uom = self.env.ref('uom.product_uom_hour', raise_if_not_found=False)
wtime = self.env.ref('uom.uom_categ_wtime')
if not uom:
uom = self.env['uom.uom'].search([('category_id', '=', wtime.id), ('uom_type', '=', 'reference')], limit=1)
if not uom:
uom = self.env['uom.uom'].search([('category_id', '=', wtime.id)], limit=1)
return uom
project_time_mode_id = fields.Many2one('uom.uom', string='Project Time Unit',
default=_default_project_time_mode_id,
help="This will set the unit of measure used in projects and tasks.\n"
"If you use the timesheet linked to projects, don't "
"forget to setup the right unit of measure in your employees.")
timesheet_encode_uom_id = fields.Many2one('uom.uom', string="Timesheet Encoding Unit",
default=_default_timesheet_encode_uom_id, domain=lambda self: [('category_id', '=', self.env.ref('uom.uom_categ_wtime').id)])
internal_project_id = fields.Many2one(
'project.project', string="Internal Project",
help="Default project value for timesheet generated from time off type.")
@api.constrains('internal_project_id')
def _check_internal_project_id_company(self):
if self.filtered(lambda company: company.internal_project_id and company.internal_project_id.sudo().company_id != company):
raise ValidationError(_('The Internal Project of a company should be in that company.'))
@api.model_create_multi
def create(self, values):
company = super(ResCompany, self).create(values)
# use sudo as the user could have the right to create a company
# but not to create a project. On the other hand, when the company
# is created, it is not in the allowed_company_ids on the env
company.sudo()._create_internal_project_task()
return company
def _create_internal_project_task(self):
results = []
type_ids_ref = self.env.ref('hr_timesheet.internal_project_default_stage', raise_if_not_found=False)
type_ids = [(4, type_ids_ref.id)] if type_ids_ref else []
for company in self:
company = company.with_company(company)
results += [{
'name': _('Internal'),
'allow_timesheets': True,
'company_id': company.id,
'type_ids': type_ids,
'task_ids': [(0, 0, {
'name': name,
'company_id': company.id,
}) for name in [_('Training'), _('Meeting')]]
}]
project_ids = self.env['project.project'].create(results)
projects_by_company = {project.company_id.id: project for project in project_ids}
for company in self:
company.internal_project_id = projects_by_company.get(company.id, False)
return project_ids
def _is_timesheet_hour_uom(self):
return self.timesheet_encode_uom_id and self.timesheet_encode_uom_id == self.env.ref('uom.product_uom_hour')
def _timesheet_uom_text(self):
return self._is_timesheet_hour_uom() and _("hours") or _("days")

View file

@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
module_project_timesheet_synchro = fields.Boolean("Awesome Timesheet",
compute="_compute_timesheet_modules", store=True, readonly=False)
module_project_timesheet_holidays = fields.Boolean("Time Off",
compute="_compute_timesheet_modules", store=True, readonly=False)
reminder_user_allow = fields.Boolean(string="Employee Reminder")
reminder_manager_allow = fields.Boolean(string="Manager Reminder")
project_time_mode_id = fields.Many2one(
'uom.uom', related='company_id.project_time_mode_id', string='Project Time Unit', readonly=False,
help="This will set the unit of measure used in projects and tasks.\n"
"If you use the timesheet linked to projects, don't "
"forget to setup the right unit of measure in your employees.")
timesheet_encode_uom_id = fields.Many2one('uom.uom', string="Encoding Unit",
related='company_id.timesheet_encode_uom_id', readonly=False)
is_encode_uom_days = fields.Boolean(compute='_compute_is_encode_uom_days')
@api.depends('timesheet_encode_uom_id')
def _compute_is_encode_uom_days(self):
product_uom_day = self.env.ref('uom.product_uom_day')
for settings in self:
settings.is_encode_uom_days = settings.timesheet_encode_uom_id == product_uom_day
@api.depends('module_hr_timesheet')
def _compute_timesheet_modules(self):
self.filtered(lambda config: not config.module_hr_timesheet).update({
'module_project_timesheet_synchro': False,
'module_project_timesheet_holidays': False,
})

View file

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models
class Uom(models.Model):
_inherit = 'uom.uom'
def _unprotected_uom_xml_ids(self):
# Override
# When timesheet App is installed, we also need to protect the hour UoM
# from deletion (and warn in case of modification)
return [
"product_uom_dozen",
]
# widget used in the webclient when this unit is the one used to encode timesheets.
timesheet_widget = fields.Char("Widget")