clinic/netforce_clinic/models/report_hd_summary.py

164 lines
5.1 KiB
Python

import time
from datetime import datetime
from calendar import monthrange
from netforce.model import Model, fields, get_model
#from netforce.access import get_active_company
class ReportHDSummary(Model):
_name="clinic.report.hd.summary"
_string="Hemodialysis Report Summary"
_transient=True
_fields={
"date": fields.Date("Month", required=True),
}
_defaults={
'date': lambda *a: time.strftime("%Y-%m-%d"),
}
def get_report_data(self,ids,context={}):
# company_id=get_active_company
# company_name=get_model("company").search("name")
# company_name=get_model("company").search_browse()
for item in get_model("company").search_browse([]):
company_name=item.name
date=datetime.now().strftime("%Y-%m-%d")
dom=[]
if ids:
obj=self.browse(ids)[0]
date=obj.date
month=int(date[5:7])
year=int(date[0:4])
weekday, total_day=monthrange(year, month)
#date_from="%s-%s-01"%(year,month)
#date_to="%s-%s-%s"%(year,month,total_day)
month_str=datetime.strptime(date,'%Y-%m-%d').strftime("%B")
next_month = str(month + 1)
if next_month == 13:
next_month = 12
next_month_str=datetime.strptime(next_month,'%m').strftime("%B")
previous_month = month-1
if previous_month == 0:
previous_month = 12
#tip previous_month=str(month-1 == 0 and 12 or month)
previous_month_str=datetime.strptime('%s'%previous_month,'%m').strftime("%B")
time_start='2014-%s-01'%(month) # 2014-10-20
time_stop='2014-%s-%s'%(month,total_day)
dom=[]
dom.append(['time_start','>=',time_start])
dom.append(['time_stop','<=',time_stop])
patients=get_model("clinic.patient").search_browse(['type','=','All'])
new_patients=len(patients)
patients_mg=get_model("clinic.patient").search_browse(['type','=','mg'])
patients_sc=get_model("clinic.patient").search_browse(['type','=','sc'])
patients_nhso=get_model("clinic.patient").search_browse(['type','=','nhso'])
patients_pn=get_model("clinic.patient").search_browse(['type','=','personal'])
dom=[]
dom.append(['state','=','completed'])
dom.append(['time_start','>=',time_start])
dom.append(['time_stop','<=',time_stop])
cur_total_case=len(get_model("clinic.hd.case").search(dom))
weekday, prev_total_day=monthrange(year, previous_month)
time_start_pre='2014-%s-01'%(previous_month) # 2014-10-20
time_stop_pre='2014-%s-%s'%(previous_month,prev_total_day)
dom=[]
dom.append(['state','=','completed'])
dom.append(['time_start','>=',time_start_pre])
dom.append(['time_stop','<=',time_stop_pre])
prev_total_case=len(get_model("clinic.hd.case").search(dom))
items={
'topic1': {
'month': month_str,
'amount': cur_total_case,
'date_from': time_start,
'date_to': time_stop,
},
'topic2':
{
'month': previous_month_str,
'amount': prev_total_case,
'date_from': time_start_pre,
'date_to': time_stop_pre,
},
'topic3':
{
'month': month_str,
'amount': new_patients,
'date_from': time_start,
'date_to': time_stop,
},
'topic4':
{
'month': month_str,
'amount': 'N/A',
'date_from': time_start,
'date_to': time_stop,
},
'topic5':
{
'month': next_month_str,
'amount': 'N/A',
'date_from': time_start,
'date_to': time_stop,
},
'topic6':
{
'month': '',
'amount': len(patients_sc),
'date_from': "",
'date_to': "",
},
'topic7':
{
'month':'',
'amount': len(patients_mg),
'date_from': "",
'date_to': "",
},
'topic8':
{
'month':'',
'amount': len(patients_nhso),
'date_from': "",
'date_to': "",
},
'topic9':
{
'month': '',
'amount': len(patients_pn),
'date_from': "",
'date_to': "",
},
}
lines=[]
for item in get_model("clinic.translate").search_browse([]):
topic=item.org
line=items.get(topic,{})
line['topic']=item.tr
lines.append(line)
data={
'month': month_str,
'year': year,
'lines': lines,
'company_name': company_name,
}
print("data", data)
return data
ReportHDSummary.register()