clinic/netforce_clinic/models/select_company.py

203 lines
7.8 KiB
Python
Raw Normal View History

2015-02-09 16:47:59 +00:00
from netforce.model import Model, fields, get_model
2015-03-15 13:40:17 +00:00
from netforce.access import get_active_user, set_active_user, get_active_company
2015-02-09 16:47:59 +00:00
class SelectCompany(Model):
_inherit="select.company"
_fields={
2015-03-15 13:40:17 +00:00
"department": fields.Selection([],"Department"),
2015-02-09 16:47:59 +00:00
}
def _get_department(self,context={}):
user_id=get_active_user()
2015-03-15 13:40:17 +00:00
user=get_model("base.user").browse(user_id)
dpt_name=user.department_id.name or ""
2015-02-09 16:47:59 +00:00
return dpt_name
_defaults={
'department': _get_department,
}
2015-03-12 11:04:43 +00:00
def get_departments(self,context={}):
user_id=get_active_user()
2015-03-13 06:43:12 +00:00
set_active_user(1)
2015-03-12 11:04:43 +00:00
dpt_ids=[]
2015-03-13 06:43:12 +00:00
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)
2015-03-12 11:04:43 +00:00
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]
2015-03-15 13:40:17 +00:00
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','='],
2015-03-15 14:19:07 +00:00
['clinic.department','code','='],
2015-03-15 13:40:17 +00:00
]
user_id=get_active_user()
user=get_model("base.user").browse(user_id)
dpt_profile=user.department_profile_id
2015-03-17 00:56:55 +00:00
set_active_user(1) #XXX
2015-03-15 13:40:17 +00:00
dpt_codes=[dpt.code for dpt in dpt_profile.departments]
2015-03-17 00:56:55 +00:00
set_active_user(user_id)
2015-03-15 13:40:17 +00:00
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!")
2015-03-12 11:04:43 +00:00
def select(self,ids,context={}):
user_id=get_active_user()
if user_id==1:
return
2015-03-15 13:40:17 +00:00
user=get_model('base.user').browse(user_id)
2015-03-12 11:04:43 +00:00
obj=self.browse(ids)[0]
2015-03-17 00:56:55 +00:00
department_name=''
2015-03-12 11:04:43 +00:00
if obj.department:
2015-03-17 00:56:55 +00:00
department_name=obj.department
2015-03-15 13:40:17 +00:00
# set to main profile of that user
2015-03-12 11:04:43 +00:00
for dpt in get_model("clinic.department").search_browse([["name","=",obj.department]]):
2015-03-13 11:42:57 +00:00
user.write({
'department_id': dpt.id, #current department
})
2015-03-12 11:04:43 +00:00
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,
})
2015-03-15 13:40:17 +00:00
else:
context['profile_id']=obj.check_profile(context=context)
pf_id=obj.share_profile(context=context)
user.write({
'department_id': None, #all include department
'profile_id': pf_id,
})
2015-03-12 11:04:43 +00:00
res=super().select(ids,context)
2015-03-17 00:56:55 +00:00
if department_name:
cookies=res.get("cookies")
if cookies:
cookies['company_name']='%s (%s)'%(cookies['company_name'], department_name)
2015-03-12 11:04:43 +00:00
return res
2015-03-13 06:43:12 +00:00
def get_select(self,context={}):
user_id=get_active_user()
department_id=None
branch_id=None
2015-03-13 11:42:57 +00:00
user=get_model("base.user").browse(user_id)
dpt=user.department_id
2015-03-13 17:51:19 +00:00
if not dpt:
2015-03-15 15:42:15 +00:00
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
}
return res
else:
return {}
2015-03-13 11:42:57 +00:00
department_id=dpt.id
2015-03-13 16:31:58 +00:00
branch_id=dpt.branch_id.id
2015-03-13 06:43:12 +00:00
return {
'department_id': department_id,
'branch_id': branch_id,
}
2015-03-12 11:04:43 +00:00
2015-02-09 16:47:59 +00:00
SelectCompany.register()