[VIEW] +mobile number in partners tree views
[burette/cyclofficine_pantin.git] / wizard / attribute_member_ident.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 # cyclofficine_pantin module for OpenERP, Custom module for La Cyclofficine
5 # de Pantin
6
7 # Copyright (C) 2015-2016 cyclofficine_pantin
8 # (<http://cyclocoop.org/index.php/les-ateliers/pantin/>)
9 #
10 # This file is a part of cyclofficine_pantin
11 #
12 # cyclofficine_pantin is free software: you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License as published
14 # by the Free Software Foundation, either version 3 of the License, or (at
15 # your option) any later version.
16 #
17 # cyclofficine_pantin is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #
25 ##############################################################################
26 from openerp.osv import osv
27 from openerp.osv import orm
28 from openerp.osv import fields
29 from openerp.tools.translate import _
30
31
32 class attribute_member_ident(orm.TransientModel):
33 _name = 'attribute_member_ident'
34 _description = 'Attribute member identification number'
35
36 def pad_this_member_ident(self, cr, uid, member_ident, context=None):
37 sequences = self.pool.get('ir.sequence')
38 id_mb_seq = sequences.search(
39 cr, uid, [('code', '=', 'member_ident')],
40 limit=1, context=context)
41 padding = sequences.read(cr, uid, id_mb_seq, ['padding'],
42 context)[0]['padding']
43 return str(member_ident).zfill(padding)
44
45 def attribute_member_ident(self, cr, uid, ids, context=None):
46 member_ident = int(self.browse(cr, uid, ids,
47 context=context)[0].member_ident)
48 partner_obj = self.pool.get('res.partner')
49 partner = partner_obj.browse(cr, uid, context['active_id'],
50 context=context)
51 previous_member_ident = int(self.get_previous_member_ident(cr, uid,
52 context))
53 if member_ident > previous_member_ident:
54 raise osv.except_osv(_('Not possible'), _('This member identifier is greater than the previous next number'))
55 elif member_ident == previous_member_ident:
56 number_next = self.pool.get('ir.sequence').get(cr, uid,
57 'member_ident')
58 partner.write({'member_ident': number_next})
59 else:
60 member_ident = self.pad_this_member_ident(cr, uid, member_ident,
61 context)
62 self.pad_this_member_ident(cr, uid, partner.write({'member_ident': member_ident}), context)
63 return {}
64
65 def get_previous_member_ident(self, cr, uid, context):
66 """Get previous member ident without increment sequence number next."""
67 sequences = self.pool.get('ir.sequence')
68 id_mb_seq = sequences.search(
69 cr, uid, [('code', '=', 'member_ident')],
70 limit=1, context=context)
71 mb_seq = sequences.read(cr, uid, id_mb_seq, ['number_next_actual'],
72 context)
73 return self.pad_this_member_ident(cr, uid,
74 mb_seq[0]['number_next_actual'],
75 context)
76
77 _columns = {
78 'previous_member_ident': fields.char(
79 'Previous member identification number',
80 readonly=True,
81 size=64,
82 help='This number is the one that is logical to attribute now.'),
83 'member_ident': fields.char('Member identification number',
84 size=64, required=True),
85 }
86
87 _defaults = {
88 'previous_member_ident': lambda self, cr, uid, context:
89 self.get_previous_member_ident(cr, uid, context),
90 'member_ident': lambda self, cr, uid, context:
91 self.get_previous_member_ident(cr, uid, context),
92 }
93
94 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: