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

View File

@ -39,6 +39,15 @@ class GolemTimetable(models.Model):
('6', _('Sunday'))], required=True) ('6', _('Sunday'))], required=True)
time_start = fields.Float(required=True, string='Start') time_start = fields.Float(required=True, string='Start')
time_stop = fields.Float(required=True, string='Stop') 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') @api.onchange('time_start')
def onchange_time_start(self): def onchange_time_start(self):
@ -53,3 +62,10 @@ class GolemTimetable(models.Model):
for timetable in self: for timetable in self:
if timetable.time_stop <= timetable.time_start: if timetable.time_stop <= timetable.time_start:
raise ValidationError(_('End time should be after than start time')) 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"> <field name="arch" type="xml">
<tree> <tree>
<field name="resource_id" /> <field name="resource_id" />
<field name="date" /> <field name="date_start" />
<field name="hour_start" widget="float_time" /> <field name="date_stop" />
<field name="hour_stop" widget="float_time" />
<field name="partner_id" /> <field name="partner_id" />
<field name="state" /> <field name="state" />
</tree> </tree>
@ -82,9 +81,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</group> </group>
<group string="Reservation"> <group string="Reservation">
<group> <group>
<field name="date" /> <field name="date_start" />
<field name="hour_start" widget="float_time" /> <field name="date_stop" />
<field name="hour_stop" widget="float_time" />
<field name="user_id" options="{'no_create': True}" /> <field name="user_id" options="{'no_create': True}" />
<field name="partner_id" /> <field name="partner_id" />
<field name="note" <field name="note"

View File

@ -27,8 +27,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<tree editable="bottom"> <tree editable="bottom">
<field name="resource_id" invisible="1" /> <field name="resource_id" invisible="1" />
<field name="weekday" /> <field name="weekday" />
<field name="time_start" string="Start hour" widget="float_time" /> <field name="availibility_24"/>
<field name="time_stop" string="Stop hour" widget="float_time" /> <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> </tree>
</field> </field>
</record> </record>