clinic/netforce_clinic/migrations/import_uc.py

142 lines
5.3 KiB
Python

from netforce.model import get_model
from netforce import migration
from netforce.utils import get_file_path
from netforce.access import get_active_user, set_active_user, set_active_company, get_active_company
from . import utils
class Migration(migration.Migration):
_name="import.uc"
_version="2.11"
def import_visit(self,lines):
visits={}
cycles=[(c['id'],'%s:00'%c['time_start'],'%s:00'%c['time_stop']) for c in get_model("clinic.cycle").search_read([[]],['time_start','time_stop'])]
patients=get_model("clinic.patient").search_browse([])
for line in lines:
dttran=line.get("dttran")
hn=line.get("hn")
if hn:
hn=''.join(x for x in hn if x.isdigit)
pid=None
for pt in patients:
if pt['hn_num']==hn:
pid=pt.id
break
if not pid:
print('continue ', pid)
continue
if dttran:
date,time=dttran.split("T")
if not time:
continue
cycle_id=None
patient=get_model("clinic.patient").browse(pid)
doctor=patient.doctor_id
department=patient.department_id
vals={
'patient_id': patient.id,
'doctor_id': doctor.id,
'department_id': department.id,
'state': 'pending',
}
for cycle in cycles:
time_start=cycle[1]
time_stop=cycle[2]
if time >= time_start:
cycle_id=cycle[0]
vals['cycle_id']=cycle_id
vals['time_start']='%s %s'%(date,time_start)
vals['time_stop']='%s %s'%(date,time_stop)
if not cycle_id:
raise Exception("not found cycle on this time %s %s %s"%(dttran, time_start,time_stop))
visit_ids=get_model("clinic.visit").search([['visit_date','=',date],['patient_id','=',pid]])
visit_id=None
if not visit_ids:
vals['visit_date']=date
visit_id=get_model('clinic.visit').create(vals)
else:
visit_id=visit_ids[0]
if visit_id:
visits.update({
visit_id: {
'hct': line.get('hct'),
}
})
pass
return visits
def confirm_visit(self,visits):
st=get_model("clinic.setting").browse(1)
st.write({
'auto_gen': True,
})
hd_case_ids=[]
visit_ids=visits.keys()
for visit in get_model('clinic.visit').browse(visit_ids):
if visit!='confirmed':
hd_case_id=visit.confirm()['next']['active_id']
hd_case=get_model("clinic.hd.case").browse(hd_case_id)
# lines
vals={
'hct': visits[visit.id]['hct'] or 0,
'lines': [],
}
for st_prod in st.products:
if st_prod.patient_type_id.id==3:
prod=st_prod.product_id
price=st_prod.price
qty=st_prod.qty
categ=st_prod.product_categ_id
if categ.code=='FEE':
amt=visits[visit.id]['amount23'] or 0
elif categ.code=='SRV':
amt=visits[visit.id]['epoadm29'] or 0
elif categ.code=='EPO':
amt=visits[visit.id]['allow37'] or 0
else:
amt=0
vals['lines'].append(('create',{
'product_id': prod.id,
'uom_id': st_prod.uom_id.id,
'product_categ_id': categ.id,
'description': st_prod.description,
'price': price,
'qty': qty,
'reimbursable': st_prod.reimbursable,
'amount': amt,
}))
hd_case.write(vals)
hd_case_ids.append(hd_case.id)
st.write({
'auto_gen': False,
})
return hd_case_ids
def hdc_done(self,hd_case_ids):
done_ids=[]
for hd_case in get_model("clinic.hd.case").browse(hd_case_ids):
if hd_case.state=='waiting_treatment':
hd_case.complete()
done_ids.append(hd_case.id)
return done_ids
def migrate(self):
#fname='22283_EURSTM_201406.xml'
fname='22283_UOCDSTM_20140902.xml'
fpath=get_file_path(fname)
node='HDBills'
lines=utils.read_xml(fpath,node=node)
set_active_company(1)
visits=self.import_visit(lines)
print("visit ", len(visits))
#hd_case_ids=self.confirm_visit(visits)
#print('hd case ', len(hd_case_ids))
#hd_case_ids=self.hdc_done(hd_case_ids)
#print('Done ', len(hd_case_ids))
return True
Migration.register()