2014-10-14 13:48:14 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2015-01-14 18:58:10 +00:00
|
|
|
from netforce.access import get_active_user, set_active_user
|
2014-10-14 13:48:14 +00:00
|
|
|
from netforce.model import Model, fields, get_model
|
2015-01-13 12:08:38 +00:00
|
|
|
from netforce.utils import get_data_path
|
2014-10-14 13:48:14 +00:00
|
|
|
|
|
|
|
FMT_DATE="%Y-%m-%d"
|
|
|
|
FMT_DATETIME="%Y-%m-%d %H:%M:%S"
|
|
|
|
|
|
|
|
class GenVisit(Model):
|
|
|
|
_name="clinic.gen.visit"
|
|
|
|
_transient=True
|
|
|
|
|
|
|
|
_fields={
|
2014-11-30 13:05:14 +00:00
|
|
|
'date_from': fields.Date("From", required=True),
|
|
|
|
'date_to': fields.Date("To", required=True),
|
2014-11-01 03:48:22 +00:00
|
|
|
'patient_lines': fields.One2Many("clinic.gen.visit.line","gen_id","Patient Lines"),
|
2015-01-11 13:58:51 +00:00
|
|
|
'patient_type_id': fields.Many2One("clinic.patient.type","Patient Type"),
|
2014-11-01 17:30:48 +00:00
|
|
|
'patient_categ_id': fields.Many2One("clinic.patient.categ", "Patient Category"),
|
2015-01-13 12:08:38 +00:00
|
|
|
'time_lines': fields.One2Many("clinic.gen.visit.time","gen_id","Time Lines"),
|
|
|
|
'department_id': fields.Many2One("clinic.department","Department"),
|
|
|
|
'doctor_id': fields.Many2One("clinic.staff","Doctor",domain=[['type','=','doctor']]),
|
2014-10-14 13:48:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 04:40:35 +00:00
|
|
|
|
2014-11-01 03:48:22 +00:00
|
|
|
def _get_patient_lines(self,context={}):
|
2014-10-14 13:48:14 +00:00
|
|
|
ids=context.get("ids")
|
2014-10-15 03:49:20 +00:00
|
|
|
if not ids:
|
|
|
|
ids=[context.get("refer_id")]
|
2014-11-01 03:48:22 +00:00
|
|
|
lines=[]
|
|
|
|
if any(ids):
|
2015-01-13 12:08:38 +00:00
|
|
|
lines=[{'patient_id': id} for id in ids if type(id)==type(1)]
|
2014-10-14 13:48:14 +00:00
|
|
|
return lines
|
|
|
|
|
2015-01-13 12:08:38 +00:00
|
|
|
def _get_time_lines(self,context={}):
|
|
|
|
return [
|
|
|
|
{'day': 'mon',},
|
|
|
|
{'day': 'tue',},
|
|
|
|
{'day': 'wed',},
|
|
|
|
{'day': 'thu',},
|
|
|
|
{'day': 'fri',},
|
|
|
|
{'day': 'sat',},
|
|
|
|
{'day': 'sun',},
|
|
|
|
]
|
2014-11-30 13:05:14 +00:00
|
|
|
|
2014-10-14 13:48:14 +00:00
|
|
|
_defaults={
|
2014-11-30 13:05:14 +00:00
|
|
|
'date_from': lambda *a: datetime.now().strftime(FMT_DATE),
|
|
|
|
'date_to': lambda *a: (datetime.now()+timedelta(days=7)).strftime(FMT_DATE),
|
2015-01-13 12:08:38 +00:00
|
|
|
'patient_lines': _get_patient_lines,
|
|
|
|
'time_lines': _get_time_lines,
|
2014-10-14 13:48:14 +00:00
|
|
|
}
|
2014-11-21 02:39:26 +00:00
|
|
|
|
2014-11-30 13:05:14 +00:00
|
|
|
def onchange_cycle(self,context={}):
|
2014-10-14 13:48:14 +00:00
|
|
|
data=context['data']
|
|
|
|
cycle_id=data['cycle_id']
|
|
|
|
if cycle_id:
|
|
|
|
cycle=get_model('clinic.cycle').browse(cycle_id)
|
2014-11-30 13:05:14 +00:00
|
|
|
data['time_start']=cycle.time_start
|
|
|
|
data['time_stop']=cycle.time_stop
|
2015-01-14 18:58:10 +00:00
|
|
|
else:
|
|
|
|
data['time_start']=''
|
|
|
|
data['time_stop']=''
|
2014-10-14 13:48:14 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def gen_visit(self,ids,context):
|
|
|
|
obj=self.browse(ids)[0]
|
2014-10-20 05:17:21 +00:00
|
|
|
visit_obj=get_model('clinic.visit')
|
2015-01-14 18:58:10 +00:00
|
|
|
b_ids=get_model('clinic.branch').search([])
|
|
|
|
branch_id=None
|
|
|
|
if b_ids:
|
|
|
|
branch_id=b_ids[0]
|
2015-01-13 12:08:38 +00:00
|
|
|
days={}
|
|
|
|
for tline in obj.time_lines:
|
|
|
|
if tline.day and tline.cycle_id and tline.time_start and tline.time_stop:
|
|
|
|
day=tline.day
|
|
|
|
cycle_id=tline.cycle_id.id
|
|
|
|
time_start=tline.time_start
|
|
|
|
time_stop=tline.time_stop
|
|
|
|
d=0
|
|
|
|
if day=='mon':
|
|
|
|
d=1
|
|
|
|
elif day=='tue':
|
|
|
|
d=2
|
|
|
|
elif day=='wed':
|
|
|
|
d=3
|
|
|
|
elif day=='thu':
|
|
|
|
d=4
|
|
|
|
elif day=='fri':
|
|
|
|
d=5
|
|
|
|
elif day=='sat':
|
|
|
|
d=6
|
|
|
|
elif day=='sun':
|
|
|
|
d=7
|
|
|
|
days.update({
|
|
|
|
d:{
|
|
|
|
'cycle_id': cycle_id,
|
|
|
|
'time_start': time_start,
|
|
|
|
'time_stop': time_stop,
|
|
|
|
}})
|
2014-10-14 13:48:14 +00:00
|
|
|
if not days:
|
2015-01-13 12:08:38 +00:00
|
|
|
raise Exception("Please select Days")
|
|
|
|
|
2014-10-15 03:49:20 +00:00
|
|
|
visit_vals=[]
|
2014-11-30 13:05:14 +00:00
|
|
|
date_from=datetime.strptime(obj.date_from,FMT_DATE)
|
|
|
|
date_to=datetime.strptime(obj.date_to,FMT_DATE)
|
2014-11-01 06:04:30 +00:00
|
|
|
patients=[p.patient_id.id for p in obj.patient_lines if p.patient_id]
|
2015-01-11 13:58:51 +00:00
|
|
|
if not patients and obj.patient_type_id:
|
|
|
|
patients=get_model("clinic.patient").search([['type_id','=',obj.patient_type_id.id]])
|
2014-11-01 17:30:48 +00:00
|
|
|
if not patients:
|
2015-01-11 13:58:51 +00:00
|
|
|
raise Exception("No patient for type %s"%obj.patient_type_id.name)
|
2014-11-01 17:30:48 +00:00
|
|
|
elif not patients and obj.patient_categ_id:
|
|
|
|
patients=get_model("clinic.patient").search([['categ_id','=',obj.patient_categ_id.id]])
|
|
|
|
if not patients:
|
|
|
|
raise Exception("No patient for category %s"%obj.patient_categ_id.name)
|
2014-11-01 06:04:30 +00:00
|
|
|
elif patients:
|
2014-11-01 08:49:27 +00:00
|
|
|
# continue to generate with condition
|
2014-11-01 06:04:30 +00:00
|
|
|
pass
|
2014-11-01 03:48:22 +00:00
|
|
|
else:
|
|
|
|
raise Exception("Please select some patient or patient type")
|
|
|
|
|
2015-01-13 12:08:38 +00:00
|
|
|
day_choices=days.keys()
|
2014-11-01 03:48:22 +00:00
|
|
|
for patient_id in patients:
|
2014-11-02 03:03:28 +00:00
|
|
|
patient=get_model("clinic.patient").browse(patient_id)
|
2014-10-15 03:49:20 +00:00
|
|
|
ntoday=1
|
|
|
|
day_total=(date_to-date_from).days+ntoday
|
2015-01-13 12:08:38 +00:00
|
|
|
for weekday in day_choices:
|
2014-10-15 03:49:20 +00:00
|
|
|
wd=date_from.weekday()
|
|
|
|
start_date=date_from
|
|
|
|
while wd != weekday-1:
|
|
|
|
start_date+=timedelta(days=1)
|
|
|
|
wd=start_date.weekday()
|
2014-10-20 05:17:21 +00:00
|
|
|
#XXX should gen only in scope
|
2014-10-15 03:49:20 +00:00
|
|
|
if start_date.strftime(FMT_DATE) > date_to.strftime(FMT_DATE):
|
|
|
|
continue
|
|
|
|
count=0
|
2014-11-01 17:30:48 +00:00
|
|
|
tmp=start_date
|
2015-01-13 12:08:38 +00:00
|
|
|
cstart=days[weekday]['time_start']
|
|
|
|
cstop=days[weekday]['time_stop']
|
2014-10-15 03:49:20 +00:00
|
|
|
while count < day_total:
|
|
|
|
tmp=start_date+timedelta(days=count)
|
2015-01-14 18:58:10 +00:00
|
|
|
visit_date="%s"%(tmp.strftime(FMT_DATE))
|
|
|
|
ttime_start="%s %s" % (visit_date,cstart)
|
|
|
|
ttime_stop="%s %s" % (visit_date,cstop)
|
2014-10-14 13:48:14 +00:00
|
|
|
vals={
|
2014-11-01 03:48:22 +00:00
|
|
|
'patient_id': patient_id,
|
2014-11-02 03:03:28 +00:00
|
|
|
'doctor_id': obj.doctor_id.id or patient.doctor_id.id,
|
2015-01-14 18:58:10 +00:00
|
|
|
'department_id': patient.department_id.id,
|
|
|
|
'branch_id': branch_id,
|
2015-01-13 12:08:38 +00:00
|
|
|
'cycle_id': days[weekday]['cycle_id'],
|
2014-11-01 06:04:30 +00:00
|
|
|
'time_start': ttime_start,
|
|
|
|
'time_stop': ttime_stop,
|
2015-01-14 18:58:10 +00:00
|
|
|
'visit_date': visit_date,
|
2014-12-19 18:19:19 +00:00
|
|
|
'state': 'pending',
|
2014-10-14 13:48:14 +00:00
|
|
|
}
|
|
|
|
visit_vals.append(vals)
|
2014-10-15 03:49:20 +00:00
|
|
|
count+=7
|
2014-10-20 05:17:21 +00:00
|
|
|
|
|
|
|
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')])
|
2014-11-01 03:48:22 +00:00
|
|
|
dom.append(['patient_id', '=', patient_id])
|
2015-01-13 12:08:38 +00:00
|
|
|
dom.append(['cycle_id', '=', days[weekday]['cycle_id']])
|
2014-12-19 18:19:19 +00:00
|
|
|
dom.append(['state','=','pending'])
|
2014-10-20 05:17:21 +00:00
|
|
|
vids=visit_obj.search(dom)
|
|
|
|
visit_obj.delete(vids)
|
2014-10-15 03:49:20 +00:00
|
|
|
start_date=tmp
|
2014-11-30 13:05:14 +00:00
|
|
|
user_id=get_active_user()
|
|
|
|
staff_ids=get_model("clinic.staff").search([['type','=','type'],['user_id','=',user_id]])
|
|
|
|
confirm_id=None
|
|
|
|
if staff_ids:
|
|
|
|
confirm_id=staff_ids[0]
|
2014-10-20 05:17:21 +00:00
|
|
|
for vals in visit_vals:
|
2015-01-14 18:58:10 +00:00
|
|
|
print("vals ", vals)
|
2014-11-30 13:05:14 +00:00
|
|
|
vals['nurse_id']=confirm_id
|
2014-10-20 05:17:21 +00:00
|
|
|
visit_obj.create(vals)
|
2014-10-14 13:48:14 +00:00
|
|
|
return {
|
|
|
|
'next': {
|
2014-11-01 03:48:22 +00:00
|
|
|
'name': 'clinic_visit',
|
2014-10-14 13:48:14 +00:00
|
|
|
'mode': 'list',
|
|
|
|
},
|
|
|
|
'flash': 'Generate OK',
|
|
|
|
}
|
|
|
|
|
2014-10-20 05:17:21 +00:00
|
|
|
def clear_visit(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
2015-01-13 12:08:38 +00:00
|
|
|
days={}
|
|
|
|
for tline in obj.time_lines:
|
|
|
|
if tline.day and tline.cycle_id and tline.time_start and tline.time_stop:
|
|
|
|
day=tline.day
|
|
|
|
cycle_id=tline.cycle_id.id
|
|
|
|
time_start=tline.time_start
|
|
|
|
time_stop=tline.time_stop
|
|
|
|
d=0
|
|
|
|
if day=='mon':
|
|
|
|
d=1
|
|
|
|
elif day=='tue':
|
|
|
|
d=2
|
|
|
|
elif day=='wed':
|
|
|
|
d=3
|
|
|
|
elif day=='thu':
|
|
|
|
d=4
|
|
|
|
elif day=='fri':
|
|
|
|
d=5
|
|
|
|
elif day=='sat':
|
|
|
|
d=6
|
|
|
|
elif day=='sun':
|
|
|
|
d=7
|
|
|
|
days.update({
|
|
|
|
d:{
|
|
|
|
'cycle_id': cycle_id,
|
|
|
|
'time_start': time_start,
|
|
|
|
'time_stop': time_stop,
|
|
|
|
}})
|
|
|
|
if not days:
|
|
|
|
raise Exception("Please select Days")
|
|
|
|
|
2014-10-20 05:17:21 +00:00
|
|
|
visit_ids=[]
|
2014-11-30 13:05:14 +00:00
|
|
|
date_from=datetime.strptime(obj.date_from,FMT_DATE)
|
|
|
|
date_to=datetime.strptime(obj.date_to,FMT_DATE)
|
2014-11-01 08:49:27 +00:00
|
|
|
# FIXME add more 1 week for make sure
|
|
|
|
date_to2=(date_to+timedelta(days=7)).strftime(FMT_DATETIME)
|
2014-10-20 05:17:21 +00:00
|
|
|
visit_obj=get_model("clinic.visit")
|
2014-11-01 06:04:30 +00:00
|
|
|
|
|
|
|
patients=[p.patient_id.id for p in obj.patient_lines if p.patient_id]
|
2014-11-01 03:48:22 +00:00
|
|
|
if not patients and obj.patient_type:
|
|
|
|
patients=get_model("clinic.patient").search([['type','=',obj.patient_type]])
|
2014-11-01 17:30:48 +00:00
|
|
|
elif not patients and obj.patient_categ_id:
|
|
|
|
patients=get_model("clinic.patient").search([['categ_id','=',obj.patient_categ_id.id]])
|
2014-11-01 06:04:30 +00:00
|
|
|
elif patients:
|
|
|
|
pass
|
2014-11-01 03:48:22 +00:00
|
|
|
else:
|
2014-11-01 06:04:30 +00:00
|
|
|
dom=[]
|
|
|
|
dom.append(['time_start','>=','%s %s'%(obj.date_from[0:10],' 00:00:00')])
|
2014-11-01 08:49:27 +00:00
|
|
|
dom.append(['time_stop','<=','%s %s'%(date_to2[0:10],' 23:59:59')])
|
2014-12-19 18:19:19 +00:00
|
|
|
dom.append(['state','=','pending'])
|
2014-11-01 06:04:30 +00:00
|
|
|
vids=visit_obj.search(dom)
|
2014-11-01 08:49:27 +00:00
|
|
|
visit_obj.delete(vids)
|
2014-11-01 06:04:30 +00:00
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'clinic_gen_visit_form',
|
|
|
|
'mode': 'form',
|
|
|
|
},
|
2014-11-02 01:26:42 +00:00
|
|
|
'flash': 'Clear %s visits'%len(vids),
|
2014-11-01 06:04:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-13 12:08:38 +00:00
|
|
|
day_choices=days.keys()
|
|
|
|
if day_choices:
|
2014-11-01 03:48:22 +00:00
|
|
|
for patient_id in patients:
|
2014-10-20 05:17:21 +00:00
|
|
|
# loop days in weekend
|
|
|
|
ntoday=1
|
|
|
|
day_total=(date_to-date_from).days+ntoday
|
2015-01-13 12:08:38 +00:00
|
|
|
for weekday in day_choices:
|
2014-10-20 05:17:21 +00:00
|
|
|
wd=date_from.weekday()
|
|
|
|
start_date=date_from
|
|
|
|
while wd != weekday-1:
|
|
|
|
start_date+=timedelta(days=1)
|
|
|
|
wd=start_date.weekday()
|
|
|
|
#XXX should gen only in scop
|
|
|
|
if start_date.strftime(FMT_DATE) > date_to.strftime(FMT_DATE):
|
|
|
|
continue
|
|
|
|
count=0
|
2014-11-01 17:30:48 +00:00
|
|
|
tmp=start_date
|
2014-10-20 05:17:21 +00:00
|
|
|
while count < day_total:
|
|
|
|
tmp=start_date+timedelta(days=count)
|
|
|
|
time_start="%s 00:00:00" % (tmp.strftime(FMT_DATE))
|
|
|
|
time_stop="%s 23:59:59" % (tmp.strftime(FMT_DATE))
|
|
|
|
dom=[]
|
|
|
|
dom.append(['time_start','>=','%s'%time_start])
|
|
|
|
dom.append(['time_stop','<=','%s'%time_stop])
|
|
|
|
dom.append(['patient_id','=','%s'%patient_id])
|
2015-01-13 12:08:38 +00:00
|
|
|
dom.append(['cycle_id','=','%s'%days[weekday]['cycle_id']])
|
2014-12-19 18:19:19 +00:00
|
|
|
dom.append(['state','=','pending'])
|
2014-10-20 05:17:21 +00:00
|
|
|
count+=7
|
|
|
|
vids=visit_obj.search(dom)
|
2015-01-13 12:08:38 +00:00
|
|
|
print('x dom ', vids)
|
2014-10-20 05:17:21 +00:00
|
|
|
if vids:
|
|
|
|
visit_ids.append(vids[0])
|
|
|
|
start_date=tmp
|
2015-01-13 12:08:38 +00:00
|
|
|
else:
|
|
|
|
# discard
|
|
|
|
time_start=obj.date_from
|
|
|
|
time_stop=obj.date_to
|
|
|
|
for patient_id in patients:
|
|
|
|
dom=[]
|
|
|
|
dom.append(['time_start','>=','%s'%time_start])
|
|
|
|
dom.append(['time_stop','<=','%s'%time_stop])
|
|
|
|
dom.append(['patient_id','=','%s'%patient_id])
|
|
|
|
dom.append(['state','=','pending'])
|
|
|
|
print('dom ', dom)
|
|
|
|
vids=visit_obj.search(dom)
|
|
|
|
if vids:
|
|
|
|
visit_ids.append(vids[0])
|
2014-10-20 05:17:21 +00:00
|
|
|
if visit_ids:
|
|
|
|
get_model("clinic.visit").delete(visit_ids)
|
2015-01-13 12:08:38 +00:00
|
|
|
|
2014-10-20 05:17:21 +00:00
|
|
|
return {
|
|
|
|
'next': {
|
2014-11-01 03:48:22 +00:00
|
|
|
'name': 'clinic_visit',
|
2014-10-20 05:17:21 +00:00
|
|
|
'mode': 'list',
|
|
|
|
},
|
2014-11-02 01:26:42 +00:00
|
|
|
'flash': 'Clear %s visits'%len(visit_ids),
|
2014-10-20 05:17:21 +00:00
|
|
|
}
|
|
|
|
|
2015-01-13 12:08:38 +00:00
|
|
|
def onchange_cycle_line(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
path=context['path']
|
|
|
|
line=get_data_path(data,path,parent=True)
|
|
|
|
cycle_id=line['cycle_id']
|
|
|
|
if cycle_id:
|
|
|
|
cycle=get_model('clinic.cycle').browse(cycle_id)
|
|
|
|
line['time_start']=cycle.time_start
|
|
|
|
line['time_stop']=cycle.time_stop
|
|
|
|
return data
|
|
|
|
|
2015-01-13 13:45:11 +00:00
|
|
|
|
2014-10-14 13:48:14 +00:00
|
|
|
GenVisit.register()
|
|
|
|
|