clinic/netforce_clinic/models/schedule.py

227 lines
7.3 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-11-14 01:58:12 +00:00
from netforce.access import get_active_company, get_active_user
2014-11-22 05:44:41 +00:00
from netforce.utils import get_data_path
2014-10-28 15:16:16 +00:00
class Schedule(Model):
_name="clinic.schedule"
2014-11-05 07:10:03 +00:00
_string="Schedule"
2014-11-14 01:58:12 +00:00
_field_name="date"
2014-10-28 15:16:16 +00:00
def _get_name(self,ids,context={}):
res={}
for obj in self.browse(ids):
2014-11-17 12:13:03 +00:00
st="\n" # XXX
2014-11-21 02:39:26 +00:00
count=0
2014-11-17 12:13:03 +00:00
for line in obj.lines:
nurse=line.nurse_id
2014-11-27 00:47:38 +00:00
level=line.level_id
2014-11-30 06:00:35 +00:00
cycle=line.cycle_id
st+='C%s %s %s\n'%(cycle.sequence,nurse.name,level.name or "")
2014-11-21 02:39:26 +00:00
count+=1
res[obj.id]='%s'%(st if count else 'No 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):
2014-11-17 12:13:03 +00:00
cycle_id=None
if obj.cycle_id:
cycle_id=obj.cycle_id.id
item_ids=get_model("clinic.cycle.item").search([['cycle_id','=',cycle_id],['date','=',obj.time_start[0:10]]])
2014-11-11 01:35:53 +00:00
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={
2014-11-14 01:58:12 +00:00
'name': fields.Char("Name", function="_get_name"),
2014-10-28 15:16:16 +00:00
"time_start": fields.DateTime("Time Start",required=True),
"time_stop": fields.DateTime("Time Stop",required=True),
2014-11-17 00:59:19 +00:00
'date': fields.Date("Date",required=True,search=True),
2014-11-14 01:58:12 +00:00
'lines': fields.One2Many("clinic.schedule.line","schedule_id","Lines"),
2014-10-28 15:16:16 +00:00
'company_id': fields.Many2One("company","Company"),
2014-11-17 00:59:19 +00:00
'state': fields.Selection([['draft','Draft'],['confirmed', 'Confirmed']],'State',search=True),
'user_id': fields.Many2One("base.user","Confirm By"),
2014-10-28 15:16:16 +00:00
}
_defaults={
2014-11-14 01:58:12 +00:00
'user_id': lambda *a: get_active_user(),
2014-10-28 15:16:16 +00:00
"company_id": lambda *a: get_active_company(),
2014-11-14 01:58:12 +00:00
'date': lambda *a: datetime.now().strftime("%Y-%m-%d"),
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-17 00:59:19 +00:00
_order="date desc"
2014-11-14 01:58:12 +00:00
_sql_constraints=[
('schedule_uniq','unique (date,company_id)','Date should be unique'),
]
2014-11-01 06:04:30 +00:00
def confirm(self,ids,context={}):
obj=self.browse(ids)[0]
2014-12-03 04:48:20 +00:00
obj.copy2cycle_item()
2014-11-01 06:04:30 +00:00
obj.write({
'state': 'confirmed',
})
2014-11-27 00:47:38 +00:00
return {
'next': {
'name': 'clinic_schedule',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'Schedule %s has been confirmed'%obj.date,
}
2014-11-01 06:04:30 +00:00
def to_draft(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'draft',
})
2014-11-27 00:47:38 +00:00
return {
'next': {
'name': 'clinic_schedule',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'Schedule %s has been set to draft'%obj.date,
}
2014-11-01 06:04:30 +00:00
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]
2014-11-17 00:59:19 +00:00
datenow=datetime.now().strftime("%Y-%m-%d")
old_ids=get_model('clinic.schedule').search([['date','=',datenow]])
if old_ids:
raise Exception("This schedule is already create!")
2014-11-11 01:35:53 +00:00
vals={
2014-11-14 01:58:12 +00:00
'lines': [],
2014-11-11 01:35:53 +00:00
}
2014-11-14 01:58:12 +00:00
for line in obj.lines:
vals['lines'].append(('create', {
'nurse_id': line.nurse_id.id,
'cycle_id': line.cycle_id.id
}
))
new_id=get_model("clinic.schedule").create(vals)
new_obj=get_model("clinic.schedule").browse(new_id)
2014-11-11 01:35:53 +00:00
return {
'next': {
'name': 'clinic_schedule',
'mode': 'form',
2014-11-14 01:58:12 +00:00
'active_id': new_id,
2014-11-11 01:35:53 +00:00
},
2014-11-14 01:58:12 +00:00
'flash': 'Copy schedule from %s to %s successfully'%(obj.date,new_obj.date)
2014-11-11 01:35:53 +00:00
}
2014-11-14 01:58:12 +00:00
def copy2cycle_item(self,ids,context={}):
2014-11-11 01:35:53 +00:00
obj=self.browse(ids)[0]
2014-11-22 05:44:41 +00:00
date=obj.date
items={}
cycles=set()
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
2014-11-26 16:04:58 +00:00
item_objs=get_model("clinic.cycle.item").search_browse([['date','=',date],['cycle_id','=',cycle.id]])
if item_objs:
item=item_objs[0]
if item.state!='draft':
continue
if not items.get(item.id):
items[item.id]=[]
items[item.id].append(('create',{
2014-11-22 05:44:41 +00:00
'nurse_id': nurse.id,
'level_id': nurse.level_id.id,
}))
line.write({
2014-11-26 16:04:58 +00:00
'cycle_item_id': item.id,
2014-11-22 05:44:41 +00:00
})
cycles.update({cycle.name})
2014-11-25 01:41:49 +00:00
else:
item_id=get_model("clinic.cycle.item").create({
'cycle_id': cycle.id,
'date': date,
})
items[item_id]=[]
items[item_id].append(('create',{
'nurse_id': nurse.id,
'level_id': nurse.level_id.id,
}))
2014-11-27 07:36:27 +00:00
line.write({
'cycle_item_id': item_id,
})
2014-11-25 01:41:49 +00:00
cycles.update({cycle.name})
2014-11-22 05:44:41 +00:00
for item_id, nurses in items.items():
item=get_model("clinic.cycle.item").browse(item_id)
for nurse in item.nurses:
nurse.delete()
item.write({
'nurses': nurses,
2014-11-17 12:13:03 +00:00
})
2014-11-11 01:35:53 +00:00
2014-11-25 01:41:49 +00:00
cycles=list(reversed(list(cycles)))
msg='Copy nurses to cycle item %s successfully'%(','.join(cycles))
2014-11-22 05:44:41 +00:00
# TODO create cycle item automatically
if not items:
msg='No cycle item to copy'
2014-11-11 01:35:53 +00:00
return {
'next': {
2014-11-22 05:44:41 +00:00
'name': 'clinic_schedule',
2014-11-11 01:35:53 +00:00
'mode': 'form',
2014-11-22 05:44:41 +00:00
'active_id': obj.id,
2014-11-11 01:35:53 +00:00
},
2014-11-22 05:44:41 +00:00
'flash': msg,
2014-11-11 01:35:53 +00:00
}
2014-11-14 01:58:12 +00:00
def onchange_date(self,context={}):
data=context['data']
date=data['date']
time_start=data['time_start'][11:]
data['time_start']='%s %s'%(date,time_start)
time_stop=data['time_stop'][11:]
data['time_stop']='%s %s'%(date,time_stop)
return data
def load_all_nurse(self,ids,context={}):
nurses=get_model("clinic.staff").search_browse([['type','=','nurse']])
2014-11-14 01:58:12 +00:00
vals={
'lines': [],
}
for nurse in nurses:
vals['lines'].append({
'cycle_id': nurse.id,
'nurse_id': nurse.id,
2014-11-11 00:51:44 +00:00
})
2014-11-14 01:58:12 +00:00
obj=self.browse(ids)[0]
obj.write(vals)
def clear(self,ids,context={}):
obj=self.browse(ids)[0]
for line in obj.lines:
line.delete()
2014-11-11 00:51:44 +00:00
return {
'next': {
2014-11-14 01:58:12 +00:00
'name': 'clinic_schedule',
2014-11-11 00:51:44 +00:00
'mode': 'form',
2014-11-14 01:58:12 +00:00
'active_id': obj.id,
2014-11-11 00:51:44 +00:00
},
2014-11-14 01:58:12 +00:00
'flash': 'List of nurse has been cleared.',
2014-11-11 00:51:44 +00:00
}
2014-11-22 05:44:41 +00:00
def onchange_nurse(self,context={}):
data=context['data']
path=context["path"]
line=get_data_path(data,path,parent=True)
nurse_id=line['nurse_id']
nurse=get_model("clinic.staff").browse(nurse_id)
2014-11-22 05:44:41 +00:00
line['level_id']=nurse.level_id.id
return data
2014-10-28 15:16:16 +00:00
Schedule.register()
2014-11-14 01:58:12 +00:00