2016-06-25 18:06:40 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-10-25 14:12:53 +02:00
|
|
|
#
|
2018-07-17 08:26:49 +02:00
|
|
|
# Copyright 2016-2018 Fabien Bourgeois <fabien@yaltik.com>
|
2016-06-25 18:06:40 +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 19:17:53 +02:00
|
|
|
""" GOLEM Members """
|
|
|
|
|
2017-06-05 09:48:01 +02:00
|
|
|
import logging
|
2017-04-29 09:16:41 +02:00
|
|
|
from odoo import models, fields, api, _
|
2017-06-05 09:48:01 +02:00
|
|
|
from odoo.exceptions import UserError
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-06-25 18:06:40 +02:00
|
|
|
|
2018-02-12 17:45:34 +01:00
|
|
|
|
2016-06-25 18:06:40 +02:00
|
|
|
class ResPartner(models.Model):
|
2017-05-01 19:17:53 +02:00
|
|
|
""" GOLEM Member partner adaptations """
|
2016-06-25 18:06:40 +02:00
|
|
|
_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
|
|
|
|
|
2017-05-01 19:17:53 +02:00
|
|
|
nationality_id = fields.Many2one('res.country', 'Nationality',
|
|
|
|
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)
|
2018-07-17 08:26:49 +02:00
|
|
|
is_member = fields.Boolean(compute='_compute_is_member')
|
|
|
|
member_number = fields.Char(related='member_id.number')
|
2016-07-08 17:15:48 +02:00
|
|
|
|
2017-06-04 19:03:34 +02:00
|
|
|
@api.depends('member_id')
|
|
|
|
def _compute_is_member(self):
|
|
|
|
""" Computes is member """
|
|
|
|
for partner in self:
|
|
|
|
partner.is_member = len(partner.member_id) > 0
|
|
|
|
|
2017-06-20 16:38:24 +02:00
|
|
|
@api.multi
|
|
|
|
def view_member(self):
|
|
|
|
""" Go to member form """
|
|
|
|
self.ensure_one()
|
2018-07-17 08:26:49 +02:00
|
|
|
return {'type': 'ir.actions.act_window',
|
|
|
|
'res_model': 'golem.member',
|
|
|
|
'view_mode': 'form',
|
|
|
|
'res_id': self[0].member_id.id if self[0].member_id else False}
|
2017-06-04 19:03:34 +02:00
|
|
|
|
2016-07-08 17:15:48 +02:00
|
|
|
@api.multi
|
|
|
|
def create_golem_member(self):
|
2017-05-01 19:17:53 +02:00
|
|
|
""" Member creation from partner form """
|
2016-07-08 17:15:48 +02:00
|
|
|
self.ensure_one()
|
2017-05-01 19:17:53 +02:00
|
|
|
gm_obj = self.env['golem.member']
|
|
|
|
gm_obj.create({'partner_id': self[0].id})
|
2016-07-08 17:15:48 +02:00
|
|
|
|
2018-02-08 10:18:04 +01:00
|
|
|
|
2016-06-25 18:06:40 +02:00
|
|
|
class GolemMember(models.Model):
|
2017-05-01 19:17:53 +02:00
|
|
|
""" GOLEM Member model """
|
2016-06-25 18:06:40 +02:00
|
|
|
_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'}
|
2017-06-05 08:37:33 +02:00
|
|
|
_sql_constraints = [('golem_member_number_manual_uniq',
|
|
|
|
'UNIQUE (number_manual)',
|
|
|
|
_('This member number has already been used.'))]
|
2016-06-25 18:06:40 +02:00
|
|
|
|
2017-05-01 19:17:53 +02:00
|
|
|
partner_id = fields.Many2one('res.partner', required=True, index=True,
|
2016-07-08 21:49:21 +02:00
|
|
|
ondelete='cascade')
|
2018-01-25 14:26:59 +01:00
|
|
|
|
2016-07-08 16:12:12 +02:00
|
|
|
@api.model
|
|
|
|
def _default_season(self):
|
|
|
|
""" Get default season """
|
|
|
|
domain = [('is_default', '=', True)]
|
2017-05-01 19:17:53 +02:00
|
|
|
return self.env['golem.season'].search(domain, limit=1)
|
2016-07-08 16:12:12 +02:00
|
|
|
|
2017-06-05 08:46:41 +02:00
|
|
|
number_name = fields.Char('Member computed name', compute='_compute_number_name')
|
2016-08-02 17:08:37 +02:00
|
|
|
number = fields.Char('Member number', store=True, readonly=True)
|
2016-07-08 16:12:12 +02:00
|
|
|
number_manual = fields.Char('Manual number', size=50, index=True,
|
|
|
|
help='Manual number overwriting automatic '
|
|
|
|
'numbering')
|
2018-07-17 09:01:36 +02:00
|
|
|
pictures_agreement = fields.Boolean('Pictures agreement?', default=True)
|
|
|
|
electronic_processing_agreement = fields.Boolean('Electronic Processing Agreement?',
|
|
|
|
default=True)
|
2016-08-02 11:48:14 +02:00
|
|
|
opt_out_sms = fields.Boolean('Out of SMS campaigns?',
|
|
|
|
help='If this field has been checked, it '
|
|
|
|
'tells that the user refuses to receive SMS')
|
2016-07-08 16:12:12 +02:00
|
|
|
season_ids = fields.Many2many('golem.season', string='Seasons',
|
2016-07-22 07:29:16 +02:00
|
|
|
required=True, default=_default_season,
|
2016-07-11 07:25:09 +02:00
|
|
|
ondelete='restrict')
|
2016-07-08 16:12:12 +02:00
|
|
|
is_current = fields.Boolean('Current user?', default=False, readonly=True,
|
2018-07-17 08:26:49 +02:00
|
|
|
store=True, compute='_compute_is_current')
|
2016-07-08 16:12:12 +02:00
|
|
|
is_number_manual = fields.Boolean('Is number manual?', store=False,
|
|
|
|
compute='_compute_is_number_manual')
|
|
|
|
|
2017-06-05 10:02:38 +02:00
|
|
|
@api.onchange('country_id')
|
2018-07-17 08:26:49 +02:00
|
|
|
def onchange_country_domain_state(self):
|
|
|
|
""" On country change : adapts state domain """
|
2017-06-05 10:02:38 +02:00
|
|
|
member = self[0]
|
|
|
|
if member.country_id:
|
|
|
|
return {
|
|
|
|
'domain': {'state_id': [('country_id', '=', member.country_id.id)]}
|
|
|
|
}
|
2018-07-17 08:26:49 +02:00
|
|
|
return {'domain': {'state_id': []}}
|
2017-06-05 10:02:38 +02:00
|
|
|
|
2017-06-05 08:46:41 +02:00
|
|
|
@api.depends('number', 'name')
|
|
|
|
def _compute_number_name(self):
|
|
|
|
""" Computes a name composed with number and name """
|
|
|
|
for member in self:
|
2017-06-05 10:02:38 +02:00
|
|
|
if member.name:
|
|
|
|
member.number_name = u'{} - {}'.format(member.number, member.name)
|
|
|
|
else:
|
|
|
|
member.number_name = u''
|
2017-06-05 08:46:41 +02:00
|
|
|
|
2016-07-07 17:16:43 +02:00
|
|
|
@api.depends('season_ids')
|
2018-07-17 08:26:49 +02:00
|
|
|
def _compute_is_current(self):
|
2016-07-08 16:12:12 +02:00
|
|
|
""" Computes is current according to seasons """
|
|
|
|
default_s = self._default_season()
|
2017-05-01 19:17:53 +02:00
|
|
|
for member in self:
|
|
|
|
member.is_current = default_s in member.season_ids
|
2016-07-08 16:12:12 +02:00
|
|
|
|
2016-08-02 17:08:37 +02:00
|
|
|
@api.depends('number')
|
2016-07-08 16:12:12 +02:00
|
|
|
def _compute_is_number_manual(self):
|
|
|
|
conf = self.env['ir.config_parameter']
|
|
|
|
is_num_man = (conf.get_param('golem_numberconfig_isautomatic') == '0')
|
2017-06-05 15:55:50 +02:00
|
|
|
self.write({'is_number_manual': is_num_man})
|
2016-07-08 16:12:12 +02:00
|
|
|
|
2017-05-01 19:17:53 +02:00
|
|
|
@api.multi
|
|
|
|
def generate_number_perseason(self):
|
2016-08-02 17:08:37 +02:00
|
|
|
""" Number generation in case of per season configuration """
|
|
|
|
res = None
|
|
|
|
conf = self.env['ir.config_parameter']
|
2017-05-01 19:17:53 +02:00
|
|
|
member_number_obj = self.env['golem.member.number']
|
2016-08-02 17:08:37 +02:00
|
|
|
for member in self:
|
2017-05-01 19:17:53 +02:00
|
|
|
for season in member.season_ids:
|
2016-08-02 17:08:37 +02:00
|
|
|
domain = ['&',
|
|
|
|
('member_id', '=', member.id),
|
2017-05-01 19:17:53 +02:00
|
|
|
('season_id', '=', season.id)]
|
|
|
|
member_num = member_number_obj.search(domain)
|
|
|
|
if not member_num:
|
|
|
|
season.write({'member_counter': season.member_counter})
|
2016-08-02 17:08:37 +02:00
|
|
|
pkey = 'golem_numberconfig_prefix'
|
2017-05-01 22:14:29 +02:00
|
|
|
pfx = conf.get_param(pkey, '')
|
2017-06-05 15:55:50 +02:00
|
|
|
number = u'{}{}'.format(pfx, unicode(season.member_counter))
|
2016-08-02 17:08:37 +02:00
|
|
|
data = {'member_id': member.id,
|
2017-05-01 19:17:53 +02:00
|
|
|
'season_id': season.id,
|
2016-08-02 17:08:37 +02:00
|
|
|
'number': number}
|
2017-05-01 19:17:53 +02:00
|
|
|
member_num = member_number_obj.create(data)
|
2017-06-05 15:55:50 +02:00
|
|
|
season.member_counter += 1
|
2017-05-01 19:17:53 +02:00
|
|
|
if season.is_default:
|
|
|
|
res = member_num.number
|
2016-08-02 17:08:37 +02:00
|
|
|
return res
|
|
|
|
|
2017-05-01 19:17:53 +02:00
|
|
|
@api.multi
|
|
|
|
def generate_number_global(self):
|
2016-08-02 17:08:37 +02:00
|
|
|
""" Number generation in case of global configuration """
|
2016-08-02 19:33:58 +02:00
|
|
|
self.ensure_one()
|
|
|
|
conf = self.env['ir.config_parameter']
|
|
|
|
domain = ['&',
|
2017-05-01 19:17:53 +02:00
|
|
|
('member_id', '=', self[0].id),
|
2016-08-02 19:33:58 +02:00
|
|
|
('season_id', '=', None)]
|
2017-05-01 19:17:53 +02:00
|
|
|
member_number_obj = self.env['golem.member.number']
|
|
|
|
member_num = member_number_obj.search(domain)
|
|
|
|
if not member_num:
|
2017-06-05 15:55:50 +02:00
|
|
|
last = int(conf.get_param('golem_number_counter', 1))
|
2017-05-01 22:14:29 +02:00
|
|
|
pfx = conf.get_param('golem_numberconfig_prefix', '')
|
2016-08-02 19:33:58 +02:00
|
|
|
number = pfx + str(last)
|
2017-05-01 19:17:53 +02:00
|
|
|
data = {'member_id': self[0].id,
|
2016-08-02 19:33:58 +02:00
|
|
|
'season_id': None,
|
|
|
|
'number': number}
|
2017-05-01 19:17:53 +02:00
|
|
|
member_num = member_number_obj.create(data)
|
2017-06-05 15:55:50 +02:00
|
|
|
last += 1
|
|
|
|
conf.set_param('golem_number_counter', str(last))
|
|
|
|
else:
|
|
|
|
member_num = member_num[0]
|
2017-05-01 19:17:53 +02:00
|
|
|
return member_num.number
|
2016-08-02 17:08:37 +02:00
|
|
|
|
2017-05-01 19:17:53 +02:00
|
|
|
@api.multi
|
|
|
|
def generate_number(self):
|
2016-08-02 17:08:37 +02:00
|
|
|
""" Computes number according to pre-existing number and chosen
|
|
|
|
seasons """
|
2016-08-02 19:33:58 +02:00
|
|
|
conf = self.env['ir.config_parameter']
|
2017-06-28 15:38:36 +02:00
|
|
|
isauto = conf.get_param('golem_numberconfig_isautomatic') == '1'
|
|
|
|
isperseason = conf.get_param('golem_numberconfig_isperseason') == '1'
|
|
|
|
isfornew = conf.get_param('golem_numberconfig_isfornewmembersonly') == '1'
|
2017-05-01 19:17:53 +02:00
|
|
|
for member in self:
|
2017-06-28 15:38:36 +02:00
|
|
|
if not isauto or (isfornew and member.number_manual):
|
2017-05-01 19:17:53 +02:00
|
|
|
member.number = member.number_manual
|
2016-08-02 17:08:37 +02:00
|
|
|
else:
|
2017-06-28 15:38:36 +02:00
|
|
|
if isperseason:
|
2017-06-28 16:25:00 +02:00
|
|
|
member.number = member.generate_number_perseason()
|
2017-05-01 19:17:53 +02:00
|
|
|
else:
|
2017-06-28 16:25:00 +02:00
|
|
|
member.number = member.generate_number_global()
|
2016-08-02 17:08:37 +02:00
|
|
|
|
|
|
|
@api.model
|
|
|
|
def create(self, values):
|
2017-05-01 19:17:53 +02:00
|
|
|
""" Number generation after creation """
|
2016-08-02 17:08:37 +02:00
|
|
|
new_member = super(GolemMember, self).create(values)
|
2017-05-01 19:17:53 +02:00
|
|
|
new_member.generate_number()
|
2016-08-02 17:08:37 +02:00
|
|
|
return new_member
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def write(self, values):
|
2017-05-01 19:17:53 +02:00
|
|
|
""" Number generation after updates """
|
2016-08-02 17:08:37 +02:00
|
|
|
res = super(GolemMember, self).write(values)
|
|
|
|
if 'season_ids' in values or 'number_manual' in values:
|
2017-05-01 19:17:53 +02:00
|
|
|
self.generate_number()
|
2016-08-02 17:08:37 +02:00
|
|
|
return res
|
|
|
|
|
2018-02-12 17:45:34 +01:00
|
|
|
|
2016-07-21 16:45:54 +02:00
|
|
|
class GolemMemberNumber(models.Model):
|
2016-07-08 16:12:12 +02:00
|
|
|
""" GOLEM Member Numbers """
|
|
|
|
_name = 'golem.member.number'
|
|
|
|
_description = 'GOLEM Member Numbers'
|
|
|
|
|
2018-07-17 08:26:49 +02:00
|
|
|
name = fields.Char(compute='_compute_name')
|
2016-07-08 16:12:12 +02:00
|
|
|
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)
|
2018-07-17 08:26:49 +02:00
|
|
|
number = fields.Char(index=True, readonly=True)
|
2016-07-08 16:12:12 +02:00
|
|
|
|
|
|
|
@api.depends('season_id')
|
|
|
|
def _compute_name(self):
|
2017-05-01 19:17:53 +02:00
|
|
|
for number in self:
|
|
|
|
number.name = number.season_id.name
|
2016-07-08 16:12:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2017-06-05 15:55:50 +02:00
|
|
|
number_from = fields.Integer('First number', default=1,
|
|
|
|
help='Number starting from, default to 1')
|
2016-07-08 16:12:12 +02:00
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def apply_config(self):
|
2017-05-01 19:17:53 +02:00
|
|
|
""" Apply new configuration """
|
2016-07-08 16:12:12 +02:00
|
|
|
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 '')
|
2017-06-20 08:23:32 +02:00
|
|
|
if self.number_from:
|
2017-06-28 16:25:00 +02:00
|
|
|
_LOGGER.warning('New number_from %s', self.number_from)
|
|
|
|
conf.set_param('golem_number_counter', unicode(self.number_from))
|
2017-06-20 08:23:32 +02:00
|
|
|
self.env['golem.season'].search([]).write({
|
2017-06-28 16:25:00 +02:00
|
|
|
'member_counter': self.number_from
|
2017-06-20 08:23:32 +02:00
|
|
|
})
|
2018-07-17 08:26:49 +02:00
|
|
|
|
2017-06-28 15:38:36 +02:00
|
|
|
@api.multi
|
|
|
|
def apply_nocompute(self):
|
|
|
|
""" Apply new configuration only for new members (keep old numbers) """
|
|
|
|
self.ensure_one()
|
|
|
|
self.apply_config()
|
|
|
|
conf = self.env['ir.config_parameter']
|
|
|
|
conf.set_param('golem_numberconfig_isfornewmembersonly', '1')
|
2016-07-08 16:12:12 +02:00
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def apply_recompute(self):
|
2017-05-01 19:17:53 +02:00
|
|
|
""" Recomputes all member numbers according to new configuration """
|
2016-07-08 16:12:12 +02:00
|
|
|
self.ensure_one()
|
|
|
|
self.apply_config()
|
|
|
|
conf = self.env['ir.config_parameter']
|
2017-06-28 15:38:36 +02:00
|
|
|
conf.set_param('golem_numberconfig_isfornewmembersonly', '0')
|
2016-07-08 16:12:12 +02:00
|
|
|
self.env['golem.member.number'].search([]).unlink()
|
2017-06-05 15:55:50 +02:00
|
|
|
self.env['golem.season'].search([]).write({
|
|
|
|
'member_counter': int(self.number_from)
|
|
|
|
})
|
2017-05-01 19:17:53 +02:00
|
|
|
self.env['golem.member'].search([]).generate_number()
|
2017-06-05 09:48:01 +02:00
|
|
|
|
2018-07-17 08:26:49 +02:00
|
|
|
|
2017-06-05 09:48:01 +02:00
|
|
|
class MergePartnerAutomatic(models.TransientModel):
|
|
|
|
""" Merge Partner Automatic adaptations """
|
|
|
|
_inherit = 'base.partner.merge.automatic.wizard'
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def action_merge(self):
|
|
|
|
""" Merge adaptations : warn if there is a member """
|
2017-06-05 15:55:50 +02:00
|
|
|
for merge in self:
|
|
|
|
for partner in merge.partner_ids:
|
|
|
|
if partner.member_id:
|
|
|
|
emsg = _('GOLEM Members merge has not been implemented yet. '
|
|
|
|
'Please only merge partners, not members, or delete '
|
|
|
|
'GOLEM Members manually before merging.')
|
|
|
|
raise UserError(emsg)
|
2017-06-05 09:48:01 +02:00
|
|
|
return super(MergePartnerAutomatic, self).action_merge()
|