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 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"), #"type": fields.Selection([("mg","Medical Government"),("sc","Social Security"),("nhso","NHSO (30฿)")],"Type"), 'file': fields.File("File"), 'payment_id': fields.Many2One("account.payment","Payment"), 'lines': fields.One2Many("clinic.input.line","je_id", "Input Line"), '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), } 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", } def process(self,ids,context={}): obj=self.browse(ids[0]) if not obj.sheets: raise Exception("No Sheet yet!") JE.register()