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