1 ##############################################################################
3 # Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 # Fabien Pinckaers <fp@tiny.Be>
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
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.
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.
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.
27 ##############################################################################
29 from osv import fields,osv
32 class res_company(osv.osv):
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),
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)
51 def _get_company_children(self, cr, uid=None, company=None):
54 ids = self.search(cr, uid, [('parent_id','child_of',[company])])
56 _get_company_children = tools.cache()(_get_company_children)
58 def _get_partner_hierarchy(self, cr, uid, company_id, context={}):
59 print "Company ID is %s: Looking for parent..." % company_id
61 parent_id = self.browse(cr, uid, company_id)['parent_id']
63 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
65 print "No parent: starting descendance search!"
66 return self._get_partner_descendance(cr, uid, company_id, [], context)
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)
77 def __init__(self, *args, **argv):
78 return super(res_company, self).__init__(*args, **argv)
81 # This function restart the cache on the _get_company_children method
83 def cache_restart(self, uid=None):
84 self._get_company_children()
86 def create(self, *args, **argv):
88 return super(res_company, self).create(*args, **argv)
90 def write(self, *args, **argv):
92 return super(res_company, self).write(*args, **argv)
94 def _get_euro(self, cr, uid, context={}):
96 return self.pool.get('res.currency').search(cr, uid, [('rate', '=', 1.0),])[0]
100 def _check_recursion(self, cr, uid, 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()))
111 'currency_id': _get_euro,
115 (_check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])