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.pks" _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'), 'epoadm29':line.get("epoadm29"), # SRV 'amount23':line.get("amount23"), # FEE 'allow37':line.get("allow37"), # EPO } }) 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': hd_case_id=visit.confirm()['next']['active_id'] hd_case=get_model("clinic.hd.case").browse(hd_case_id) # lines vals={ 'hct': visits[visit.id]['hct'] or 0, 'lines': [], } for st_prod in st.products: if st_prod.patient_type_id.id==3: prod=st_prod.product_id price=st_prod.price qty=st_prod.qty categ=st_prod.product_categ_id if categ.code=='FEE': amt=visits[visit.id]['amount23'] or 0 elif categ.code=='SRV': amt=visits[visit.id]['epoadm29'] or 0 elif categ.code=='EPO': amt=visits[visit.id]['allow37'] or 0 else: amt=0 vals['lines'].append(('create',{ 'product_id': prod.id, 'uom_id': st_prod.uom_id.id, 'product_categ_id': categ.id, 'description': st_prod.description, 'price': price, 'qty': qty, 'reimbursable': st_prod.reimbursable, 'amount': amt, })) hd_case.write(vals) 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)) set_active_company(1) return True Migration.register()