clinic/netforce_clinic/models/report_medical_summary.py

89 lines
2.8 KiB
Python
Raw Normal View History

2014-11-05 06:45:28 +00:00
import time
2014-11-10 05:10:39 +00:00
from calendar import monthrange
2014-11-05 06:45:28 +00:00
from netforce.model import Model,fields,get_model
2014-11-05 07:03:12 +00:00
from netforce.access import get_active_company
2014-11-10 05:10:39 +00:00
from . import utils
2014-11-05 06:45:28 +00:00
class ReportMedicalSummary(Model):
_name="clinic.report.medical.summary"
_string="Report Medical 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={}):
2014-11-10 05:10:39 +00:00
date=time.strftime("%Y-%m-%d")
defaults=context.get('defaults')
if defaults:
date=defaults['date']
year=int(date[0:4])
month=int(date[5:7])
weekday, total_day=monthrange(year, month)
dom=[]
dom.append([['state','=','completed']])
dom.append([['time_start','>=','%s-%s-01 00:00:00'%(year,month)]])
dom.append([['time_stop','<=','%s-%s-%s 23:59:59'%(year,month,total_day)]])
2014-11-05 07:28:20 +00:00
2014-11-10 11:33:01 +00:00
products={}
for prod in get_model("product").search_browse([['type','=','stock']]):
products[prod.code]={}
for patient_type in ('sc','nhso','personal'):
products[prod.code][patient_type]={
'qty': 0,
'name': prod.name,
'code': prod.code,
}
2014-11-10 05:10:39 +00:00
for hd_case in get_model('clinic.hd.case').search_browse(dom):
2014-11-10 11:33:01 +00:00
patient_type=hd_case.patient_id.type
2014-11-10 05:10:39 +00:00
for line in hd_case.lines:
prod=line.product_id
2014-11-10 11:33:01 +00:00
if line.type=='fee' or prod.type=='service':
2014-11-10 05:10:39 +00:00
continue
2014-11-10 11:33:01 +00:00
products[prod.code][patient_type]['qty']+=line.qty
lines=[]
for prod, records in products.items():
line={
'prod_name': records['sc']['name'], # XXX
}
for patient_type in ('sc','nhso','personal'):
line.update({
patient_type: records[patient_type]['qty'],
})
lines.append(line)
2014-11-10 05:10:39 +00:00
2014-11-05 07:03:12 +00:00
company_id=get_active_company()
company=get_model('company').browse(company_id)
2014-11-10 11:33:01 +00:00
month_str=utils.MONTHS['th_TH'][month]
lines=sorted(lines, key=lambda x: x['prod_name'])
titles={
'prod_name': 'ชื่อยา',
'sc': utils.PATIENT_TYPE['sc'],
'nhso': utils.PATIENT_TYPE['nhso'],
'personal': utils.PATIENT_TYPE['personal'],
}
2014-11-05 07:03:12 +00:00
data={
'company_name': company.name or "",
'parent_company_name': company.parent_id.name or "",
2014-11-10 11:33:01 +00:00
'titles': titles,
'lines': lines,
'month': month_str,
'year': year,
2014-11-05 07:03:12 +00:00
}
2014-11-10 11:33:01 +00:00
data.update(titles)
2014-11-05 06:45:28 +00:00
return data
ReportMedicalSummary.register()