[FIX] Remove the select for update and use a lock table
[odoo/odoo.git] / bin / addons / base / ir / ir_sequence.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import time
24 from osv import fields,osv
25
26 class ir_sequence_type(osv.osv):
27     _name = 'ir.sequence.type'
28     _columns = {
29         'name': fields.char('Sequence Name',size=64, required=True),
30         'code': fields.char('Sequence Code',size=32, required=True),
31     }
32 ir_sequence_type()
33
34 def _code_get(self, cr, uid, context={}):
35     cr.execute('select code, name from ir_sequence_type')
36     return cr.fetchall()
37
38 class ir_sequence(osv.osv):
39     _name = 'ir.sequence'
40     _columns = {
41         'name': fields.char('Sequence Name',size=64, required=True),
42         'code': fields.selection(_code_get, 'Sequence Code',size=64, required=True),
43         'active': fields.boolean('Active'),
44         'prefix': fields.char('Prefix',size=64),
45         'suffix': fields.char('Suffix',size=64),
46         'number_next': fields.integer('Next Number', required=True),
47         'number_increment': fields.integer('Increment Number', required=True),
48         'padding' : fields.integer('Number padding', required=True),
49     }
50     _defaults = {
51         'active': lambda *a: True,
52         'number_increment': lambda *a: 1,
53         'number_next': lambda *a: 1,
54         'padding' : lambda *a : 0,
55     }
56
57     def _process(self, s):
58         return (s or '') % {
59             'year':time.strftime('%Y'), 
60             'month': time.strftime('%m'), 
61             'day':time.strftime('%d'),
62             'y': time.strftime('%y'),
63             'doy': time.strftime('%j'),
64             'woy': time.strftime('%W'),
65             'weekday': time.strftime('%w'),
66             'h24': time.strftime('%H'),
67             'h12': time.strftime('%I'),
68             'min': time.strftime('%M'),
69             'sec': time.strftime('%S'),
70         }
71
72     def get_id(self, cr, uid, sequence_id, test='id=%s', context={}):
73         try:
74             cr.execute('lock table ir_sequence')
75             cr.execute('select id,number_next,number_increment,prefix,suffix,padding from ir_sequence where '+test+' and active=True', (sequence_id,))
76             res = cr.dictfetchone()
77             if res:
78                 cr.execute('update ir_sequence set number_next=number_next+number_increment where id=%s and active=True', (res['id'],))
79                 if res['number_next']:
80                     return self._process(res['prefix']) + '%%0%sd' % res['padding'] % res['number_next'] + self._process(res['suffix'])
81                 else:
82                     return self._process(res['prefix']) + self._process(res['suffix'])
83         finally:
84             cr.commit()
85         return False
86
87     def get(self, cr, uid, code):
88         return self.get_id(cr, uid, code, test='code=%s')
89 ir_sequence()
90
91
92 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
93