clinic/netforce_clinic/models/setting.py

220 lines
8.6 KiB
Python

import time
import random
import datetime
import xlrd
import xmltodict
from netforce.model import Model, fields, get_model
from netforce.utils import get_file_path
from netforce.database import get_connection
class ClinicSetting(Model):
_name="clinic.setting"
_string="Setting"
_fields={
"var_k": fields.Float("K"),
'file': fields.File("File"),
}
def read_excel(self,fpath=None):
data={}
if fpath:
suffix=fpath.split(".")[-1]
if suffix not in ('xls', 'xlsx'):
raise Exception("ERROR : please should file xls or xlsx")
wb=xlrd.open_workbook(fpath)
sheet=wb.sheet_by_name("Sheet1")
# read header values into the list
keys = [sheet.cell(0, col_index).value for col_index in range(sheet.ncols)]
data=[]
for row_index in range(1, sheet.nrows):
#d = {(keys[col_index] or "").lower(): sheet.cell(row_index, col_index).value for col_index in range(sheet.ncols)}
d={}
for col_index in range(sheet.ncols):
ctype=sheet.cell(row_index,col_index).ctype
if ctype==3:
value=sheet.cell(row_index, col_index).value
year, month, day, hour, minute, second = xlrd.xldate_as_tuple(value,wb.datemode)
value=datetime.datetime(year, month, day, hour, minute,second)
value=value.strftime("%Y-%m-%d")
else:
value=sheet.cell(row_index, col_index).value
d.update({(keys[col_index] or "").lower():value})
data.append(d)
return data
def read_cash(self,ids,context={}):
obj=self.browse(ids)[0]
fname=obj.file
fpath=get_file_path(fname)
lines=self.read_excel(fpath)
if not lines:
raise Exception("Wrong File")
medicals=[]
patients=[]
for line in lines:
medical=line['medical'] or ""
if not medical in medicals:
medicals.append(medical)
patient=line['hn'] or ""
name14=line['name14'] or ""
value=(patient,name14)
if not value in patients:
patients.append(value)
#vals={
#'hn': hn,
#'name14': line.get('name14'),
#'hcode18': hcode,
#'amount23': line.get('amount23'),
#"cur": line.get('cur'),
#'epoadm29': line.get('epoadm29'),
#'eponame': line.get('eponame'),
#'ln': line.get('ln'),
#'st': line.get('st'),
#'allow37': line.get('allow37'),
#'dttran': line.get("dttran"),
#'type': type,
#}
products=[p['name'] for p in get_model("product").search_read([],['name'])]
print('generate product')
print("="*50)
seq=1
for md in medicals:
md=md.replace(" ","")
if not md:
continue
if not md in products:
vals={
'code': "m"+("%s"%seq).zfill(4),
'name':md,
'uom_id': 2,
'type': 'stock',
}
seq+=1
prod_id=get_model("product").create(vals)
print(prod_id)
print("="*50)
print('generate patient')
hns=[pt['hn'] for pt in get_model("clinic.patient").search_read([],['hn'])]
for hn, name in patients:
hn=hn.replace(' ',"")
if not hn:
continue
if not hn in hns:
vals={
'hn': hn,
'name': name,
'type': 'sc',
}
pt_id=get_model("clinic.patient").create(vals)
print('hn ', pt_id)
print("="*50)
patients=get_model("clinic.patient").search_read([],['name','hn'])
visits=get_model("clinic.visit").search_read([],['number','time_start','cycle_id','state'])
doctor_ids=[dt['id'] for dt in get_model("clinic.doctor").search_read([],['name'])]
nurse_ids=[ns['id'] for ns in get_model("clinic.nurse").search_read([],['name'])]
cycle_ids=[cc['id'] for cc in get_model("clinic.cycle").search_read([],['name'])]
department_ids=[dp['id'] for dp in get_model("clinic.department").search_read([],['name'])]
#db=get_connection()
def get_patient(hn):
for pt in patients:
if hn==pt['hn']:
return pt
return None
def get_visit(date,cycle_name=None,state=None):
for visit in visits:
if date==visit['time_start'][0:10]:
if state:
print(state, " ", visit['state'])
if visit['state']==state:
if cycle_name:
if cycle_name==visit['cycle_id'][1]:
return visit
else:
return None
return visit
else:
print("else ")
return None
return visit
print("="*50)
print("create simple visit")
timenow=time.strftime("%H:%M:%S")
timenow2="%s:%s:%s" % (int(time.strftime("%H"))+1, time.strftime("%M"), time.strftime("%S"))
time_start=timenow
for line in lines:
hn=line['hn']
date=line['dttran']
visit=get_visit(date)
if not visit:
patient=get_patient(hn)
if patient:
time_start="%s %s"%(date,timenow)
time_stop="%s %s"%(date,timenow2)
vals={
'time_start': time_start,
'time_stop': time_stop,
'patient_id': patient['id'],
'cycle_id': random.choice(cycle_ids),
'nurse_id': random.choice(nurse_ids),
'doctor_id': random.choice(doctor_ids),
'department_id': random.choice(department_ids),
}
visit_id=get_model("clinic.visit").create(vals)
print("create visit %s ", visit_id)
print("="*50)
print("visit with state is draft")
count=0
for line in lines:
date=line['dttran']
# prevent timeout
if count > 10:
break
visit=get_visit(date=date,state='draft')
if visit:
visit_id=visit['id']
context['called']=True
hd_case_id=get_model("clinic.visit").browse(visit_id).confirm(context=context)
#allow37=line['allow37']
hct=line['hct']
medical=line['medical']
medical_cost=line['medical_cost']
inject_service=line['inject_service']
vals={
'hct': hct and hct or 0,
'lines': [],
}
if medical:
prods=get_model("product").search_browse([['name','=',medical]])
if prods:
prod=prods[0]
vals['lines'].append(('create',{
'type': 'other',
'product_id': prod.id,
'description': prod.name,
'uom_id': prod.uom_id.id,
'qty': 1,
'price': medical_cost,
'amount': medical_cost,
}))
if inject_service:
vals['lines'].append(('create',{
'type': 'other',
'product_id': 55, #XXX
'description': prod.name,
'uom_id': prod.uom_id.id,
'qty': 1,
'price': medical_cost,
'amount': medical_cost,
}))
hd_case=get_model("clinic.hd.case").browse(hd_case_id)
hd_case.write(vals)
count+=1
print("#%s write hd_case %s ok and visit %s" % (count,hd_case_id,hd_case.visit_id.number))
print("="*50)
print("Done")
return
ClinicSetting.register()