def write(self, cr, uid, ids, vals, context=None):
if isinstance(ids, (int, long)):
ids = [ids]
+ #res.partner must only allow to set the company_id of a partner if it
+ #is the same as the company of all users that inherit from this partner
+ #(this is to allow the code from res_users to write to the partner!) or
+ #if setting the company_id to False (this is compatible with any user company)
+ if vals.get('company_id'):
+ user_pool = self.pool.get('res.users')
+ for partner in self.browse(cr, uid, ids, context=context):
- user_companies = set([users.company_id.id for users in partner.user_ids])
- if len(user_companies) > 1 or vals.get('company_id') not in user_companies:
- raise osv.except_osv(_("Warning"),_("You can not change the company as the partner/user has mutiple user linked with different companies."))
++ if partner.user_ids:
++ user_companies = set([user.company_id.id for user in partner.user_ids])
++ if len(user_companies) > 1 or vals.get('company_id') not in user_companies:
++ raise osv.except_osv(_("Warning"),_("You can not change the company as the partner/user has multiple user linked with different companies."))
result = super(res_partner,self).write(cr, uid, ids, vals, context=context)
for partner in self.browse(cr, uid, ids, context=context):
self._fields_sync(cr, uid, partner, vals, context)
uid = 1 # safe fields only, so we write as super-user to bypass access rights
res = super(res_users, self).write(cr, uid, ids, values, context=context)
-
+ if 'company_id' in values:
+ for user in self.browse(cr, uid, ids, context=context):
+ # if partner is global we keep it that way
- if user.partner_id.company_id and user.partner_id.company_id.id != values.get('company_id'):
++ if user.partner_id.company_id and user.partner_id.company_id.id != values['company_id']:
+ user.partner_id.write({'company_id': user.company_id.id})
# clear caches linked to the users
self.pool.get('ir.model.access').call_cache_clearing_methods(cr)
clear = partial(self.pool.get('ir.rule').clear_cache, cr)