clinic/netforce_clinic/models/patient_move.py

245 lines
7.5 KiB
Python
Raw Permalink Normal View History

2015-07-28 11:58:54 +00:00
import time
from datetime import datetime
from calendar import monthrange
2015-07-28 11:58:54 +00:00
from netforce.model import Model, fields, get_model
from netforce.access import get_active_user, set_active_user
from . import utils
2015-07-28 11:58:54 +00:00
class PatientMove(Model):
_name="clinic.patient.move"
2015-07-28 11:58:54 +00:00
_fields={
'patient_id': fields.Many2One('clinic.patient','Patient',search=True, required=True),
2017-06-12 17:51:25 +00:00
'patient_type_id': fields.Many2One("clinic.patient.type","Patient Type"),
2017-06-04 17:47:34 +00:00
'date': fields.Date("Date", required=True,search=True),
2017-06-09 20:07:54 +00:00
'location_from_id': fields.Many2One("clinic.department","Location From",search=True,required=True),
'location_to_id': fields.Many2One("clinic.department","Location To",search=True,required=True),
"state": fields.Selection([['normal','Normal'],['new','New'],['dispose','Dispose']], "State"),
'note': fields.Text("Note",search=True),
2015-07-28 11:58:54 +00:00
}
_defaults={
'date': lambda *a: time.strftime("%Y-%m-%d"),
'state': 'normal',
2015-07-28 11:58:54 +00:00
}
def onchange_patient(self, context={}):
data=context['data']
patient_id=data['patient_id']
patient=get_model('clinic.patient').browse(patient_id)
location_from_id=patient.department_id.id
location_to_id=get_model('clinic.patient')._get_department(context)
data.update({
'location_from_id': location_from_id,
'location_to_id': location_to_id,
2017-06-12 17:51:25 +00:00
'patient_type_id': patient.type_id.id,
})
return data
def dispose_patient(self, ids, context={}):
obj=self.browse(ids)[0]
if obj.state=='new':
obj.patient_id.write({
'dispose': False,
})
elif obj.state=='dispose':
obj.patient_id.write({
'dispose': True,
})
else:
pass
def create(self, vals, **kw):
new_id=super().create(vals, **kw)
obj=self.browse(new_id)
obj.dispose_patient()
return new_id
def write(self, ids, vals, **kw):
super().write(ids, vals, **kw)
for obj in self.browse(ids):
obj.dispose_patient()
def get_data(self, date, department_id, context={}):
if not date:
raise Exception("Missing date!")
2017-06-04 17:47:34 +00:00
y,m,d=date.split("-")
y=int(y)
m=int(m)
weekday, day_month=monthrange(y, m)
cond1=[
2017-06-04 17:47:34 +00:00
['date','>=','%s-%s-01'%(y,str(m).zfill(2))],
['date','<=','%s-%s-%s'%(y,str(m).zfill(2),day_month)],
['location_to_id','=', department_id],
]
2017-06-12 17:51:25 +00:00
cond=cond1+[['state','=','normal']]
ids=self.search(cond)
2017-06-12 17:51:25 +00:00
move_ids=ids
prev_month=len(ids)
cond=cond1+[['state','=','new']]
ids=self.search(cond)
2017-06-12 17:51:25 +00:00
move_ids+=ids
current_month=len(ids)
current_items=[]
for index, val in enumerate(self.read(ids)):
val['no']=index+1
current_items.append(val)
cond=cond1+[['state','=','dispose']]
ids=self.search(cond)
2017-06-12 17:51:25 +00:00
dispose_ids=ids
dispose_month=len(ids)
dispose_items=[]
for index, val in enumerate(self.read(ids)):
val['no']=index+1
dispose_items.append(val)
2017-06-12 17:51:25 +00:00
move_ids=[id for id in move_ids if id not in dispose_ids]
# group by type
types={}
for move in get_model("clinic.patient.move").browse(move_ids):
type_name=move.patient_type_id.name
types.setdefault(type_name,0)
types[type_name]+=1
patient_types={}
for pt in get_model("clinic.patient.type").search_browse([]):
patient_types[pt.name]=types.get(pt.name, '')
type_lines=[]
type_names=sorted(patient_types.keys())
for type_name in type_names:
qty=patient_types[type_name]
type_lines.append({
'name': type_name,
'qty': qty,
})
next_month=prev_month+current_month-dispose_month
prev_m=m-1
if prev_m <= 0:
prev_m=1
next_m=m+1
if next_m > 12:
next_m=1
prev_month_thai=utils.MONTHS['th_TH'][prev_m]
current_month_thai=utils.MONTHS['th_TH'][m]
dispose_month_thai=utils.MONTHS['th_TH'][m]
next_month_thai=utils.MONTHS['th_TH'][next_m]
res={
'prev_month': prev_month,
'prev_month_thai': prev_month_thai,
'current_month': current_month,
'current_month_thai': current_month_thai,
'current_items': current_items,
'dispose_month': dispose_month,
'dispose_month_thai': dispose_month_thai,
'dispose_items': dispose_items,
'next_month': next_month,
'next_month_thai': next_month_thai,
2017-06-12 17:51:25 +00:00
'type_lines': type_lines,
}
return res
2017-06-04 17:47:34 +00:00
def auto_get_data(self, date, department_id, context={}):
user_id=get_active_user()
set_active_user(1)
2017-06-04 17:47:34 +00:00
datenow=time.strftime("%Y-%m-%d")
if not date:
raise Exception("Missing date!")
2017-06-04 17:47:34 +00:00
if not department_id:
raise Exception("Missing department!")
y,m,d=date.split("-")
weekday, day_month=monthrange(int(y), int(m))
cond=[
2017-06-04 17:47:34 +00:00
['date','>=','%s-%s-01'%(y,m)],
['date','<=','%s-%s-%s'%(y,m,day_month)],
['location_to_id','=',department_id],
]
res=self.search(cond)
2017-06-04 17:47:34 +00:00
#XXX reset data
if res and date > datenow:
ids=self.search(cond)
print("reset data...")
print(ids)
self.delete(ids)
res=None
# copy data from previous month
if not res:
y=int(y)
m=int(m)-1
if m<=0:
m=1
y-=1
weekday, day_month=monthrange(int(y), int(m))
cond=[
2017-06-04 17:47:34 +00:00
['date','>=','%s-%s-01'%(y,m)],
['date','<=','%s-%s-%s'%(y,m,day_month)],
['location_to_id','=',department_id],
]
res=self.search_browse(cond)
for obj in res:
if obj.state=='dispose':
continue
vals={
'location_from_id': obj.location_from_id.id,
'location_to_id': obj.location_to_id.id,
'patient_id': obj.patient_id.id,
2017-06-12 17:51:25 +00:00
'patient_type_id': obj.patient_type_id.id,
'date': date,
}
self.create(vals)
# copy from patient
if not res:
cond=[
['walkin','=','no'],
['dispose','=',False],
]
locations={
'LS-FL1': 1,
'LS-FL2': 2,
'LS-FL3': 3,
'SS-FL3': 4,
'SS-FL4': 5,
}
for obj in get_model("clinic.patient").search_browse(cond):
if not obj.location:
continue
locs=obj.location.split(",")
vals={
'location_from_id': locations[locs[0]], #first
'location_to_id': locations[locs[-1]], #last
'patient_id': obj.id,
2017-06-12 17:51:25 +00:00
'patient_type_id': obj.type_id.id,
'note': obj.note,
'date': date,
}
self.create(vals)
set_active_user(user_id)
PatientMove.register()