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_id': fields.Many2One("clinic.hospital", "Hospital",required=True), '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"), } def get_hcode(self,context={}): settings=get_model("settings").browse(1) hcode=settings.hospital_code or "" return hcode 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 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 _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"), 'hcode_id': get_hcode_id, 'patient_type_id': _get_patient_type, } def import_visit(self,ids,context={}): obj=self.browse(ids)[0] ptype=obj.patient_type_id res={} if ptype.code=='S': res=self.import_visit_pks(ids,context) elif ptype.code=='U': 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_no','department_id','doctor_id'] for pt in get_model("clinic.patient").search_read([[]],field_name): hn_no=pt['hn_no'] 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_no]={ '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_no=line.get('hn') hn=get_hn(hn_no) date,time=line.get("dttran").split("T") patient=patients.get(hn) if not patient: fail_qty+=1 msg+='%s,%s,%s\n'%(hn_no,'','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_no,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={}): 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") 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'])] for line in lines: hcode=line.get('hcode18') if not hcode: hcode='0' hcode=int(hcode) hcode=str(hcode) if obj.hcode_id.code==hcode: 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) return { 'next': { 'name': 'import_clinic_visit', 'mode': 'form', 'active_id': obj.id, }, 'flash': 'Import successully', } ImportVisit.register()