clinic/netforce_clinic/models/import_visit.py

93 lines
3.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-02 07:08:20 +00:00
}
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
_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-02 07:08:20 +00:00
}
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)
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")
#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)
2014-12-03 04:48:20 +00:00
return {
'next': {
'name': 'import_clinic_visit',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'Create visit successully',
}
2014-12-02 07:08:20 +00:00
ImportVisit.register()