2018-02-18 14:10:30 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2018 Youssef El Ouahby <youssef@yaltik.com>
|
|
|
|
# Copyright 2018 Fabien Bourgeois <fabien@yaltik.com>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
""" GOLEM Resource Reservation """
|
|
|
|
|
|
|
|
from math import modf
|
2018-03-11 21:50:18 +01:00
|
|
|
from datetime import timedelta
|
2018-02-18 14:10:30 +01:00
|
|
|
from odoo import models, fields, api, _
|
2018-02-19 08:00:55 +01:00
|
|
|
from odoo.exceptions import ValidationError
|
2018-02-18 14:10:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
class GolemResourceReservation(models.Model):
|
|
|
|
""" GOLEM Resource Reservation Model """
|
|
|
|
_name = 'golem.resource.reservation'
|
|
|
|
_description = 'GOLEM Reservation Model'
|
2018-02-18 17:30:11 +01:00
|
|
|
_inherit = 'mail.thread'
|
2018-03-17 07:58:04 +01:00
|
|
|
_order = 'day_start desc, hour_start asc'
|
2018-02-18 14:10:30 +01:00
|
|
|
|
|
|
|
name = fields.Char(compute='_compute_name', store=True)
|
|
|
|
# TODO: handle multiple days reservation
|
2018-03-11 21:50:18 +01:00
|
|
|
date_start = fields.Datetime('Start date', required=True,
|
|
|
|
index=True, readonly=True,
|
|
|
|
states={'draft': [('readonly', False)]})
|
|
|
|
date_stop = fields.Datetime('Stop date', required=True,
|
|
|
|
index=True, readonly=True,
|
|
|
|
states={'draft': [('readonly', False)]})
|
2018-03-17 07:58:04 +01:00
|
|
|
day_start = fields.Date(compute='_compute_day_hour_start', store=True)
|
|
|
|
hour_start = fields.Float(compute='_compute_day_hour_start', store=True)
|
2018-02-18 14:10:30 +01:00
|
|
|
resource_id = fields.Many2one('golem.resource', required=True, index=True,
|
2018-02-18 16:41:01 +01:00
|
|
|
string='Resource', readonly=True,
|
2018-02-18 17:30:11 +01:00
|
|
|
track_visibility='onchange',
|
2018-02-18 16:41:01 +01:00
|
|
|
states={'draft': [('readonly', False)]})
|
2018-02-18 17:17:32 +01:00
|
|
|
resource_avaibility_start = fields.Date(related='resource_id.avaibility_start')
|
|
|
|
resource_avaibility_stop = fields.Date(related='resource_id.avaibility_stop')
|
2018-03-10 07:53:04 +01:00
|
|
|
resource_avaibility_24_7 = fields.Boolean(related='resource_id.availibility_24_7')
|
2018-02-18 17:17:32 +01:00
|
|
|
resource_timetable_ids = fields.One2many(related='resource_id.timetable_ids')
|
|
|
|
|
2018-03-09 08:44:17 +01:00
|
|
|
note = fields.Text(help='Notes, optional subject for the reservation, reason')
|
|
|
|
|
2018-02-18 16:41:01 +01:00
|
|
|
user_id = fields.Many2one('res.users', required=True, index=True, readonly=True,
|
|
|
|
string='User', default=lambda self: self.env.user,
|
|
|
|
states={'draft': [('readonly', False)]})
|
2018-02-18 14:10:30 +01:00
|
|
|
partner_id = fields.Many2one('res.partner', string='On behalf of',
|
2018-02-18 16:41:01 +01:00
|
|
|
required=True, index=True, readonly=True,
|
2018-02-18 17:30:11 +01:00
|
|
|
track_visibility='onchange',
|
2018-02-18 16:41:01 +01:00
|
|
|
states={'draft': [('readonly', False)]})
|
2018-02-18 17:30:11 +01:00
|
|
|
state = fields.Selection([('canceled', 'Canceled'),
|
|
|
|
('draft', 'Draft'),
|
|
|
|
('confirmed', 'Confirmed'),
|
|
|
|
('validated', 'Validated'),
|
|
|
|
('rejected', 'Rejected')],
|
|
|
|
default='draft', track_visibility='onchange')
|
2018-02-18 14:10:30 +01:00
|
|
|
|
2018-02-18 17:30:11 +01:00
|
|
|
rejection_reason = fields.Text(readonly=True, track_visibility='onchange')
|
2018-02-18 14:10:30 +01:00
|
|
|
|
2018-03-11 21:50:18 +01:00
|
|
|
@api.depends('resource_id', 'date_start')
|
2018-02-18 14:10:30 +01:00
|
|
|
def _compute_name(self):
|
|
|
|
""" Computes reservation name """
|
|
|
|
for reservation in self:
|
|
|
|
reservation.name = u'{}/{}'.format(reservation.resource_id.name,
|
2018-03-11 21:50:18 +01:00
|
|
|
reservation.date_start)
|
|
|
|
|
2018-03-17 07:58:04 +01:00
|
|
|
@api.depends('date_start')
|
|
|
|
def _compute_day_hour_start(self):
|
|
|
|
""" Computes Day and Hour Start : for better sorting """
|
|
|
|
for reservation in self:
|
|
|
|
if reservation.date_start:
|
|
|
|
date_start = fields.Datetime.from_string(reservation.date_start)
|
|
|
|
reservation.day_start = date_start.date().isoformat()
|
|
|
|
reservation.hour_start = date_start.hour + date_start.minute / 60.0
|
|
|
|
|
2018-03-11 21:50:18 +01:00
|
|
|
@api.onchange('date_start')
|
|
|
|
def onchange_date_start(self):
|
2018-02-18 16:41:01 +01:00
|
|
|
""" Propose automatically stop hour after start hour had been filled """
|
|
|
|
for reservation in self:
|
2018-03-11 21:50:18 +01:00
|
|
|
if reservation.date_start:
|
|
|
|
start = fields.Datetime.from_string(reservation.date_start)
|
|
|
|
duration = timedelta(hours=1)
|
|
|
|
reservation.date_stop = start + duration
|
|
|
|
|
2018-02-18 16:41:01 +01:00
|
|
|
|
2018-03-11 21:50:18 +01:00
|
|
|
@api.constrains('date_start', 'date_stop')
|
|
|
|
def _check_date_consistency(self):
|
|
|
|
""" Checks date consistency """
|
2018-02-18 16:41:01 +01:00
|
|
|
for reservation in self:
|
2018-03-11 21:50:18 +01:00
|
|
|
if reservation.date_stop <= reservation.date_start:
|
|
|
|
raise ValidationError(_('Stop date should be after start date'))
|
2018-02-18 14:10:30 +01:00
|
|
|
|
|
|
|
@api.multi
|
2018-02-18 16:41:01 +01:00
|
|
|
def state_draft(self):
|
2018-02-18 14:10:30 +01:00
|
|
|
""" Status to draft """
|
2018-02-18 16:41:01 +01:00
|
|
|
self.write({'state': 'draft'})
|
2018-02-18 14:10:30 +01:00
|
|
|
|
|
|
|
@api.multi
|
2018-02-18 16:41:01 +01:00
|
|
|
def state_confirm(self):
|
2018-02-18 14:10:30 +01:00
|
|
|
""" Confirms reservation, or validates it if not workflow is involved """
|
|
|
|
for reservation in self:
|
2018-02-18 16:41:01 +01:00
|
|
|
# Needed, for constraint checking
|
|
|
|
reservation.state = 'confirmed'
|
|
|
|
if not reservation.resource_id.validation_required:
|
|
|
|
reservation.state = 'validated'
|
2018-02-18 14:10:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
@api.multi
|
2018-02-18 16:41:01 +01:00
|
|
|
def state_canceled(self):
|
2018-02-18 14:10:30 +01:00
|
|
|
""" Status to cancel """
|
2018-02-18 16:41:01 +01:00
|
|
|
self.write({'state': 'canceled'})
|
2018-02-18 14:10:30 +01:00
|
|
|
|
|
|
|
@api.multi
|
2018-02-18 16:41:01 +01:00
|
|
|
def state_validated(self):
|
2018-02-18 14:10:30 +01:00
|
|
|
""" Status to validated """
|
2018-02-18 16:41:01 +01:00
|
|
|
self.write({'state': 'validated'})
|
2018-02-18 14:10:30 +01:00
|
|
|
|
|
|
|
@api.multi
|
2018-02-18 16:41:01 +01:00
|
|
|
def state_rejected(self):
|
2018-02-18 14:10:30 +01:00
|
|
|
""" Wizard call for reservation reject """
|
|
|
|
self.ensure_one()
|
|
|
|
reservation_id = self[0]
|
|
|
|
return {'name' : _('Please enter the rejection reason'),
|
|
|
|
'type' : 'ir.actions.act_window',
|
|
|
|
'res_model' : 'golem.reservation.rejection.wizard',
|
|
|
|
'context': {'default_reservation_id': reservation_id.id},
|
|
|
|
'view_mode': 'form',
|
|
|
|
'target': 'new'}
|
|
|
|
|
|
|
|
|
2018-02-18 16:41:01 +01:00
|
|
|
@api.constrains('state')
|
|
|
|
def check_access(self):
|
|
|
|
""" Checks access when state is updated """
|
|
|
|
reservation = self[0]
|
|
|
|
if reservation.state in ('rejected', 'validated'):
|
|
|
|
if not self.env.user.has_group('golem_base.group_golem_manager'):
|
2018-02-19 08:00:55 +01:00
|
|
|
verr = _('You do not have permissions to validate or reject a reservation.')
|
|
|
|
raise ValidationError(verr)
|
2018-02-18 16:41:01 +01:00
|
|
|
|
|
|
|
@api.constrains('state')
|
2018-02-18 14:10:30 +01:00
|
|
|
def check_confirmed(self):
|
|
|
|
""" Check date coherence on reservation confirmation """
|
|
|
|
for reservation in self:
|
2018-02-18 16:41:01 +01:00
|
|
|
if reservation.state == 'confirmed':
|
2018-02-18 14:10:30 +01:00
|
|
|
# Check is reservation is not taking place out of the resource avaibility period
|
2018-03-11 21:50:18 +01:00
|
|
|
if reservation.date_start < reservation.resource_id.avaibility_start or \
|
|
|
|
reservation.date_stop > reservation.resource_id.avaibility_stop:
|
2018-02-19 08:00:55 +01:00
|
|
|
verr = _('Not allowed, the resource is not available in '
|
2018-02-18 14:10:30 +01:00
|
|
|
'this period, please choose another périod before '
|
|
|
|
'confirming')
|
2018-02-19 08:00:55 +01:00
|
|
|
raise ValidationError(verr)
|
2018-03-09 14:30:58 +01:00
|
|
|
#check if the resource hasn't a total availibility
|
|
|
|
if not reservation.resource_id.availibility_24_7:
|
|
|
|
# Check if reservation is not taking place out the avaibility timetables
|
2018-03-11 21:50:18 +01:00
|
|
|
date_start = fields.Datetime.from_string(reservation.date_start)
|
|
|
|
date_stop = fields.Datetime.from_string(reservation.date_stop)
|
2018-03-12 12:48:09 +01:00
|
|
|
reservation_period = [date_start + timedelta(days=x) for x in range(
|
2018-03-17 08:39:43 +01:00
|
|
|
(date_stop - date_start).days + 1)]
|
2018-03-11 21:50:18 +01:00
|
|
|
for reservation_day in reservation_period:
|
|
|
|
is_day_allowed = False
|
|
|
|
for timetable in reservation.resource_id.timetable_ids:
|
|
|
|
# Check for the time according to resource timetable avaibility
|
|
|
|
#date = fields.Datetime.from_string(reservation_day)
|
|
|
|
if int(timetable.weekday) == reservation_day.weekday():
|
|
|
|
is_day_allowed = True
|
|
|
|
#only check if the day hasn't a 24 availibility
|
2018-03-17 08:39:43 +01:00
|
|
|
if not timetable.availibility_24:
|
|
|
|
reservation_day_date = reservation_day.date()
|
|
|
|
day_start = date_start.date()
|
|
|
|
day_stop = date_stop.date()
|
|
|
|
if reservation_day_date == day_start and \
|
|
|
|
reservation_day_date == day_stop:
|
|
|
|
hour_start = date_start.hour + date_start.minute / 60.0
|
|
|
|
hour_stop = date_stop.hour + date_stop.minute / 60.0
|
|
|
|
elif reservation_day_date == day_start:
|
|
|
|
hour_start = date_start.hour + date_start.minute / 60.0
|
|
|
|
hour_stop = 23.98 # Just before 23:59
|
|
|
|
elif reservation_day_date == day_stop:
|
2018-03-11 21:50:18 +01:00
|
|
|
hour_start = 0.0
|
2018-03-17 08:39:43 +01:00
|
|
|
hour_stop = date_stop.hour + date_stop.minute / 60.0
|
2018-03-12 12:48:09 +01:00
|
|
|
else:
|
2018-03-17 08:39:43 +01:00
|
|
|
#if the day is not a start nor stop it
|
|
|
|
#should be covered on all day
|
|
|
|
#strange, as availibility_24 is not True
|
|
|
|
hour_start = 0.0
|
|
|
|
hour_stop = 23.98
|
2018-03-11 21:50:18 +01:00
|
|
|
|
2018-03-12 12:48:09 +01:00
|
|
|
if is_day_allowed and (hour_start < timetable.time_start or \
|
|
|
|
hour_stop > timetable.time_stop):
|
2018-03-11 21:50:18 +01:00
|
|
|
verr = _('Not allowed, the resource is not available '
|
2018-03-12 15:34:43 +01:00
|
|
|
'during this period, please choose another '
|
2018-03-11 21:50:18 +01:00
|
|
|
'time before confirming.')
|
|
|
|
raise ValidationError(verr)
|
|
|
|
if not is_day_allowed:
|
|
|
|
verr = _('Not allowed, the resource is not available '
|
2018-03-17 08:39:43 +01:00
|
|
|
'this day : {}. Please choose another '
|
|
|
|
'date.'.format(reservation_day.strftime('%A')))
|
2018-03-11 21:50:18 +01:00
|
|
|
raise ValidationError(verr)
|
2018-02-18 14:10:30 +01:00
|
|
|
# Check if the resource is already taken during this period
|
2018-03-17 08:39:43 +01:00
|
|
|
# PERF : check for res that can be in conflict,
|
|
|
|
# do not iterate over all reservations
|
2018-02-18 14:10:30 +01:00
|
|
|
domain = [('resource_id', '=', reservation.resource_id.id),
|
2018-03-17 08:39:43 +01:00
|
|
|
('date_start', '<=', reservation.date_stop),
|
|
|
|
('date_stop', '>=', reservation.date_start),
|
2018-02-18 16:41:01 +01:00
|
|
|
('state', 'in', ('confirmed', 'validated')),
|
2018-02-18 14:10:30 +01:00
|
|
|
('id', '!=', reservation.id)]
|
|
|
|
reservations = self.env['golem.resource.reservation'].search(domain)
|
|
|
|
for other_res in reservations:
|
2018-03-11 21:50:18 +01:00
|
|
|
if (other_res.date_start < reservation.date_start < other_res.date_stop) or \
|
|
|
|
(other_res.date_start < reservation.date_stop < other_res.date_stop):
|
2018-02-19 08:00:55 +01:00
|
|
|
verr = _('Not allowed, the resource is already taken '
|
2018-02-18 14:10:30 +01:00
|
|
|
'during this period : from {} to {} this day, '
|
|
|
|
'please choose another périod before confirming.')
|
2018-02-19 08:00:55 +01:00
|
|
|
raise ValidationError(verr.format(other_res.date_start,
|
|
|
|
other_res.date_stop))
|