clinic/netforce_clinic/models/new_patient.py

189 lines
5.5 KiB
Python

import time
from netforce.model import Model, fields, get_model
from netforce.access import get_active_user, set_active_user
'''
lines:
patient, location -> text
patient:
share location
'''
class NewPatient(Model):
_name="new.patient"
_transient=True
_fields={
'name': fields.Char("Patient Name", required=True),
'date': fields.Date("Date"),
'lines': fields.One2Many("new.patient.line","new_id","Lines"),
'state': fields.Selection([['step1','Step1'],['step2','Step2']], 'State'),
'location_from_id': fields.Many2One("clinic.department","From Location"),
'location_to_id': fields.Many2One("clinic.department","To Location"),
}
_defaults={
'state': 'step1',
'date': lambda *a: time.strftime("%Y-%m-%d")
}
def find_patient(self, ids, context={}):
user_id=get_active_user()
set_active_user(1)
obj=self.browse(ids)[0]
get_model("new.patient.line").delete([l.id for l in obj.lines])
lines=[]
cond=[
['name','ilike',obj.name],
]
for patient in get_model("clinic.patient").search_browse(cond+[["dispose","=",True]]):
vals={
'patient_id': patient.id,
'department_id': patient.department_id.id,
'patient_name': patient.name,
'department_name': patient.department_id.name,
}
lines.append(('create',vals))
if not lines:
for patient_id in get_model("clinic.patient").search(cond):
return {
'next': {
'name': "clinic_patient",
"mode": "form",
"active_id": patient_id,
},
'flash': "ผู้ป่วยนี้มีอยู่แล้วในระบบ",
}
if len(lines)==1:
lines[0][1]['choose']=True
obj.write({
'lines': lines,
})
set_active_user(user_id)
def step1(self, ids, context={}):
obj=self.browse(ids)[0]
res=obj.find_patient()
if res:
return res
obj.write({
'state': 'step2',
})
def back_step1(self, ids, context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'step1',
})
print("back step1")
def step2(self, ids, context={}):
print("step2")
location_to_id=get_model("clinic.patient")._get_department(context)
user_id=get_active_user()
set_active_user(1)
obj=self.browse(ids)[0]
if not obj.lines:
res=obj.name.split(" ")
fname=''
lname=''
if len(res)>1:
fname=res[0]
lname=res[1]
else:
fname=obj.name
return {
'next': {
'name': 'clinic_patient',
'mode': 'form',
'defaults': {
'first_name': fname,
'last_name': lname,
},
}
}
else:
select_lines=[]
for line in obj.lines:
if line.choose:
select_lines.append(line)
if not select_lines:
raise Exception("Please choose patient!")
if len(select_lines)>1:
raise Exception("Can not select patient more than 1!")
line=[line for line in obj.lines if line.choose][0]
patient=line.patient_id
patient_id=patient.id
#default location from
#1. find in patient profile
location_code=patient.location.split(",")[-1]
location_from_id=None
for location_id in get_model("clinic.department").search([['code','=',location_code]]):
location_from_id=location_id
obj.write({
'state': 'step3',
'location_from_id': location_from_id,
'location_to_id': location_to_id,
})
set_active_user(user_id)
def back_step2(self, ids, context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'step2',
})
print("back step2")
def confirm(self, ids, context={}):
user_id=get_active_user()
set_active_user(1)
obj=self.browse(ids)[0]
line=[line for line in obj.lines if line.choose][0]
patient=line.patient_id
#share location here
location=patient.location.split(",")+[obj.location_to_id.code or '']
sloc=list(set(location))
location=','.join([loc for loc in sloc if loc])
patient.write({
'location': location,
})
set_active_user(user_id)
patient_id=patient.id
vals={
'patient_id': patient_id,
'location_from_id': obj.location_from_id.id,
'location_to_id': obj.location_to_id.id,
'state': 'new',
'date': obj.date,
}
get_model("clinic.patient.move").create(vals)
patient.write({
'dispose': False,
})
return {
'next': {
'name': 'clinic_patient',
'mode': 'form',
'active_id': patient.id,
},
'flash': "New patient successful!",
}
NewPatient.register()