from netforce.model import Model, fields, get_model from netforce.access import set_active_user, get_active_user class ReportCycleSetting(Model): _name="clinic.report.cycle.setting" _transient=True _fields={ 'department_id': fields.Many2One("clinic.department","Department"), 'branch_id': fields.Many2One("clinic.branch","Branch"), 'cycle_id': fields.Many2One("clinic.cycle","Cycle"), } 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'] def default_get(self,field_names=None,context={},**kw): defaults=context.get("defaults",{}) branch_id=defaults.get('branch_id') if branch_id: branch_id=int(branch_id) else: branch_id=self._get_branch(context=context) department_id=defaults.get('department_id') if department_id: department_id=int(department_id) else: department_id=self._get_department(context=context) res={ 'branch_id': branch_id, 'department_id': department_id, } print('report.cycle.item.setting', res) return res def get_report_data(self,ids,context={}): defaults=self.default_get(context=context) department_id=defaults.get('department_id',None) branch_id=defaults.get('branch_id',None) cycle_id=defaults.get('cycle_id',None) if ids: obj=self.browse(ids)[0] department_id=obj.department_id.id branch_id=obj.branch_id.id cycle_id=obj.cycle_id.id pts={} dom=[] dom.append(['patient_id.state','=','admit']) if department_id: dom.append(['department_id','=',department_id]) if cycle_id: dom.append(['cycle_id','=',cycle_id]) pcs=get_model("clinic.patient.cycle").search_browse(dom) user_id=get_active_user() set_active_user(1) if not pcs: # Create patient cycle with that department for pt in get_model("clinic.patient").search_browse([['department_id','=',department_id]]): if pt.walkin=='yes': continue for pc in pt.cycles: vals={ 'patient_id': pt.id, 'department_id': pt.department_id.id, 'cycle_id': pc.cycle_id.id, 'day': pc.day, } res=get_model("clinic.patient.cycle").search([[ ['patient_id','=',pt.id], ['day','=',pc.day], ['cycle_id','=',pc.cycle_id.id], ['department_id','=', pt.department_id.id], ]]) if not res: pcycle_id=get_model("clinic.patient.cycle").create(vals) print("create ", pcycle_id) else: print("exist ", res) pcs=get_model("clinic.patient.cycle").search_browse(dom) if not pcs: raise Exception("Please go to menu 'Patients -> Patient Cycles' and import data for %s"%(department_id)) for pc in pcs: patient=pc.patient_id dpt=pc.department_id branch=dpt.branch_id if not branch: continue if branch_id and branch_id!=branch.id: continue if patient.resign_date: continue key=(patient.id,dpt.id) if not pts.get(key): login='' if patient.cw_uid: login=patient.cw_uid.login pts[key]={ 'w_time': patient.cw_time, 'w_uid': login, 'patient_name': patient.name, 'department_name': dpt.name, 'mon_cycle_id': None, 'tue_cycle_id': None, 'wed_cycle_id': None, 'thu_cycle_id': None, 'fri_cycle_id': None, 'sat_cycle_id': None, 'sun_cycle_id': None, } cycle=pc.cycle_id day=pc.day or '' if day=='mon': pts[key]['mon_cycle_id']=cycle.name elif day=='tue': pts[key]['tue_cycle_id']=cycle.name elif day=='wed': pts[key]['wed_cycle_id']=cycle.name elif day=='thu': pts[key]['thu_cycle_id']=cycle.name elif day=='fri': pts[key]['fri_cycle_id']=cycle.name elif day=='sat': pts[key]['sat_cycle_id']=cycle.name else: pts[key]['sun_cycle_id']=cycle.name lines=[] for k, vals in pts.items(): pt_id, dpt_id=k if pt_id: vals['patient_id']=pt_id if not dpt_id: pt=get_model("clinic.patient").browse(pt_id) dpt_id=pt.department_id.id if dpt_id: vals['department_id']=dpt_id lines.append(vals) no=1 nlines=[] for line in sorted(lines,key=lambda x:x['patient_name']): line['no']=no nlines.append(line) no+=1 set_active_user(user_id) data={ 'lines': nlines, 'department_id': department_id, } return data def onchange_branch(self,context={}): data=context['data'] data['department_id']=None return data ReportCycleSetting.register()