MULTI_COMPANY_ACCOUNT, ACCOUNT, KERNEL, SALE, PURCHASE: add multi-company for accounting with a lot improvement
bzr revid: ced-f64ff379976afb70dac3ba7baaf68eef393645c5
This commit is contained in:
parent
408b1ee9f0
commit
6085c0d63d
|
@ -56,13 +56,11 @@ class res_company(osv.osv):
|
||||||
_get_company_children = tools.cache()(_get_company_children)
|
_get_company_children = tools.cache()(_get_company_children)
|
||||||
|
|
||||||
def _get_partner_hierarchy(self, cr, uid, company_id, context={}):
|
def _get_partner_hierarchy(self, cr, uid, company_id, context={}):
|
||||||
print "Company ID is %s: Looking for parent..." % company_id
|
|
||||||
if company_id:
|
if company_id:
|
||||||
parent_id = self.browse(cr, uid, company_id)['parent_id']
|
parent_id = self.browse(cr, uid, company_id)['parent_id']
|
||||||
if parent_id:
|
if parent_id:
|
||||||
return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
|
return self._get_partner_hierarchy(cr, uid, parent_id.id, context)
|
||||||
else:
|
else:
|
||||||
print "No parent: starting descendance search!"
|
|
||||||
return self._get_partner_descendance(cr, uid, company_id, [], context)
|
return self._get_partner_descendance(cr, uid, company_id, [], context)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
@ -70,7 +68,6 @@ class res_company(osv.osv):
|
||||||
descendance.append(self.browse(cr, uid, company_id).partner_id.id)
|
descendance.append(self.browse(cr, uid, company_id).partner_id.id)
|
||||||
for child_id in self._get_company_children(cr, uid, company_id):
|
for child_id in self._get_company_children(cr, uid, company_id):
|
||||||
if child_id != company_id:
|
if child_id != company_id:
|
||||||
print "Hello, I'm %s, child of %s" % (child_id, company_id)
|
|
||||||
descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
|
descendance = self._get_partner_descendance(cr, uid, child_id, descendance)
|
||||||
return descendance
|
return descendance
|
||||||
|
|
||||||
|
|
|
@ -46,6 +46,8 @@ import netsvc
|
||||||
import psycopg
|
import psycopg
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
import tools
|
||||||
|
|
||||||
def _symbol_set(symb):
|
def _symbol_set(symb):
|
||||||
if symb==None or symb==False:
|
if symb==None or symb==False:
|
||||||
return None
|
return None
|
||||||
|
@ -275,7 +277,16 @@ class many2one(_column):
|
||||||
res.setdefault(id, '')
|
res.setdefault(id, '')
|
||||||
obj = obj.pool.get(self._obj)
|
obj = obj.pool.get(self._obj)
|
||||||
# build a dictionary of the form {'id_of_distant_resource': name_of_distant_resource}
|
# build a dictionary of the form {'id_of_distant_resource': name_of_distant_resource}
|
||||||
names = dict(obj.name_get(cr, user, filter(None, res.values()), context))
|
from orm import except_orm
|
||||||
|
try:
|
||||||
|
names = dict(obj.name_get(cr, user, filter(None, res.values()), context))
|
||||||
|
except except_orm:
|
||||||
|
names={}
|
||||||
|
for id in filter(None, res.values()):
|
||||||
|
try:
|
||||||
|
names[id] = dict(obj.name_get(cr, user, [id], context))[id]
|
||||||
|
except except_orm:
|
||||||
|
names[id] = "===Access error==="
|
||||||
for r in res.keys():
|
for r in res.keys():
|
||||||
if res[r] and res[r] in names:
|
if res[r] and res[r] in names:
|
||||||
res[r] = (res[r], names[res[r]])
|
res[r] = (res[r], names[res[r]])
|
||||||
|
@ -381,11 +392,16 @@ class many2many(_column):
|
||||||
res = {}
|
res = {}
|
||||||
if not ids:
|
if not ids:
|
||||||
return res
|
return res
|
||||||
ids_s = ','.join(map(str,ids))
|
|
||||||
limit_str = self._limit is not None and ' limit %d' % self._limit or ''
|
|
||||||
cr.execute('select '+self._id2+','+self._id1+' from '+self._rel+' where '+self._id1+' in ('+ids_s+')'+limit_str+' offset %d', (offset,))
|
|
||||||
for id in ids:
|
for id in ids:
|
||||||
res[id] = []
|
res[id] = []
|
||||||
|
ids_s = ','.join(map(str,ids))
|
||||||
|
limit_str = self._limit is not None and ' limit %d' % self._limit or ''
|
||||||
|
obj = obj.pool.get(self._obj)
|
||||||
|
if 'company_id' in obj._columns:
|
||||||
|
compids = tools.get_user_companies(cr, user)
|
||||||
|
cr.execute('SELECT r.'+self._id2+', r.'+self._id1+' FROM '+self._rel+' AS r, '+obj._table+' AS o WHERE r.'+self._id1+' in ('+ids_s+') AND r.'+self._id2+' = o.id AND (o.company_id IN ('+','.join(map(str,compids))+') OR o.company_id IS NULL)'+limit_str+' OFFSET %d', (offset,))
|
||||||
|
else:
|
||||||
|
cr.execute('select '+self._id2+','+self._id1+' from '+self._rel+' where '+self._id1+' in ('+ids_s+')'+limit_str+' offset %d', (offset,))
|
||||||
for r in cr.fetchall():
|
for r in cr.fetchall():
|
||||||
res[r[1]].append(r[0])
|
res[r[1]].append(r[0])
|
||||||
return res
|
return res
|
||||||
|
@ -409,7 +425,11 @@ class many2many(_column):
|
||||||
elif act[0]==5:
|
elif act[0]==5:
|
||||||
cr.execute('update '+self._rel+' set '+self._id2+'=null where '+self._id2+'=%d', (id,))
|
cr.execute('update '+self._rel+' set '+self._id2+'=null where '+self._id2+'=%d', (id,))
|
||||||
elif act[0]==6:
|
elif act[0]==6:
|
||||||
cr.execute('delete from '+self._rel+' where '+self._id1+'=%d', (id, ))
|
if 'company_id' in obj._columns and not user == 1:
|
||||||
|
compids = tools.get_user_companies(cr, user)
|
||||||
|
cr.execute('delete from '+self._rel+' where '+self._id1+'=%d AND '+self._id2+' IN (SELECT r.'+self._id2+' FROM '+self._rel+' AS r, '+obj._table+' AS o WHERE r.'+self._id1+'=%d AND r.'+self._id2+' = o.id AND (o.company_id IN ('+','.join(map(str,compids))+') OR o.company_id IS NULL))', (id, id, ))
|
||||||
|
else:
|
||||||
|
cr.execute('delete from '+self._rel+' where '+self._id1+'=%d', (id, ))
|
||||||
for act_nbr in act[2]:
|
for act_nbr in act[2]:
|
||||||
cr.execute('insert into '+self._rel+' ('+self._id1+','+self._id2+') values (%d, %d)', (id, act_nbr))
|
cr.execute('insert into '+self._rel+' ('+self._id1+','+self._id2+') values (%d, %d)', (id, act_nbr))
|
||||||
|
|
||||||
|
|
|
@ -173,6 +173,13 @@ class browse_record(object):
|
||||||
if isinstance(f, fields.many2one) or isinstance(f, fields.one2one):
|
if isinstance(f, fields.many2one) or isinstance(f, fields.one2one):
|
||||||
if data[n]:
|
if data[n]:
|
||||||
obj = self._table.pool.get(f._obj)
|
obj = self._table.pool.get(f._obj)
|
||||||
|
compids=False
|
||||||
|
if 'company_id' in obj._columns and not self._uid == 1:
|
||||||
|
compids = tools.get_user_companies(self._cr, self._uid)
|
||||||
|
if compids:
|
||||||
|
self._cr.execute('SELECT id FROM '+obj._table+' where id = %d AND (company_id in ('+','.join(map(str,compids))+') or company_id is null)', (data[n],))
|
||||||
|
if not self._cr.fetchall():
|
||||||
|
raise except_orm('BrowseError', 'Object %s (id:%d) is linked to the object %s (id:%d) that is not in your company' %(self._table._description, self._id, obj._description, data[n]))
|
||||||
data[n] = browse_record(self._cr, self._uid, data[n], obj, self._cache, context=self._context, list_class=self._list_class)
|
data[n] = browse_record(self._cr, self._uid, data[n], obj, self._cache, context=self._context, list_class=self._list_class)
|
||||||
else:
|
else:
|
||||||
data[n] = browse_null()
|
data[n] = browse_null()
|
||||||
|
@ -703,14 +710,25 @@ class orm(object):
|
||||||
if fields==None:
|
if fields==None:
|
||||||
fields = self._columns.keys()
|
fields = self._columns.keys()
|
||||||
|
|
||||||
|
# if the object has a field named 'company_id', filter out all
|
||||||
|
# records which do not concern the current company (the company
|
||||||
|
# of the current user) or its "childs"
|
||||||
|
company_clause='true'
|
||||||
|
compids=False
|
||||||
|
if 'company_id' in self._columns and not user == 1:
|
||||||
|
compids = tools.get_user_companies(cr, user)
|
||||||
|
if compids:
|
||||||
|
company_clause = '(company_id in ('+','.join(map(str,compids))+') or company_id is null)'
|
||||||
|
|
||||||
# all inherited fields + all non inherited fields for which the attribute whose name is in load is True
|
# all inherited fields + all non inherited fields for which the attribute whose name is in load is True
|
||||||
fields_pre = filter(lambda x: x in self._columns and getattr(self._columns[x],'_classic_write'), fields) + self._inherits.values()
|
fields_pre = filter(lambda x: x in self._columns and getattr(self._columns[x],'_classic_write'), fields) + self._inherits.values()
|
||||||
|
|
||||||
if len(fields_pre):
|
if len(fields_pre) or compids:
|
||||||
cr.execute('select %s from %s where id = any(array[%s]) order by %s' % (','.join(fields_pre + ['id']), self._table, ','.join([str(x) for x in ids]), self._order))
|
cr.execute('select %s from %s where id = any(array[%s]) and %s order by %s' % (','.join(fields_pre + ['id']), self._table, ','.join([str(x) for x in ids]), company_clause, self._order))
|
||||||
|
uniq_id = []
|
||||||
if not cr.rowcount:
|
[uniq_id.append(i) for i in ids if not uniq_id.count(i)]
|
||||||
return []
|
if not cr.rowcount == len(uniq_id) and compids:
|
||||||
|
raise except_orm('ReadError', 'You try to read objects (%s) that is not in your company' % self._description)
|
||||||
res = cr.dictfetchall()
|
res = cr.dictfetchall()
|
||||||
else:
|
else:
|
||||||
res = map(lambda x: {'id':x}, ids)
|
res = map(lambda x: {'id':x}, ids)
|
||||||
|
@ -1363,8 +1381,8 @@ class orm(object):
|
||||||
# if the object has a field named 'company_id', filter out all
|
# if the object has a field named 'company_id', filter out all
|
||||||
# records which do not concern the current company (the company
|
# records which do not concern the current company (the company
|
||||||
# of the current user) or its "childs"
|
# of the current user) or its "childs"
|
||||||
if 'company_id' in self._columns:
|
if 'company_id' in self._columns and not user == 1:
|
||||||
compids = self.pool.get('res.company')._get_child_ids(cr, user, user)
|
compids = tools.get_user_companies(cr, user)
|
||||||
if compids:
|
if compids:
|
||||||
compids.append(False)
|
compids.append(False)
|
||||||
args.append(('company_id','in',compids))
|
args.append(('company_id','in',compids))
|
||||||
|
|
|
@ -86,7 +86,7 @@ class osv_pool(netsvc.Service):
|
||||||
return res
|
return res
|
||||||
except orm.except_orm, inst:
|
except orm.except_orm, inst:
|
||||||
#self.abortResponse(1, inst.value[0], inst.name, inst.value[1])
|
#self.abortResponse(1, inst.value[0], inst.name, inst.value[1])
|
||||||
self.abortResponse(1, inst.value[0], 'warning', inst.value[1])
|
self.abortResponse(1, inst.name, 'warning', inst.value)
|
||||||
except except_osv, inst:
|
except except_osv, inst:
|
||||||
self.abortResponse(1, inst.name, inst.exc_type, inst.value)
|
self.abortResponse(1, inst.name, inst.exc_type, inst.value)
|
||||||
except psycopg.IntegrityError, inst:
|
except psycopg.IntegrityError, inst:
|
||||||
|
|
|
@ -380,4 +380,18 @@ def scan_languages():
|
||||||
lang_dict = get_languages()
|
lang_dict = get_languages()
|
||||||
return [(lang, lang_dict.get(lang, lang)) for lang in file_list]
|
return [(lang, lang_dict.get(lang, lang)) for lang in file_list]
|
||||||
|
|
||||||
|
|
||||||
|
def get_user_companies(cr, user):
|
||||||
|
def _get_company_children(cr, ids):
|
||||||
|
if not ids:
|
||||||
|
return []
|
||||||
|
cr.execute('SELECT id FROM res_company WHERE parent_id = any(array[%s])' %(','.join([str(x) for x in ids]),))
|
||||||
|
res=[x[0] for x in cr.fetchall()]
|
||||||
|
res.extend(_get_company_children(cr, res))
|
||||||
|
return res
|
||||||
|
cr.execute('SELECT comp.id FROM res_company AS comp, res_users AS u WHERE u.id = %d AND comp.id = u.company_id' % (user,))
|
||||||
|
compids=[cr.fetchone()[0]]
|
||||||
|
compids.extend(_get_company_children(cr, compids))
|
||||||
|
return compids
|
||||||
|
|
||||||
# vim:noexpandtab
|
# vim:noexpandtab
|
||||||
|
|
Loading…
Reference in New Issue