[WIZARD] +manual member identification number attribution
[burette/cyclofficine_pantin.git] / wizard / attribute_member_ident.py
diff --git a/wizard/attribute_member_ident.py b/wizard/attribute_member_ident.py
new file mode 100644 (file)
index 0000000..7cf9331
--- /dev/null
@@ -0,0 +1,94 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    cyclofficine_pantin module for OpenERP, Custom module for La Cyclofficine
+#    de Pantin
+
+#    Copyright (C) 2015-2016 cyclofficine_pantin
+#    (<http://cyclocoop.org/index.php/les-ateliers/pantin/>)
+#
+#    This file is a part of cyclofficine_pantin
+#
+#    cyclofficine_pantin is free software: you can redistribute it and/or
+#    modify it under the terms of the GNU General Public License as published
+#    by the Free Software Foundation, either version 3 of the License, or (at
+#    your option) any later version.
+#
+#    cyclofficine_pantin 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 General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+from openerp.osv import osv
+from openerp.osv import orm
+from openerp.osv import fields
+from openerp.tools.translate import _
+
+
+class attribute_member_ident(orm.TransientModel):
+    _name = 'attribute_member_ident'
+    _description = 'Attribute member identification number'
+
+    def pad_this_member_ident(self, cr, uid, member_ident, context=None):
+        sequences = self.pool.get('ir.sequence')
+        id_mb_seq = sequences.search(
+            cr, uid, [('code', '=', 'member_ident')],
+            limit=1, context=context)
+        padding = sequences.read(cr, uid, id_mb_seq, ['padding'],
+                                 context)[0]['padding']
+        return str(member_ident).zfill(padding)
+
+    def attribute_member_ident(self, cr, uid, ids, context=None):
+        member_ident = int(self.browse(cr, uid, ids,
+                                       context=context)[0].member_ident)
+        partner_obj = self.pool.get('res.partner')
+        partner = partner_obj.browse(cr, uid, context['active_id'],
+                                     context=context)
+        previous_member_ident = int(self.get_previous_member_ident(cr, uid,
+                                                                   context))
+        if member_ident > previous_member_ident:
+            raise osv.except_osv(_('Not possible'), _('This member identifier is greater than the previous next number'))
+        elif member_ident == previous_member_ident:
+            number_next = self.pool.get('ir.sequence').get(cr, uid,
+                                                           'member_ident')
+            partner.write({'member_ident': number_next})
+        else:
+            member_ident = self.pad_this_member_ident(cr, uid, member_ident,
+                                                      context)
+            self.pad_this_member_ident(cr, uid, partner.write({'member_ident': member_ident}), context)
+        return {}
+
+    def get_previous_member_ident(self, cr, uid, context):
+        """Get previous member ident without increment sequence number next."""
+        sequences = self.pool.get('ir.sequence')
+        id_mb_seq = sequences.search(
+            cr, uid, [('code', '=', 'member_ident')],
+            limit=1, context=context)
+        mb_seq = sequences.read(cr, uid, id_mb_seq, ['number_next_actual'],
+                                context)
+        return self.pad_this_member_ident(cr, uid,
+                                          mb_seq[0]['number_next_actual'],
+                                          context)
+
+    _columns = {
+        'previous_member_ident': fields.char(
+            'Previous member identification number',
+            readonly=True,
+            size=64,
+            help='This number is the one that is logical to attribute now.'),
+        'member_ident': fields.char('Member identification number',
+                                    size=64, required=True),
+    }
+
+    _defaults = {
+        'previous_member_ident': lambda self, cr, uid, context:
+        self.get_previous_member_ident(cr, uid, context),
+        'member_ident': lambda self, cr, uid, context:
+        self.get_previous_member_ident(cr, uid, context),
+    }
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: