clinic/netforce_clinic/models/report_medical_summary.py

256 lines
9.5 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),
"prod_categ_id": fields.Many2One("product.categ","Category"),
"branch_id": fields.Many2One("clinic.branch","Branch"),
"department_id": fields.Many2One("clinic.department","Department"),
'report_type': fields.Selection([['completed','Completed'],['not_completed','Not Completed']],"Report Type"),
}
# in case link from another report
def default_get(self,field_names=None,context={},**kw):
defaults=context.get("defaults",{})
date=defaults.get('date',time.strftime("%Y-%m-%d"))
year,month=time.strftime("%Y-%m").split("-")
weekday, total_day=monthrange(int(year), int(month))
date_from=defaults.get('date_from','%s-%s-01'%(year,month))
date_to=defaults.get('date_to',"%s-%s-%s"%(year,month,total_day))
categ_id=defaults.get('categ_id',None)
report_type=defaults.get('report_type',"completed")
if not categ_id:
categ_ids=get_model("product.categ").search([['code','=','MDC']])
if categ_ids:
categ_id=categ_ids[0]
branch_id=defaults.get('branch_id',None)
select_dpt=get_model('select.company').get_select()
if not branch_id:
if select_dpt:
branch_id=select_dpt['branch_id']
else:
branch_id=int(branch_id or "0")
department_id=defaults.get('department_id',None)
if not department_id:
if select_dpt.get('department_ids'):
department_id=select_dpt['department_ids'][0]
else:
department_id=select_dpt['department_id']
else:
department_id=int(department_id or "0")
res={
'date': date,
'date_from': date_from,
'date_to': date_to,
'prod_categ_id': categ_id,
'branch_id': branch_id,
'department_id': department_id,
'report_type': report_type,
}
return res
def get_report_data(self,ids,context={}):
year, month=time.strftime("%Y-%m").split("-")
weekday, total_day=monthrange(int(year), int(month))
defaults=self.default_get(context=context)
time_start=defaults.get("date_from")
time_stop=defaults.get("date_to")
prod_categ_id=defaults.get("prod_categ_id")
branch_id=defaults.get("branch_id")
department_id=defaults.get("department_id")
report_type=defaults.get("report_type","completed")
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
report_type=obj.report_type or "completed"
print('report_type ---> ', report_type)
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'])
dom.append(['report_visible','=',True])
if prod_categ_id:
dom.append(['categ_id.id','child_of',prod_categ_id])
categ_ids=set()
for prod in get_model("product").search_browse(dom):
prod_code=prod.code or ""
categ=prod.categ_id
if categ and categ.parent_id:
categ_ids.update({categ.id})
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])
if report_type=='completed':
dom.append(["state","in",["waiting_payment","paid"]])
else:
dom.append(["state","not in",["waiting_payment","paid"]])
for hd_case in get_model('clinic.hd.case').search_browse(dom):
patient_type_id=hd_case.patient_type_id.id
for line in hd_case.lines:
prod=line.product_id
prod_code=prod.code or ""
categ=line.product_categ_id
if not prod_code or not prod.active:
continue
if categ and categ.id not in list(categ_ids):
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_char=25
titles=[{'name': 'ชื่อยา'}]
prod_titles={}
for patient_type_id,type_name in sorted(patient_types.items(), key=lambda x: x[0]):
prod_titles.update({patient_type_id: type_name})
titles.append({
'name':type_name,
})
plines=[]
def rzero(n):
if not n:
return ''
n="{0:,.0f}".format(n)
return n
for prod, prod_vals in products.items():
count=1
total=0.0
sub_lines=[]
all_ptypes=""
prod_id=None
pvals={}
for patient_type_id,type_name in sorted(patient_types.items(), key=lambda x: x[0]):
all_ptypes+="%s,"%patient_type_id
qty=prod_vals[patient_type_id]['qty'] or 0
prod_id=prod_vals[patient_type_id]['prod_id']
prod_name=prod_vals[patient_type_id]['name'] or ""
prod_name_org=prod_vals[patient_type_id]['name'] or ""
prod_name=len(prod_name) > limit_char and '%s...' %prod_name[0:limit_char] or prod_name
pvals.update({
'prod_name': prod_name,
patient_type_id : rzero(qty),
})
line={
'prod_name': prod_name,
'prod_name_org': prod_name_org,
'prod_id': prod_id,
'total': 0,
}
sub_lines.append({
'qty': qty,
'types': "%s"%patient_type_id,
'time_start': time_start,
'time_stop': time_stop,
'product_id': prod_id,
'product_categ_id': prod_categ_id,
})
total+=qty
count+=1
sub_lines.append({
'qty': total,
'types': all_ptypes,
'time_start': time_start,
'time_stop': time_stop,
'product_id': prod_id,
'product_categ_id': prod_categ_id,
})
line['sub_lines']=sub_lines
lines.append(line)
pvals[999]=int(total)
plines.append(pvals)
for line in lines:
st=""
for x in line['sub_lines']:
st+='%s'%(x['types'])
titles.append({'name':'รวม'})
company_id=get_active_company()
company=get_model('company').browse(company_id)
month_str=utils.MONTHS['th_TH'][int(month)]
# remove zero
for line in lines:
for sline in line['sub_lines']:
qty=sline['qty']
if not qty:
sline['qty']=''
lines=sorted(lines, key=lambda x: x['prod_name'])
year=int(year)+543
sub_name=''
if department_id:
dpt=get_model("clinic.department").browse(department_id)
sub_name="(%s)" % dpt.name or ""
elif branch_id:
branch=get_model("clinic.branch").browse(branch_id)
sub_name="(%s)" % branch.name or ""
plines=sorted(plines,key=lambda x:x['prod_name'])
data={
'company_name': '%s %s' % (company.name or "", sub_name),
'parent_company_name': company.parent_id.name or "",
'titles': titles,
'lines': lines,
'plines': plines,
'prod_titles': prod_titles,
'month': month_str,
'year': year,
'report_type': report_type,
}
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
def onchange_branch(self,context={}):
data=context['data']
data['department_id']=None
return data
ReportMedicalSummary.register()