clinic/netforce_clinic/models/import_payment.py

300 lines
9.5 KiB
Python
Raw Normal View History

2014-10-04 15:51:54 +00:00
import time
2014-12-03 17:46:50 +00:00
from datetime import datetime
2014-12-03 02:16:05 +00:00
from calendar import monthrange
2014-10-04 15:51:54 +00:00
from netforce.model import Model, fields, get_model
2014-12-03 09:11:41 +00:00
from netforce.access import get_active_company
2014-10-04 15:51:54 +00:00
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"
2014-12-03 09:11:41 +00:00
_multi_company=True
2014-12-03 00:05:18 +00:00
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-12-03 11:40:37 +00:00
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
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 11:40:37 +00:00
'total_row': fields.Integer("Total"),
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"),
2014-12-03 11:40:37 +00:00
'fail_qty': fields.Integer("Other"),
2014-12-03 02:16:05 +00:00
'match_qty': fields.Integer("Match"),
'unmatch_qty': fields.Integer("UnMatch"),
2014-12-03 11:40:37 +00:00
'state': fields.Selection([['draft','Draft'],['confirmed','Confirmed'],['approved','Approved'],['fail','Fail'],['success','Success']],'State'),
2014-12-03 04:48:20 +00:00
'payment_id': fields.Many2One("account.payment","Payment"),
2014-12-03 09:11:41 +00:00
'company_id': fields.Many2One("company","Company"),
2014-12-03 11:40:37 +00:00
'partner_id': fields.Many2One("partner","Fee Contact",function="_get_partner"),
2014-12-03 17:46:50 +00:00
'is_uc': fields.Integer("Is UC"),
'node': fields.Char("Node"),
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-12-03 11:40:37 +00:00
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
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,
2014-12-03 09:11:41 +00:00
'company_id': lambda *a: get_active_company(),
2014-12-03 11:40:37 +00:00
'type_id': _get_patient_type,
2014-12-03 00:05:18 +00:00
'max_row': 50,
'state': 'draft',
2014-12-03 17:46:50 +00:00
'is_uc': 0,
'node': 'HDBills',
2014-10-04 15:51:54 +00:00
}
2014-12-03 17:46:50 +00:00
2014-12-04 08:37:54 +00:00
def get_patient_invoice(self,state="waiting_payment"):
print("getting patient invoice")
2014-12-03 02:16:05 +00:00
dom=[]
2014-12-04 08:37:54 +00:00
dom.append(['state','=',state])
2014-12-03 02:16:05 +00:00
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-04 08:37:54 +00:00
return invoices
def get_all_patient(self):
2014-12-03 04:48:20 +00:00
print("getting patient")
2014-12-03 02:16:05 +00:00
patients={}
2014-12-04 08:37:54 +00:00
for pt in get_model("clinic.patient").search_read([[]],['name','hn','hn_num']):
2014-12-03 02:16:05 +00:00
key=pt['hn_num']
patients[key]={
'id': pt['id'],
2014-12-04 08:37:54 +00:00
'name': pt['name'],
2014-12-03 02:16:05 +00:00
}
2014-12-04 08:37:54 +00:00
return patients
2014-12-03 04:48:20 +00:00
2014-12-04 08:37:54 +00:00
def get_hd_case(self):
2014-12-03 04:48:20 +00:00
print("getting hd case")
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-04 08:37:54 +00:00
return hd_cases
2014-12-03 02:16:05 +00:00
2014-12-04 08:37:54 +00:00
def get_hn_num(self,hn=""):
return ''.join(h for h in hn if h.isdigit())
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=""
2014-11-30 13:05:14 +00:00
for line in lines:
2014-12-04 14:30:52 +00:00
pass
2014-12-03 03:31:05 +00:00
2014-10-27 19:01:18 +00:00
obj.write({
2014-12-03 00:05:18 +00:00
'total_row': len(lines),
'msg': msg,
2014-10-27 19:01:18 +00:00
})
2014-12-04 14:30:52 +00:00
2014-12-03 17:46:50 +00:00
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")
2014-12-04 08:37:54 +00:00
2014-12-03 17:46:50 +00:00
for line in lines:
date,time=line.get("dttran").split("T")
invno=line.get("invno")
hdrate=float(line.get("hdrate","0"))
2014-12-04 08:37:54 +00:00
hn=line.get('hn',"")
hn_num=self.get_hn_num(hn)
2014-12-03 17:46:50 +00:00
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
2014-10-27 19:01:18 +00:00
2014-12-03 09:11:41 +00:00
def approve(self,ids,context={}):
obj=self.browse(ids)[0]
2014-12-03 11:40:37 +00:00
partner=obj.partner_id
2014-12-03 09:11:41 +00:00
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")
2014-12-03 11:40:37 +00:00
account_id=account_receivable_id.id
datenow=time.strftime("%Y-%m-%d")
2014-12-03 09:11:41 +00:00
vals={
"partner_id": partner.id,
"company_id": company_id,
"type": "in",
"pay_type": "invoice",
2014-12-03 11:40:37 +00:00
'date': datenow,
2014-12-03 09:11:41 +00:00
"account_id": account_id,
2014-12-03 11:40:37 +00:00
'related_id': "import.clinic.payment,%s"%obj.id,
2014-12-03 09:11:41 +00:00
'invoice_lines': [],
}
2014-12-03 11:40:37 +00:00
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,
}))
2014-12-03 09:11:41 +00:00
2014-12-03 11:40:37 +00:00
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',
})
2014-12-03 09:11:41 +00:00
#payment=get_model('account.payment').browse(payment_id)
#payment.post()
return {
'next': {
'name': 'import_clinic_payment',
'mode': 'form',
'active_id': obj.id,
},
2014-12-03 11:40:37 +00:00
'flash': '%s has been paid'%obj.type_id.name,
2014-12-03 09:11:41 +00:00
}
2014-12-03 17:46:50 +00:00
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
2014-12-03 02:16:05 +00:00
2014-12-04 08:37:54 +00:00
2014-10-04 15:51:54 +00:00
ImportPayment.register()