diff --git a/golem_resource_holiday/models/__init__.py b/golem_resource_holiday/models/__init__.py index be75a73..fc1cacc 100644 --- a/golem_resource_holiday/models/__init__.py +++ b/golem_resource_holiday/models/__init__.py @@ -18,4 +18,5 @@ from . import ( golem_resource_holiday, golem_resource_holiday_period, - golem_resource, golem_resource_holiday_period_selection) + golem_resource, golem_resource_holiday_selection, + golem_resource_reservation) diff --git a/golem_resource_holiday/models/golem_resource.py b/golem_resource_holiday/models/golem_resource.py index cf9f619..95a51ae 100644 --- a/golem_resource_holiday/models/golem_resource.py +++ b/golem_resource_holiday/models/golem_resource.py @@ -18,11 +18,40 @@ """ GOLEM Resources Adaptations """ -from odoo import models, fields +from odoo import models, fields, api, _ class GolemResource(models.Model): """ GOLEM Resource Model """ _inherit = 'golem.resource' - holiday_period_selection = fields.One2many('golem.resource.holiday.period.selection', 'resource_id') + holiday_period_ids = fields.Many2many('golem.resource.holiday.period', string='Holiday period list') + + holiday_selection_ids = fields.One2many('golem.resource.holiday.selection', 'resource_id', + string='Holiday selection') + + @api.constrains('holiday_period_ids') + def synchronise_holiday_selection(self): + """ Synchronise holiday selection """ + for resource in self: + period_ids = resource.holiday_period_ids.ids + + #compute selections 2 remove from list if any + self.env['golem.resource.holiday.selection'].search([ + ('resource_id', '=', resource.id), + ('period_id', 'not in', period_ids) + ]).unlink() + #selection_2_remove.unlink() + + #compute selection to add to list if any + if resource.holiday_period_ids: + existing_holiday = self.env['golem.resource.holiday.selection'].search([ + ('resource_id', '=', resource.id)]).mapped('holiday_id') + holiday_2_add = self.env['golem.resource.holiday'].search([ + ('period_id', 'in', period_ids)]) - existing_holiday + + for holiday in holiday_2_add: + self.env['golem.resource.holiday.selection'].create({ + 'holiday_id' : holiday.id, + 'resource_id': resource.id, + }) diff --git a/golem_resource_holiday/models/golem_resource_holiday.py b/golem_resource_holiday/models/golem_resource_holiday.py index 48b7829..0ddae15 100644 --- a/golem_resource_holiday/models/golem_resource_holiday.py +++ b/golem_resource_holiday/models/golem_resource_holiday.py @@ -35,13 +35,13 @@ class GolemResourceHoliday(models.Model): date_start = fields.Date('Holiday start') date_end = fields.Date('Holiday end') - @api.depends('period_id', 'date_start') + @api.depends('period_id', 'date_start', 'date_end') def _compute_name(self): """ Computes holiday name """ for holiday in self: - holiday.name = u'{}/{}/{}'.format(holiday.period_id.name, + holiday.name = u'{}/{}:{}'.format(holiday.period_id.name, holiday.date_start, - holiday.id) + holiday.date_end) @api.constrains('date_start', 'date_end') def _check_date_consistency(self): diff --git a/golem_resource_holiday/models/golem_resource_holiday_period_selection.py b/golem_resource_holiday/models/golem_resource_holiday_selection.py similarity index 66% rename from golem_resource_holiday/models/golem_resource_holiday_period_selection.py rename to golem_resource_holiday/models/golem_resource_holiday_selection.py index 9b2d40a..b303bd8 100644 --- a/golem_resource_holiday/models/golem_resource_holiday_period_selection.py +++ b/golem_resource_holiday/models/golem_resource_holiday_selection.py @@ -16,17 +16,18 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -""" GOLEM Resource Holiday Period Selection Management """ +""" GOLEM Resource Holiday Selection Management """ from odoo import models, fields, api, _ from odoo.exceptions import ValidationError -class GolemResourceHolidayPeriodSelection(models.Model): - """ GOLEM Resource Holiday Period Selection""" - _name = 'golem.resource.holiday.period.selection' - _description = 'GOLEM Resource Holiday Period Selection Managementl' +class GolemResourceHolidaySelection(models.Model): + """ GOLEM Resource Holiday Selection""" + _name = 'golem.resource.holiday.selection' + _description = 'GOLEM Resource Holiday Selection Management' - holiday_period_id = fields.Many2one('golem.resource.holiday.period', required=True) + holiday_id = fields.Many2one('golem.resource.holiday', 'Holiday', required=True) resource_id = fields.Many2one('golem.resource', required=True) - is_reservation_possible = fields.Boolean() + is_reservation_possible = fields.Boolean(string='Reservation during holiday') + period_id = fields.Many2one(related='holiday_id.period_id', store=True) diff --git a/golem_resource_holiday/models/golem_resource_reservation.py b/golem_resource_holiday/models/golem_resource_reservation.py new file mode 100644 index 0000000..ea041e7 --- /dev/null +++ b/golem_resource_holiday/models/golem_resource_reservation.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 Fabien Bourgeois +# Copyright 2020 Youssef El Ouahby +# +# 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 . + +""" GOLEM Resources Reservation Adaptations """ + +from odoo import models, fields, api, _ +from odoo.exceptions import ValidationError + + +class GolemResourceReservation(models.Model): + """ GOLEM Resource Reservation Model """ + _inherit = 'golem.resource.reservation' + + @api.constrains('resource_id', 'date_start', 'date_stop') + def check_resource_holiday_date(self): + for reservation in self: + resource_holiday = reservation.resource_id.holiday_selection_ids.filtered(lambda s: not s.is_reservation_possible) + if resource_holiday: + for holiday in resource_holiday: + if (holiday.holiday_id.date_start < reservation.date_start < holiday.holiday_id.date_end) or \ + (holiday.holiday_id.date_start < reservation.date_stop < holiday.holiday_id.date_end): + raise ValidationError(_('This resource cannot be reserved during the' + ' period between {} and {}, as there is a holiday, please choose ' + 'another date').format(holiday.holiday_id.date_start, + holiday.holiday_id.date_end + )) diff --git a/golem_resource_holiday/views/golem_resource_holiday_views.xml b/golem_resource_holiday/views/golem_resource_holiday_views.xml index 7403e71..adc0c09 100644 --- a/golem_resource_holiday/views/golem_resource_holiday_views.xml +++ b/golem_resource_holiday/views/golem_resource_holiday_views.xml @@ -42,7 +42,7 @@ along with this program. If not, see . - + @@ -78,7 +78,7 @@ along with this program. If not, see . . - - + + + - +