clinic/netforce_clinic/models/import_visit.py

83 lines
3.1 KiB
Python

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"),
'hcode': fields.Char("Hospital Code"),
}
def get_hcode(self,context={}):
settings=get_model("settings").browse(1)
hcode=settings.hospital_code or ""
return hcode
_defaults={
'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"),
'hcode': get_hcode,
}
def import_visit(self,ids,context={}):
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")
for line in lines:
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'])]
hcode=line.get('hcode18')
if not hcode:
hcode='0'
hcode=int(hcode)
hcode=str(hcode)
if obj.hcode==hcode:
name=line.get("name14")
#hn=line.get('hn')
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)
ImportVisit.register()