1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 ##############################################################################
31 from osv import fields,osv
34 class res_company(osv.osv):
38 'name': fields.char('Company Name', size=64, required=True),
39 'parent_id': fields.many2one('res.company', 'Parent Company', select=True),
40 'child_ids': fields.one2many('res.company', 'parent_id', 'Childs Company'),
41 'partner_id': fields.many2one('res.partner', 'Partner', required=True),
42 'rml_header1': fields.char('Report Header', size=200),
43 'rml_footer1': fields.char('Report Footer 1', size=200),
44 'rml_footer2': fields.char('Report Footer 2', size=200),
45 'rml_header' : fields.text('RML Header'),
46 'rml_header2' : fields.text('RML Internal Header'),
47 'logo' : fields.binary('Logo'),
48 'currency_id': fields.many2one('res.currency', 'Currency', required=True),
49 'currency_ids': fields.one2many('res.currency', 'company_id', 'Currency Rates'),
52 def _get_child_ids(self, cr, uid, uid2, context={}):
53 company = self.pool.get('res.users').company_get(cr, uid, uid2)
54 ids = self._get_company_children(cr, uid, company)
57 def _get_company_children(self, cr, uid=None, company=None):
60 ids = self.search(cr, uid, [('parent_id','child_of',[company])])
62 _get_company_children = tools.cache()(_get_company_children)
64 def _get_partner_hierarchy(self, cr, uid, company_id, context={}):
66 parent_id = self.browse(cr, uid, company_id)['parent_id']
68 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
70 return self._get_partner_descendance(cr, uid, company_id, [], context)
73 def _get_partner_descendance(self, cr, uid, company_id, descendance, context={}):
74 descendance.append(self.browse(cr, uid, company_id).partner_id.id)
75 for child_id in self._get_company_children(cr, uid, company_id):
76 if child_id != company_id:
77 descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
80 def __init__(self, *args, **argv):
81 return super(res_company, self).__init__(*args, **argv)
84 # This function restart the cache on the _get_company_children method
86 def cache_restart(self, uid=None):
87 self._get_company_children()
89 def create(self, *args, **argv):
91 return super(res_company, self).create(*args, **argv)
93 def write(self, *args, **argv):
95 # Restart the cache on the company_get method
96 self.pool.get('ir.rule').domain_get()
97 return super(res_company, self).write(*args, **argv)
99 def _get_euro(self, cr, uid, context={}):
101 return self.pool.get('res.currency').search(cr, uid, [])[0]
105 def _check_recursion(self, cr, uid, ids):
108 cr.execute('select distinct parent_id from res_company where id in ('+','.join(map(str,ids))+')')
109 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
115 def _get_header2(self,cr,uid,ids):
119 <frame id="first" x1="1.3cm" y1="1.5cm" width="18.4cm" height="26.5cm"/>
121 <fill color="black"/>
122 <stroke color="black"/>
123 <setFont name="Helvetica" size="8"/>
124 <drawString x="1.3cm" y="28.3cm"> [[ formatLang(time.strftime("%Y-%m-%d"), date=True) ]] [[ time.strftime("%H:%M") ]]</drawString>
125 <setFont name="Helvetica-Bold" size="10"/>
126 <drawString x="9.8cm" y="28.3cm">[[ company.partner_id.name ]]</drawString>
127 <setFont name="Helvetica" size="8"/>
128 <drawRightString x="19.7cm" y="28.3cm"><pageNumber/> / </drawRightString>
129 <drawString x="19.8cm" y="28.3cm"><pageCount/></drawString>
130 <stroke color="#aaaaaa"/>
131 <lines>1.3cm 28.1cm 20cm 28.1cm</lines>
135 def _get_header(self,cr,uid,ids):
137 return tools.file_open('custom/corporate_rml_header.rml').read()
142 <frame id="first" x1="1.3cm" y1="2.5cm" height="23.0cm" width="19cm"/>
144 <!-- You Logo - Change X,Y,Width and Height -->
145 <image x="1.3cm" y="27.6cm" height="40.0" >[[company.logo]]</image>
146 <setFont name="Helvetica" size="8"/>
147 <fill color="black"/>
148 <stroke color="black"/>
149 <lines>1.3cm 27.7cm 20cm 27.7cm</lines>
151 <drawRightString x="20cm" y="27.8cm">[[ company.rml_header1 ]]</drawRightString>
154 <drawString x="1.3cm" y="27.2cm">[[ company.partner_id.name ]]</drawString>
155 <drawString x="1.3cm" y="26.8cm">[[ company.partner_id.address and company.partner_id.address[0].street ]]</drawString>
156 <drawString x="1.3cm" y="26.4cm">[[ company.partner_id.address and company.partner_id.address[0].zip ]] [[ company.partner_id.address and company.partner_id.address[0].city ]] - [[ company.partner_id.address and company.partner_id.address[0].country_id and company.partner_id.address[0].country_id.name ]]</drawString>
157 <drawString x="1.3cm" y="26.0cm">Phone:</drawString>
158 <drawRightString x="7cm" y="26.0cm">[[ company.partner_id.address and company.partner_id.address[0].phone ]]</drawRightString>
159 <drawString x="1.3cm" y="25.6cm">Mail:</drawString>
160 <drawRightString x="7cm" y="25.6cm">[[ company.partner_id.address and company.partner_id.address[0].email ]]</drawRightString>
161 <lines>1.3cm 25.5cm 7cm 25.5cm</lines>
165 <lines>1.2cm 2.15cm 19.9cm 2.15cm</lines>
167 <drawCentredString x="10.5cm" y="1.7cm">[[ company.rml_footer1 ]]</drawCentredString>
168 <drawCentredString x="10.5cm" y="1.25cm">[[ company.rml_footer2 ]]</drawCentredString>
169 <drawCentredString x="10.5cm" y="0.8cm">Contact : [[ user.name ]] - Page: <pageNumber/></drawCentredString>
174 'currency_id': _get_euro,
175 'rml_header':_get_header,
176 'rml_header2': _get_header2
180 (_check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
186 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: