2014-12-02 07:08:20 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
from netforce.model import Model, fields, get_model
|
|
|
|
from netforce.utils import get_file_path
|
|
|
|
from . import utils
|
|
|
|
|
|
|
|
class ImportHDCase(Model):
|
|
|
|
_name="clinic.import.hd.case"
|
|
|
|
_transient=True
|
|
|
|
|
|
|
|
_fields={
|
|
|
|
'date': fields.Date("Date"),
|
|
|
|
'file': fields.File("File"),
|
2014-12-03 00:05:18 +00:00
|
|
|
'hcode_id': fields.Many2One("clinic.hospital", "Hospital",required=True),
|
2014-12-02 07:08:20 +00:00
|
|
|
'max_row': fields.Integer("Max Row"),
|
2014-12-03 00:05:18 +00:00
|
|
|
'remain_row': fields.Integer("Import Pending"),
|
|
|
|
'total_row': fields.Integer("Total Case"),
|
|
|
|
'msg': fields.Text("Message"),
|
|
|
|
'done_qty': fields.Integer("Success"),
|
|
|
|
'fail_qty': fields.Integer("Fail"),
|
2014-12-02 07:08:20 +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-02 07:08:20 +00:00
|
|
|
|
|
|
|
_defaults={
|
|
|
|
'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"),
|
2014-12-03 00:05:18 +00:00
|
|
|
'hcode_id': get_hcode_id,
|
2014-12-02 08:34:28 +00:00
|
|
|
'max_row': 50,
|
2014-12-02 07:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def import_hd_case(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
fname=obj.file
|
|
|
|
fpath=get_file_path(fname)
|
|
|
|
lines=utils.read_excel(fpath,show_datetime=True)
|
|
|
|
if not lines:
|
|
|
|
raise Exception("Wrong File")
|
|
|
|
|
|
|
|
st_lines=[]
|
|
|
|
st=get_model("clinic.setting").browse(1)
|
|
|
|
for st_prod in st.products:
|
|
|
|
prod=st_prod.product_id
|
|
|
|
if st_prod.patient_type=='sc':
|
|
|
|
st_lines.append(('create',{
|
|
|
|
'type': st_prod.type,
|
|
|
|
'product_id': prod.id,
|
|
|
|
'uom_id': prod.uom_id.id,
|
|
|
|
'description': prod.name,
|
|
|
|
'qty': 1,
|
|
|
|
'price': st_prod.price,
|
|
|
|
'amount': st_prod.price,
|
|
|
|
}))
|
|
|
|
policy=''
|
|
|
|
option=''
|
|
|
|
for st_policy in st.invoice_policies:
|
|
|
|
if st_policy=='sc':
|
|
|
|
policy=st_policy.invoice_policy
|
|
|
|
option=st_policy.invoice_option
|
2014-12-03 00:05:18 +00:00
|
|
|
msg=""
|
2014-12-02 07:08:20 +00:00
|
|
|
max_row=obj.max_row
|
|
|
|
count=0
|
2014-12-03 00:05:18 +00:00
|
|
|
nofound=0
|
|
|
|
did=0
|
|
|
|
blank=0
|
|
|
|
fail_qty=0
|
|
|
|
done_qty=0
|
2014-12-02 07:08:20 +00:00
|
|
|
for line in lines:
|
|
|
|
name=line.get("name14")
|
|
|
|
hn=line.get('hn',"")
|
|
|
|
hct=line.get("hct","")
|
|
|
|
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
|
|
|
|
else:
|
|
|
|
blank+=1
|
2014-12-02 07:08:20 +00:00
|
|
|
continue
|
|
|
|
dttran=line.get("dttran","")
|
|
|
|
date=dttran[0:10]
|
|
|
|
pt_ids=get_model("clinic.patient").search([['name','=',name]])
|
|
|
|
if not pt_ids:
|
|
|
|
pt_ids=get_model("clinic.patient").search([['hn','=',hn]])
|
|
|
|
|
|
|
|
if count > max_row:
|
|
|
|
#XXX timeout
|
|
|
|
break
|
|
|
|
|
|
|
|
hlines=[]
|
|
|
|
if pt_ids:
|
|
|
|
prod_price=line.get("epopay27") # XXX
|
|
|
|
prod_code=line.get("code31")
|
|
|
|
prods=get_model("product").search_browse([['code','=',prod_code]])
|
|
|
|
if prods:
|
|
|
|
prod=prods[0]
|
|
|
|
hlines=[('create',{
|
|
|
|
'type': 'medicine',
|
|
|
|
'product_id': prod.id,
|
|
|
|
'uom_id': prod.uom_id.id,
|
|
|
|
'description': prod.name,
|
|
|
|
'price': prod_price or 0.0,
|
|
|
|
'qty': 1,
|
|
|
|
'amount': prod_price or 0.0,
|
|
|
|
})]
|
|
|
|
|
|
|
|
patient_id=pt_ids[0]
|
|
|
|
visit_obj=get_model("clinic.visit")
|
|
|
|
visits=visit_obj.search_browse([['visit_date','=',date],['patient_id','=',patient_id],['state','=','draft']])
|
|
|
|
if visits:
|
|
|
|
count+=1
|
2014-12-03 00:05:18 +00:00
|
|
|
done_qty+=1
|
2014-12-02 07:08:20 +00:00
|
|
|
hlines+=st_lines
|
|
|
|
visit=visits[0]
|
|
|
|
cycle=visit.cycle_id
|
|
|
|
vals={
|
|
|
|
'patient_id': patient_id,
|
|
|
|
'hct': hct or 0,
|
|
|
|
'invoice_policy': policy,
|
|
|
|
'invoice_option': option,
|
|
|
|
'date': visit.visit_date,
|
|
|
|
'time_start': visit.time_start,
|
|
|
|
'time_stop': visit.time_stop, # XXX
|
|
|
|
'visit_id': visit.id,
|
|
|
|
'cycle_id': cycle.id,
|
|
|
|
'lines': hlines,
|
|
|
|
}
|
|
|
|
hd_case_obj=get_model("clinic.hd.case")
|
|
|
|
hd_case_id=hd_case_obj.create(vals)
|
|
|
|
visit.confirm()
|
|
|
|
hd_case=hd_case_obj.browse(hd_case_id)
|
|
|
|
hd_case.done()
|
|
|
|
for inv in hd_case.invoices:
|
|
|
|
inv.write({
|
|
|
|
'date': inv.due_date,
|
|
|
|
})
|
2014-12-03 00:05:18 +00:00
|
|
|
msg+="confirm visit %s\n"%(visit.visit_date)
|
2014-12-02 07:08:20 +00:00
|
|
|
else:
|
2014-12-03 00:05:18 +00:00
|
|
|
did+=1
|
|
|
|
#print("not found visit for %s on %s"%(name,date))
|
2014-12-02 07:08:20 +00:00
|
|
|
else:
|
|
|
|
pass
|
2014-12-03 00:05:18 +00:00
|
|
|
remain_row=len(lines)-blank-did-count
|
|
|
|
if remain_row <= 0:
|
|
|
|
msg="Nothing to import"
|
2014-12-03 04:48:20 +00:00
|
|
|
final_msg=''
|
|
|
|
total_row=len(lines)
|
|
|
|
if done_qty <=0:
|
|
|
|
final_msg="Please import visit first"
|
|
|
|
total_row=0
|
|
|
|
done_qty=0
|
|
|
|
fail_qty=0
|
|
|
|
remain_row=0
|
|
|
|
else:
|
|
|
|
final_msg="Done!"
|
2014-12-02 08:34:28 +00:00
|
|
|
obj.write({
|
2014-12-03 04:48:20 +00:00
|
|
|
'total_row': total_row,
|
2014-12-03 00:05:18 +00:00
|
|
|
'remain_row': remain_row,
|
|
|
|
'msg': msg,
|
2014-12-03 04:48:20 +00:00
|
|
|
'done_qty': done_qty and done_qty-1 or 0,
|
2014-12-03 00:05:18 +00:00
|
|
|
'fail_qty': fail_qty,
|
2014-12-02 08:34:28 +00:00
|
|
|
})
|
|
|
|
print("Done!")
|
2014-12-03 04:48:20 +00:00
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'import_clinic_hd_case',
|
|
|
|
'mode': 'form',
|
|
|
|
'active_id': obj.id,
|
|
|
|
},
|
|
|
|
'flash': final_msg,
|
|
|
|
}
|
2014-12-02 08:34:28 +00:00
|
|
|
|
2014-12-02 07:08:20 +00:00
|
|
|
ImportHDCase.register()
|