clinic/netforce_clinic/models/make_apt.py

219 lines
7.2 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
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),
"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={}
for pc in get_model("clinic.patient.cycle").search_browse([]):
patient=pc.patient_id
dpt=pc.department_id
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': 'Reload Successfully',
}
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
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
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])
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',
}
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', '=', patient.id])
dom.append(['cycle_id', '=', cycle.id])
dom.append(['state','=','pending'])
vids=visit_obj.search(dom)
visit_obj.delete(vids)
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."%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
MakeAPT.register()