2016-10-10 11:28:28 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-07-31 19:59:56 +02:00
|
|
|
# Copyright 2017-2018 Fabien Bourgeois <fabien@yaltik.com>
|
2016-10-10 11:28:28 +02:00
|
|
|
#
|
2018-07-31 19:59:56 +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.
|
2016-10-10 11:28:28 +02:00
|
|
|
#
|
2018-07-31 19:59:56 +02:00
|
|
|
# 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.
|
2016-10-10 11:28:28 +02:00
|
|
|
#
|
2018-07-31 19:59:56 +02:00
|
|
|
# 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/>.
|
2016-10-10 11:28:28 +02:00
|
|
|
|
2017-05-01 23:43:59 +02:00
|
|
|
""" GOLEM Activity Registration State """
|
2016-10-10 11:28:28 +02:00
|
|
|
|
2018-08-01 13:44:00 +02:00
|
|
|
from odoo import models, fields, api, _
|
2018-09-26 11:44:24 +02:00
|
|
|
from odoo.exceptions import UserError, ValidationError
|
2016-10-10 11:28:28 +02:00
|
|
|
|
|
|
|
class GolemMember(models.Model):
|
2017-05-01 23:43:59 +02:00
|
|
|
""" GOLEM Member adaptations """
|
2016-10-10 11:28:28 +02:00
|
|
|
_inherit = 'golem.member'
|
|
|
|
|
|
|
|
has_draft_registrations = fields.Boolean(
|
|
|
|
'Has draft registrations ?',
|
2017-05-01 23:43:59 +02:00
|
|
|
compute='_compute_has_draft_reg')
|
2016-10-10 11:28:28 +02:00
|
|
|
|
2018-07-31 19:59:56 +02:00
|
|
|
@api.depends('activity_registration_ids', 'activity_registration_ids.state')
|
2017-05-01 23:43:59 +02:00
|
|
|
def _compute_has_draft_reg(self):
|
2016-10-10 11:28:28 +02:00
|
|
|
""" Check if there are draft states in member activities """
|
2017-05-01 23:43:59 +02:00
|
|
|
for member in self:
|
2018-07-31 19:59:56 +02:00
|
|
|
regis = member.activity_registration_ids
|
|
|
|
regis = regis.filtered(lambda r: r.state == 'draft')
|
|
|
|
member.has_draft_registrations = bool(len(regis))
|
2016-10-10 11:28:28 +02:00
|
|
|
|
2017-05-01 23:43:59 +02:00
|
|
|
@api.multi
|
2016-10-10 11:28:28 +02:00
|
|
|
def do_validate_registrations(self):
|
|
|
|
""" Validate all draft registrations """
|
2017-06-14 23:50:35 +02:00
|
|
|
self.ensure_one()
|
|
|
|
member = self[0]
|
2018-07-31 19:59:56 +02:00
|
|
|
member.activity_registration_ids.filtered(
|
|
|
|
lambda r: r.state == 'draft'
|
|
|
|
).write({'state': 'confirmed'})
|
2016-10-10 11:28:28 +02:00
|
|
|
|
|
|
|
|
2016-10-16 11:09:29 +02:00
|
|
|
class GolemActivity(models.Model):
|
2017-05-01 23:43:59 +02:00
|
|
|
""" GOLEM Activity adaptations """
|
2016-10-16 11:09:29 +02:00
|
|
|
_inherit = 'golem.activity'
|
2016-10-10 11:28:28 +02:00
|
|
|
|
2017-05-01 23:43:59 +02:00
|
|
|
@api.multi
|
2016-10-16 11:09:29 +02:00
|
|
|
@api.depends('activity_registration_ids')
|
2017-05-01 23:43:59 +02:00
|
|
|
def compute_places_used(self):
|
|
|
|
""" Computes used places """
|
|
|
|
for activity in self:
|
|
|
|
rids = activity.activity_registration_ids
|
|
|
|
activity.places_used = len(rids.filtered(lambda r: r.state == 'confirmed'))
|
2016-10-10 11:28:28 +02:00
|
|
|
|
|
|
|
|
2016-10-16 11:09:29 +02:00
|
|
|
class GolemActivityRegistration(models.Model):
|
2017-05-01 23:43:59 +02:00
|
|
|
""" GOLEM Activity Registration adaptations """
|
2016-10-16 11:09:29 +02:00
|
|
|
_inherit = 'golem.activity.registration'
|
2016-10-10 11:28:28 +02:00
|
|
|
|
|
|
|
state = fields.Selection([('draft', 'Draft'), ('confirmed', 'Confirmed'),
|
|
|
|
('canceled', 'Canceled')], required=True,
|
|
|
|
default='draft')
|
|
|
|
|
2018-08-01 13:44:00 +02:00
|
|
|
@api.multi
|
|
|
|
def state_draft(self):
|
|
|
|
""" Set registration to state draft """
|
|
|
|
self.write({'state': 'draft'})
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def state_confirm(self):
|
|
|
|
""" Set registration to state confirmed """
|
|
|
|
if self.filtered(lambda r: r.state == 'canceled'):
|
|
|
|
uerr = _('You can not confirm a canceled registration.')
|
|
|
|
raise UserError(uerr)
|
|
|
|
self.write({'state': 'confirmed'})
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def state_cancel(self):
|
|
|
|
""" Set registration to state canceled """
|
|
|
|
self.write({'state': 'canceled'})
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def state_remove(self):
|
|
|
|
""" Remove registrations, only if canceled """
|
|
|
|
if self.filtered(lambda r: r.state != 'canceled'):
|
2018-09-26 11:44:24 +02:00
|
|
|
uerr = _('You can not remove a uncanceled registration.')
|
2018-08-01 13:44:00 +02:00
|
|
|
raise UserError(uerr)
|
|
|
|
self.unlink()
|
2018-09-26 11:44:24 +02:00
|
|
|
return {'type': 'ir.actions.client', 'tag': 'reload'}
|
2018-08-01 13:44:00 +02:00
|
|
|
|
2016-10-10 11:28:28 +02:00
|
|
|
@api.multi
|
|
|
|
def write(self, values):
|
|
|
|
""" Recomputes values linked to registrations when state change """
|
2016-10-16 11:09:29 +02:00
|
|
|
res = super(GolemActivityRegistration, self).write(values)
|
2017-06-15 01:14:20 +02:00
|
|
|
if values.get('state'):
|
2017-05-01 23:43:59 +02:00
|
|
|
for registration in self:
|
|
|
|
registration.activity_id.compute_places_used()
|
2016-10-10 11:28:28 +02:00
|
|
|
return res
|