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