Compare commits

...

3 Commits

6 changed files with 134 additions and 57 deletions

View File

@ -49,7 +49,10 @@ class GolemResource(models.Model):
reservation_ids = fields.One2many('golem.resource.reservation', 'resource_id',
string='Reservations')
#the resource is available in every hour, in every day during the availibility period
availibility_24_7 = fields.Boolean(string='24/7 availibility')
#the resource is available 24h during the chosen days
availibility_24_24 = fields.Boolean(string='24/24 availibility')
@api.multi
def active_toggle(self):
@ -64,3 +67,9 @@ class GolemResource(models.Model):
if resource.avaibility_stop <= resource.avaibility_start:
raise ValidationError(_('End availibility should be after than '
'start availibility'))
@api.onchange('availibility_24_24')
def _onchange_availibility_24_24(self):
for resource in self:
if resource.availibility_24_24:
resource.timetable_ids.write({'time_start': 0.0,
'time_stop': 23.99})

View File

@ -19,8 +19,11 @@
""" GOLEM Resource Reservation """
from math import modf
from datetime import timedelta
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
import logging
_logger = logging.getLogger(__name__)
class GolemResourceReservation(models.Model):
@ -28,18 +31,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,
date_start = fields.Datetime('Start date',required=True,
index=True, readonly=True,
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)]})
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 = 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,
@ -47,6 +56,8 @@ class GolemResourceReservation(models.Model):
states={'draft': [('readonly', False)]})
resource_avaibility_start = fields.Date(related='resource_id.avaibility_start')
resource_avaibility_stop = fields.Date(related='resource_id.avaibility_stop')
resource_availibility_24_7 = fields.Boolean(related='resource_id.availibility_24_7')
resource_availibility_24_24 = fields.Boolean(related='resource_id.availibility_24_24')
resource_timetable_ids = fields.One2many(related='resource_id.timetable_ids')
note = fields.Text(help='Notes, optional subject for the reservation, reason')
@ -69,46 +80,48 @@ class GolemResourceReservation(models.Model):
resource_reservation_count = fields.Integer(compute='_reservation_count')
@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):
""" Propose automatically stop hour after start hour had been filled """
@api.onchange('date_start')
def onchange_date_start(self):
""" Propose automatically stop date after start date 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):
@ -157,22 +170,71 @@ class GolemResourceReservation(models.Model):
verr = _('You do not have permissions to validate or reject a reservation.')
raise ValidationError(verr)
@api.constrains('state')
def check_confirmed(self):
""" Check date coherence on reservation confirmation """
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')
raise ValidationError(verr)
#check if it's a multidays reservation
res_date_start = fields.Datetime.from_string(reservation.date_start).date()
res_date_stop = fields.Datetime.from_string(reservation.date_stop).date()
multidays = res_date_start != res_date_stop
#check if the resource hasn't a total availibility
if not reservation.resource_id.availibility_24_7:
if not reservation.resource_availibility_24_7:
if reservation.resource_availibility_24_24 and multidays:
pass
elif reservation.resource_availibility_24_24:
# 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_start)
if int(timetable.weekday) == date.weekday():
is_day_allowed = True
if not is_day_allowed:
verr = _('Not allowed, the resource is not available '
'this day. Please choose another date.')
raise ValidationError(verr)
elif not reservation.resource_availibility_24_24 and multidays:
pass
else:
pass
# 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_start)
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)
# 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)
@ -187,7 +249,8 @@ class GolemResourceReservation(models.Model):
if not is_day_allowed:
verr = _('Not allowed, the resource is not available '
'this day. Please choose another date.')
raise ValidationError(verr)
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),
@ -202,7 +265,7 @@ class GolemResourceReservation(models.Model):
'during this period : from {} to {} this day, '
'please choose another périod before confirming.')
raise ValidationError(verr.format(other_res.date_start,
other_res.date_stop))
other_res.date_stop))"""
@api.multi
def reservation_calendar(self):
""" current resource reservation calendar """

View File

@ -30,6 +30,7 @@ class GolemTimetable(models.Model):
resource_id = fields.Many2one('golem.resource', required=True,
string='Linked resource')
availibility_24_24 = fields.Boolean(related="resource_id.availibility_24_24")
weekday = fields.Selection([('0', _('Monday')),
('1', _('Tuesday')),
('2', _('Wednesday')),
@ -37,8 +38,8 @@ class GolemTimetable(models.Model):
('4', _('Friday')),
('5', _('Saturday')),
('6', _('Sunday'))], required=True)
time_start = fields.Float(required=True, string='Start')
time_stop = fields.Float(required=True, string='Stop')
time_start = fields.Float(required=True, string='Start', default=0.0)
time_stop = fields.Float(required=True, string='Stop', default=23.99)
@api.onchange('time_start')
def onchange_time_start(self):

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>
@ -85,15 +84,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<group string="Resource">
<field name="id" invisible="1" />
<field name="resource_id" options="{'no_create': True}" />
<field name="resource_availibility_24_7" readonly="1" />
<field name="resource_avaibility_start" readonly="1" />
<field name="resource_avaibility_stop" readonly="1" />
<field name="resource_timetable_ids" readonly="1" />
<field name="resource_timetable_ids"
attrs="{'invisible': [('resource_availibility_24_7', '=', True)]}" readonly="1" />
</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="time_start" string="Start hour" widget="float_time"
attrs="{'readonly': [('availibility_24_24', '=', True)]}"/>
<field name="time_stop" string="Stop hour" widget="float_time"
attrs="{'readonly': [('availibility_24_24', '=', True)]}"/>
<field name="availibility_24_24" invisible='1'/>
</tree>
</field>
</record>

View File

@ -74,9 +74,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
Please save the resource before fixing the timetable availibility"
</p>
<group colspan="2" attrs="{'invisible': [('availibility_24_7', '=', True)]}">
<field name="availibility_24_24"/>
<field name="timetable_ids"
context="{'default_resource_id': active_id}"
attrs="{'readonly': [('id', '=', False)]}" />
attrs="{'readonly': [('id', '=', False)]}"/>
</group>
</group>
</sheet>