import time from datetime import datetime, timedelta #from calendar import monthrange from netforce.model import Model, fields, get_model from netforce.access import get_active_company from . import utils DRT=5 class VisitBoard(Model): _name="clinic.visit.board" _string="Visit Board" _transient=True _fields={ "date_from": fields.Date("From", required=True), "date_to": fields.Date("To", required=True), 'patient_id': fields.Many2One("clinic.patient","Patient"), } _defaults={ 'date_from': lambda *a: time.strftime("%Y-%m-%d"), 'date_to': lambda *a: (datetime.now()+timedelta(days=DRT)).strftime("%Y-%m-%d"), } def get_report_data(self,ids,context={}): company_id=get_active_company() company=get_model("company").browse(company_id) date_from=datetime.now().strftime("%Y-%m-%d") date_to=(datetime.now()+timedelta(days=DRT)).strftime("%Y-%m-%d") patient_id=None if ids: obj=self.browse(ids)[0] date_from=obj.date_from date_to=obj.date_to patient_id=obj.patient_id.id time_start='%s 00:00:00'%(date_from) time_stop='%s 23:59:59'%(date_to) dom=[] dom.append(['time_start','>=','%s'%time_start]) dom.append(['time_stop','<=','%s'%time_stop]) if patient_id: dom.append(['patient_id','=',patient_id]) lines=[] empty_line={ 'no': '', 'number': '', 'visit_id': None, 'cycle_name': '', 'cycle_color': '', 'patient_name': '', 'patient_type': '', 'doctor_name': '', 'hd_case_number': '', 'hd_case_id': None, 'success_color': '', 'date':'', 'title': True, 'details':'', } types={} no=1 for visit in get_model("clinic.visit").search_browse(dom): hd_case_id=None hd_case_number='' if visit.hd_cases: hd_case=visit.hd_cases[0] hd_case_id=hd_case.id, hd_case_number=hd_case.number, number=visit.number if number=='/': number='รอการรรักษา' cycle=visit.cycle_id patient=visit.patient_id visit_date=visit.visit_date visit_color='' if visit.state=='confirmed': visit_color='#99ff99' elif visit.state=='cancelled': visit_color='#D84B4B', line={ 'number': number, 'visit_id': visit.id, 'cycle_name': cycle.name, 'cycle_color': cycle.color, 'patient_name': patient.name, 'patient_type': utils.PATIENT_TYPE.get(patient.type,''), 'doctor_name': visit.doctor_id.name, 'hd_case_number': hd_case_number, 'hd_case_id': hd_case_id, 'visit_color': visit_color, 'date': visit_date, 'title': False, 'details':'', 'no': no, } lines.append(line) no+=1 if not types.get(visit_date): ptype={} [ptype.setdefault(t,0) for t in utils.PATIENT_TYPE.keys()] types[visit_date]=ptype types[visit_date][patient.type]+=1 dates=[] index=0 for line in lines: date=line['date'] if date not in dates: line=empty_line.copy() total_qty=0 for qty in types[date].values(): total_qty+=qty line['cycle_name']=utils.date2thai(date,format='%(Td)s %(d)s %(Tm)s',lang="th_TH2"), line['details']='รวม %s: %s'%(total_qty,', '.join('%s %s'%(utils.PATIENT_TYPE[k],v) for k,v in types[date].items())) lines.insert(index,line) dates.append(date) index+=1 has_duration=False if date_from != date_to: has_duration=True data={ 'lines': lines, 'date': utils.date2thai(date_from,format='ประจำวัน%(Td)s ที่ %(d)s %(Tm)s %(BY)s'), 'company_name': company.name, 'company_parent_name': company.parent_id.name, 'has_duration': has_duration, 'date_from': utils.date2thai(date_from,format='%(d)s %(Tm)s %(By)s',lang="th_TH2"), 'date_to': utils.date2thai(date_to,format='%(d)s %(Tm)s %(By)s',lang="th_TH2"), } return data VisitBoard.register()