| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from ast import literal_eval |
| 4 |
|
| 5 |
from odoo import api, models, fields, _ |
| 6 |
from odoo.exceptions import UserError |
| 7 |
from odoo.tools import SQL |
| 8 |
|
| 9 |
|
| 10 |
class HrEmployee(models.Model): |
| 11 |
_inherit = 'hr.employee' |
| 12 |
|
| 13 |
has_timesheet = fields.Boolean(compute='_compute_has_timesheet', export_string_translation=False) |
| 14 |
|
| 15 |
def _compute_has_timesheet(self): |
| 16 |
if self.ids: |
| 17 |
result = dict(self.env.execute_query(SQL( |
| 18 |
""" SELECT id, EXISTS( |
| 19 |
SELECT 1 FROM account_analytic_line |
| 20 |
WHERE project_id IS NOT NULL AND employee_id = e.id |
| 21 |
LIMIT 1) |
| 22 |
FROM hr_employee e |
| 23 |
WHERE id in %s """, |
| 24 |
tuple(self.ids), |
| 25 |
))) |
| 26 |
else: |
| 27 |
result = {} |
| 28 |
|
| 29 |
for employee in self: |
| 30 |
employee.has_timesheet = result.get(employee._origin.id, False) |
| 31 |
|
| 32 |
@api.depends('company_id', 'user_id') |
| 33 |
@api.depends_context('allowed_company_ids') |
| 34 |
def _compute_display_name(self): |
| 35 |
super()._compute_display_name() |
| 36 |
allowed_company_ids = self.env.context.get('allowed_company_ids', []) |
| 37 |
if len(allowed_company_ids) <= 1: |
| 38 |
return |
| 39 |
|
| 40 |
employees_count_per_user = { |
| 41 |
user.id: count |
| 42 |
for user, count in self.env['hr.employee'].sudo()._read_group( |
| 43 |
[('user_id', 'in', self.user_id.ids), ('company_id', 'in', allowed_company_ids)], |
| 44 |
['user_id'], |
| 45 |
['__count'], |
| 46 |
) |
| 47 |
} |
| 48 |
for employee in self: |
| 49 |
if employees_count_per_user.get(employee.user_id.id, 0) > 1: |
| 50 |
employee.display_name = f'{employee.display_name} - {employee.company_id.name}' |
| 51 |
|
| 52 |
def action_unlink_wizard(self): |
| 53 |
wizard = self.env['hr.employee.delete.wizard'].create({ |
| 54 |
'employee_ids': self.ids, |
| 55 |
}) |
| 56 |
if not self.env.user.has_group('hr_timesheet.group_hr_timesheet_approver') and wizard.has_timesheet and not wizard.has_active_employee: |
| 57 |
raise UserError(_('You cannot delete employees who have timesheets.')) |
| 58 |
|
| 59 |
return { |
| 60 |
'name': _('Confirmation'), |
| 61 |
'view_mode': 'form', |
| 62 |
'res_model': 'hr.employee.delete.wizard', |
| 63 |
'views': [(self.env.ref('hr_timesheet.hr_employee_delete_wizard_form').id, 'form')], |
| 64 |
'type': 'ir.actions.act_window', |
| 65 |
'res_id': wizard.id, |
| 66 |
'target': 'new', |
| 67 |
'context': self.env.context, |
| 68 |
} |
| 69 |
|
| 70 |
def action_timesheet_from_employee(self): |
| 71 |
action = self.env["ir.actions.act_window"]._for_xml_id("hr_timesheet.timesheet_action_from_employee") |
| 72 |
context = literal_eval(action['context'].replace('active_id', str(self.id))) |
| 73 |
context['create'] = context.get('create', True) and self.active |
| 74 |
action['context'] = context |
| 75 |
return action |