2016-06-26 06:31:15 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-02-28 08:40:28 +01:00
|
|
|
# Copyright 2016-2018 Fabien Bourgeois <fabien@yaltik.com>
|
2016-06-26 06:31:15 +02:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2017-05-01 16:27:16 +02:00
|
|
|
""" GOLEM Season management """
|
2016-06-26 06:31:15 +02:00
|
|
|
|
2017-05-01 16:27:16 +02:00
|
|
|
from odoo import models, fields, api, _
|
2018-07-20 08:12:51 +02:00
|
|
|
from odoo.exceptions import ValidationError
|
2016-06-26 06:31:15 +02:00
|
|
|
|
|
|
|
class GolemSeason(models.Model):
|
|
|
|
""" GOLEM Season """
|
|
|
|
_name = 'golem.season'
|
|
|
|
_description = 'GOLEM Season'
|
|
|
|
_order = 'date_start desc'
|
2016-07-13 17:48:34 +02:00
|
|
|
_sql_constraints = [('golem_season_name_uniq', 'UNIQUE (name)',
|
|
|
|
_('This season name has already been used.'))]
|
2016-06-26 06:31:15 +02:00
|
|
|
|
2018-01-16 11:51:32 +01:00
|
|
|
name = fields.Char('Season name', copy=False, required=True)
|
2018-03-06 08:16:15 +01:00
|
|
|
membership_ids = fields.One2many('product.template', 'membership_season_id',
|
2018-02-28 08:40:28 +01:00
|
|
|
string='Membership types',
|
|
|
|
domain=[('membership', '=', True)])
|
2016-07-08 16:12:12 +02:00
|
|
|
member_counter = fields.Integer('Counter for member number generation',
|
2017-06-05 15:55:50 +02:00
|
|
|
readonly=True, default=1)
|
2016-06-26 06:31:15 +02:00
|
|
|
date_start = fields.Date('Period start')
|
|
|
|
date_end = fields.Date('Period end')
|
2018-07-20 08:12:51 +02:00
|
|
|
is_default = fields.Boolean('Default season for views?', readonly=True)
|
2016-06-26 06:31:15 +02:00
|
|
|
|
2018-02-28 08:40:28 +01:00
|
|
|
@api.onchange('membership_ids')
|
2018-01-11 14:51:18 +01:00
|
|
|
def _onchange_season_dates(self):
|
|
|
|
""" Sets defaults dates according to membership type """
|
2018-01-12 09:52:45 +01:00
|
|
|
for season in self:
|
2018-02-28 08:40:28 +01:00
|
|
|
if season.membership_ids and not season.date_start:
|
2018-01-17 15:44:43 +01:00
|
|
|
season.update({
|
2018-02-28 08:40:28 +01:00
|
|
|
'date_start': season.membership_ids[0].membership_date_from,
|
|
|
|
'date_end': season.membership_ids[0].membership_date_to
|
2018-01-17 15:44:43 +01:00
|
|
|
})
|
2018-01-11 14:51:18 +01:00
|
|
|
|
2016-06-26 06:31:15 +02:00
|
|
|
@api.constrains('date_start', 'date_end')
|
|
|
|
def _check_period(self):
|
|
|
|
""" Check if end date if after start date and if there is no conflict
|
|
|
|
with existing periods """
|
|
|
|
for season in self:
|
2018-01-17 11:13:49 +01:00
|
|
|
if season.date_start or season.date_end:
|
|
|
|
if season.date_start and not season.date_end:
|
2018-07-20 08:12:51 +02:00
|
|
|
raise ValidationError(_('The date end is required'))
|
2018-01-17 11:13:49 +01:00
|
|
|
elif season.date_end and not season.date_start:
|
2018-07-20 08:12:51 +02:00
|
|
|
raise ValidationError(_('The date start is required'))
|
2018-01-17 15:44:43 +01:00
|
|
|
if season.date_start > season.date_end:
|
2018-07-20 08:12:51 +02:00
|
|
|
raise ValidationError(_('Start of the period cannot be '
|
2018-07-20 10:09:24 +02:00
|
|
|
'after end of the period.'))
|
2018-01-18 15:27:35 +01:00
|
|
|
seasons = self.env['golem.season'].search([])
|
|
|
|
for eachs in seasons:
|
|
|
|
if eachs.date_start < season.date_start < eachs.date_end:
|
|
|
|
msg = _(u'Start of the period is in range of an '
|
|
|
|
'existing period.')
|
2018-07-20 08:12:51 +02:00
|
|
|
raise ValidationError(msg)
|
2018-01-18 15:27:35 +01:00
|
|
|
if eachs.date_start < season.date_end < eachs.date_end:
|
|
|
|
msg = _(u'End of the period is in range of an '
|
|
|
|
'existing period.')
|
2018-07-20 08:12:51 +02:00
|
|
|
raise ValidationError(msg)
|
2018-01-18 15:27:35 +01:00
|
|
|
if season.date_start < eachs.date_start < season.date_end:
|
|
|
|
msg = _(u'Current period cannot be included into '
|
|
|
|
'another existing period.')
|
2018-07-20 08:12:51 +02:00
|
|
|
raise ValidationError(msg)
|
2016-07-11 07:11:27 +02:00
|
|
|
|
2017-05-01 16:27:16 +02:00
|
|
|
@api.multi
|
2016-07-11 07:11:27 +02:00
|
|
|
def do_default_season(self):
|
2018-07-20 10:09:24 +02:00
|
|
|
""" is_default on and ensure that only one is_default is active """
|
2017-05-01 16:27:16 +02:00
|
|
|
self.ensure_one()
|
2016-07-07 17:16:43 +02:00
|
|
|
old_default_season = self.search([('is_default', '=', True)])
|
2016-07-11 20:14:53 +02:00
|
|
|
if old_default_season:
|
|
|
|
old_default_season.is_default = False
|
2016-07-11 07:11:27 +02:00
|
|
|
self.is_default = True
|
|
|
|
|
2016-07-13 17:43:35 +02:00
|
|
|
@api.model
|
|
|
|
def create(self, values):
|
|
|
|
""" If the season if the first one created, it must be by default """
|
|
|
|
if self.search_count([]) == 0:
|
|
|
|
values['is_default'] = True
|
|
|
|
return super(GolemSeason, self).create(values)
|
|
|
|
|
2016-07-11 07:23:58 +02:00
|
|
|
@api.multi
|
|
|
|
def unlink(self):
|
2016-07-13 17:43:35 +02:00
|
|
|
""" Forbids default season removal """
|
2017-05-01 16:27:16 +02:00
|
|
|
for season in self:
|
|
|
|
if season.is_default:
|
2016-07-11 07:23:58 +02:00
|
|
|
emsg = _('You can\'t delete the default season')
|
2018-07-20 08:12:51 +02:00
|
|
|
raise ValidationError(emsg)
|
2016-07-11 07:23:58 +02:00
|
|
|
else:
|
|
|
|
return super(GolemSeason, self).unlink()
|