Sequence bugfixes
[odoo/odoo.git] / bin / addons / base / ir / ir_sequence.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #                    Fabien Pinckaers <fp@tiny.Be>
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsability of assessing all potential
8 # consequences resulting from its eventual inadequacies and bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # garantees and support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 ##############################################################################
28
29 import time
30 from osv import fields,osv
31
32 class ir_sequence_type(osv.osv):
33         _name = 'ir.sequence.type'
34         _columns = {
35                 'name': fields.char('Sequence Name',size=64, required=True),
36                 'code': fields.char('Sequence Code',size=32, required=True),
37         }
38 ir_sequence_type()
39
40 def _code_get(self, cr, uid, context={}):
41         cr.execute('select code, name from ir_sequence_type')
42         return cr.fetchall()
43
44 class ir_sequence(osv.osv):
45         _name = 'ir.sequence'
46         _columns = {
47                 'name': fields.char('Sequence Name',size=64, required=True),
48                 'code': fields.selection(_code_get, 'Sequence Code',size=64, required=True),
49                 'active': fields.boolean('Active'),
50                 'prefix': fields.char('Prefix',size=64),
51                 'suffix': fields.char('Suffix',size=64),
52                 'number_next': fields.integer('Next Number', required=True),
53                 'number_increment': fields.integer('Increment Number', required=True),
54                 'padding' : fields.integer('Number padding', required=True),
55         }
56         _defaults = {
57                 'active': lambda *a: True,
58                 'number_increment': lambda *a: 1,
59                 'number_next': lambda *a: 1,
60                 'padding' : lambda *a : 0,
61         }
62
63         def _process(self, s):
64                 return (s or '') % {'year':time.strftime('%Y'), 'month': time.strftime('%m'), 'day':time.strftime('%d')}
65
66         def get_id(self, cr, uid, sequence_id, test='id=%d'):
67                 try:
68                         cr.execute('lock table ir_sequence')
69                         cr.execute('select id,number_next,number_increment,prefix,suffix,padding from ir_sequence where '+test+' and active=True', (sequence_id,))
70                         res = cr.dictfetchone()
71                         if res:
72                                 cr.execute('update ir_sequence set number_next=number_next+number_increment where id=%d and active=True', (res['id'],))
73                                 if res['number_next']:
74                                         return self._process(res['prefix']) + '%%0%sd' % res['padding'] % res['number_next'] + self._process(res['suffix'])
75                                 else:
76                                         return self._process(res['prefix']) + self._process(res['suffix'])
77                         cr.commit()
78                 except:
79                         cr.rollback()
80                         return False
81                 return False
82
83         def get(self, cr, uid, code):
84                 return self.get_id(cr, uid, code, test='code=%s')
85 ir_sequence()
86