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
26 from tools.translate import _
27 from tools.safe_eval import safe_eval as eval
29 class multi_company_default(osv.osv):
31 Manage multi company default value
33 _name = 'multi_company.default'
34 _description = 'Default multi company'
35 _order = 'company_id,sequence,id'
38 'sequence': fields.integer('Sequence'),
39 'name': fields.char('Name', size=256, 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=256, required=True,
47 help='Expression, must be True to match\nuse context.get or user (browse)'),
48 'field_id': fields.many2one('ir.model.fields', 'Field', help='Select field property'),
52 'expression': lambda *a: 'True',
53 'sequence': lambda *a: 100,
56 def copy(self, cr, uid, id, default=None, context=None):
58 Add (copy) in the name when duplicate record
64 company = self.browse(cr, uid, id, context=context)
65 default = default.copy()
66 default['name'] = company.name + _(' (copy)')
67 return super(multi_company_default, self).copy(cr, uid, id, default, context=context)
69 multi_company_default()
72 class res_company(osv.osv):
74 _description = 'Companies'
76 'name': fields.char('Company Name', size=64, required=True),
77 'parent_id': fields.many2one('res.company', 'Parent Company', select=True),
78 'child_ids': fields.one2many('res.company', 'parent_id', 'Child Companies'),
79 'partner_id': fields.many2one('res.partner', 'Partner', required=True),
80 'rml_header1': fields.char('Report Header', size=200),
81 'rml_footer1': fields.char('Report Footer 1', size=200),
82 'rml_footer2': fields.char('Report Footer 2', size=200),
83 'rml_header' : fields.text('RML Header'),
84 'rml_header2' : fields.text('RML Internal Header'),
85 'logo' : fields.binary('Logo'),
86 'currency_id': fields.many2one('res.currency', 'Currency', required=True),
87 'currency_ids': fields.one2many('res.currency', 'company_id', 'Currency'),
88 'user_ids': fields.many2many('res.users', 'res_company_users_rel', 'cid', 'user_id', 'Accepted Users'),
89 'account_no':fields.char('Account No.', size=64),
92 def search(self, cr, uid, args, offset=0, limit=None, order=None,
93 context=None, count=False):
97 user_preference = context.get('user_preference', False)
99 # TODO: improve this as soon as the client sends the proper
100 # combination of active_id and active_model we'll be able to
101 # use active_id here to restrict to the user being modified instead
103 user_id = context.get('user_id', uid)
105 user = self.pool.get('res.users').browse(cr, uid, user_id, context=context)
106 cmp_ids = list(set([user.company_id.id] + [cmp.id for cmp in user.company_ids]))
108 return super(res_company, self).search(cr, uid, args, offset=offset, limit=limit, order=order,
109 context=context, count=count)
111 def _company_default_get(self, cr, uid, object=False, field=False, context=None):
113 Check if the object for this company have a default value
117 proxy = self.pool.get('multi_company.default')
119 ('object_id.model', '=', object),
120 ('field_id', '=', field),
123 ids = proxy.search(cr, uid, args, context=context)
124 for rule in proxy.browse(cr, uid, ids, context):
125 user = self.pool.get('res.users').browse(cr, uid, uid)
126 if eval(rule.expression, {'context': context, 'user': user}):
127 return rule.company_dest_id.id
128 user_company_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.id
129 return user_company_id
131 def _get_child_ids(self, cr, uid, uid2, context={}):
132 company = self.pool.get('res.users').company_get(cr, uid, uid2)
133 ids = self._get_company_children(cr, uid, company)
137 def _get_company_children(self, cr, uid=None, company=None):
140 ids = self.search(cr, uid, [('parent_id','child_of',[company])])
143 def _get_partner_hierarchy(self, cr, uid, company_id, context={}):
145 parent_id = self.browse(cr, uid, company_id)['parent_id']
147 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
149 return self._get_partner_descendance(cr, uid, company_id, [], context)
152 def _get_partner_descendance(self, cr, uid, company_id, descendance, context={}):
153 descendance.append(self.browse(cr, uid, company_id).partner_id.id)
154 for child_id in self._get_company_children(cr, uid, company_id):
155 if child_id != company_id:
156 descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
160 # This function restart the cache on the _get_company_children method
162 def cache_restart(self, cr):
163 self._get_company_children.clear_cache(cr.dbname)
165 def create(self, cr, *args, **argv):
166 self.cache_restart(cr)
167 return super(res_company, self).create(cr, *args, **argv)
169 def write(self, cr, *args, **argv):
170 self.cache_restart(cr)
171 return super(res_company, self).write(cr, *args, **argv)
173 def _get_euro(self, cr, uid, context={}):
175 return self.pool.get('res.currency').search(cr, uid, [])[0]
179 def _check_recursion(self, cr, uid, ids):
182 cr.execute('select distinct parent_id from res_company where id in %s',(tuple(ids),))
183 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
189 def _get_logo(self, cr, uid, ids):
190 return open(os.path.join(
191 tools.config['root_path'], '..', 'pixmaps', 'openerp-header.png'),
192 'rb') .read().encode('base64')
195 def _get_header2(self,cr,uid,ids):
199 <frame id="first" x1="1.3cm" y1="1.5cm" width="18.4cm" height="26.5cm"/>
201 <fill color="black"/>
202 <stroke color="black"/>
203 <setFont name="DejaVu Sans" size="8"/>
204 <drawString x="1.3cm" y="28.3cm"> [[ formatLang(time.strftime("%Y-%m-%d"), date=True) ]] [[ time.strftime("%H:%M") ]]</drawString>
205 <setFont name="DejaVu Sans Bold" size="10"/>
206 <drawString x="9.8cm" y="28.3cm">[[ company.partner_id.name ]]</drawString>
207 <setFont name="DejaVu Sans" size="8"/>
208 <drawRightString x="19.7cm" y="28.3cm"><pageNumber/> / </drawRightString>
209 <drawString x="19.8cm" y="28.3cm"><pageCount/></drawString>
210 <stroke color="#000000"/>
211 <lines>1.3cm 28.1cm 20cm 28.1cm</lines>
215 def _get_header(self,cr,uid,ids):
217 return tools.file_open(os.path.join('base', 'report', 'corporate_rml_header.rml')).read()
222 <frame id="first" x1="1.3cm" y1="2.5cm" height="23.0cm" width="19cm"/>
224 <!-- You Logo - Change X,Y,Width and Height -->
225 <image x="1.3cm" y="27.6cm" height="40.0" >[[ company.logo or removeParentNode('image') ]]</image>
226 <setFont name="DejaVu Sans" size="8"/>
227 <fill color="black"/>
228 <stroke color="black"/>
229 <lines>1.3cm 27.7cm 20cm 27.7cm</lines>
231 <drawRightString x="20cm" y="27.8cm">[[ company.rml_header1 ]]</drawRightString>
234 <drawString x="1.3cm" y="27.2cm">[[ company.partner_id.name ]]</drawString>
235 <drawString x="1.3cm" y="26.8cm">[[ company.partner_id.address and company.partner_id.address[0].street or '' ]]</drawString>
236 <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>
237 <drawString x="1.3cm" y="26.0cm">Phone:</drawString>
238 <drawRightString x="7cm" y="26.0cm">[[ company.partner_id.address and company.partner_id.address[0].phone or '' ]]</drawRightString>
239 <drawString x="1.3cm" y="25.6cm">Mail:</drawString>
240 <drawRightString x="7cm" y="25.6cm">[[ company.partner_id.address and company.partner_id.address[0].email or '' ]]</drawRightString>
241 <lines>1.3cm 25.5cm 7cm 25.5cm</lines>
245 <lines>1.2cm 2.15cm 19.9cm 2.15cm</lines>
247 <drawCentredString x="10.5cm" y="1.7cm">[[ company.rml_footer1 ]]</drawCentredString>
248 <drawCentredString x="10.5cm" y="1.25cm">[[ company.rml_footer2 ]]</drawCentredString>
249 <drawCentredString x="10.5cm" y="0.8cm">Contact : [[ user.name ]] - Page: <pageNumber/></drawCentredString>
254 'currency_id': _get_euro,
255 'rml_header':_get_header,
256 'rml_header2': _get_header2,
261 (_check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
266 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: