diff --git a/netforce_clinic/models/patient.py b/netforce_clinic/models/patient.py index 58bb9eb..0d0bf0f 100644 --- a/netforce_clinic/models/patient.py +++ b/netforce_clinic/models/patient.py @@ -330,6 +330,15 @@ class Patient(Model): }) #TODO create patient.move + patient_move={ + "patient_id": obj.id, + "patient_name": "%s %s"% (obj.first_name or "",obj.last_name or ""), + "patient_type": obj.type_id.name, + "type": "in", + #"location_from_id": obj.location, + #"location_to_id": obj.location, + } + get_model("clinic.patient.move").create(patient_move) return obj_id def delete(self,ids,context={}): @@ -375,6 +384,16 @@ class Patient(Model): if not vals.get("resign_date"): vals['resign_date']=time.strftime("%Y-%m-%d") vals['rm_remain_visit']=True + obj=self.browse(ids)[0] + patient_move={ + "patient_id": obj.id, + "patient_name": "%s %s"% (obj.first_name or "",obj.last_name or ""), + "patient_type": obj.type_id.name, + "type": "out", + #"location_from_id": obj.location, + #"location_to_id": obj.location, + } + get_model("clinic.patient.move").create(patient_move) else: vals['state']='admit' vals['rm_remain_visit']=False diff --git a/netforce_clinic/models/patient_move.py b/netforce_clinic/models/patient_move.py index 4d94992..2756708 100644 --- a/netforce_clinic/models/patient_move.py +++ b/netforce_clinic/models/patient_move.py @@ -11,9 +11,10 @@ class PatientMove(Model): "patient_name": fields.Char("Patient Name"), "patient_type": fields.Char("Patient Type"), #ปกส. .... "type": fields.Selection([['in','In'],['out','Out']], 'Type', required=True), + "location_from_id": fields.Many2One("stock.location", "From Location", required=False, search=True), + "location_to_id": fields.Many2One("stock.location", "To Location", required=False, search=True), } - _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"), } diff --git a/netforce_clinic/models/report_discontinue_patient.py b/netforce_clinic/models/report_discontinue_patient.py index 1267eb7..edfc021 100644 --- a/netforce_clinic/models/report_discontinue_patient.py +++ b/netforce_clinic/models/report_discontinue_patient.py @@ -51,6 +51,76 @@ class ReportDiscontinuePatient(Model): } return res + def get_report_data_revise(self,ids,context={}): + company_id=get_active_company() + company=get_model('company').browse(company_id) + 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") + branch_id=defaults.get("branch_id") + department_id=defaults.get("department_id") + if ids: + obj=self.browse(ids)[0] + month=obj.date_from.split("-")[1] + time_start=obj.date_from + time_stop=obj.date_to + branch_id=obj.branch_id.id + department_id=obj.department_id.id + # new patient of this month + dom=[] + dom.append(['date','>=',time_start]) + dom.append(['date','<=',time_stop]) + dom.append(['type','=','out']) + #dom.append(['resign_date','>=',time_start]) + #dom.append(['resign_date','<=',time_stop]) + #dom.append(['walkin','=',"no"]) + #dom.append(['dispose','=',True]) + #if branch_id: + #dom.append(['branch_id','=',branch_id]) + #if department_id: + #dom.append(['department_id','=',department_id]) + print("dom ", dom) + records=get_model('clinic.patient.move').search_browse(dom) + lines=[] + no=1 + for record in records: + if record.patient_id.branch_id.id!=branch_id: + continue + if record.patient_id.department_id.id!=department_id: + continue + lines.append({ + 'no': no, + 'name': record.patient_name or '', + 'pid': record.id, + 'note': record.patient_id.note or '', + 'resign_date': record.date[:10] or '', + }) + no+=1 + month_str=utils.MONTHS['th_TH'][int(month)] + start=int(time_start[8:10]) + stop=int(time_stop[8:10]) + diff=stop-start + 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 "" + data={ + 'company_name': '%s %s' % (company.name or "", sub_name), + 'parent_company_name': company.parent_id.name or "", + 'lines': lines, + 'month': month_str, + 'from': time_start, + 'to': time_stop, + 'is_duration': diff+1 < total_day, + 'year': year, + } + return data + def get_report_data(self,ids,context={}): company_id=get_active_company() company=get_model('company').browse(company_id) diff --git a/netforce_clinic/models/report_hd_case_detail.py b/netforce_clinic/models/report_hd_case_detail.py index a6ee966..cf31819 100644 --- a/netforce_clinic/models/report_hd_case_detail.py +++ b/netforce_clinic/models/report_hd_case_detail.py @@ -77,6 +77,295 @@ class ReportHDCaseDetail(Model): #print('report.hd.case.detail.defaults ', res) return res + def get_report_data_revise(self,ids,context={}): + user_id=get_active_user() + set_active_user(1) + + company_id=get_active_company() + company=get_model("company").browse(company_id) + defaults=self.default_get(context=context) + date=defaults.get('date',time.strftime("%Y-%m-%d")) + year=int(date[0:4]) + crr_month=int(date[5:7]) + weekday, crr_total_day=monthrange(year, crr_month) + 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') + report_type=defaults.get('report_type','topic1') + patient_type_id=defaults.get('patient_type_id') + time_start='%s-%s-01'%(year,str(crr_month).zfill(2)) + time_stop='%s-%s-%s'%(year,str(crr_month).zfill(2),crr_total_day) + titles=dict(self._fields['report_type'].selection) + title=titles.get(report_type,'no title') + 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]) + time_start=obj.date_from + time_stop=obj.date_to + year=int(date[0:4]) + date_from=obj.date_from + date_to=obj.date_to + report_type=obj.report_type or 'topic1' + hdcase_type=obj.hdcase_type or 'completed' + patient_type_id=obj.patient_type_id.id + 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 replace_quote(dom=""): + dom=dom.replace("False","false") + dom=dom.replace("True","true") + return dom.replace("'","\"") + dom=[] + lines=[] + total=0 + # current hd case + if report_type=='topic1': + title=titles.get(report_type,'no title') + dom=[] + dom.append(["date",">=",date_from]) + dom.append(["date","<=",date_to]) + if hdcase_type=='completed': + dom.append(["state","in",["waiting_payment","paid"]]) + else: + dom.append(["state","not in",["waiting_payment","paid"]]) + if branch_id: + dom.append(['branch_id','=',branch_id]) + if department_id: + dom.append(['department_id','=',department_id]) + total=0 + for hdcase in get_model("clinic.hd.case").search_browse(dom,order="number"): + patient=hdcase.patient_id + lines.append({ + 'number': hdcase.number, + 'date': hdcase.date, + 'name': patient.name, + 'note': hdcase.note, + }) + total+=1 + # number of patient from previous month + elif report_type=='topic2': + title=titles.get(report_type,'no title') + title+=' '+prev_month_str + prev_weekday, prev_total_day=monthrange(prev_year, prev_month) + reg_date='%s-%s-%s'%(prev_year,str(prev_month).zfill(2),str(prev_total_day).zfill(2)) + print('topic2 ', reg_date) + dom=[] + dom.append(['reg_date','<=',reg_date]) + dom.append(['walkin','=',"no"]) + #dom.append(['dispose','=',False]) + if branch_id: + dom.append(['branch_id','=',branch_id]) + if department_id: + dom.append(['department_id','=',department_id]) + total=0 + for patient in get_model("clinic.patient").search_browse(dom): + if patient.dispose and patient.resign_date: + if patient.resign_date < date_from: + print('continue >>> ', patient.name, patient.resign_date, date_from, date_to) + continue + elif patient.resign_date > date_to: + pass + elif not patient.resign_date > date_to: + print('pass >>> ', patient.name, patient.resign_date, date_from, date_to) + elif not (patient.resign_date>=date_from and patient.resign_date<=date_to): + print('continue >>> ', patient.name, patient.resign_date, date_from, date_to) + continue + print('pass >>> ', patient.name, patient.resign_date, date_from, date_to) + lines.append({ + 'number': patient.hn_no, + 'pid': patient.id, + 'name': patient.name, + 'note': patient.note, + 'date': patient.reg_date, + }) + total+=1 + # new patient of this month + #TODO should include move_history + elif report_type=='topic3': + title=titles.get(report_type,'no title') + title+=' '+month_str + dom=[] + dom.append(['date','>=',time_start]) + dom.append(['date','<=',time_stop]) + dom.append(['type','=',"in"]) + #dom.append(['reg_date','>=',time_start]) + #dom.append(['reg_date','<=',time_stop]) + #dom.append(['walkin','=',"no"]) + #dom.append(['dispose','=',False]) + #if branch_id: + #dom.append(['branch_id','=',branch_id]) + #if department_id: + #dom.append(['department_id','=',department_id]) + total=0 + for moves in get_model('clinic.patient.move').search_browse(dom): + if moves.patient_id.branch_id.id!=branch_id: + continue + if moves.patient_id.department_id.id!=department_id: + continue + lines.append({ + #'number': patient.hn_no, + #'pid': patient.id, + #'name': patient.name, + #'note': patient.note, + #'date': patient.reg_date, + 'number': moves.patient_id.hn_no, + 'pid': moves.patient_id.id, + 'name': moves.patient_id.name, + 'note': moves.patient_id.note, + 'date': moves.date[:10], + }) + total+=1 + # number for patient who resign for this month + #TODO should include move_history + elif report_type=='topic4': + title=titles.get(report_type,'no title') + title+=' '+month_str + dom=[] + #dom.append(['resign_date','>=',time_start]) + #dom.append(['resign_date','<=',time_stop]) + dom.append(['date','>=',time_start]) + dom.append(['date','<=',time_stop]) + dom.append(['type','=',"out"]) + #dom.append(['walkin','=',"no"]) + #dom.append(['dispose','=',True]) + #if branch_id: + #dom.append(['branch_id','=',branch_id]) + #if department_id: + #dom.append(['department_id','=',department_id]) + total=0 + for moves in get_model('clinic.patient.move').search_browse(dom): + if moves.patient_id.branch_id.id!=branch_id: + continue + if moves.patient_id.department_id.id!=department_id: + continue + lines.append({ + 'number': moves.patient_id.hn_no, + 'pid': moves.patient_id.id, + 'name': moves.patient_id.name, + 'note': moves.patient_id.note, + 'date': moves.date[:10], + }) + total+=1 + # all patient who are in hospital on select month + elif report_type=='topic5': + print('topic5 ', time_stop) + title=titles.get(report_type,'no title') + title+=' '+next_month_str + dom=[] + dom.append(['reg_date','<=',time_stop]) + if branch_id: + dom.append(['branch_id','=',branch_id]) + if department_id: + dom.append(['department_id','=',department_id]) + dom.append(['walkin','=',"no"]) + total=0 + for patient in get_model('clinic.patient').search_browse(dom,order="reg_date"): + if patient.dispose and patient.resign_date: + if patient.resign_date < date_from: + print('1. continue >>> ', patient.name, patient.resign_date, date_from, date_to) + continue + elif patient.resign_date > date_to: + pass + elif patient.resign_date > date_to: + print('2. pass >>> ', patient.name, patient.resign_date, date_from, date_to) + elif (patient.resign_date>=date_from and patient.resign_date<=date_to): + print('3. continue >>> ', patient.name, patient.resign_date, date_from, date_to) + continue + print('4. pass >>> ', patient.name, patient.resign_date, date_from, date_to) + lines.append({ + 'number': patient.hn_no, + 'pid': patient.id, + 'name': patient.name, + 'note': patient.note, + 'date': patient.reg_date, + 'dispose': patient.dispose and 'yes' or 'no', + }) + total+=1 + # find patient movement + elif report_type=='topic6': + print('topic6') + ptype=None + if patient_type_id: + ptype=get_model('clinic.patient.type').browse(patient_type_id) + if not ptype: + return {'total': 0} + title=ptype.name or '' + dom=[] + dom.append(['reg_date','<=',time_stop]) + dom.append(['type_id','=',ptype['id']]) + dom.append(['walkin','=',"no"]) + dom.append(['type_id','=',ptype.id]) + if branch_id: + dom.append(['branch_id','=',branch_id]) + if department_id: + dom.append(['department_id','=',department_id]) + total=0 + for patient in get_model("clinic.patient").search_browse(dom): + if patient.resign_date: + if patient.dispose and patient.resign_date < date_from: + print('1. continue >>> ', patient.name, patient.resign_date, date_from, date_to) + continue + elif patient.resign_date > date_to: + pass + elif patient.resign_date > date_to: + print('2. pass >>> ', patient.name, patient.resign_date, date_from, date_to) + elif (patient.resign_date>=date_from and patient.resign_date<=date_to): + print('3. continue >>> ', patient.name, patient.resign_date, date_from, date_to) + continue + print('4. pass >>> ', patient.name, patient.resign_date, date_from, date_to) + lines.append({ + 'pid': patient.id, + 'number': patient.hn_no, + 'name': patient.name or '', + 'date': patient.reg_date, + }) + total+=1 + 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 "" + no=1 + for line in lines: + line['no']="{0:,.0f}".format(no) + no+=1 + data={ + 'total_txt': "{0:,.0f}".format(total), + 'total': total, + 'title': title, + 'report_type': report_type, + 'date': date, + 'date_from': date_from, + 'date_to': date_to, + 'month': month_str, + 'year': year, + 'branch_id': branch_id, + 'department_id': department_id, + 'lines': lines, + 'company_name': '%s %s'% (company.name or "", sub_name), + 'parent_company_name': company.parent_id.name or "", + } + set_active_user(user_id) + return data + def get_report_data(self,ids,context={}): user_id=get_active_user() set_active_user(1) diff --git a/netforce_clinic/models/report_hdcase_summary.py b/netforce_clinic/models/report_hdcase_summary.py index 9167607..43ec6b1 100644 --- a/netforce_clinic/models/report_hdcase_summary.py +++ b/netforce_clinic/models/report_hdcase_summary.py @@ -69,6 +69,179 @@ class ReportHDCaseSummary(Model): #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={}): diff --git a/netforce_clinic/models/report_recent_patient.py b/netforce_clinic/models/report_recent_patient.py index c423d33..104edad 100644 --- a/netforce_clinic/models/report_recent_patient.py +++ b/netforce_clinic/models/report_recent_patient.py @@ -50,6 +50,76 @@ class ReportRecentPatient(Model): } return res + def get_report_data_revise(self,ids,context={}): + company_id=get_active_company() + company=get_model('company').browse(company_id) + 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") + branch_id=defaults.get("branch_id") + department_id=defaults.get("department_id") + if ids: + obj=self.browse(ids)[0] + month=obj.date_from.split("-")[1] + time_start=obj.date_from + time_stop=obj.date_to + branch_id=obj.branch_id.id + department_id=obj.department_id.id + # new patient of this month + dom=[] + dom.append(['date','>=',time_start]) + dom.append(['date','<=',time_stop]) + dom.append(['type','=',"in"]) + #dom.append(['reg_date','>=',time_start]) + #dom.append(['reg_date','<=',time_stop]) + #dom.append(['walkin','=',"no"]) + #dom.append(['dispose','=',False]) + #if branch_id: + #dom.append(['branch_id','=',branch_id]) + #if department_id: + #dom.append(['department_id','=',department_id]) + records=get_model('clinic.patient.move').search_browse(dom) + lines=[] + no=1 + for record in records: + if record.patient_id.branch_id.id!=branch_id: + continue + if record.patient_id.department_id.id!=department_id: + continue + lines.append({ + 'no': no, + 'name': record.patient_name or '', + 'pid': record.id, + 'note': record.patient_id.note or '', + 'reg_date': record.date[:10], + }) + no+=1 + + month_str=utils.MONTHS['th_TH'][int(month)] + start=int(time_start[8:10]) + stop=int(time_stop[8:10]) + diff=stop-start + 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 "" + data={ + 'company_name': '%s %s'%(company.name or "", sub_name), + 'parent_company_name': company.parent_id.name or "", + 'lines': lines, + 'month': month_str, + 'from': time_start, + 'to': time_stop, + 'is_duration': diff+1 < total_day, + 'year': year, + } + return data + def get_report_data(self,ids,context={}): company_id=get_active_company() company=get_model('company').browse(company_id) diff --git a/netforce_clinic/templates/report_hdcase_summary.hbs b/netforce_clinic/templates/report_hdcase_summary.hbs index 017ee66..785d089 100644 --- a/netforce_clinic/templates/report_hdcase_summary.hbs +++ b/netforce_clinic/templates/report_hdcase_summary.hbs @@ -68,7 +68,10 @@ {{no}} {{reg_date}} - {{view "link" string=name action="clinic_patient" action_options="mode=form" active_id=pid}} + {{view "link" string=name action="clinic_patient_move" action_options="mode=form" active_id=pid}} + + + {{/each}} @@ -98,7 +101,10 @@ {{no}} {{resign_date}} - {{view "link" string=name action="clinic_patient" action_options="mode=form" active_id=pid}} + {{view "link" string=name action="clinic_patient_move" action_options="mode=form" active_id=pid}} + + + {{/each}}