clinic/netforce_clinic/models/import_payment.py

208 lines
6.9 KiB
Python
Raw Normal View History

2014-10-04 15:51:54 +00:00
import time
2014-12-03 02:16:05 +00:00
from datetime import datetime, timedelta
from calendar import monthrange
2014-10-04 15:51:54 +00:00
from netforce.model import Model, fields, get_model
from netforce.utils import get_file_path
2014-12-02 11:41:05 +00:00
from . import utils
2014-11-30 13:05:14 +00:00
2014-10-04 15:51:54 +00:00
class ImportPayment(Model):
2014-12-03 00:05:18 +00:00
_name="import.clinic.payment"
_string="Clinic Payment"
def _get_name(self,ids,context={}):
res={}
for obj in self.browse(ids):
res[obj.id]=obj.type_id.name
return res
2014-10-27 14:17:22 +00:00
2014-10-04 15:51:54 +00:00
_fields={
2014-12-03 00:05:18 +00:00
'name': fields.Char("Name",function="_get_name"),
2014-12-03 02:16:05 +00:00
'type_id': fields.Many2One("clinic.patient.type","Patient Type",required=True),
'hcode_id': fields.Many2One("clinic.hospital", "Hospital",required=True),
2014-12-03 00:05:18 +00:00
'date': fields.Date("Date"),
2014-12-03 02:16:05 +00:00
'date_from': fields.Date("From"),
'date_to': fields.Date("To"),
2014-10-05 17:00:16 +00:00
'file': fields.File("File"),
2014-12-03 00:05:18 +00:00
'max_row': fields.Integer("Max Row"),
'remain_row': fields.Integer("Pending"),
2014-12-03 03:31:05 +00:00
'total_row': fields.Integer("Total Record"),
2014-12-03 02:16:05 +00:00
'est_time': fields.Float("Estimate Time",scale=4),
2014-12-03 00:05:18 +00:00
'msg': fields.Text("Message"),
'done_qty': fields.Integer("Success"),
'fail_qty': fields.Integer("Fail"),
2014-12-03 02:16:05 +00:00
'match_qty': fields.Integer("Match"),
'unmatch_qty': fields.Integer("UnMatch"),
'state': fields.Selection([['draft','Draft'],['confirmed','Confirmed'],['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","UnMatch",domain=[["state","=","unmatch"]]),
2014-12-03 04:48:20 +00:00
'payment_id': fields.Many2One("account.payment","Payment"),
2014-10-04 15:51:54 +00:00
}
2014-10-22 03:45:23 +00:00
2014-12-03 00:05:18 +00:00
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
2014-12-03 02:16:05 +00:00
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)
2014-10-27 14:17:22 +00:00
2014-10-04 15:51:54 +00:00
_defaults={
2014-12-03 02:16:05 +00:00
'date': lambda *a: time.strftime("%Y-%m-%d"),
'date_from': _get_date_from,
'date_to': _get_date_to,
2014-12-03 00:05:18 +00:00
'hcode_id': get_hcode_id,
'max_row': 50,
'state': 'draft',
2014-10-04 15:51:54 +00:00
}
2014-10-24 04:24:33 +00:00
2014-12-03 00:05:18 +00:00
def import_payment(self,ids,context={}):
2014-12-03 02:16:05 +00:00
fmt='%Y-%m-%d %H:%M:%S'
start_time=time.strftime(fmt)
2014-10-05 17:00:16 +00:00
obj=self.browse(ids)[0]
2014-10-22 03:45:23 +00:00
fname=obj.file
fpath=get_file_path(fname)
2014-12-03 04:48:20 +00:00
patient_type=obj.type_id
if patient_type.code in ('PKS'):
lines=utils.read_excel(fpath,show_datetime=False)
else:
lines=utils.read_xml(fpath,show_datetime=False)
2014-10-24 08:08:56 +00:00
if not lines:
raise Exception("Wrong File")
2014-12-03 00:05:18 +00:00
msg=""
count=0
nofound=0
did=0
blank=0
fail_qty=0
done_qty=0
2014-12-03 02:16:05 +00:00
match_qty=0
unmatch_qty=0
2014-12-03 00:05:18 +00:00
msg+=""*10; msg+="hcode,hn,name\n"
2014-12-03 04:48:20 +00:00
print("getting invoice")
2014-12-03 02:16:05 +00:00
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,
}
2014-12-03 04:48:20 +00:00
print("getting patient")
2014-12-03 02:16:05 +00:00
patients={}
for pt in get_model("clinic.patient").search_read([[]],['hn','hn_num']):
key=pt['hn_num']
patients[key]={
'id': pt['id'],
}
2014-12-03 04:48:20 +00:00
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'],
}
2014-12-03 02:16:05 +00:00
def get_hn_num(hn=""):
return ''.join(h for h in hn if h.isdigit())
mlines=[]
umlines=[]
2014-11-30 13:05:14 +00:00
for line in lines:
2014-12-03 00:05:18 +00:00
name=line.get("name14")
hn=line.get('hn',"")
2014-12-03 02:16:05 +00:00
hn_num=get_hn_num(hn)
2014-12-03 00:05:18 +00:00
hct=line.get("hct","")
2014-12-03 02:16:05 +00:00
inv_date=line.get("dttran")
2014-12-03 04:48:20 +00:00
amount=line.get('amount23')
2014-11-30 13:05:14 +00:00
hcode=line.get('hcode18','0')
if not hcode:
hcode='0'
hcode=int(hcode)
hcode=str(hcode)
2014-12-03 00:05:18 +00:00
if not obj.hcode_id.code==hcode:
if hcode:
nofound+=1
if hcode!='0':
msg+="not found %s, %s, %s \n"%(hcode,hn,name)
fail_qty+=1
2014-12-02 11:41:05 +00:00
else:
2014-12-03 00:05:18 +00:00
blank+=1
continue
2014-12-03 02:16:05 +00:00
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,
2014-12-03 04:48:20 +00:00
'amount': inv['amount_due'] or 0.0,
2014-12-03 02:16:05 +00:00
'state': 'match',
}))
match_qty+=1
else:
2014-12-03 04:48:20 +00:00
hd_case=hd_cases.get(key)
vals={
2014-12-03 02:16:05 +00:00
'date': inv_date,
'patient_id': patient_id,
2014-12-03 04:48:20 +00:00
'amount': amount,
}
if hd_case:
vals['hd_case_id']=hd_case['id']
umlines.append(('create',vals))
2014-12-03 02:16:05 +00:00
unmatch_qty+=1
2014-12-03 03:31:05 +00:00
2014-12-03 02:16:05 +00:00
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)
2014-12-03 00:05:18 +00:00
remain_row=len(lines)-blank-did-count
if remain_row <= 0:
msg="Nothing to import"
2014-10-27 19:01:18 +00:00
obj.write({
2014-12-03 00:05:18 +00:00
'total_row': len(lines),
2014-12-03 02:16:05 +00:00
'remain_row': remain_row-match_qty,
2014-12-03 00:05:18 +00:00
'msg': msg,
2014-12-03 03:31:05 +00:00
'done_qty': match_qty+unmatch_qty,
2014-12-03 00:05:18 +00:00
'fail_qty': fail_qty,
2014-12-03 02:16:05 +00:00
'match_qty': match_qty,
'unmatch_qty': unmatch_qty,
'est_time': est_time.seconds/3600,
'match_lines': mlines,
'unmatch_lines': umlines,
2014-10-27 19:01:18 +00:00
})
2014-12-03 00:05:18 +00:00
print("Done!")
2014-10-27 19:01:18 +00:00
2014-12-03 02:16:05 +00:00
2014-10-04 15:51:54 +00:00
ImportPayment.register()