clinic/netforce_clinic/models/load_nurse.py

91 lines
2.7 KiB
Python
Raw 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"),
2014-11-14 01:58:12 +00:00
'categ_id': fields.Many2One("clinic.personal.categ","Category", 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.personal").search_browse([['type','=','nurse']]):
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,
2014-11-22 05:44:41 +00:00
'cycle_seq': cycle.sequence,
})
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']
return data
LoadNurse.register()