| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from odoo import fields, models, api |
| 4 |
|
| 5 |
from odoo.addons.hr_homeworking.models.hr_homeworking import DAYS |
| 6 |
|
| 7 |
|
| 8 |
class HomeworkLocationWizard(models.TransientModel): |
| 9 |
_name = 'homework.location.wizard' |
| 10 |
_description = 'Set Homework Location Wizard' |
| 11 |
|
| 12 |
work_location_id = fields.Many2one('hr.work.location', required=True, string="Location") |
| 13 |
work_location_name = fields.Char(related='work_location_id.name', string="Location name") |
| 14 |
work_location_type = fields.Selection(related="work_location_id.location_type") |
| 15 |
employee_id = fields.Many2one('hr.employee', default=lambda self: self.env.user.employee_id, required=True, ondelete="cascade") |
| 16 |
employee_name = fields.Char(related="employee_id.name") |
| 17 |
weekly = fields.Boolean(default=False) |
| 18 |
date = fields.Date(string="Date") |
| 19 |
day_week_string = fields.Char(compute="_compute_day_week_string") |
| 20 |
|
| 21 |
@api.depends('date') |
| 22 |
def _compute_day_week_string(self): |
| 23 |
for record in self: |
| 24 |
record.day_week_string = record.date.strftime("%A") if record.date else '' |
| 25 |
|
| 26 |
def set_employee_location(self): |
| 27 |
self.ensure_one() |
| 28 |
if not self.date: |
| 29 |
return |
| 30 |
default_employee_id = self.env.context.get('default_employee_id') or self.env.user.employee_id.id |
| 31 |
employee_id = self.env['hr.employee'].browse(self.employee_id.id or default_employee_id) |
| 32 |
employee_location = self.env['hr.employee.location'].search([ |
| 33 |
('date', '=', self.date), |
| 34 |
('employee_id', '=', employee_id.id) |
| 35 |
]) |
| 36 |
weekday = self.date.weekday() |
| 37 |
default_location_for_current_date = DAYS[weekday] |
| 38 |
if self.weekly: |
| 39 |
# delete any exceptions on the current date |
| 40 |
if employee_location: |
| 41 |
employee_location.unlink() |
| 42 |
employee_id.sudo().user_id.write({ |
| 43 |
default_location_for_current_date: self.work_location_id.id, |
| 44 |
}) |
| 45 |
else: |
| 46 |
# check if work_location_id is the same as the default one for that day |
| 47 |
if self.work_location_id.id == employee_id[default_location_for_current_date].id: |
| 48 |
employee_location.unlink() |
| 49 |
# check if worklocation is set for that employee that day |
| 50 |
elif employee_location: |
| 51 |
employee_location.write({ |
| 52 |
'date': self.date, |
| 53 |
'employee_id': employee_id.id, |
| 54 |
'work_location_id': self.work_location_id.id |
| 55 |
}) |
| 56 |
else: |
| 57 |
self.env['hr.employee.location'].create({ |
| 58 |
'date': self.date, |
| 59 |
'employee_id': employee_id.id, |
| 60 |
'work_location_id': self.work_location_id.id |
| 61 |
}) |