[IMP] ir_sequence,res_company,res_currency objects related to company_id.
[odoo/odoo.git] / bin / addons / base / ir / ir_sequence.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 import time
23 from osv import fields,osv
24 import pooler
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         'company_id' : fields.many2one('res.company', 'Company'),
50     }
51     _defaults = {
52         'active': lambda *a: True,
53         'number_increment': lambda *a: 1,
54         'number_next': lambda *a: 1,
55         'padding' : lambda *a : 0,
56         'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'ir.sequence', c)
57     }
58
59     def _process(self, s):
60         return (s or '') % {
61             'year':time.strftime('%Y'), 
62             'month': time.strftime('%m'), 
63             'day':time.strftime('%d'),
64             'y': time.strftime('%y'),
65             'doy': time.strftime('%j'),
66             'woy': time.strftime('%W'),
67             'weekday': time.strftime('%w'),
68             'h24': time.strftime('%H'),
69             'h12': time.strftime('%I'),
70             'min': time.strftime('%M'),
71             'sec': time.strftime('%S'),
72         }
73
74     def get_id(self, cr, uid, sequence_id, test='id=%s', context=None):
75         try:
76             cr.execute('SELECT id, number_next, prefix, suffix, padding FROM ir_sequence WHERE '+test+' AND active=%s FOR UPDATE', (sequence_id, True))
77             res = cr.dictfetchone()
78             if res:
79                 cr.execute('UPDATE ir_sequence SET number_next=number_next+number_increment WHERE id=%s AND active=%s', (res['id'], True))
80                 if res['number_next']:
81                     return self._process(res['prefix']) + '%%0%sd' % res['padding'] % res['number_next'] + self._process(res['suffix'])
82                 else:
83                     return self._process(res['prefix']) + self._process(res['suffix'])
84         finally:
85             cr.commit()
86         return False
87
88     def get(self, cr, uid, code):
89         return self.get_id(cr, uid, code, test='code=%s')
90 ir_sequence()
91
92
93 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
94