import time from calendar import monthrange from netforce.model import Model, fields, get_model from netforce.utils import get_file_path from netforce.database import get_connection from netforce.access import get_active_company 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"), 'note': fields.Text("Note"), } 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("Wrong 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={} matches3={} matches4={} dom=[] dom.append(['date',">=",obj.date_from]) dom.append(['date',"<=",obj.date_to]) dom.append(['patient_id.type_id',"=",obj.patient_type_id.id]) dom.append(['state','!=', 'completed']) for exp in get_model('clinic.hd.case.expense').search_browse(dom): patient=exp.patient_id hn=patient.hn_no pid=patient.card_no pid=pid and pid or "" name=patient.name or '' name_check=name.replace(" ","") date=exp.date or "" lfee_amt=exp.fee_amt or 0 lmdc_amt=exp.mdc_amt or 0 lsrv_amt=exp.srv_amt or 0 #hn key1='%s:%s:%s:%s:%s'%( hn, date, lfee_amt, lsrv_amt, lmdc_amt, ) # id card key2='%s:%s:%s:%s:%s'%( pid, date, lfee_amt, lsrv_amt, lmdc_amt, ) key3='%s:%s:%s:%s:%s'%( name_check, date, lfee_amt, lsrv_amt, lmdc_amt, ) key4='%s:%s'%(name_check,date) vals={ 'pid': pid, 'name': name, 'date': date, 'fee_amt': lfee_amt, 'service_amt': lsrv_amt, 'epo_amt': lmdc_amt, 'hn': hn, 'expense_id': exp.id, } if not matches1.get(key1): matches1[key1]=vals if not matches2.get(key2): matches2[key2]=vals if not matches3.get(key3): matches3[key3]=vals if not matches4.get(key3): matches4[key4]=vals nf_hcode='' if obj.hcode_id: nf_hcode=obj.hcode_id.code records=[] if obj.pcode=='SSO': no=1 for line in lines: hcode=line.get('hcode18') if not hcode: hcode='0' hcode=int(hcode) hcode=str(hcode) if nf_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") name2=name name=name.replace(" ","") pid=line.get('pid') 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'%( pid, date, lfee_amt, lsrv_amt, lmdc_amt, ) key3='%s:%s:%s:%s:%s'%( name, date, lfee_amt, lsrv_amt, lmdc_amt, ) key4='%s:%s'%(name,date) #vals={ #'pid': pid, #'name': name, #'date': date, #'fee_amt': lfee_amt, #'service_amt': lsrv_amt, #'epo_amt': lmdc_amt, #} record={ 'no': no, 'date':date, 'patient_name': name2, 'hn': hn, 'fee_amt': lfee_amt, 'srv_amt': lsrv_amt, 'mdc_amt': lmdc_amt, 'note': '', 'nf_patient': '', 'nf_pid': '', 'nf_hn': '', 'nf_fee_amt': 0, 'nf_srv_amt': 0, 'nf_mdc_amt': 0, 'expense_id': None, 'expense_number': '', 'found': False, } if not matches1.get(key1): if not matches2.get(key2): if not matches4.get(key3): notes=matches4.get(key4) if notes: print("not found") record.update({ 'nf_date': notes['date'], 'nf_hn': notes['hn'], 'nf_pid': notes['pid'], 'nf_patient_name': notes['name'], 'nf_fee_amt': notes['fee_amt'], 'nf_srv_amt': notes['srv_amt'], 'nf_mdc_amt': notes['mdc_amt'], 'expense_id': notes['expense_id'], 'expense_number': notes['expense_number'], }) else: print(">> ", notes) else: print("found 3.") # found 3. found=matches3[key3] record.update({ 'expense_id': found['expense_id'], }) else: # found 2. print("found 2.") found=matches2[key2] record.update({ 'expense_id': found['expense_id'], }) else: # found 1. found=matches1[key1] print("found 1.") record.update({ 'expense_id': found['expense_id'], 'expense_number': found['expense_number'], 'found': True, }) records.append(record) no+=1 elif obj.pcode=='UC': for line in lines: #{'amount': '1500.0000', #'cstat': None, #'dttran': '2014-09-27T10:00:00', #'epostat': 'E', #'hdflag': 'COU', #'hdrate': '1500.0000', #'hn': '98511252', #'hreg': 'NHSO1', #'invno': '437941480', #'paid': '0.0000', #'paychk': '1', #'reimbpay': '0.0000', #'rid': '2190', #'station': '01'} date,time=(line['dttran'] or "").split("T") amt=line['amount'] or "0" amt=float(amt) if date and time: key1='%s-%s-%s'%( line['hn'], date, amt, ) if not matches1.get(key1): matches1[key1]={ 'invno': line['invno'], } return records 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 def do_import(self,ids,context={}): st=get_model("clinic.setting").browse(1) if not st.import_account_id: raise Exception("Import account not found (Ratchawat Setting -> Accounting)") obj=self.browse(ids)[0] if not obj.expenes: raise Exception("Nothing to import") obj=self.browse(ids)[0] partner=obj.patient_type_id.contact_id company_id=get_active_company() vals={ "partner_id": partner.id, "company_id": company_id, "type": "in", "pay_type": "invoice", 'date': time.strftime("%Y-%m-%d"), "account_id": st.import_account_id.id, 'invoice_lines': [], 'rd_cust': True, #XXX } for exp in obj.expenes: if exp.state=='match': for inv in exp.hd_case_id.invoices: if inv.partner_id.id==partner.id: vals['invoice_lines'].append(('create',{ 'invoice_id': inv.id, 'amount': inv.amount_due or 0, })) #should not match again exp.write({ 'state': 'completed', }) hdcase=exp.hd_case_id if hdcase: hdcase.write({ 'state': 'paid', }) if not vals['invoice_lines']: raise Exception("Nothing to import") payment_id=get_model("account.payment").create(vals,context={"type":"in"}) return { 'next': { 'name': 'payment', 'mode': 'form', 'active_id': payment_id, }, 'flash': 'Create Payment successfully', } def update_id(self,ids,context={}): obj=self.browse(ids)[0] lines=obj.get_line() pts={} for line in lines: pid=line.get("pid") name=line.get("name14") hn=line.get("hn") if not pts.get(name): pts[name]=pid for pt in get_model('clinic.patient').search_browse([]): pid=pts.get(pt.name) if pid: if pt.card_no: pid=''.join([str(x) for x in pt.card_no if x.isnumeric()]) print(pt.name, 'pid ', pid) else: pid=int(pid) print("pid ", pid) pt.write({ 'card_no': pid, }) print("Done!") def get_report_data(self,ids,context={}): lines=[] if ids: obj=self.browse(ids)[0] lines=obj.match() data={ 'lines': lines, } return data def match_old(self,ids,context={}): obj=self.browse(ids)[0] lines=obj.get_line() db=get_connection() db.execute("truncate m2m_clinic_hd_case_expense_clinic_matching_payment;") matches1={} matches2={} matches3={} 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") pid=line.get('pid') 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'%( pid, date, lfee_amt, lsrv_amt, lmdc_amt, ) key3='%s:%s:%s:%s:%s'%( name, date, lfee_amt, lsrv_amt, lmdc_amt, ) vals={ 'pid': pid, 'name': name, 'date': date, 'fee_amt': lfee_amt, 'service_amt': lsrv_amt, 'epo_amt': lmdc_amt, } if not matches1.get(key1): matches1[key1]=vals if not matches2.get(key2): matches2[key2]=vals if not matches3.get(key3): matches2[key2]=vals elif obj.pcode=='UC': for line in lines: #{'amount': '1500.0000', #'cstat': None, #'dttran': '2014-09-27T10:00:00', #'epostat': 'E', #'hdflag': 'COU', #'hdrate': '1500.0000', #'hn': '98511252', #'hreg': 'NHSO1', #'invno': '437941480', #'paid': '0.0000', #'paychk': '1', #'reimbpay': '0.0000', #'rid': '2190', #'station': '01'} date,time=(line['dttran'] or "").split("T") amt=line['amount'] or "0" amt=float(amt) if date and time: key1='%s-%s-%s'%( line['hn'], date, amt, ) if not matches1.get(key1): matches1[key1]={ 'invno': line['invno'], } note="" exp_ids=[] dom=[] dom.append(['date',">=",obj.date_from]) dom.append(['date',"<=",obj.date_to]) dom.append(['patient_id.type_id',"=",obj.patient_type_id.id]) dom.append(['state','!=', 'completed']) for exp in get_model('clinic.hd.case.expense').search_browse(dom): exp_ids.append(exp.id) pt=exp.patient_id if obj.pcode=='UC': hdcase=exp.hd_case_id date=hdcase.date fee_amt=0 for line in hdcase.lines: categ=line.product_categ_id if categ.code=='FEE': fee_amt+=line.amount or 0 key='%s-%s-%s'%(pt.hn_no,date,amt) found=matches1.get(key) if found: exp.write({ 'state': 'match', }) else: note+="not found %s\n"%key elif obj.pcode=='SSO': key1='%s:%s:%s:%s:%s'%( pt.hn_no, exp.date, exp.fee_amt and exp.fee_amt or 0, exp.srv_amt and exp.srv_amt or 0, exp.mdc_amt and exp.mdc_amt or 0, ) key2='%s:%s:%s:%s:%s'%( pt.name or "", exp.date, exp.fee_amt and exp.fee_amt or 0, exp.srv_amt and exp.srv_amt or 0, exp.mdc_amt and exp.mdc_amt or 0, ) found=matches1.get(key1) or matches2.get(key2) if found: hdcase=exp.hd_case_id if hdcase: for inv in hdcase.invoices: pass #if #pass exp.write({ 'state': 'match', }) else: note+="not found %s, %s\n"%(key1,key2) obj.write({ 'expenes': [('add',exp_ids)], 'note': note, }) print("Done!") flash='Succesfully' if not exp_ids: flash="No match" return { 'next': { 'name': 'clinic_matching_payment', 'mode': 'form', 'active_id': obj.id, }, 'flash': flash, } MatchingPayment.register()