377 lines
12 KiB
Python
377 lines
12 KiB
Python
import time
|
|
from datetime import datetime
|
|
from calendar import monthrange
|
|
|
|
from netforce.model import Model, fields, get_model
|
|
from netforce.access import get_active_company
|
|
from netforce.utils import get_file_path
|
|
from . import utils
|
|
|
|
class ImportPayment(Model):
|
|
_name="import.clinic.payment"
|
|
_string="Clinic Payment"
|
|
_multi_company=True
|
|
|
|
def _get_name(self,ids,context={}):
|
|
res={}
|
|
for obj in self.browse(ids):
|
|
res[obj.id]=obj.type_id.name
|
|
return res
|
|
|
|
def _get_partner(self,ids,context={}):
|
|
res={}
|
|
for obj in self.browse(ids):
|
|
ptype=obj.type_id
|
|
contact_id=None
|
|
if ptype:
|
|
contact=ptype.contact_id
|
|
if contact:
|
|
contact_id=contact.id
|
|
res[obj.id]=contact_id
|
|
return res
|
|
|
|
_fields={
|
|
'name': fields.Char("Name",function="_get_name"),
|
|
'type_id': fields.Many2One("clinic.patient.type","Patient Type",required=True),
|
|
'hcode_id': fields.Many2One("clinic.hospital", "Hospital",required=True),
|
|
'date': fields.Date("Date"),
|
|
'date_from': fields.Date("From"),
|
|
'date_to': fields.Date("To"),
|
|
'file': fields.File("File"),
|
|
'max_row': fields.Integer("Max Row"),
|
|
'remain_row': fields.Integer("Pending"),
|
|
'total_row': fields.Integer("Total"),
|
|
'est_time': fields.Float("Estimate Time",scale=4),
|
|
'msg': fields.Text("Message"),
|
|
'done_qty': fields.Integer("Success"),
|
|
'fail_qty': fields.Integer("Other"),
|
|
'match_qty': fields.Integer("Match"),
|
|
'unmatch_qty': fields.Integer("UnMatch"),
|
|
'state': fields.Selection([['draft','Draft'],['confirmed','Confirmed'],['approved','Approved'],['fail','Fail'],['success','Success']],'State'),
|
|
'match_lines': fields.One2Many("import.clinic.payment.line","import_payment_id","Match",domain=[["state","=","match"]]),
|
|
'unmatch_lines': fields.One2Many("import.clinic.payment.line","import_payment_id","Un Match",domain=[["state","=","unmatch"]]),
|
|
'payment_id': fields.Many2One("account.payment","Payment"),
|
|
'company_id': fields.Many2One("company","Company"),
|
|
'partner_id': fields.Many2One("partner","Fee Contact",function="_get_partner"),
|
|
'is_uc': fields.Integer("Is UC"),
|
|
'node': fields.Char("Node"),
|
|
}
|
|
|
|
def get_hcode_id(self,context={}):
|
|
hp_ids=get_model("clinic.hospital").search([])
|
|
hp_id=None
|
|
if hp_ids:
|
|
hp_id=hp_ids[0]
|
|
return hp_id
|
|
|
|
def _get_date_from(self,context={}):
|
|
datenow=time.strftime("%Y-%m-%d")
|
|
year,month,day=datenow.split("-")
|
|
return '%s-%s-01'%(year,month)
|
|
|
|
def _get_date_to(self,context={}):
|
|
datenow=datetime.now().strftime("%Y-%m-%d")
|
|
year,month,day=datenow.split("-")
|
|
weekday, total_day=monthrange(int(year), int(month))
|
|
return '%s-%s-%s'%(year,month,total_day)
|
|
|
|
def _get_patient_type(self,context={}):
|
|
st=get_model('clinic.setting').browse(1)
|
|
ptype=st.patient_type_id
|
|
ptype_id=None
|
|
if ptype:
|
|
ptype_id=ptype.id
|
|
return ptype_id
|
|
|
|
_defaults={
|
|
'date': lambda *a: time.strftime("%Y-%m-%d"),
|
|
'date_from': _get_date_from,
|
|
'date_to': _get_date_to,
|
|
'hcode_id': get_hcode_id,
|
|
'company_id': lambda *a: get_active_company(),
|
|
'type_id': _get_patient_type,
|
|
'max_row': 50,
|
|
'state': 'draft',
|
|
'is_uc': 0,
|
|
'node': 'HDBills',
|
|
}
|
|
|
|
def import_payment_pks(self,ids,context={}):
|
|
fmt='%Y-%m-%d %H:%M:%S'
|
|
start_time=time.strftime(fmt)
|
|
obj=self.browse(ids)[0]
|
|
fname=obj.file
|
|
fpath=get_file_path(fname)
|
|
lines=utils.read_excel(fpath,show_datetime=False)
|
|
if not lines:
|
|
raise Exception("No data to import")
|
|
msg=""
|
|
nofound=0
|
|
blank=0
|
|
fail_qty=0
|
|
match_qty=0
|
|
unmatch_qty=0
|
|
msg+=""*10; msg+="hcode,hn,name,note\n"
|
|
print("getting invoice")
|
|
dom=[]
|
|
dom.append(['state','=','waiting_payment'])
|
|
invoices={}
|
|
for inv in get_model("account.invoice").search_browse(dom):
|
|
hd_case=inv.related_id
|
|
patient=hd_case.patient_id
|
|
key=(inv.date,patient.id)
|
|
invoices[key]={
|
|
'id': inv.id,
|
|
'amount_due': inv.amount_due,
|
|
'hd_case_id': hd_case.id,
|
|
}
|
|
|
|
print("getting patient")
|
|
patients={}
|
|
for pt in get_model("clinic.patient").search_read([[]],['hn','hn_num']):
|
|
key=pt['hn_num']
|
|
patients[key]={
|
|
'id': pt['id'],
|
|
}
|
|
|
|
print("getting hd case")
|
|
# need to optimize -> from, to
|
|
hd_cases={}
|
|
for hd_case in get_model("clinic.hd.case").search_read([[]],['patient_id','date']):
|
|
patient_id=hd_case['patient_id'][0]
|
|
date=hd_case['date']
|
|
key=(date,patient_id)
|
|
hd_cases[key]={
|
|
'id': hd_case['id'],
|
|
}
|
|
|
|
def get_hn_num(hn=""):
|
|
return ''.join(h for h in hn if h.isdigit())
|
|
|
|
mlines=[]
|
|
umlines=[]
|
|
for line in lines:
|
|
patient_name=line.get("name14")
|
|
hn=line.get('hn',"")
|
|
hn_num=get_hn_num(hn)
|
|
#hct=line.get("hct","")
|
|
inv_date=line.get("dttran")
|
|
#amount=line.get('amount23')
|
|
hcode=line.get('hcode18','0')
|
|
if not hcode:
|
|
hcode='0'
|
|
hcode=int(hcode)
|
|
hcode=str(hcode)
|
|
if not obj.hcode_id.code==hcode:
|
|
if hcode:
|
|
nofound+=1
|
|
if hcode!='0':
|
|
msg+="%s, %s, %s, Wrong hospital code \n"%(hcode,hn,patient_name)
|
|
fail_qty+=1
|
|
else:
|
|
blank+=1
|
|
else:
|
|
blank+=1
|
|
continue
|
|
|
|
if patients.get(hn_num):
|
|
patient_id=patients[hn_num]['id']
|
|
key=(inv_date,patient_id)
|
|
if invoices.get(key):
|
|
inv=invoices[key]
|
|
mlines.append(('create',{
|
|
'date': inv_date,
|
|
'invoice_id': inv['id'],
|
|
'hd_case_id': inv['hd_case_id'],
|
|
'patient_id': patient_id,
|
|
'amount': inv['amount_due'] or 0.0,
|
|
'state': 'match',
|
|
}))
|
|
match_qty+=1
|
|
else:
|
|
hd_case=hd_cases.get(key)
|
|
vals={
|
|
'date': inv_date,
|
|
'patient_id': patient_id,
|
|
}
|
|
if hd_case:
|
|
vals['hd_case_id']=hd_case['id']
|
|
umlines.append(('create',vals))
|
|
unmatch_qty+=1
|
|
else:
|
|
msg+="%s,%s,%s,Not found invoice on %s\n"%(hcode,hn,patient_name,inv_date)
|
|
nofound+=1
|
|
|
|
unmatch_qty+=nofound #XXX
|
|
for mline in obj.match_lines:
|
|
mline.delete()
|
|
for umline in obj.unmatch_lines:
|
|
umline.delete()
|
|
|
|
stop_time=time.strftime(fmt)
|
|
est_time=datetime.strptime(stop_time,fmt)-datetime.strptime(start_time,fmt)
|
|
|
|
total_row=len(lines)
|
|
remain_row=total_row-blank-match_qty-unmatch_qty
|
|
obj.write({
|
|
'total_row': len(lines),
|
|
'remain_row': remain_row-match_qty,
|
|
'msg': msg,
|
|
'done_qty': match_qty+unmatch_qty,
|
|
'fail_qty': fail_qty,
|
|
'match_qty': match_qty,
|
|
'unmatch_qty': unmatch_qty-fail_qty,
|
|
'est_time': est_time.seconds/3600,
|
|
'match_lines': mlines,
|
|
'unmatch_lines': umlines,
|
|
})
|
|
print("Done!")
|
|
return {
|
|
'next': {
|
|
'name': 'import_clinic_payment',
|
|
'mode': 'form',
|
|
'active_id': obj.id,
|
|
},
|
|
'flash': 'Import successfully'
|
|
}
|
|
|
|
def import_payment_uc(self,ids,context={}):
|
|
obj=self.browse(ids)[0]
|
|
fname=obj.file
|
|
fpath=get_file_path(fname)
|
|
lines=utils.read_xml(fpath,node=obj.node)
|
|
if not lines:
|
|
raise Exception("No Data to import")
|
|
total=0.0
|
|
qty=0
|
|
import pprint
|
|
for line in lines:
|
|
pprint.pprint(line)
|
|
date,time=line.get("dttran").split("T")
|
|
invno=line.get("invno")
|
|
hdrate=float(line.get("hdrate","0"))
|
|
hn=line.get("hn")
|
|
paid=float(line.get("paid","0"))
|
|
epostat=line.get('epostat')
|
|
total+=hdrate
|
|
qty+=1
|
|
|
|
print('Total: ', total)
|
|
print("Qty: ", qty)
|
|
obj.write({
|
|
'match_qty': 0,
|
|
'unmatch_qty': 0,
|
|
})
|
|
return {
|
|
'next': {
|
|
'name': 'import_clinic_payment',
|
|
'mode': 'form',
|
|
'active_id': obj.id,
|
|
},
|
|
'flash': 'Import succeffully',
|
|
}
|
|
|
|
def import_payment(self,ids,context={}):
|
|
obj=self.browse(ids)[0]
|
|
fname=obj.file
|
|
if not fname:
|
|
raise Exception("File not found")
|
|
patient_type=obj.type_id
|
|
res={}
|
|
if patient_type.code in ('PKS','SC'):
|
|
res=self.import_payment_pks(ids,context)
|
|
elif patient_type.code in ('UC','uc','UC.','uc.'):
|
|
res=self.import_payment_uc(ids,context)
|
|
else:
|
|
raise Exception("No script to import payment for type %s"%patient_type.name)
|
|
return res
|
|
|
|
def approve(self,ids,context={}):
|
|
obj=self.browse(ids)[0]
|
|
partner=obj.partner_id
|
|
if not partner:
|
|
raise Exception("No contact on this patient")
|
|
company_id=get_active_company()
|
|
account_receivable_id=partner.account_receivable_id
|
|
if not account_receivable_id:
|
|
st=get_model('settings').browse(1)
|
|
account_receivable_id=st.account_receivable_id
|
|
if not account_receivable_id:
|
|
raise Exception("Not found account recieveable in account setting")
|
|
account_id=account_receivable_id.id
|
|
datenow=time.strftime("%Y-%m-%d")
|
|
vals={
|
|
"partner_id": partner.id,
|
|
"company_id": company_id,
|
|
"type": "in",
|
|
"pay_type": "invoice",
|
|
'date': datenow,
|
|
"account_id": account_id,
|
|
'related_id': "import.clinic.payment,%s"%obj.id,
|
|
'invoice_lines': [],
|
|
}
|
|
|
|
for mline in obj.match_lines:
|
|
inv=mline.invoice_id
|
|
if inv:
|
|
amt=inv.amount_due or 0.0
|
|
vals['invoice_lines'].append(('create',{
|
|
'type': 'invoice',
|
|
'account_id': account_id,
|
|
'invoice_id': inv.id,
|
|
'amount': amt,
|
|
}))
|
|
|
|
for umline in obj.unmatch_lines:
|
|
inv=mline.invoice_id
|
|
if inv:
|
|
amt=inv.amount_due or 0.0
|
|
vals['invoice_lines'].append(('create',{
|
|
'type': 'invoice',
|
|
'account_id': account_id,
|
|
'invoice_id': inv.id,
|
|
'amount': amt,
|
|
}))
|
|
|
|
payment=obj.payment_id
|
|
if payment:
|
|
for inv_line in payment.invoice_lines:
|
|
inv_line.delete()
|
|
payment.write(vals)
|
|
else:
|
|
payment_id=get_model("account.payment").create(vals,context={"type":"in"})
|
|
vals['payment_id']=payment_id
|
|
obj.write({
|
|
'payment_id': payment_id,
|
|
'state': 'approved',
|
|
})
|
|
|
|
#payment=get_model('account.payment').browse(payment_id)
|
|
#payment.post()
|
|
return {
|
|
'next': {
|
|
'name': 'import_clinic_payment',
|
|
'mode': 'form',
|
|
'active_id': obj.id,
|
|
},
|
|
'flash': '%s has been paid'%obj.type_id.name,
|
|
}
|
|
|
|
def to_draft(self,ids,context={}):
|
|
obj=self.browse(ids)[0]
|
|
obj.write({
|
|
'state': 'draft',
|
|
})
|
|
|
|
def onchange_type(self,context={}):
|
|
data=context['data']
|
|
is_uc=0
|
|
type_id=data['type_id']
|
|
ptype=get_model('clinic.patient.type').browse(type_id)
|
|
if ptype.code=='UC':
|
|
is_uc=1
|
|
data['is_uc']=is_uc
|
|
return data
|
|
|
|
ImportPayment.register()
|