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 affected by this rule'),
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'
77 'name': fields.char('Company Name', size=64, required=True),
78 'parent_id': fields.many2one('res.company', 'Parent Company', select=True),
79 'child_ids': fields.one2many('res.company', 'parent_id', 'Child Companies'),
80 'partner_id': fields.many2one('res.partner', 'Partner', required=True),
81 'rml_header1': fields.char('Report Header', size=200),
82 'rml_footer1': fields.char('Report Footer 1', size=200),
83 'rml_footer2': fields.char('Report Footer 2', size=200),
84 'rml_header' : fields.text('RML Header'),
85 'rml_header2' : fields.text('RML Internal Header'),
86 'rml_header3' : fields.text('RML Internal Header'),
87 'logo' : fields.binary('Logo'),
88 'currency_id': fields.many2one('res.currency', 'Currency', required=True),
89 'currency_ids': fields.one2many('res.currency', 'company_id', 'Currency'),
90 'user_ids': fields.many2many('res.users', 'res_company_users_rel', 'cid', 'user_id', 'Accepted Users'),
91 'account_no':fields.char('Account No.', size=64),
94 def _search(self, cr, uid, args, offset=0, limit=None, order=None,
95 context=None, count=False, access_rights_uid=None):
99 user_preference = context.get('user_preference', False)
101 user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
102 cmp_ids = list(set([user.company_id.id] + [cmp.id for cmp in user.company_ids]))
104 return super(res_company, self)._search(cr, uid, args, offset=offset, limit=limit, order=order,
105 context=context, count=count, access_rights_uid=access_rights_uid)
107 def _company_default_get(self, cr, uid, object=False, field=False, context=None):
109 Check if the object for this company have a default value
113 proxy = self.pool.get('multi_company.default')
115 ('object_id.model', '=', object),
116 ('field_id', '=', field),
119 ids = proxy.search(cr, uid, args, context=context)
120 user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
121 for rule in proxy.browse(cr, uid, ids, context):
122 if eval(rule.expression, {'context': context, 'user': user}):
123 return rule.company_dest_id.id
124 return user.company_id.id
126 def _get_child_ids(self, cr, uid, uid2, context={}):
127 company = self.pool.get('res.users').company_get(cr, uid, uid2)
128 ids = self._get_company_children(cr, uid, company)
132 def _get_company_children(self, cr, uid=None, company=None):
135 ids = self.search(cr, uid, [('parent_id','child_of',[company])])
138 def _get_partner_hierarchy(self, cr, uid, company_id, context={}):
140 parent_id = self.browse(cr, uid, company_id)['parent_id']
142 return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
144 return self._get_partner_descendance(cr, uid, company_id, [], context)
147 def _get_partner_descendance(self, cr, uid, company_id, descendance, context={}):
148 descendance.append(self.browse(cr, uid, company_id).partner_id.id)
149 for child_id in self._get_company_children(cr, uid, company_id):
150 if child_id != company_id:
151 descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
155 # This function restart the cache on the _get_company_children method
157 def cache_restart(self, cr):
158 self._get_company_children.clear_cache(cr.dbname)
160 def create(self, cr, uid, vals, context=None):
161 if not vals.get('name', False) or vals.get('partner_id', False):
162 self.cache_restart(cr)
163 return super(res_company, self).create(cr, uid, vals, context=context)
164 obj_partner = self.pool.get('res.partner')
165 partner_id = obj_partner.create(cr, uid, {'name': vals['name']}, context=context)
166 vals.update({'partner_id': partner_id})
167 self.cache_restart(cr)
168 company_id = super(res_company, self).create(cr, uid, vals, context=context)
169 obj_partner.write(cr, uid, partner_id, {'company_id': company_id}, context=context)
172 def write(self, cr, *args, **argv):
173 self.cache_restart(cr)
174 return super(res_company, self).write(cr, *args, **argv)
176 def _get_euro(self, cr, uid, context={}):
178 return self.pool.get('res.currency').search(cr, uid, [])[0]
182 def _get_logo(self, cr, uid, ids):
183 return open(os.path.join(
184 tools.config['root_path'], '..', 'pixmaps', 'openerp-header.png'),
185 'rb') .read().encode('base64')
187 def _get_header3(self,cr,uid,ids):
191 <frame id="first" x1="28.0" y1="28.0" width="786" height="525"/>
193 <fill color="black"/>
194 <stroke color="black"/>
195 <setFont name="DejaVu Sans" size="8"/>
196 <drawString x="25" y="555"> [[ formatLang(time.strftime("%Y-%m-%d"), date=True) ]] [[ time.strftime("%H:%M") ]]</drawString>
197 <setFont name="DejaVu Sans Bold" size="10"/>
198 <drawString x="382" y="555">[[ company.partner_id.name ]]</drawString>
199 <stroke color="#000000"/>
200 <lines>25 550 818 550</lines>
204 def _get_header2(self,cr,uid,ids):
208 <frame id="first" x1="28.0" y1="28.0" width="539" height="772"/>
210 <fill color="black"/>
211 <stroke color="black"/>
212 <setFont name="DejaVu Sans" size="8"/>
213 <drawString x="1.0cm" y="28.3cm"> [[ formatLang(time.strftime("%Y-%m-%d"), date=True) ]] [[ time.strftime("%H:%M") ]]</drawString>
214 <setFont name="DejaVu Sans Bold" size="10"/>
215 <drawString x="9.3cm" y="28.3cm">[[ company.partner_id.name ]]</drawString>
216 <stroke color="#000000"/>
217 <lines>1.0cm 28.1cm 20.1cm 28.1cm</lines>
221 def _get_header(self,cr,uid,ids):
223 return tools.file_open(os.path.join('base', 'report', 'corporate_rml_header.rml')).read()
228 <frame id="first" x1="1.3cm" y1="2.5cm" height="23.0cm" width="19cm"/>
230 <!-- You Logo - Change X,Y,Width and Height -->
231 <image x="1.3cm" y="27.6cm" height="40.0" >[[ company.logo or removeParentNode('image') ]]</image>
232 <setFont name="DejaVu Sans" size="8"/>
233 <fill color="black"/>
234 <stroke color="black"/>
235 <lines>1.3cm 27.7cm 20cm 27.7cm</lines>
237 <drawRightString x="20cm" y="27.8cm">[[ company.rml_header1 ]]</drawRightString>
240 <drawString x="1.3cm" y="27.2cm">[[ company.partner_id.name ]]</drawString>
241 <drawString x="1.3cm" y="26.8cm">[[ company.partner_id.address and company.partner_id.address[0].street or '' ]]</drawString>
242 <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>
243 <drawString x="1.3cm" y="26.0cm">Phone:</drawString>
244 <drawRightString x="7cm" y="26.0cm">[[ company.partner_id.address and company.partner_id.address[0].phone or '' ]]</drawRightString>
245 <drawString x="1.3cm" y="25.6cm">Mail:</drawString>
246 <drawRightString x="7cm" y="25.6cm">[[ company.partner_id.address and company.partner_id.address[0].email or '' ]]</drawRightString>
247 <lines>1.3cm 25.5cm 7cm 25.5cm</lines>
251 <lines>1.2cm 2.15cm 19.9cm 2.15cm</lines>
253 <drawCentredString x="10.5cm" y="1.7cm">[[ company.rml_footer1 ]]</drawCentredString>
254 <drawCentredString x="10.5cm" y="1.25cm">[[ company.rml_footer2 ]]</drawCentredString>
255 <drawCentredString x="10.5cm" y="0.8cm">Contact : [[ user.name ]] - Page: <pageNumber/></drawCentredString>
260 'currency_id': _get_euro,
261 'rml_header':_get_header,
262 'rml_header2': _get_header2,
263 'rml_header3': _get_header3,
268 (osv.osv._check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
273 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: