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"), '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"), } def get_hcode(self,context={}): settings=get_model("settings").browse(1) hcode=settings.hospital_code or "" return hcode 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 _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"), 'hcode_id': get_hcode_id, 'patient_type_id': _get_patient_type, } def import_patient(self,ids,context={}): obj=self.browse(ids)[0] ptype=obj.patient_type_id res={} if ptype.code=='PKS': res=self.import_patient_pks(ids,context) elif ptype.code=='UC': 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 fname=obj.file fpath=get_file_path(fname) lines=utils.read_excel(fpath,show_datetime=True) if not lines: raise Exception("Wrong File") msg="" msg+="hcode,hn,name,note\n" done_qty=0 fail_qty=0 for line in lines: hcode=line.get('hcode18','0') if not hcode: hcode='0' hcode=int(hcode) hcode=str(hcode) 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]]) if not patient_ids: done_qty+=1 vals={ 'name': patient_name, 'hn': hn, 'type_id': ptype.id, } patient_id=get_model('clinic.patient').create(vals) msg+="%s,%s,%s,%s\n"%(hcode,hn,patient_name,"Created") print("create patient ", patient_id) 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', } ImportPatient.register()