KERNEL, MULTI_COMPANY_*, STOCK, BASE: extend search child_of
[odoo/odoo.git] / bin / addons / base / res / res_company.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 from osv import fields,osv
30 import tools
31
32 class res_company(osv.osv):
33         _name = "res.company"
34
35         _columns = {
36                 'name': fields.char('Company Name', size=64, required=True),
37                 'parent_id': fields.many2one('res.company', 'Parent Company', select=True),
38                 'child_ids': fields.one2many('res.company', 'parent_id', 'Childs Company'),
39                 'partner_id': fields.many2one('res.partner', 'Partner', required=True),
40                 'rml_header1': fields.char('Report Header', size=200),
41                 'rml_footer1': fields.char('Report Footer 1', size=200),
42                 'rml_footer2': fields.char('Report Footer 2', size=200),
43                 'currency_id': fields.many2one('res.currency', 'Currency', required=True),
44         }
45         
46         def _get_child_ids(self, cr, uid, uid2, context={}):
47                 company = self.pool.get('res.users').company_get(cr, uid, uid2)
48                 ids = self._get_company_children(cr, uid, company)
49                 return ids
50
51         def _get_company_children(self, cr, uid=None, company=None):
52                 if not company:
53                         return []
54                 ids =  self.search(cr, uid, [('parent_id','child_of',[company])])
55                 return ids
56         _get_company_children = tools.cache()(_get_company_children)
57
58         def _get_partner_hierarchy(self, cr, uid, company_id, context={}):
59                 if company_id:
60                         parent_id = self.browse(cr, uid, company_id)['parent_id']
61                         if parent_id:
62                                 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
63                         else:
64                                 return self._get_partner_descendance(cr, uid, company_id, [], context)
65                 return []
66
67         def _get_partner_descendance(self, cr, uid, company_id, descendance, context={}):
68                 descendance.append(self.browse(cr, uid, company_id).partner_id.id)
69                 for child_id in self._get_company_children(cr, uid, company_id):
70                         if child_id != company_id:
71                                 descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
72                 return descendance
73
74         def __init__(self, *args, **argv):
75                 return super(res_company, self).__init__(*args, **argv)
76
77         #
78         # This function restart the cache on the _get_company_children method
79         #
80         def cache_restart(self, uid=None):
81                 self._get_company_children()
82
83         def create(self, *args, **argv):
84                 self.cache_restart()
85                 return super(res_company, self).create(*args, **argv)
86
87         def write(self, *args, **argv):
88                 self.cache_restart()
89                 # Restart the cache on the company_get method
90                 self.pool.get('ir.rule').domain_get()
91                 return super(res_company, self).write(*args, **argv)
92
93         def _get_euro(self, cr, uid, context={}):
94                 try:
95                         return self.pool.get('res.currency').search(cr, uid, [('rate', '=', 1.0),])[0]
96                 except:
97                         return 1
98         
99         def _check_recursion(self, cr, uid, ids):
100                 level = 100
101                 while len(ids):
102                         cr.execute('select distinct parent_id from res_company where id in ('+','.join(map(str,ids))+')')
103                         ids = filter(None, map(lambda x:x[0], cr.fetchall()))
104                         if not level:
105                                 return False
106                         level -= 1
107                 return True
108         
109         _defaults = {
110                 'currency_id': _get_euro,
111         }
112
113         _constraints = [
114                 (_check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
115         ]
116
117 res_company()
118