From e52c9028fa45fde2584cb7cd825ea6a191be13c0 Mon Sep 17 00:00:00 2001 From: eloyoussef Date: Sun, 11 Mar 2018 21:50:18 +0100 Subject: [PATCH] =?UTF-8?q?ajout=20du=20fonction=20de=20verification=20de?= =?UTF-8?q?=20conflit=20de=20r=C3=A9servation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../models/golem_resource_reservation.py | 149 +++++++++++------- .../models/golem_resource_timetable.py | 16 ++ .../golem_resource_reservation_views.xml | 10 +- .../views/golem_resource_timetable_views.xml | 7 +- 4 files changed, 114 insertions(+), 68 deletions(-) diff --git a/golem_resource/models/golem_resource_reservation.py b/golem_resource/models/golem_resource_reservation.py index 79206e7..f323e7c 100644 --- a/golem_resource/models/golem_resource_reservation.py +++ b/golem_resource/models/golem_resource_reservation.py @@ -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.') diff --git a/golem_resource/models/golem_resource_timetable.py b/golem_resource/models/golem_resource_timetable.py index e3eb8c9..9597643 100644 --- a/golem_resource/models/golem_resource_timetable.py +++ b/golem_resource/models/golem_resource_timetable.py @@ -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}) diff --git a/golem_resource/views/golem_resource_reservation_views.xml b/golem_resource/views/golem_resource_reservation_views.xml index e542f0a..03c2063 100644 --- a/golem_resource/views/golem_resource_reservation_views.xml +++ b/golem_resource/views/golem_resource_reservation_views.xml @@ -39,9 +39,8 @@ along with this program. If not, see . - - - + + @@ -82,9 +81,8 @@ along with this program. If not, see . - - - + + . - - + + +