clinic/netforce_clinic/models/select_company.py

130 lines
4.7 KiB
Python

from netforce.model import Model, fields, get_model
from netforce.access import get_active_user, set_active_user
from netforce.database import get_connection
class SelectCompany(Model):
_inherit="select.company"
_fields={
"department": fields.Selection([],"Deparment")
}
def _get_department(self,context={}):
user_id=get_active_user()
dpt_name=None
for staff in get_model("clinic.staff").search_browse([['user_id','=',user_id]]):
dpt=staff.department_id
dpt_name=dpt.name
return dpt_name
_defaults={
'department': _get_department,
}
def _get_departments(self,context={}):
user_id=get_active_user()
user=get_model('base.user').browse(user_id)
dom=[]
if user.department_profile_id:
set_active_user(1)
dpt_ids=[]
for dpt in user.department_profile_id.departments:
dpt_ids.append(dpt.id)
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 _select(self,ids,context={}):
obj=self.browse(ids)[0]
if obj.department:
user_id=get_active_user()
user=get_model('base.user').browse(user_id)
set_active_user(1)
staff=None
for dpt in get_model("clinic.department").search_browse([["name","=",obj.department]]):
for staff in get_model("clinic.staff").search_browse([['user_id','=',user_id]]):
staff.write({
'department_id': dpt.id,
'branch_id': dpt.branch_id.id,
})
if not staff:
raise Exception("No staff match with user %s"%user.name)
pf_id=None
if dpt.branch_id.code=='LS':
for pf in get_model("profile").search_browse([['name','=','Nurse Laksi']]):
pf_id=pf.id
elif dpt.branch_id.code=='SS':
for pf in get_model("profile").search_browse([['name','=','Nurse Samsan']]):
pf_id=pf.id
if pf_id:
user=get_model("base.user").browse(user_id)
user.write({
'profile_id': pf_id,
})
set_active_user(user_id)
res=super().select(ids,context)
return res
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 select(self,ids,context={}):
user_id=get_active_user()
if user_id==1:
return
obj=self.browse(ids)[0]
if obj.department:
user=get_model('base.user').browse(user_id)
set_active_user(1)
for dpt in get_model("clinic.department").search_browse([["name","=",obj.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,
})
set_active_user(user_id)
res=super().select(ids,context)
return res
def get_select(self,context={}):
user_id=get_active_user()
db=get_connection()
department_id=None
branch_id=None
count=0
for r in db.query("select department from select_company where write_uid=%s order by id desc",user_id):
for dpt in get_model("clinic.department").search_browse([['name','=',r['department']]]):
department_id=dpt.id
branch_id=dpt.branch_id.id
count+=1
break
if count<1:
return {}
return {
'department_id': department_id,
'branch_id': branch_id,
}
SelectCompany.register()