remplacement de date par datetime et mise à jours de la fonction de calcul de date_stop on change

This commit is contained in:
eloyoussef 2018-03-09 17:46:00 +01:00
parent b3705b95ef
commit 350efce581
2 changed files with 59 additions and 48 deletions

View File

@ -19,8 +19,11 @@
""" 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
import logging
_logger = logging.getLogger(__name__)
class GolemResourceReservation(models.Model): class GolemResourceReservation(models.Model):
@ -28,18 +31,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,
@ -69,46 +78,48 @@ class GolemResourceReservation(models.Model):
resource_reservation_count = fields.Integer(compute='_reservation_count') resource_reservation_count = fields.Integer(compute='_reservation_count')
@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 date after start date 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') @api.constrains('date_start', 'date_stop')
def _check_hour_consistency(self): def _check_date_consistency(self):
""" Checks hour consistency """ """ 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):
@ -157,10 +168,11 @@ class GolemResourceReservation(models.Model):
verr = _('You do not have permissions to validate or reject a reservation.') verr = _('You do not have permissions to validate or reject a reservation.')
raise ValidationError(verr) raise ValidationError(verr)
"""
@api.constrains('state') @api.constrains('state')
def check_confirmed(self): def check_confirmed(self):"""
""" Check date coherence on reservation confirmation """ """ Check date coherence on reservation confirmation """
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 < reservation.resource_id.avaibility_start or \
@ -203,6 +215,7 @@ class GolemResourceReservation(models.Model):
'please choose another périod before confirming.') 'please choose another périod before confirming.')
raise ValidationError(verr.format(other_res.date_start, raise ValidationError(verr.format(other_res.date_start,
other_res.date_stop)) other_res.date_stop))
"""
@api.multi @api.multi
def reservation_calendar(self): def reservation_calendar(self):
""" current resource reservation calendar """ """ current resource reservation calendar """

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>
@ -91,9 +90,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"