clinic/netforce_clinic/models/visit.py

200 lines
7.3 KiB
Python
Raw Normal View History

2014-10-02 00:34:58 +00:00
import time
2014-10-02 02:02:22 +00:00
import datetime
2014-10-02 00:34:58 +00:00
2014-08-19 11:36:46 +00:00
from netforce.model import Model, fields, get_model
2014-10-02 19:12:52 +00:00
from netforce.access import get_active_company, get_active_user, set_active_user
2014-08-19 11:36:46 +00:00
class Visit(Model):
_name="clinic.visit"
_string="Visit"
_audit_log=True
_name_field="number"
_multi_company=True
_fields={
"number": fields.Char("Number",required=True,search=True),
2014-10-01 10:52:21 +00:00
"patient_id": fields.Many2One("clinic.patient","Patient",required=True,search=True),
2014-08-19 11:36:46 +00:00
"doctor_id": fields.Many2One("clinic.doctor","Doctor",search=True),
"nurse_id": fields.Many2One("clinic.nurse","Nurse",search=True),
2014-10-01 10:52:21 +00:00
"visit_date": fields.Date("Visit Date",required=True,search=True),
2014-10-02 00:34:58 +00:00
"department_id": fields.Many2One("clinic.department", "Department",search=True),
2014-09-11 03:21:52 +00:00
"cycle": fields.Selection([("1","One"),("2","Two"),("3","Tree"),("4","Four")],"Cycle",required=True),
2014-10-02 00:34:58 +00:00
"state": fields.Selection([("pending","Pending"),("confirmed","Confirmed"),("cancelled","Cancelled")],"Status",required=True),
2014-08-19 11:36:46 +00:00
"comments": fields.One2Many("message","related_id","Comments"),
"company_id": fields.Many2One("company","Company"),
2014-10-02 19:12:52 +00:00
'time_use': fields.Integer("Time Use(hrs)"),
2014-08-19 11:36:46 +00:00
}
def _get_number(self,context={}):
while 1:
2014-10-02 19:12:52 +00:00
seq_id=get_model("sequence").find_sequence(name="Clinic Visit")
num=get_model("sequence").get_next_number(seq_id,context=context)
2014-08-19 11:36:46 +00:00
if not num:
return None
2014-10-02 19:12:52 +00:00
user_id=get_active_user()
set_active_user(1)
2014-08-19 11:36:46 +00:00
res=self.search([["number","=",num]])
2014-10-02 19:12:52 +00:00
set_active_user(user_id)
2014-08-19 11:36:46 +00:00
if not res:
return num
2014-10-02 19:12:52 +00:00
get_model("sequence").increment_number(seq_id,context=context)
2014-08-19 11:36:46 +00:00
2014-10-02 00:34:58 +00:00
def _get_nurse(self,context={}):
user_id=get_active_user()
print("user_id ",user_id)
nurse_ids=get_model("clinic.nurse").search([['user_id','=',user_id]])
if nurse_ids:
return nurse_ids[0]
return None
2014-08-19 11:36:46 +00:00
_defaults={
2014-09-11 03:21:52 +00:00
"state": "pending",
2014-10-02 00:34:58 +00:00
"cycle": "1",
'visit_date': time.strftime("%Y-%m-%d"),
2014-08-19 11:36:46 +00:00
"number": _get_number,
"company_id": lambda *a: get_active_company(),
2014-10-02 00:34:58 +00:00
'nurse_id': _get_nurse,
2014-10-02 19:12:52 +00:00
'time_use': 1,
2014-08-19 11:36:46 +00:00
}
2014-10-03 07:27:57 +00:00
_order="number desc"
2014-08-19 11:36:46 +00:00
2014-10-02 19:12:52 +00:00
def get_dialyzer(self):
return
2014-08-19 11:36:46 +00:00
2014-10-02 00:34:58 +00:00
def confirm(self,ids,context={}):
2014-09-11 03:21:52 +00:00
obj=self.browse(ids)[0]
2014-10-02 02:02:22 +00:00
hd_case_obj=get_model("clinic.hd.case")
dt=datetime.datetime
fmt_date="%Y-%m-%d %H:%M:%S"
timenow=dt.now().strftime("%H:%M:%S")
date_from=dt.strptime("%s %s"%(obj.visit_date,timenow),fmt_date)
2014-10-02 19:12:52 +00:00
seconds=(obj.time_use or 1)*3600
date_to=date_from+datetime.timedelta(seconds=seconds)
2014-10-02 02:02:22 +00:00
date_to=date_to.strftime(fmt_date)
date_from=date_from.strftime(fmt_date)
2014-10-02 19:12:52 +00:00
include_fee=obj.patient_id.type in ('mg','sc','nhso') and True or False
vals={
2014-10-02 02:02:22 +00:00
'patient_id': obj.patient_id.id,
'doctor_id': obj.doctor_id.id,
'nurse_id': obj.nurse_id.id,
'department_id': obj.department_id.id,
'date_start': date_from,
'date_stop': date_to,
'visit_id': obj.id,
2014-10-02 19:12:52 +00:00
'fee': include_fee and 1500.00 or 0.0,
'fee_type': obj.patient_id.type,
'lines':[],
'dialyzers': [],
}
patient_type={
"mg":"Medical Government",
"sc":"Social Security",
"nhso":"NHSO (30฿)",
"personal": "Personal",
"others": "Others",
}
categ_name=patient_type.get(obj.patient_id.type)
categ_ids=get_model("partner.categ").search([['name','=',categ_name]])
if not categ_ids:
raise Exception("Category: %s not found"%categ_name)
partner_id=None
if obj.patient_id.type in ("mg","sc","nhso"):
partner_obj=get_model("partner")
for partner in partner_obj.search_browse([]):
if partner.categ_id.id in categ_ids:
partner_id=partner.id
vals['fee_partner_id']=partner_id
break
# find dialyser
2014-10-03 07:27:57 +00:00
for dlz in get_model("clinic.dialyzer").search_browse([['patient_id','=',obj.patient_id.id],['state','=','active']],order="id desc"):
use_time=dlz.use_time or 0
use_time+=1
# XXX update dialyzer
dlz.write({
'use_time': use_time,
})
if use_time > dlz.max_use_time:
dlz.write({
"state": "expire",
})
continue
vals['dialyzers'].append(('create',{
"dialyzer_id": dlz.id,
2014-10-02 19:12:52 +00:00
"description": dlz.description,
2014-10-03 07:27:57 +00:00
"use_time": use_time,
2014-10-02 19:12:52 +00:00
"max_use_time": dlz.max_use_time,
"member_type": dlz.member_type,
"dialyzer_type": dlz.dialyzer_type,
"bid_flow_rate": dlz.bid_flow_rate,
"ultrafittration": dlz.ultrafittration,
2014-10-03 07:27:57 +00:00
}))
if not vals['dialyzers']:
raise Exception("%s don't have dialyzer for treatment" % obj.patient_id.name)
hd_case_id=hd_case_obj.create(vals)
2014-09-11 03:21:52 +00:00
obj.write({"state":"confirmed"})
2014-10-02 02:02:22 +00:00
return {
'next': {
'name': 'clinic_hd_case',
'mode': 'form',
'active_id': hd_case_id,
},
2014-10-02 19:12:52 +00:00
'flash': 'Visit %s has been confirmed'%obj.number,
2014-10-02 02:02:22 +00:00
}
2014-08-19 11:36:46 +00:00
2014-10-02 00:34:58 +00:00
def cancel(self,ids,context={}):
2014-08-19 11:36:46 +00:00
obj=self.browse(ids)[0]
2014-10-02 00:34:58 +00:00
obj.write({"state":"cancelled"})
2014-08-19 11:36:46 +00:00
2014-10-02 02:02:22 +00:00
def reopen(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({"state":"pending"})
2014-10-02 00:34:58 +00:00
def onchange_patient(self,context={}):
data=context['data']
patient_id=data['patient_id']
2014-10-03 07:27:57 +00:00
visits=self.search_browse([['patient_id','=',patient_id]],order="number desc")
2014-10-02 00:34:58 +00:00
if visits:
2014-10-03 07:27:57 +00:00
#print([(v.id, v.visit_date,v.cycle) for v in visits])
visit=visits[0]
2014-10-02 00:34:58 +00:00
data['doctor_id']=visit.doctor_id.id
data['department_id']=visit.department_id.id
2014-10-03 07:27:57 +00:00
cycle=int(visit.cycle)+1
data['cycle']=str(cycle)
if cycle>4:
data['cycle']='1'
2014-10-02 00:34:58 +00:00
else:
2014-10-03 06:09:10 +00:00
data['doctor_id']=None
2014-10-02 00:34:58 +00:00
data['department_id']=None
return data
2014-10-02 19:12:52 +00:00
def copy(self,ids,context={}):
obj=self.browse(ids[0])
vals={
'patient_id': obj.patient_id.id,
'doctor_id': obj.doctor_id.id,
'nurse_id': obj.nurse_id.id,
'department_id': obj.department_id.id,
}
2014-10-03 07:27:57 +00:00
# XXX update cycle
visits=self.search_browse([['patient_id','=',obj.patient_id.id]],order="number desc")
if visits:
visit=visits[0] # order desc already
cycle=int(visit.cycle)+1
vals['cycle']=str(cycle)
if cycle>4:
vals['cycle']='1'
2014-10-02 19:12:52 +00:00
new_id=self.create(vals,context=context)
new_obj=self.browse(new_id)
return {
'next':{
'name': 'clinic_visit',
'mode': 'form',
'active_id': new_id,
},
'flash': 'Visit %s is copy to %s'%(obj.number,new_obj.number),
}
2014-08-19 11:36:46 +00:00
Visit.register()