2015-02-09 16:47:59 +00:00
|
|
|
from netforce.model import Model, fields, get_model
|
|
|
|
from netforce.access import get_active_user, set_active_user
|
2015-03-13 06:43:12 +00:00
|
|
|
from netforce.database import get_connection
|
2015-02-09 16:47:59 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
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]]):
|
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,
|
|
|
|
})
|
|
|
|
set_active_user(user_id)
|
|
|
|
res=super().select(ids,context)
|
|
|
|
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
|
|
|
|
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()
|