clinic/netforce_clinic/models/je.py

141 lines
4.5 KiB
Python
Raw Normal View History

2014-10-03 06:09:10 +00:00
import time
import xlrd
from netforce.model import Model, fields, get_model
from netforce.access import get_active_user, set_active_user
from netforce.utils import get_file_path
2014-10-03 08:24:53 +00:00
from netforce.utils import get_data_path
2014-10-03 06:09:10 +00:00
class JE(Model):
_name="clinic.je"
_string="Import Journal Entry"
_fields={
"number": fields.Char("Number"),
"name": fields.Char("Description"),
"date_import": fields.DateTime("Date Import"),
2014-10-03 08:24:53 +00:00
"type": fields.Selection([("mg","Medical Government"),("sc","Social Security"),("nhso","NHSO (30฿)")],"Type"),
2014-10-03 06:09:10 +00:00
'file': fields.File("File"),
'payment_id': fields.Many2One("account.payment","Payment"),
'lines': fields.One2Many("clinic.input.line","je_id", "Input Line"),
2014-10-03 08:24:53 +00:00
'input_data': fields.One2Many("clinic.input.data","je_id", "Input Data"),
2014-10-03 06:09:10 +00:00
'sheets': fields.One2Many("clinic.file.sheet","je_id", "Sheets"),
'columns': fields.One2Many("clinic.file.column","je_id", "Columns"),
"state": fields.Selection([("draft","Draft"),('sheet_loaded','Sheet Loaded'),("fail","Fail"),("done","Done")],"Status",required=True),
2014-10-03 08:24:53 +00:00
'description': fields.Text("Description")
2014-10-03 06:09:10 +00:00
}
def _get_number(self,context={}):
while 1:
seq_id=get_model("sequence").find_sequence(name="Clinic Import JE")
num=get_model("sequence").get_next_number(seq_id,context=context)
if not num:
return None
user_id=get_active_user()
set_active_user(1)
res=self.search([["number","=",num]])
set_active_user(user_id)
if not res:
return num
get_model("sequence").increment_number(seq_id,context=context)
def _get_name(self,context={}):
timenow=time.strftime("%Y-%m-%d")
return 'Import Journal Entry - %s'%timenow
_defaults={
'date_import': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"),
'number': _get_number,
'name': _get_name,
'state': 'draft',
}
def load_sheet(self,ids,context={}):
obj=self.browse(ids[0])
fname=obj.file
if not fname:
raise Exception("Please select file!")
fpath=get_file_path(fname)
suffix=fpath.split(".")[-1]
if suffix not in ('xls', 'xlsx'):
raise Exception("ERROR : file support only xls, xlsx")
wb=xlrd.open_workbook(fpath)
sheets=wb.sheet_names()
index=0
# XXX clear old sheet
for sheet in obj.sheets:
sheet.delete()
vals={
'sheets': [],
}
for sheet in sheets:
line={
'index': index,
'name': sheet,
}
vals["sheets"].append(("create",line))
vals['state']='sheet_loaded'
obj.write(vals)
return {
'next': {
'name': "clinic_je",
'mode': 'form',
'active_id': obj.id,
},
'flash': "Load sheet successfully",
}
2014-10-03 08:24:53 +00:00
def load_data(self,ids,context={}):
2014-10-03 06:09:10 +00:00
obj=self.browse(ids[0])
2014-10-03 08:24:53 +00:00
if not obj.file:
raise Exception("Please select some file!")
2014-10-03 06:09:10 +00:00
if not obj.sheets:
2014-10-03 08:24:53 +00:00
raise Exception("Please load sheet!")
indexes=[]
for sheet in obj.sheets:
if sheet.select=='yes':
indexes.append((sheet.index,sheet.name))
if not indexes:
raise Exception("Please select some sheet!")
if len(indexes) > 1:
raise Exception("Only one sheet can select!")
sheet_name=indexes[0][1]
fname=obj.file
fpath=get_file_path(fname)
wb=xlrd.open_workbook(fpath)
ws=wb.sheet_by_name(sheet_name)
num_rows = ws.nrows - 1
curr_row = -1
records=[]
import pprint
while curr_row < num_rows:
curr_row += 1
#row=ws.row(curr_row)
record={}
# XXX
for col in (1,2,3,4):
record[col]=ws.cell_value(curr_row, col)
records.append(record)
pprint.pprint(records)
def find_invoice(self,ids,context={}):
pass
2014-10-03 06:09:10 +00:00
2014-10-03 08:24:53 +00:00
def import2journal(self,ids,context={}):
pass
def onchange_sheet(self,context={}):
data=context["data"]
path=context["path"]
line=get_data_path(data,path,parent=True)
for sheet in data['sheets']:
if sheet['name']!=line['name']:
sheet['select']=None
return data
2014-10-03 06:09:10 +00:00
JE.register()