clinic/netforce_clinic/models/import_payment.py

92 lines
2.7 KiB
Python
Raw Normal View History

2014-10-04 15:51:54 +00:00
import time
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-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)
worksheet=wb.sheet_by_name("Sheet1")
num_rows=worksheet.nrows-1
curr_row=-1
while curr_row < num_rows:
curr_row +=1
2014-10-22 08:55:57 +00:00
worksheet.cell_value(curr_row,2)
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")
for line in lines:
print(line)
2014-10-05 17:00:16 +00:00
2014-10-24 04:24:33 +00:00
def import_mg(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)
print("fpath ", fpath)
2014-10-24 04:24:33 +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)
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()