plan to supply module for the multi currency for multi company
[odoo/odoo.git] / bin / addons / base / res / res_company.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # $Id$
7 #
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
13 # Service Company
14 #
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.
19 #
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.
24 #
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.
28 #
29 ##############################################################################
30
31 from osv import fields,osv
32 import tools
33
34 class res_company(osv.osv):
35     _name = "res.company"
36
37     _columns = {
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     }
50     
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)
54         return ids
55
56     def _get_company_children(self, cr, uid=None, company=None):
57         if not company:
58             return []
59         ids =  self.search(cr, uid, [('parent_id','child_of',[company])])
60         return ids
61     _get_company_children = tools.cache()(_get_company_children)
62
63     def _get_partner_hierarchy(self, cr, uid, company_id, context={}):
64         if company_id:
65             parent_id = self.browse(cr, uid, company_id)['parent_id']
66             if parent_id:
67                 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
68             else:
69                 return self._get_partner_descendance(cr, uid, company_id, [], context)
70         return []
71
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)
77         return descendance
78
79     def __init__(self, *args, **argv):
80         return super(res_company, self).__init__(*args, **argv)
81
82     #
83     # This function restart the cache on the _get_company_children method
84     #
85     def cache_restart(self, uid=None):
86         self._get_company_children()
87
88     def create(self, *args, **argv):
89         self.cache_restart()
90         return super(res_company, self).create(*args, **argv)
91
92     def write(self, *args, **argv):
93         self.cache_restart()
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)
97
98     def _get_euro(self, cr, uid, context={}):
99         try:
100             return self.pool.get('res.currency').search(cr, uid, [])[0]
101         except:
102             return False
103     
104     def _check_recursion(self, cr, uid, ids):
105         level = 100
106         while len(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()))
109             if not level:
110                 return False
111             level -= 1
112         return True
113     
114     def _get_header2(self,cr,uid,ids):
115         return """
116         <header>
117         <pageTemplate>
118         <frame id="first" x1="1.3cm" y1="1.5cm" width="18.4cm" height="26.5cm"/>
119         <pageGraphics>
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>
131         </pageGraphics>
132         </pageTemplate>
133 </header>"""
134     def _get_header(self,cr,uid,ids):
135         try :
136             return tools.file_open('custom/corporate_rml_header.rml').read()
137         except:
138             return """
139     <header>
140     <pageTemplate>
141         <frame id="first" x1="1.3cm" y1="2.5cm" height="23.0cm" width="19cm"/>
142         <pageGraphics>
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>
149
150             <drawRightString x="20cm" y="27.8cm">[[ company.rml_header1 ]]</drawRightString>
151
152
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>
161
162             <!--page bottom-->
163
164             <lines>1.2cm 2.15cm 19.9cm 2.15cm</lines>
165
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>
169         </pageGraphics>
170     </pageTemplate>
171 </header>"""
172     _defaults = {
173         'currency_id': _get_euro,
174         'rml_header':_get_header,
175         'rml_header2': _get_header2
176     }
177
178     _constraints = [
179         (_check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
180     ]
181
182 res_company()
183
184
185 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
186