merge
[odoo/odoo.git] / bin / addons / base / res / res_company.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 from osv import osv
23 from osv import fields
24 import os
25 import tools
26 from tools.translate import _
27
28
29 class multi_company_default(osv.osv):
30     """
31     Manage multi company default value
32     """
33     _name = 'multi_company.default'
34     _description = 'Default multi company'
35     _order = 'company_id,sequence,id'
36
37     _columns = {
38         'sequence': fields.integer('Sequence'),
39         'name': fields.char('Name', size=32, required=True, help='Name it to easily find a record'),
40         'company_id': fields.many2one('res.company', 'Main Company', required=True,
41             help='Company where the user is connected'),
42         'company_dest_id': fields.many2one('res.company', 'Default Company', required=True,
43             help='Company to store the current record'),
44         'object_id': fields.many2one('ir.model', 'Object', required=True,
45             help='Object affect by this rules'),
46         'expression': fields.char('Expression', size=32, required=True,
47             help='Expression, must be True to match'),
48     }
49
50     _defaults = {
51         'expression': lambda *a: 'True',
52         'sequence': lambda *a: 100,
53     }
54
55     def copy(self, cr, uid, id, default=None, context=None):
56         """
57         Add (copy) in the name when duplicate record
58         """
59         if not context:
60             context = {}
61         if not default:
62             default = {}
63         company = self.browse(cr, uid, id, context=context)
64         default = default.copy()
65         default['name'] = company.name + _(' (copy)')
66         return super(multi_company_default, self).copy(cr, uid, id, default, context=context)
67
68 multi_company_default()
69
70
71 class res_company(osv.osv):
72     _name = "res.company"
73     _description = 'Companies'
74     _columns = {
75         'name': fields.char('Company Name', size=64, required=True),
76         'parent_id': fields.many2one('res.company', 'Parent Company', select=True),
77         'child_ids': fields.one2many('res.company', 'parent_id', 'Child Companies'),
78         'partner_id': fields.many2one('res.partner', 'Partner', required=True),
79         'rml_header1': fields.char('Report Header', size=200),
80         'rml_footer1': fields.char('Report Footer 1', size=200),
81         'rml_footer2': fields.char('Report Footer 2', size=200),
82         'rml_header' : fields.text('RML Header'),
83         'rml_header2' : fields.text('RML Internal Header'),
84         'logo' : fields.binary('Logo'),
85         'currency_id': fields.many2one('res.currency', 'Currency', required=True),
86         'currency_ids': fields.one2many('res.currency', 'company_id', 'Currency'),
87         'user_ids': fields.many2many('res.users', 'res_company_users_rel', 'cid', 'user_id', 'Accepted Users')
88     }
89
90
91     def _company_default_get(self, cr, uid, object=False, context=None):
92         """
93         Check if the object for this company have a default value
94         """
95         if not context:
96             context = {}
97         proxy = self.pool.get('multi_company.default')
98         ids = proxy.search(cr, uid, [('object_id.model', '=', object)])
99         for rule in proxy.browse(cr, uid, ids, context):
100             user = self.pool.get('res.users').browse(cr, uid, uid)
101             if eval(rule.expression, {'context': context, 'user': user}):
102                 return rule.company_dest_id.id
103         return self.pool.get('res.users').browse(cr, uid, uid).company_id.id
104
105     def _get_child_ids(self, cr, uid, uid2, context={}):
106         company = self.pool.get('res.users').company_get(cr, uid, uid2)
107         ids = self._get_company_children(cr, uid, company)
108         return ids
109
110     def _get_company_children(self, cr, uid=None, company=None):
111         if not company:
112             return []
113         ids =  self.search(cr, uid, [('parent_id','child_of',[company])])
114         return ids
115     _get_company_children = tools.cache()(_get_company_children)
116
117     def _get_partner_hierarchy(self, cr, uid, company_id, context={}):
118         if company_id:
119             parent_id = self.browse(cr, uid, company_id)['parent_id']
120             if parent_id:
121                 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
122             else:
123                 return self._get_partner_descendance(cr, uid, company_id, [], context)
124         return []
125
126     def _get_partner_descendance(self, cr, uid, company_id, descendance, context={}):
127         descendance.append(self.browse(cr, uid, company_id).partner_id.id)
128         for child_id in self._get_company_children(cr, uid, company_id):
129             if child_id != company_id:
130                 descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
131         return descendance
132
133     #
134     # This function restart the cache on the _get_company_children method
135     #
136     def cache_restart(self, cr):
137         self._get_company_children.clear_cache(cr.dbname)
138
139     def create(self, cr, *args, **argv):
140         self.cache_restart(cr)
141         return super(res_company, self).create(cr, *args, **argv)
142
143     def write(self, cr, *args, **argv):
144         self.cache_restart(cr)
145         # Restart the cache on the company_get method
146         self.pool.get('ir.rule').domain_get.clear_cache(cr.dbname)
147         return super(res_company, self).write(cr, *args, **argv)
148
149     def _get_euro(self, cr, uid, context={}):
150         try:
151             return self.pool.get('res.currency').search(cr, uid, [])[0]
152         except:
153             return False
154
155     def _check_recursion(self, cr, uid, ids):
156         level = 100
157         while len(ids):
158             cr.execute('select distinct parent_id from res_company where id in ('+','.join(map(str, ids))+')')
159             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
160             if not level:
161                 return False
162             level -= 1
163         return True
164
165     def _get_header2(self,cr,uid,ids):
166         return """
167         <header>
168         <pageTemplate>
169         <frame id="first" x1="1.3cm" y1="1.5cm" width="18.4cm" height="26.5cm"/>
170         <pageGraphics>
171         <fill color="black"/>
172         <stroke color="black"/>
173         <setFont name="DejaVu Sans" size="8"/>
174         <drawString x="1.3cm" y="28.3cm"> [[ formatLang(time.strftime("%Y-%m-%d"), date=True) ]]  [[ time.strftime("%H:%M") ]]</drawString>
175         <setFont name="DejaVu Sans Bold" size="10"/>
176         <drawString x="9.8cm" y="28.3cm">[[ company.partner_id.name ]]</drawString>
177         <setFont name="DejaVu Sans" size="8"/>
178         <drawRightString x="19.7cm" y="28.3cm"><pageNumber/> /  </drawRightString>
179         <drawString x="19.8cm" y="28.3cm"><pageCount/></drawString>
180         <stroke color="#000000"/>
181         <lines>1.3cm 28.1cm 20cm 28.1cm</lines>
182         </pageGraphics>
183         </pageTemplate>
184 </header>"""
185     def _get_header(self,cr,uid,ids):
186         try :
187             return tools.file_open(os.path.join('base', 'report', 'corporate_rml_header.rml')).read()
188         except:
189             return """
190     <header>
191     <pageTemplate>
192         <frame id="first" x1="1.3cm" y1="2.5cm" height="23.0cm" width="19cm"/>
193         <pageGraphics>
194             <!-- You Logo - Change X,Y,Width and Height -->
195         <image x="1.3cm" y="27.6cm" height="40.0" >[[company.logo]]</image>
196             <setFont name="DejaVu Sans" size="8"/>
197             <fill color="black"/>
198             <stroke color="black"/>
199             <lines>1.3cm 27.7cm 20cm 27.7cm</lines>
200
201             <drawRightString x="20cm" y="27.8cm">[[ company.rml_header1 ]]</drawRightString>
202
203
204             <drawString x="1.3cm" y="27.2cm">[[ company.partner_id.name ]]</drawString>
205             <drawString x="1.3cm" y="26.8cm">[[ company.partner_id.address and company.partner_id.address[0].street or  '' ]]</drawString>
206             <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>
207             <drawString x="1.3cm" y="26.0cm">Phone:</drawString>
208             <drawRightString x="7cm" y="26.0cm">[[ company.partner_id.address and company.partner_id.address[0].phone or '' ]]</drawRightString>
209             <drawString x="1.3cm" y="25.6cm">Mail:</drawString>
210             <drawRightString x="7cm" y="25.6cm">[[ company.partner_id.address and company.partner_id.address[0].email or '' ]]</drawRightString>
211             <lines>1.3cm 25.5cm 7cm 25.5cm</lines>
212
213             <!--page bottom-->
214
215             <lines>1.2cm 2.15cm 19.9cm 2.15cm</lines>
216
217             <drawCentredString x="10.5cm" y="1.7cm">[[ company.rml_footer1 ]]</drawCentredString>
218             <drawCentredString x="10.5cm" y="1.25cm">[[ company.rml_footer2 ]]</drawCentredString>
219             <drawCentredString x="10.5cm" y="0.8cm">Contact : [[ user.name ]] - Page: <pageNumber/></drawCentredString>
220         </pageGraphics>
221     </pageTemplate>
222 </header>"""
223     _defaults = {
224         'currency_id': _get_euro,
225         'rml_header':_get_header,
226         'rml_header2': _get_header2
227     }
228
229     _constraints = [
230         (_check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
231     ]
232
233 res_company()
234
235 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
236