New trunk
[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 threading
30 import time
31 from osv import fields,osv
32
33 class ir_sequence_type(osv.osv):
34         _name = 'ir.sequence.type'
35         _columns = {
36                 'name': fields.char('Sequence Name',size=64, required=True),
37                 'code': fields.char('Sequence Code',size=32, required=True),
38         }
39 ir_sequence_type()
40
41 def _code_get(self, cr, uid, context={}):
42         cr.execute('select code, name from ir_sequence_type')
43         return cr.fetchall()
44
45 class ir_sequence(osv.osv):
46         _name = 'ir.sequence'
47         _columns = {
48                 'name': fields.char('Sequence Name',size=64, required=True),
49                 'code': fields.selection(_code_get, 'Sequence Code',size=64, required=True),
50                 'active': fields.boolean('Active'),
51                 'prefix': fields.char('Prefix',size=64),
52                 'suffix': fields.char('Suffix',size=64),
53                 'number_next': fields.integer('Next Number', required=True),
54                 'number_increment': fields.integer('Increment Number', required=True),
55                 'padding' : fields.integer('Number padding', required=True),
56         }
57         _defaults = {
58                 'active': lambda *a: True,
59                 'number_increment': lambda *a: 1,
60                 'number_next': lambda *a: 1,
61                 'padding' : lambda *a : 0,
62         }
63
64         sequence_semaphore = threading.Semaphore()
65
66         def _process(self, s):
67                 return (s or '') % {'year':time.strftime('%Y'), 'month': time.strftime('%m'), 'day':time.strftime('%d')}
68
69         def get_id(self, cr, uid, sequence_id, test='id=%d'):
70                 self.sequence_semaphore.acquire()
71                 cr.execute('select id,number_next,number_increment,prefix,suffix,padding from ir_sequence where '+test+' and active=True', (sequence_id,))
72                 res = cr.dictfetchone()
73                 if res:
74                         cr.execute('update ir_sequence set number_next=number_next+number_increment where id=%d and active=True', (res['id'],))
75                         self.sequence_semaphore.release()
76                         if res['number_next']:
77                                 return self._process(res['prefix']) + '%%0%sd' % res['padding'] % res['number_next'] + self._process(res['suffix'])
78                         else:
79                                 return self._process(res['prefix']) + self._process(res['suffix'])
80                 else:
81                         self.sequence_semaphore.release()
82                 return False
83
84         def get(self, cr, uid, code):
85                 return self.get_id(cr, uid, code, test='code=%s')
86
87         def set(self, cr, uid, code, next_number):
88                 self.sequence_semaphore.acquire()
89                 cr.execute('update ir_sequence set number_next=%d where code=%s and active=True', (next_number, code,))
90                 self.sequence_semaphore.release()
91                 return True
92 ir_sequence()
93