from netforce.model import Model, fields, get_model class CycleItemCopy(Model): _name="clinic.cycle.item.copy" _transient=True _fields={ "cycle_item_id": fields.Many2One("clinic.cycle.item","Cycle Item",required=True,on_delete="cascade"), 'choice': fields.Selection([['item','Cycle Item'],['schd','Schedule']],'Choice'), "item_copy_id": fields.Many2One("clinic.cycle.item","Cycle Item"), "schd_copy_id": fields.Many2One("clinic.schedule","Schedule"), 'department_id': fields.Many2One("clinic.department","Department"), 'lines': fields.One2Many("clinic.cycle.item.copy.line","item_copy_id","Lines"), } def _get_cycle_item_id(self,context={}): refer_id=context.get("refer_id") if not refer_id: return None return int(refer_id) def _get_department(self,context={}): refer_id=context.get("refer_id") if not refer_id: return None item=get_model('clinic.cycle.item').browse(int(refer_id)) dpt=item.department_id print('dpt ', dpt.id) return dpt.id _defaults={ 'cycle_item_id': _get_cycle_item_id, 'department_id': _get_department, 'choice': 'item', } def update_line(self,context={}): data=context['data'] lines=[] if data['choice']=='item': item=get_model("clinic.cycle.item").browse(data['item_copy_id']) for line in item.lines: nr=line.nurse_id dpt=nr.department_id lines.append({ 'nurse_id':nr.id, 'department_id': dpt.id, }) elif data['choice']=='schd': item=get_model("clinic.cycle.item").browse(data['cycle_item_id']) schd=get_model("clinic.schedule").browse(data['schd_copy_id']) cycle=item.cycle_id for line in schd.lines: nr=line.nurse_id dpt=nr.department_id if cycle.id==line.cycle_id.id: lines.append({ 'nurse_id':nr.id, 'department_id': dpt.id, }) else: pass return lines def onchange_choice(self,context={}): data=context['data'] if data['choice']: data['lines']=self.update_line(context=context) return data def onchange_item(self,context={}): data=context['data'] data['lines']=self.update_line(context=context) return data def load(self,ids,context={}): obj=self.browse(ids)[0] item=obj.cycle_item_id for line in item.lines: line.delete() lines=[] for line in obj.lines: nr=line.nurse_id lines.append(('create', { 'nurse_id': nr.id, 'level_id': nr.level_id.id, 'state': nr.state, })) item.write({ 'lines': lines, }) return { 'next': { 'name': 'clinic_cycle_item', 'mode': 'form', 'active_id': item.id, }, 'flash': 'Load Nurse Complete!', } CycleItemCopy.register()