[IMP] hr_homeworking: prevent deleting worklocations used by employees

This commit prevents deleting worklocations if they are used as default of an
employee. In case the worklocation is deleted, exceptions using that
worklocation are also deleted.

task-3527196

closes odoo/odoo#137502

Related: odoo/upgrade#5260
Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
This commit is contained in:
Leonardo Pavan Rocha
2023-10-12 16:12:04 +00:00
parent 3d61afeb60
commit cbab7b013e
9 changed files with 31 additions and 10 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ class ResConfigSettings(models.TransientModel):
related='company_id.resource_calendar_id', readonly=False)
module_hr_presence = fields.Boolean(string="Advanced Presence Control")
module_hr_skills = fields.Boolean(string="Skills Management")
module_hr_homeworking = fields.Boolean(string="Homeworking")
module_hr_homeworking = fields.Boolean(string="Remote Work")
hr_presence_control_login = fields.Boolean(string="Based on user status in system", config_parameter='hr.hr_presence_control_login')
hr_presence_control_email = fields.Boolean(string="Based on number of emails sent", config_parameter='hr_presence.hr_presence_control_email')
hr_presence_control_ip = fields.Boolean(string="Based on IP Address", config_parameter='hr_presence.hr_presence_control_ip')
@@ -43,6 +43,9 @@
<setting help="Enrich employee profiles with skills and resumes" id="enrich_employee_setting">
<field name="module_hr_skills"/>
</setting>
<setting id="home_working_setting" help="Display remote working settings for each employee and dedicated reports. Presence icons will be updated with remote working location.">
<field name="module_hr_homeworking"/>
</setting>
</block>
<block title="Work Organization" name="work_organization_setting_container">
<setting company_dependent="1" help="Set default company schedule to manage your employees working time" id="default_company_schedule_setting">
+2 -2
View File
@@ -2,9 +2,9 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
{
'name': 'Homeworking',
'name': 'Remote Work',
'version': '1.0',
'category': 'Human Resources/Homeworking',
'category': 'Human Resources/Remote Work',
'depends': ['hr', 'calendar'],
'data': [
'security/security.xml',
+1
View File
@@ -4,3 +4,4 @@ from . import hr_employee
from . import res_partner
from . import res_users
from . import hr_homeworking
from . import hr_work_location
+3 -3
View File
@@ -13,7 +13,7 @@ class HrEmployeeBase(models.AbstractModel):
@api.model
def _default_location_id(self):
return self.env.ref('hr.home_work_office')
return self.env.ref('hr.home_work_office', raise_if_not_found=False)
monday_location_id = fields.Many2one('hr.work.location', string='Monday', default=lambda self: self._default_location_id())
tuesday_location_id = fields.Many2one('hr.work.location', string='Tuesday', default=lambda self: self._default_location_id())
@@ -81,9 +81,9 @@ class HrEmployeeBase(models.AbstractModel):
def _get_worklocation(self, start_date, end_date):
work_locations_by_employee = defaultdict(dict)
for employee in self:
work_locations_by_employee[employee.id]["user_id"] = employee["user_id"].id
work_locations_by_employee[employee.id]["user_id"] = employee.user_id.id
work_locations_by_employee[employee.id]["employee_id"] = employee.id
work_locations_by_employee[employee.id]["partner_id"] = employee["user_partner_id"].id
work_locations_by_employee[employee.id]["partner_id"] = employee.user_partner_id.id
work_locations_by_employee[employee.id]["employee_name"] = employee.name
for day in DAYS:
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _, api, models
from odoo.exceptions import UserError
from odoo.addons.hr_homeworking.models.hr_homeworking import DAYS
class WorkLocation(models.Model):
_inherit = "hr.work.location"
@api.ondelete(at_uninstall=False)
def _unlink_except_used_by_employee(self):
domains = [(day, 'in', self.ids) for day in DAYS]
employee_uses_location = self.env['hr.employee'].search_count(domains, limit=1)
if employee_uses_location:
raise UserError(_("You cannot delete locations that are being used by your employees"))
exceptions_using_location = self.env['hr.employee.location'].search(('work_location_id', 'in', self.ids))
exceptions_using_location.unlink()
@@ -20,7 +20,7 @@
<attribute name="invisible">1</attribute>
</xpath>
<xpath expr="//group[@name='departure']" position="after">
<group string="Homeworking">
<group string="Remote Work">
<span class="text-muted fst-italic oe_inline" colspan="2">Specify your default work location for each day of the week. This schedule will repeat itself each week.</span>
<field name="monday_location_id" placeholder="Unspecified" force_save="1"/>
<field name="tuesday_location_id" placeholder="Unspecified" force_save="1"/>
+1 -1
View File
@@ -7,7 +7,7 @@
<field name="inherit_id" ref="hr.res_users_view_form_profile"/>
<field name="arch" type="xml">
<xpath expr="//group[@name='managers']" position="after">
<group name="Homeworking" string="Homeworking">
<group name="Homeworking" string="Remote Work">
<span class="text-muted fst-italic oe_inline" colspan="2">Specify your default work location for each day of the week. This schedule will repeat itself each week.</span>
<field name="monday_location_id" placeholder="Unspecified"/>
<field name="tuesday_location_id" placeholder="Unspecified"/>
@@ -2,7 +2,6 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models, api
from odoo.addons.resource.models.utils import string_to_datetime
class HomeworkLocationWizard(models.TransientModel):
@@ -32,7 +31,7 @@ class HomeworkLocationWizard(models.TransientModel):
('date', '=', self.date),
('employee_id', '=', employee_id.id)
])
date_week_name = string_to_datetime(self.date).date().strftime("%A").lower() # convert Tuesday to tuesday
date_week_name = self.day_week_string.lower() # convert Tuesday to tuesday
default_location_for_current_date = f"{date_week_name}_location_id"
if self.weekly:
# delete any exceptions on the current date