244 lines
9.1 KiB
Python
244 lines
9.1 KiB
Python
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
|
|
|
|
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
|
|
|
|
_fields={
|
|
'date_from': fields.DateTime("From", required=True),
|
|
'date_to': fields.DateTime("To", required=True),
|
|
'cycle_id': fields.Many2One("clinic.cycle","Cycle", required=True),
|
|
'lines': fields.One2Many("clinic.gen.visit.line","gen_id","Lines"),
|
|
'monday': fields.Boolean("Monday"),
|
|
'tuesday': fields.Boolean("Tuesdays"),
|
|
'wednesday': fields.Boolean("Wednesdays"),
|
|
'thursday': fields.Boolean("Thursdays"),
|
|
'friday': fields.Boolean("Fridays"),
|
|
'doctor_id': fields.Many2One("clinic.doctor","Doctor"),
|
|
'nurse_id': fields.Many2One("clinic.nurse","Nurse"),
|
|
'department_id': fields.Many2One("clinic.department","Department"),
|
|
'duration': fields.Integer("Duration (hrs)", function="_get_duration"),
|
|
}
|
|
|
|
def _get_lines(self,context={}):
|
|
ids=context.get("ids")
|
|
if not ids:
|
|
ids=[context.get("refer_id")]
|
|
lines=[{'patient_id': id} for id in ids]
|
|
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
|
|
|
|
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
|
|
|
|
_defaults={
|
|
'lines': _get_lines,
|
|
'date_from': lambda *a: datetime.now().strftime(FMT_DATETIME),
|
|
'date_to': lambda *a: datetime.now().strftime(FMT_DATETIME),
|
|
'nurse_id': _get_nurse,
|
|
'duration': 1,
|
|
'cycle_id': _get_cycle,
|
|
'monday': True,
|
|
}
|
|
|
|
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:])
|
|
duration=(datetime.strptime(data['date_to'],FMT_DATETIME)-datetime.strptime(data['date_from'],FMT_DATETIME)).seconds/3600
|
|
data['duration']=duration
|
|
return data
|
|
|
|
def onchange_dateto(self,context={}):
|
|
data=context['data']
|
|
data['duration']=(datetime.strptime(data['date_to'],FMT_DATETIME)-datetime.strptime(data['date_from'],FMT_DATETIME)).seconds/3600
|
|
return data
|
|
|
|
def gen_visit(self,ids,context):
|
|
obj=self.browse(ids)[0]
|
|
visit_obj=get_model('clinic.visit')
|
|
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]
|
|
if not days:
|
|
raise Exception("Please select Day")
|
|
visit_vals=[]
|
|
date_from=datetime.strptime(obj.date_from,FMT_DATETIME)
|
|
date_to=datetime.strptime(obj.date_to,FMT_DATETIME)
|
|
for line in obj.lines:
|
|
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 scope
|
|
if start_date.strftime(FMT_DATE) > date_to.strftime(FMT_DATE):
|
|
continue
|
|
count=0
|
|
while count < day_total:
|
|
tmp=start_date+timedelta(days=count)
|
|
vals={
|
|
'patient_id': line.patient_id.id,
|
|
'doctor_id': obj.doctor_id.id,
|
|
'nurse_id': obj.nurse_id.id,
|
|
'department_id': obj.department_id.id,
|
|
'cycle_id': obj.cycle_id.id,
|
|
'time_start': "%s %s" % (tmp.strftime(FMT_DATE),date_from.strftime(FMT_DATETIME)[11:]),
|
|
'time_stop': "%s %s" % (tmp.strftime(FMT_DATE),date_to.strftime(FMT_DATETIME)[11:]),
|
|
'state': 'draft',
|
|
}
|
|
visit_vals.append(vals)
|
|
count+=7
|
|
|
|
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', '=', line.patient_id.id])
|
|
dom.append(['state','=','draft'])
|
|
vids=visit_obj.search(dom)
|
|
visit_obj.delete(vids)
|
|
start_date=tmp
|
|
|
|
for vals in visit_vals:
|
|
visit_obj.create(vals)
|
|
|
|
return {
|
|
'next': {
|
|
'name': 'clinic_patient',
|
|
'mode': 'list',
|
|
},
|
|
'flash': 'Generate OK',
|
|
}
|
|
|
|
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)
|
|
visit_obj=get_model("clinic.visit")
|
|
if days:
|
|
for line in obj.lines:
|
|
# 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
|
|
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))
|
|
patient_id=line.patient_id.id
|
|
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
|
|
print(dom)
|
|
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
|
|
for line in obj.lines:
|
|
patient_id=line.patient_id.id
|
|
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:
|
|
print("visit_ids ", len(visit_ids))
|
|
get_model("clinic.visit").delete(visit_ids)
|
|
return {
|
|
'next': {
|
|
'name': 'clinic_patient',
|
|
'mode': 'list',
|
|
},
|
|
'flash': 'Clear OK',
|
|
}
|
|
|
|
def _clear_visit(self,ids,context):
|
|
obj=self.browse(ids)[0]
|
|
visit_obj=get_model("clinic.visit")
|
|
for line in obj.lines:
|
|
patient_id=line.patient_id
|
|
dom=[]
|
|
dom.append(['patient_id','=',patient_id.id])
|
|
dom.append(['state','=','draft'])
|
|
# clear
|
|
# only from to and day
|
|
visit_ids=visit_obj.search(dom)
|
|
visit_obj.delete(visit_ids)
|
|
return {
|
|
'next': {
|
|
'name': 'clinic_patient',
|
|
'mode': 'list',
|
|
},
|
|
'flash': 'Clear OK',
|
|
}
|
|
|
|
GenVisit.register()
|
|
|