clinic/netforce_clinic/models/visit.py

429 lines
14 KiB
Python

import time
from datetime import datetime, timedelta
from netforce.model import Model, fields, get_model
from netforce.access import get_active_company, get_active_user, set_active_user
FMT_DATE="%Y-%m-%d %H:%M:%S"
class Visit(Model):
_name="clinic.visit"
_string="Visit"
_audit_log=True
_name_field="number"
_multi_company=True
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
def _get_color(self,ids,context={}):
res={}
for obj in self.browse(ids):
color=get_model("clinic.cycle").browse(obj.cycle_id.id).color
res[obj.id]=color
return res
_fields={
"number": fields.Char("Number",required=True,search=True),
'sickbed_id': fields.Many2One("clinic.sickbed","Sickbed"),
"time_start": fields.DateTime("Start Time",required=True),
"time_stop": fields.DateTime("End Time",required=True),
"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),
"department_id": fields.Many2One("clinic.department", "Department",search=True),
"comments": fields.One2Many("message","related_id","Comments"),
"company_id": fields.Many2One("company","Company"),
'hd_cases': fields.One2Many('clinic.hd.case','visit_id',"HD Cases",readonly=True),
"cycle_id": fields.Many2One("clinic.cycle","Cycle"),
'cycle_color': fields.Char('Color',function="_get_color"),
"state": fields.Selection([["draft","Draft"],['pending','Pending'],["confirmed","Confirmed"],["cancelled","Cancelled"]],"Status",required=True),
"comments": fields.One2Many("message","related_id","Comments"),
'visit_date': fields.Date('Visit Date'),
'print_date': fields.Date('Print Date',function="_get_print_date"),
'cycle_item_id': fields.Many2One("clinic.cycle.item","Cycle Item"), #XXX on_delete="cascade" -> rm visit from cycle item
'sequence': fields.Char("Sequence"),
'note': fields.Text('Note'),
}
def _get_number(self,context={}):
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)
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)
def _get_nurse(self,context={}):
user_id=get_active_user()
nurse_ids=get_model("clinic.staff").search([['user_id','=',user_id],['type','=','nurse']])
if nurse_ids:
return nurse_ids[0]
return None
def _get_time_start(self,context={}):
now=datetime.now()
starttime=now.strftime("%Y-%m-%d %H:%M:%S")
return starttime
def _get_time_stop(self,context={}):
hr=timedelta(seconds=3600)
now=datetime.now()
stoptime=(now+hr).strftime("%Y-%m-%d %H:%M:%S")
return stoptime
_defaults={
"state": "draft",
'time_start': _get_time_start,
'time_stop': _get_time_stop,
'visit_date': lambda *a: time.strftime("%Y-%m-%d"),
"number": "/",
"company_id": lambda *a: get_active_company(),
'nurse_id': _get_nurse,
}
_order="sequence"
def new_dlz(self,patient_id):
if not patient_id:
raise Exception("No Patient to creat Dialyzer")
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
dlz_id=get_model('clinic.dialyzer').create(dlz_vals)
dialyzer=get_model("clinic.dialyzer").browse(dlz_id)
dialyzer.confirm()
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:
dlz_id=self.new_dlz(patient.id)
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
dlz_id=self.new_dlz(patient.id)
dialyzer=get_model("clinic.dialyzer").browse(dlz_id)
#raise Exception("%s is expired"%dialyzer.number)
use_time+=1
vals={
"description": dialyzer.description or dialyzer.product_id.name or "",
"use_time": use_time,
"max_use_time": dialyzer.max_use_time,
"member_type": dialyzer.member_type,
"dialyzer_type": dialyzer.dialyzer_type,
"bid_flow_rate": dialyzer.bid_flow_rate,
"ultrafittration": dialyzer.ultrafittration,
"state": dialyzer.state,
'dialyzer_id': dialyzer.id,
}
return vals
def confirm(self,ids,context={}):
obj=self.browse(ids)[0]
number=obj.number
if number=="/" or not number:
number=self._get_number(context)
vals={
'number': number,
'state': 'confirmed',
}
user_id=get_active_user()
staff=get_model("clinic.staff").search_browse([['user_id','=',user_id]])
if staff:
vals['nurse_id']=staff[0].id
obj.write(vals)
hd_case_obj=get_model("clinic.hd.case")
vals={
'patient_id': obj.patient_id.id,
'nurse_id': obj.nurse_id.id,
'department_id': obj.department_id.id,
'sickbed_id': obj.sickbed_id.id,
'time_start': obj.time_start,
'time_stop': obj.time_stop,
'cycle_id' : obj.cycle_id.id,
'visit_id': obj.id,
'lines':[],
'dialyzers': [],
'staffs': [],
'state': 'waiting_treatment',
}
st=get_model("clinic.setting").browse(1)
if st.find_dlz:
dialyzer=self.get_dlz(obj.id)
vals['dialyzers'].append(('create',dialyzer))
# 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)
if context.get("called"): #XXX call outside
return hd_case_id
date=vals['time_start'][0:10]
cycle=obj.cycle_id
dom=[]
dom.append(['date','=',date])
dom.append(['cycle_id','=',cycle.id])
item_obj=get_model('clinic.cycle.item')
item_ids=item_obj.search(dom)
def get_schedule(date):
dom=[]
dom.append(['date','=',date])
schedule=None
schedules=get_model("clinic.schedule").search_browse(dom)
if schedules:
schedule=schedules[0]
return schedule
item_vals={
'cycle_id': cycle.id,
'date': date,
'nurses': [],
}
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)
if item.state!='draft':
for nurse in item.nurses:
nurse.delete()
item=item_obj.browse(item_id)
if item.state!='draft':
schedule=get_schedule(date)
if schedule:
for line in schedule.lines:
if line.cycle_id.id==cycle.id:
nurse=line.nurse_id
item_vals['nurses'].append(('create',{
'nurse_id': nurse.id,
'level_id': nurse.level_id.id,
}))
item.write(item_vals)
obj.write({
'cycle_item_id': item_id,
})
hd_case=hd_case_obj.browse(hd_case_id)
hd_case.write({
'cycle_item_id': item_id,
})
return {
'next': {
'name': 'clinic_hd_case',
'mode': 'form',
'active_id': hd_case_id,
},
'flash': 'Visit has been confirmed',
}
def discard(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({"state":"cancelled"})
def reopen(self,ids,context={ }):
obj=self.browse(ids)[0]
obj.write({"state":"draf t"})
def onchange_patient(self,context={}):
data=context['data']
patient_id=data['patient_id']
patient=get_model("clinic.patient").browse(patient_id)
visits=self.search_browse([['patient_id','=',patient_id]],order="number desc")
if visits:
visit=visits[0]
doctor=visit.doctor_id
department=visit.department_id
if not department:
department=patient.department_id
data['doctor_id']=doctor.id
data['department_id']=department.id
if not patient.doctor_id:
patient.write({
'doctor_id': doctor.id,
})
else:
doctor=patient.doctor_id
department=patient.department_id
data['doctor_id']=doctor.id
data['department_id']=department.id
return data
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,
'cycle_id': obj.cycle_id.id,
'time_start': obj.time_start,
'time_stop': obj.time_stop,
'visit_date': obj.visit_date,
}
new_id=self.create(vals,context=context)
return {
'next':{
'name': 'clinic_visit',
'mode': 'form',
'active_id': new_id,
},
'flash': 'Visit %s successfully'%(obj.number),
}
def delete(self,ids,context={}):
for obj in self.browse(ids):
if obj.state not in ('draft','pending'):
raise Exception("Can not delete visit %s because state is not draft!"%obj.number)
super().delete(ids,context=context)
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,
'date_visit': obj.date_visit,
'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"),
'cycle_name': obj.cycle_id.name or "",
}
return data
def onchange_cycle(self,context={}):
data=context['data']
cycle_id=data['cycle_id']
date=data['visit_date']
if not date:
date=data['time_start'][0:10]
if cycle_id:
cycle=get_model('clinic.cycle').browse(cycle_id)
data['time_start']=date+' %s:00'%cycle.time_start
data['time_stop']=date+' %s:00'%cycle.time_stop
return data
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
def to_draft(self,ids,context={}):
obj=self.browse(ids)[0]
for hd_case in obj.hd_cases:
hd_case.to_draft()
for line in hd_case.lines:
line.delete()
for ps in hd_case.staffs:
ps.delete()
#hd_case.delete()
obj.write({
'state': 'draft',
})
def gen_no(self,ids,context={}):
obj=self.browse(ids)[0]
if obj.number != '/' or obj.state!='draft':
return
number=self._get_number(context)
obj.write({
'number': number,
})
return {
'next': {
'name': 'clinic_visit',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'Generate number succesfully',
}
def create(self, vals,**kw):
cycle_id=vals['cycle_id']
cycle=get_model("clinic.cycle").browse(cycle_id)
vals['sequence']='%s-%s'%(vals['time_start'][0:10],cycle.sequence) #date-sequence
vals['visit_date']=vals['time_start'][0:10]
new_id=super().create(vals,**kw)
return new_id
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
}
def write(self,ids,vals,**kw):
obj=self.browse(ids)[0]
cycle=obj.cycle_id
vals['sequence']='%s-%s'%(obj.time_start[0:10],cycle.sequence) #date-sequence
vals['visit_date']=obj.time_start[0:10]
super().write(ids,vals,**kw)
def pending(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'pending',
})
Visit.register()