From c60feaf23a41b6acb72a8068e8ac8c1d85e7c78c Mon Sep 17 00:00:00 2001 From: Fabien Bourgeois Date: Tue, 2 Aug 2016 17:08:37 +0200 Subject: [PATCH] [FIX]GOLEM Member number generation fixes on creation with usage of extension of create and write --- golem_member/models/golem_member.py | 134 +++++++++++++++++----------- golem_season/models/golem_season.py | 2 +- 2 files changed, 82 insertions(+), 54 deletions(-) diff --git a/golem_member/models/golem_member.py b/golem_member/models/golem_member.py index bcd05de..2915046 100644 --- a/golem_member/models/golem_member.py +++ b/golem_member/models/golem_member.py @@ -58,8 +58,7 @@ class GolemMember(models.Model): domain = [('is_default', '=', True)] return self.env['golem.season'].search(domain) - number = fields.Char('Member number', store=True, readonly=True, - compute='_compute_number') + number = fields.Char('Member number', store=True, readonly=True) number_manual = fields.Char('Manual number', size=50, index=True, help='Manual number overwriting automatic ' 'numbering') @@ -79,55 +78,6 @@ class GolemMember(models.Model): '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 - @api.depends('season_ids') def _compute_is_current(self): """ Computes is current according to seasons """ @@ -135,12 +85,90 @@ class GolemMember(models.Model): for m in self: m.is_current = default_s in m.season_ids - @api.depends('lastname') + @api.depends('number') 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 + @api.one + def _generate_number_perseason(self): + """ Number generation in case of per season configuration """ + res = None + conf = self.env['ir.config_parameter'] + for member in self: + 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: + res = mn.number + return res + + @api.one + def _generate_number_global(self): + """ Number generation in case of global configuration """ + for member in self: + conf = self.env['ir.config_parameter'] + 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) + return mn.number + + @api.one + def _generate_number(self): + """ Computes number according to pre-existing number and chosen + seasons """ + 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 conf.get_param('golem_numberconfig_isperseason') == '1': + mn = member._generate_number_perseason() + else: + mn = member._generate_number_global() + if mn: + member.number = mn[0] + + @api.model + @api.returns('self', lambda rec: rec.id) + def create(self, values): + new_member = super(GolemMember, self).create(values) + new_member._generate_number() + return new_member + + @api.multi + def write(self, values): + res = super(GolemMember, self).write(values) + if 'season_ids' in values or 'number_manual' in values: + self._generate_number() + return res + class GolemMemberNumber(models.Model): """ GOLEM Member Numbers """ @@ -205,4 +233,4 @@ class GolemNumberConfig(models.TransientModel): 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() + self.env['golem.member'].search([])._generate_number() diff --git a/golem_season/models/golem_season.py b/golem_season/models/golem_season.py index 9d995bf..03da4b1 100644 --- a/golem_season/models/golem_season.py +++ b/golem_season/models/golem_season.py @@ -70,7 +70,7 @@ class GolemSeason(models.Model): if 'golem.member' in self.env.registry: all_members = self.env['golem.member'].search([]) all_members._compute_is_current() - all_members._compute_number() + all_members._generate_number() if 'golem.activity' in self.env.registry: self.env['golem.activity'].search([])._compute_is_current()