2014-10-02 00:34:58 +00:00
|
|
|
import time
|
2014-10-14 03:57:20 +00:00
|
|
|
from datetime import datetime, timedelta
|
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-10-14 03:57:20 +00:00
|
|
|
#from netforce.utils import get_data_path
|
|
|
|
|
|
|
|
FMT_DATE="%Y-%m-%d %H:%M:%S"
|
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
|
2014-10-04 15:51:54 +00:00
|
|
|
|
2014-08-19 11:36:46 +00:00
|
|
|
_fields={
|
|
|
|
"number": fields.Char("Number",required=True,search=True),
|
2014-10-14 13:48:14 +00:00
|
|
|
"time_start": fields.DateTime("Time Start",required=True),
|
|
|
|
"time_stop": fields.DateTime("Time Stop",required=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-02 00:34:58 +00:00
|
|
|
"department_id": fields.Many2One("clinic.department", "Department",search=True),
|
2014-08-19 11:36:46 +00:00
|
|
|
"comments": fields.One2Many("message","related_id","Comments"),
|
|
|
|
"company_id": fields.Many2One("company","Company"),
|
2014-10-05 08:47:12 +00:00
|
|
|
'hd_cases': fields.One2Many('clinic.hd.case','visit_id',"HD Cases",readonly=True),
|
2014-10-14 03:57:20 +00:00
|
|
|
"cycle_id": fields.Many2One("clinic.cycle","Cycle"),
|
2014-10-15 03:49:20 +00:00
|
|
|
"state": fields.Selection([["draft","Draft"],["confirmed","Confirmed"],["cancelled","Cancelled"]],"Status",required=True),
|
2014-10-14 03:57:20 +00:00
|
|
|
"comments": fields.One2Many("message","related_id","Comments"),
|
2014-08-19 11:36:46 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 08:10:20 +00:00
|
|
|
def _get_number( self,context={}):
|
2014-08-19 11:36:46 +00:00
|
|
|
while 1:
|
2014-10-13 08:10:20 +00:00
|
|
|
seq_name='Clinic Visit'
|
|
|
|
seq_id=get_model("sequence").find_sequence(name=seq_name)
|
|
|
|
if not seq_id:
|
|
|
|
raise Exception("Can not found sequence %s"%seq_name)
|
2014-10-02 19:12:52 +00:00
|
|
|
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]
|
2014-10-13 08:10:20 +00:00
|
|
|
return None
|
2014-08-19 11:36:46 +00:00
|
|
|
|
2014-10-05 05:47:19 +00:00
|
|
|
def _get_time_start(self,context={}):
|
2014-10-14 03:57:20 +00:00
|
|
|
now=datetime.now()
|
2014-10-05 05:47:19 +00:00
|
|
|
starttime=now.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
return starttime
|
|
|
|
|
|
|
|
def _get_time_stop(self,context={}):
|
2014-10-14 03:57:20 +00:00
|
|
|
hr=timedelta(seconds=3600)
|
|
|
|
now=datetime.now()
|
2014-10-05 05:47:19 +00:00
|
|
|
stoptime=(now+hr).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
return stoptime
|
|
|
|
|
2014-08-19 11:36:46 +00:00
|
|
|
_defaults={
|
2014-10-04 15:51:54 +00:00
|
|
|
"state": "draft",
|
2014-10-05 05:47:19 +00:00
|
|
|
'time_start': _get_time_start,
|
|
|
|
'time_stop': _get_time_stop,
|
2014-10-04 15:51:54 +00:00
|
|
|
"number": "/",
|
2014-08-19 11:36:46 +00:00
|
|
|
"company_id": lambda *a: get_active_company(),
|
2014-10-02 00:34:58 +00:00
|
|
|
'nurse_id': _get_nurse,
|
2014-08-19 11:36:46 +00:00
|
|
|
}
|
2014-10-04 15:51:54 +00:00
|
|
|
_order="id 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-10-04 15:51:54 +00:00
|
|
|
obj=self.browse(ids[0])
|
|
|
|
number=obj.number
|
|
|
|
if number=="/":
|
|
|
|
number=self._get_number(context)
|
|
|
|
vals={
|
|
|
|
'number': number,
|
2014-10-15 03:49:20 +00:00
|
|
|
'state': 'confirmed',
|
2014-10-04 15:51:54 +00:00
|
|
|
}
|
|
|
|
obj.write(vals)
|
2014-10-02 02:02:22 +00:00
|
|
|
hd_case_obj=get_model("clinic.hd.case")
|
2014-10-15 03:49:20 +00:00
|
|
|
#dt=datetime
|
|
|
|
#datenow=dt.now().strftime("%Y-%m-%d")
|
2014-10-04 15:51:54 +00:00
|
|
|
obj=self.browse(ids)[0]
|
2014-10-15 03:49:20 +00:00
|
|
|
#date_visit=obj.time_start[0:10]
|
2014-10-04 15:51:54 +00:00
|
|
|
# cannot treatment if current date is not same visit date
|
2014-10-15 03:49:20 +00:00
|
|
|
#if date_visit!=datenow:
|
|
|
|
#raise Exception("Today is not treament date!")
|
2014-10-04 15:51:54 +00:00
|
|
|
|
2014-10-02 19:12:52 +00:00
|
|
|
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,
|
2014-10-05 05:47:19 +00:00
|
|
|
'time_start': obj.time_start,
|
|
|
|
'time_stop': obj.time_stop,
|
2014-10-13 08:10:20 +00:00
|
|
|
'cycle_id' : obj.cycle_id.id,
|
2014-10-02 02:02:22 +00:00
|
|
|
'visit_id': obj.id,
|
2014-10-14 03:57:20 +00:00
|
|
|
'cycle_id': obj.cycle_id.id,
|
2014-10-02 19:12:52 +00:00
|
|
|
'fee_type': obj.patient_id.type,
|
|
|
|
'lines':[],
|
2014-10-15 03:49:20 +00:00
|
|
|
'gm_lines':[],
|
2014-10-02 19:12:52 +00:00
|
|
|
'dialyzers': [],
|
2014-10-15 07:52:15 +00:00
|
|
|
'state': 'draft',
|
2014-10-02 19:12:52 +00:00
|
|
|
}
|
2014-10-15 07:52:15 +00:00
|
|
|
|
2014-10-15 03:49:20 +00:00
|
|
|
products=get_model("product").search_browse([['code','=','FEE']])
|
|
|
|
for product in products:
|
|
|
|
vals['gm_lines'].append(('create',{
|
|
|
|
'product_id': product.id,
|
|
|
|
'description': product.name or "",
|
|
|
|
'qty': 1,
|
|
|
|
'price': product.sale_price or 0.0,
|
|
|
|
'amount': product.sale_price or 0.0,
|
|
|
|
'uom_id': product.uom_id.id,
|
|
|
|
}))
|
2014-10-02 19:12:52 +00:00
|
|
|
|
|
|
|
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:
|
2014-10-13 08:10:20 +00:00
|
|
|
raise Exception("Partner Category: %s not found"%categ_name)
|
2014-10-02 19:12:52 +00:00
|
|
|
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
|
|
|
|
|
2014-10-03 07:27:57 +00:00
|
|
|
hd_case_id=hd_case_obj.create(vals)
|
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-05 05:47:19 +00:00
|
|
|
def discard(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-13 08:10:20 +00:00
|
|
|
def reopen(self,ids,context={ }):
|
2014-10-02 02:02:22 +00:00
|
|
|
obj=self.browse(ids)[0]
|
2014-10-13 08:10:20 +00:00
|
|
|
obj.write({"state":"draf t"})
|
2014-10-02 02:02:22 +00:00
|
|
|
|
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
|
|
|
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
|
|
|
|
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
|
2014-10-13 08:10:20 +00:00
|
|
|
|
|
|
|
return data
|
2014-10-02 19:12:52 +00:00
|
|
|
|
2014-10-13 08:10:20 +00:00
|
|
|
def copy(self,ids,context={}) :
|
2014-10-02 19:12:52 +00:00
|
|
|
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-14 03:57:20 +00:00
|
|
|
'cycle': obj.cycle,
|
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,
|
|
|
|
},
|
2014-10-13 08:10:20 +00:00
|
|
|
|
2014-10-02 19:12:52 +00:00
|
|
|
'flash': 'Visit %s is copy to %s'%(obj.number,new_obj.number),
|
|
|
|
}
|
2014-10-13 08:10:20 +00:00
|
|
|
|
2014-10-04 15:51:54 +00:00
|
|
|
|
|
|
|
def delete(self,ids,context={}):
|
|
|
|
for obj in self.browse(ids):
|
|
|
|
if obj.state!='draft':
|
|
|
|
raise Exception("Can not delete visit %s because state is not draft!"%obj.number)
|
|
|
|
super().delete(ids,context=context)
|
2014-10-04 16:11:34 +00:00
|
|
|
|
|
|
|
def get_data(self,context={}):
|
|
|
|
ref_id=context.get("refer_id")
|
|
|
|
if not ref_id:
|
|
|
|
return {}
|
|
|
|
ref_id=int(ref_id)
|
|
|
|
obj=self.browse(ref_id)
|
|
|
|
data={
|
|
|
|
'number': obj.number,
|
2014-10-05 08:29:21 +00:00
|
|
|
'date_visit': obj.date_visit,
|
2014-10-04 16:11:34 +00:00
|
|
|
'department_name': obj.department_id.name or "",
|
|
|
|
'patient_name': obj.patient_id.name or "",
|
|
|
|
'doctor_name': obj.doctor_id.name or "",
|
|
|
|
'nurse_name': obj.nurse_id.name or "",
|
|
|
|
'print_date': time.strftime("%d/%m/%Y"),
|
2014-10-14 04:31:51 +00:00
|
|
|
'cycle_name': obj.cycle_id.name or "",
|
2014-10-04 16:11:34 +00:00
|
|
|
}
|
2014-10-13 08:10:20 +00:00
|
|
|
return data
|
2014-08-19 11:36:46 +00:00
|
|
|
|
2014-10-14 03:57:20 +00:00
|
|
|
def onchange_time(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
cycle_id=data['cycle_id']
|
|
|
|
duration=1
|
|
|
|
if cycle_id:
|
|
|
|
cycle=get_model('clinic.cycle').browse(cycle_id)
|
|
|
|
duration=cycle.duration or 0
|
|
|
|
seconds=duration*3600
|
2014-10-05 05:47:19 +00:00
|
|
|
time_start=data['time_start']
|
2014-10-14 03:57:20 +00:00
|
|
|
data['time_stop']=(datetime.strptime(time_start,FMT_DATE)+timedelta(seconds=seconds)).strftime(FMT_DATE)
|
2014-10-05 05:47:19 +00:00
|
|
|
return data
|
2014-10-15 03:49:20 +00:00
|
|
|
|
|
|
|
def to_draft(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
for hd_case in obj.hd_cases:
|
|
|
|
# XXX
|
|
|
|
hd_case.write({
|
|
|
|
'state': 'draft',
|
|
|
|
})
|
|
|
|
hd_case.delete()
|
|
|
|
obj.write({
|
|
|
|
'state': 'draft',
|
|
|
|
})
|
2014-10-05 08:29:21 +00:00
|
|
|
|
2014-08-19 11:36:46 +00:00
|
|
|
Visit.register()
|