clinic/netforce_clinic/models/schedule.py

131 lines
4.2 KiB
Python
Raw Normal View History

2014-11-03 11:22:48 +00:00
from datetime import datetime, timedelta
2014-11-11 00:51:44 +00:00
from netforce.model import Model, fields, get_model
2014-10-28 15:16:16 +00:00
from netforce.access import get_active_company
class Schedule(Model):
_name="clinic.schedule"
2014-11-05 07:10:03 +00:00
_string="Schedule"
2014-10-28 15:16:16 +00:00
#_field_name="cycle_id"
def _get_name(self,ids,context={}):
res={}
for obj in self.browse(ids):
total_nurse=len(obj.nurses)
2014-11-01 17:30:48 +00:00
res[obj.id]='%s, Nurse: %s'%(obj.cycle_id.name or "", total_nurse)
2014-10-28 15:16:16 +00:00
return res
2014-11-11 01:35:53 +00:00
def _get_item(self,ids,context={}):
res={}
for obj in self.browse(ids):
item_ids=get_model("clinic.cycle.item").search([['cycle_id','=',obj.cycle_id.id],['date','=',obj.time_start[0:10]]])
item_id=None
if item_ids:
item_id=item_ids[0]
res[obj.id]=item_id
return res
2014-10-28 15:16:16 +00:00
_fields={
"name": fields.Char("Name",function="_get_name"),
'cycle_id': fields.Many2One("clinic.cycle","Cycle",required=True),
2014-11-11 01:35:53 +00:00
'cycle_item_id': fields.Many2One("clinic.cycle.item","Cycle Item", function="_get_item"),
2014-10-28 15:16:16 +00:00
"time_start": fields.DateTime("Time Start",required=True),
"time_stop": fields.DateTime("Time Stop",required=True),
'company_id': fields.Many2One("company","Company"),
2014-11-01 06:04:30 +00:00
'nurses': fields.Many2Many('clinic.personal','Nurses'), # XXX domain
'state': fields.Selection([['draft','Draft'],['confirmed', 'Confirmed']],'State'),
2014-10-28 15:16:16 +00:00
}
_defaults={
"company_id": lambda *a: get_active_company(),
2014-11-03 11:22:48 +00:00
'time_start': lambda *a: datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'time_stop': lambda *a: (datetime.now()+timedelta(seconds=3600)).strftime("%Y-%m-%d %H:%M:%S"),
'state': 'draft',
2014-10-28 15:16:16 +00:00
}
2014-11-01 17:30:48 +00:00
2014-11-01 06:04:30 +00:00
def confirm(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'confirmed',
})
def to_draft(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'draft',
})
2014-11-11 00:51:44 +00:00
2014-11-11 01:35:53 +00:00
def copy(self,ids,context={}):
obj=self.browse(ids)[0]
nurse_ids=[nurse.id for nurse in obj.nurses]
vals={
'cycle_id': obj.cycle_id.id,
'time_start': obj.time_start,
'time_stop': obj.time_stop,
'nurses': [('add', nurse_ids)],
}
obj_id=get_model("clinic.schedule").create(vals)
return {
'next': {
'name': 'clinic_schedule',
'mode': 'form',
'active_id': obj_id,
},
'flash': 'Copy schedule successfully',
}
def copy_nurses(self,ids,context={}):
obj=self.browse(ids)[0]
nurses=[]
for nurse in obj.nurses:
nurses.append(('create',{
'nurse_id': nurse.id,
'level_id': nurse.level_id.id,
}))
for nurse in obj.cycle_item_id.nurses:
nurse.delete()
obj.cycle_item_id.write({
'nurses': nurses,
})
return {
'next': {
'name': 'clinic_cycle_item',
'mode': 'form',
'active_id': obj.cycle_item_id.id,
},
'flash': 'Copy nurses to cycle item successfully',
}
2014-11-11 00:51:44 +00:00
def view_cycle_item(self,ids,context={}):
obj=self.browse(ids)[0]
2014-11-11 01:35:53 +00:00
item_id=obj.cycle_item_id.id
2014-11-11 00:51:44 +00:00
msg=''
if not item_id:
# XXX
item_id=get_model("clinic.cycle.item").create({
'cycle_id': obj.cycle_id.id,
'date': obj.time_start[0:10],
})
#raise Exception("No found cycle item")
item_nurse_obj=get_model("clinic.cycle.item.nurse")
for nurse in obj.nurses:
item_nurse_obj.create({
'cycle_item_id': item_id,
'nurse_id': nurse.id,
'level_id': nurse.level_id.id,
})
msg='Create Cycle Item successfully'
return {
'next': {
'name': 'clinic_cycle_item',
'mode': 'form',
'active_id': item_id,
},
'flash': msg,
}
2014-10-28 15:16:16 +00:00
Schedule.register()