clinic/netforce_clinic/models/load_nurse.py

110 lines
3.5 KiB
Python
Raw Permalink Normal View History

2014-11-14 01:58:12 +00:00
from netforce.model import Model, fields, get_model
class LoadNurse(Model):
_name="clinic.schedule.load.nurse"
_transient=True
_fields={
2014-11-17 00:59:19 +00:00
"schedule_id": fields.Many2One("clinic.schedule","Schedule",on_delete="cascade"),
'categ_id': fields.Many2One("clinic.staff.categ","Category", domain=[['type','=','nurse']]),
2015-01-13 14:33:38 +00:00
'level_id': fields.Many2One("clinic.staff.level","Level", domain=[['type','=','nurse']]),
2014-11-14 01:58:12 +00:00
"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=[]
2015-01-13 14:33:38 +00:00
for nurse in get_model("clinic.staff").search_browse([['type','=','nurse']], limit=5):
2014-11-14 01:58:12 +00:00
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]
2014-11-17 00:59:19 +00:00
if not obj.schedule_id:
raise Exception("Please save schedule before copy")
2014-11-14 01:58:12 +00:00
schedule=get_model("clinic.schedule").browse(obj.schedule_id.id)
vals={
'lines': [],
}
cycles=set()
2014-11-22 05:44:41 +00:00
lines=[]
2014-11-14 01:58:12 +00:00
for line in obj.lines:
nurse=line.nurse_id
2014-11-22 05:44:41 +00:00
cycle=line.cycle_id
cycles.update({(cycle.id,nurse.id)})
lines.append({
'cycle_id': cycle.id,
2014-11-14 01:58:12 +00:00
'nurse_id': nurse.id,
2015-01-13 14:33:38 +00:00
'cycle_seq': cycle.sequence or 0,
2014-11-22 05:44:41 +00:00
})
2014-11-14 01:58:12 +00:00
for line in schedule.lines:
2014-11-22 05:44:41 +00:00
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'],
}))
2014-11-14 01:58:12 +00:00
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']
2015-01-13 14:33:38 +00:00
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)
2014-11-14 01:58:12 +00:00
return data
2015-01-13 14:33:38 +00:00
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
2014-11-14 01:58:12 +00:00
LoadNurse.register()