clinic/netforce_clinic/models/matching_payment.py

532 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-18 18:50:15 +00:00
from netforce.utils import get_file_path, get_data_path
2015-02-21 13:38:21 +00:00
from netforce.access import get_active_company, get_active_user
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
2015-02-25 15:23:16 +00:00
def _get_store(self,ids,context={}):
res={}
for obj in self.browse(ids):
partner=obj.patient_type_id.contact_id
res[obj.id]={
'partner_id': partner.id,
}
return res
2015-02-20 10:59:49 +00:00
def _get_all(self,ids,context={}):
res={}
for obj in self.browse(ids):
2015-02-20 13:10:40 +00:00
total_match=0
total_fee=0
total_srv=0
total_epo=0
2015-02-20 10:59:49 +00:00
for line in obj.lines:
2015-02-25 15:23:16 +00:00
if line.state!='match':
continue
2015-02-20 13:10:40 +00:00
total_fee+=line.fee or 0
total_srv+=line.srv or 0
total_epo+=line.epo or 0
2015-02-20 10:59:49 +00:00
state=line.state or ''
if state=='match':
2015-02-20 13:10:40 +00:00
total_match+=1
2015-02-20 10:59:49 +00:00
res[obj.id]={
'total': len(obj.lines),
2015-02-20 13:10:40 +00:00
'total_match': total_match,
'total_unmatch': len(obj.lines)-total_match,
'total_fee': total_fee,
'total_srv': total_srv,
'total_epo': total_epo,
2015-02-20 10:59:49 +00:00
}
return res
2015-01-16 11:19:49 +00:00
_fields={
2015-02-20 10:59:49 +00:00
'name': fields.Char("Name"),
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-25 15:23:16 +00:00
'partner_id': fields.Many2One("partner","Contact", function="_get_store", function_multi=True, store=True),
2015-02-04 14:10:41 +00:00
'pcode': fields.Char("Code",required=True),
'hcode_id': fields.Many2One("clinic.hospital","HCode"),
2015-02-18 18:50:15 +00:00
'lines': fields.One2Many("clinic.matching.payment.line","match_id", "Lines"),
2015-02-05 01:01:12 +00:00
'note': fields.Text("Note"),
2015-02-20 10:59:49 +00:00
'total': fields.Integer("Total",function="_get_all",function_multi=True),
2015-02-20 13:10:40 +00:00
'total_match': fields.Integer("Match",function="_get_all",function_multi=True),
'total_unmatch': fields.Integer("Unmatch",function="_get_all",function_multi=True),
'total_fee': fields.Float("FEE",function="_get_all",function_multi=True),
'total_srv': fields.Float("Service",function="_get_all",function_multi=True),
'total_epo': fields.Float("EPO",function="_get_all",function_multi=True),
2015-02-20 10:59:49 +00:00
'state': fields.Selection([['draft','Draft'],['approved','Approved']],'State'),
2015-02-21 13:38:21 +00:00
'user_id': fields.Many2One("base.user","Approver"),
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-02-20 10:59:49 +00:00
def _get_name(self,ids,context={}):
return time.strftime("%Y/%m")
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"),
2015-02-20 10:59:49 +00:00
'name': _get_name,
2015-02-04 14:10:41 +00:00
'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-02-20 10:59:49 +00:00
'state': 'draft',
2015-02-21 13:38:21 +00:00
'user_id': lambda *a: get_active_user(),
2015-01-16 11:19:49 +00:00
}
2015-02-20 10:59:49 +00:00
_order="date desc"
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-19 08:15:41 +00:00
try:
n,sf=fname.split(".")
except Exception as e:
print("ERROR: wrong file ", e)
2015-02-04 14:10:41 +00:00
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(self,ids,context={}):
obj=self.browse(ids)[0]
lines=obj.get_line()
2015-02-20 10:59:49 +00:00
print("len.lines ", len(lines))
2015-02-19 08:15:41 +00:00
patient_type=obj.patient_type_id
if not patient_type:
raise Exception("not found patient type")
contact=patient_type.contact_id
if not contact:
raise Exception("contact not found")
2015-02-11 07:51:21 +00:00
matches1={}
matches2={}
matches3={}
2015-02-20 10:59:49 +00:00
matches_uc={}
2015-02-11 07:51:21 +00:00
dom=[]
dom.append(['date',">=",obj.date_from])
dom.append(['date',"<=",obj.date_to])
2015-02-19 08:15:41 +00:00
dom.append(['partner_id',"=",contact.id])
dom.append(['state','=', 'waiting_payment'])
2015-02-20 10:59:49 +00:00
invoices=get_model('account.invoice').search_browse(dom)
print("len(invoices)", len(invoices))
for invoice in invoices:
pname,hn,card_no='', '', ''
if invoice.related_id:
hdcase=invoice.related_id
patient=hdcase.patient_id
pname=patient.name_check or ""
card_no=patient.card_no or ""
hn=patient.hn_no
elif invoice.ref:
pname=invoice.ref or ''
pname=pname.replace(" ","") # remove space
2015-02-25 14:14:57 +00:00
for pt in get_model("clinic.patient").search_browse([['name_check','=',pname]]):
hn=pt.hn_no or ""
2015-02-20 10:59:49 +00:00
else:
pass
fee,epo,srv=0, 0, 0
due_amount=0
for inv_line in invoice.lines:
amount=inv_line.amount or 0
prod=inv_line.product_id
categ_code=''
categ=prod.categ_id
if categ:
categ_code=categ.code
if categ_code=='FEE':
fee+=amount
elif categ_code=='SRV':
srv+=amount
elif categ_code=='EPO':
epo+=amount
else:
due_amount+=amount
2015-02-11 07:51:21 +00:00
vals={
2015-02-20 10:59:49 +00:00
'invoice_id': invoice.id,
'fee': fee,
'epo': epo,
'srv': srv,
2015-02-11 07:51:21 +00:00
}
2015-02-20 10:59:49 +00:00
due_amount+=fee+epo+srv
date=invoice.date
req_vals=[date,due_amount]
key1=':'.join([str(v) for v in [hn]+req_vals])
key2=':'.join([str(v) for v in [card_no]+req_vals])
key3=':'.join([str(v) for v in [pname]+req_vals]) # name no space
key_uc=':'.join([str(v) for v in [hn,date,due_amount]])
# becareful!!!
2015-02-11 07:51:21 +00:00
if not matches1.get(key1):
matches1[key1]=vals
2015-02-20 10:59:49 +00:00
if not matches2.get(key2):
matches2[key2]=vals
if not matches3.get(key3):
matches3[key3]=vals
if not matches_uc.get(key_uc):
matches_uc[key_uc]=vals
2015-02-18 16:53:29 +00:00
nf_hcode=''
if obj.hcode_id:
nf_hcode=obj.hcode_id.code
records=[]
2015-02-11 07:51:21 +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)
2015-02-18 16:53:29 +00:00
if nf_hcode==hcode:
2015-02-20 10:59:49 +00:00
epo=line.get('epoadm29') or 0
fee=line.get('amount23') or 0
srv=line.get('allow37') or 0
date_treatment=line.get("dttran")
2015-02-11 07:51:21 +00:00
name=line.get("name14")
2015-02-20 10:59:49 +00:00
name_nospace=name.replace(" ","")
card_no=int(line.get('pid'))
date=date_treatment[0:10]
time=date_treatment[11:] #XXX
2015-02-11 07:51:21 +00:00
if not time:
print("wrong format")
continue
2015-02-20 10:59:49 +00:00
due_amount=fee+epo+srv
req_vals=[date,due_amount]
2015-02-11 07:51:21 +00:00
hn=line.get('hn')
hn=''.join([x for x in hn if x.isdigit()])
2015-02-20 10:59:49 +00:00
key1=':'.join([str(v) for v in [hn]+req_vals])
key2=':'.join([str(v) for v in [card_no]+req_vals])
key3=':'.join([str(v) for v in [name_nospace]+req_vals])
2015-02-18 16:53:29 +00:00
record={
'date':date,
2015-02-20 10:59:49 +00:00
'patient_name': name,
'pid': card_no,
2015-02-18 16:53:29 +00:00
'hn': hn,
2015-02-20 10:59:49 +00:00
'fee': fee,
'srv': srv,
'epo': epo,
2015-02-19 08:15:41 +00:00
'invoice_id': None,
2015-02-20 10:59:49 +00:00
'state': 'unmatch',
2015-02-11 07:51:21 +00:00
}
2015-02-20 10:59:49 +00:00
#if hn=='100794':
#import pdb; pdb.set_trace()
if matches1.get(key1):
print("found ", key1)
record.update({
'invoice_id': matches1[key1]['invoice_id'],
'state': 'match',
})
elif matches2.get(key2):
print("found ", key2)
record.update({
'invoice_id': matches2[key2]['invoice_id'],
'state': 'match',
})
elif matches3.get(key3):
print("found ", key3)
2015-02-18 16:53:29 +00:00
record.update({
2015-02-20 10:59:49 +00:00
'invoice_id': matches3[key3]['invoice_id'],
2015-02-19 08:15:41 +00:00
'state': 'match',
2015-02-18 16:53:29 +00:00
})
2015-02-20 10:59:49 +00:00
else:
pass
## unmatch
2015-02-18 16:53:29 +00:00
records.append(record)
2015-02-11 07:51:21 +00:00
elif obj.pcode=='UC':
2015-02-18 18:50:15 +00:00
records=[]
2015-02-11 07:51:21 +00:00
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")
2015-02-20 10:59:49 +00:00
fee=0
if line.get('amount'):
fee=float(line['amount'])
2015-02-18 18:50:15 +00:00
hn=line['hn']
hn=hn.replace(" ", "")
2015-02-20 10:59:49 +00:00
key_uc=''
2015-02-11 07:51:21 +00:00
if date and time:
2015-02-20 10:59:49 +00:00
key_uc='%s:%s:%s'%(line['hn'],date,fee,)
pname=''
2015-02-18 18:50:15 +00:00
pid=''
for pt in get_model("clinic.patient").search_browse([['hn_no','=',hn]]):
2015-02-20 10:59:49 +00:00
pname=pt.name or ""
pid=pt.card_no or ""
2015-02-18 18:50:15 +00:00
record={
'date':date,
2015-02-20 10:59:49 +00:00
'name': pname,
2015-02-18 18:50:15 +00:00
'pid': pid,
'hn': hn,
2015-02-20 10:59:49 +00:00
'fee': fee,
'srv': 0,
'epo': 0,
2015-02-19 08:15:41 +00:00
'invoice_id': None,
2015-02-20 10:59:49 +00:00
'state': 'unmatch',
2015-02-18 18:50:15 +00:00
}
2015-02-20 10:59:49 +00:00
if matches_uc.get(key_uc):
2015-02-20 01:52:14 +00:00
record.update({
2015-02-20 10:59:49 +00:00
'invoice_id': matches1[key_uc]['invoice_id'],
'state': 'match',
2015-02-20 01:52:14 +00:00
})
else:
pass
#print("not found")
2015-02-18 18:50:15 +00:00
records.append(record)
2015-02-20 10:59:49 +00:00
if obj.hcode_id and not records:
raise Exception("Nothing to match! for %s"%(obj.hcode_id.name or ""))
if not records:
raise Exception("Nothing to match! Please verify your file and your filter condition.")
2015-02-18 16:53:29 +00:00
return records
2015-02-11 07:51:21 +00:00
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)
2015-02-20 10:59:49 +00:00
data['name']='%s/%s'%(year,month)
2015-02-04 14:10:41 +00:00
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]
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
}
2015-02-18 18:50:15 +00:00
for line in obj.lines:
2015-02-19 08:15:41 +00:00
inv=line.invoice_id
if inv:
vals['invoice_lines'].append(('create',{
'invoice_id': inv.id,
'amount': inv.amount_due or 0,
}))
if inv.related_id:
hdcase=inv.related_id
2015-02-04 19:02:44 +00:00
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
2015-02-18 18:50:15 +00:00
def do_match(self,ids,context={}):
obj=self.browse(ids)[0]
records=obj.match()
lines=[]
if obj.pcode=='SSO':
for record in records:
2015-02-20 10:59:49 +00:00
state=record['state'] or ''
2015-02-18 18:50:15 +00:00
vals={
'date': record['date'],
'hn': record['hn'],
'pid': record['pid'],
'name': record['patient_name'],
2015-02-20 10:59:49 +00:00
'epo': record['epo'],
'srv': record['srv'],
'fee': record['fee'],
2015-02-19 08:15:41 +00:00
'invoice_id': record['invoice_id'],
2015-02-20 10:59:49 +00:00
'state': state,
2015-02-18 18:50:15 +00:00
}
lines.append(('create', vals))
elif obj.pcode=='UC':
for record in records:
2015-02-20 10:59:49 +00:00
state=record['state'] or ''
2015-02-18 18:50:15 +00:00
vals={
'date': record['date'],
'hn': record['hn'],
'pid': record['pid'],
'name': record['name'],
2015-02-25 14:14:57 +00:00
'fee': record['fee'],
2015-02-19 08:15:41 +00:00
'invoice_id': record['invoice_id'],
2015-02-20 10:59:49 +00:00
'state': state,
2015-02-18 18:50:15 +00:00
}
lines.append(('create', vals))
for line in obj.lines:
line.delete()
obj.write({
'lines': lines,
})
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={}):
2015-02-12 08:52:35 +00:00
lines=[]
if ids:
obj=self.browse(ids)[0]
lines=obj.match()
2015-02-11 07:51:21 +00:00
data={
'lines': lines,
}
return data
2015-02-20 10:59:49 +00:00
def onchange_invoice(self,context={}):
2015-02-18 18:50:15 +00:00
data=context['data']
path=context['path']
line=get_data_path(data,path,parent=True)
2015-02-20 10:59:49 +00:00
if line.get("invoice_id"):
2015-02-18 18:50:15 +00:00
line['state']='match'
else:
2015-02-20 10:59:49 +00:00
line['state']=''
2015-02-18 18:50:15 +00:00
return data
2015-02-20 10:59:49 +00:00
def clear_line(self,ids,context={}):
obj=self.browse(ids)[0]
line_ids=[line.id for line in obj.lines]
get_model("clinic.matching.payment.line").delete(line_ids)
print("Cleared %s"%(line_ids))
2015-02-21 13:38:21 +00:00
def approve(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'approved',
})
return {
'next': {
'name': 'clinic_matching_payment',
'mode': 'form',
'active_id': obj.id,
},
'flash': '%s has been approved'%obj.name,
}
def to_draft(self,ids,context={}):
for obj in self.browse(ids):
obj.write({
'state': 'draft',
})
2015-02-25 15:23:16 +00:00
def create(self,vals,**kw):
new_id=super().create(vals,**kw)
self.function_store([new_id])
return new_id
def write(self,ids,vals,**kw):
super().write(ids,vals,**kw)
self.function_store(ids)
2015-02-26 00:56:41 +00:00
def get_data(self,context={}):
data={}
return data
def print_invoice_unmatch(self,ids,context={}):
obj=self.browse(ids)[0]
return {
'next':{
'name': 'clinic_matching_payment',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'TODO',
}
2015-02-25 14:14:57 +00:00
2015-01-16 11:19:49 +00:00
MatchingPayment.register()