diff --git a/openerp/addons/base/res/res_partner.py b/openerp/addons/base/res/res_partner.py index 92414e764f3..ef22d935ffc 100644 --- a/openerp/addons/base/res/res_partner.py +++ b/openerp/addons/base/res/res_partner.py @@ -489,6 +489,17 @@ class res_partner(osv.osv, format_address): 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): + 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) diff --git a/openerp/addons/base/res/res_users.py b/openerp/addons/base/res/res_users.py index 0e74a5379f4..b1aeaf28107 100644 --- a/openerp/addons/base/res/res_users.py +++ b/openerp/addons/base/res/res_users.py @@ -279,6 +279,13 @@ class res_users(osv.osv): return result + def create(self, cr, uid, vals, context=None): + user_id = super(res_users, self).create(cr, uid, vals, context=context) + user = self.browse(cr, uid, user_id, context=context) + if user.partner_id.company_id: + user.partner_id.write({'company_id': user.company_id.id}) + return user_id + def write(self, cr, uid, ids, values, context=None): if not hasattr(ids, '__iter__'): ids = [ids] @@ -293,7 +300,11 @@ class res_users(osv.osv): 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['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)