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),
|
2014-11-13 06:30:51 +00:00
|
|
|
"date_from": fields.Date("From", required=True),
|
|
|
|
"date_to": fields.Date("To", required=True),
|
2014-11-05 06:45:28 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 06:30:51 +00:00
|
|
|
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)
|
|
|
|
|
2014-11-05 06:45:28 +00:00
|
|
|
_defaults={
|
|
|
|
'date': lambda *a: time.strftime("%Y-%m-%d"),
|
2014-11-13 06:30:51 +00:00
|
|
|
'date_from': _get_date_from,
|
|
|
|
'date_to': _get_date_to,
|
2014-11-05 06:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def get_report_data(self,ids,context={}):
|
2014-11-13 06:30:51 +00:00
|
|
|
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)
|
|
|
|
|
2014-11-10 05:10:39 +00:00
|
|
|
defaults=context.get('defaults')
|
|
|
|
if defaults:
|
2014-11-13 06:30:51 +00:00
|
|
|
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
|
2014-11-10 05:10:39 +00:00
|
|
|
|
|
|
|
dom=[]
|
2014-11-13 06:30:51 +00:00
|
|
|
#dom.append([['state','=','completed']])
|
|
|
|
dom.append(['time_start','>=','%s 00:00:00'%time_start])
|
|
|
|
dom.append(['time_stop','<=','%s 23:59:59'%time_stop])
|
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 14:52:47 +00:00
|
|
|
'prod_id': prod.id,
|
2014-11-10 11:33:01 +00:00
|
|
|
}
|
|
|
|
|
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=[]
|
|
|
|
|
2014-11-12 11:13:23 +00:00
|
|
|
limit=25
|
2014-11-10 11:33:01 +00:00
|
|
|
for prod, records in products.items():
|
2014-11-12 11:13:23 +00:00
|
|
|
prod_name=records['sc']['name'] or ""
|
2014-11-13 05:03:49 +00:00
|
|
|
prod_name_org=records['sc']['name'] or ""
|
2014-11-12 11:13:23 +00:00
|
|
|
prod_name=len(prod_name) > limit and '%s...' %prod_name[0:limit] or prod_name
|
2014-11-10 11:33:01 +00:00
|
|
|
line={
|
2014-11-12 11:13:23 +00:00
|
|
|
'prod_name': prod_name,
|
2014-11-13 05:03:49 +00:00
|
|
|
'prod_name_org': prod_name_org,
|
2014-11-10 14:52:47 +00:00
|
|
|
'prod_id': records[patient_type]['prod_id'],
|
2014-11-12 11:13:23 +00:00
|
|
|
'total': 0,
|
2014-11-10 11:33:01 +00:00
|
|
|
}
|
|
|
|
for patient_type in ('sc','nhso','personal'):
|
|
|
|
line.update({
|
2014-11-12 11:13:23 +00:00
|
|
|
patient_type: records[patient_type]['qty'] or 0,
|
2014-11-10 11:33:01 +00:00
|
|
|
})
|
2014-11-12 11:13:23 +00:00
|
|
|
line['total']+=line[patient_type]
|
2014-11-10 11:33:01 +00:00
|
|
|
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-13 06:30:51 +00:00
|
|
|
month_str=utils.MONTHS['th_TH'][int(month)]
|
2014-11-10 11:33:01 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-11-13 06:30:51 +00:00
|
|
|
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
|
|
|
|
|
2014-11-05 06:45:28 +00:00
|
|
|
ReportMedicalSummary.register()
|