convert tabs to 4 spaces
[odoo/odoo.git] / bin / addons / base / res / res_company.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # $Id$
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 #
28 ##############################################################################
29
30 from osv import fields,osv
31 import tools
32
33 class res_company(osv.osv):
34     _name = "res.company"
35
36     _columns = {
37         'name': fields.char('Company Name', size=64, required=True),
38         'parent_id': fields.many2one('res.company', 'Parent Company', select=True),
39         'child_ids': fields.one2many('res.company', 'parent_id', 'Childs Company'),
40         'partner_id': fields.many2one('res.partner', 'Partner', required=True),
41         'rml_header1': fields.char('Report Header', size=200),
42         'rml_footer1': fields.char('Report Footer 1', size=200),
43         'rml_footer2': fields.char('Report Footer 2', size=200),
44         'rml_header' : fields.text('RML Header'),
45         'rml_header2' : fields.text('RML Internal Header'),
46         'logo' : fields.binary('Logo'),
47         'currency_id': fields.many2one('res.currency', 'Currency', required=True),
48     }
49     
50     def _get_child_ids(self, cr, uid, uid2, context={}):
51         company = self.pool.get('res.users').company_get(cr, uid, uid2)
52         ids = self._get_company_children(cr, uid, company)
53         return ids
54
55     def _get_company_children(self, cr, uid=None, company=None):
56         if not company:
57             return []
58         ids =  self.search(cr, uid, [('parent_id','child_of',[company])])
59         return ids
60     _get_company_children = tools.cache()(_get_company_children)
61
62     def _get_partner_hierarchy(self, cr, uid, company_id, context={}):
63         if company_id:
64             parent_id = self.browse(cr, uid, company_id)['parent_id']
65             if parent_id:
66                 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
67             else:
68                 return self._get_partner_descendance(cr, uid, company_id, [], context)
69         return []
70
71     def _get_partner_descendance(self, cr, uid, company_id, descendance, context={}):
72         descendance.append(self.browse(cr, uid, company_id).partner_id.id)
73         for child_id in self._get_company_children(cr, uid, company_id):
74             if child_id != company_id:
75                 descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
76         return descendance
77
78     def __init__(self, *args, **argv):
79         return super(res_company, self).__init__(*args, **argv)
80
81     #
82     # This function restart the cache on the _get_company_children method
83     #
84     def cache_restart(self, uid=None):
85         self._get_company_children()
86
87     def create(self, *args, **argv):
88         self.cache_restart()
89         return super(res_company, self).create(*args, **argv)
90
91     def write(self, *args, **argv):
92         self.cache_restart()
93         # Restart the cache on the company_get method
94         self.pool.get('ir.rule').domain_get()
95         return super(res_company, self).write(*args, **argv)
96
97     def _get_euro(self, cr, uid, context={}):
98         try:
99             return self.pool.get('res.currency').search(cr, uid, [('rate', '=', 1.0),])[0]
100         except:
101             return 1
102     
103     def _check_recursion(self, cr, uid, ids):
104         level = 100
105         while len(ids):
106             cr.execute('select distinct parent_id from res_company where id in ('+','.join(map(str,ids))+')')
107             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
108             if not level:
109                 return False
110             level -= 1
111         return True
112     
113     def _get_header2(self,cr,uid,ids):
114         return """
115         <header>
116         <pageTemplate>
117         <frame id="first" x1="1.3cm" y1="1.5cm" width="18.4cm" height="26.5cm"/>
118         <pageGraphics>
119         <fill color="black"/>
120         <stroke color="black"/>
121         <setFont name="Helvetica" size="8"/>
122         <drawString x="1.3cm" y="28.3cm"> [[ formatLang(time.strftime("%Y-%m-%d"), date=True) ]]  [[ time.strftime("%H:%M") ]]</drawString>
123         <setFont name="Helvetica-Bold" size="10"/>
124         <drawString x="9.8cm" y="28.3cm">[[ company.partner_id.name ]]</drawString>
125         <setFont name="Helvetica" size="8"/>
126         <drawRightString x="19.7cm" y="28.3cm"><pageNumber/> /  </drawRightString>
127         <drawString x="19.8cm" y="28.3cm"><pageCount/></drawString>
128         <stroke color="#aaaaaa"/>
129         <lines>1.3cm 28.1cm 20cm 28.1cm</lines>
130         </pageGraphics>
131         </pageTemplate>
132 </header>"""
133     def _get_header(self,cr,uid,ids):
134         try :
135             return tools.file_open('custom/corporate_rml_header.rml').read()
136         except:
137             return """
138     <header>
139     <pageTemplate>
140         <frame id="first" x1="1.3cm" y1="2.5cm" height="23.0cm" width="19cm"/>
141         <pageGraphics>
142             <!-- You Logo - Change X,Y,Width and Height -->
143         <image x="1.3cm" y="27.6cm" height="40.0" >[[company.logo]]</image>
144             <setFont name="Helvetica" size="8"/>
145             <fill color="black"/>
146             <stroke color="black"/>
147             <lines>1.3cm 27.7cm 20cm 27.7cm</lines>
148
149             <drawRightString x="20cm" y="27.8cm">[[ company.rml_header1 ]]</drawRightString>
150
151
152             <drawString x="1.3cm" y="27.2cm">[[ company.partner_id.name ]]</drawString>
153             <drawString x="1.3cm" y="26.8cm">[[ company.partner_id.address and company.partner_id.address[0].street ]]</drawString>
154             <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>
155             <drawString x="1.3cm" y="26.0cm">Phone:</drawString>
156             <drawRightString x="7cm" y="26.0cm">[[ company.partner_id.address and company.partner_id.address[0].phone ]]</drawRightString>
157             <drawString x="1.3cm" y="25.6cm">Mail:</drawString>
158             <drawRightString x="7cm" y="25.6cm">[[ company.partner_id.address and company.partner_id.address[0].email ]]</drawRightString>
159             <lines>1.3cm 25.5cm 7cm 25.5cm</lines>
160
161             <!--page bottom-->
162
163             <lines>1.2cm 2.15cm 19.9cm 2.15cm</lines>
164
165             <drawCentredString x="10.5cm" y="1.7cm">[[ company.rml_footer1 ]]</drawCentredString>
166             <drawCentredString x="10.5cm" y="1.25cm">[[ company.rml_footer2 ]]</drawCentredString>
167             <drawCentredString x="10.5cm" y="0.8cm">Contact : [[ user.name ]] - Page: <pageNumber/></drawCentredString>
168         </pageGraphics>
169     </pageTemplate>
170 </header>"""
171     _defaults = {
172         'currency_id': _get_euro,
173         'rml_header':_get_header,
174         'rml_header2': _get_header2
175     }
176
177     _constraints = [
178         (_check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
179     ]
180
181 res_company()
182