clinic/netforce_clinic/models/import_visit.py

208 lines
7.4 KiB
Python
Raw Normal View History

2014-12-02 07:08:20 +00:00
import time
from netforce.model import Model, fields, get_model
from netforce.utils import get_file_path
from . import utils
class ImportVisit(Model):
_name="clinic.import.visit"
_transient=True
_fields={
'date': fields.DateTime("Date"),
'file': fields.File("File"),
'result': fields.Text("Success"),
2014-12-03 04:48:20 +00:00
'hcode_id': fields.Many2One("clinic.hospital", "Hospital",required=True),
2014-12-03 17:46:50 +00:00
'patient_type_id': fields.Many2One('clinic.patient.type','Type',required=True),
'msg': fields.Text("Message"),
'done_qty': fields.Integer("Success"),
'fail_qty': fields.Integer("Fail"),
2014-12-02 07:08:20 +00:00
}
2014-12-03 17:46:50 +00:00
def get_hcode(self,context={}):
settings=get_model("settings").browse(1)
hcode=settings.hospital_code or ""
return hcode
2014-12-03 04:48:20 +00:00
def get_hcode_id(self,context={}):
hp_ids=get_model("clinic.hospital").search([])
hp_id=None
if hp_ids:
hp_id=hp_ids[0]
return hp_id
2014-12-02 07:08:20 +00:00
2014-12-03 17:46:50 +00:00
def _get_patient_type(self,context={}):
st=get_model('clinic.setting').browse(1)
ptype=st.patient_type_id
ptype_id=None
if ptype:
ptype_id=ptype.id
return ptype_id
2014-12-02 07:08:20 +00:00
_defaults={
'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"),
2014-12-03 04:48:20 +00:00
'hcode_id': get_hcode_id,
2014-12-03 17:46:50 +00:00
'patient_type_id': _get_patient_type,
2014-12-02 07:08:20 +00:00
}
2014-12-03 17:46:50 +00:00
2014-12-02 07:08:20 +00:00
def import_visit(self,ids,context={}):
2014-12-03 17:46:50 +00:00
obj=self.browse(ids)[0]
ptype=obj.patient_type_id
res={}
2014-12-16 09:53:36 +00:00
if ptype.code=='S':
2014-12-03 17:46:50 +00:00
res=self.import_visit_pks(ids,context)
2014-12-16 09:53:36 +00:00
elif ptype.code=='U':
2014-12-03 17:46:50 +00:00
res=self.import_visit_uc(ids,context)
else:
raise Exception('No script to import visit with type %s'%ptype.name)
return res
def import_visit_uc(self,ids,context):
obj=self.browse(ids)[0]
#ptype=obj.patient_type_id
fname=obj.file
fpath=get_file_path(fname)
lines=utils.read_xml(fpath,node='HDBills')
if not lines:
raise Exception("Wrong File")
cycles=[(c['id'],'%s:00'%c['time_start'],'%s:00'%c['time_stop']) for c in get_model("clinic.cycle").search_read([[]],['time_start','time_stop'])]
patients={}
field_name=['name','hn','hn_num','department_id','doctor_id']
for pt in get_model("clinic.patient").search_read([[]],field_name):
hn_num=pt['hn_num']
dp=pt['department_id']
department_id=None
if dp:
department_id=dp[0]
doctor=pt['doctor_id']
doctor_id=None
if doctor:
doctor_id=doctor[0]
patients[hn_num]={
'id': pt['id'],
'hn': pt['hn'],
'department_id': department_id,
'doctor_id': doctor_id,
'name': pt['name'],
}
done_qty=0
fail_qty=0
msg='hn,name,note\n'
def get_hn(hn=""):
return ''.join(h for h in hn if h.isdigit())
for line in lines:
hn_num=line.get('hn')
hn=get_hn(hn_num)
date,time=line.get("dttran").split("T")
patient=patients.get(hn)
if not patient:
fail_qty+=1
msg+='%s,%s,%s\n'%(hn_num,'','Not found hn')
continue
vals={
'patient_id': patient['id'],
'doctor_id': patient['doctor_id'],
'department_id': patient['department_id'],
}
cycle_id=None
for cycle in cycles:
time_start=cycle[1]
time_stop=cycle[2]
if time >= time_start:
cycle_id=cycle[0]
vals['cycle_id']=cycle_id
vals['time_start']='%s %s'%(date,time_start)
vals['time_stop']='%s %s'%(date,time_stop)
if not cycle_id:
raise Exception("not found cycle on this time %s"%(time))
vals['visit_date']=date
dom=[]
dom.append(['visit_date','=',date])
dom.append(['patient_id','=',patient['id']])
visit_ids=get_model("clinic.visit").search(dom)
if not visit_ids:
visit_id=get_model('clinic.visit').create(vals)
done_qty+=1
msg+='%s,%s,%s\n'%(hn_num,patient['name'],'Create visit %s'%date)
print('create visit %s'%visit_id)
obj.write({
'done_qty': done_qty,
'fail_qty': fail_qty,
'msg': msg,
})
return {
'next': {
'name': 'import_clinic_visit',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'Import successfully',
}
def import_visit_pks(self,ids,context={}):
2014-12-02 07:08:20 +00:00
obj=self.browse(ids)[0]
fname=obj.file
fpath=get_file_path(fname)
lines=utils.read_excel(fpath,show_datetime=True)
if not lines:
raise Exception("Wrong File")
2014-12-03 17:46:50 +00:00
cycles=[(c['id'],'%s:00'%c['time_start'],'%s:00'%c['time_stop']) for c in get_model("clinic.cycle").search_read([[]],['time_start','time_stop'])]
2014-12-02 07:08:20 +00:00
for line in lines:
hcode=line.get('hcode18')
if not hcode:
hcode='0'
hcode=int(hcode)
hcode=str(hcode)
2014-12-03 04:48:20 +00:00
if obj.hcode_id.code==hcode:
2014-12-02 07:08:20 +00:00
name=line.get("name14")
patient_ids=get_model("clinic.patient").search([['name','=',name]])
if patient_ids:
patient_id=patient_ids[0]
patient=get_model("clinic.patient").browse(patient_id)
doctor=patient.doctor_id
department=patient.department_id
vals={
'patient_id': patient.id,
'doctor_id': doctor.id,
'department_id': department.id,
}
# find cycle
dttran=line.get("dttran")
date=dttran[0:10]
time=dttran[11:]
cycle_id=None
for cycle in cycles:
time_start=cycle[1]
time_stop=cycle[2]
if time >= time_start:
cycle_id=cycle[0]
vals['cycle_id']=cycle_id
vals['time_start']='%s %s'%(date,time_start)
vals['time_stop']='%s %s'%(date,time_stop)
if not cycle_id:
raise Exception("not found cycle on this time %s"%(dttran))
vals['visit_date']=date
visit_ids=get_model("clinic.visit").search([['visit_date','=',date],['patient_id','=',patient_id]])
if not visit_ids:
visit_id=get_model('clinic.visit').create(vals)
print('create visit ', visit_id, date)
else:
print('already ', date, ' ', name)
else:
print("found ", name)
2014-12-03 04:48:20 +00:00
return {
'next': {
'name': 'import_clinic_visit',
'mode': 'form',
'active_id': obj.id,
},
2014-12-03 17:46:50 +00:00
'flash': 'Import successully',
2014-12-03 04:48:20 +00:00
}
2014-12-02 07:08:20 +00:00
ImportVisit.register()