import time from calendar import monthrange from netforce.model import Model, fields, get_model from netforce.utils import get_file_path from . import utils class MatchingPayment(Model): _name="clinic.matching.payment" _transient=True _fields={ "date": fields.Date("Month"), "date_from": fields.Date("From", required=True), "date_to": fields.Date("To", required=True), 'file': fields.File("File"), 'patient_type_id': fields.Many2One("clinic.patient.type","Patient Type",required=True), 'pcode': fields.Char("Code",required=True), 'hcode_id': fields.Many2One("clinic.hospital","HCode"), 'expenes': fields.Many2Many("clinic.hd.case.expense","Expenses"), } def _get_ptype(self,context={}): tids=get_model('clinic.patient.type').search([['default','=',True]]) tid=None if tids: tid=tids[0] return tid def _get_pcode(self,context={}): types=get_model('clinic.patient.type').search_browse([['default','=',True]]) if types: return types[0].code def _get_date_from(self,context={}): year,month=time.strftime("%Y-%m").split("-") return '%s-%s-01'%(year,month) def _get_date_to(self,context={}): year,month,day=time.strftime("%Y-%m-%d").split("-") weekday, total_day=monthrange(int(year), int(month)) return "%s-%s-%s"%(year,month,total_day) _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d"), 'date_from': _get_date_from, 'date_to': _get_date_to, 'patient_type_id': _get_ptype, 'pcode': _get_pcode, } def get_line(self,ids,context={}): obj=self.browse(ids)[0] if not obj.file: raise Exception("File not found!") fname=obj.file if obj.pcode=='SSO': hcode=obj.hcode_id.code or "" if not hcode: raise Exception("Please input hcode") n,sf=fname.split(".") if sf not in ('xls','xlsx'): raise Exception("File should be xls or xlsx") fpath=get_file_path(fname) lines=utils.read_excel(fpath,show_datetime=True) elif obj.pcode=='UC': fpath=get_file_path(fname) node='HDBills' lines=utils.read_xml(fpath,node=node) hcode=fname.split("_")[0] else: raise Exception("Type %s is not support"%obj.pcode or "") if not lines: raise Exception("No data to match") return lines def match(self,ids,context={}): obj=self.browse(ids)[0] lines=obj.get_line() matches1={} matches2={} if obj.pcode=='SSO': for line in lines: hcode=line.get('hcode18') if not hcode: hcode='0' hcode=int(hcode) hcode=str(hcode) if obj.hcode==hcode: lsrv_amt=line.get('epoadm29') or 0 lfee_amt=line.get('amount23') or 0 lmdc_amt=line.get('allow37') or 0 dttran=line.get("dttran") name=line.get("name14") date=dttran[0:10] time=dttran[11:] #XXX if not time: print("wrong format") continue hn=line.get('hn') hn=''.join([x for x in hn if x.isdigit()]) key1='%s:%s:%s:%s:%s'%( hn, date, lfee_amt, lsrv_amt, lmdc_amt, ) key2='%s:%s:%s:%s:%s'%( name, date, lfee_amt, lsrv_amt, lmdc_amt, ) if not matches1.get(key1): matches1[key1]=0 if not matches2.get(key2): matches2[key2]=0 elif obj.pcode=='UC': pass exp_ids=[] dom=[] dom.append(['date',">=",obj.date_from]) dom.append(['date',"<=",obj.date_to]) return for exp in get_model('clinic.hd.case.expense').search_browse(dom): exp_ids.append(exp.id) #TODO Checking obj.write({ 'expenes': [('add',exp_ids)] }) return { 'next': { 'name': 'clinic_matching_payment', 'mode': 'form', 'active_id': obj.id, }, 'flash': 'Succesfully', } def onchange_date(self,context={}): data=context['data'] date=data['date'] year,month,day=date.split("-") weekday, total_day=monthrange(int(year), int(month)) data['date_from']="%s-%s-01"%(year,month) data['date_to']="%s-%s-%s"%(year,month,total_day) return data def onchange_ptype(self,context={}): data=context['data'] type_id=data['patient_type_id'] if type_id: t=get_model('clinic.patient.type').browse(type_id) data['pcode']=t.code or "" return data MatchingPayment.register()