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),
51 def _get_child_ids(self, cr, uid, uid2, context={}):
52 company = self.pool.get('res.users').company_get(cr, uid, uid2)
53 ids = self._get_company_children(cr, uid, company)
56 def _get_company_children(self, cr, uid=None, company=None):
59 ids = self.search(cr, uid, [('parent_id','child_of',[company])])
61 _get_company_children = tools.cache()(_get_company_children)
63 def _get_partner_hierarchy(self, cr, uid, company_id, context={}):
65 parent_id = self.browse(cr, uid, company_id)['parent_id']
67 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
69 return self._get_partner_descendance(cr, uid, company_id, [], context)
72 def _get_partner_descendance(self, cr, uid, company_id, descendance, context={}):
73 descendance.append(self.browse(cr, uid, company_id).partner_id.id)
74 for child_id in self._get_company_children(cr, uid, company_id):
75 if child_id != company_id:
76 descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
79 def __init__(self, *args, **argv):
80 return super(res_company, self).__init__(*args, **argv)
83 # This function restart the cache on the _get_company_children method
85 def cache_restart(self, uid=None):
86 self._get_company_children()
88 def create(self, *args, **argv):
90 return super(res_company, self).create(*args, **argv)
92 def write(self, *args, **argv):
94 # Restart the cache on the company_get method
95 self.pool.get('ir.rule').domain_get()
96 return super(res_company, self).write(*args, **argv)
98 def _get_euro(self, cr, uid, context={}):
100 return self.pool.get('res.currency').search(cr, uid, [])[0]
104 def _check_recursion(self, cr, uid, ids):
107 cr.execute('select distinct parent_id from res_company where id in ('+','.join(map(str,ids))+')')
108 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
114 def _get_header2(self,cr,uid,ids):
118 <frame id="first" x1="1.3cm" y1="1.5cm" width="18.4cm" height="26.5cm"/>
120 <fill color="black"/>
121 <stroke color="black"/>
122 <setFont name="Helvetica" size="8"/>
123 <drawString x="1.3cm" y="28.3cm"> [[ formatLang(time.strftime("%Y-%m-%d"), date=True) ]] [[ time.strftime("%H:%M") ]]</drawString>
124 <setFont name="Helvetica-Bold" size="10"/>
125 <drawString x="9.8cm" y="28.3cm">[[ company.partner_id.name ]]</drawString>
126 <setFont name="Helvetica" size="8"/>
127 <drawRightString x="19.7cm" y="28.3cm"><pageNumber/> / </drawRightString>
128 <drawString x="19.8cm" y="28.3cm"><pageCount/></drawString>
129 <stroke color="#aaaaaa"/>
130 <lines>1.3cm 28.1cm 20cm 28.1cm</lines>
134 def _get_header(self,cr,uid,ids):
136 return tools.file_open('custom/corporate_rml_header.rml').read()
141 <frame id="first" x1="1.3cm" y1="2.5cm" height="23.0cm" width="19cm"/>
143 <!-- You Logo - Change X,Y,Width and Height -->
144 <image x="1.3cm" y="27.6cm" height="40.0" >[[company.logo]]</image>
145 <setFont name="Helvetica" size="8"/>
146 <fill color="black"/>
147 <stroke color="black"/>
148 <lines>1.3cm 27.7cm 20cm 27.7cm</lines>
150 <drawRightString x="20cm" y="27.8cm">[[ company.rml_header1 ]]</drawRightString>
153 <drawString x="1.3cm" y="27.2cm">[[ company.partner_id.name ]]</drawString>
154 <drawString x="1.3cm" y="26.8cm">[[ company.partner_id.address and company.partner_id.address[0].street ]]</drawString>
155 <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>
156 <drawString x="1.3cm" y="26.0cm">Phone:</drawString>
157 <drawRightString x="7cm" y="26.0cm">[[ company.partner_id.address and company.partner_id.address[0].phone ]]</drawRightString>
158 <drawString x="1.3cm" y="25.6cm">Mail:</drawString>
159 <drawRightString x="7cm" y="25.6cm">[[ company.partner_id.address and company.partner_id.address[0].email ]]</drawRightString>
160 <lines>1.3cm 25.5cm 7cm 25.5cm</lines>
164 <lines>1.2cm 2.15cm 19.9cm 2.15cm</lines>
166 <drawCentredString x="10.5cm" y="1.7cm">[[ company.rml_footer1 ]]</drawCentredString>
167 <drawCentredString x="10.5cm" y="1.25cm">[[ company.rml_footer2 ]]</drawCentredString>
168 <drawCentredString x="10.5cm" y="0.8cm">Contact : [[ user.name ]] - Page: <pageNumber/></drawCentredString>
173 'currency_id': _get_euro,
174 'rml_header':_get_header,
175 'rml_header2': _get_header2
179 (_check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
185 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: