310 lines
11 KiB
Python
310 lines
11 KiB
Python
import time
|
|
from datetime import datetime, timedelta
|
|
from calendar import monthrange
|
|
|
|
from netforce.model import Model, fields, get_model
|
|
from netforce.access import get_active_user
|
|
from netforce.utils import get_data_path
|
|
|
|
DRT=0
|
|
FMT_DATE="%Y-%m-%d"
|
|
FMT_DATETIME="%Y-%m-%d %H:%M:%S"
|
|
|
|
class MakeAPT(Model):
|
|
_name="clinic.make.apt"
|
|
_transient=True
|
|
|
|
_fields={
|
|
"date": fields.Date("Month"),
|
|
"date_from": fields.Date("From", required=True),
|
|
"date_to": fields.Date("To", required=True),
|
|
'department_id': fields.Many2One("clinic.department","Department"),
|
|
'branch_id': fields.Many2One("clinic.branch","Branch"),
|
|
'cycle_id': fields.Many2One("clinic.cycle","Cycle"),
|
|
"lines": fields.One2Many("clinic.make.apt.line","apt_id","Lines"),
|
|
}
|
|
|
|
def _get_date_from(self,context={}):
|
|
year,month,day=time.strftime(FMT_DATE).split("-")
|
|
return "%s-%s-%s"%(year,month,"01")
|
|
|
|
def _get_date_to(self,context={}):
|
|
year,month,day=time.strftime(FMT_DATE).split("-")
|
|
weekday, total_day=monthrange(int(year), int(month))
|
|
return "%s-%s-%s"%(year,month,("%s"%total_day).zfill(2))
|
|
|
|
_defaults={
|
|
'date': lambda *a: time.strftime("%Y-%m-%d"),
|
|
'date_from': _get_date_from,
|
|
'date_to': _get_date_to,
|
|
}
|
|
|
|
def load(self,ids,context={}):
|
|
obj=self.browse(ids)[0]
|
|
pts={}
|
|
dom=[]
|
|
dom.append(['patient_id.state','=','admit'])
|
|
if obj.department_id:
|
|
dom.append(['department_id','=',obj.department_id.id])
|
|
if obj.cycle_id:
|
|
dom.append(['cycle_id','=',obj.cycle_id.id])
|
|
branch_id=obj.branch_id.id
|
|
pcs=get_model("clinic.patient.cycle").search_browse(dom)
|
|
if not pcs:
|
|
# Create patient cycle with that department
|
|
for pt in get_model("clinic.patient").search_browse([['department_id','=',obj.department_id.id]]):
|
|
if pt.walkin=='yes':
|
|
continue
|
|
for pc in pt.cycles:
|
|
vals={
|
|
'patient_id': pt.id,
|
|
'department_id': pt.department_id.id,
|
|
'cycle_id': pc.cycle_id.id,
|
|
'day': pc.day,
|
|
}
|
|
res=get_model("clinic.patient.cycle").search([[
|
|
['patient_id','=',pt.id],
|
|
['day','=',pc.day],
|
|
['cycle_id','=',pc.cycle_id.id],
|
|
['department_id','=', pt.department_id.id],
|
|
]])
|
|
if not res:
|
|
pcycle_id=get_model("clinic.patient.cycle").create(vals)
|
|
print("create ", pcycle_id)
|
|
else:
|
|
print("exist ", res)
|
|
pcs=get_model("clinic.patient.cycle").search_browse(dom)
|
|
if not pcs:
|
|
raise Exception("Please go to menu 'Patients -> Patient Cycles' and import data for %s"%(obj.department_id.name))
|
|
for pc in pcs:
|
|
patient=pc.patient_id
|
|
dpt=pc.department_id
|
|
branch=dpt.branch_id
|
|
if not branch:
|
|
continue
|
|
if branch_id and branch_id!=branch.id:
|
|
continue
|
|
key=(patient.id,dpt.id)
|
|
if not pts.get(key):
|
|
pts[key]={
|
|
'mon_cycle_id': None,
|
|
'tue_cycle_id': None,
|
|
'wed_cycle_id': None,
|
|
'thu_cycle_id': None,
|
|
'fri_cycle_id': None,
|
|
'sat_cycle_id': None,
|
|
'sun_cycle_id': None,
|
|
}
|
|
cycle=pc.cycle_id
|
|
day=pc.day or ''
|
|
if day=='mon':
|
|
pts[key]['mon_cycle_id']=cycle.id
|
|
elif day=='tue':
|
|
pts[key]['tue_cycle_id']=cycle.id
|
|
elif day=='wed':
|
|
pts[key]['wed_cycle_id']=cycle.id
|
|
elif day=='thu':
|
|
pts[key]['thu_cycle_id']=cycle.id
|
|
elif day=='fri':
|
|
pts[key]['fri_cycle_id']=cycle.id
|
|
elif day=='sat':
|
|
pts[key]['sat_cycle_id']=cycle.id
|
|
else:
|
|
pts[key]['sun_cycle_id']=cycle.id
|
|
|
|
lines=[]
|
|
for k, vals in pts.items():
|
|
pt_id, dpt_id=k
|
|
if pt_id:
|
|
vals['patient_id']=pt_id
|
|
if not dpt_id:
|
|
pt=get_model("clinic.patient").browse(pt_id)
|
|
dpt_id=pt.department_id.id
|
|
if dpt_id:
|
|
vals['department_id']=dpt_id
|
|
lines.append(('create',vals))
|
|
|
|
for line in obj.lines:
|
|
line.delete()
|
|
|
|
obj.write({
|
|
'lines': lines,
|
|
})
|
|
|
|
return {
|
|
'next': {
|
|
'name': 'clinic_make_apt',
|
|
'mode': 'form',
|
|
'active_id': obj.id,
|
|
},
|
|
#'flash': 'Ready to generate', #remove this message because user confuse
|
|
}
|
|
|
|
def gen(self,ids,context={}):
|
|
obj=self.browse(ids)[0]
|
|
if not obj.lines:
|
|
raise Exception("Nothing to generate")
|
|
|
|
visit_obj=get_model("clinic.visit")
|
|
visit_vals=[]
|
|
date_from=datetime.strptime(obj.date_from,FMT_DATE)
|
|
date_to=datetime.strptime(obj.date_to,FMT_DATE)
|
|
old_pids=[]
|
|
for line in obj.lines:
|
|
patient=line.patient_id
|
|
if not patient:
|
|
continue
|
|
dpt=line.department_id or patient.department_id
|
|
# skip already gen
|
|
if patient.id in old_pids:
|
|
print("skip patient ", patient.id, old_pids)
|
|
continue
|
|
old_pids.append(patient.id)
|
|
|
|
days=[]
|
|
if line.mon_cycle_id:
|
|
cycle=line.mon_cycle_id
|
|
days.append([1,cycle,dpt])
|
|
|
|
if line.tue_cycle_id:
|
|
cycle=line.tue_cycle_id
|
|
days.append([2,cycle,dpt])
|
|
|
|
if line.wed_cycle_id:
|
|
cycle=line.wed_cycle_id
|
|
days.append([3,cycle,dpt])
|
|
|
|
if line.thu_cycle_id:
|
|
cycle=line.thu_cycle_id
|
|
days.append([4,cycle,dpt])
|
|
|
|
if line.fri_cycle_id:
|
|
cycle=line.fri_cycle_id
|
|
days.append([5,cycle,dpt])
|
|
|
|
if line.sat_cycle_id:
|
|
cycle=line.sat_cycle_id
|
|
days.append([6,cycle,dpt])
|
|
|
|
if line.sun_cycle_id:
|
|
cycle=line.sun_cycle_id
|
|
days.append([7,cycle,dpt])
|
|
# remove the rest
|
|
dom=[]
|
|
dom.append(['visit_date','>=', obj.date_from])
|
|
dom.append(['visit_date','<=', (datetime.strptime(obj.date_to,FMT_DATE)+timedelta(days=14)).strftime(FMT_DATE)])
|
|
dom.append(['patient_id', '=', patient.id])
|
|
dom.append(['state','=','pending'])
|
|
vids=visit_obj.search(dom)
|
|
visit_obj.delete(vids)
|
|
ntoday=1
|
|
day_total=(date_to-date_from).days+ntoday
|
|
for weekday, cycle, department in days:
|
|
wd=date_from.weekday()
|
|
start_date=date_from
|
|
while wd != weekday-1:
|
|
start_date+=timedelta(days=1)
|
|
wd=start_date.weekday()
|
|
|
|
if start_date.strftime(FMT_DATE) > date_to.strftime(FMT_DATE):
|
|
continue
|
|
count=0
|
|
tmp=start_date
|
|
cstart=cycle.time_start
|
|
cstop=cycle.time_stop
|
|
while count < day_total:
|
|
tmp=start_date+timedelta(days=count)
|
|
visit_date="%s"%(tmp.strftime(FMT_DATE))
|
|
ttime_start="%s %s" % (visit_date,cstart)
|
|
ttime_stop="%s %s" % (visit_date,cstop)
|
|
vals={
|
|
'patient_id': patient.id,
|
|
'doctor_id': patient.doctor_id.id,
|
|
'department_id': department.id,
|
|
'branch_id': patient.branch_id.id or department.branch_id.id,
|
|
'cycle_id': cycle.id,
|
|
'time_start': ttime_start,
|
|
'time_stop': ttime_stop,
|
|
'visit_date': visit_date,
|
|
'state': 'pending',
|
|
}
|
|
dom=[]
|
|
dom.append(['time_start','>=','%s %s'%(tmp.strftime(FMT_DATE)[0:10],' 00:00:00')])
|
|
dom.append(['time_stop','<=','%s %s'%(tmp.strftime(FMT_DATE)[0:10],' 23:59:59')])
|
|
dom.append(['patient_id', '=', patient.id])
|
|
dom.append(['cycle_id', '=', cycle.id])
|
|
dom.append(['state','in',['confirmed']])
|
|
vids=visit_obj.search(dom)
|
|
if not vids:
|
|
visit_vals.append(vals)
|
|
count+=7 #XXX recheck
|
|
start_date=tmp
|
|
user_id=get_active_user()
|
|
staff_ids=get_model("clinic.staff").search([['type','=','type'],['user_id','=',user_id]])
|
|
confirm_id=None
|
|
|
|
flash='Generate Visit Successfully'
|
|
if staff_ids:
|
|
confirm_id=staff_ids[0]
|
|
count=0
|
|
for vals in visit_vals:
|
|
vals['nurse_id']=confirm_id
|
|
visit_obj.create(vals)
|
|
count+=1
|
|
if count:
|
|
flash="Visit is generated succesfully %s items, please go to visit board."%count
|
|
return {
|
|
'next': {
|
|
'name': 'clinic_make_apt',
|
|
'mode': 'form',
|
|
'active_id': obj.id,
|
|
},
|
|
'flash': flash,
|
|
}
|
|
|
|
def onchange_date(self,context={}):
|
|
data=context['data']
|
|
date=data['date']
|
|
year,month,day=date.split("-")
|
|
weekday, total_day=monthrange(int(year), int(month))
|
|
data['date_from']="%s-%s-01"%(year,month)
|
|
data['date_to']="%s-%s-%s"%(year,month,total_day)
|
|
return data
|
|
|
|
def onchange_branch(self,context={}):
|
|
data=context['data']
|
|
data['department_id']=None
|
|
return data
|
|
|
|
def onchange_patient(self,context={}):
|
|
data=context['data']
|
|
path=context['path']
|
|
line=get_data_path(data,path,parent=True)
|
|
patient_id=line['patient_id']
|
|
patient=get_model("clinic.patient").browse(patient_id)
|
|
department_id=None
|
|
if patient.department_id:
|
|
department_id=patient.department_id.id
|
|
line['department_id']=department_id
|
|
for cycle in patient.cycles:
|
|
cycle_id=cycle.cycle_id.id
|
|
day=cycle.day
|
|
print('day ', day)
|
|
if day=='mon':
|
|
line['mon_cycle_id']=cycle_id
|
|
elif day=='tue':
|
|
line['tue_cycle_id']=cycle_id
|
|
elif day=='wed':
|
|
line['wed_cycle_id']=cycle_id
|
|
elif day=='thu':
|
|
line['thu_cycle_id']=cycle_id
|
|
elif day=='fri':
|
|
line['fri_cycle_id']=cycle_id
|
|
elif day=='sat':
|
|
line['sat_cycle_id']=cycle_id
|
|
elif day=='sun':
|
|
line['sun_cycle_id']=cycle_id
|
|
return data
|
|
|
|
MakeAPT.register()
|