from netforce.model import get_model from netforce import migration from netforce.utils import get_file_path from netforce.access import get_active_user, set_active_user, set_active_company, get_active_company from . import utils class Migration(migration.Migration): _name="import.clinic.hd.case" _version="2.10" def import_visit(self,lines): visits={} 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) hcode_impt='23869' if hcode_impt==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, 'state': 'pending', } # find cycle dttran=line.get("dttran") date=dttran[0:10] time=dttran[11:] if not time: continue 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 %s %s"%(dttran, time_start,time_stop)) visit_ids=get_model("clinic.visit").search([['visit_date','=',date],['patient_id','=',patient_id]]) visit_id=None if not visit_ids: vals['visit_date']=date visit_id=get_model('clinic.visit').create(vals) #print('create visit ', visit_id, date) else: visit_id=visit_ids[0] #print('already ', date, ' ', name) if visit_id: visits.update({ visit_id: { 'hct': line.get('hct'), } }) else: print("found ", name) return visits def confirm_visit(self,visits): st=get_model("clinic.setting").browse(1) st.write({ 'auto_gen': True, }) hd_case_ids=[] visit_ids=visits.keys() for visit in get_model('clinic.visit').browse(visit_ids): if visit!='confirmed': visit.confirm() hd_case=visit.hd_cases[0] # lines hd_case.write({ 'hct': visits[visit.id]['hct'] or 0, }) hd_case_ids.append(hd_case.id) st.write({ 'auto_gen': False, }) return hd_case_ids def hdc_done(self,hd_case_ids): done_ids=[] for hd_case in get_model("clinic.hd.case").browse(hd_case_ids): if hd_case.state=='waiting_treatment': hd_case.complete() done_ids.append(hd_case.id) return done_ids def migrate(self): fname='pks.xls' fpath=get_file_path(fname) lines=utils.read_excel(fpath,show_datetime=True) set_active_company(1) visits=self.import_visit(lines) print("visit ", len(visits)) hd_case_ids=self.confirm_visit(visits) print('hd case ', len(hd_case_ids)) hd_case_ids=self.hdc_done(hd_case_ids) print('Done ', len(hd_case_ids)) return True Migration.register()