script update patient cycle

conv_bal
watcha.h 2015-03-06 15:46:53 +07:00
parent 0c0c79a00b
commit 86e6f77724
2 changed files with 135 additions and 9 deletions

View File

@ -1 +1,3 @@
dom=[['patient_id.field_bool','=',True]] => not working
pattern id card: 3 3013 00180 19 0

View File

@ -132,17 +132,141 @@ class ClinicSetting(Model):
})
def run_script(self,ids,context={}):
user_id=get_active_user()
if user_id !=1:
print("Only admin!!")
return
obj=self.browse(ids)[0]
#obj.update_staff_department()
#obj.remove_rotation()
obj.update_staff_level()
#obj.update_cycle_item_level()
files=['SS3-1.csv','SS4-1.csv','SS4.csv']
self.update_pcycle(files)
print("Done!")
def update_pcycle(self,files=[]):
import csv
for _file in files:
f=open("/tmp/%s"%_file,"r")
rd=csv.reader(f)
row1=next(rd)
headers=[]
for header in row1:
header='_'.join([x.lower() for x in header.split()])
headers.append(header)
lines=[]
for row in rd:
vals=dict(zip(headers,row))
lines.append(vals)
def get_dicts(model_name,dom=[]):
res={}
if not model_name:
return res
for sr in get_model(model_name).search_read(dom,['name']):
res[sr['name']]=sr['id']
return res
vasculars=get_dicts('clinic.vascular.access')
cycles=get_dicts('clinic.cycle')
titles=get_dicts('clinic.name.title')
default_title=None
for tname, tid in titles.items():
if tname=='No Title':
default_title=tid
break
types=get_dicts('clinic.patient.type')
dpts=get_dicts('clinic.department')
doctors=get_dicts(model_name='clinic.staff',dom=['type','=','doctor'])
days={
'Monday': 'mon',
'Tuesday': 'tue',
'Wednesday': 'wed',
'Thursday': 'thu',
'Friday': 'fri',
'Saturday': 'sat',
'Sunday': 'sun',
}
datas={}
olds=set({})
dbid2=0
dpt_id=None
for line in lines:
dbid=line['database_id']
if dbid not in olds:
dbid=line['database_id']
dbid2=dbid
else:
dbid=dbid2
olds.update({dbid})
fname=line['first_name']
lname=line['last_name']
dname=line['doctor']
cycle=line['cycles/cycle']
cycle=cycles.get(cycle,None)
day=line['cycles/day']
day=days.get(day)
dpt=line['department']
dpt=dpts.get(dpt,None)
if not dpt:
dpt=dpt_id
else:
dpt_id=dpt
bday=line['birthday'] or None
title=line['title']
title=titles.get(title,default_title) or None
idcard=line['id_card']
trt=line['trt']
type=line['type']
type=types.get(type,None)
valc=line['vascular_ac.']
valc=vasculars.get(valc)
hn=line['hn_number']
if not datas.get(dbid):
datas[dbid]={
'card_no': (idcard or "").replace(" ",""),
'number': hn,
'type_id': type,
'first_name': fname,
'last_name': lname,
'birthday': bday,
'title_id': title,
'trt_no': trt,
'doctor_id': doctors.get(dname),
'vascular_acc': valc,
'cycles': [],
}
if dbid and cycle and day:
datas[dbid]['cycles'].append({'department_id': dpt, 'cycle_id': cycle, 'day': day})
def del_pcycle(patient_id):
ids=get_model("clinic.patient.cycle").search([['patient_id','=',patient_id]])
get_model("clinic.patient.cycle").delete(ids)
print("pcycle del ", ids)
def create_pcycle(all_vals):
new_ids=[]
for vals in all_vals:
try:
id=get_model("clinic.patient.cycle").create(vals)
except Exception as e:
print("ERROR : ", e)
new_ids.append(id)
print("pcycle create ", new_ids)
print(" update patient data")
for patient_id, vals in datas.items():
if not patient_id:
continue
try:
m,d,y=(vals['birthday'] or "").split("/")
vals['birthday']='%s-%s-%s'%(y,m.zfill(2),d.zfill(2))
except:
pass
if not vals['card_no']:
vals['card_no']='/'
pvals=vals.copy()
del pvals['cycles']
get_model("clinic.patient").browse(int(patient_id)).write(pvals)
del_pcycle(patient_id)
all_vals=[]
for cycle_vals in vals['cycles']:
cycle_vals.update({'patient_id': patient_id})
all_vals.append(cycle_vals)
create_pcycle(all_vals)
print("Done!")
def update_staff_department(self,ids,context={}):
user_id=get_active_user()
if user_id !=1: