diff --git a/addons/hr_work_entry/__init__.py b/addons/hr_work_entry/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/addons/hr_work_entry/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/addons/hr_work_entry/__manifest__.py b/addons/hr_work_entry/__manifest__.py new file mode 100644 index 00000000000..957fbf0bc1e --- /dev/null +++ b/addons/hr_work_entry/__manifest__.py @@ -0,0 +1,23 @@ +#-*- coding:utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +{ + 'name': 'Work Entries', + 'category': 'Human Resources/Employees', + 'sequence': 39, + 'summary': 'Manages work entries', + 'description': "", + 'installable': True, + 'depends': [ + 'hr', + ], + 'data': [ + 'security/hr_work_entry_security.xml', + 'security/ir.model.access.csv', + 'data/hr_work_entry_data.xml', + 'views/hr_work_entry_views.xml', + ], + 'qweb': [ + "static/src/xml/work_entry_templates.xml", + ], +} diff --git a/addons/hr_work_entry/data/hr_work_entry_data.xml b/addons/hr_work_entry/data/hr_work_entry_data.xml new file mode 100644 index 00000000000..f4d2a6fb1fd --- /dev/null +++ b/addons/hr_work_entry/data/hr_work_entry_data.xml @@ -0,0 +1,13 @@ + + + + + + + Attendance + 0 + WORK100 + + + + diff --git a/addons/hr_work_entry/models/__init__.py b/addons/hr_work_entry/models/__init__.py new file mode 100644 index 00000000000..7fe98a79706 --- /dev/null +++ b/addons/hr_work_entry/models/__init__.py @@ -0,0 +1,5 @@ +# -*- coding:utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import hr_work_entry +from . import resource diff --git a/addons/hr_work_entry/models/hr_work_entry.py b/addons/hr_work_entry/models/hr_work_entry.py new file mode 100644 index 00000000000..0a185cadb36 --- /dev/null +++ b/addons/hr_work_entry/models/hr_work_entry.py @@ -0,0 +1,211 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from contextlib import contextmanager +from dateutil.relativedelta import relativedelta + +from odoo import api, fields, models + + +class HrWorkEntry(models.Model): + _name = 'hr.work.entry' + _description = 'HR Work Entry' + _order = 'display_warning desc,state,date_start' + + name = fields.Char(required=True) + active = fields.Boolean(default=True) + employee_id = fields.Many2one('hr.employee', required=True) + date_start = fields.Datetime(required=True, string='From') + date_stop = fields.Datetime(string='To') + duration = fields.Float(compute='_compute_duration', inverse='_inverse_duration', store=True, string="Period") + work_entry_type_id = fields.Many2one('hr.work.entry.type') + color = fields.Integer(related='work_entry_type_id.color', readonly=True) + state = fields.Selection([ + ('draft', 'Draft'), + ('confirmed', 'Confirmed'), + ('validated', 'Validated'), + ('cancelled', 'Cancelled') + ], default='draft') + display_warning = fields.Boolean(string="Error") + company_id = fields.Many2one('res.company', string='Company', readonly=True, required=True, + default=lambda self: self.env.company) + + _sql_constraints = [ + ('_work_entry_has_end', 'check (date_stop IS NOT NULL)', 'Work entry must end. Please define an end date or a duration.'), + ('_work_entry_start_before_end', 'check (date_stop > date_start)', 'Starting time should be before end time.') + ] + + @api.onchange('duration') + def _onchange_duration(self): + self._inverse_duration() + + @api.depends('date_stop', 'date_start') + def _compute_duration(self): + for work_entry in self: + work_entry.duration = work_entry._get_duration(work_entry.date_start, work_entry.date_stop) + + def _inverse_duration(self): + for work_entry in self: + if work_entry.date_start and work_entry.duration: + work_entry.date_stop = work_entry.date_start + relativedelta(hours=work_entry.duration) + + def _get_duration(self, date_start, date_stop): + if not date_start or not date_stop: + return 0 + dt = date_stop - date_start + return dt.days * 24 + dt.seconds / 3600 # Number of hours + + @api.multi + def action_validate(self): + """ + Try to validate work entries. + If some errors are found, set `display_warning` to True for conflicting work entries + and validation fails. + :return: True if validation succeded + """ + work_entries = self.filtered(lambda work_entry: work_entry.state != 'validated') + if not work_entries._check_if_error(): + work_entries.write({'state': 'validated'}) + return True + return False + + @api.multi + def _check_if_error(self): + if not self: + return False + undefined_type = self.filtered(lambda b: not b.work_entry_type_id) + undefined_type.write({'display_warning': True}) + conflict = self._mark_conflicting_work_entries(min(self.mapped('date_start')), max(self.mapped('date_stop'))) + return undefined_type or conflict + + @api.model + def _mark_conflicting_work_entries(self, start, stop): + """ + Set `display_warning` to True for overlapping work entries + between two dates. + Return True if overlapping work entries were detected. + """ + # Use the postgresql range type `tsrange` which is a range of timestamp + # It supports the intersection operator (&&) useful to detect overlap. + # use '()' to exlude the lower and upper bounds of the range. + # Filter on date_start and date_stop (both indexed) in the EXISTS clause to + # limit the resulting set size and fasten the query. + query = """ + SELECT b1.id + FROM hr_work_entry b1 + WHERE + b1.date_start <= %s + AND b1.date_stop >= %s + AND active = TRUE + AND EXISTS ( + SELECT 1 + FROM hr_work_entry b2 + WHERE + b2.date_start <= %s + AND b2.date_stop >= %s + AND active = TRUE + AND tsrange(b1.date_start, b1.date_stop, '()') && tsrange(b2.date_start, b2.date_stop, '()') + AND b1.id <> b2.id + AND b1.employee_id = b2.employee_id + ); + """ + self.env.cr.execute(query, (stop, start, stop, start)) + conflicts = [res.get('id') for res in self.env.cr.dictfetchall()] + self.browse(conflicts).write({ + 'display_warning': True, + }) + return bool(conflicts) + + @api.model_create_multi + def create(self, vals_list): + work_entries = super().create(vals_list) + work_entries._check_if_error() + return work_entries + + def write(self, vals): + skip_check = not bool({'date_start', 'date_stop', 'employee_id', 'work_entry_type_id', 'active'} & vals.keys()) + if 'state' in vals: + if vals['state'] == 'draft': + vals['active'] = True + elif vals['state'] == 'cancelled': + vals['active'] = False + skip_check &= not any(self.mapped('display_warning')) + elif vals['state'] == 'validated': + # A validated work entry is never conflicting + vals['display_warning'] = False + + if 'active' in vals: + vals['state'] = 'confirmed' if vals['active'] else 'cancelled' + + with self._error_checking(skip=skip_check): + return super(HrWorkEntry, self).write(vals) + + @api.multi + def unlink(self): + with self._error_checking(): + return super().unlink() + + def _reset_conflicting_state(self): + self.write({'display_warning': False}) + + @contextmanager + def _error_checking(self, start=None, stop=None, skip=False): + """ + Context manager used for conflicts checking. + When exiting the context manager, conflicts are checked + for all work entries within a date range. By default, the start and end dates are + computed according to `self` (min and max respectively) but it can be overwritten by providing + other values as parameter. + :param start: datetime to overwrite the default behaviour + :param stop: datetime to overwrite the default behaviour + :param skip: If True, no error checking is done + """ + try: + skip = skip or self.env.context.get('hr_work_entry_no_check', False) + start = start or min(self.mapped('date_start'), default=False) + stop = stop or max(self.mapped('date_stop'), default=False) + if not skip and start and stop: + work_entries = self.sudo().with_context(hr_work_entry_no_check=True).search([ + ('date_start', '<', stop), + ('date_stop', '>', start), + ('state', 'not in', ('validated', 'cancelled')), + ]) + work_entries._reset_conflicting_state() + yield + finally: + if not skip and start and stop: + # New work entries are handled in the create method, + # no need to reload work entries. + work_entries.exists()._check_if_error() + + +class HrWorkEntryType(models.Model): + _name = 'hr.work.entry.type' + _description = 'HR Work Entry Type' + + name = fields.Char(required=True) + code = fields.Char(required=True) + color = fields.Integer(default=0) + sequence = fields.Integer(default=25) + active = fields.Boolean( + 'Active', default=True, + help="If the active field is set to false, it will allow you to hide the work entry type without removing it.") + + _sql_constraints = [ + ('unique_work_entry_code', 'UNIQUE(code)', 'The same code cannot be associated to multiple work entry types.'), + ] + + +class Contacts(models.Model): + """ Personnal calendar filter """ + + _name = 'hr.user.work.entry.employee' + _description = 'Work Entries Employees' + + user_id = fields.Many2one('res.users', 'Me', required=True, default=lambda self: self.env.user) + employee_id = fields.Many2one('hr.employee', 'Employee', required=True) + active = fields.Boolean('Active', default=True) + + _sql_constraints = [ + ('user_id_employee_id_unique', 'UNIQUE(user_id,employee_id)', 'You cannot have the same employee twice.') + ] diff --git a/addons/hr_work_entry/models/resource.py b/addons/hr_work_entry/models/resource.py new file mode 100644 index 00000000000..bf05682d221 --- /dev/null +++ b/addons/hr_work_entry/models/resource.py @@ -0,0 +1,19 @@ +# -*- coding:utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models, fields + + +class ResourceCalendarAttendance(models.Model): + _inherit = 'resource.calendar.attendance' + + def _default_work_entry_type_id(self): + return self.env.ref('hr_work_entry.work_entry_type_attendance', raise_if_not_found=False) + + work_entry_type_id = fields.Many2one('hr.work.entry.type', 'Work Entry Type', default=_default_work_entry_type_id) + + +class ResourceCalendarLeave(models.Model): + _inherit = 'resource.calendar.leaves' + + work_entry_type_id = fields.Many2one('hr.work.entry.type', 'Work Entry Type') diff --git a/addons/hr_work_entry/security/hr_work_entry_security.xml b/addons/hr_work_entry/security/hr_work_entry_security.xml new file mode 100644 index 00000000000..357f37fd2ff --- /dev/null +++ b/addons/hr_work_entry/security/hr_work_entry_security.xml @@ -0,0 +1,17 @@ + + + + + + Work entries/Employee calendar filter: only self + + [('user_id', '=', user.id)] + + + + + + + + + diff --git a/addons/hr_work_entry/security/ir.model.access.csv b/addons/hr_work_entry/security/ir.model.access.csv new file mode 100644 index 00000000000..7350f08cb78 --- /dev/null +++ b/addons/hr_work_entry/security/ir.model.access.csv @@ -0,0 +1,5 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_hr_work_entry_officer,access_hr_work_entry_officer,model_hr_work_entry,hr.group_hr_user,1,1,1,1 +access_hr_work_entry_type_officer,access_hr_work_entry_type_officer,model_hr_work_entry_type,hr.group_hr_user,1,0,0,0 +access_hr_work_entry_type_manager,access_hr_work_entry_type_manager,model_hr_work_entry_type,hr.group_hr_manager,1,1,1,1 +access_hr_work_entry_employee,access_hr_work_entry_employee,model_hr_user_work_entry_employee,hr.group_hr_user,1,1,1,1 \ No newline at end of file diff --git a/addons/hr_work_entry/static/src/xml/work_entry_templates.xml b/addons/hr_work_entry/static/src/xml/work_entry_templates.xml new file mode 100644 index 00000000000..ad1f6d9e523 --- /dev/null +++ b/addons/hr_work_entry/static/src/xml/work_entry_templates.xml @@ -0,0 +1,15 @@ + + + + + + + + + We found some conflicts, + + click here to revise them + + + + diff --git a/addons/hr_work_entry/views/hr_work_entry_views.xml b/addons/hr_work_entry/views/hr_work_entry_views.xml new file mode 100644 index 00000000000..f703733cbd6 --- /dev/null +++ b/addons/hr_work_entry/views/hr_work_entry_views.xml @@ -0,0 +1,195 @@ + + + + + + + Work Entry + hr.work.entry + {'search_default_work_entries_error': 1} + tree,calendar,form,pivot + + + + Work Entry + hr.work.entry + calendar,tree,form,pivot + + + + hr.work.entry.calendar + hr.work.entry + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + hr.work.entry.form + hr.work.entry + + + + + + + + + + + + + + + + + + + + + + + Hours + + + + + + + + + + hr.work.entry.tree + hr.work.entry + + + + + + + + + + + + + + hr.work.entry.filter + hr.work.entry + + + + + + + + + + + + + + + + + Work Entry Types + hr.work.entry.type + kanban,tree,form + + + + hr.work.entry.type.tree + hr.work.entry.type + + + + + + + + + + hr.work.entry.type.form + hr.work.entry.type + + + + + + + + + + + + + + + + + + + + + hr.work.entry.type.kanban.view + hr.work.entry.type + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +