from netforce.model import Model, fields, get_model class ChangeVisit(Model): _name="clinic.change.visit" _transient=True _fields={ "visit_id": fields.Many2One("clinic.visit","Visit",required=True,on_delete="cascade"), "time_start": fields.DateTime("Start Time",required=True), "time_stop": fields.DateTime("End Time",required=True), "patient_id": fields.Many2One("clinic.patient","Patient",required=True,search=True,domain=[['state','=','admit']]), "doctor_id": fields.Many2One("clinic.staff","Doctor", domain=[['type','=','doctor']],search=True), "department_id": fields.Many2One("clinic.department", "Department",search=True), 'branch_id': fields.Many2One("clinic.branch","Branch"), "cycle_id": fields.Many2One("clinic.cycle","Cycle"), 'visit_date': fields.Date('Date',required=True), 'note': fields.Text('Note'), } def default_get(self,field_names=None,context={},**kw): print("clinic.change.visit.default") #defaults=context.get("defaults",{}) refer_id=context.get("refer_id") res={} if refer_id: refer_id=int(refer_id) obj=get_model("clinic.visit").browse(refer_id) res={ 'visit_id': refer_id, 'patient_id': obj.patient_id.id, 'doctor_id': obj.doctor_id.id, 'department_id': obj.department_id.id, 'branch_id': obj.department_id.branch_id.id, 'cycle_id': obj.cycle_id.id, 'time_start': obj.time_start, 'time_stop': obj.time_stop, 'visit_date': obj.visit_date, } print(res) return res def do_change(self,ids,context): obj=self.browse(ids)[0] dom=[ ['visit_date','=',obj.visit_date], ['patient_id','=',obj.patient_id.id], ['cycle_id','=',obj.cycle_id.id], #XXX ['department_id','=',obj.department_id.id], ] res=get_model("clinic.visit").search(dom) if res: raise Exception("This visit already exist in the system!") new_id=get_model("clinic.visit").create({ 'patient_id': obj.patient_id.id, 'doctor_id': obj.doctor_id.id, 'department_id': obj.department_id.id, 'branch_id': obj.department_id.branch_id.id, 'cycle_id': obj.cycle_id.id, 'time_start': obj.time_start, 'time_stop': obj.time_stop, 'visit_date': obj.visit_date, 'state': 'pending', }) obj.visit_id.to_draft() obj.visit_id.cancel() visit=get_model("clinic.visit").browse(new_id) return { 'next': { 'name': 'clinic_visit', 'mode': 'form', 'active_id': new_id, }, 'flash': 'Copy visit %s to %s has been successfully'%(obj.visit_id.visit_date, visit.visit_date) } def onchange_date(self,context={}): data=context['data'] date=data['visit_date'] time_start=data['time_start'][11:] time_stop=data['time_stop'][11:] data['time_start']='%s %s'%(date,time_start) data['time_stop']='%s %s'%(date,time_stop) return data def onchange_department(self,context={}): data=context['data'] dpt_id=data['department_id'] if dpt_id: dpt=get_model("clinic.department").browse(dpt_id) data['branch_id']=dpt.branch_id.id return data def onchange_cycle(self,context={}): data=context['data'] cycle_id=data['cycle_id'] date=data['visit_date'] if not date: date=data['time_start'][0:10] if cycle_id: cycle=get_model('clinic.cycle').browse(cycle_id) data['time_start']=date+' %s:00'%cycle.time_start data['time_stop']=date+' %s:00'%cycle.time_stop return data def onchange_datefrom(self,context={}): data=context['data'] data['visit_date']=data['time_start'][0:10] return data def onchange_patient(self,context={}): data=context['data'] patient_id=data['patient_id'] patient=get_model("clinic.patient").browse(patient_id) doctor=patient.doctor_id visits=self.search_browse([['patient_id','=',patient_id]],order="number desc") if visits: visit=visits[0] department_id=None if visit.department_id: department_id=visit.department_id.id elif patient.department_id: department_id=patient.department_id.id branch_id=None if visit.branch_id: branch_id=visit.branch_id.id elif patient.branch_id: branch_id=patient.branch_id.id data['department_id']=department_id data['branch_id']=branch_id else: department=patient.department_id branch=patient.branch_id data['department_id']=department.id data['branch_id']=branch.id data['doctor_id']=doctor.id return data ChangeVisit.register()