import time import datetime import xlrd import xmltodict 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 from netforce.database import get_connection class ImportPayment(Model): _name="clinic.import.payment" _transient=True _fields={ 'date': fields.DateTime("Date"), 'file': fields.File("File"), 'result': fields.Text("Success"), 'hcode': fields.Char("Hospital Code"), } def get_hcode(self,context={}): settings=get_model("settings").browse(1) hcode=settings.hospital_code or "" return hcode _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"), 'hcode': get_hcode, } 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_xml(self,fpath=None,node=""): data={} if not node: return data if fpath: suffix=fpath.split(".")[-1] if suffix not in ('xml'): raise Exception("ERROR : please should file xml") data=xmltodict.parse(open(fpath,"r").read()) 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_uc(self,ids,context={}): obj=self.browse(ids)[0] fname=obj.file fpath=get_file_path(fname) if not fpath: raise Exception("Please select file") lines=self.read_xml(fpath,node='HDBills') if not lines: raise Exception("Wrong file") data_uc=get_model("clinic.data.uc") uc_ids=data_uc.search([]) data_uc.delete(uc_ids) result="" result+="Match: %s"%(50) result+="\n" result+="*"*50 for line in lines: # TODO need to check match or not data_uc.create(line) line['type']='success' print(line) result+="\n" result+="Not Match: %s"%(40) result+="\n" result+="*"*50 obj.write({ 'result': result, }) def import_sc(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") 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']] fail_qty=0 success_qty=0 hd_cases=get_model("clinic.hd.case").search_browse([['state','=','completed']]) def get_hdcase(hn,date): for hd_case in hd_cases: hd_date=hd_case.time_start[0:10] if hn==hd_case.patient_id.hn and date==hd_date: #XXX return hd_case.id 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) hcode=int((line.get('hcode18') or "0")) # XXX type='match' if hcode!=(int(obj.hcode)): continue dttran=line.get("dttran","") hd_case_id=get_hdcase(hn,dttran) if hd_case_id: success_qty+=1 else: fail_qty+=1 type='not_match' 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, 'hd_case_id': hd_case_id, } data_sc.create(vals) msg='' msg+="%s\n"%("*"*50) msg+='success: %s\n'%success_qty msg+='fail: %s\n'%fail_qty msg+="%s\n"%("*"*50) obj.write({ 'result': msg, }) def import_mg(self,ids,context={}): obj=self.browse(ids)[0] fname=obj.file fpath=get_file_path(fname) print("fpath ", fpath) def clear_sc(self,ids,context={}): sc_ids=get_model("clinic.data.sc").search([]) get_model("clinic.data.sc").delete(sc_ids) obj=self.browse(ids)[0] obj.write({ 'result': 'Clear OK', }) def match_invoice_sc(self,ids,context={}): obj=self.browse(ids)[0] for sc in get_model("clinic.data.sc").search_browse([['type','=','match']]): for invoice in sc.hd_case_id.invoices: print(invoice.number) obj.write({ 'result': 'Match OK', }) def approve_sc(self,ids,context={}): settings=get_model("settings").browse(1) account_id=settings.ap_sc_id.id if not account_id: raise Exception("No Account payment for Social Security") obj=self.browse(ids)[0] vals={ 'partner_id': '', "company_id": get_active_company(), "type": "in", "pay_type": "invoice", "date": obj.date or time.strftime("%Y-%m-%d"), "account_id": account_id, 'invoice_lines': [], } invoice_ids=[] partner_id=None for sc in get_model("clinic.data.sc").search_browse([['type','=','match']]): if not partner_id: partner_id=sc.hd_case_id.fee_partner_id.id for invoice in sc.hd_case_id.invoices: invoice_ids.append(invoice.id) vals['invoice_lines'].append(('create',{ 'invoice_id': invoice.id, 'amount': invoice.amount_due or 0.0, })) sc.delete() #XXX vals['partner_id']=partner_id payment_id=get_model("account.payment").create(vals,context={"type":"in"}) #get_model("account.payment").browse(payment_id).post() return { 'next': { 'name': 'payment', 'mode': 'form', 'active_id': payment_id, }, 'flash': 'Paid', } ImportPayment.register()