ajout du fonction de verification de conflit de réservation

This commit is contained in:
eloyoussef 2018-03-11 21:50:18 +01:00
parent 1004254314
commit e52c9028fa
4 changed files with 114 additions and 68 deletions

View File

@ -19,6 +19,7 @@
""" GOLEM Resource Reservation """
from math import modf
from datetime import timedelta
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
@ -28,18 +29,24 @@ class GolemResourceReservation(models.Model):
_name = 'golem.resource.reservation'
_description = 'GOLEM Reservation Model'
_inherit = 'mail.thread'
_order = 'date desc,hour_start asc'
_order = 'date_start desc'
name = fields.Char(compute='_compute_name', store=True)
# TODO: handle multiple days reservation
date = fields.Date(required=True, index=True, readonly=True,
states={'draft': [('readonly', False)]})
hour_start = fields.Float('Start hour', required=True, readonly=True,
states={'draft': [('readonly', False)]})
hour_stop = fields.Float('Stop hour', required=True, readonly=True,
states={'draft': [('readonly', False)]})
date_start = fields.Datetime(compute='_compute_date_start', store=True, index=True)
date_stop = fields.Datetime(compute='_compute_date_stop', store=True, index=True)
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)]})
#date = fields.Date(required=True, index=True, readonly=True,
# states={'draft': [('readonly', False)]})
#hour_start = fields.Float('Start hour', required=True, readonly=True,
# states={'draft': [('readonly', False)]})
#hour_stop = fields.Float('Stop hour', required=True, readonly=True,
# states={'draft': [('readonly', False)]})
#date_start = fields.Datetime(compute='_compute_date_start', store=True, index=True)
#date_stop = fields.Datetime(compute='_compute_date_stop', store=True, index=True)
resource_id = fields.Many2one('golem.resource', required=True, index=True,
string='Resource', readonly=True,
@ -68,46 +75,49 @@ class GolemResourceReservation(models.Model):
rejection_reason = fields.Text(readonly=True, track_visibility='onchange')
@api.depends('resource_id', 'date')
@api.depends('resource_id', 'date_start')
def _compute_name(self):
""" Computes reservation name """
for reservation in self:
reservation.name = u'{}/{}'.format(reservation.resource_id.name,
reservation.date)
reservation.date_start)
@api.depends('date', 'hour_start')
def _compute_date_start(self):
""" Computes Date start """
for reservation in self:
minute_start, hour_start = modf(reservation.hour_start)
hour_start = int(hour_start)
minute_start = int(round(minute_start * 60))
reservation.date_start = u'{} {}:{}:00'.format(reservation.date,
hour_start, minute_start)
#@api.depends('date', 'hour_start')
#def _compute_date_start(self):
# """ Computes Date start """
# for reservation in self:
# minute_start, hour_start = modf(reservation.hour_start)
# hour_start = int(hour_start)
# minute_start = int(round(minute_start * 60))
# reservation.date_start = u'{} {}:{}:00'.format(reservation.date,
# hour_start, minute_start)
@api.depends('date', 'hour_stop')
def _compute_date_stop(self):
""" Computes Date stop """
for reservation in self:
minute_stop, hour_stop = modf(reservation.hour_stop)
hour_stop = int(hour_stop)
minute_stop = int(round(minute_stop * 60))
reservation.date_stop = u'{} {}:{}:00'.format(reservation.date,
hour_stop, minute_stop)
#@api.depends('date', 'hour_stop')
#def _compute_date_stop(self):
# """ Computes Date stop """
# for reservation in self:
# minute_stop, hour_stop = modf(reservation.hour_stop)
# hour_stop = int(hour_stop)
# minute_stop = int(round(minute_stop * 60))
# reservation.date_stop = u'{} {}:{}:00'.format(reservation.date,
# hour_stop, minute_stop)
@api.onchange('hour_start')
def onchange_hour_start(self):
@api.onchange('date_start')
def onchange_date_start(self):
""" Propose automatically stop hour after start hour had been filled """
for reservation in self:
if reservation.hour_start and not reservation.hour_stop:
reservation.hour_stop = reservation.hour_start + 1
if reservation.date_start:
start = fields.Datetime.from_string(reservation.date_start)
duration = timedelta(hours=1)
reservation.date_stop = start + duration
@api.constrains('hour_start', 'hour_stop')
def _check_hour_consistency(self):
""" Checks hour consistency """
@api.constrains('date_start', 'date_stop')
def _check_date_consistency(self):
""" Checks date consistency """
for reservation in self:
if reservation.hour_stop <= reservation.hour_start:
raise ValidationError(_('End time should be after than start time'))
if reservation.date_stop <= reservation.date_start:
raise ValidationError(_('Stop date should be after start date'))
@api.multi
def state_draft(self):
@ -162,8 +172,8 @@ class GolemResourceReservation(models.Model):
for reservation in self:
if reservation.state == 'confirmed':
# Check is reservation is not taking place out of the resource avaibility period
if reservation.date < reservation.resource_id.avaibility_start or \
reservation.date > reservation.resource_id.avaibility_stop:
if reservation.date_start < reservation.resource_id.avaibility_start or \
reservation.date_stop > reservation.resource_id.avaibility_stop:
verr = _('Not allowed, the resource is not available in '
'this period, please choose another périod before '
'confirming')
@ -171,32 +181,51 @@ class GolemResourceReservation(models.Model):
#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
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.date)
if int(timetable.weekday) == date.weekday():
is_day_allowed = True
if reservation.hour_start < timetable.time_start or \
reservation.hour_stop > timetable.time_stop:
verr = _('Not allowed, the resource is not available '
'during this period, please choose another '
'time before confirming.')
raise ValidationError(verr)
if not is_day_allowed:
verr = _('Not allowed, the resource is not available '
'this day. Please choose another date.')
raise ValidationError(verr)
date_start = fields.Datetime.from_string(reservation.date_start)
date_stop = fields.Datetime.from_string(reservation.date_stop)
reservation_period = [date_start + timedelta(days=x) for x in range((date_stop - date_start).days +1)]
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
if not timetable.availibility_24:
hour_start = 0.0
hour_stop = 0.0
if (reservation_day == date_start and
reservation_day == date_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_start and
reservation_day != date_stop):
hour_start = date_start.hour + date_start.minute/60.0
hour_stop = 23.98
elif (reservation_day == date_stop and
reservation_day != date_start):
hour_start = 0.0
hour_stop = date_stop.hour + date_stop.minute/60.0
if hour_start < timetable.time_start or \
hour_stop > timetable.time_stop:
verr = _('Not allowed, the resource is not available '
'during this period, please choose another '
'time before confirming.')
raise ValidationError(verr)
if not is_day_allowed:
verr = _('Not allowed, the resource is not available '
'this day. Please choose another date.')
raise ValidationError(verr)
# Check if the resource is already taken during this period
# PERF : check the date, not iterate over all reservations
domain = [('resource_id', '=', reservation.resource_id.id),
('date', '=', reservation.date),
('state', 'in', ('confirmed', 'validated')),
('id', '!=', reservation.id)]
reservations = self.env['golem.resource.reservation'].search(domain)
for other_res in reservations:
if (other_res.hour_start < reservation.hour_start < other_res.hour_stop) or \
(other_res.hour_start < reservation.hour_stop < other_res.hour_stop):
if (other_res.date_start < reservation.date_start < other_res.date_stop) or \
(other_res.date_start < reservation.date_stop < other_res.date_stop):
verr = _('Not allowed, the resource is already taken '
'during this period : from {} to {} this day, '
'please choose another périod before confirming.')

