2018-02-07 01:44:48 +01:00
# -*- coding: utf-8 -*-
# Copyright 2017 Fabien Bourgeois <fabien@yaltik.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-02-08 23:21:33 +01:00
from odoo import models , fields , api , _ , exceptions
2018-02-10 00:05:36 +01:00
2018-02-07 10:16:12 +01:00
#modèle de base : ressources
2018-02-07 01:44:48 +01:00
class GolemResources ( models . Model ) :
""" GOLEM Resources """
_name = ' golem.resources '
_description = ' GOLEM Resources '
2018-02-08 23:21:33 +01:00
name = fields . Char ( required = True )
active = fields . Boolean ( default = True )
2018-02-13 14:00:16 +01:00
validation_required = fields . Boolean ( default = True )
2018-02-08 23:21:33 +01:00
resource_type = fields . Many2one ( " golem.resourcetype " )
resource_responsible = fields . Many2one ( " res.partner " )
article_link = fields . Many2one ( " product.template " )
2018-02-07 10:16:12 +01:00
#Configuration de disponibilité(période, jours et horaire)
2018-02-08 23:21:33 +01:00
start_of_availability_date = fields . Date ( required = True )
end_of_availability_date = fields . Date ( required = True )
2018-02-08 00:58:23 +01:00
timetable = fields . One2many ( " golem.timetable " , " resource_id " , string = " Availibility timetable " )
2018-02-10 00:05:36 +01:00
reservation = fields . One2many ( " golem.reservation " , " linked_resource " )
2018-02-07 18:13:02 +01:00
@api.multi
def active_change ( self ) :
self . active = not self . active
2018-02-07 01:44:48 +01:00
2018-02-07 10:16:12 +01:00
#modèle gestion des reservation
2018-02-07 01:44:48 +01:00
class GolemReservation ( models . Model ) :
""" GOLEM Reservation """
_name = ' golem.reservation '
_description = ' GOLEM Reservation '
2018-02-08 23:21:33 +01:00
start_date = fields . Datetime ( )
end_date = fields . Datetime ( )
linked_resource = fields . Many2one ( ' golem.resources ' , required = True )
2018-02-12 16:05:52 +01:00
user = fields . Many2one ( ' res.users ' , required = True , default = lambda self : self . env . user )
on_behalf_of = fields . Many2one ( ' res.partner ' , required = True , default = lambda self : self . env [ ' res.partner ' ] )
2018-02-10 00:05:36 +01:00
#statut=fields.Char()
2018-02-07 01:44:48 +01:00
status = fields . Selection ( [
( ' draft ' , " Draft " ) ,
( ' confirmed ' , " Confirmed " ) ,
( ' canceled ' , " Canceled " ) ,
2018-02-13 14:00:16 +01:00
( ' validated ' , " Validated " ) ,
( ' rejected ' , " Rejected " ) ,
2018-02-07 01:44:48 +01:00
] , default = ' draft ' )
@api.multi
def status_draft ( self ) :
self . status = ' draft '
@api.multi
def status_confirm ( self ) :
2018-02-10 00:05:36 +01:00
self . status = ' confirmed '
2018-02-07 01:44:48 +01:00
@api.multi
def status_canceled ( self ) :
self . status = ' canceled '
2018-02-08 23:21:33 +01:00
@api.constrains ( ' status ' )
def _onConfirmReservation ( self ) :
2018-02-10 00:05:36 +01:00
if self . status == ' confirmed ' :
2018-02-12 16:05:52 +01:00
#verifyin is the reservation is taking place out of the resource availibility period
2018-02-10 00:05:36 +01:00
if ( self . start_date < self . linked_resource . start_of_availability_date or self . end_date > self . linked_resource . end_of_availability_date ) :
raise exceptions . UserError ( ' Not allowed, the resource is not available in this period, please choose another périod before confirming %s ' % self . linked_resource . start_of_availability_date )
else :
2018-02-12 16:05:52 +01:00
#verifying if the reservation is taking place out the availibility timetable
#defining a boolean flag, which will determine if the day of the reservation is available
r_allowed = False
for day in self . linked_resource . timetable :
#if the day is available, look for the time if it's inside the resource timetable availibility
if day . name . id_day == fields . Datetime . from_string ( self . start_date ) . weekday ( ) :
start_hour = fields . Datetime . from_string ( self . start_date ) . hour
start_min = float ( fields . Datetime . from_string ( self . start_date ) . minute ) #+(int(fields.Datetime.from_string(self.start_date).min))/100
start_time_r = start_hour + start_min / 100
start_hour = fields . Datetime . from_string ( self . end_date ) . hour
start_min = float ( fields . Datetime . from_string ( self . end_date ) . minute ) #+(int(fields.Datetime.from_string(self.start_date).min))/100
end_time_r = start_hour + start_min / 100
#if the time is suitable, the flag state is changed
if ( start_time_r > day . start_time and end_time_r < day . end_time ) :
r_allowed = True
#if the flag is changed no erreur is raised.
if ( not r_allowed ) :
raise exceptions . UserError ( " Not allowed, the resource is not available during this timetable, please choose another time before confirming " )
#verifying if the resource is already taken during this period
2018-02-10 00:05:36 +01:00
for reservation in self . linked_resource . reservation :
if ( self . id != reservation . id and reservation . status == ' confirmed ' and not ( self . end_date < reservation . start_date or self . start_date > reservation . end_date ) ) :
raise exceptions . UserError ( " Not allowed, the resource is taken during this period, please choose another période before confirming " )
2018-02-08 23:21:33 +01:00
2018-02-07 10:16:12 +01:00
#modèle de base pour identifier le type de la ressource
2018-02-07 01:44:48 +01:00
class GolemResourceType ( models . Model ) :
""" GOLEM Resource Type """
_name = ' golem.resourcetype '
_description = ' GOLEM Resource Type '
2018-02-08 23:21:33 +01:00
name = fields . Char ( string = ' Resource Type ' , required = True )
2018-02-07 01:44:48 +01:00
2018-02-07 10:16:12 +01:00
#modèle de base pour stocker les jours de la semaine
class GolemWeekDay ( models . Model ) :
""" GOLEM Week Day """
2018-02-07 01:44:48 +01:00
_name = ' golem.weekday '
_description = ' GOLEM Week Day '
name = fields . Char ( string = ' Week Day ' )
2018-02-12 16:05:52 +01:00
id_day = fields . Integer ( )
2018-02-07 18:13:02 +01:00
#modèle de gestion horaire
2018-02-08 00:58:23 +01:00
class GolemTimetable ( models . Model ) :
""" Golem Timetable """
_name = " golem.timetable "
_description = " Golem Timetable "
2018-02-07 18:13:02 +01:00
resource_id = fields . Many2one ( " golem.resources " , required = True )
name = fields . Many2one ( " golem.weekday " , required = True )
start_time = fields . Float ( required = True )
end_time = fields . Float ( required = True )
2018-02-08 12:02:46 +01:00
2018-02-08 12:22:52 +01:00
@api.constrains ( ' start_time ' , ' end_time ' )
2018-02-08 12:02:46 +01:00
def _check_time_consistency ( self ) :
2018-02-08 23:21:33 +01:00
if self . end_time < self . start_time :
raise exceptions . ValidationError ( _ ( ' End time should be higher than start time ' ) )