clinic/netforce_clinic/models/report_medical_summary.py

194 lines
7.0 KiB
Python

import time
from calendar import monthrange
from netforce.model import Model,fields,get_model
from netforce.access import get_active_company, get_active_user
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),
"prod_categ_id": fields.Many2One("product.categ","Category"),
"branch_id": fields.Many2One("clinic.branch","Branch"),
"department_id": fields.Many2One("clinic.department","Department"),
}
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)
def _get_prod_categ(self,context={}):
categ_id=None
ids=get_model("product.categ").search([['code','=','EPO']])
if ids:
categ_id=ids[0]
return categ_id
def _get_branch(self,context={}):
user_id=get_active_user()
staff=get_model("clinic.staff").search_browse([['user_id','=',user_id]])
if staff:
return staff[0].branch_id.id
def _get_department(self,context={}):
user_id=get_active_user()
staff=get_model("clinic.staff").search_browse([['user_id','=',user_id]])
if staff:
return staff[0].department_id.id
_defaults={
'date': lambda *a: time.strftime("%Y-%m-%d"),
'date_from': _get_date_from,
'date_to': _get_date_to,
'prod_categ_id': _get_prod_categ,
'branch_id':_get_branch,
'department_id': _get_department,
}
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("-")
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)
prod_categ_id=None
branch_id=None
department_id=None
res=get_model("clinic.report.medical.summary").default_get()
if res:
if res.get("prod_categ_id"):
prod_categ_id=res.get("prod_categ_id")[0]
if res.get("branch_id"):
branch_id=res.get("branch_id")[0]
if res.get("department_id"):
department_id=res.get("department_id")[0]
if ids:
obj=self.browse(ids)[0]
prod_categ_id=obj.prod_categ_id.id
branch_id=obj.branch_id.id
department_id=obj.department_id.id
month=obj.date_from.split("-")[1]
time_start=obj.date_from
time_stop=obj.date_to
products={}
patient_types={t['id']: t['name'] for t in get_model('clinic.patient.type').search_read([[]],['name'],order="name")}
dom=[]
dom.append(['type','=','stock'])
if prod_categ_id:
dom.append(['categ_id','=',prod_categ_id])
print('dom ', dom)
for prod in get_model("product").search_browse(dom):
prod_code=prod.code or ""
products[prod_code]={}
for patient_type_id,type_name in patient_types.items():
products[prod_code][patient_type_id]={
'qty': 0,
'name': prod.name,
'code': prod_code or "",
'prod_id': prod.id,
}
dom=[]
dom.append(['time_start','>=','%s 00:00:00'%time_start])
dom.append(['time_stop','<=','%s 23:59:59'%time_stop])
if branch_id:
dom.append(['branch_id','=',branch_id])
if department_id:
dom.append(['department_id','=',department_id])
for hd_case in get_model('clinic.hd.case').search_browse(dom):
patient_type_id=hd_case.patient_id.type_id.id
for line in hd_case.lines:
prod=line.product_id
prod_code=prod.code or ""
if not prod_code or not prod.active:
continue
if line.type=='fee' or prod.type=='service':
continue
if prod_categ_id and prod_categ_id != prod.categ_id.id:
continue
products[prod_code][patient_type_id]['qty']+=line.qty
lines=[]
limit=25
titles=[{'name': 'ชื่อยา'}]
for patient_type_id,type_name in sorted(patient_types.items(), key=lambda x: x[0]):
titles.append({
'name':type_name,
})
for prod, records in products.items():
prod_name=records[patient_type_id]['name'] or ""
prod_name_org=records[patient_type_id]['name'] or ""
prod_name=len(prod_name) > limit and '%s...' %prod_name[0:limit] or prod_name
count=1
total=0.0
sub_lines=[]
for patient_type_id,type_name in sorted(patient_types.items(), key=lambda x: x[0]):
qty=records[patient_type_id]['qty'] or 0
line={
'prod_name': prod_name,
'prod_name_org': prod_name_org,
'prod_id': records[patient_type_id]['prod_id'],
'total': 0,
}
sub_lines.append({
'qty': qty,
})
total+=qty
count+=1
sub_lines.append({
'qty': total,
})
line['sub_lines']=sub_lines
lines.append(line)
titles.append({'name':'รวม'})
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'])
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,
}
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()