2016-06-25 18:06:40 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2016 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/>.
|
|
|
|
|
2016-07-07 17:16:43 +02:00
|
|
|
from openerp import models, fields, api, _
|
2016-06-25 18:06:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ResPartner(models.Model):
|
|
|
|
_inherit = 'res.partner'
|
|
|
|
|
2016-07-08 19:27:01 +02:00
|
|
|
@api.model
|
2016-06-25 18:06:40 +02:00
|
|
|
def _get_default_nationality_id(self):
|
|
|
|
return self.env.ref('base.main_company').country_id
|
|
|
|
|
2016-06-27 22:21:19 +02:00
|
|
|
nationality_id = fields.Many2one(default=_get_default_nationality_id)
|
2016-07-08 19:27:01 +02:00
|
|
|
country_id = fields.Many2one(default=_get_default_nationality_id)
|
2016-06-25 18:06:40 +02:00
|
|
|
|
|
|
|
# Gender overwriting : no need for 'other' choice
|
2016-07-07 11:33:56 +02:00
|
|
|
gender = fields.Selection([('male', _('Male')), ('female', _('Female'))])
|
2016-06-25 18:06:40 +02:00
|
|
|
|
2016-07-08 17:15:48 +02:00
|
|
|
member_id = fields.One2many('golem.member', 'partner_id', 'GOLEM Member',
|
|
|
|
readonly=True)
|
2016-07-08 18:23:04 +02:00
|
|
|
member_number = fields.Char('Member number', related='member_id.number')
|
2016-07-08 17:15:48 +02:00
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def create_golem_member(self):
|
|
|
|
self.ensure_one()
|
|
|
|
gm = self.env['golem.member']
|
|
|
|
gm.create({'partner_id': self.id})
|
|
|
|
return True
|
|
|
|
|
2016-06-25 18:06:40 +02:00
|
|
|
|
|
|
|
class GolemMember(models.Model):
|
|
|
|
_name = 'golem.member'
|
2016-06-27 12:37:15 +02:00
|
|
|
_description = 'GOLEM Member'
|
2016-06-25 18:06:40 +02:00
|
|
|
_inherit = 'mail.thread'
|
|
|
|
_inherits = {'res.partner': 'partner_id'}
|
|
|
|
|
2016-07-08 21:49:21 +02:00
|
|
|
partner_id = fields.Many2one('res.partner', required=True,
|
|
|
|
ondelete='cascade')
|
|
|
|
|
2016-07-08 16:12:12 +02:00
|
|
|
@api.model
|
|
|
|
def _default_season(self):
|
|
|
|
""" Get default season """
|
|
|
|
domain = [('is_default', '=', True)]
|
|
|
|
return self.env['golem.season'].search(domain)
|
|
|
|
|
|
|
|
number = fields.Char('Member number', store=True, readonly=True,
|
|
|
|
compute='_compute_number')
|
|
|
|
number_manual = fields.Char('Manual number', size=50, index=True,
|
|
|
|
help='Manual number overwriting automatic '
|
|
|
|
'numbering')
|
2016-06-25 18:06:40 +02:00
|
|
|
pictures_agreement = fields.Boolean('Pictures agreement?')
|
2016-06-25 22:07:59 +02:00
|
|
|
opt_out_sms = fields.Boolean('Out of SMS campaigns')
|
2016-07-08 16:12:12 +02:00
|
|
|
season_ids = fields.Many2many('golem.season', string='Seasons',
|
|
|
|
required=True, auto_join=True,
|
2016-07-11 07:25:09 +02:00
|
|
|
default=_default_season,
|
|
|
|
ondelete='restrict')
|
2016-07-08 16:12:12 +02:00
|
|
|
is_current = fields.Boolean('Current user?', default=False, readonly=True,
|
|
|
|
store=True, compute='_compute_is_current')
|
|
|
|
is_number_manual = fields.Boolean('Is number manual?', store=False,
|
|
|
|
compute='_compute_is_number_manual')
|
|
|
|
|
|
|
|
_sql_constraints = [('golem_member_number_manual_uniq',
|
|
|
|
'UNIQUE (number_manual)',
|
|
|
|
_('This member number has already been used.'))]
|
|
|
|
|
|
|
|
@api.depends('number_manual', 'season_ids')
|
|
|
|
def _compute_number(self):
|
|
|
|
""" Computes number according to pre-existing number and chosen
|
|
|
|
seasons, or sets as manual """
|
|
|
|
for member in self:
|
|
|
|
conf = self.env['ir.config_parameter']
|
|
|
|
if conf.get_param('golem_numberconfig_isautomatic') == '0':
|
|
|
|
member.number = member.number_manual
|
|
|
|
else:
|
|
|
|
if member.id:
|
|
|
|
member.number = ''
|
|
|
|
if conf.get_param('golem_numberconfig_isperseason') == '1':
|
|
|
|
for s in member.season_ids:
|
|
|
|
domain = ['&',
|
|
|
|
('member_id', '=', member.id),
|
|
|
|
('season_id', '=', s.id)]
|
|
|
|
member_num = self.env['golem.member.number']
|
|
|
|
mn = member_num.search(domain)
|
|
|
|
if not mn:
|
|
|
|
s.member_counter += 1
|
|
|
|
s.write({'member_counter': s.member_counter})
|
|
|
|
pkey = 'golem_numberconfig_prefix'
|
|
|
|
pfx = conf.get_param(pkey)
|
|
|
|
number = pfx + str(s.member_counter)
|
|
|
|
data = {'member_id': member.id,
|
|
|
|
'season_id': s.id,
|
|
|
|
'number': number}
|
|
|
|
mn = member_num.create(data)
|
|
|
|
if s.is_default:
|
|
|
|
member.number = mn.number
|
|
|
|
else:
|
|
|
|
domain = ['&',
|
|
|
|
('member_id', '=', member.id),
|
|
|
|
('season_id', '=', None)]
|
|
|
|
member_num = self.env['golem.member.number']
|
|
|
|
mn = member_num.search(domain)
|
|
|
|
if not mn:
|
|
|
|
last = int(conf.get_param('golem_number_counter',
|
|
|
|
0))
|
|
|
|
last += 1
|
|
|
|
conf.set_param('golem_number_counter', str(last))
|
|
|
|
pfx = conf.get_param('golem_numberconfig_prefix')
|
|
|
|
number = pfx + str(last)
|
|
|
|
data = {'member_id': member.id,
|
|
|
|
'season_id': None,
|
|
|
|
'number': number}
|
|
|
|
mn = member_num.create(data)
|
|
|
|
member.number = mn.number
|
2016-07-07 17:16:43 +02:00
|
|
|
|
|
|
|
@api.depends('season_ids')
|
|
|
|
def _compute_is_current(self):
|
2016-07-08 16:12:12 +02:00
|
|
|
""" Computes is current according to seasons """
|
|
|
|
default_s = self._default_season()
|
|
|
|
for m in self:
|
|
|
|
m.is_current = default_s in m.season_ids
|
|
|
|
|
|
|
|
@api.depends('lastname')
|
|
|
|
def _compute_is_number_manual(self):
|
|
|
|
conf = self.env['ir.config_parameter']
|
|
|
|
is_num_man = (conf.get_param('golem_numberconfig_isautomatic') == '0')
|
|
|
|
self.is_number_manual = is_num_man
|
|
|
|
|
|
|
|
|
|
|
|
class GolemMemberSeason(models.Model):
|
|
|
|
""" GOLEM Member Numbers """
|
|
|
|
_name = 'golem.member.number'
|
|
|
|
_description = 'GOLEM Member Numbers'
|
|
|
|
|
|
|
|
name = fields.Char('Name', compute='_compute_name')
|
|
|
|
member_id = fields.Many2one('golem.member', string='Member', index=True,
|
|
|
|
required=True, ondelete='cascade',
|
|
|
|
auto_join=True)
|
|
|
|
season_id = fields.Many2one('golem.season', string='Season', index=True,
|
|
|
|
auto_join=True)
|
|
|
|
number = fields.Char('Number', index=True, readonly=True)
|
|
|
|
|
|
|
|
@api.depends('season_id')
|
|
|
|
def _compute_name(self):
|
|
|
|
for row in self:
|
|
|
|
row.name = row.season_id.name
|
|
|
|
|
|
|
|
|
|
|
|
class GolemNumberConfig(models.TransientModel):
|
|
|
|
""" Configuration for number computing """
|
|
|
|
_name = 'golem.member.numberconfig'
|
|
|
|
_description = 'Configuration for number computing'
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _default_is_automatic(self):
|
|
|
|
conf = self.env['ir.config_parameter']
|
|
|
|
return conf.get_param('golem_numberconfig_isautomatic', '1')
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _default_is_per_season(self):
|
|
|
|
conf = self.env['ir.config_parameter']
|
|
|
|
return conf.get_param('golem_numberconfig_isperseason', '0')
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _default_prefix(self):
|
|
|
|
conf = self.env['ir.config_parameter']
|
|
|
|
return conf.get_param('golem_numberconfig_prefix', '')
|
|
|
|
|
|
|
|
is_automatic = fields.Selection([('1', _('Yes')), ('0', _('No'))],
|
|
|
|
string='Computed automatically?',
|
|
|
|
default=_default_is_automatic)
|
|
|
|
is_per_season = fields.Selection([('1', _('Yes')), ('0', _('No'))],
|
|
|
|
string='Per season number?',
|
|
|
|
default=_default_is_per_season)
|
|
|
|
prefix = fields.Char('Optional prefix', default=_default_prefix)
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def apply_config(self):
|
|
|
|
self.ensure_one()
|
|
|
|
conf = self.env['ir.config_parameter']
|
|
|
|
conf.set_param('golem_numberconfig_isautomatic', self.is_automatic)
|
|
|
|
conf.set_param('golem_numberconfig_isperseason', self.is_per_season)
|
|
|
|
conf.set_param('golem_numberconfig_prefix', self.prefix or '')
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def apply_recompute(self):
|
|
|
|
self.ensure_one()
|
|
|
|
self.apply_config()
|
|
|
|
conf = self.env['ir.config_parameter']
|
|
|
|
conf.set_param('golem_number_counter', '0')
|
|
|
|
self.env['golem.member.number'].search([]).unlink()
|
|
|
|
self.env['golem.season'].search([]).write({'member_counter': 0})
|
|
|
|
self.env['golem.member'].search([])._compute_number()
|