script to prevent duplicate

conv_bal
watcha.h@almacom.co.th 2015-04-28 10:29:15 +07:00
parent 2326aa85b7
commit d8fbab28d9
3 changed files with 68 additions and 8 deletions

View File

@ -252,7 +252,7 @@ class Patient(Model):
'dispose': False, 'dispose': False,
} }
_sql_constraints=("clinic_patient_key_uniq","unique(name_check,branch_id)","name should be unique"), _sql_constraints=("clinic_patient_key_uniq","unique(name_check)","name should be unique"),
_order="reg_date desc" _order="reg_date desc"
def check_idcard(self,card_type,idcard=''): def check_idcard(self,card_type,idcard=''):

View File

@ -183,6 +183,7 @@ class ClinicSetting(Model):
}) })
sts={} sts={}
print('='*50, 'patients duplicate') print('='*50, 'patients duplicate')
db=get_connection()
for pt, vals in pts.items(): for pt, vals in pts.items():
vals=sorted(vals,key=lambda x: x['date']) vals=sorted(vals,key=lambda x: x['date'])
count=len(vals) count=len(vals)
@ -190,14 +191,74 @@ class ClinicSetting(Model):
if count > 1: if count > 1:
id_lines=[] id_lines=[]
for val in vals: for val in vals:
print(val['id'], val['name'])
no+=1 no+=1
pt_id=val['id'] pt_id=val['id']
print(no, val['id'], val['name'], val['date'])
if no==len(vals): if no==len(vals):
# hdcase for del_pt_id in id_lines:
# visit visit_ids=[]
# dialyzer hdcase_ids=[]
print('---> ', pt_id, id_lines) print('del_pt_id ', del_pt_id)
for hdcase in get_model('clinic.hd.case').search_browse([['patient_id','=',del_pt_id]]):
visit_ids.append(hdcase.visit_id.id)
hdcase_ids.append(hdcase.id)
print('hdcase_ids ', hdcase_ids, 'visit_ids ', visit_ids)
if hdcase_ids:
print('>>>>>>>> pt_id ', pt_id)
db.execute('update clinic_hd_case set patient_id=%s where id in %s', pt_id,tuple(hdcase_ids))
db.execute('update clinic_visit set patient_id=%s where id in %s', pt_id,tuple(visit_ids))
db.execute('update clinic_dialyzer set patient_id=%s where patient_id = %s', pt_id,del_pt_id)
#for hdcase in get_model("clinic.hd.case").browse(hdcase_ids):
#hdcase.write({
#'patient_id': pt_id,
#})
#for vs in get_model("clinic.visit").browse(visit_ids):
#vs.write({
#'patient_id': pt_id,
#})
#for dlz in get_model("clinic.dialyzer").search_browse([['patient_id','=',del_pt_id]]):
#dlz.write({
#'patient_id': pt_id,
#})
for ap in get_model("clinic.setting.account.patient").search_browse([['patient_id','=',del_pt_id]]):
ap.write({
'patient_id': pt_id,
})
for hdce in get_model("clinic.hd.case.expense").search_browse([['patient_id','=',del_pt_id]]):
hdce.write({
'patient_id': pt_id,
})
#get_model('clinic.patient').delete([del_pt_id])
for pt in get_model('clinic.patient').browse([del_pt_id]):
pt.write({
'first_name': '__%s'% pt.first_name or "",
'active': False,
})
else:
print('patient zero delete ', val)
for vs in get_model("clinic.visit").search_browse([['patient_id','=',del_pt_id]]):
vs.write({
'patient_id': pt_id,
})
for dlz in get_model("clinic.dialyzer").search_browse([['patient_id','=',del_pt_id]]):
dlz.write({
'patient_id': pt_id,
})
for ap in get_model("clinic.setting.account.patient").search_browse([['patient_id','=',del_pt_id]]):
ap.write({
'patient_id': pt_id,
})
for hdce in get_model("clinic.hd.case.expense").search_browse([['patient_id','=',del_pt_id]]):
hdce.write({
'patient_id': pt_id,
})
#get_model('clinic.patient').delete([del_pt_id])
for pt in get_model('clinic.patient').browse([del_pt_id]):
pt.write({
'first_name': '__%s'% pt.first_name or "",
'active': False,
})
print('---> ', pt_id, id_lines, val['date'])
else: else:
id_lines.append(pt_id) id_lines.append(pt_id)

View File

@ -250,8 +250,7 @@ class Staff(Model):
'location': _get_default_location, 'location': _get_default_location,
} }
#_key=["name_check","branch_id"] #not working _sql_constraints=("clinic_staff_key_uniq","unique(name_check)","name should be unique"),
_sql_constraints=("clinic_staff_key_uniq","unique(name_check,branch_id)","name should be unique"),
_order="date desc,number desc" _order="date desc,number desc"
def create_contact(self,code,name): def create_contact(self,code,name):