BASE: prevent recursion in company
[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                 print "Company ID is %s: Looking for parent..." % company_id
60                 if company_id:
61                         parent_id = self.browse(cr, uid, company_id)['parent_id']
62                         if parent_id:
63                                 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
64                         else:
65                                 print "No parent: starting descendance search!"
66                                 return self._get_partner_descendance(cr, uid, company_id, [], context)
67                 return []
68
69         def _get_partner_descendance(self, cr, uid, company_id, descendance, context={}):
70                 descendance.append(self.browse(cr, uid, company_id).partner_id.id)
71                 for child_id in self._get_company_children(cr, uid, company_id):
72                         if child_id != company_id:
73                                 print "Hello, I'm %s, child of %s" % (child_id, company_id)
74                                 descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
75                 return descendance
76
77         def __init__(self, *args, **argv):
78                 return super(res_company, self).__init__(*args, **argv)
79
80         #
81         # This function restart the cache on the _get_company_children method
82         #
83         def cache_restart(self, uid=None):
84                 self._get_company_children()
85
86         def create(self, *args, **argv):
87                 self.cache_restart()
88                 return super(res_company, self).create(*args, **argv)
89
90         def write(self, *args, **argv):
91                 self.cache_restart()
92                 return super(res_company, self).write(*args, **argv)
93
94         def _get_euro(self, cr, uid, context={}):
95                 try:
96                         return self.pool.get('res.currency').search(cr, uid, [('rate', '=', 1.0),])[0]
97                 except:
98                         return 1
99         
100         def _check_recursion(self, cr, uid, ids):
101                 level = 100
102                 while len(ids):
103                         cr.execute('select distinct parent_id from res_company where id in ('+','.join(map(str,ids))+')')
104                         ids = filter(None, map(lambda x:x[0], cr.fetchall()))
105                         if not level:
106                                 return False
107                         level -= 1
108                 return True
109         
110         _defaults = {
111                 'currency_id': _get_euro,
112         }
113
114         _constraints = [
115                 (_check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
116         ]
117
118 res_company()
119