2015-03-06 05:15:19 +00:00
|
|
|
import time
|
2015-03-12 10:28:58 +00:00
|
|
|
from calendar import monthrange
|
2015-03-06 05:15:19 +00:00
|
|
|
|
|
|
|
from netforce.model import Model,fields,get_model
|
|
|
|
from netforce.access import get_active_company
|
|
|
|
|
|
|
|
from . import utils
|
|
|
|
|
|
|
|
class ReportCycleItem(Model):
|
|
|
|
_name="clinic.report.cycle.item"
|
|
|
|
_string="Report Cycle Item"
|
|
|
|
_transient=True
|
|
|
|
|
|
|
|
_fields={
|
|
|
|
"date": fields.Date("Month"),
|
|
|
|
"date_from": fields.Date("From", required=True),
|
|
|
|
"date_to": fields.Date("To", required=True),
|
|
|
|
"cycle_id": fields.Many2One("clinic.cycle","Cycle"),
|
2015-03-30 06:40:46 +00:00
|
|
|
"ptype_id": fields.Many2One("clinic.patient.type","Patient Type"),
|
2015-03-06 05:15:19 +00:00
|
|
|
'branch_id': fields.Many2One("clinic.branch","Branch"),
|
|
|
|
'department_id': fields.Many2One("clinic.department","Department"),
|
|
|
|
}
|
|
|
|
|
|
|
|
def default_get(self,field_names=None,context={},**kw):
|
|
|
|
defaults=context.get("defaults",{})
|
|
|
|
date=defaults.get('date',time.strftime("%Y-%m-%d"))
|
|
|
|
year,month=time.strftime("%Y-%m").split("-")
|
|
|
|
date_from=defaults.get('date_from','%s-%s-01'%(year,month))
|
|
|
|
date_to=defaults.get('date_to','%s-%s-01'%(year,month))
|
2015-03-13 16:31:58 +00:00
|
|
|
branch_id=defaults.get('branch_id')
|
2015-03-30 06:40:46 +00:00
|
|
|
ptype_id=defaults.get('ptype_id')
|
2015-03-13 16:31:58 +00:00
|
|
|
department_id=defaults.get('department_id')
|
|
|
|
res=get_model('select.company').get_select()
|
|
|
|
if res:
|
|
|
|
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-06 05:15:19 +00:00
|
|
|
res={
|
|
|
|
'date': date,
|
|
|
|
'date_from': date_from,
|
|
|
|
'date_to': date_to,
|
2015-03-13 16:31:58 +00:00
|
|
|
'branch_id': branch_id,
|
|
|
|
'department_id': department_id,
|
2015-03-06 05:15:19 +00:00
|
|
|
}
|
2015-03-13 16:31:58 +00:00
|
|
|
print('res', res)
|
2015-03-06 05:15:19 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
def get_report_data(self,ids,context={}):
|
|
|
|
company_id=get_active_company()
|
|
|
|
company=get_model('company').browse(company_id)
|
|
|
|
defaults=self.default_get(context=context)
|
|
|
|
date_from=defaults.get("date_from")
|
|
|
|
date_to=defaults.get("date_to")
|
2015-03-13 16:31:58 +00:00
|
|
|
branch_id=defaults.get("branch_id")
|
|
|
|
department_id=defaults.get("department_id")
|
2015-03-30 06:40:46 +00:00
|
|
|
ptype_id=defaults.get("ptype_id")
|
2015-03-06 05:15:19 +00:00
|
|
|
month=date_from.split("-")[1]
|
|
|
|
cycle_id=None
|
|
|
|
if ids:
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
month=obj.date_from.split("-")[1]
|
|
|
|
date_from=obj.date_from
|
|
|
|
date_to=obj.date_to
|
|
|
|
branch_id=obj.branch_id.id
|
|
|
|
department_id=obj.department_id.id
|
|
|
|
cycle_id=obj.cycle_id.id
|
2015-03-30 06:40:46 +00:00
|
|
|
ptype_id=obj.ptype_id.id
|
2015-03-06 05:15:19 +00:00
|
|
|
# new patient of this month
|
|
|
|
dom=[]
|
|
|
|
dom.append(['date','>=',date_from])
|
|
|
|
dom.append(['date','<=',date_to])
|
|
|
|
dom.append(['state','=','validated'])
|
|
|
|
if cycle_id:
|
|
|
|
dom.append(['cycle_id','=',cycle_id])
|
|
|
|
if branch_id:
|
|
|
|
dom.append(['branch_id','=',branch_id])
|
|
|
|
if department_id:
|
|
|
|
dom.append(['department_id','=',department_id])
|
|
|
|
lines=[]
|
2015-03-13 16:31:58 +00:00
|
|
|
cycles={}
|
2015-04-03 04:45:16 +00:00
|
|
|
vasculars={}
|
|
|
|
for vscl in get_model("clinic.vascular.access").search_read([],['code','description']):
|
|
|
|
vasculars[vscl['code']]={
|
|
|
|
'qty': 0,
|
|
|
|
'description': vscl['description'] or ''
|
|
|
|
}
|
|
|
|
ptypes={}
|
|
|
|
for ptype in get_model("clinic.patient.type").search_read([],['name']):
|
|
|
|
ptypes[ptype['name'] or ""]=0
|
2015-03-06 11:30:00 +00:00
|
|
|
for citem in get_model('clinic.cycle.item').search_browse(dom,order="date"):
|
2015-03-13 16:31:58 +00:00
|
|
|
cycle=citem.cycle_id
|
|
|
|
if cycle.id not in cycles.keys():
|
|
|
|
cycles[cycle.id]=[]
|
|
|
|
for line in citem.lines:
|
|
|
|
nurse=line.nurse_id
|
2015-05-04 10:35:11 +00:00
|
|
|
cycles[cycle.id].append({
|
|
|
|
'name': nurse.name,
|
|
|
|
'first_name': nurse.first_name or "",
|
|
|
|
})
|
2015-03-06 05:15:19 +00:00
|
|
|
for hdcase in citem.hd_cases:
|
|
|
|
patient=hdcase.patient_id
|
2015-04-03 04:45:16 +00:00
|
|
|
vascular=patient.vascular_acc
|
2015-04-03 04:58:18 +00:00
|
|
|
if vascular:
|
|
|
|
vasculars[vascular.code]['qty']+=1
|
2015-03-06 05:15:19 +00:00
|
|
|
ptype=patient.type_id
|
2015-03-30 06:40:46 +00:00
|
|
|
if ptype_id and ptype_id!=ptype.id:
|
|
|
|
continue
|
2015-04-03 04:45:16 +00:00
|
|
|
ptypes[ptype.name or ""]+=1
|
2015-03-06 05:15:19 +00:00
|
|
|
doctor=hdcase.doctor_id
|
|
|
|
cycle=hdcase.cycle_id
|
|
|
|
dpt=hdcase.department_id
|
2015-03-13 16:31:58 +00:00
|
|
|
dlz_use=hdcase.dlz_use or 0
|
2015-03-18 10:35:49 +00:00
|
|
|
dlz_drop=False
|
2015-03-13 16:31:58 +00:00
|
|
|
if dlz_use==hdcase.dlz_max:
|
|
|
|
dlz_use="%sทิ้ง"%dlz_use
|
2015-03-18 10:35:49 +00:00
|
|
|
dlz_drop=True
|
|
|
|
cancel=False
|
2015-05-04 10:35:11 +00:00
|
|
|
row_color=''
|
2015-03-18 10:35:49 +00:00
|
|
|
if hdcase.state=='cancelled':
|
|
|
|
cancel=True
|
2015-05-04 10:35:11 +00:00
|
|
|
row_color='#b6b6b6'
|
2015-03-06 05:15:19 +00:00
|
|
|
lines.append({
|
2015-03-18 10:35:49 +00:00
|
|
|
'dlz_drop': dlz_drop,
|
|
|
|
'cancel': cancel,
|
2015-05-04 10:35:11 +00:00
|
|
|
'row_color': row_color,
|
2015-03-06 05:15:19 +00:00
|
|
|
'pname': patient.name or '',
|
|
|
|
'pid': patient.id or '',
|
|
|
|
'hn': patient.hn_no,
|
|
|
|
'did': doctor.id,
|
|
|
|
'dname': doctor.name or "",
|
|
|
|
'date': hdcase.date,
|
|
|
|
'epo': hdcase.epo,
|
2015-03-30 06:40:46 +00:00
|
|
|
'mdc': hdcase.mdc,
|
|
|
|
'mdc_name': hdcase.mdc_name,
|
|
|
|
'fee': abs(hdcase.fee),
|
2015-03-13 16:31:58 +00:00
|
|
|
'dlz_name': hdcase.dlz_name,
|
|
|
|
'dlz_use': dlz_use,
|
|
|
|
'dlz_id': hdcase.dlz_id,
|
|
|
|
'cseq': cycle.sequence or 0,
|
|
|
|
'cid': cycle.id,
|
|
|
|
'hdcase_id': hdcase.id,
|
|
|
|
'note': hdcase.note or "",
|
2015-03-06 05:15:19 +00:00
|
|
|
'cname': cycle.name or '',
|
2015-03-13 16:31:58 +00:00
|
|
|
'ctid': citem.id or '',
|
2015-03-06 05:15:19 +00:00
|
|
|
'hct': hdcase.hct or 0,
|
|
|
|
'tname': ptype.name or '',
|
|
|
|
'tid': ptype.id,
|
|
|
|
'dpt_id': dpt.id,
|
|
|
|
'dpt_name': dpt.name or "",
|
|
|
|
})
|
|
|
|
month_str=utils.MONTHS['th_TH'][int(month)]
|
|
|
|
company_name=company.name or ""
|
|
|
|
if department_id:
|
|
|
|
department=get_model("clinic.department").browse(department_id)
|
|
|
|
company_name+=" ("+department.name+")"
|
|
|
|
elif branch_id:
|
|
|
|
branch=get_model("clinic.branch").browse(branch_id)
|
|
|
|
company_name+=" ("+branch.name+")"
|
2015-03-13 16:31:58 +00:00
|
|
|
nlines=[]
|
|
|
|
index=0
|
|
|
|
old=[]
|
2015-03-24 04:05:50 +00:00
|
|
|
total_fee=0
|
2015-03-30 06:40:46 +00:00
|
|
|
total_mdc=0
|
2015-05-04 10:35:11 +00:00
|
|
|
dates={}
|
|
|
|
no=0
|
|
|
|
count=0
|
|
|
|
sub_fee=0
|
|
|
|
sub_mdc=0
|
|
|
|
pt=0
|
2015-03-13 16:31:58 +00:00
|
|
|
for line in sorted(lines,key=lambda x:(x['date'],x['cseq'])):
|
2015-05-04 10:35:11 +00:00
|
|
|
pt+=1
|
|
|
|
date=line['date'] or ''
|
|
|
|
key='%s-%s'%(date,line['cseq'])
|
|
|
|
if key not in dates.keys():
|
|
|
|
no=1
|
|
|
|
count=0
|
|
|
|
sub_fee=0
|
|
|
|
sub_mdc=0
|
|
|
|
for x in lines:
|
|
|
|
if x['cseq']==line['cseq'] and x['date']==date:
|
|
|
|
sub_fee+=x['fee'] or 0
|
|
|
|
sub_mdc+=x['mdc'] or 0
|
|
|
|
count+=1
|
|
|
|
line['date_txt']=line['date']
|
|
|
|
line['cseq_txt']=line['cseq']
|
|
|
|
dates[key]=0
|
|
|
|
else:
|
|
|
|
no+=1
|
|
|
|
dates[key]+=1
|
|
|
|
line['no']=no
|
2015-03-24 04:05:50 +00:00
|
|
|
total_fee+=line.get("fee",0)
|
2015-03-30 06:40:46 +00:00
|
|
|
total_mdc+=line.get("mdc",0)
|
2015-03-13 16:31:58 +00:00
|
|
|
cid=line['cid']
|
|
|
|
if not cid in old:
|
|
|
|
old.append(cid)
|
|
|
|
index=0
|
|
|
|
else:
|
|
|
|
index+=1
|
|
|
|
cres=cycles[cid]
|
|
|
|
line['nurse']=''
|
|
|
|
if index < len(cres):
|
2015-05-04 10:35:11 +00:00
|
|
|
line['nurse']=cres[index]['name']
|
|
|
|
line['nfirst_name']=cres[index]['first_name']
|
2015-03-13 16:31:58 +00:00
|
|
|
nlines.append(line)
|
2015-05-04 10:35:11 +00:00
|
|
|
if no==count:
|
|
|
|
nlines.append({
|
|
|
|
'sub': 'show',
|
|
|
|
'row_color': '#dfdfdf',
|
|
|
|
'no': count,
|
|
|
|
'fee': sub_fee,
|
|
|
|
'mdc': sub_mdc,
|
|
|
|
})
|
2015-04-03 04:45:16 +00:00
|
|
|
vscl_lines=[]
|
|
|
|
for k,v in vasculars.items():
|
|
|
|
vscl_lines.append({
|
|
|
|
'description': v['description'],
|
|
|
|
'qty': v['qty'],
|
|
|
|
})
|
|
|
|
ptype_lines=[]
|
|
|
|
total_pt=0
|
|
|
|
for pname,qty in ptypes.items():
|
|
|
|
ptype_lines.append({
|
|
|
|
'name': pname,
|
|
|
|
'qty': qty,
|
|
|
|
})
|
|
|
|
total_pt+=qty
|
2015-05-04 10:49:53 +00:00
|
|
|
vscl_lines2=[{}]
|
|
|
|
index=1
|
|
|
|
for vscl_line in vscl_lines:
|
|
|
|
vscl_lines2[0].update({
|
|
|
|
'v%s'%index: vscl_line['description'],
|
|
|
|
'v%s_1'%index: vscl_line['qty'],
|
|
|
|
})
|
|
|
|
index+=1
|
2015-03-06 05:15:19 +00:00
|
|
|
data={
|
|
|
|
'company_name': company_name or "",
|
2015-03-13 16:31:58 +00:00
|
|
|
'lines': nlines,
|
2015-04-03 04:45:16 +00:00
|
|
|
'vscl_lines': vscl_lines,
|
2015-05-04 10:49:53 +00:00
|
|
|
'vscl_lines2': vscl_lines2,
|
2015-04-03 04:45:16 +00:00
|
|
|
'ptype_lines': ptype_lines,
|
2015-03-06 05:15:19 +00:00
|
|
|
'month': month_str,
|
|
|
|
'date_from': date_from,
|
|
|
|
'date_to': date_to,
|
2015-05-04 10:35:11 +00:00
|
|
|
'total_pt': pt,
|
2015-03-24 04:05:50 +00:00
|
|
|
'total_fee': total_fee,
|
2015-03-30 06:40:46 +00:00
|
|
|
'total_mdc': total_mdc,
|
2015-04-03 04:45:16 +00:00
|
|
|
'total_pt': total_pt,
|
2015-03-06 05:15:19 +00:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
|
|
|
|
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-03-13 16:31:58 +00:00
|
|
|
def onchange_type(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
data['department_id']=None
|
|
|
|
return data
|
|
|
|
|
|
|
|
def onchange_datefrom(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
data['date_to']=data['date_from']
|
|
|
|
return data
|
|
|
|
|
2015-03-06 05:15:19 +00:00
|
|
|
ReportCycleItem.register()
|