2014-12-02 07:08:20 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
from netforce.model import Model, fields, get_model
|
|
|
|
from netforce.utils import get_file_path
|
|
|
|
from . import utils
|
|
|
|
|
|
|
|
class ImportPatient(Model):
|
|
|
|
_name="clinic.import.patient"
|
|
|
|
_transient=True
|
|
|
|
|
|
|
|
_fields={
|
|
|
|
'date': fields.DateTime("Date"),
|
|
|
|
'file': fields.File("File"),
|
|
|
|
'result': fields.Text("Success"),
|
2014-12-03 11:40:37 +00:00
|
|
|
'hcode_id': fields.Many2One("clinic.hospital", "Hospital",required=True),
|
|
|
|
'patient_type_id': fields.Many2One('clinic.patient.type','Type',required=True),
|
|
|
|
'msg': fields.Text("Message"),
|
|
|
|
'done_qty': fields.Integer("Success"),
|
|
|
|
'fail_qty': fields.Integer("Fail"),
|
2014-12-02 07:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def get_hcode(self,context={}):
|
|
|
|
settings=get_model("settings").browse(1)
|
|
|
|
hcode=settings.hospital_code or ""
|
|
|
|
return hcode
|
|
|
|
|
2014-12-03 11:40:37 +00:00
|
|
|
def get_hcode_id(self,context={}):
|
|
|
|
hp_ids=get_model("clinic.hospital").search([])
|
|
|
|
hp_id=None
|
|
|
|
if hp_ids:
|
|
|
|
hp_id=hp_ids[0]
|
|
|
|
return hp_id
|
|
|
|
|
|
|
|
def _get_patient_type(self,context={}):
|
|
|
|
st=get_model('clinic.setting').browse(1)
|
|
|
|
ptype=st.patient_type_id
|
|
|
|
ptype_id=None
|
|
|
|
if ptype:
|
|
|
|
ptype_id=ptype.id
|
|
|
|
return ptype_id
|
|
|
|
|
2014-12-02 07:08:20 +00:00
|
|
|
_defaults={
|
|
|
|
'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"),
|
2014-12-03 11:40:37 +00:00
|
|
|
'hcode_id': get_hcode_id,
|
|
|
|
'patient_type_id': _get_patient_type,
|
2014-12-02 07:08:20 +00:00
|
|
|
}
|
2014-12-03 11:40:37 +00:00
|
|
|
|
2014-12-02 07:08:20 +00:00
|
|
|
def import_patient(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
2014-12-03 11:40:37 +00:00
|
|
|
ptype=obj.patient_type_id
|
|
|
|
res={}
|
2014-12-16 09:53:36 +00:00
|
|
|
if ptype.code=='S':
|
2014-12-03 11:40:37 +00:00
|
|
|
res=self.import_patient_pks(ids,context)
|
2014-12-16 09:53:36 +00:00
|
|
|
elif ptype.code=='U':
|
2014-12-03 11:40:37 +00:00
|
|
|
res=self.import_patient_uc(ids,context)
|
|
|
|
else:
|
|
|
|
raise Exception('No script to import patient with type %s'%ptype.name)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
def import_patient_uc(self,ids,context):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
#ptype=obj.patient_type_id
|
|
|
|
fname=obj.file
|
|
|
|
fpath=get_file_path(fname)
|
|
|
|
lines=utils.read_xml(fpath,node='HDBills')
|
|
|
|
if not lines:
|
|
|
|
raise Exception("Wrong File")
|
|
|
|
for line in lines:
|
|
|
|
print(line)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'import_clinic_patient',
|
|
|
|
'mode': 'form',
|
|
|
|
'active_id': obj.id,
|
|
|
|
},
|
|
|
|
'flash': 'Import successfully',
|
|
|
|
}
|
|
|
|
|
|
|
|
def import_patient_pks(self,ids,context):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
ptype=obj.patient_type_id
|
2014-12-02 07:08:20 +00:00
|
|
|
fname=obj.file
|
|
|
|
fpath=get_file_path(fname)
|
|
|
|
lines=utils.read_excel(fpath,show_datetime=True)
|
|
|
|
if not lines:
|
|
|
|
raise Exception("Wrong File")
|
2014-12-03 11:40:37 +00:00
|
|
|
msg=""
|
|
|
|
msg+="hcode,hn,name,note\n"
|
|
|
|
done_qty=0
|
|
|
|
fail_qty=0
|
2014-12-02 07:08:20 +00:00
|
|
|
for line in lines:
|
|
|
|
hcode=line.get('hcode18','0')
|
|
|
|
if not hcode:
|
|
|
|
hcode='0'
|
|
|
|
hcode=int(hcode)
|
|
|
|
hcode=str(hcode)
|
2014-12-03 11:40:37 +00:00
|
|
|
hn=line.get('hn',"")
|
|
|
|
patient_name=line.get("name14")
|
|
|
|
if obj.hcode_id.code==hcode:
|
|
|
|
patient_ids=get_model("clinic.patient").search([['name','=',patient_name]])
|
|
|
|
|
2014-12-02 07:08:20 +00:00
|
|
|
if not patient_ids:
|
2014-12-03 11:40:37 +00:00
|
|
|
done_qty+=1
|
2014-12-02 07:08:20 +00:00
|
|
|
vals={
|
2014-12-03 11:40:37 +00:00
|
|
|
'name': patient_name,
|
2014-12-02 07:08:20 +00:00
|
|
|
'hn': hn,
|
2014-12-03 11:40:37 +00:00
|
|
|
'type_id': ptype.id,
|
2014-12-02 07:08:20 +00:00
|
|
|
}
|
|
|
|
patient_id=get_model('clinic.patient').create(vals)
|
2014-12-03 11:40:37 +00:00
|
|
|
msg+="%s,%s,%s,%s\n"%(hcode,hn,patient_name,"Created")
|
2014-12-02 07:08:20 +00:00
|
|
|
print("create patient ", patient_id)
|
2014-12-03 11:40:37 +00:00
|
|
|
else:
|
|
|
|
if hcode!='0':
|
|
|
|
msg+="%s,%s,%s,%s\n"%(hcode,hn,patient_name,"Wrong Code hospital")
|
|
|
|
fail_qty+=1
|
|
|
|
obj.write({
|
|
|
|
'fail_qty': fail_qty,
|
|
|
|
'done_qty': done_qty,
|
|
|
|
'msg': msg,
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'import_clinic_patient',
|
|
|
|
'mode': 'form',
|
|
|
|
'active_id': obj.id,
|
|
|
|
},
|
|
|
|
'flash': 'Import successfully',
|
|
|
|
}
|
2014-12-02 07:08:20 +00:00
|
|
|
|
|
|
|
ImportPatient.register()
|