clinic/netforce_clinic/models/report_hdcase_summary.py

96 lines
2.8 KiB
Python
Raw Normal View History

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 ReportHDCaseSummaryV2(Model):
_name="report.hdcase.summary"
_trasient=True
_fields={
"month": fields.Date("Month"),
"branch_id": fields.Many2One("clinic.branch","Branch"),
"department_id": fields.Many2One("clinic.department","Department"),
}
def default_get(self,field_names=None,context={},**kw):
vals={
"month": time.strftime("%Y-%m-%d"),
}
select_dpt=get_model('select.company').get_select()
if select_dpt:
vals.update({
"branch_id": select_dpt['branch_id'],
"department_id": select_dpt['department_id'],
})
return vals
def get_report_data(self, ids, context={}):
defaults=self.default_get(context=context)
month=defaults.get("month")
y,m,d=month.split("-")
branch_id=defaults.get("branch_id")
department_id=defaults.get("department_id")
if ids:
obj=self.browse(ids)[0]
branch_id=obj.branch_id.id
department_id=obj.department_id.id
month=obj.month
y,m,d=month.split("-")
#auto gen at the fist time of that month
get_model("clinic.patient.move").auto_get_data(context={'date': month})
crr_month=m
weekday, total_day=monthrange(int(y), int(m))
date_from="-".join([y,m,"01"])
date_to="-".join([y,m,str(total_day)])
cond=[
['date','>=',date_from],
['date','<=',date_to],
['state','in',["waiting_payment","paid"]],
]
#if branch_id:
#cond.append([
#'branch_id','=',branch_id,
#])
if department_id:
cond.append([
'department_id','=',department_id,
])
hdcase_ids=get_model("clinic.hd.case").search(cond)
company_id=get_active_company()
company=get_model("company").browse(company_id)
department=get_model("clinic.department").browse(department_id)
year_thai=utils.date2thai(month, lang='th_TH').split("-")[0]
month_thai=utils.MONTHS['th_TH'][int(crr_month)]
data={
'company_name': company.name,
'department_name': department.name,
'month_thai': month_thai,
'year_thai': year_thai,
'total_hdcase': len(hdcase_ids),
}
data2=get_model("clinic.patient.move").get_data(date=month, department_id=department_id)
data.update(data2)
from pprint import pprint
pprint(data)
return data
ReportHDCaseSummaryV2.register()