Modifs Report
[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="1cm" y1="1.5cm" width="19.0cm" height="26.5cm"/>
118                 <pageGraphics>
119                 <setFont name="Helvetica" size="10"/>
120                 <fill color="black"/>
121                 <stroke color="black"/>
122                 <setFont name="Helvetica" size="8"/>
123                 <!--<drawString x="1cm" y="28.3cm">Currency: [[ company.currency_id.name ]]</drawString>-->
124                 <drawString x="1cm" 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.5cm" y="28.3cm">[[ company.partner_id.name ]]</drawString>
127                 <setFont name="Helvetica" size="8"/>
128                 <drawRightString x="19.5cm" y="28.3cm"><pageNumber/> /  </drawRightString>
129                 <drawString x="19.6cm" y="28.3cm"><pageCount/></drawString>
130                 <stroke color="#aaaaaa"/>
131                 <!--<drawString x="1cm" y="28.3cm">[[user.name]]</drawString>-->
132                 <lines>1cm 28.1cm 20cm 28.1cm</lines>
133                 </pageGraphics>
134                 </pageTemplate>
135 </header>"""
136         def _get_header(self,cr,uid,ids):
137                 try :
138                         return tools.file_open('custom/corporate_rml_header.rml').read()
139                 except:
140                         return """
141         <header>
142         <pageTemplate>
143                 <frame id="first" x1="1cm" y1="2.5cm" height="23.0cm" width="19cm"/>
144                 <pageGraphics>
145                         <!-- You Logo - Change X,Y,Width and Height -->
146                 <image x="1cm" y="27.6cm" height="40.0" >[[company.logo]]</image>
147                         <setFont name="Helvetica" size="8"/>
148                         <fill color="black"/>
149                         <stroke color="black"/>
150                         <!--drawString x="1cm" y="27.8cm">[[ company.partner_id.name ]]</drawString-->
151                         <lines>1cm 27.7cm 20cm 27.7cm</lines>
152
153                         <drawRightString x="20cm" y="27.8cm">[[ company.rml_header1 ]]</drawRightString>
154
155
156                         <drawString x="1cm" y="27.2cm">[[ company.partner_id.name ]]</drawString>
157                         <drawString x="1cm" y="26.8cm">[[ company.partner_id.address and company.partner_id.address[0].street ]]</drawString>
158                         <drawString x="1cm" 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>
159                         <drawString x="1cm" y="26.0cm">Phone:</drawString>
160                         <drawRightString x="7cm" y="26.0cm">[[ company.partner_id.address and company.partner_id.address[0].phone ]]</drawRightString>
161                         <drawString x="1cm" y="25.6cm">Mail:</drawString>
162                         <drawRightString x="7cm" y="25.6cm">[[ company.partner_id.address and company.partner_id.address[0].email ]]</drawRightString>
163                         <lines>1cm 25.5cm 7cm 25.5cm</lines>
164
165                         <!--page bottom-->
166
167                         <lines>1.5cm 2.15cm 19.9cm 2.15cm</lines>
168
169                         <drawCentredString x="10.5cm" y="1.7cm">[[ company.rml_footer1 ]]</drawCentredString>
170                         <drawCentredString x="10.5cm" y="1.25cm">[[ company.rml_footer2 ]]</drawCentredString>
171                         <drawCentredString x="10.5cm" y="0.8cm">Contact : [[ user.name ]] - Page: <pageNumber/></drawCentredString>
172                 </pageGraphics>
173         </pageTemplate>
174 </header>"""
175         _defaults = {
176                 'currency_id': _get_euro,
177                 'rml_header':_get_header,
178                 'rml_header2': _get_header2
179         }
180
181         _constraints = [
182                 (_check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
183         ]
184
185 res_company()
186