2014-10-14 13:48:14 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
from netforce.access import get_active_user
|
|
|
|
from netforce.model import Model, fields, get_model
|
|
|
|
|
|
|
|
FMT_DATE="%Y-%m-%d"
|
|
|
|
FMT_DATETIME="%Y-%m-%d %H:%M:%S"
|
|
|
|
|
|
|
|
class GenVisit(Model):
|
|
|
|
_name="clinic.gen.visit"
|
|
|
|
_transient=True
|
|
|
|
|
2014-10-29 04:40:35 +00:00
|
|
|
|
2014-10-14 13:48:14 +00:00
|
|
|
def _get_duration(self,ids,context={}):
|
|
|
|
res={}
|
|
|
|
for obj in self.browse(ids):
|
|
|
|
duration=datetime.strptime(obj.date_to,FMT_DATETIME)-datetime.strptime(obj.date_from,FMT_DATETIME)
|
|
|
|
res[obj.id]=duration.days
|
|
|
|
return res
|
2014-10-29 04:40:35 +00:00
|
|
|
|
|
|
|
|
2014-10-14 13:48:14 +00:00
|
|
|
_fields={
|
|
|
|
'date_from': fields.DateTime("From", required=True),
|
|
|
|
'date_to': fields.DateTime("To", required=True),
|
|
|
|
'cycle_id': fields.Many2One("clinic.cycle","Cycle", required=True),
|
2014-11-01 03:48:22 +00:00
|
|
|
'patient_lines': fields.One2Many("clinic.gen.visit.line","gen_id","Patient Lines"),
|
|
|
|
'nurse_lines': fields.One2Many("clinic.gen.visit.line","gen_id","Nurse Lines"),
|
2014-10-14 13:48:14 +00:00
|
|
|
'monday': fields.Boolean("Monday"),
|
|
|
|
'tuesday': fields.Boolean("Tuesdays"),
|
|
|
|
'wednesday': fields.Boolean("Wednesdays"),
|
|
|
|
'thursday': fields.Boolean("Thursdays"),
|
|
|
|
'friday': fields.Boolean("Fridays"),
|
2014-10-28 13:39:29 +00:00
|
|
|
'sathurday': fields.Boolean("Sathurday"),
|
|
|
|
'sunday': fields.Boolean("Sunday"),
|
2014-11-01 17:43:54 +00:00
|
|
|
'doctor_id': fields.Many2One("clinic.personal","Doctor",domain=[['type','=','doctor']]),
|
2014-10-24 08:08:56 +00:00
|
|
|
'department_id': fields.Many2One("clinic.department","Department"),
|
2014-10-15 03:49:20 +00:00
|
|
|
'duration': fields.Integer("Duration (hrs)", function="_get_duration"),
|
2014-11-01 03:48:22 +00:00
|
|
|
"patient_type": fields.Selection([("mg","Medical Government"),("sc","Social Security"),("nhso","NHSO (30฿)"),("personal","Personal"),("others","Others")],"Patient Type"),
|
2014-11-01 17:30:48 +00:00
|
|
|
'patient_categ_id': fields.Many2One("clinic.patient.categ", "Patient Category"),
|
2014-11-01 03:48:22 +00:00
|
|
|
'nurse_categ_id': fields.Many2One("clinic.personal.categ", "Nurse Category", domain=[['type','=','nurse']]),
|
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=[]
|
|
|
|
# XXX [None]
|
|
|
|
if any(ids):
|
|
|
|
lines=[{'patient_id': id} for id in ids]
|
2014-10-14 13:48:14 +00:00
|
|
|
return lines
|
|
|
|
|
|
|
|
def _get_nurse(self,context={}):
|
|
|
|
user_id=get_active_user()
|
|
|
|
print("user_id ",user_id)
|
|
|
|
nurse_ids=get_model("clinic.nurse").search([['user_id','=',user_id]])
|
|
|
|
if nurse_ids:
|
|
|
|
return nurse_ids[0]
|
|
|
|
return None
|
|
|
|
|
2014-10-15 03:49:20 +00:00
|
|
|
def _get_cycle(self,context={}):
|
|
|
|
cycle_ids=get_model('clinic.cycle').search([],order="sequence")
|
|
|
|
if cycle_ids:
|
|
|
|
cycle_id=cycle_ids[0]
|
|
|
|
return cycle_id or None
|
|
|
|
|
2014-10-14 13:48:14 +00:00
|
|
|
_defaults={
|
2014-11-01 03:48:22 +00:00
|
|
|
'patient_lines': _get_patient_lines,
|
2014-10-14 13:48:14 +00:00
|
|
|
'date_from': lambda *a: datetime.now().strftime(FMT_DATETIME),
|
2014-11-01 03:48:22 +00:00
|
|
|
'date_to': lambda *a: (datetime.now()+timedelta(days=7)).strftime(FMT_DATETIME),
|
2014-10-14 13:48:14 +00:00
|
|
|
'nurse_id': _get_nurse,
|
|
|
|
'duration': 1,
|
2014-10-15 03:49:20 +00:00
|
|
|
'cycle_id': _get_cycle,
|
|
|
|
'monday': True,
|
2014-10-14 13:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def onchange_time(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
cycle_id=data['cycle_id']
|
|
|
|
duration=1
|
|
|
|
if cycle_id:
|
|
|
|
cycle=get_model('clinic.cycle').browse(cycle_id)
|
|
|
|
duration=cycle.duration or 0
|
|
|
|
seconds=duration*3600
|
|
|
|
date_from=data['date_from']
|
|
|
|
date_to=(datetime.strptime(date_from,FMT_DATETIME)+timedelta(seconds=seconds)).strftime(FMT_DATETIME)
|
|
|
|
data['date_to']='%s %s'%(data['date_to'][0:10],date_to[11:])
|
2014-10-15 03:49:20 +00:00
|
|
|
duration=(datetime.strptime(data['date_to'],FMT_DATETIME)-datetime.strptime(data['date_from'],FMT_DATETIME)).seconds/3600
|
|
|
|
data['duration']=duration
|
2014-10-14 13:48:14 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def onchange_dateto(self,context={}):
|
|
|
|
data=context['data']
|
2014-10-15 03:49:20 +00:00
|
|
|
data['duration']=(datetime.strptime(data['date_to'],FMT_DATETIME)-datetime.strptime(data['date_from'],FMT_DATETIME)).seconds/3600
|
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')
|
2014-10-14 13:48:14 +00:00
|
|
|
days=[
|
|
|
|
obj.monday and 1 or 0,
|
|
|
|
obj.tuesday and 2 or 0,
|
|
|
|
obj.wednesday and 3 or 0,
|
|
|
|
obj.thursday and 4 or 0,
|
|
|
|
obj.friday and 5 or 0,
|
2014-10-28 13:39:29 +00:00
|
|
|
obj.sathurday and 6 or 0,
|
|
|
|
obj.sunday and 7 or 0,
|
2014-10-14 13:48:14 +00:00
|
|
|
]
|
|
|
|
days=[day for day in days if day]
|
|
|
|
if not days:
|
|
|
|
raise Exception("Please select Day")
|
2014-10-15 03:49:20 +00:00
|
|
|
visit_vals=[]
|
|
|
|
date_from=datetime.strptime(obj.date_from,FMT_DATETIME)
|
|
|
|
date_to=datetime.strptime(obj.date_to,FMT_DATETIME)
|
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
|
|
|
if not patients:
|
|
|
|
raise Exception("No patient for type %s"%obj.patient_type)
|
|
|
|
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")
|
|
|
|
|
2014-11-03 00:21:37 +00:00
|
|
|
nurse_vals=[]
|
2014-11-02 07:13:20 +00:00
|
|
|
for nurse_line in obj.nurse_lines:
|
2014-11-03 00:21:37 +00:00
|
|
|
nurse=nurse_line.nurse_id
|
2014-11-02 07:13:20 +00:00
|
|
|
# None
|
2014-11-03 00:21:37 +00:00
|
|
|
if nurse:
|
|
|
|
nurse_vals.append({
|
|
|
|
'id': nurse.id,
|
|
|
|
'level_id': nurse.level_id.id,
|
|
|
|
})
|
|
|
|
if not nurse_vals:
|
|
|
|
for nurse in get_model("clinic.personal").search_browse([['type','=','nurse'],['categ_id','=',obj.nurse_categ_id.id]]):
|
|
|
|
nurse_vals.append({
|
|
|
|
'id': nurse.id,
|
|
|
|
'level_id': nurse.level_id.id,
|
|
|
|
})
|
2014-11-01 06:04:30 +00:00
|
|
|
schedules={}
|
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
|
|
|
|
for weekday in days:
|
|
|
|
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
|
2014-10-15 03:49:20 +00:00
|
|
|
while count < day_total:
|
|
|
|
tmp=start_date+timedelta(days=count)
|
2014-11-01 06:04:30 +00:00
|
|
|
ttime_start="%s %s" % (tmp.strftime(FMT_DATE),date_from.strftime(FMT_DATETIME)[11:])
|
|
|
|
ttime_stop="%s %s" % (tmp.strftime(FMT_DATE),date_to.strftime(FMT_DATETIME)[11:])
|
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,
|
2014-10-24 08:08:56 +00:00
|
|
|
'department_id': obj.department_id.id,
|
2014-10-14 13:48:14 +00:00
|
|
|
'cycle_id': obj.cycle_id.id,
|
2014-11-01 06:04:30 +00:00
|
|
|
'time_start': ttime_start,
|
|
|
|
'time_stop': ttime_stop,
|
2014-10-14 13:48:14 +00:00
|
|
|
'state': 'draft',
|
|
|
|
}
|
|
|
|
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])
|
2014-10-26 08:48:51 +00:00
|
|
|
dom.append(['cycle_id', '=', obj.cycle_id.id]) #XXX
|
2014-10-20 05:17:21 +00:00
|
|
|
dom.append(['state','=','draft'])
|
|
|
|
vids=visit_obj.search(dom)
|
|
|
|
visit_obj.delete(vids)
|
2014-11-01 06:04:30 +00:00
|
|
|
key='%s-%s'%(vals['cycle_id'], vals['time_start'])
|
|
|
|
if not schedules.get(key):
|
|
|
|
schedules[key]={
|
|
|
|
'cycle_id': obj.cycle_id.id,
|
|
|
|
'time_start': ttime_start,
|
|
|
|
'time_stop': ttime_stop,
|
|
|
|
'state':'draft',
|
|
|
|
}
|
|
|
|
|
2014-10-15 03:49:20 +00:00
|
|
|
start_date=tmp
|
|
|
|
|
2014-10-20 05:17:21 +00:00
|
|
|
for vals in visit_vals:
|
2014-11-03 00:21:37 +00:00
|
|
|
vals['nurse_vals']=nurse_vals # XXX
|
2014-10-20 05:17:21 +00:00
|
|
|
visit_obj.create(vals)
|
2014-11-01 06:04:30 +00:00
|
|
|
dom=[]
|
|
|
|
dom.append(['time_start','>=','%s %s'%(obj.date_from[0:10],' 00:00:00')])
|
|
|
|
dom.append(['time_stop','<=','%s %s'%(start_date.strftime(FMT_DATE)[0:10],' 23:59:59')])
|
|
|
|
dom.append(['cycle_id','=',obj.cycle_id.id])
|
|
|
|
schedule_obj=get_model("clinic.schedule")
|
|
|
|
schedule_ids=schedule_obj.search(dom)
|
|
|
|
schedule_obj.delete(schedule_ids)
|
2014-11-03 00:21:37 +00:00
|
|
|
nurse_ids=[nurse['id'] for nurse in nurse_vals]
|
2014-11-01 06:04:30 +00:00
|
|
|
for key, vals in schedules.items():
|
|
|
|
vals['nurses']=[('add', nurse_ids)]
|
|
|
|
schedule_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]
|
|
|
|
days=[
|
|
|
|
obj.monday and 1 or 0,
|
|
|
|
obj.tuesday and 2 or 0,
|
|
|
|
obj.wednesday and 3 or 0,
|
|
|
|
obj.thursday and 4 or 0,
|
|
|
|
obj.friday and 5 or 0,
|
|
|
|
]
|
|
|
|
days=[day for day in days if day]
|
|
|
|
visit_ids=[]
|
|
|
|
date_from=datetime.strptime(obj.date_from,FMT_DATETIME)
|
|
|
|
date_to=datetime.strptime(obj.date_to,FMT_DATETIME)
|
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
|
|
|
schedule_obj=get_model("clinic.schedule")
|
|
|
|
|
|
|
|
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:
|
2014-11-01 08:49:27 +00:00
|
|
|
# continue to create with critiria
|
2014-11-01 06:04:30 +00:00
|
|
|
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-11-01 06:04:30 +00:00
|
|
|
dom.append(['cycle_id','=',obj.cycle_id.id])
|
|
|
|
dom.append(['state','=','draft'])
|
|
|
|
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
|
|
|
schedule_obj=get_model("clinic.schedule")
|
|
|
|
schedule_ids=schedule_obj.search(dom)
|
|
|
|
schedule_obj.delete(schedule_ids)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-10-20 05:17:21 +00:00
|
|
|
if days:
|
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
|
|
|
|
for weekday in days:
|
|
|
|
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])
|
|
|
|
dom.append(['state','=','draft'])
|
|
|
|
count+=7
|
|
|
|
vids=visit_obj.search(dom)
|
|
|
|
if vids:
|
|
|
|
visit_ids.append(vids[0])
|
|
|
|
start_date=tmp
|
|
|
|
else:
|
|
|
|
time_start=obj.date_from
|
|
|
|
time_stop=obj.date_to
|
2014-11-01 03:48:22 +00:00
|
|
|
for patient_id in patients:
|
2014-10-20 05:17:21 +00:00
|
|
|
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','=','draft'])
|
|
|
|
vids=visit_obj.search(dom)
|
|
|
|
if vids:
|
|
|
|
visit_ids.append(vids[0])
|
|
|
|
if visit_ids:
|
|
|
|
get_model("clinic.visit").delete(visit_ids)
|
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-11-01 06:04:30 +00:00
|
|
|
dom.append(['cycle_id','=',obj.cycle_id.id])
|
|
|
|
schedule_ids=schedule_obj.search(dom)
|
|
|
|
schedule_obj.delete(schedule_ids)
|
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
|
|
|
}
|
|
|
|
|
2014-10-14 13:48:14 +00:00
|
|
|
GenVisit.register()
|
|
|
|
|