clinic/netforce_clinic/models/import_payment.py

178 lines
5.9 KiB
Python
Raw Normal View History

2014-10-04 15:51:54 +00:00
import time
2014-10-24 10:40:01 +00:00
import datetime
2014-10-04 15:51:54 +00:00
import xlrd
2014-10-24 04:24:33 +00:00
import xmltodict
2014-10-04 15:51:54 +00:00
from netforce.model import Model, fields, get_model
from netforce.access import get_active_company
from netforce.utils import get_file_path
from netforce.utils import get_data_path
2014-10-05 17:00:16 +00:00
from netforce.database import get_connection
2014-10-04 15:51:54 +00:00
class ImportPayment(Model):
_name="clinic.import.payment"
2014-10-22 03:45:23 +00:00
_transient=True
2014-10-04 15:51:54 +00:00
_fields={
2014-10-22 03:45:23 +00:00
'date': fields.DateTime("Date"),
2014-10-05 17:00:16 +00:00
'file': fields.File("File"),
2014-10-24 08:08:56 +00:00
'result': fields.Text("Success"),
2014-10-04 15:51:54 +00:00
}
2014-10-22 03:45:23 +00:00
2014-10-04 15:51:54 +00:00
_defaults={
2014-10-22 03:45:23 +00:00
'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"),
2014-10-04 15:51:54 +00:00
}
2014-10-05 17:00:16 +00:00
2014-10-22 03:45:23 +00:00
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)
2014-10-24 08:08:56 +00:00
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):
2014-10-24 10:40:01 +00:00
#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})
2014-10-24 08:08:56 +00:00
data.append(d)
2014-10-05 17:00:16 +00:00
return data
2014-10-24 04:24:33 +00:00
def read_xml(self,fpath=None,node=""):
2014-10-22 03:45:23 +00:00
data={}
2014-10-24 04:24:33 +00:00
if not node:
return data
2014-10-22 03:45:23 +00:00
if fpath:
suffix=fpath.split(".")[-1]
if suffix not in ('xml'):
raise Exception("ERROR : please should file xml")
2014-10-24 04:24:33 +00:00
data=xmltodict.parse(open(fpath,"r").read())
2014-10-05 17:00:16 +00:00
2014-10-24 04:24:33 +00:00
stmstm=data.get('STMSTM')
if stmstm:
hdbills=stmstm.get(node)
if not hdbills:
return {}
lines=[]
for k, v in hdbills.items():
collections=v
for collection in collections:
if isinstance(collection,dict):
line={}
for i, j in collection.items():
key=(i or "").lower()
line[key]=j
lines.append(line)
#titles=[title for title, value in lines[0].items()]
return lines
def import_nhso(self,ids,context={}):
2014-10-05 17:00:16 +00:00
obj=self.browse(ids)[0]
2014-10-22 03:45:23 +00:00
fname=obj.file
fpath=get_file_path(fname)
2014-10-24 04:24:33 +00:00
if not fpath:
raise Exception("Please select file")
lines=self.read_xml(fpath,node='HDBills')
if not lines:
raise Exception("Wrong file")
2014-10-24 08:08:56 +00:00
data_nhso=get_model("clinic.data.nhso")
nhso_ids=data_nhso.search([])
data_nhso.delete(nhso_ids)
result=""
result+="Match: %s"%(50)
result+="\n"
result+="*"*50
2014-10-24 04:24:33 +00:00
for line in lines:
2014-10-24 08:08:56 +00:00
# TODO need to check match or not
data_nhso.create(line)
line['type']='success'
2014-10-24 04:24:33 +00:00
print(line)
2014-10-24 08:08:56 +00:00
result+="\n"
result+="Not Match: %s"%(40)
result+="\n"
result+="*"*50
obj.write({
'result': result,
})
2014-10-05 17:00:16 +00:00
2014-10-22 03:45:23 +00:00
def import_sc(self,ids,context={}):
2014-10-05 17:00:16 +00:00
obj=self.browse(ids)[0]
2014-10-22 03:45:23 +00:00
fname=obj.file
fpath=get_file_path(fname)
2014-10-24 08:08:56 +00:00
lines=self.read_excel(fpath)
if not lines:
raise Exception("Wrong File")
data_sc=get_model("clinic.data.sc")
sc_ids=data_sc.search([])
data_sc.delete(sc_ids)
st={}
patient=get_model("clinic.patient")
old_patient=[x['hn'] for x in patient.search_read([],['hn']) if x['hn']]
2014-10-24 10:40:01 +00:00
fail_qty=0
success_qty=0
2014-10-24 08:08:56 +00:00
for line in lines:
hn=line.get('hn')
if not hn:
continue
# create patient if not found
if not st.get(hn) and (not hn in old_patient):
patient.create({
'type': 'sc',
'hn': hn,
'name': line.get('name14'),
'reg_date': time.strftime("%Y-%m-%d"),
})
st.update({hn: line.get('name14')})
print("create %s ok"%hn)
2014-10-24 10:40:01 +00:00
hcode=int((line.get('hcode18') or "0")) # XXX
type=hcode==23869 and 'success' or 'fail'
if type=='success':
success_qty+=1
else:
fail_qty+=1
2014-10-24 08:08:56 +00:00
vals={
'hn': hn,
'name14': line.get('name14'),
2014-10-24 10:40:01 +00:00
'hcode18': hcode,
2014-10-24 08:08:56 +00:00
'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'),
2014-10-24 10:40:01 +00:00
'dttran': line.get("dttran"),
'type': type,
2014-10-24 08:08:56 +00:00
}
data_sc.create(vals)
2014-10-24 10:40:01 +00:00
msg=''
msg+="%s\n"%("*"*50)
msg+='success: %s\n'%success_qty
msg+='fail: %s\n'%fail_qty
msg+="%s\n"%("*"*50)
2014-10-24 08:08:56 +00:00
obj.write({
'result': msg,
})
2014-10-24 10:40:01 +00:00
def import_mg(self,ids,context={}):
obj=self.browse(ids)[0]
fname=obj.file
fpath=get_file_path(fname)
print("fpath ", fpath)
2014-10-04 15:51:54 +00:00
2014-10-06 00:30:57 +00:00
2014-10-04 15:51:54 +00:00
ImportPayment.register()