clinic/netforce_clinic/models/matching_payment.py

276 lines
9.2 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"),
}
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
def match(self,ids,context={}):
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={}
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")
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'%(
name,
date,
lfee_amt,
lsrv_amt,
lmdc_amt,
)
if not matches1.get(key1):
matches1[key1]=0
if not matches2.get(key2):
matches2[key2]=0
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'],
}
#print("="*50)
#for key, values in matches1.items():
#x=key.split("-")
#hn=x[0]
#if hn=='11712556':
#print(key)
#print("="*50)
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-04 19:02:44 +00:00
if obj.pcode=='UC':
pt=exp.patient_id
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)
#if pt.hn_no=='11712556':
#print('key ', key)
found=matches1.get(key)
if found:
exp.write({
'state': 'match',
})
2015-01-16 11:19:49 +00:00
obj.write({
'expenes': [('add',exp_ids)]
})
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-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': [],
}
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-04 14:10:41 +00:00
2015-01-16 11:19:49 +00:00
MatchingPayment.register()