clinic/netforce_clinic/models/visit.py

494 lines
17 KiB
Python
Raw Normal View History

2014-10-02 00:34:58 +00:00
import time
2014-10-14 03:57:20 +00:00
from datetime import datetime, timedelta
2014-10-02 00:34:58 +00:00
2014-08-19 11:36:46 +00:00
from netforce.model import Model, fields, get_model
2014-10-02 19:12:52 +00:00
from netforce.access import get_active_company, get_active_user, set_active_user
2014-10-14 03:57:20 +00:00
FMT_DATE="%Y-%m-%d %H:%M:%S"
2014-08-19 11:36:46 +00:00
class Visit(Model):
_name="clinic.visit"
_string="Visit"
_audit_log=True
_name_field="number"
_multi_company=True
2014-10-15 17:12:03 +00:00
def _get_print_date(self,ids,context={}):
res={}
for obj in self.browse(ids):
res[obj.id]=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return res
2014-11-17 00:59:19 +00:00
2015-01-09 05:19:52 +00:00
def _get_all(self,ids,context={}):
2014-11-17 00:59:19 +00:00
res={}
for obj in self.browse(ids):
2015-01-09 05:19:52 +00:00
cycle=obj.cycle_id
color=cycle.color
res[obj.id]={
'cycle_color': color,
'sequence': '%s-%s'%(obj.time_start[0:10],cycle.sequence), #date-sequence
}
2014-11-17 00:59:19 +00:00
return res
2014-11-01 17:30:48 +00:00
2014-08-19 11:36:46 +00:00
_fields={
"number": fields.Char("Number",required=True,search=True),
2014-11-29 14:56:15 +00:00
"time_start": fields.DateTime("Start Time",required=True),
"time_stop": fields.DateTime("End Time",required=True),
2014-10-01 10:52:21 +00:00
"patient_id": fields.Many2One("clinic.patient","Patient",required=True,search=True),
"doctor_id": fields.Many2One("clinic.staff","Doctor", domain=[['type','=','doctor']],search=True),
"nurse_id": fields.Many2One("clinic.staff","Confirm By", domain=[['type','=','nurse']],search=True),
2014-10-02 00:34:58 +00:00
"department_id": fields.Many2One("clinic.department", "Department",search=True),
2014-08-19 11:36:46 +00:00
"comments": fields.One2Many("message","related_id","Comments"),
"company_id": fields.Many2One("company","Company"),
2014-10-05 08:47:12 +00:00
'hd_cases': fields.One2Many('clinic.hd.case','visit_id',"HD Cases",readonly=True),
2014-10-14 03:57:20 +00:00
"cycle_id": fields.Many2One("clinic.cycle","Cycle"),
2014-12-04 14:08:29 +00:00
"state": fields.Selection([["draft","Draft"],['pending','Pending'],["confirmed","Confirmed"],["cancelled","Cancelled"]],"Status",required=True),
2014-10-14 03:57:20 +00:00
"comments": fields.One2Many("message","related_id","Comments"),
2014-10-15 17:12:03 +00:00
'print_date': fields.Date('Print Date',function="_get_print_date"),
2014-11-03 00:21:37 +00:00
'cycle_item_id': fields.Many2One("clinic.cycle.item","Cycle Item"), #XXX on_delete="cascade" -> rm visit from cycle item
2015-01-09 05:19:52 +00:00
'sequence': fields.Char("Sequence",function="_get_all",function_multi=True,store=True),
'visit_date': fields.Date('Date'),
2015-01-09 05:19:52 +00:00
'cycle_color': fields.Char('Color',function="_get_all",function_multi=True,store=True),
2014-11-17 00:59:19 +00:00
'note': fields.Text('Note'),
2015-01-14 11:00:14 +00:00
'branch_id': fields.Many2One("clinic.branch","Branch"),
2014-08-19 11:36:46 +00:00
}
2014-10-15 17:12:03 +00:00
def _get_number(self,context={}):
2014-08-19 11:36:46 +00:00
while 1:
seq_name='Clinic Visit'
seq_id=get_model("sequence").find_sequence(name=seq_name)
if not seq_id:
raise Exception("Can not found sequence %s"%seq_name)
2014-10-02 19:12:52 +00:00
num=get_model("sequence").get_next_number(seq_id,context=context)
2014-08-19 11:36:46 +00:00
if not num:
return None
2014-10-02 19:12:52 +00:00
user_id=get_active_user()
set_active_user(1)
2014-08-19 11:36:46 +00:00
res=self.search([["number","=",num]])
2014-10-02 19:12:52 +00:00
set_active_user(user_id)
2014-08-19 11:36:46 +00:00
if not res:
return num
2014-10-02 19:12:52 +00:00
get_model("sequence").increment_number(seq_id,context=context)
2014-08-19 11:36:46 +00:00
2014-10-02 00:34:58 +00:00
def _get_nurse(self,context={}):
user_id=get_active_user()
nurse_ids=get_model("clinic.staff").search([['user_id','=',user_id],['type','=','nurse']])
2014-10-02 00:34:58 +00:00
if nurse_ids:
return nurse_ids[0]
return None
2014-08-19 11:36:46 +00:00
2014-10-05 05:47:19 +00:00
def _get_time_start(self,context={}):
2014-10-14 03:57:20 +00:00
now=datetime.now()
2014-10-05 05:47:19 +00:00
starttime=now.strftime("%Y-%m-%d %H:%M:%S")
return starttime
def _get_time_stop(self,context={}):
2014-10-14 03:57:20 +00:00
hr=timedelta(seconds=3600)
now=datetime.now()
2014-10-05 05:47:19 +00:00
stoptime=(now+hr).strftime("%Y-%m-%d %H:%M:%S")
return stoptime
2014-08-19 11:36:46 +00:00
_defaults={
2014-10-04 15:51:54 +00:00
"state": "draft",
2014-11-01 17:30:48 +00:00
'time_start': _get_time_start,
'time_stop': _get_time_stop,
2014-12-03 17:46:50 +00:00
'visit_date': lambda *a: time.strftime("%Y-%m-%d"),
2014-10-04 15:51:54 +00:00
"number": "/",
2014-08-19 11:36:46 +00:00
"company_id": lambda *a: get_active_company(),
2014-10-02 00:34:58 +00:00
'nurse_id': _get_nurse,
2014-08-19 11:36:46 +00:00
}
2014-11-02 03:03:28 +00:00
2014-11-01 17:30:48 +00:00
_order="sequence"
2014-12-04 14:08:29 +00:00
2015-01-14 18:58:10 +00:00
def new_dlz(self,patient_id,department_id):
2014-12-04 14:08:29 +00:00
if not patient_id:
raise Exception("No Patient to creat Dialyzer")
2015-01-14 18:58:10 +00:00
if not department_id:
raise Exception("No Department to creat Dialyzer")
2014-12-04 14:08:29 +00:00
dlz_vals=get_model("clinic.dialyzer").default_get()
dlz_vals['product_id']=dlz_vals['product_id'][0]
dlz_vals['company_id']=dlz_vals['company_id'][0]
dlz_vals['patient_id']=patient_id
2015-01-14 18:58:10 +00:00
dlz_vals['department_id']=department_id
2014-12-04 14:08:29 +00:00
dlz_id=get_model('clinic.dialyzer').create(dlz_vals)
dialyzer=get_model("clinic.dialyzer").browse(dlz_id)
2015-02-10 08:30:21 +00:00
dialyzer.validate()
2014-12-04 14:08:29 +00:00
return dlz_id
def get_dlz(self,visit_id):
obj=self.browse(visit_id)
patient=obj.patient_id
dialyzers=get_model("clinic.dialyzer").search_browse([['patient_id','=',patient.id],['state','=','active']])
if not dialyzers:
2015-01-15 08:45:11 +00:00
return {} ### for to new dlz from hd case
2015-01-14 18:58:10 +00:00
dlz_id=self.new_dlz(patient.id, obj.department_id.id)
2014-12-04 14:08:29 +00:00
dialyzer=get_model("clinic.dialyzer").browse(dlz_id)
else:
dialyzer=dialyzers[-1]
use_time=dialyzer.use_time or 0
max_time=dialyzer.max_use_time or 0
if use_time > max_time:
dialyzer.write({
'state': 'expire',
})
# get new dialyzer
2015-01-14 18:58:10 +00:00
dlz_id=self.new_dlz(patient.id, obj.department_id.id)
2014-12-04 14:08:29 +00:00
dialyzer=get_model("clinic.dialyzer").browse(dlz_id)
use_time+=1
vals={
2015-02-10 05:22:04 +00:00
'dialyzer_id': dialyzer.id,
'membrane_type': dialyzer.membrane_type or '',
"dialyzer_type": dialyzer.dialyzer_type,
2014-12-04 14:08:29 +00:00
"use_time": use_time,
"max_use_time": dialyzer.max_use_time,
2015-02-10 05:22:04 +00:00
"description": dialyzer.name or dialyzer.product_id.name or "",
2014-12-04 14:08:29 +00:00
}
return vals
2014-08-19 11:36:46 +00:00
2014-10-02 00:34:58 +00:00
def confirm(self,ids,context={}):
2014-11-29 14:56:15 +00:00
obj=self.browse(ids)[0]
2014-10-04 15:51:54 +00:00
number=obj.number
2014-10-27 14:17:22 +00:00
if number=="/" or not number:
2014-10-04 15:51:54 +00:00
number=self._get_number(context)
vals={
'number': number,
2014-10-15 03:49:20 +00:00
'state': 'confirmed',
2014-10-04 15:51:54 +00:00
}
2014-11-29 14:56:15 +00:00
user_id=get_active_user()
staff=get_model("clinic.staff").search_browse([['user_id','=',user_id]])
if staff:
vals['nurse_id']=staff[0].id
2014-10-04 15:51:54 +00:00
obj.write(vals)
2014-10-02 02:02:22 +00:00
hd_case_obj=get_model("clinic.hd.case")
2014-10-02 19:12:52 +00:00
vals={
2014-10-02 02:02:22 +00:00
'patient_id': obj.patient_id.id,
2015-01-30 11:15:13 +00:00
'patient_type_id': obj.patient_id.type_id.id,
2015-01-29 14:55:05 +00:00
'vascular_acc': obj.patient_id.vascular_acc.id,
'nurse_id': obj.nurse_id.id,
2014-10-02 02:02:22 +00:00
'department_id': obj.department_id.id,
2014-10-05 05:47:19 +00:00
'time_start': obj.time_start,
'time_stop': obj.time_stop,
'cycle_id' : obj.cycle_id.id,
2014-10-02 02:02:22 +00:00
'visit_id': obj.id,
2015-01-14 11:00:14 +00:00
'branch_id': obj.branch_id.id,
2014-10-02 19:12:52 +00:00
'lines':[],
'dialyzers': [],
'staffs': [],
2014-11-29 14:56:15 +00:00
'state': 'waiting_treatment',
2015-02-04 19:02:44 +00:00
'date': obj.visit_date, #XXX
2014-10-02 19:12:52 +00:00
}
2014-11-02 07:13:20 +00:00
2014-12-04 14:08:29 +00:00
st=get_model("clinic.setting").browse(1)
if st.find_dlz:
dialyzer=self.get_dlz(obj.id)
2015-01-15 08:45:11 +00:00
if dialyzer:
vals['dialyzers'].append(('create',dialyzer))
2014-12-04 14:08:29 +00:00
2015-01-14 05:30:22 +00:00
vals['staffs'].append(('create',{
'staff_id': obj.doctor_id.id,
'type': 'doctor',
'priop': 'owner',
}))
2014-11-22 06:08:42 +00:00
# use exist hd_case (in case set to draft)
hd_case_id=None
if obj.hd_cases:
hd_case=obj.hd_cases[0]
hd_case.write(vals)
hd_case_id=hd_case.id
else:
hd_case_id=hd_case_obj.create(vals)
2014-10-27 14:17:22 +00:00
if context.get("called"): #XXX call outside
return hd_case_id
2014-11-21 02:39:26 +00:00
date=vals['time_start'][0:10]
cycle=obj.cycle_id
2015-01-14 13:36:23 +00:00
branch=obj.branch_id
2015-01-16 11:19:49 +00:00
department=obj.department_id
2014-11-21 02:39:26 +00:00
dom=[]
2015-01-15 06:32:52 +00:00
if date:
dom.append(['date','=',date])
if cycle:
dom.append(['cycle_id','=',cycle.id])
if branch:
dom.append(['branch_id','=',branch.id])
2015-01-16 11:19:49 +00:00
if department:
dom.append(['department_id','=',department.id])
2014-11-21 02:39:26 +00:00
item_obj=get_model('clinic.cycle.item')
item_ids=item_obj.search(dom)
def get_schedule(date):
dom=[]
dom.append(['date','=',date])
2015-01-22 07:20:04 +00:00
if branch:
dom.append(['branch_id','=',branch.id])
if department:
dom.append(['department_id','=',department.id])
2014-11-21 02:39:26 +00:00
schedule=None
schedules=get_model("clinic.schedule").search_browse(dom)
if schedules:
schedule=schedules[0]
return schedule
item_vals={
'cycle_id': cycle.id,
2015-01-14 13:36:23 +00:00
'branch_id': branch.id,
2015-01-16 11:19:49 +00:00
'department_id': department.id,
2014-11-21 02:39:26 +00:00
'date': date,
2015-01-19 01:27:10 +00:00
'lines': [],
2014-11-21 02:39:26 +00:00
}
item_id=None
if not item_ids:
item_id=item_obj.create(item_vals)
else:
# clear old nurse in cycle item
item_id=item_ids[0]
item=item_obj.browse(item_id)
2015-02-09 12:49:26 +00:00
2015-02-09 06:57:23 +00:00
#FIXME problem 1 staff can have many department
user_id=get_active_user()
set_active_user(1)
2015-01-22 07:20:04 +00:00
# copy nurse from schedule(only draft)
2014-11-21 02:39:26 +00:00
item=item_obj.browse(item_id)
2015-01-22 07:20:04 +00:00
if not item.lines and item.state=='draft':
2014-11-25 05:24:51 +00:00
schedule=get_schedule(date)
if schedule:
for line in schedule.lines:
if line.cycle_id.id==cycle.id:
nurse=line.nurse_id
2015-02-09 12:49:26 +00:00
dpt=line.department_id
2015-01-19 01:27:10 +00:00
item_vals['lines'].append(('create',{
2014-11-25 05:24:51 +00:00
'nurse_id': nurse.id,
'level_id': nurse.level_id.id,
2015-02-09 12:49:26 +00:00
'department_id': dpt.id,
2015-02-08 08:48:08 +00:00
'categ_id': nurse.categ_id.id,
2014-11-25 05:24:51 +00:00
}))
item.write(item_vals)
2014-11-21 02:39:26 +00:00
2015-01-22 07:20:04 +00:00
# copy nurse from previous cycle item
if not item.lines:
dom=[
['branch_id','=',branch.id],
['department_id','=',department.id],
2015-01-23 00:13:14 +00:00
['cycle_id','=',cycle.id],
2015-01-22 07:20:04 +00:00
]
2015-01-23 09:41:12 +00:00
for item2 in get_model("clinic.cycle.item").search_browse(dom):
2015-01-22 07:20:04 +00:00
lines=[]
for line in item2.lines:
nr=line.nurse_id
2015-02-09 12:49:26 +00:00
dpt=line.department_id
2015-01-22 07:20:04 +00:00
lines.append(('create', {
'nurse_id': nr.id,
'level_id': nr.level_id.id,
2015-02-09 12:49:26 +00:00
'department_id': dpt.id,
2015-02-08 08:48:08 +00:00
'categ_id': nr.categ_id.id,
2015-01-22 07:20:04 +00:00
}))
if lines:
print("copy nurse from %s to %s"%(item2.name, item.name))
item.write({
'lines': lines,
})
break
2014-11-21 02:39:26 +00:00
obj.write({
'cycle_item_id': item_id,
})
2015-02-09 06:57:23 +00:00
#XXX
set_active_user(user_id)
2014-11-21 02:39:26 +00:00
hd_case=hd_case_obj.browse(hd_case_id)
hd_case.write({
'cycle_item_id': item_id,
})
2014-10-02 02:02:22 +00:00
return {
'next': {
'name': 'clinic_hd_case',
'mode': 'form',
'active_id': hd_case_id,
},
2014-12-04 08:37:54 +00:00
'flash': 'Visit has been confirmed',
2014-10-02 02:02:22 +00:00
}
2014-08-19 11:36:46 +00:00
2014-10-05 05:47:19 +00:00
def discard(self,ids,context={}):
2014-08-19 11:36:46 +00:00
obj=self.browse(ids)[0]
2014-10-02 00:34:58 +00:00
obj.write({"state":"cancelled"})
2014-08-19 11:36:46 +00:00
def reopen(self,ids,context={ }):
2014-10-02 02:02:22 +00:00
obj=self.browse(ids)[0]
obj.write({"state":"draf t"})
2014-10-02 02:02:22 +00:00
2014-10-02 00:34:58 +00:00
def onchange_patient(self,context={}):
data=context['data']
patient_id=data['patient_id']
2014-12-04 08:37:54 +00:00
patient=get_model("clinic.patient").browse(patient_id)
2014-10-03 07:27:57 +00:00
visits=self.search_browse([['patient_id','=',patient_id]],order="number desc")
2014-10-02 00:34:58 +00:00
if visits:
2014-10-03 07:27:57 +00:00
visit=visits[0]
2014-11-27 15:14:31 +00:00
doctor=visit.doctor_id
2015-01-11 08:10:38 +00:00
department_id=None
if visit.department_id:
department_id=visit.department_id.id
elif patient.department_id:
department_id=patient.department_id.id
2015-01-14 11:00:14 +00:00
branch_id=None
if visit.branch_id:
branch_id=visit.branch_id.id
elif patient.branch_id:
branch_id=patient.branch_id.id
print("branch_id ", branch_id)
2014-11-27 15:14:31 +00:00
data['doctor_id']=doctor.id
2015-01-11 08:10:38 +00:00
data['department_id']=department_id
2015-01-14 11:00:14 +00:00
data['branch_id']=branch_id
2015-01-11 08:10:38 +00:00
2014-12-04 08:37:54 +00:00
if not patient.doctor_id:
patient.write({
'doctor_id': doctor.id,
})
2014-10-02 00:34:58 +00:00
else:
2014-12-04 17:32:39 +00:00
doctor=patient.doctor_id
department=patient.department_id
2015-01-14 11:00:14 +00:00
branch=patient.branch_id
2014-12-04 17:32:39 +00:00
data['doctor_id']=doctor.id
data['department_id']=department.id
2015-01-14 11:00:14 +00:00
data['branch_id']=branch.id
return data
2014-10-02 19:12:52 +00:00
def copy(self,ids,context={}) :
2014-10-02 19:12:52 +00:00
obj=self.browse(ids[0])
vals={
'patient_id': obj.patient_id.id,
'doctor_id': obj.doctor_id.id,
'nurse_id': obj.nurse_id.id,
'department_id': obj.department_id.id,
2014-12-04 14:08:29 +00:00
'cycle_id': obj.cycle_id.id,
'time_start': obj.time_start,
'time_stop': obj.time_stop,
'visit_date': obj.visit_date,
2014-10-02 19:12:52 +00:00
}
new_id=self.create(vals,context=context)
return {
'next':{
'name': 'clinic_visit',
'mode': 'form',
'active_id': new_id,
},
2014-12-04 14:08:29 +00:00
'flash': 'Visit %s successfully'%(obj.number),
2014-10-02 19:12:52 +00:00
}
2014-10-04 15:51:54 +00:00
def delete(self,ids,context={}):
for obj in self.browse(ids):
2014-12-19 18:19:19 +00:00
if obj.state not in ('draft','pending'):
2014-10-04 15:51:54 +00:00
raise Exception("Can not delete visit %s because state is not draft!"%obj.number)
super().delete(ids,context=context)
2014-10-04 16:11:34 +00:00
def get_data(self,context={}):
ref_id=context.get("refer_id")
if not ref_id:
return {}
ref_id=int(ref_id)
obj=self.browse(ref_id)
data={
'number': obj.number,
2014-10-05 08:29:21 +00:00
'date_visit': obj.date_visit,
2014-10-04 16:11:34 +00:00
'department_name': obj.department_id.name or "",
'patient_name': obj.patient_id.name or "",
'doctor_name': obj.doctor_id.name or "",
'nurse_name': obj.nurse_id.name or "",
'print_date': time.strftime("%d/%m/%Y"),
2014-10-14 04:31:51 +00:00
'cycle_name': obj.cycle_id.name or "",
2014-10-04 16:11:34 +00:00
}
return data
2014-08-19 11:36:46 +00:00
2014-12-03 17:46:50 +00:00
def onchange_cycle(self,context={}):
2014-10-14 03:57:20 +00:00
data=context['data']
cycle_id=data['cycle_id']
2014-12-03 17:46:50 +00:00
date=data['visit_date']
if not date:
date=data['time_start'][0:10]
2014-10-14 03:57:20 +00:00
if cycle_id:
cycle=get_model('clinic.cycle').browse(cycle_id)
2014-12-03 17:46:50 +00:00
data['time_start']=date+' %s:00'%cycle.time_start
data['time_stop']=date+' %s:00'%cycle.time_stop
2014-10-05 05:47:19 +00:00
return data
2014-10-15 03:49:20 +00:00
2014-12-03 17:46:50 +00:00
def onchange_date(self,context={}):
data=context['data']
date=data['visit_date']
time_start=data['time_start'][11:]
time_stop=data['time_stop'][11:]
data['time_start']='%s %s'%(date,time_start)
data['time_stop']='%s %s'%(date,time_stop)
return data
def onchange_datefrom(self,context={}):
data=context['data']
data['visit_date']=data['time_start'][0:10]
return data
2014-10-15 03:49:20 +00:00
def to_draft(self,ids,context={}):
obj=self.browse(ids)[0]
for hd_case in obj.hd_cases:
2014-11-22 06:08:42 +00:00
hd_case.to_draft()
for line in hd_case.lines:
line.delete()
for ps in hd_case.staffs:
2014-11-22 06:08:42 +00:00
ps.delete()
2014-10-15 03:49:20 +00:00
obj.write({
'state': 'draft',
})
2014-10-15 17:12:03 +00:00
def gen_no(self,ids,context={}):
obj=self.browse(ids)[0]
2015-01-15 01:18:44 +00:00
if obj.number != '/' or obj.state not in ('draft','pending'):
2014-10-15 17:12:03 +00:00
return
number=self._get_number(context)
obj.write({
'number': number,
})
return {
'next': {
'name': 'clinic_visit',
'mode': 'form',
'active_id': obj.id,
},
2014-11-25 01:41:49 +00:00
'flash': 'Generate number succesfully',
2014-10-15 17:12:03 +00:00
}
2014-11-01 08:49:27 +00:00
2014-11-01 17:30:48 +00:00
def cancel(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'cancelled',
})
return {
'next': {
'name': 'clinic_visit',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'Visit\'s %s has been cancelled'%obj.patient_id.name
}
2015-01-09 05:19:52 +00:00
def create(self, vals,**kw):
new_id=super().create(vals,**kw)
self.function_store([new_id])
return new_id
2014-11-02 02:13:15 +00:00
def write(self,ids,vals,**kw):
super().write(ids,vals,**kw)
2015-01-09 05:19:52 +00:00
self.function_store(ids)
2014-12-15 06:24:58 +00:00
def pending(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'pending',
})
2014-10-05 08:29:21 +00:00
2014-08-19 11:36:46 +00:00
Visit.register()