clinic/netforce_clinic/models/visit.py

330 lines
12 KiB
Python
Raw Normal View History

2014-10-02 00:34:58 +00:00
import time
2014-10-02 02:02:22 +00:00
import datetime
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-04 18:33:18 +00:00
from netforce.utils import get_data_path
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-04 15:51:54 +00:00
def _get_visit_late(self,ids,context={}):
res={}
2014-10-04 18:33:18 +00:00
#oneday=datetime.timedelta(days=1)
#yesterday=datetime.datetime.now()-oneday
2014-10-04 15:51:54 +00:00
datenow=time.strftime("%Y-%m-%d")
for obj in self.browse(ids):
late=0
2014-10-05 08:29:21 +00:00
if datenow > obj.date_visit and obj.state=='waiting_treatment':
2014-10-04 15:51:54 +00:00
late=1
res[obj.id]=late
return res
2014-10-05 05:47:19 +00:00
2014-08-19 11:36:46 +00:00
_fields={
"number": fields.Char("Number",required=True,search=True),
2014-10-01 10:52:21 +00:00
"patient_id": fields.Many2One("clinic.patient","Patient",required=True,search=True),
2014-08-19 11:36:46 +00:00
"doctor_id": fields.Many2One("clinic.doctor","Doctor",search=True),
"nurse_id": fields.Many2One("clinic.nurse","Nurse",search=True),
2014-10-02 00:34:58 +00:00
"department_id": fields.Many2One("clinic.department", "Department",search=True),
2014-09-11 03:21:52 +00:00
"cycle": fields.Selection([("1","One"),("2","Two"),("3","Tree"),("4","Four")],"Cycle",required=True),
2014-10-04 15:51:54 +00:00
"state": fields.Selection([("draft","Draft"),("waiting_treatment","Waiting Treatment"),('treatment','Treatment'),("cancelled","Cancelled")],"Status",required=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:29:21 +00:00
'time_use': fields.Integer("Fix Hour"),
2014-10-04 15:51:54 +00:00
'late_visit': fields.Integer("Late Vist",function="_get_visit_late"),
2014-10-04 18:33:18 +00:00
"dialyzers": fields.One2Many("clinic.dialyzer.line","visit_id","Dialyzers"),
2014-10-05 08:29:21 +00:00
"date_visit": fields.Date("Visit Date",required=True,search=True),
2014-10-05 05:47:19 +00:00
"time_start": fields.DateTime("Time Start"),
"time_stop": fields.DateTime("Time Stop"),
2014-10-05 08:29:21 +00:00
'planes': fields.One2Many("clinic.visit.plane","visit_id","Planning"),
2014-08-19 11:36:46 +00:00
}
def _get_number(self,context={}):
while 1:
2014-10-02 19:12:52 +00:00
seq_id=get_model("sequence").find_sequence(name="Clinic Visit")
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()
print("user_id ",user_id)
nurse_ids=get_model("clinic.nurse").search([['user_id','=',user_id]])
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={}):
now=datetime.datetime.now()
starttime=now.strftime("%Y-%m-%d %H:%M:%S")
return starttime
def _get_time_stop(self,context={}):
hr=datetime.timedelta(seconds=3600)
now=datetime.datetime.now()
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-10-02 00:34:58 +00:00
"cycle": "1",
2014-10-05 08:29:21 +00:00
'date_visit': lambda *a: time.strftime("%Y-%m-%d"),
2014-10-05 05:47:19 +00:00
'time_start': _get_time_start,
'time_stop': _get_time_stop,
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-10-02 19:12:52 +00:00
'time_use': 1,
2014-08-19 11:36:46 +00:00
}
2014-10-04 15:51:54 +00:00
_order="id desc"
2014-08-19 11:36:46 +00:00
2014-10-02 19:12:52 +00:00
def get_dialyzer(self):
return
2014-08-19 11:36:46 +00:00
2014-10-02 00:34:58 +00:00
def confirm(self,ids,context={}):
2014-10-04 15:51:54 +00:00
obj=self.browse(ids[0])
number=obj.number
if number=="/":
number=self._get_number(context)
vals={
'number': number,
'state': 'waiting_treatment',
}
obj.write(vals)
def do_treatment(self,ids,context={}):
2014-10-02 02:02:22 +00:00
hd_case_obj=get_model("clinic.hd.case")
dt=datetime.datetime
2014-10-04 15:51:54 +00:00
datenow=dt.now().strftime("%Y-%m-%d")
obj=self.browse(ids)[0]
# cannot treatment if current date is not same visit date
2014-10-05 08:29:21 +00:00
if obj.date_visit!=datenow:
2014-10-04 15:51:54 +00:00
raise Exception("Today is not treament date!")
2014-10-04 18:33:18 +00:00
if not obj.dialyzers:
raise Exception("Please select dialyzer!")
2014-10-05 05:47:19 +00:00
if len(obj.dialyzers)>1:
raise Exception("Can select only 1 dialyzer!")
2014-10-04 15:51:54 +00:00
2014-10-02 19:12:52 +00:00
include_fee=obj.patient_id.type in ('mg','sc','nhso') and True or False
vals={
2014-10-02 02:02:22 +00:00
'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-10-05 05:47:19 +00:00
'time_start': obj.time_start,
'time_stop': obj.time_stop,
2014-10-02 02:02:22 +00:00
'visit_id': obj.id,
2014-10-02 19:12:52 +00:00
'fee': include_fee and 1500.00 or 0.0,
'fee_type': obj.patient_id.type,
'lines':[],
'dialyzers': [],
2014-10-04 18:33:18 +00:00
'state': 'in_progress',
2014-10-02 19:12:52 +00:00
}
patient_type={
"mg":"Medical Government",
"sc":"Social Security",
"nhso":"NHSO (30฿)",
"personal": "Personal",
"others": "Others",
}
categ_name=patient_type.get(obj.patient_id.type)
categ_ids=get_model("partner.categ").search([['name','=',categ_name]])
if not categ_ids:
raise Exception("Category: %s not found"%categ_name)
partner_id=None
if obj.patient_id.type in ("mg","sc","nhso"):
partner_obj=get_model("partner")
for partner in partner_obj.search_browse([]):
if partner.categ_id.id in categ_ids:
partner_id=partner.id
vals['fee_partner_id']=partner_id
break
# find dialyser
2014-10-04 18:33:18 +00:00
if not obj.dialyzers:
2014-10-04 15:51:54 +00:00
raise Exception("%s don't have dialyzer for treatment yet" % obj.patient_id.name)
2014-10-03 07:27:57 +00:00
hd_case_id=hd_case_obj.create(vals)
2014-10-04 18:33:18 +00:00
for dlz in obj.dialyzers:
# TODO check max time and expire day also
dlz.dialyzer_id.write({'use_time': dlz.use_time}) # count use time
dlz.write({
'hd_case_id': hd_case_id,
})
2014-10-05 08:29:21 +00:00
for plan in obj.planes:
plan.write({
'hd_case_id': hd_case_id,
})
2014-10-04 15:51:54 +00:00
obj.write({"state":"treatment"})
2014-10-02 02:02:22 +00:00
return {
'next': {
'name': 'clinic_hd_case',
'mode': 'form',
'active_id': hd_case_id,
},
2014-10-02 19:12:52 +00:00
'flash': 'Visit %s has been confirmed'%obj.number,
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
2014-10-02 02:02:22 +00:00
def reopen(self,ids,context={}):
obj=self.browse(ids)[0]
2014-10-04 15:51:54 +00:00
obj.write({"state":"draft"})
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-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-05 08:29:21 +00:00
#print([(v.id, v.date_visit,v.cycle) for v in visits])
2014-10-03 07:27:57 +00:00
visit=visits[0]
2014-10-02 00:34:58 +00:00
data['doctor_id']=visit.doctor_id.id
data['department_id']=visit.department_id.id
2014-10-03 07:27:57 +00:00
cycle=int(visit.cycle)+1
data['cycle']=str(cycle)
if cycle>4:
data['cycle']='1'
2014-10-02 00:34:58 +00:00
else:
2014-10-03 06:09:10 +00:00
data['doctor_id']=None
2014-10-02 00:34:58 +00:00
data['department_id']=None
return data
2014-10-02 19:12:52 +00:00
def copy(self,ids,context={}):
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-10-03 07:27:57 +00:00
# XXX update cycle
visits=self.search_browse([['patient_id','=',obj.patient_id.id]],order="number desc")
if visits:
visit=visits[0] # order desc already
cycle=int(visit.cycle)+1
vals['cycle']=str(cycle)
if cycle>4:
vals['cycle']='1'
2014-10-02 19:12:52 +00:00
new_id=self.create(vals,context=context)
new_obj=self.browse(new_id)
return {
'next':{
'name': 'clinic_visit',
'mode': 'form',
'active_id': new_id,
},
'flash': 'Visit %s is copy to %s'%(obj.number,new_obj.number),
}
2014-10-04 15:51:54 +00:00
def delete(self,ids,context={}):
for obj in self.browse(ids):
if obj.state!='draft':
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)
cycle_list={'1': 'One','2': 'Two','3': 'Tree','4': 'Four'}
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 "",
'cycle': cycle_list.get(obj.cycle,""),
'print_date': time.strftime("%d/%m/%Y"),
}
return data
2014-08-19 11:36:46 +00:00
2014-10-04 18:33:18 +00:00
def onchange_dialyzer(self,context={}):
data=context["data"]
path=context["path"]
line=get_data_path(data,path,parent=True)
dialyzer_id=line.get("dialyzer_id")
if not dialyzer_id:
return {}
dialyzer=get_model("clinic.dialyzer").browse(dialyzer_id)
use_time=dialyzer.use_time or 0
use_time+=1
line["description"]=dialyzer.description or ""
line["use_time"]=use_time
line["max_use_time"]=dialyzer.max_use_time
line["member_type"]=dialyzer.member_type
line["dialyzer_type"]=dialyzer.dialyzer_type
line["bid_flow_rate"]=dialyzer.bid_flow_rate
line["ultrafittration"]=dialyzer.ultrafittration
line["state"]=dialyzer.state
return data
2014-10-05 05:47:19 +00:00
def onchange_timeuse(self,context={}):
data=context["data"]
time_start=data['time_start']
fmt_date="%Y-%m-%d %H:%M:%S"
time_start=datetime.datetime.strptime(time_start,fmt_date)
time_use=data['time_use']
seconds=(time_use or 1)*3600
time_stop=time_start+datetime.timedelta(seconds=seconds)
data['time_stop']=time_stop.strftime(fmt_date)
return data
2014-10-04 18:33:18 +00:00
2014-10-05 08:29:21 +00:00
def onchange_date_visit(self,context={}):
data=context["data"]
2014-10-05 08:36:58 +00:00
timenow=time.strftime("%H:%M:%S")
2014-10-05 08:29:21 +00:00
date_visit=data['date_visit']
fmt_date="%Y-%m-%d %H:%M:%S"
time_start=datetime.datetime.strptime("%s %s"%(date_visit,timenow),fmt_date)
2014-10-05 08:36:58 +00:00
print("time_start ", time_start)
2014-10-05 08:29:21 +00:00
time_use=data['time_use']
seconds=(time_use or 1)*3600
time_stop=time_start+datetime.timedelta(seconds=seconds)
2014-10-05 08:36:58 +00:00
data['time_start']=time_start.strftime(fmt_date)
2014-10-05 08:29:21 +00:00
data['time_stop']=time_stop.strftime(fmt_date)
return data
def onchange_dateplane(self,context={}):
data=context["data"]
path=context["path"]
line=get_data_path(data,path,parent=True)
start_date=line.get("date")
if not start_date:
return {}
timenow=time.strftime("%H:%M:%S")
timevisit=timenow
# XXX
timevisit=data['time_start'][11:20]
fmt_date="%Y-%m-%d %H:%M:%S"
fix_hrs=data['time_use'] or 0
time_start=datetime.datetime.strptime("%s %s"%(start_date,timevisit),fmt_date)
seconds=fix_hrs*3600
time_stop=time_start+datetime.timedelta(seconds=seconds)
line['time_start']=time_start.strftime(fmt_date)
line['time_stop']=time_stop.strftime(fmt_date)
if not line.get('state'):
line['state']='open'
return data
2014-08-19 11:36:46 +00:00
Visit.register()