import time import urllib.parse as urllib from datetime import datetime 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 ReportHDCaseSummary(Model): _name="report.hdcase.summary" _string="Report HDCase Summary" _transient=True _fields={ "date": fields.Date("Month", required=True), "date_from": fields.Date("From", required=True), "date_to": fields.Date("To", required=True), 'branch_id': fields.Many2One("clinic.branch","Branch"), 'department_id': fields.Many2One("clinic.department","Departments"), 'hdcase_type': fields.Selection([['completed','Completed'],['not_completed','Not Completed']],"HDCase Type"), } 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_branch(self,context={}): res=get_model('select.company').get_select() if res: return res['branch_id'] def _get_department(self,context={}): res=get_model('select.company').get_select() if res: if res.get("department_ids"): return res['department_ids'][0] else: return res['department_id'] _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d"), 'date_from': _get_date_from, 'date_to': _get_date_to, 'branch_id': _get_branch, 'department_id': _get_department, 'hdcase_type': 'completed', } def get_report_data(self,ids,context={}): company_id=get_active_company() company=get_model("company").browse(company_id) date=datetime.now().strftime("%Y-%m-%d") year=int(date[0:4]) crr_month=int(date[5:7]) weekday, crr_total_day=monthrange(year, crr_month) defaults=self.default_get(context=context) date_from=defaults.get('date_from',date) date_to=defaults.get('date_to',date) branch_id=defaults.get("branch_id",None) department_id=defaults.get("department_id",None) hdcase_type=defaults.get('hdcase_type','completed') #TODO get data from clinic.patient.move #DO SOME STAFF data={} year=year+543 sub_name='' if department_id: department_id=department_id[0] if branch_id: branch_id=branch_id[0] if ids: obj=self.browse(ids)[0] branch_id=obj.branch_id.id department_id=obj.department_id.id date=obj.date crr_month=int(date[5:7]) #XXX year=int(date[0:4]) date_from=obj.date_from date_to=obj.date_to hdcase_type=obj.hdcase_type or 'completed' prev_year=year next_year=year prev_month=crr_month-1 next_month=crr_month+1 if crr_month==1: prev_month=12 prev_year-=1 if crr_month==12: next_month=1 next_year+=1 month_str=utils.MONTHS['th_TH'][crr_month] next_month_str=utils.MONTHS['th_TH'][next_month] prev_month_str=utils.MONTHS['th_TH'][prev_month] def rzero(n): if not n: return '0' n="{0:,.0f}".format(n) return n def set_default(dom=[],topic='topic1'): dom_txt='' for f,op,v in dom: if 'in' in op: continue dom_txt+='defaults.%s%s%s&'%(f,op,v) if date: dom_txt+='&defaults.date=%s'%date if date_from: dom_txt+='&defaults.date_from=%s'%date_from if date_to: dom_txt+='&defaults.date_to=%s'%date_to if topic: dom_txt+='&defaults.report_type=%s'%topic if hdcase_type: dom_txt+='&defaults.hdcase_type=%s'%hdcase_type return dom_txt def set_ctx(dom=[],topic='topic1'): ctx={'defaults': {}} ctx['defaults'].update({ 'report_type': topic, 'hdcase_type': hdcase_type, 'date': date, 'date_from': date_from, 'date_to': date_to, }) for f,op,v in dom: if 'in' in op: #XXX continue ctx['defaults'].update({ f: v, }) return ctx dom=[] if branch_id: dom.append(['branch_id','=',branch_id]) if department_id: dom.append(['department_id','=',department_id]) topics=utils.TOPICS count=6 number=[1,2,3,4,5] for ptype in get_model("clinic.patient.type").search_browse([[]]): topic='topic%s'%count topics.update({ topic:{ 'name': ptype.name, 'unit': 'คน', 'ptype_id': ptype.id, } }) number.append(count) count+=1 month_val='' items={} for index in number: if index==2: weekday, prev_total_day=monthrange(prev_year, prev_month) month_val=prev_month_str elif index==5: month_val=next_month_str else: month_val=month_str topic="topic%s"%(str(index)) if 'ptype_id' in topics[topic]: dom2=dom+[['patient_type_id','=',topics[topic]['ptype_id']]] dom_txt=set_default(dom2,topic) ctx=set_ctx(dom2,topic) qty=get_model("clinic.report.hd.case.detail").get_report_data_revise(ids=[],context=ctx)['total'] else: dom_txt=set_default(dom,topic) ctx=set_ctx(dom,topic) qty=get_model("clinic.report.hd.case.detail").get_report_data_revise(ids=[],context=ctx)['total'] item_vals={ "topic": topics[topic]['name'], "unit": topics[topic]['unit'], "month": month_val if index<=5 else "", "qty": rzero(qty), "action": 'clinic_patient' if index==5 else "", "link": "clinic_report_hd_case_detail&%s"%dom_txt, } if hdcase_type!="completed" and index==1: item_vals['link']='clinic_report_hd_case_detail&%s'%dom_txt items[topic]=item_vals lines=[] items_vals=sorted(items, key=lambda s: int(s[5:])) for item in items_vals: line=items[item] lines.append(line) if hdcase_type=='not_completed': lines[0]['topic']='%s (ไม่สำเร็จ)'%lines[0]['topic'] context['defaults']={ 'date': date, 'date_from': date_from, 'date_to': date_to, 'branch_id': branch_id, 'department_id': department_id, 'hdcase_type': hdcase_type, } medicals=get_model("clinic.report.medical.summary").get_report_data(ids=[],context=context) if department_id: dpt=get_model("clinic.department").browse(department_id) sub_name="(%s)" % dpt.name or "" if branch_id: branch=get_model("clinic.branch").browse(branch_id) sub_name="(%s)" % branch.name or "" medical_lines=medicals['lines'] medical_titles=medicals['titles'] plines=medicals['plines'] prod_titles=medicals['prod_titles'] date_print=time.strftime("%m/%d/%Y %H:%M:%S") user=get_model('base.user').browse(get_active_user()) data={ 'user_name': user.name, 'hdcase_type': hdcase_type, 'branch_id': branch_id, 'department_id': department_id, 'date_from': date_from, 'date_to': date_to, 'date': date, 'date_print': date_print, 'month': month_str, 'year': year, 'lines': lines, 'recent_patients': get_model("clinic.report.recent.patient").get_report_data_revise(ids=[],context=context)['lines'], 'resign_patients': get_model("clinic.report.discontinue.patient").get_report_data_revise(ids=[],context=context)['lines'], 'medicals': medical_lines, 'titles': medical_titles, 'plines': plines, 'prod_titles': prod_titles, 'company_name': '%s %s'% (company.name or "", sub_name), 'parent_company_name': company.parent_id.name or "", } 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 def run_report(self,ids,context={}): return { 'next': { 'name': 'clinic_print_hd_case_summary', 'refer_id': ids[0], 'action_options': 'convert=pdf', } } def export_pdf(self,ids,context={}): raise Exception("TODO") return ReportHDCaseSummary.register()