| 1 |
|
| 2 |
|
| 3 |
from datetime import datetime, time |
| 4 |
from dateutil.relativedelta import relativedelta |
| 5 |
from pytz import timezone, utc |
| 6 |
|
| 7 |
from odoo import api, fields, models, _ |
| 8 |
from odoo.exceptions import ValidationError |
| 9 |
from odoo.fields import Datetime |
| 10 |
|
| 11 |
|
| 12 |
class ResourceCalendarLeaves(models.Model): |
| 13 |
_name = 'resource.calendar.leaves' |
| 14 |
_description = "Resource Time Off Detail" |
| 15 |
_order = "date_from" |
| 16 |
|
| 17 |
@api.model |
| 18 |
def default_get(self, fields): |
| 19 |
res = super().default_get(fields) |
| 20 |
if 'date_from' in fields and 'date_to' in fields and not res.get('date_from') and not res.get('date_to'): |
| 21 |
|
| 22 |
today = Datetime.now() |
| 23 |
calendar = self.env.company.resource_calendar_id |
| 24 |
if 'calendar_id' in res: |
| 25 |
calendar = self.env['resource.calendar'].browse(res['calendar_id']) |
| 26 |
tz = timezone(calendar.tz or 'UTC') |
| 27 |
date_from = tz.localize(datetime.combine(today, time.min)) |
| 28 |
date_to = tz.localize(datetime.combine(today, time.max)) |
| 29 |
res.update( |
| 30 |
date_from=date_from.astimezone(utc).replace(tzinfo=None), |
| 31 |
date_to=date_to.astimezone(utc).replace(tzinfo=None) |
| 32 |
) |
| 33 |
return res |
| 34 |
|
| 35 |
name = fields.Char('Reason') |
| 36 |
company_id = fields.Many2one( |
| 37 |
'res.company', string="Company", readonly=True, store=True, |
| 38 |
default=lambda self: self.env.company, compute='_compute_company_id') |
| 39 |
calendar_id = fields.Many2one( |
| 40 |
'resource.calendar', "Working Hours", |
| 41 |
compute='_compute_calendar_id', store=True, readonly=False, |
| 42 |
domain="[('company_id', 'in', [company_id, False])]", |
| 43 |
check_company=True, index=True, |
| 44 |
) |
| 45 |
date_from = fields.Datetime('Start Date', required=True) |
| 46 |
date_to = fields.Datetime('End Date', compute="_compute_date_to", readonly=False, required=True, store=True) |
| 47 |
resource_id = fields.Many2one( |
| 48 |
"resource.resource", 'Resource', index=True, |
| 49 |
help="If empty, this is a generic time off for the company. If a resource is set, the time off is only for this resource") |
| 50 |
time_type = fields.Selection([('leave', 'Time Off'), ('other', 'Other')], default='leave', |
| 51 |
help="Whether this should be computed as a time off or as work time (eg: formation)") |
| 52 |
|
| 53 |
@api.depends('resource_id.calendar_id') |
| 54 |
def _compute_calendar_id(self): |
| 55 |
for leave in self.filtered('resource_id'): |
| 56 |
leave.calendar_id = leave.resource_id.calendar_id |
| 57 |
|
| 58 |
@api.depends('calendar_id') |
| 59 |
def _compute_company_id(self): |
| 60 |
for leave in self: |
| 61 |
leave.company_id = leave.calendar_id.company_id or self.env.company |
| 62 |
|
| 63 |
@api.depends('date_from') |
| 64 |
def _compute_date_to(self): |
| 65 |
user_tz = self.env.tz |
| 66 |
if not (self.env.user.tz or self.env.context.get('tz')): |
| 67 |
user_tz = timezone(self.company_id.resource_calendar_id.tz or 'UTC') |
| 68 |
for leave in self: |
| 69 |
if not leave.date_from or (leave.date_to and leave.date_to > leave.date_from): |
| 70 |
continue |
| 71 |
local_date_from = utc.localize(leave.date_from).astimezone(user_tz) |
| 72 |
local_date_to = local_date_from + relativedelta(hour=23, minute=59, second=59) |
| 73 |
leave.date_to = local_date_to.astimezone(utc).replace(tzinfo=None) |
| 74 |
|
| 75 |
@api.constrains('date_from', 'date_to') |
| 76 |
def check_dates(self): |
| 77 |
if self.filtered(lambda leave: leave.date_from > leave.date_to): |
| 78 |
raise ValidationError(_('The start date of the time off must be earlier than the end date.')) |
| 79 |
|
| 80 |
def _copy_leave_vals(self): |
| 81 |
self.ensure_one() |
| 82 |
return { |
| 83 |
'name': self.name, |
| 84 |
'date_from': self.date_from, |
| 85 |
'date_to': self.date_to, |
| 86 |
'time_type': self.time_type, |
| 87 |
} |