clinic/netforce_clinic/models/matching_payment.py

537 lines
18 KiB
Python
Raw Normal View History

2015-02-04 14:10:41 +00:00
import time
from calendar import monthrange
2015-01-16 11:19:49 +00:00
from netforce.model import Model, fields, get_model
2015-02-04 14:10:41 +00:00
from netforce.utils import get_file_path
2015-02-04 19:02:44 +00:00
from netforce.database import get_connection
from netforce.access import get_active_company
2015-02-04 14:10:41 +00:00
from . import utils
2015-01-16 11:19:49 +00:00
class MatchingPayment(Model):
_name="clinic.matching.payment"
_transient=True
_fields={
2015-02-04 14:10:41 +00:00
"date": fields.Date("Month"),
"date_from": fields.Date("From", required=True),
"date_to": fields.Date("To", required=True),
2015-01-16 11:19:49 +00:00
'file': fields.File("File"),
'patient_type_id': fields.Many2One("clinic.patient.type","Patient Type",required=True),
2015-02-04 14:10:41 +00:00
'pcode': fields.Char("Code",required=True),
'hcode_id': fields.Many2One("clinic.hospital","HCode"),
2015-01-16 11:19:49 +00:00
'expenes': fields.Many2Many("clinic.hd.case.expense","Expenses"),
2015-02-05 01:01:12 +00:00
'note': fields.Text("Note"),
2015-01-16 11:19:49 +00:00
}
def _get_ptype(self,context={}):
tids=get_model('clinic.patient.type').search([['default','=',True]])
tid=None
if tids:
tid=tids[0]
return tid
2015-02-04 14:10:41 +00:00
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)
2015-01-16 11:19:49 +00:00
_defaults={
2015-02-04 14:10:41 +00:00
'date': lambda *a: time.strftime("%Y-%m-%d"),
'date_from': _get_date_from,
'date_to': _get_date_to,
2015-01-16 11:19:49 +00:00
'patient_type_id': _get_ptype,
2015-02-04 14:10:41 +00:00
'pcode': _get_pcode,
2015-01-16 11:19:49 +00:00
}
2015-02-04 14:10:41 +00:00
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:
2015-02-04 19:02:44 +00:00
raise Exception("Wrong HCode")
2015-02-04 14:10:41 +00:00
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
2015-01-16 11:19:49 +00:00
2015-02-11 07:51:21 +00:00
def match_old(self,ids,context={}):
2015-01-16 11:19:49 +00:00
obj=self.browse(ids)[0]
2015-02-04 14:10:41 +00:00
lines=obj.get_line()
2015-02-04 19:02:44 +00:00
db=get_connection()
db.execute("truncate m2m_clinic_hd_case_expense_clinic_matching_payment;")
2015-02-04 14:10:41 +00:00
matches1={}
matches2={}
2015-02-11 07:51:21 +00:00
matches3={}
2015-02-04 14:10:41 +00:00
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")
2015-02-11 07:51:21 +00:00
pid=line.get('pid')
2015-02-04 14:10:41 +00:00
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'%(
2015-02-11 07:51:21 +00:00
pid,
date,
lfee_amt,
lsrv_amt,
lmdc_amt,
)
key3='%s:%s:%s:%s:%s'%(
2015-02-04 14:10:41 +00:00
name,
date,
lfee_amt,
lsrv_amt,
lmdc_amt,
)
2015-02-11 07:51:21 +00:00
vals={
'pid': pid,
'name': name,
'date': date,
'fee_amt': lfee_amt,
'service_amt': lsrv_amt,
'epo_amt': lmdc_amt,
}
2015-02-04 14:10:41 +00:00
if not matches1.get(key1):
2015-02-11 07:51:21 +00:00
matches1[key1]=vals
2015-02-04 14:10:41 +00:00
if not matches2.get(key2):
2015-02-11 07:51:21 +00:00
matches2[key2]=vals
if not matches3.get(key3):
matches2[key2]=vals
2015-02-04 14:10:41 +00:00
2015-02-04 19:02:44 +00:00
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'],
}
2015-02-05 01:01:12 +00:00
note=""
2015-01-16 11:19:49 +00:00
exp_ids=[]
2015-02-04 14:10:41 +00:00
dom=[]
dom.append(['date',">=",obj.date_from])
dom.append(['date',"<=",obj.date_to])
2015-02-04 19:02:44 +00:00
dom.append(['patient_id.type_id',"=",obj.patient_type_id.id])
dom.append(['state','!=', 'completed'])
2015-02-04 14:10:41 +00:00
for exp in get_model('clinic.hd.case.expense').search_browse(dom):
2015-01-16 11:19:49 +00:00
exp_ids.append(exp.id)
2015-02-05 01:01:12 +00:00
pt=exp.patient_id
2015-02-04 19:02:44 +00:00
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',
})
2015-02-05 01:01:12 +00:00
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:
2015-02-05 09:03:51 +00:00
hdcase=exp.hd_case_id
if hdcase:
for inv in hdcase.invoices:
pass
#if
#pass
exp.write({
'state': 'match',
})
2015-02-05 01:01:12 +00:00
else:
note+="not found %s, %s\n"%(key1,key2)
2015-01-16 11:19:49 +00:00
obj.write({
2015-02-05 01:01:12 +00:00
'expenes': [('add',exp_ids)],
'note': note,
2015-01-16 11:19:49 +00:00
})
2015-02-04 19:02:44 +00:00
print("Done!")
flash='Succesfully'
if not exp_ids:
flash="No match"
2015-01-16 11:19:49 +00:00
return {
'next': {
'name': 'clinic_matching_payment',
'mode': 'form',
'active_id': obj.id,
},
2015-02-04 19:02:44 +00:00
'flash': flash,
2015-01-16 11:19:49 +00:00
}
2015-02-11 07:51:21 +00:00
def match(self,ids,context={}):
obj=self.browse(ids)[0]
lines=obj.get_line()
return lines
matches1={}
matches2={}
matches3={}
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):
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
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'],
}
for exp in get_model('clinic.hd.case.expense').search_browse(dom):
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',
})
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
exp.write({
'state': 'match',
})
flash='Done!'
return {
'next': {
'name': 'clinic_matching_payment',
'mode': 'form',
'active_id': obj.id,
},
'flash': flash,
}
2015-02-04 14:10:41 +00:00
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
2015-02-04 19:02:44 +00:00
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': [],
2015-02-05 01:01:12 +00:00
'rd_cust': True, #XXX
2015-02-04 19:02:44 +00:00
}
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',
}
2015-02-05 09:03:51 +00:00
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!")
2015-02-04 19:02:44 +00:00
2015-02-11 07:51:21 +00:00
def get_report_data(self,ids,context={}):
obj=self.browse(ids)[0]
lines=obj.match()
data={
'lines': lines,
}
return data
2015-01-16 11:19:49 +00:00
MatchingPayment.register()