import csv import xlrd from netforce.model import get_model from netforce import migration from netforce.access import set_active_user, set_active_company from netforce.database import get_connection class Migration(migration.Migration): _name="clinic.reimport.10to15" _version="2.12.4" def migrate(self): set_active_company(1) set_active_user(1) #find hdcase => date, patient => fill up => hct , erythopoie => validate #///////////////// SAMSEN ////////////////////// #path="/home/watcha/Desktop/RD-10-15.csv" for fname in ["RD-10-15.csv","LS-10-15.csv"]: #for fname in ["RD-10-15.csv"]: path="/tmp/"+fname field_names=[] nolines=[] print('RUN.%s ... '%fname) with open(path) as csvfile: reader = csv.DictReader(csvfile) field_names=reader.fieldnames count=0 found=0 no=0 for row in reader: count+=1 name=row['name'] hn=row['hn'] pid=row['pid'] cond=[ ['or',[ ['name','ilike',name], #['hn','ilike',hn], #['card_no','ilike',pid], ], ], ] pids=get_model("clinic.patient").search(cond) if pids: patient_id=pids[0] dpt=row['dpt'] dpt_id=None if dpt=='LS01': dpt_id=1 elif dpt=='LS02': dpt_id=2 elif dpt=='LS03': dpt_id=3 elif dpt=='RD02': dpt_id=4 elif dpt=='RD03': dpt_id=5 # สามเสนช้น 4 cycle_id=None cycle=row['cycle'] if '1' in cycle: cycle_id=1 elif '2' in cycle: cycle_id=2 elif '3' in cycle: cycle_id=3 elif '4' in cycle: cycle_id=4 cycle=get_model("clinic.cycle").browse(cycle_id) m,d,y=row['datehd'].split("/") #mm/dd/yyyy date="-".join([y.zfill(2), m.zfill(2), d.zfill(2)]) cond=[ ['date','=', date], ['department_id','=',dpt_id], #['cycle_id','=',cycle_id], # maybe change the cycle ['patient_id','=',pids[0]], ] #print('cond >>> ', cond) res=get_model("clinic.hd.case").search_read(cond,['cycle_id']) if res: if len(res)>1: pass #continue hdcase_id=res[0]['id'] hdcase=get_model("clinic.hd.case").browse(hdcase_id) vals={'hct': row['hct']} for line in hdcase.lines: categ=line.product_categ_id if categ: #36 | Fee #43 | Service #34 | Erythropoietin if categ.id==43: line.write({ 'qty': row['qty'], 'price': row['inject_price'], }) if categ.id==34: prods=get_model("product").search_read([['description','ilike',row['epo_nf']]],['name']) if prods: product_id=prods[0]['id'] desc=prods[0]['name'] or "" line.write({ 'description': desc, 'product_id': product_id, 'qty': row['qty'], 'price': row['product_price'], }) if cycle_id!=res[0]['cycle_id'][0]: print("update cycle hdcase.%s "%(hdcase.number)) hdcase.visit_id.write({ 'cycle_id': cycle_id, }) vals.update({ 'cycle_id': cycle_id, }) if vals and hdcase.state not in ("waiting_payment","paid"): hdcase.write(vals) #req_fee : 1=> topay else claim expense if not hdcase.req_fee: hdcase.make_invoices() hdcase.post_invoices() hdcase.write({ 'state': 'waiting_payment' }) else: hdcase.make_payment() hdcase.write({ 'state': 'paid' }) found+=1 else: no+=1 # create new visit -> hdcase patient=get_model('clinic.patient').browse(patient_id) dpt=get_model("clinic.department").browse(dpt_id) visit_vals={ 'patient_id': patient.id, 'department_id': dpt.id, 'cycle_id': cycle_id, 'doctor_id': patient.doctor_id.id, 'branch_id': dpt.branch_id.id, 'time_start': '%s %s:00'%(date,cycle.time_start), 'time_stop': '%s %s:00'%(date,cycle.time_stop), 'visit_date': date, 'state': 'pending', } context={ 'no_dlz': True, 'random_sickbed': True } new_id=get_model('clinic.visit').create(visit_vals,context=context) visit=get_model('clinic.visit').browse(new_id) visit.confirm(context) for hdcase in visit.hd_cases: for line in hdcase.lines: categ=line.product_categ_id if categ: #36 | Fee #43 | Service #34 | Erythropoietin if categ.id==43: line.write({ 'qty': row['qty'], 'price': row['inject_price'], }) if categ.id==34: prods=get_model("product").search_read([['description','ilike',row['epo_nf']]],['name']) if prods: product_id=prods[0]['id'] desc=prods[0]['name'] or "" line.write({ 'product_id': product_id, 'description': desc, 'qty': row['qty'], 'price': row['product_price'], }) if not hdcase.req_fee: hdcase.make_invoices() hdcase.post_invoices() hdcase.write({ 'state': 'waiting_payment' }) else: hdcase.make_payment() hdcase.write({ 'state': 'paid' }) nolines.append(row) else: no+=1 #XXX if field_names and nolines: fname="no-%s"%(fname) path='/tmp/%s'%(fname) with open(path, 'w') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=field_names) writer.writeheader() for noline in nolines: writer.writerow(noline) print("Missing %s please checkout %s"%(len(nolines), path)) print(count, found, no) #///////////////// LAKSI ////////////////////// Migration.register()