[MERGE] Forward-port saas-5 up to d9cda97
[odoo/odoo.git] / openerp / 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 import os
23 import re
24 import openerp
25 from openerp import SUPERUSER_ID, tools
26 from openerp.osv import fields, osv
27 from openerp.tools.translate import _
28 from openerp.tools.safe_eval import safe_eval as eval
29 from openerp.tools import image_resize_image
30   
31 class multi_company_default(osv.osv):
32     """
33     Manage multi company default value
34     """
35     _name = 'multi_company.default'
36     _description = 'Default multi company'
37     _order = 'company_id,sequence,id'
38
39     _columns = {
40         'sequence': fields.integer('Sequence'),
41         'name': fields.char('Name', required=True, help='Name it to easily find a record'),
42         'company_id': fields.many2one('res.company', 'Main Company', required=True,
43             help='Company where the user is connected'),
44         'company_dest_id': fields.many2one('res.company', 'Default Company', required=True,
45             help='Company to store the current record'),
46         'object_id': fields.many2one('ir.model', 'Object', required=True,
47             help='Object affected by this rule'),
48         'expression': fields.char('Expression', required=True,
49             help='Expression, must be True to match\nuse context.get or user (browse)'),
50         'field_id': fields.many2one('ir.model.fields', 'Field', help='Select field property'),
51     }
52
53     _defaults = {
54         'expression': 'True',
55         'sequence': 100,
56     }
57
58     def copy(self, cr, uid, id, default=None, context=None):
59         """
60         Add (copy) in the name when duplicate record
61         """
62         if not context:
63             context = {}
64         if not default:
65             default = {}
66         company = self.browse(cr, uid, id, context=context)
67         default = default.copy()
68         default['name'] = company.name + _(' (copy)')
69         return super(multi_company_default, self).copy(cr, uid, id, default, context=context)
70
71 multi_company_default()
72
73 class res_company(osv.osv):
74     _name = "res.company"
75     _description = 'Companies'
76     _order = 'name'
77     
78     def _get_address_data(self, cr, uid, ids, field_names, arg, context=None):
79         """ Read the 'address' functional fields. """
80         result = {}
81         part_obj = self.pool.get('res.partner')
82         for company in self.browse(cr, uid, ids, context=context):
83             result[company.id] = {}.fromkeys(field_names, False)
84             if company.partner_id:
85                 address_data = part_obj.address_get(cr, openerp.SUPERUSER_ID, [company.partner_id.id], adr_pref=['default'])
86                 if address_data['default']:
87                     address = part_obj.read(cr, openerp.SUPERUSER_ID, address_data['default'], field_names, context=context)
88                     for field in field_names:
89                         result[company.id][field] = address[field] or False
90         return result
91
92     def _set_address_data(self, cr, uid, company_id, name, value, arg, context=None):
93         """ Write the 'address' functional fields. """
94         company = self.browse(cr, uid, company_id, context=context)
95         if company.partner_id:
96             part_obj = self.pool.get('res.partner')
97             address_data = part_obj.address_get(cr, uid, [company.partner_id.id], adr_pref=['default'])
98             address = address_data['default']
99             if address:
100                 part_obj.write(cr, uid, [address], {name: value or False}, context=context)
101             else:
102                 part_obj.create(cr, uid, {name: value or False, 'parent_id': company.partner_id.id}, context=context)
103         return True
104
105     def _get_logo_web(self, cr, uid, ids, _field_name, _args, context=None):
106         result = dict.fromkeys(ids, False)
107         for record in self.browse(cr, uid, ids, context=context):
108             size = (180, None)
109             result[record.id] = image_resize_image(record.partner_id.image, size)
110         return result
111         
112     def _get_companies_from_partner(self, cr, uid, ids, context=None):
113         return self.pool['res.company'].search(cr, uid, [('partner_id', 'in', ids)], context=context)
114
115     _columns = {
116         'name': fields.related('partner_id', 'name', string='Company Name', size=128, required=True, store=True, type='char'),
117         'parent_id': fields.many2one('res.company', 'Parent Company', select=True),
118         'child_ids': fields.one2many('res.company', 'parent_id', 'Child Companies'),
119         'partner_id': fields.many2one('res.partner', 'Partner', required=True),
120         'rml_header': fields.text('RML Header', required=True),
121         'rml_header1': fields.char('Company Tagline', help="Appears by default on the top right corner of your printed documents (report header)."),
122         'rml_header2': fields.text('RML Internal Header', required=True),
123         'rml_header3': fields.text('RML Internal Header for Landscape Reports', required=True),
124         'rml_footer': fields.text('Report Footer', help="Footer text displayed at the bottom of all reports."),
125         'rml_footer_readonly': fields.related('rml_footer', type='text', string='Report Footer', readonly=True),
126         'custom_footer': fields.boolean('Custom Footer', help="Check this to define the report footer manually.  Otherwise it will be filled in automatically."),
127         'font': fields.many2one('res.font', string="Font", domain=[('mode', 'in', ('Normal', 'Regular', 'all', 'Book'))],
128             help="Set the font into the report header, it will be used as default font in the RML reports of the user company"),
129         'logo': fields.related('partner_id', 'image', string="Logo", type="binary"),
130         'logo_web': fields.function(_get_logo_web, string="Logo Web", type="binary", store={
131             'res.company': (lambda s, c, u, i, x: i, ['partner_id'], 10),
132             'res.partner': (_get_companies_from_partner, ['image'], 10),
133         }),
134         'currency_id': fields.many2one('res.currency', 'Currency', required=True),
135         'currency_ids': fields.one2many('res.currency', 'company_id', 'Currency'),
136         'user_ids': fields.many2many('res.users', 'res_company_users_rel', 'cid', 'user_id', 'Accepted Users'),
137         'account_no':fields.char('Account No.'),
138         'street': fields.function(_get_address_data, fnct_inv=_set_address_data, size=128, type='char', string="Street", multi='address'),
139         'street2': fields.function(_get_address_data, fnct_inv=_set_address_data, size=128, type='char', string="Street2", multi='address'),
140         'zip': fields.function(_get_address_data, fnct_inv=_set_address_data, size=24, type='char', string="Zip", multi='address'),
141         'city': fields.function(_get_address_data, fnct_inv=_set_address_data, size=24, type='char', string="City", multi='address'),
142         'state_id': fields.function(_get_address_data, fnct_inv=_set_address_data, type='many2one', relation='res.country.state', string="Fed. State", multi='address'),
143         'bank_ids': fields.one2many('res.partner.bank','company_id', 'Bank Accounts', help='Bank accounts related to this company'),
144         'country_id': fields.function(_get_address_data, fnct_inv=_set_address_data, type='many2one', relation='res.country', string="Country", multi='address'),
145         'email': fields.related('partner_id', 'email', size=64, type='char', string="Email", store=True),
146         'phone': fields.related('partner_id', 'phone', size=64, type='char', string="Phone", store=True),
147         'fax': fields.function(_get_address_data, fnct_inv=_set_address_data, size=64, type='char', string="Fax", multi='address'),
148         'website': fields.related('partner_id', 'website', string="Website", type="char", size=64),
149         'vat': fields.related('partner_id', 'vat', string="Tax ID", type="char", size=32),
150         'company_registry': fields.char('Company Registry', size=64),
151         'rml_paper_format': fields.selection([('a4', 'A4'), ('us_letter', 'US Letter')], "Paper Format", required=True, oldname='paper_format'),
152     }
153     _sql_constraints = [
154         ('name_uniq', 'unique (name)', 'The company name must be unique !')
155     ]
156
157     def onchange_footer(self, cr, uid, ids, custom_footer, phone, fax, email, website, vat, company_registry, bank_ids, context=None):
158         if custom_footer:
159             return {}
160
161         # first line (notice that missing elements are filtered out before the join)
162         res = ' | '.join(filter(bool, [
163             phone            and '%s: %s' % (_('Phone'), phone),
164             fax              and '%s: %s' % (_('Fax'), fax),
165             email            and '%s: %s' % (_('Email'), email),
166             website          and '%s: %s' % (_('Website'), website),
167             vat              and '%s: %s' % (_('TIN'), vat),
168             company_registry and '%s: %s' % (_('Reg'), company_registry),
169         ]))
170         # second line: bank accounts
171         res_partner_bank = self.pool.get('res.partner.bank')
172         account_data = self.resolve_2many_commands(cr, uid, 'bank_ids', bank_ids, context=context)
173         account_names = res_partner_bank._prepare_name_get(cr, uid, account_data, context=context)
174         if account_names:
175             title = _('Bank Accounts') if len(account_names) > 1 else _('Bank Account')
176             res += '\n%s: %s' % (title, ', '.join(name for id, name in account_names))
177
178         return {'value': {'rml_footer': res, 'rml_footer_readonly': res}}
179     def onchange_state(self, cr, uid, ids, state_id, context=None):
180         if state_id:
181             return {'value':{'country_id': self.pool.get('res.country.state').browse(cr, uid, state_id, context).country_id.id }}
182         return {}
183         
184     def onchange_font_name(self, cr, uid, ids, font, rml_header, rml_header2, rml_header3, context=None):
185         """ To change default header style of all <para> and drawstring. """
186
187         def _change_header(header,font):
188             """ Replace default fontname use in header and setfont tag """
189             
190             default_para = re.sub('fontName.?=.?".*"', 'fontName="%s"'% font, header)
191             return re.sub('(<setFont.?name.?=.?)(".*?")(.)', '\g<1>"%s"\g<3>'% font, default_para)
192         
193         if not font:
194             return True
195         fontname = self.pool.get('res.font').browse(cr, uid, font, context=context).name
196         return {'value':{
197                         'rml_header': _change_header(rml_header, fontname),
198                         'rml_header2':_change_header(rml_header2, fontname),
199                         'rml_header3':_change_header(rml_header3, fontname)
200                         }}
201
202     def on_change_country(self, cr, uid, ids, country_id, context=None):
203         res = {'domain': {'state_id': []}}
204         currency_id = self._get_euro(cr, uid, context=context)
205         if country_id:
206             currency_id = self.pool.get('res.country').browse(cr, uid, country_id, context=context).currency_id.id
207             res['domain'] = {'state_id': [('country_id','=',country_id)]}
208         res['value'] = {'currency_id': currency_id}
209         return res
210
211     def name_search(self, cr, uid, name='', args=None, operator='ilike', context=None, limit=100):
212         if context is None:
213             context = {}
214         if context.pop('user_preference', None):
215             # We browse as superuser. Otherwise, the user would be able to
216             # select only the currently visible companies (according to rules,
217             # which are probably to allow to see the child companies) even if
218             # she belongs to some other companies.
219             user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid, context=context)
220             cmp_ids = list(set([user.company_id.id] + [cmp.id for cmp in user.company_ids]))
221             uid = SUPERUSER_ID
222             args = (args or []) + [('id', 'in', cmp_ids)]
223         return super(res_company, self).name_search(cr, uid, name=name, args=args, operator=operator, context=context, limit=limit)
224
225     def _company_default_get(self, cr, uid, object=False, field=False, context=None):
226         """
227         Check if the object for this company have a default value
228         """
229         if not context:
230             context = {}
231         proxy = self.pool.get('multi_company.default')
232         args = [
233             ('object_id.model', '=', object),
234             ('field_id', '=', field),
235         ]
236
237         ids = proxy.search(cr, uid, args, context=context)
238         user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid, context=context)
239         for rule in proxy.browse(cr, uid, ids, context):
240             if eval(rule.expression, {'context': context, 'user': user}):
241                 return rule.company_dest_id.id
242         return user.company_id.id
243
244     @tools.ormcache()
245     def _get_company_children(self, cr, uid=None, company=None):
246         if not company:
247             return []
248         ids =  self.search(cr, uid, [('parent_id','child_of',[company])])
249         return ids
250
251     def _get_partner_hierarchy(self, cr, uid, company_id, context=None):
252         if company_id:
253             parent_id = self.browse(cr, uid, company_id)['parent_id']
254             if parent_id:
255                 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
256             else:
257                 return self._get_partner_descendance(cr, uid, company_id, [], context)
258         return []
259
260     def _get_partner_descendance(self, cr, uid, company_id, descendance, context=None):
261         descendance.append(self.browse(cr, uid, company_id).partner_id.id)
262         for child_id in self._get_company_children(cr, uid, company_id):
263             if child_id != company_id:
264                 descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
265         return descendance
266
267     #
268     # This function restart the cache on the _get_company_children method
269     #
270     def cache_restart(self, cr):
271         self._get_company_children.clear_cache(self)
272
273     def create(self, cr, uid, vals, context=None):
274         if not vals.get('name', False) or vals.get('partner_id', False):
275             self.cache_restart(cr)
276             return super(res_company, self).create(cr, uid, vals, context=context)
277         obj_partner = self.pool.get('res.partner')
278         partner_id = obj_partner.create(cr, uid, {'name': vals['name'], 'is_company':True, 'image': vals.get('logo', False)}, context=context)
279         vals.update({'partner_id': partner_id})
280         self.cache_restart(cr)
281         company_id = super(res_company, self).create(cr, uid, vals, context=context)
282         obj_partner.write(cr, uid, [partner_id], {'company_id': company_id}, context=context)
283         return company_id
284
285     def write(self, cr, uid, ids, values, context=None):
286         self.cache_restart(cr)
287         return super(res_company, self).write(cr, uid, ids, values, context=context)
288
289     def _get_euro(self, cr, uid, context=None):
290         rate_obj = self.pool.get('res.currency.rate')
291         rate_id = rate_obj.search(cr, uid, [('rate', '=', 1)], context=context)
292         return rate_id and rate_obj.browse(cr, uid, rate_id[0], context=context).currency_id.id or False
293
294     def _get_logo(self, cr, uid, ids):
295         return open(os.path.join( tools.config['root_path'], 'addons', 'base', 'res', 'res_company_logo.png'), 'rb') .read().encode('base64')
296
297     def _get_font(self, cr, uid, ids):
298         font_obj = self.pool.get('res.font')
299         res = font_obj.search(cr, uid, [('family', '=', 'Helvetica'), ('mode', '=', 'all')], limit=1)
300         return res and res[0] or False       
301
302     _header = """
303 <header>
304 <pageTemplate>
305     <frame id="first" x1="28.0" y1="28.0" width="%s" height="%s"/>
306     <stylesheet>
307        <!-- Set here the default font to use for all <para> tags -->
308        <paraStyle name='Normal' fontName="DejaVuSans"/>
309     </stylesheet>
310     <pageGraphics>
311         <fill color="black"/>
312         <stroke color="black"/>
313         <setFont name="DejaVuSans" size="8"/>
314         <drawString x="%s" y="%s"> [[ formatLang(time.strftime("%%Y-%%m-%%d"), date=True) ]]  [[ time.strftime("%%H:%%M") ]]</drawString>
315         <setFont name="DejaVuSans-Bold" size="10"/>
316         <drawCentredString x="%s" y="%s">[[ company.partner_id.name ]]</drawCentredString>
317         <stroke color="#000000"/>
318         <lines>%s</lines>
319         <!-- Set here the default font to use for all <drawString> tags -->
320         <!-- don't forget to change the 2 other occurence of <setFont> above if needed --> 
321         <setFont name="DejaVuSans" size="8"/>
322     </pageGraphics>
323 </pageTemplate>
324 </header>"""
325
326     _header2 = _header % (539, 772, "1.0cm", "28.3cm", "11.1cm", "28.3cm", "1.0cm 28.1cm 20.1cm 28.1cm")
327
328     _header3 = _header % (786, 525, 25, 555, 440, 555, "25 550 818 550")
329
330     def _get_header(self,cr,uid,ids):
331         try :
332             header_file = tools.file_open(os.path.join('base', 'report', 'corporate_rml_header.rml'))
333             try:
334                 return header_file.read()
335             finally:
336                 header_file.close()
337         except:
338             return self._header_a4
339
340     _header_main = """
341 <header>
342     <pageTemplate>
343         <frame id="first" x1="1.3cm" y1="3.0cm" height="%s" width="19.0cm"/>
344          <stylesheet>
345             <!-- Set here the default font to use for all <para> tags -->
346             <paraStyle name='Normal' fontName="DejaVuSans"/>
347             <paraStyle name="main_footer" fontSize="8.0" alignment="CENTER"/>
348             <paraStyle name="main_header" fontSize="8.0" leading="10" alignment="LEFT" spaceBefore="0.0" spaceAfter="0.0"/>
349          </stylesheet>
350         <pageGraphics>
351             <!-- Set here the default font to use for all <drawString> tags -->
352             <setFont name="DejaVuSans" size="8"/>
353             <!-- You Logo - Change X,Y,Width and Height -->
354             <image x="1.3cm" y="%s" height="40.0" >[[ company.logo or removeParentNode('image') ]]</image>
355             <fill color="black"/>
356             <stroke color="black"/>
357
358             <!-- page header -->
359             <lines>1.3cm %s 20cm %s</lines>
360             <drawRightString x="20cm" y="%s">[[ company.rml_header1 ]]</drawRightString>
361             <drawString x="1.3cm" y="%s">[[ company.partner_id.name ]]</drawString>
362             <place x="1.3cm" y="%s" height="1.8cm" width="15.0cm">
363                 <para style="main_header">[[ display_address(company.partner_id) or  '' ]]</para>
364             </place>
365             <drawString x="1.3cm" y="%s">Phone:</drawString>
366             <drawRightString x="7cm" y="%s">[[ company.partner_id.phone or '' ]]</drawRightString>
367             <drawString x="1.3cm" y="%s">Mail:</drawString>
368             <drawRightString x="7cm" y="%s">[[ company.partner_id.email or '' ]]</drawRightString>
369             <lines>1.3cm %s 7cm %s</lines>
370
371             <!-- left margin -->
372             <rotate degrees="90"/>
373             <fill color="grey"/>
374             <drawString x="2.65cm" y="-0.4cm">generated by OpenERP.com</drawString>
375             <fill color="black"/>
376             <rotate degrees="-90"/>
377
378             <!--page bottom-->
379             <lines>1.2cm 2.65cm 19.9cm 2.65cm</lines>
380             <place x="1.3cm" y="0cm" height="2.55cm" width="19.0cm">
381                 <para style="main_footer">[[ company.rml_footer ]]</para>
382                 <para style="main_footer">Contact : [[ user.name ]] - Page: <pageNumber/></para>
383             </place>
384         </pageGraphics>
385     </pageTemplate>
386 </header>"""
387
388     _header_a4 = _header_main % ('21.7cm', '27.7cm', '27.7cm', '27.7cm', '27.8cm', '27.3cm', '25.3cm', '25.0cm', '25.0cm', '24.6cm', '24.6cm', '24.5cm', '24.5cm')
389     _header_letter = _header_main % ('20cm', '26.0cm', '26.0cm', '26.0cm', '26.1cm', '25.6cm', '23.6cm', '23.3cm', '23.3cm', '22.9cm', '22.9cm', '22.8cm', '22.8cm')
390
391     def onchange_rml_paper_format(self, cr, uid, ids, rml_paper_format, context=None):
392         if rml_paper_format == 'us_letter':
393             return {'value': {'rml_header': self._header_letter}}
394         return {'value': {'rml_header': self._header_a4}}
395
396     def act_discover_fonts(self, cr, uid, ids, context=None):
397         return self.pool.get("res.font").font_scan(cr, uid, context=context)
398
399     _defaults = {
400         'currency_id': _get_euro,
401         'rml_paper_format': 'a4',
402         'rml_header':_get_header,
403         'rml_header2': _header2,
404         'rml_header3': _header3,
405         'logo':_get_logo,
406         'font':_get_font,
407     }
408
409     _constraints = [
410         (osv.osv._check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
411     ]
412
413 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: