clinic/netforce_clinic/models/staff.py

393 lines
15 KiB
Python
Raw Permalink Normal View History

2014-10-29 09:10:19 +00:00
import time
from netforce.model import Model, fields, get_model
from netforce.access import get_active_company, get_active_user, set_active_user
class Staff(Model):
_name="clinic.staff"
_string="Staff"
2014-10-29 09:10:19 +00:00
_audit_log=True
_multi_company=True
2014-11-05 05:06:43 +00:00
def _get_age(self,ids,context={}):
2014-10-29 09:10:19 +00:00
res={}
year_now=int(time.strftime("%Y"))
for obj in self.browse(ids):
age=0
if obj.birthday:
year_bd=int(obj.birthday[0:4])
age=year_now-year_bd
res[obj.id]=age
return res
2014-11-05 05:06:43 +00:00
2014-11-14 01:58:12 +00:00
def _get_cycle(self,ids,context={}):
res={}
for obj in self.browse(ids):
cycle_id=None
for line in obj.cycles:
cycle=line.cycle_id
cycle_id=cycle.id
res[obj.id]=cycle_id
return res
2014-11-21 16:11:57 +00:00
2015-03-03 02:32:45 +00:00
def _get_rotation(self,ids,context={}):
2014-11-21 16:11:57 +00:00
res={}
for obj in self.browse(ids):
2014-11-25 16:31:29 +00:00
wage=0
2014-11-23 13:44:25 +00:00
max_cycle=0
2015-02-18 09:21:11 +00:00
ot_per_cycle=0
2015-03-03 02:32:45 +00:00
level_id=None
2015-03-03 16:53:31 +00:00
categ_id=None
2015-03-19 06:08:29 +00:00
for rotate in sorted(obj.rotations,key=lambda x: x.hire_date or ""):
2015-03-03 02:32:45 +00:00
if rotate.state=='approved':
level_id=rotate.level_id.id
wage=rotate.wage
max_cycle=rotate.max_cycle
ot_per_cycle=rotate.ot_per_cycle or 0
2015-03-03 16:53:31 +00:00
categ_id=rotate.categ_id.id
2014-11-23 13:44:25 +00:00
res[obj.id]={
2014-11-25 16:31:29 +00:00
'wage': wage,
2014-11-23 13:44:25 +00:00
'max_cycle': max_cycle,
2015-02-18 08:50:35 +00:00
'ot_per_cycle': ot_per_cycle,
2015-03-03 02:32:45 +00:00
'level_id': level_id,
2015-03-03 16:53:31 +00:00
'categ_id': categ_id,
2014-11-23 13:44:25 +00:00
}
2014-11-21 16:11:57 +00:00
return res
2014-12-04 02:52:49 +00:00
2015-01-14 04:31:24 +00:00
def _get_hdcase(self,ids,context={}):
2015-01-14 04:53:51 +00:00
# problem slow to load
2015-01-14 04:31:24 +00:00
res={}
for obj in self.browse(ids):
2015-01-14 04:53:51 +00:00
hids=[]
if obj.type=='doctor':
hids=get_model("clinic.hd.case.staff").search([['staff_id','=',obj.id]])
elif obj.type=='nurse':
item_ids=[item['cycle_item_id'][0] for item in get_model("clinic.cycle.item.line").search_read([['nurse_id','=',obj.id]],['cycle_item_id'])]
2015-01-14 04:53:51 +00:00
for item in get_model("clinic.cycle.item").browse(item_ids):
for hdcase in item.hd_cases:
hids.append(hdcase.id)
res[obj.id]=hids
2015-01-14 04:31:24 +00:00
return res
def _get_name(self,ids,context={}):
res={}
for obj in self.browse(ids):
name=''
2015-04-29 01:36:05 +00:00
title_name=''
title=obj.title_id
2015-06-24 11:02:34 +00:00
name_check=""
if title:
title_name=title.name or ""
title_name=title_name.replace(" ","")
if title_name.lower()!='notitle':
name+=obj.title_id.name or ""
if obj.first_name:
name+=obj.first_name or ""
2015-06-24 11:02:34 +00:00
name_check+=obj.first_name or ""
name+=" "
if obj.last_name:
name+=obj.last_name or ""
2015-06-24 11:02:34 +00:00
name_check+=obj.last_name or ""
if not obj.active:
2015-03-03 16:53:31 +00:00
name+='__'
elif context.get('active'):
2015-03-03 16:53:31 +00:00
name+='__'
res[obj.id]={
'name': name,
2015-04-29 01:36:05 +00:00
'name_check': name_check, # remove all space for make sure
}
return res
2015-03-11 01:44:02 +00:00
def _get_location(self,ids,context={}):
res={}
2015-06-03 04:39:08 +00:00
user_id=get_active_user()
set_active_user(1)
2015-03-11 01:44:02 +00:00
for obj in self.browse(ids):
2015-06-03 04:39:08 +00:00
st_codes=set([d.code for d in obj.departments])
res[obj.id]=",".join([dcode for dcode in sorted(st_codes)])
set_active_user(user_id)
2015-03-11 01:44:02 +00:00
return res
2014-11-14 01:58:12 +00:00
2015-03-13 17:20:48 +00:00
def _get_department_names(self,ids,context={}):
res={}
2015-03-15 15:42:15 +00:00
user_id=get_active_user()
set_active_user(1)
2015-03-13 17:20:48 +00:00
for obj in self.browse(ids):
2015-06-03 04:39:08 +00:00
dpt_st=set([dpt.name for dpt in obj.departments])
res[obj.id]=','.join([dpt_name for dpt_name in sorted(dpt_st)])
2015-03-15 15:42:15 +00:00
set_active_user(user_id)
2015-03-13 17:20:48 +00:00
return res
2015-04-27 10:53:47 +00:00
2014-10-29 09:10:19 +00:00
_fields={
'employee_id': fields.Many2One("hr.employee","Employee"),
2014-11-27 06:04:20 +00:00
"number": fields.Char("Number",required=True,search=True),
2015-02-24 14:59:49 +00:00
"title_id": fields.Many2One("clinic.name.title","Title"),
"first_name": fields.Char("First Name"),
"last_name": fields.Char("Last Name"),
"name": fields.Char("Name",function="_get_name",function_multi=True,store=True,required=True,search=True),
"name_check": fields.Char("Name",function="_get_name",function_multi=True,store=True),
2014-11-05 05:06:43 +00:00
"name_eng": fields.Char("Eng Name",search=True),
"nick_name": fields.Char("Nick Name",search=True),
2015-02-27 16:10:50 +00:00
"identification" : fields.Char("ID Card"),
2014-11-05 05:06:43 +00:00
"expiry_card" : fields.Date("Expired Card"),
2014-10-29 09:10:19 +00:00
"birthday": fields.Date("Birthday",search=True),
"age": fields.Integer("Age", function="_get_age"),
2015-01-21 10:57:11 +00:00
"state": fields.Selection([["part_time","Part Time"],["full_time","Full Time"]],"Working Status",search=True),
"type": fields.Selection([['staff','Staff'],["doctor","Doctor"],["nurse","Nurse"]],"Type"),
2014-11-05 05:06:43 +00:00
"gender": fields.Selection([["male","Male"],["female","Female"]],"Gender"),
"nation_id": fields.Many2One("clinic.nation","Nationality"),
2014-10-29 09:10:19 +00:00
"mobile": fields.Char("Mobile",required=False,search=True),
"phone": fields.Char("Phone",search=True),
'email': fields.Char("Email"),
2014-11-05 05:06:43 +00:00
"prof_license" : fields.Char("License No."),
2014-10-29 09:10:19 +00:00
"prof_license_date" : fields.Date("License Date"),
2014-11-05 05:06:43 +00:00
"prof_license_expiry" : fields.Date("Expired License"),
2014-10-29 09:10:19 +00:00
"birthday": fields.Date("BirthDay",search=True),
"department_id": fields.Many2One("clinic.department", "Department",search=True),
"addresses": fields.One2Many("address","staff_id","Addresses"),
2014-10-29 09:10:19 +00:00
"comments": fields.One2Many("message","related_id","Comments"),
2014-11-02 03:38:17 +00:00
"nurse_visits": fields.One2Many("clinic.visit","nurse_id","Visits"),
"doctor_visits": fields.One2Many("clinic.visit","doctor_id","Visits"),
2015-01-14 08:06:19 +00:00
"company_id": fields.Many2One("company","Company",search=True),
2015-01-18 10:31:54 +00:00
"user_id": fields.Many2One("base.user","User",search=True),
2015-01-09 05:19:52 +00:00
'image': fields.File("Image"),
2014-10-29 09:10:19 +00:00
'note': fields.Text("Note"),
2015-03-05 09:55:22 +00:00
'categ_id': fields.Many2One("clinic.staff.categ", "Category",function="_get_rotation",function_multi=True,store=True),
'level_id': fields.Many2One("clinic.staff.level", "Level", function="_get_rotation",function_multi=True,store=True),
2014-10-29 09:10:19 +00:00
'active': fields.Boolean("Active"),
'date': fields.Date("Register Date"),
2015-03-05 09:55:22 +00:00
'wage': fields.Float("Wage", function="_get_rotation",function_multi=True,store=True),
'max_cycle': fields.Integer("Max Cycle", function="_get_rotation",function_multi=True,store=True),
'ot_per_cycle': fields.Integer("OT Per Cycle", function="_get_rotation",function_multi=True,store=True),
2014-11-27 15:14:31 +00:00
'check_max_cycle': fields.Boolean("Check Max Cycle"),
2014-11-05 05:06:43 +00:00
'hire_date': fields.Date("Hire Date"),
'resign_date': fields.Date("Resign Date"),
"documents": fields.One2Many("document","related_id","Documents"),
2014-11-25 16:31:29 +00:00
"rotations": fields.One2Many("clinic.staff.rotation","staff_id","Staff Rotation"),
"cycles": fields.One2Many("clinic.staff.cycle","staff_id","Cycles"),
'cycle_id': fields.Many2One('clinic.cycle','Last Cycle',function="_get_cycle"),
2014-12-04 02:52:49 +00:00
"hd_case_staffs": fields.One2Many("clinic.hd.case.staff","staff_id","HD Cases"),
2015-01-14 05:30:22 +00:00
"hd_cases": fields.Many2Many("clinic.hd.case","HD Cases",function="_get_hdcase"), # not need to use (it's slow to load)
2015-04-27 10:53:47 +00:00
"cycle_item_lines": fields.One2Many("clinic.cycle.item.line","nurse_id","Cycle Items"),
2015-01-14 13:36:23 +00:00
'branch_id': fields.Many2One("clinic.branch","Branch", search=True),
2015-02-18 16:53:29 +00:00
"partner_id": fields.Many2One("partner","Contact"),
2015-02-24 14:59:49 +00:00
'departments': fields.Many2Many("clinic.department","Departments"),
2015-03-13 17:20:48 +00:00
'department_names': fields.Text("Departments",function="_get_department_names"),
2015-03-11 01:44:02 +00:00
"location": fields.Char("Location",function="_get_location",store=True),
2014-10-29 09:10:19 +00:00
}
def _get_number(self,context={}):
while 1:
2015-03-17 15:51:56 +00:00
seq_type='clinic_staff'
seq_id=get_model("sequence").find_sequence(type=seq_type)
2014-10-29 09:10:19 +00:00
if not seq_id:
return "/"
num=get_model("sequence").get_next_number(seq_id,context=context)
if not num:
return None
user_id=get_active_user()
set_active_user(1)
res=self.search([["number","=",num]])
set_active_user(user_id)
if not res:
return num
get_model("sequence").increment_number(seq_id,context=context)
2015-01-14 17:49:17 +00:00
def _get_branch(self,context={}):
b_ids=get_model('clinic.branch').search([])
if b_ids:
return b_ids[0]
2015-03-13 17:51:19 +00:00
def _get_departments(self,context={}):
res=get_model('select.company').get_select()
if res:
2015-03-15 15:42:15 +00:00
if res.get("department_ids"):
return res['department_ids']
else:
return [res['department_id']]
2015-03-12 11:04:43 +00:00
return get_model("clinic.department").search([])
2015-03-15 05:36:17 +00:00
def _get_default_location(self,context={}):
res=get_model('select.company').get_select()
2015-03-15 15:42:15 +00:00
code=''
2015-03-15 05:36:17 +00:00
if res:
2015-03-15 15:42:15 +00:00
dpt_ids=[]
if res.get("department_ids"):
dpt_ids=res['department_ids']
else:
2015-03-17 12:18:05 +00:00
dpt_ids=[res['department_id']]
2015-03-15 15:42:15 +00:00
dpts=get_model("clinic.department").browse(dpt_ids)
code=','.join([dpt.code for dpt in dpts])
return code
def _get_department(self,context={}):
res=get_model('select.company').get_select()
if res:
if res.get("department_ids"):
return res['department_ids'][0]
else:
return res['department_id']
2015-03-15 05:36:17 +00:00
2014-10-29 09:10:19 +00:00
_defaults={
'active': True,
2015-01-21 09:53:56 +00:00
"state": "part_time",
2014-11-29 00:48:00 +00:00
"type": lambda self,ctx: ctx.get("type") or "staff",
2014-10-29 09:10:19 +00:00
"date": lambda *a: time.strftime("%Y-%m-%d"),
"number": _get_number,
"company_id": lambda *a: get_active_company(),
2015-01-14 17:49:17 +00:00
'branch_id': _get_branch,
2015-03-13 17:51:19 +00:00
'departments': _get_departments,
2015-03-15 15:42:15 +00:00
'department_id': _get_department,
2015-03-15 05:36:17 +00:00
'location': _get_default_location,
2014-10-29 09:10:19 +00:00
}
2015-04-28 03:29:15 +00:00
_sql_constraints=("clinic_staff_key_uniq","unique(name_check)","name should be unique"),
2014-10-29 09:10:19 +00:00
_order="date desc,number desc"
2014-11-25 16:31:29 +00:00
2015-02-25 13:19:50 +00:00
def create_contact(self,code,name):
2015-02-18 16:53:29 +00:00
partner_id=get_model("partner").create({
2015-02-25 13:19:50 +00:00
'code': code,
2015-02-18 16:53:29 +00:00
'name': name,
'last_name': name,
'type': 'person',
'is_staff': True,
})
address_id=get_model('address').create({
'type': 'shipping',
'partner_id': partner_id,
'address': 'your address',
'address2': 'your address2',
'city': 'your city',
'postal_code': 'your zip',
'country_id': 1,
})
return partner_id, address_id
def check_contact(self,name=""):
partner_id=None
dom=[
['name','=',name],
['is_staff','=',True],
]
for partner in get_model("partner").search_browse(dom):
if partner.name==name:
partner_id=partner.id
break
return partner_id
2014-11-26 01:39:49 +00:00
def check_emp(self,name="", employee_id=None):
names=name.split(" ")
first_name=names[0]
last_name=names[-1]
if first_name==last_name:
first_name=""
vals={
'first_name': first_name,
'last_name': last_name,
}
2014-11-25 16:31:29 +00:00
emp_obj=get_model("hr.employee")
emp_ids=emp_obj.search([['last_name','=',last_name]])
2014-11-26 01:39:49 +00:00
emp_id=None
if not emp_ids and not employee_id:
emp_id=emp_obj.create(vals)
elif employee_id:
emp_obj.browse(employee_id).write(vals)
emp_id=employee_id
else:
emp_id=emp_ids[0]
return emp_id
def create(self,vals,**kw):
2015-02-25 13:19:50 +00:00
title_id=vals['title_id']
title=get_model("clinic.name.title").browse(title_id)
title_name=title.name or ""
vals['name']=''
if title_name.lower()=='notitle':
vals['name']='%s%s %s'%(title_name,vals['first_name'],vals['last_name'])
else:
vals['name']='%s %s'%(vals['first_name'],vals['last_name'])
2015-03-11 10:08:42 +00:00
partner_name='%s %s'%(vals['first_name'],vals['last_name']) #XXX
2014-11-26 01:39:49 +00:00
employee_id=vals.get('employee_id')
2015-02-25 13:19:50 +00:00
name=vals['name']
2014-11-26 01:39:49 +00:00
emp_id=self.check_emp(name,employee_id)
if emp_id:
vals['employee_id']=emp_id
2015-03-11 10:08:42 +00:00
partner_id=get_model("clinic.staff").check_contact(partner_name)
2015-02-18 16:53:29 +00:00
address_id=None
if not partner_id:
2015-03-11 10:08:42 +00:00
vals['partner_id'],address_id=self.create_contact(vals['number'],partner_name)
2014-11-25 16:31:29 +00:00
new_id=super().create(vals,**kw)
self.function_store([new_id])
2015-02-25 13:19:50 +00:00
for obj in self.browse([new_id]):
if address_id:
addr=get_model('address').browse(address_id)
addr.write({
'partner_id': vals['partner_id'],
'staff_id': new_id,
})
2015-03-12 11:04:43 +00:00
if not obj.departments:
dpt_ids=get_model("clinic.department").search([])
for st in get_model("clinic.staff").search_browse([]):
if not st.departments:
st.write({
'departments': [['set',dpt_ids]],
})
print('set all department for ', st.name)
2014-11-25 16:31:29 +00:00
return new_id
2014-10-29 09:10:19 +00:00
2014-11-26 01:39:49 +00:00
def write(self,ids,vals,**kw):
obj=self.browse(ids)[0]
name=vals.get("name","")
if not name:
name=obj.name
emp_id=self.check_emp(name,obj.employee_id.id)
if emp_id:
vals['employee_id']=emp_id
2015-03-11 10:08:42 +00:00
partner_name='%s %s'%(obj.first_name or "",obj.last_name or "")
partner_id=get_model("clinic.staff").check_contact(partner_name)
2015-02-18 16:53:29 +00:00
address_id=None
if not partner_id:
2015-03-11 10:08:42 +00:00
vals['partner_id'],address_id=self.create_contact(partner_name,partner_name) #XXX
2014-11-26 01:39:49 +00:00
super().write(ids,vals,**kw)
self.function_store(ids)
2015-02-18 16:53:29 +00:00
if address_id:
addr=get_model('address').browse(address_id)
addr.write({
'partner_id': vals['partner_id'],
'staff_id': ids[0],
})
2015-03-12 11:04:43 +00:00
if not obj.departments:
dpt_ids=get_model("clinic.department").search([])
for st in get_model("clinic.staff").search_browse([]):
if not st.departments:
st.write({
'departments': [['set',dpt_ids]],
})
print('set all department for ', st.name)
2014-11-26 01:39:49 +00:00
2014-12-08 14:54:25 +00:00
def name_get(self,ids,context={}):
vals=[]
for obj in self.browse(ids):
2015-01-14 17:49:17 +00:00
name=obj.name or ""
2015-02-09 12:49:26 +00:00
nickname=obj.nick_name or ""
2015-03-02 04:03:07 +00:00
if nickname:
name+=" (%s)"%(nickname)
2014-12-08 14:54:25 +00:00
vals.append((obj.id,name))
return vals
def name_search(self,name,domain=None,condition=[],context={},**kw):
2014-12-08 14:54:25 +00:00
dom=[["name","ilike","%"+name+"%"]]
if domain:
dom=[dom,domain]
ids1=self.search(dom)
2015-02-09 12:49:26 +00:00
dom=[["nick_name","ilike","%"+name+"%"]]
if domain:
dom=[dom,domain]
2015-03-02 04:03:07 +00:00
ids2=self.search(dom)
ids=list(set(ids1+ids2))
2014-12-08 14:54:25 +00:00
return self.name_get(ids,context=context)
Staff.register()