clinic/netforce_clinic/models/visit_board.py

406 lines
15 KiB
Python
Raw Normal View History

2014-11-25 10:26:10 +00:00
import time
2014-12-02 08:34:28 +00:00
from calendar import monthrange
2014-11-25 10:26:10 +00:00
from datetime import datetime, timedelta
2015-03-13 16:31:58 +00:00
2014-11-25 10:26:10 +00:00
from netforce.model import Model, fields, get_model
2015-05-07 08:10:23 +00:00
from netforce.access import get_active_company
2014-11-25 10:26:10 +00:00
from . import utils
2014-12-20 09:48:29 +00:00
DRT=0
HD_STATE={
"draft":"Draft",
'waiting_treatment':'Waiting Treatment',
"in_progress":"In Progress",
"completed":"Finish Treatment",
'paid':'Paid',
"waiting_payment":"Waiting Payment",
"discountinued":"Discountinued",
"cancelled":"Cancelled"
}
2014-11-25 10:26:10 +00:00
2015-05-07 08:10:23 +00:00
DAYS={
'mon': 0,
'tue': 1,
'wed': 2,
'thu': 3,
'fri': 4,
'sat': 5,
'sun': 6,
}
2014-11-25 10:26:10 +00:00
class VisitBoard(Model):
_name="clinic.visit.board"
_string="Visit Board"
_transient=True
2015-01-21 08:29:14 +00:00
_name_field="date"
2015-01-21 11:31:51 +00:00
2014-11-25 10:26:10 +00:00
_fields={
2015-01-11 13:58:51 +00:00
"date": fields.Date("Month", required=False),
2014-11-25 10:26:10 +00:00
"date_from": fields.Date("From", required=True),
"date_to": fields.Date("To", required=True),
2015-03-02 04:03:07 +00:00
'patient_id': fields.Many2One("clinic.patient","Patient",domain=[['state','=','admit']]),
2015-01-15 15:38:28 +00:00
'cycle_id': fields.Many2One("clinic.cycle","Cycle"),
'doctor_id': fields.Many2One("clinic.staff","Doctor",domain=[["type","=","doctor"]]),
2015-01-19 05:34:50 +00:00
'department_id': fields.Many2One("clinic.department","Department"),
'branch_id': fields.Many2One("clinic.branch","Branch"),
2014-11-25 10:26:10 +00:00
}
2015-01-21 11:31:51 +00:00
2015-03-14 03:57:18 +00:00
def default_get(self,field_names=None,context={},**kw):
defaults=context.get("defaults",{})
date=defaults.get('date',time.strftime("%Y-%m-%d"))
date_from=defaults.get("date_from",time.strftime("%Y-%m-%d"))
date_to=defaults.get("date_to")
if not date_to:
date_to=(datetime.now()+timedelta(days=DRT)).strftime("%Y-%m-%d")
branch_id=defaults.get("branch_id")
department_id=defaults.get("department_id")
2015-03-13 06:43:12 +00:00
res=get_model('select.company').get_select()
if res:
2015-03-14 03:57:18 +00:00
if not branch_id:
branch_id=res['branch_id']
if not department_id:
2015-03-15 15:42:15 +00:00
if res.get('department_ids'):
department_id=res['department_ids'][0]
else:
department_id=res['department_id']
2015-03-14 03:57:18 +00:00
res={
'date': date,
'date_from': date_from,
'date_to': date_to,
'branch_id': branch_id,
'department_id': department_id,
}
return res
2014-11-25 10:26:10 +00:00
def get_report_data(self,ids,context={}):
company_id=get_active_company()
company=get_model("company").browse(company_id)
date_from=datetime.now().strftime("%Y-%m-%d")
date_to=(datetime.now()+timedelta(days=DRT)).strftime("%Y-%m-%d")
patient_id=None
2015-01-15 15:38:28 +00:00
cycle_id=None
doctor_id=None
2015-03-13 06:43:12 +00:00
defaults=self.default_get(context=context)
department_id=defaults.get("department_id",None)
branch_id=defaults.get("branch_id",None)
2015-03-14 03:57:18 +00:00
print('defaults ', defaults)
2014-11-25 10:26:10 +00:00
if ids:
obj=self.browse(ids)[0]
date_from=obj.date_from
date_to=obj.date_to
patient_id=obj.patient_id.id
2015-01-15 15:38:28 +00:00
cycle_id=obj.cycle_id.id
doctor_id=obj.doctor_id.id
2015-01-19 03:08:14 +00:00
department_id=obj.department_id.id
2015-01-19 05:34:50 +00:00
branch_id=obj.branch_id.id
2015-05-07 08:10:23 +00:00
def auto_gen_visit(dom=[]):
2015-05-08 01:09:12 +00:00
dom.append(['dispose','=',False])
dom.append(['walkin','=','no'])
2015-05-07 08:10:23 +00:00
def daterange(start_date, end_date):
for n in range(int ((end_date - start_date).days)):
yield start_date + timedelta(n)
def convert_date(date_txt):
try:
if not date_txt:
raise Exception("Date Empty")
return datetime.strptime(date_txt,"%Y-%m-%d")
except:
raise Exception("Wrong Format Date")
# IMPROVE
# check day of week of this day
for date in daterange(convert_date(date_from),convert_date(date_to)+timedelta(days=1)):
weekday=date.weekday()
date_txt=date.strftime("%Y-%m-%d")
2015-05-08 01:09:12 +00:00
datenow=time.strftime("%Y-%m-%d")
if date_txt < datenow:
print("continue ", date_txt, datenow)
continue
2015-05-07 08:10:23 +00:00
for pt in get_model("clinic.patient").search_browse(dom):
for pc in pt.cycles:
w=DAYS.get(pc.day,0) #default monday
cycle=pc.cycle_id
department=pc.department_id
if weekday==w:
dom2=[
['visit_date','>=',date_txt],
['visit_date','<=',date_txt],
['patient_id','=',pt.id],
['cycle_id','=',cycle.id],
['department_id','=',department.id],
]
res=get_model("clinic.visit").search(dom2)
# create visit auto
if not res:
vals={
'patient_id': pt.id,
'department_id': department.id,
'cycle_id': cycle.id,
'doctor_id': pt.doctor_id.id,
'branch_id': department.branch_id.id,
'time_start': '%s %s:00'%(date_txt,cycle.time_start),
'time_stop': '%s %s:00'%(date_txt,cycle.time_stop),
'visit_date': date_txt,
'state': 'pending',
}
visit_id=get_model("clinic.visit").create(vals)
print('create new visit %s for %s'%(visit_id,pt.name))
2014-11-25 10:26:10 +00:00
time_start='%s 00:00:00'%(date_from)
time_stop='%s 23:59:59'%(date_to)
dom=[]
dom.append(['time_start','>=','%s'%time_start])
dom.append(['time_stop','<=','%s'%time_stop])
if patient_id:
dom.append(['patient_id','=',patient_id])
2015-01-15 15:38:28 +00:00
if cycle_id:
dom.append(['cycle_id','=',cycle_id])
if doctor_id:
dom.append(['doctor_id','=',doctor_id])
2015-01-19 03:08:14 +00:00
if department_id:
dom.append(['department_id','=',department_id])
2015-01-19 05:34:50 +00:00
if branch_id:
dom.append(['branch_id','=',branch_id])
2015-05-07 08:10:23 +00:00
#gen visit
if department_id:
auto_gen_visit(dom=[['department_id','=',department_id]])
elif branch_id:
auto_gen_visit(dom=[['branch_id','=',branch_id]])
2014-11-25 10:26:10 +00:00
lines=[]
empty_line={
'no': '',
'number': '',
'visit_id': None,
2015-04-30 15:47:56 +00:00
'visit_state': '',
2014-11-25 10:26:10 +00:00
'cycle_name': '',
'cycle_color': '',
2015-01-16 11:19:49 +00:00
'department_id': None,
'department_name': None,
'branch_id': None,
'branch_name': None,
2014-11-26 07:35:09 +00:00
'patient_id': None,
2014-11-25 10:26:10 +00:00
'patient_name': '',
'patient_type': '',
2014-12-20 09:48:29 +00:00
'patient_type_id': None,
2014-11-25 10:26:10 +00:00
'doctor_name': '',
2014-11-26 07:35:09 +00:00
'doctor_id': None,
2014-11-25 10:26:10 +00:00
'hd_case_number': '',
'hd_case_id': None,
'success_color': '',
'date':'',
'title': True,
2014-12-20 09:48:29 +00:00
'footer': False,
2014-11-25 10:26:10 +00:00
'details':'',
2014-12-20 09:48:29 +00:00
'hd_case_state': '',
2014-12-20 10:09:27 +00:00
'hn': '',
2014-11-25 10:26:10 +00:00
}
2014-12-02 07:08:20 +00:00
patient_types={t['id']:t['name'] for t in get_model("clinic.patient.type").search_read([[]],['name'])}
2014-12-20 10:54:34 +00:00
cycle_names={t['id']:t['name'] for t in get_model("clinic.cycle").search_read([[]],['name'])}
2015-01-19 08:30:51 +00:00
brch_names={t['id']:t['name'] for t in get_model("clinic.branch").search_read([[]],['name'])}
dpt_names={t['id']:t['name'] for t in get_model("clinic.department").search_read([[]],['name'])}
2014-12-20 16:03:25 +00:00
total_wait=0
total_done=0
total_cancel=0
2014-11-25 10:26:10 +00:00
types={}
2014-12-20 10:54:34 +00:00
cycles={}
2014-11-25 10:26:10 +00:00
no=1
2015-01-19 08:30:51 +00:00
dpt={}
brch={}
2014-11-25 10:26:10 +00:00
for visit in get_model("clinic.visit").search_browse(dom):
2014-12-20 16:03:25 +00:00
if visit.state in ('draft','pending'):
total_wait+=1
elif visit.state in ('confirmed'):
total_done+=1
else:
total_cancel+=1
2014-11-25 10:26:10 +00:00
hd_case_id=None
hd_case_number=''
2014-12-20 09:48:29 +00:00
hd_case_state=''
visit_color=''
2014-12-20 16:03:25 +00:00
sickbed_name='N/A'
sickbed_id=None
2014-11-25 10:26:10 +00:00
if visit.hd_cases:
hd_case=visit.hd_cases[0]
2014-12-20 16:03:25 +00:00
sickbed_name=hd_case.sickbed_id.name or "N/A"
sickbed_id=hd_case.sickbed_id.id
2014-11-25 10:26:10 +00:00
hd_case_id=hd_case.id,
2014-12-20 09:48:29 +00:00
if hd_case.number=='/':
2015-04-30 09:41:34 +00:00
hd_case_number='Waiting'
2014-12-20 09:48:29 +00:00
else:
hd_case_number=hd_case.number
hd_case_state=hd_case.state
if hd_case_state=='completed':
2015-04-30 15:10:55 +00:00
#visit_color='#99ff99'
visit_color=''
2014-12-20 16:03:25 +00:00
elif hd_case_state=='in_progress':
2015-04-30 15:10:55 +00:00
#visit_color='#f9e37d'
visit_color=''
2015-04-30 15:47:56 +00:00
elif hd_case_state=='cancelled':
visit_color='#dbdbdb',
hd_case_number="Cancelled"
2014-11-25 10:26:10 +00:00
number=visit.number
if number=='/':
2015-04-30 09:41:34 +00:00
number='Waiting'
2014-11-25 10:26:10 +00:00
cycle=visit.cycle_id
patient=visit.patient_id
2015-02-20 05:16:51 +00:00
if not patient.active:
print('skip not active ', patient.name)
continue
2015-01-16 11:19:49 +00:00
branch=visit.branch_id
department=visit.department_id
if not branch:
branch=department.branch_id
2015-01-20 13:58:38 +00:00
2015-01-11 05:29:32 +00:00
hn_name=patient.hn_no or '-'
2014-12-20 16:03:25 +00:00
2014-11-25 10:26:10 +00:00
visit_date=visit.visit_date
2014-12-20 16:03:25 +00:00
if visit.state=='cancelled':
visit_color='#dbdbdb',
if visit.number=='/':
2015-04-30 09:41:34 +00:00
number='Cancelled'
2014-12-20 16:03:25 +00:00
else:
2015-04-30 09:41:34 +00:00
number+='Cancelled'
2014-11-25 10:26:10 +00:00
line={
'number': number,
2014-12-20 10:09:27 +00:00
'hn_name': hn_name,
2014-11-25 10:26:10 +00:00
'visit_id': visit.id,
2014-12-20 16:03:25 +00:00
'sickbed_name': sickbed_name,
'sickbed_id': sickbed_id,
2014-11-25 10:26:10 +00:00
'cycle_name': cycle.name,
'cycle_color': cycle.color,
2015-01-16 11:19:49 +00:00
'department_id': department.id,
'department_name': department.name or '',
2015-01-20 13:58:38 +00:00
'branch_id': branch and branch.id or None,
'branch_name': branch and branch.name or '',
2014-11-25 10:26:10 +00:00
'patient_name': patient.name,
2014-11-26 07:35:09 +00:00
'patient_id': patient.id,
2014-12-02 07:08:20 +00:00
'patient_type': patient.type_id.name or "",
2014-12-20 09:48:29 +00:00
'patient_type_id': patient.type_id.id or None,
2014-11-25 10:26:10 +00:00
'doctor_name': visit.doctor_id.name,
2014-11-26 07:35:09 +00:00
'doctor_id': visit.doctor_id.id,
2014-11-25 10:26:10 +00:00
'hd_case_number': hd_case_number,
2015-04-30 09:41:34 +00:00
'hd_case_state': hd_case_state,
'hd_case_state_txt':HD_STATE.get(hd_case_state,''),
2014-11-25 10:26:10 +00:00
'hd_case_id': hd_case_id,
'visit_color': visit_color,
'date': visit_date,
'title': False,
2014-12-20 09:48:29 +00:00
'footer': False,
2014-11-25 10:26:10 +00:00
'details':'',
2015-01-09 07:48:01 +00:00
'details1':'',
'details2':'',
'details3':'',
2015-01-19 08:30:51 +00:00
'details4':'',
'details5':'',
2014-11-25 10:26:10 +00:00
'no': no,
2014-12-20 09:48:29 +00:00
'note': visit.note,
2014-11-25 10:26:10 +00:00
}
lines.append(line)
no+=1
if not types.get(visit_date):
ptype={}
2014-12-02 07:08:20 +00:00
[ptype.setdefault(t,0) for t in patient_types.keys()]
2014-11-25 10:26:10 +00:00
types[visit_date]=ptype
2014-12-02 07:08:20 +00:00
types[visit_date][patient.type_id.id]+=1
2014-12-20 10:54:34 +00:00
if not cycles.get(visit_date):
cycle_name={}
[cycle_name.setdefault(cid,0) for cid in cycle_names.keys()]
cycles[visit_date]=cycle_name
cycles[visit_date][cycle.id]+=1
2015-01-19 08:30:51 +00:00
if not brch.get(visit_date):
brch_name={}
[brch_name.setdefault(cid,0) for cid in brch_names.keys()]
brch[visit_date]=brch_name
brch[visit_date][branch.id]+=1
if not dpt.get(visit_date):
dpt_name={}
[dpt_name.setdefault(cid,0) for cid in dpt_names.keys()]
dpt[visit_date]=dpt_name
dpt[visit_date][department.id]+=1
2014-11-25 10:26:10 +00:00
dates=[]
index=0
2014-12-20 09:48:29 +00:00
total_qty=0
count=0
2014-11-25 10:26:10 +00:00
for line in lines:
date=line['date']
2014-12-20 09:48:29 +00:00
if not date:
continue
2014-11-25 10:26:10 +00:00
if date not in dates:
2014-12-20 09:48:29 +00:00
count=0
2014-11-25 10:26:10 +00:00
total_qty=0
2014-12-20 09:48:29 +00:00
line=empty_line.copy()
2014-11-25 10:26:10 +00:00
for qty in types[date].values():
total_qty+=qty
line['cycle_name']=utils.date2thai(date,format='%(Td)s %(d)s %(Tm)s',lang="th_TH2"),
lines.insert(index,line)
dates.append(date)
2014-12-21 02:54:45 +00:00
if count==total_qty and not patient_id:
2014-12-20 09:48:29 +00:00
index+=1
# footer
line=empty_line.copy()
2015-01-16 11:19:49 +00:00
patient_str='%s'%', '.join('%s: %s'%(patient_types[k],v) for k,v in types[date].items())
cycle_str='%s'%', '.join('%s: %s'%(cycle_names[k],v) for k,v in cycles[date].items())
2015-01-19 08:30:51 +00:00
brch_str='%s'%', '.join('%s: %s'%(brch_names[k],v) for k,v in brch[date].items())
dpt_str='%s'%', '.join('%s: %s'%(dpt_names[k],v) for k,v in dpt[date].items())
2015-01-16 11:19:49 +00:00
summary_str='%s'%', '.join(['รับไว้: %s'%total_wait,'จำหน่ายแล้ว: %s'%total_done, 'ยกเลิก: %s'%total_cancel])
2014-12-20 16:03:25 +00:00
line['details']='ทั้งหมด %s: %s'%(total_qty,', '.join([summary_str,cycle_str,patient_str]))
2015-01-19 08:39:14 +00:00
line['details1']=brch_str
2015-01-19 08:30:51 +00:00
line['details2']=summary_str
line['details3']=cycle_str
2015-01-19 08:39:14 +00:00
line['details4']=patient_str
line['details5']=dpt_str
2014-12-20 09:48:29 +00:00
line['footer']=True
line['title']=False
lines.insert(index,line)
count+=1
2014-11-25 10:26:10 +00:00
index+=1
has_duration=False
if date_from != date_to:
has_duration=True
data={
'lines': lines,
'date': utils.date2thai(date_from,format='ประจำวัน%(Td)s ที่ %(d)s %(Tm)s %(BY)s'),
'company_name': company.name,
'company_parent_name': company.parent_id.name,
'has_duration': has_duration,
'date_from': utils.date2thai(date_from,format='%(d)s %(Tm)s %(By)s',lang="th_TH2"),
'date_to': utils.date2thai(date_to,format='%(d)s %(Tm)s %(By)s',lang="th_TH2"),
}
return data
2014-12-02 08:34:28 +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-01-22 07:20:04 +00:00
def onchange_datefrom(self,context={}):
data=context['data']
data['date_to']=data['date_from']
return data
def onchange_branch(self,context={}):
data=context['data']
data['department_id']=None
return data
2015-01-21 08:29:14 +00:00
def confirm(self,ids,context={}):
obj=self.browse(ids)[0]
return {
'next': {
'refer_id': obj.id,
'name': 'clinic_popup_visit_confirm',
}
}
2015-01-22 10:40:05 +00:00
2014-11-25 10:26:10 +00:00
VisitBoard.register()