View File

@ -39,6 +39,15 @@ class GolemTimetable(models.Model):
('6', _('Sunday'))], required=True)
time_start = fields.Float(required=True, string='Start')
time_stop = fields.Float(required=True, string='Stop')
availibility_24 = fields.Boolean(string="All day")
@api.onchange('availibility_24')
def _set_time_start_stop(self):
""" set start and stop time """
self.ensure_one()
if self.availibility_24:
self.time_start = 0.0
self.time_stop = 23.99
@api.onchange('time_start')
def onchange_time_start(self):
@ -53,3 +62,10 @@ class GolemTimetable(models.Model):
for timetable in self:
if timetable.time_stop <= timetable.time_start:
raise ValidationError(_('End time should be after than start time'))
@api.constrains('time_start', 'time_stop')
def _check_time_all_day(self):
""" Checks time all day availibility """
for timetable in self:
if timetable.time_stop > 23.98 and timetable.time_start == 0:
timetable.write({'availibility_24': True})

View File

@ -39,9 +39,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<field name="arch" type="xml">
<tree>
<field name="resource_id" />
<field name="date" />
<field name="hour_start" widget="float_time" />
<field name="hour_stop" widget="float_time" />
<field name="date_start" />
<field name="date_stop" />
<field name="partner_id" />
<field name="state" />
</tree>
@ -82,9 +81,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</group>
<group string="Reservation">
<group>
<field name="date" />
<field name="hour_start" widget="float_time" />
<field name="hour_stop" widget="float_time" />
<field name="date_start" />
<field name="date_stop" />
<field name="user_id" options="{'no_create': True}" />
<field name="partner_id" />
<field name="note"

View File

@ -27,8 +27,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<tree editable="bottom">
<field name="resource_id" invisible="1" />
<field name="weekday" />
<field name="time_start" string="Start hour" widget="float_time" />
<field name="time_stop" string="Stop hour" widget="float_time" />
<field name="availibility_24"/>
<field name="time_start" string="Start hour" widget="float_time"
attrs="{'readonly': [('availibility_24', '=', True)]}"/>
<field name="time_stop" string="Stop hour" widget="float_time"
attrs="{'readonly': [('availibility_24', '=', True)]}"/>
</tree>
</field>
</record>