clinic/netforce_clinic/models/report_medical_summary.py

130 lines
4.4 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 ReportMedicalSummary(Model):
_name="clinic.report.medical.summary"
_string="Report Medical Summary"
_transient=True
_fields={
"date": fields.Date("Month", required=True),
"date_from": fields.Date("From", required=True),
"date_to": fields.Date("To", required=True),
}
def _get_date_from(self,context={}):
year,month=time.strftime("%Y-%m").split("-")
return '%s-%s-01'%(year,month)
def _get_date_to(self,context={}):
year,month,day=time.strftime("%Y-%m-%d").split("-")
weekday, total_day=monthrange(int(year), int(month))
return "%s-%s-%s"%(year,month,total_day)
_defaults={
'date': lambda *a: time.strftime("%Y-%m-%d"),
'date_from': _get_date_from,
'date_to': _get_date_to,
}
def get_report_data(self,ids,context={}):
year, month=time.strftime("%Y-%m").split("-")
weekday, total_day=monthrange(int(year), int(month))
time_start='%s-%s-01'%(year,str(month).zfill(2))
time_stop='%s-%s-%s'%(year,str(month).zfill(2),total_day)
defaults=context.get('defaults')
if defaults:
year,month,total_day=defaults['date'].split("-")
time_start='%s-%s-01'%(year,str(month).zfill(2))
time_stop='%s-%s-%s'%(year,str(month).zfill(2),total_day)
if ids:
obj=self.browse(ids)[0]
month=obj.date_from.split("-")[1]
time_start=obj.date_from
time_stop=obj.date_to
dom=[]
#dom.append([['state','=','completed']])
dom.append(['time_start','>=','%s 00:00:00'%time_start])
dom.append(['time_stop','<=','%s 23:59:59'%time_stop])
products={}
for prod in get_model("product").search_browse([['type','=','stock']]):
products[prod.code]={}
for patient_type in ('sc','uc','others'):
products[prod.code][patient_type]={
'qty': 0,
'name': prod.name,
'code': prod.code,
'prod_id': prod.id,
}
for hd_case in get_model('clinic.hd.case').search_browse(dom):
patient_type=hd_case.patient_id.type
for line in hd_case.lines:
prod=line.product_id
if line.type=='fee' or prod.type=='service':
continue
products[prod.code][patient_type]['qty']+=line.qty
lines=[]
limit=25
for prod, records in products.items():
prod_name=records['sc']['name'] or ""
prod_name_org=records['sc']['name'] or ""
prod_name=len(prod_name) > limit and '%s...' %prod_name[0:limit] or prod_name
line={
'prod_name': prod_name,
'prod_name_org': prod_name_org,
'prod_id': records[patient_type]['prod_id'],
'total': 0,
}
for patient_type in ('sc','uc','others'):
line.update({
patient_type: records[patient_type]['qty'] or 0,
})
line['total']+=line[patient_type]
lines.append(line)
company_id=get_active_company()
company=get_model('company').browse(company_id)
month_str=utils.MONTHS['th_TH'][int(month)]
lines=sorted(lines, key=lambda x: x['prod_name'])
titles={
'prod_name': 'ชื่อยา',
'sc': utils.PATIENT_TYPE['sc'],
'uc': utils.PATIENT_TYPE['uc'],
'others': utils.PATIENT_TYPE['others'],
}
year=int(year)+543
data={
'company_name': company.name or "",
'parent_company_name': company.parent_id.name or "",
'titles': titles,
'lines': lines,
'month': month_str,
'year': year,
}
data.update(titles)
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
ReportMedicalSummary.register()