add new field to have company wise seprate currency
[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         'currency_ids': fields.one2many('res.currency', 'company_id', 'Currency Rates'),
50     }
51     
52     def _get_child_ids(self, cr, uid, uid2, context={}):
53         company = self.pool.get('res.users').company_get(cr, uid, uid2)
54         ids = self._get_company_children(cr, uid, company)
55         return ids
56
57     def _get_company_children(self, cr, uid=None, company=None):
58         if not company:
59             return []
60         ids =  self.search(cr, uid, [('parent_id','child_of',[company])])
61         return ids
62     _get_company_children = tools.cache()(_get_company_children)
63
64     def _get_partner_hierarchy(self, cr, uid, company_id, context={}):
65         if company_id:
66             parent_id = self.browse(cr, uid, company_id)['parent_id']
67             if parent_id:
68                 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
69             else:
70                 return self._get_partner_descendance(cr, uid, company_id, [], context)
71         return []
72
73     def _get_partner_descendance(self, cr, uid, company_id, descendance, context={}):
74         descendance.append(self.browse(cr, uid, company_id).partner_id.id)
75         for child_id in self._get_company_children(cr, uid, company_id):
76             if child_id != company_id:
77                 descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
78         return descendance
79
80     def __init__(self, *args, **argv):
81         return super(res_company, self).__init__(*args, **argv)
82
83     #
84     # This function restart the cache on the _get_company_children method
85     #
86     def cache_restart(self, uid=None):
87         self._get_company_children()
88
89     def create(self, *args, **argv):
90         self.cache_restart()
91         return super(res_company, self).create(*args, **argv)
92
93     def write(self, *args, **argv):
94         self.cache_restart()
95         # Restart the cache on the company_get method
96         self.pool.get('ir.rule').domain_get()
97         return super(res_company, self).write(*args, **argv)
98
99     def _get_euro(self, cr, uid, context={}):
100         try:
101             return self.pool.get('res.currency').search(cr, uid, [])[0]
102         except:
103             return False
104     
105     def _check_recursion(self, cr, uid, ids):
106         level = 100
107         while len(ids):
108             cr.execute('select distinct parent_id from res_company where id in ('+','.join(map(str,ids))+')')
109             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
110             if not level:
111                 return False
112             level -= 1
113         return True
114     
115     def _get_header2(self,cr,uid,ids):
116         return """
117         <header>
118         <pageTemplate>
119         <frame id="first" x1="1.3cm" y1="1.5cm" width="18.4cm" height="26.5cm"/>
120         <pageGraphics>
121         <fill color="black"/>
122         <stroke color="black"/>
123         <setFont name="Helvetica" size="8"/>
124         <drawString x="1.3cm" 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.8cm" y="28.3cm">[[ company.partner_id.name ]]</drawString>
127         <setFont name="Helvetica" size="8"/>
128         <drawRightString x="19.7cm" y="28.3cm"><pageNumber/> /  </drawRightString>
129         <drawString x="19.8cm" y="28.3cm"><pageCount/></drawString>
130         <stroke color="#aaaaaa"/>
131         <lines>1.3cm 28.1cm 20cm 28.1cm</lines>
132         </pageGraphics>
133         </pageTemplate>
134 </header>"""
135     def _get_header(self,cr,uid,ids):
136         try :
137             return tools.file_open('custom/corporate_rml_header.rml').read()
138         except:
139             return """
140     <header>
141     <pageTemplate>
142         <frame id="first" x1="1.3cm" y1="2.5cm" height="23.0cm" width="19cm"/>
143         <pageGraphics>
144             <!-- You Logo - Change X,Y,Width and Height -->
145         <image x="1.3cm" y="27.6cm" height="40.0" >[[company.logo]]</image>
146             <setFont name="Helvetica" size="8"/>
147             <fill color="black"/>
148             <stroke color="black"/>
149             <lines>1.3cm 27.7cm 20cm 27.7cm</lines>
150
151             <drawRightString x="20cm" y="27.8cm">[[ company.rml_header1 ]]</drawRightString>
152
153
154             <drawString x="1.3cm" y="27.2cm">[[ company.partner_id.name ]]</drawString>
155             <drawString x="1.3cm" y="26.8cm">[[ company.partner_id.address and company.partner_id.address[0].street ]]</drawString>
156             <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>
157             <drawString x="1.3cm" y="26.0cm">Phone:</drawString>
158             <drawRightString x="7cm" y="26.0cm">[[ company.partner_id.address and company.partner_id.address[0].phone ]]</drawRightString>
159             <drawString x="1.3cm" y="25.6cm">Mail:</drawString>
160             <drawRightString x="7cm" y="25.6cm">[[ company.partner_id.address and company.partner_id.address[0].email ]]</drawRightString>
161             <lines>1.3cm 25.5cm 7cm 25.5cm</lines>
162
163             <!--page bottom-->
164
165             <lines>1.2cm 2.15cm 19.9cm 2.15cm</lines>
166
167             <drawCentredString x="10.5cm" y="1.7cm">[[ company.rml_footer1 ]]</drawCentredString>
168             <drawCentredString x="10.5cm" y="1.25cm">[[ company.rml_footer2 ]]</drawCentredString>
169             <drawCentredString x="10.5cm" y="0.8cm">Contact : [[ user.name ]] - Page: <pageNumber/></drawCentredString>
170         </pageGraphics>
171     </pageTemplate>
172 </header>"""
173     _defaults = {
174         'currency_id': _get_euro,
175         'rml_header':_get_header,
176         'rml_header2': _get_header2
177     }
178
179     _constraints = [
180         (_check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
181     ]
182
183 res_company()
184
185
186 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
187