clinic/netforce_clinic/models/make_apt.py

273 lines
9.7 KiB
Python
Raw Normal View History

2015-01-15 11:45:11 +00:00
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
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),
2015-01-19 05:34:50 +00:00
'department_id': fields.Many2One("clinic.department","Department"),
'branch_id': fields.Many2One("clinic.branch","Branch"),
'cycle_id': fields.Many2One("clinic.cycle","Cycle"),
2015-01-15 11:45:11 +00:00
"lines": fields.One2Many("clinic.make.apt.line","apt_id","Lines"),
}
2015-01-15 15:38:28 +00:00
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))
2015-01-15 11:45:11 +00:00
_defaults={
'date': lambda *a: time.strftime("%Y-%m-%d"),
2015-01-15 15:38:28 +00:00
'date_from': _get_date_from,
'date_to': _get_date_to,
2015-01-15 11:45:11 +00:00
}
def load(self,ids,context={}):
obj=self.browse(ids)[0]
pts={}
2015-01-19 05:34:50 +00:00
dom=[]
2015-03-02 04:03:07 +00:00
dom.append(['patient_id.state','=','admit'])
2015-01-19 05:34:50 +00:00
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
2015-02-02 01:41:43 +00:00
pcs=get_model("clinic.patient.cycle").search_browse(dom)
2015-02-02 06:48:25 +00:00
if not pcs:
# Create patient cycle with that department
for pt in get_model("clinic.patient").search_browse([['department_id','=',obj.department_id.id]]):
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)
2015-02-02 01:41:43 +00:00
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:
2015-01-15 11:45:11 +00:00
patient=pc.patient_id
dpt=pc.department_id
2015-02-02 01:41:43 +00:00
branch=dpt.branch_id
2015-03-02 04:03:07 +00:00
if not branch:
continue
2015-02-02 01:41:43 +00:00
if branch_id and branch_id!=branch.id:
continue
2015-01-15 14:11:35 +00:00
key=(patient.id,dpt.id)
2015-01-15 11:45:11 +00:00
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():
2015-01-15 14:11:35 +00:00
pt_id, dpt_id=k
if pt_id:
vals['patient_id']=pt_id
2015-01-15 15:38:28 +00:00
if not dpt_id:
pt=get_model("clinic.patient").browse(pt_id)
dpt_id=pt.department_id.id
2015-01-15 14:11:35 +00:00
if dpt_id:
vals['department_id']=dpt_id
2015-01-15 11:45:11 +00:00
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,
},
2015-03-02 04:03:07 +00:00
#'flash': 'Ready to generate', #remove this message because user confuse
2015-01-15 11:45:11 +00:00
}
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)
for line in obj.lines:
patient=line.patient_id
if not patient:
continue
2015-01-15 11:45:11 +00:00
dpt=line.department_id or patient.department_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
2015-01-15 15:38:28 +00:00
days.append([3,cycle,dpt])
2015-01-15 11:45:11 +00:00
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)
2015-01-15 11:45:11 +00:00
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()
2015-01-16 04:00:24 +00:00
2015-01-15 11:45:11 +00:00
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']])
2015-01-15 11:45:11 +00:00
vids=visit_obj.search(dom)
if not vids:
visit_vals.append(vals)
count+=7
2015-01-15 11:45:11 +00:00
start_date=tmp
user_id=get_active_user()
staff_ids=get_model("clinic.staff").search([['type','=','type'],['user_id','=',user_id]])
confirm_id=None
2015-01-15 15:38:28 +00:00
flash='Generate Visit Successfully'
2015-01-15 11:45:11 +00:00
if staff_ids:
confirm_id=staff_ids[0]
2015-01-15 15:38:28 +00:00
count=0
2015-01-15 11:45:11 +00:00
for vals in visit_vals:
vals['nurse_id']=confirm_id
visit_obj.create(vals)
2015-01-15 15:38:28 +00:00
count+=1
if count:
2015-02-02 01:41:43 +00:00
flash="Visit is generated succesfully %s items, please go to visit board."%count
2015-01-15 11:45:11 +00:00
return {
'next': {
'name': 'clinic_make_apt',
'mode': 'form',
'active_id': obj.id,
},
2015-01-15 15:38:28 +00:00
'flash': flash,
2015-01-15 11:45:11 +00:00
}
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
2015-02-02 06:48:25 +00:00
def onchange_branch(self,context={}):
data=context['data']
data['department_id']=None
return data
2015-01-15 11:45:11 +00:00
MakeAPT.register()