from netforce.model import Model, fields, get_model class LoadNurse(Model): _name="clinic.schedule.load.nurse" _transient=True _fields={ "schedule_id": fields.Many2One("clinic.schedule","Schedule",on_delete="cascade"), 'categ_id': fields.Many2One("clinic.staff.categ","Category", domain=[['type','=','nurse']]), 'level_id': fields.Many2One("clinic.staff.level","Level", domain=[['type','=','nurse']]), "lines": fields.One2Many("clinic.schedule.load.nurse.line",'load_id', 'Lines'), } def _get_schedule_id(self,context={}): schedule_id=context.get("refer_id") if not schedule_id: return None return int(schedule_id) def _get_lines(self,context={}): cycle_ids=get_model("clinic.cycle").search([]) cycle_id=None if cycle_ids: cycle_id=cycle_ids[0] lines=[] for nurse in get_model("clinic.staff").search_browse([['type','=','nurse']], limit=5): in_cycle_id=None if nurse.cycle_id.id: in_cycle_id=nurse.cycle_id.id lines.append({ 'nurse_id': nurse.id, 'cycle_id': in_cycle_id and in_cycle_id or cycle_id, }) return lines _defaults={ 'schedule_id': _get_schedule_id, 'lines': _get_lines, } def copy2schedule(self,ids,context): obj=self.browse(ids)[0] if not obj.schedule_id: raise Exception("Please save schedule before copy") schedule=get_model("clinic.schedule").browse(obj.schedule_id.id) vals={ 'lines': [], } cycles=set() lines=[] for line in obj.lines: nurse=line.nurse_id cycle=line.cycle_id cycles.update({(cycle.id,nurse.id)}) lines.append({ 'cycle_id': cycle.id, 'nurse_id': nurse.id, 'cycle_seq': cycle.sequence or 0, }) for line in schedule.lines: for cycle in cycles: cycle_id,nurse_id=cycle if line.cycle_id.id==cycle_id and line.nurse_id.id==nurse_id: line.delete() for line in sorted(lines,key=lambda x: x['cycle_seq']): vals['lines'].append(('create',{ 'cycle_id': line['cycle_id'], 'nurse_id': line['nurse_id'], })) schedule.write(vals) return { 'next': { 'name': 'clinic_schedule', 'mode': 'form', 'active_id': schedule.id, }, 'flash': 'Copy nurse to schedule succesfully' } def onchange_categ(self,context={}): data=context['data'] data['lines']=[] categ_id=data['categ_id'] for nurse in get_model("clinic.staff").search([['type','=','nurse'],['categ_id','=',categ_id]]): data['lines'].append(('create',{ 'nurse_id': nurse.id} )) print('data ', data) return data def onchange_level(self,context={}): data=context['data'] data['lines']=[] level_id=data['level_id'] for nurse in get_model("clinic.staff").search_browse([['type','=','nurse']]): if level_id==nurse.level_id.id: data['lines'].append({ 'nurse_id': nurse.id, 'cycle_id': nurse.cycle_id.id, }) return data LoadNurse.register()