402 lines
15 KiB
Python
402 lines
15 KiB
Python
import time
|
|
from calendar import monthrange
|
|
|
|
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"),
|
|
"ptype_id": fields.Many2One("clinic.patient.type","Patient Type"),
|
|
'branch_id': fields.Many2One("clinic.branch","Branch"),
|
|
'department_id': fields.Many2One("clinic.department","Department"),
|
|
'sort_by': fields.Selection([['hdcase_number','HDCase Number'],['pname','Patient Name'],['mdc_name','EPO']],'Sort By'),
|
|
}
|
|
|
|
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("-")
|
|
datenow=time.strftime("%Y-%m-%d")
|
|
date_from=defaults.get('date_from',datenow)
|
|
date_to=defaults.get('date_to',datenow)
|
|
branch_id=defaults.get('branch_id')
|
|
if branch_id:
|
|
branch_id=int(branch_id)
|
|
department_id=defaults.get('department_id')
|
|
if department_id:
|
|
department_id=int(department_id)
|
|
ptype_id=defaults.get('ptype_id')
|
|
res=get_model('select.company').get_select()
|
|
if res:
|
|
if not branch_id:
|
|
branch_id=res['branch_id']
|
|
if not department_id:
|
|
if res.get("department_ids"):
|
|
department_id=res['department_ids'][0]
|
|
else:
|
|
department_id=res['department_id']
|
|
res={
|
|
'date': date,
|
|
'date_from': date_from,
|
|
'date_to': date_to,
|
|
'branch_id': branch_id,
|
|
'department_id': department_id,
|
|
'ptype_id': ptype_id,
|
|
'sort_by': 'mdc_name',
|
|
}
|
|
print('report.cycle.item.defautls', res)
|
|
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")
|
|
branch_id=defaults.get("branch_id")
|
|
department_id=defaults.get("department_id")
|
|
ptype_id=defaults.get("ptype_id")
|
|
print('date_from ', date_from)
|
|
month=date_from.split("-")[1]
|
|
cycle_id=None
|
|
sort_by='mdc_name'
|
|
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
|
|
ptype_id=obj.ptype_id.id
|
|
sort_by=obj.sort_by or 'mdc_name'
|
|
# 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=[]
|
|
cycles={}
|
|
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
|
|
cycle_check={}
|
|
for citem in get_model('clinic.cycle.item').search_browse(dom,order="date"):
|
|
cycle=citem.cycle_id
|
|
date=citem.date
|
|
key='%s-%s'%(date,cycle.id)
|
|
if key not in cycles.keys():
|
|
cycles[key]=[]
|
|
cycle_check[key]=0
|
|
for line in citem.lines:
|
|
nurse=line.nurse_id
|
|
cycles[key].append({
|
|
'name': nurse.name,
|
|
'first_name': nurse.first_name or "",
|
|
'cycle_item_id': citem.id,
|
|
})
|
|
for hdcase in citem.hd_cases:
|
|
if hdcase.state not in ('paid', 'waiting_payment'):
|
|
continue
|
|
patient=hdcase.patient_id
|
|
vascular=patient.vascular_acc
|
|
if vascular:
|
|
vasculars[vascular.code]['qty']+=1
|
|
ptype=patient.type_id
|
|
if ptype_id and ptype_id!=ptype.id:
|
|
continue
|
|
key='%s-%s'%(hdcase.date,hdcase.cycle_id.id)
|
|
cycle_check[key]+=1
|
|
ptypes[ptype.name or ""]+=1
|
|
doctor=hdcase.doctor_id
|
|
cycle=hdcase.cycle_id
|
|
dpt=hdcase.department_id
|
|
dlz_use=hdcase.dlz_use or 0
|
|
dlz_drop=False
|
|
for dlz_line in hdcase.dialyzers:
|
|
dlz=dlz_line.dialyzer_id
|
|
if dlz.state in ('drop','expire'):
|
|
dlz_use="%sทิ้ง"%dlz_use
|
|
dlz_drop=True
|
|
cancel=False
|
|
row_color=''
|
|
def reformat_number(number=''):
|
|
if not number:
|
|
return ""
|
|
number=number.replace("HDC/","")
|
|
number=number.replace("HDC-","")
|
|
return number
|
|
|
|
total_epo=0
|
|
total_ivr=0
|
|
|
|
for line in hdcase.lines:
|
|
prod=line.product_id
|
|
if prod.categ_id:
|
|
if prod.categ_id.code=='EPO':
|
|
total_epo+=line.qty or 0
|
|
if prod.categ_id.code=='IVR':
|
|
total_ivr+=line.qty or 0
|
|
|
|
lines.append({
|
|
'dlz_drop': dlz_drop,
|
|
'cancel': cancel,
|
|
'row_color': row_color,
|
|
'pname': patient.name or '',
|
|
'pid': patient.id or '',
|
|
'hn': patient.hn_no,
|
|
'did': doctor.id,
|
|
'dname': '%s %s'%(doctor.first_name or "", doctor.last_name or ""),
|
|
'dfirst': doctor.first_name or "",
|
|
'date': hdcase.date,
|
|
'epo': hdcase.epo,
|
|
'mdc': hdcase.mdc,
|
|
'hdcase_number': reformat_number(hdcase.number),
|
|
'hdcase_id': hdcase.id,
|
|
'mdc_name': hdcase.mdc_name or hdcase.epo,
|
|
'mdc_desc': hdcase.mdc_desc,
|
|
'mdc_items': hdcase.mdc_items,
|
|
'iron_name': hdcase.iron_name or '',
|
|
'fee': abs(hdcase.fee),
|
|
'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 "",
|
|
'cname': cycle.name or '',
|
|
'ctid': citem.id or '',
|
|
'hct': hdcase.hct or 0,
|
|
'tname': ptype.name or '',
|
|
'tid': ptype.id,
|
|
'dpt_id': dpt.id,
|
|
'dpt_name': dpt.name or "",
|
|
'total_epo': total_epo,
|
|
'total_ivr': total_ivr,
|
|
})
|
|
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+")"
|
|
nlines=[]
|
|
clines=[]
|
|
index=0
|
|
old=[]
|
|
total_fee=0
|
|
total_mdc=0
|
|
dates={}
|
|
no=0
|
|
count=0
|
|
sub_fee=0
|
|
sub_mdc=0
|
|
pt=0
|
|
total_epos={}
|
|
for line in sorted(lines,key=lambda x:(x['date'],x['cseq'])):
|
|
pt+=1
|
|
date=line['date'] or ''
|
|
key='%s-%s'%(date,line['cseq'])
|
|
if key not in dates.keys():
|
|
no=1
|
|
count=0
|
|
sub_fee=1
|
|
sub_mdc=0
|
|
epos={}
|
|
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
|
|
x_name=x['mdc_name']
|
|
epos.setdefault(x_name,0)
|
|
epos.setdefault(x['iron_name'],0)
|
|
epos[x['iron_name']]+=x['total_ivr'] or 0 #XXX
|
|
if ',' in x_name:
|
|
del epos[x_name]
|
|
for k,v in eval(x['mdc_items']).items():
|
|
epos[k]=v
|
|
else:
|
|
epos[x_name]+=x['total_epo'] 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
|
|
total_fee+=line.get("fee",0)
|
|
total_mdc+=line.get("mdc",0)
|
|
cid=line['cid']
|
|
if not key in old:
|
|
old.append(key)
|
|
index=0
|
|
else:
|
|
index+=1
|
|
key='%s-%s'%(date,cid)
|
|
cres=cycles[key]
|
|
line['nurse']=''
|
|
more_lines=[]
|
|
if index < len(cres):
|
|
line['nurse']=cres[index]['name']
|
|
line['nfirst_name']=cres[index]['first_name']
|
|
if index+1==cycle_check[key]:
|
|
for i in range(index+1,len(cres)):
|
|
more_lines.append({
|
|
'nurse': cres[i]['name'],
|
|
'nfirst_name': cres[i]['first_name'],
|
|
'ctid': cres[i]['cycle_item_id'],
|
|
})
|
|
#nlines.append(line)
|
|
clines.append(line)
|
|
# if nurse more that cres, should show name of nurses
|
|
nlines+=more_lines
|
|
if no==count:
|
|
epo_items=[{'name': k, 'qty': v} for k,v in epos.items() if k ]
|
|
#sort item
|
|
#clines=sorted(clines,key=lambda cl: cl[sort_by])
|
|
no=1
|
|
index=len(nlines)
|
|
date_txt=''
|
|
cseq_txt=''
|
|
nurse_items=[]
|
|
for cline in clines:
|
|
if cline.get('nfirst_name'):
|
|
nurse_items.append({
|
|
'nurse': cline.get('nurse'),
|
|
'nfirst_name': cline.get('nfirst_name'),
|
|
'ctid': cline.get('ctid'),
|
|
})
|
|
cline['nurse']=''
|
|
cline['nfirst_name']=''
|
|
cline['ctid']=''
|
|
if cline.get('cseq_txt'):
|
|
date_txt=cline.get('date_txt')
|
|
cseq_txt=cline.get('cseq_txt')
|
|
cline['date_txt']=''
|
|
cline['cseq_txt']=''
|
|
cline['no']=no
|
|
nlines.append(cline)
|
|
no+=1
|
|
nlines[index]['date_txt']=date_txt
|
|
nlines[index]['cseq_txt']=cseq_txt
|
|
for nurse_item in nurse_items:
|
|
nlines[index].update({
|
|
'nurse': nurse_item['nurse'],
|
|
'nfirst_name': nurse_item['nfirst_name'],
|
|
'ctid': nurse_item['ctid'],
|
|
})
|
|
index+=1
|
|
clines=[]
|
|
nlines.append({
|
|
'sub': 'show',
|
|
'is_sub': True,
|
|
'sub_txt': 'รวม',
|
|
'row_color': '#dfdfdf',
|
|
'no': count,
|
|
'fee': sub_fee,
|
|
'mdc': sub_mdc,
|
|
'epo_items': epo_items,
|
|
'epo_txt': ', '.join(['%s = %s'%(k,v) for k,v in epos.items() if k])
|
|
})
|
|
for epo_item in epo_items:
|
|
if not total_epos.get(epo_item['name']):
|
|
total_epos[epo_item['name']]=0
|
|
total_epos[epo_item['name']]+=epo_item['qty'] or 0
|
|
total_epo_items=[{'name': k, 'qty': v} for k,v in total_epos.items() if k ]
|
|
vscl_lines=[]
|
|
for k,v in vasculars.items():
|
|
vscl_lines.append({
|
|
'description': v['description'],
|
|
'qty': v['qty'],
|
|
})
|
|
vscl_lines=sorted(vscl_lines,key=lambda x: x['description'])
|
|
ptype_lines=[]
|
|
total_pt=0
|
|
pkeys=list(ptypes.keys())
|
|
for pname in sorted(pkeys):
|
|
qty=ptypes[pname]
|
|
ptype_lines.append({
|
|
'name': pname,
|
|
'qty': qty,
|
|
})
|
|
total_pt+=qty
|
|
ptype_txt=' '.join(['%s: %s'%(x['name'],x['qty']) for x in ptype_lines])
|
|
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
|
|
vscl_txt=' '.join(['%s = %s'%(x['description'],x['qty']) for x in vscl_lines])
|
|
total_epo_txt=''
|
|
for t_epo in total_epo_items:
|
|
total_epo_txt+='%s = %s '%(t_epo['name'], t_epo['qty'])
|
|
data={
|
|
'company_name': company_name or "",
|
|
'lines': nlines,
|
|
'vscl_lines': vscl_lines,
|
|
'vscl_lines2': vscl_lines2,
|
|
'vscl_txt': vscl_txt,
|
|
'ptype_lines': ptype_lines,
|
|
'ptype_txt': ptype_txt,
|
|
'month': month_str,
|
|
'date_from': date_from,
|
|
'date_to': date_to,
|
|
'total_fee': total_fee,
|
|
'total_mdc': total_mdc,
|
|
'total_pt': total_pt,
|
|
'total_epo': sum([x['qty'] for x in total_epo_items]),
|
|
'total_epo_items': total_epo_items,
|
|
'total_epo_txt': total_epo_txt,
|
|
}
|
|
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
|
|
|
|
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
|
|
|
|
ReportCycleItem.register()
|