209 lines
8.0 KiB
Python
209 lines
8.0 KiB
Python
from netforce.model import Model, fields, get_model
|
|
from netforce.access import get_active_user, set_active_user, get_active_company
|
|
|
|
class SelectCompany(Model):
|
|
_inherit="select.company"
|
|
_fields={
|
|
"department": fields.Selection([],"Department"),
|
|
}
|
|
|
|
def _get_department(self,context={}):
|
|
user_id=get_active_user()
|
|
user=get_model("base.user").browse(user_id)
|
|
dpt_name=user.department_id.name or ""
|
|
return dpt_name
|
|
|
|
_defaults={
|
|
'department': _get_department,
|
|
}
|
|
|
|
def get_departments(self,context={}):
|
|
user_id=get_active_user()
|
|
set_active_user(1)
|
|
dpt_ids=[]
|
|
user=get_model("base.user").browse(user_id)
|
|
if user.department_profile_id:
|
|
for dpt in user.department_profile_id.departments:
|
|
dpt_ids.append(dpt.id)
|
|
if not dpt_ids:
|
|
for st in get_model("clinic.staff").search_browse(['user_id','=',user_id]):
|
|
for dpt in st.departments:
|
|
dpt_ids.append(dpt.id)
|
|
dom=[]
|
|
if dpt_ids:
|
|
dom.append(['id','in',dpt_ids])
|
|
res=get_model("clinic.department").search_read(dom,["name"])
|
|
set_active_user(user_id)
|
|
return [(r["name"],r["name"]) for r in res]
|
|
|
|
def check_profile(self,ids,context={}):
|
|
user_id=get_active_user()
|
|
user=get_model("base.user").browse(user_id)
|
|
dpt_profile=user.department_profile_id
|
|
if not dpt_profile:
|
|
raise Exception("Missing department profile! Please contact admin.")
|
|
set_active_user(1)
|
|
for obj in self.browse(ids):
|
|
code=dpt_profile.name or ''
|
|
pf_ids=get_model('profile').search(['code','=',code])
|
|
if pf_ids:
|
|
print("Profile %s is already exist"%(code))
|
|
set_active_user(user_id)
|
|
return pf_ids[0]
|
|
except_perms=['Clinic Menu Settings','Clinic Staff Tab Accounting']
|
|
other_perms=[]
|
|
for otp in get_model('permission').search_read(['name','ilike','clinic'],['name']):
|
|
name=otp['name']
|
|
if name in except_perms:
|
|
continue
|
|
other_perms.append(otp['id'])
|
|
except_model=['clinic.cycle','clinic.department']
|
|
perms=[]
|
|
for model in get_model('model').search_read([['name','ilike','clinic']],['name']):
|
|
vals={
|
|
'model_id': model['id'],
|
|
'perm_read': True,
|
|
'perm_create': True,
|
|
'perm_write': True,
|
|
'perm_delete': True,
|
|
}
|
|
name=model['name']
|
|
if name in except_model:
|
|
vals['perm_create']=False
|
|
vals['perm_write']=False
|
|
vals['perm_delete']=False
|
|
perms.append(('create', vals))
|
|
profile_id=get_model('profile').create({
|
|
'perms': perms,
|
|
'code': code,
|
|
'name': code,
|
|
'perms': perms,
|
|
'other_perms': [('set',other_perms)],
|
|
'login_company_id': get_active_company(),
|
|
'home_action': 'clinic_visit_board',
|
|
})
|
|
print("create profile %s"%(code))
|
|
set_active_user(user_id)
|
|
return profile_id
|
|
|
|
def share_profile(self,ids,context={}):
|
|
#rule: branch_code-deparment_code
|
|
profile_id=context.get('profile_id')
|
|
if not profile_id:
|
|
raise Exception("Profile not found")
|
|
models=[
|
|
['clinic.patient','location','ilike'],
|
|
['clinic.patient.cycle','department_id.code','='],
|
|
['clinic.staff','location','ilike'],
|
|
['clinic.staff.rotation','department_id.code','='],
|
|
['clinic.visit','department_id.code','='],
|
|
['clinic.hd.case','department_id.code','='],
|
|
['clinic.cycle.item','department_id.code','='],
|
|
['clinic.sickbed','department_id.code','='],
|
|
['clinic.shop','department_id.code','='],
|
|
['clinic.dialyzer','department_id.code','='],
|
|
['clinic.schedule','department_id.code','='],
|
|
['clinic.department','code','='],
|
|
]
|
|
|
|
user_id=get_active_user()
|
|
user=get_model("base.user").browse(user_id)
|
|
dpt_profile=user.department_profile_id
|
|
set_active_user(1) #XXX
|
|
dpt_codes=[dpt.code for dpt in dpt_profile.departments]
|
|
set_active_user(user_id)
|
|
exist_model=[]
|
|
for obj in self.browse(ids):
|
|
for sa in get_model('share.access').search_browse([]):
|
|
for pf in sa.profiles:
|
|
if profile_id==pf.id:
|
|
exist_model.append(sa.model_id.name)
|
|
break
|
|
# create new
|
|
for model,field_dom, op in models:
|
|
if model in exist_model:
|
|
print('already exist ', model)
|
|
continue
|
|
for model_id in get_model("model").search(['name','=',model]):
|
|
dom=[]
|
|
dom='["or",%s]'%(', '.join(['["%s","%s","%s"]'%(field_dom,op,dpt_code) for dpt_code in dpt_codes]))
|
|
print('dom ', dom)
|
|
get_model('share.access').create({
|
|
'model_id': model_id,
|
|
'profiles': [['set',[profile_id]]],
|
|
'default_access': 'custom',
|
|
'filter_type': 'rw',
|
|
'select_profile': 'include',
|
|
'domain': dom,
|
|
})
|
|
return profile_id
|
|
print("Done!")
|
|
|
|
def select(self,ids,context={}):
|
|
user_id=get_active_user()
|
|
user=get_model('base.user').browse(user_id)
|
|
obj=self.browse(ids)[0]
|
|
department_name=''
|
|
if obj.department:
|
|
department_name=obj.department
|
|
# set to main profile of that user
|
|
for dpt in get_model("clinic.department").search_browse([["name","=",obj.department]]):
|
|
user.write({
|
|
'department_id': dpt.id, #current department
|
|
})
|
|
pf_id=None
|
|
for pf in get_model("profile").search_read(['code','=',dpt.code]):
|
|
pf_id=pf['id']
|
|
print('selected profile ', pf['name'])
|
|
if pf_id:
|
|
user=get_model("base.user").browse(user_id)
|
|
user.write({
|
|
'profile_id': pf_id,
|
|
})
|
|
else:
|
|
context['profile_id']=obj.check_profile(context=context)
|
|
pf_id=obj.share_profile(context=context)
|
|
old_pf_id=user.old_profile_id.id
|
|
if old_pf_id:
|
|
pf_id=old_pf_id
|
|
user.write({
|
|
'department_id': None, #all include department
|
|
'profile_id': pf_id,
|
|
})
|
|
res=super().select(ids,context)
|
|
cookies=res.get("cookies")
|
|
if cookies:
|
|
if department_name:
|
|
cookies['company_name']='%s (%s)'%(cookies['company_name'], department_name)
|
|
elif user.branch_id:
|
|
cookies['company_name']='%s (%s)'%(cookies['company_name'], user.branch_id.name or "")
|
|
return res
|
|
|
|
def get_select(self,context={}):
|
|
user_id=get_active_user()
|
|
department_id=None
|
|
branch_id=None
|
|
user=get_model("base.user").browse(user_id)
|
|
dpt=user.department_id
|
|
res={
|
|
'department_id': None,
|
|
'branch_id': None,
|
|
}
|
|
if not dpt:
|
|
dpts=user.department_profile_id.departments
|
|
if dpts:
|
|
res={
|
|
'department_ids': [dpt.id for dpt in dpts],
|
|
'branch_id': dpts[0].branch_id.id, #XXX
|
|
}
|
|
else:
|
|
department_id=dpt.id
|
|
branch_id=dpt.branch_id.id
|
|
res={
|
|
'department_id': department_id,
|
|
'branch_id': branch_id,
|
|
}
|
|
return res
|
|
|
|
SelectCompany.register()
|