1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
23 from osv import fields
27 from openerp import SUPERUSER_ID
28 from tools.translate import _
29 from tools.safe_eval import safe_eval as eval
31 class multi_company_default(osv.osv):
33 Manage multi company default value
35 _name = 'multi_company.default'
36 _description = 'Default multi company'
37 _order = 'company_id,sequence,id'
40 'sequence': fields.integer('Sequence'),
41 'name': fields.char('Name', size=256, 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', size=256, 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'),
54 'expression': lambda *a: 'True',
55 'sequence': lambda *a: 100,
58 def copy(self, cr, uid, id, default=None, context=None):
60 Add (copy) in the name when duplicate record
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)
71 multi_company_default()
73 class res_company(osv.osv):
75 _description = 'Companies'
78 def _get_address_data(self, cr, uid, ids, field_names, arg, context=None):
79 """ Read the 'address' functional fields. """
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
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']
100 part_obj.write(cr, uid, [address], {name: value or False})
102 part_obj.create(cr, uid, {name: value or False, 'parent_id': company.partner_id.id}, context=context)
106 'name': fields.related('partner_id', 'name', string='Company Name', size=128, required=True, store=True, type='char'),
107 'parent_id': fields.many2one('res.company', 'Parent Company', select=True),
108 'child_ids': fields.one2many('res.company', 'parent_id', 'Child Companies'),
109 'partner_id': fields.many2one('res.partner', 'Partner', required=True),
110 'rml_header': fields.text('RML Header', required=True),
111 'rml_header1': fields.char('Company Slogan', size=200, help="Appears by default on the top right corner of your printed documents (report header)."),
112 'rml_header2': fields.text('RML Internal Header', required=True),
113 'rml_header3': fields.text('RML Internal Header for Landscape Reports', required=True),
114 'rml_footer': fields.text('Report Footer', help="Footer text displayed at the bottom of all reports."),
115 'rml_footer_readonly': fields.related('rml_footer', type='text', string='Report Footer', readonly=True),
116 'custom_footer': fields.boolean('Custom Footer', help="Check this to define the report footer manually. Otherwise it will be filled in automatically."),
117 'logo': fields.related('partner_id', 'image', string="Logo", type="binary"),
118 'currency_id': fields.many2one('res.currency', 'Currency', required=True),
119 'currency_ids': fields.one2many('res.currency', 'company_id', 'Currency'),
120 'user_ids': fields.many2many('res.users', 'res_company_users_rel', 'cid', 'user_id', 'Accepted Users'),
121 'account_no':fields.char('Account No.', size=64),
122 'street': fields.function(_get_address_data, fnct_inv=_set_address_data, size=128, type='char', string="Street", multi='address'),
123 'street2': fields.function(_get_address_data, fnct_inv=_set_address_data, size=128, type='char', string="Street2", multi='address'),
124 'zip': fields.function(_get_address_data, fnct_inv=_set_address_data, size=24, type='char', string="Zip", multi='address'),
125 'city': fields.function(_get_address_data, fnct_inv=_set_address_data, size=24, type='char', string="City", multi='address'),
126 'state_id': fields.function(_get_address_data, fnct_inv=_set_address_data, type='many2one', domain="[('country_id', '=', country_id)]", relation='res.country.state', string="Fed. State", multi='address'),
127 'bank_ids': fields.one2many('res.partner.bank','company_id', 'Bank Accounts', help='Bank accounts related to this company'),
128 'country_id': fields.function(_get_address_data, fnct_inv=_set_address_data, type='many2one', relation='res.country', string="Country", multi='address'),
129 'email': fields.function(_get_address_data, fnct_inv=_set_address_data, size=64, type='char', string="Email", multi='address'),
130 'phone': fields.function(_get_address_data, fnct_inv=_set_address_data, size=64, type='char', string="Phone", multi='address'),
131 'fax': fields.function(_get_address_data, fnct_inv=_set_address_data, size=64, type='char', string="Fax", multi='address'),
132 'website': fields.related('partner_id', 'website', string="Website", type="char", size=64),
133 'vat': fields.related('partner_id', 'vat', string="Tax ID", type="char", size=32),
134 'company_registry': fields.char('Company Registry', size=64),
135 'paper_format': fields.selection([('a4', 'A4'), ('us_letter', 'US Letter')], "Paper Format", required=True),
138 ('name_uniq', 'unique (name)', 'The company name must be unique !')
141 def onchange_footer(self, cr, uid, ids, custom_footer, phone, fax, email, website, vat, company_registry, bank_ids, context=None):
145 # first line (notice that missing elements are filtered out before the join)
146 res = ' | '.join(filter(bool, [
147 phone and '%s: %s' % (_('Phone'), phone),
148 fax and '%s: %s' % (_('Fax'), fax),
149 email and '%s: %s' % (_('Email'), email),
150 website and '%s: %s' % (_('Website'), website),
151 vat and '%s: %s' % (_('TIN'), vat),
152 company_registry and '%s: %s' % (_('Reg'), company_registry),
154 # second line: bank accounts
155 res_partner_bank = self.pool.get('res.partner.bank')
156 account_data = self.resolve_2many_commands(cr, uid, 'bank_ids', bank_ids, context=context)
157 account_names = res_partner_bank._prepare_name_get(cr, uid, account_data, context=context)
159 title = _('Bank Accounts') if len(account_names) > 1 else _('Bank Account')
160 res += '\n%s: %s' % (title, ', '.join(name for id, name in account_names))
162 return {'value': {'rml_footer': res, 'rml_footer_readonly': res}}
164 def on_change_country(self, cr, uid, ids, country_id, context=None):
165 currency_id = self._get_euro(cr, uid, context=context)
167 currency_id = self.pool.get('res.country').browse(cr, uid, country_id, context=context).currency_id.id
168 return {'value': {'currency_id': currency_id}}
170 def _search(self, cr, uid, args, offset=0, limit=None, order=None,
171 context=None, count=False, access_rights_uid=None):
174 if context.get('user_preference'):
175 # We browse as superuser. Otherwise, the user would be able to
176 # select only the currently visible companies (according to rules,
177 # which are probably to allow to see the child companies) even if
178 # she belongs to some other companies.
179 user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid, context=context)
180 cmp_ids = list(set([user.company_id.id] + [cmp.id for cmp in user.company_ids]))
182 return super(res_company, self)._search(cr, uid, args, offset=offset, limit=limit, order=order,
183 context=context, count=count, access_rights_uid=access_rights_uid)
185 def _company_default_get(self, cr, uid, object=False, field=False, context=None):
187 Check if the object for this company have a default value
191 proxy = self.pool.get('multi_company.default')
193 ('object_id.model', '=', object),
194 ('field_id', '=', field),
197 ids = proxy.search(cr, uid, args, context=context)
198 user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid, context=context)
199 for rule in proxy.browse(cr, uid, ids, context):
200 if eval(rule.expression, {'context': context, 'user': user}):
201 return rule.company_dest_id.id
202 return user.company_id.id
205 def _get_company_children(self, cr, uid=None, company=None):
208 ids = self.search(cr, uid, [('parent_id','child_of',[company])])
211 def _get_partner_hierarchy(self, cr, uid, company_id, context=None):
213 parent_id = self.browse(cr, uid, company_id)['parent_id']
215 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
217 return self._get_partner_descendance(cr, uid, company_id, [], context)
220 def _get_partner_descendance(self, cr, uid, company_id, descendance, context=None):
221 descendance.append(self.browse(cr, uid, company_id).partner_id.id)
222 for child_id in self._get_company_children(cr, uid, company_id):
223 if child_id != company_id:
224 descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
228 # This function restart the cache on the _get_company_children method
230 def cache_restart(self, cr):
231 self._get_company_children.clear_cache(self)
233 def create(self, cr, uid, vals, context=None):
234 if not vals.get('name', False) or vals.get('partner_id', False):
235 self.cache_restart(cr)
236 return super(res_company, self).create(cr, uid, vals, context=context)
237 obj_partner = self.pool.get('res.partner')
238 partner_id = obj_partner.create(cr, uid, {'name': vals['name'], 'is_company':True}, context=context)
239 vals.update({'partner_id': partner_id})
240 self.cache_restart(cr)
241 company_id = super(res_company, self).create(cr, uid, vals, context=context)
242 obj_partner.write(cr, uid, partner_id, {'company_id': company_id}, context=context)
245 def write(self, cr, uid, ids, values, context=None):
246 self.cache_restart(cr)
247 return super(res_company, self).write(cr, uid, ids, values, context=context)
249 def _get_euro(self, cr, uid, context=None):
250 rate_obj = self.pool.get('res.currency.rate')
251 rate_id = rate_obj.search(cr, uid, [('rate', '=', 1)], context=context)
252 return rate_id and rate_obj.browse(cr, uid, rate_id[0], context=context).currency_id.id or False
254 def _get_logo(self, cr, uid, ids):
255 return open(os.path.join( tools.config['root_path'], 'addons', 'base', 'res', 'res_company_logo.png'), 'rb') .read().encode('base64')
260 <frame id="first" x1="28.0" y1="28.0" width="%s" height="%s"/>
262 <fill color="black"/>
263 <stroke color="black"/>
264 <setFont name="DejaVu Sans" size="8"/>
265 <drawString x="%s" y="%s"> [[ formatLang(time.strftime("%%Y-%%m-%%d"), date=True) ]] [[ time.strftime("%%H:%%M") ]]</drawString>
266 <setFont name="DejaVu Sans Bold" size="10"/>
267 <drawCentredString x="%s" y="%s">[[ company.partner_id.name ]]</drawCentredString>
268 <stroke color="#000000"/>
274 _header2 = _header % (539, 772, "1.0cm", "28.3cm", "11.1cm", "28.3cm", "1.0cm 28.1cm 20.1cm 28.1cm")
276 _header3 = _header % (786, 525, 25, 555, 440, 555, "25 550 818 550")
278 def _get_header(self,cr,uid,ids):
280 header_file = tools.file_open(os.path.join('base', 'report', 'corporate_rml_header.rml'))
282 return header_file.read()
286 return self._header_a4
291 <frame id="first" x1="1.3cm" y1="3.0cm" height="%s" width="19.0cm"/>
293 <paraStyle name="main_footer" fontName="DejaVu Sans" fontSize="8.0" alignment="CENTER"/>
294 <paraStyle name="main_header" fontName="DejaVu Sans" fontSize="8.0" leading="10" alignment="LEFT" spaceBefore="0.0" spaceAfter="0.0"/>
297 <!-- You Logo - Change X,Y,Width and Height -->
298 <image x="1.3cm" y="%s" height="40.0" >[[ company.logo or removeParentNode('image') ]]</image>
299 <setFont name="DejaVu Sans" size="8"/>
300 <fill color="black"/>
301 <stroke color="black"/>
304 <lines>1.3cm %s 20cm %s</lines>
305 <drawRightString x="20cm" y="%s">[[ company.rml_header1 ]]</drawRightString>
306 <drawString x="1.3cm" y="%s">[[ company.partner_id.name ]]</drawString>
307 <place x="1.3cm" y="%s" height="1.55cm" width="15.0cm">
308 <para style="main_header">[[ display_address(company.partner_id) or '' ]]</para>
310 <drawString x="1.3cm" y="%s">Phone:</drawString>
311 <drawRightString x="7cm" y="%s">[[ company.partner_id.phone or '' ]]</drawRightString>
312 <drawString x="1.3cm" y="%s">Mail:</drawString>
313 <drawRightString x="7cm" y="%s">[[ company.partner_id.email or '' ]]</drawRightString>
314 <lines>1.3cm %s 7cm %s</lines>
317 <rotate degrees="90"/>
319 <drawString x="2.65cm" y="-0.4cm">produced by OpenERP.com</drawString>
320 <fill color="black"/>
321 <rotate degrees="-90"/>
324 <lines>1.2cm 2.65cm 19.9cm 2.65cm</lines>
325 <place x="1.3cm" y="0cm" height="2.55cm" width="19.0cm">
326 <para style="main_footer">[[ company.rml_footer ]]</para>
327 <para style="main_footer">Contact : [[ user.name ]] - Page: <pageNumber/></para>
333 _header_a4 = _header_main % ('23.0cm', '27.6cm', '27.7cm', '27.7cm', '27.8cm', '27.4cm', '25.8cm', '26.0cm', '26.0cm', '25.6cm', '25.6cm', '25.5cm', '25.5cm')
334 _header_letter = _header_main % ('21.3cm', '25.9cm', '26.0cm', '26.0cm', '26.1cm', '25.5cm', '25.1cm', '24.3cm', '24.3cm', '23.9cm', '23.9cm', '23.8cm', '23.8cm')
336 def onchange_paper_format(self, cr, uid, ids, paper_format, context=None):
337 if paper_format == 'us_letter':
338 return {'value': {'rml_header': self._header_letter}}
339 return {'value': {'rml_header': self._header_a4}}
342 'currency_id': _get_euro,
343 'paper_format': 'a4',
344 'rml_header':_get_header,
345 'rml_header2': _header2,
346 'rml_header3': _header3,
351 (osv.osv._check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
357 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: