clinic/netforce_clinic/models/schedule.py

247 lines
7.9 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"
2015-01-22 07:20:04 +00:00
_name_field="name"
2014-12-26 02:37:59 +00:00
_key=['name']
2014-10-28 15:16:16 +00:00
def _get_name(self,ids,context={}):
res={}
for obj in self.browse(ids):
2015-01-22 07:20:04 +00:00
count=len(obj.lines)
dpt=obj.department_id
name='%s %s'%(obj.date, dpt.name or '')
res[obj.id]='%s'%(name if count else 'Waiting planing')
2014-10-28 15:16:16 +00:00
return res
2015-01-22 07:20:04 +00:00
2014-10-28 15:16:16 +00:00
_fields={
2015-01-22 07:20:04 +00:00
'name': fields.Char("Name", function="_get_name",store=True),
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"),
2015-01-14 13:36:23 +00:00
'branch_id': fields.Many2One("clinic.branch","Branch",required=True, search=True),
2015-01-16 11:19:49 +00:00
'department_id': fields.Many2One("clinic.department","Department",required=True, search=True),
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
}
2014-12-09 07:29:30 +00:00
def _get_date(self,context={}):
datenow=datetime.now().strftime("%Y-%m-%d")
return datenow
2014-10-28 15:16:16 +00:00
2015-01-14 13:36:23 +00:00
def _get_branch(self,context={}):
b_ids=get_model("clinic.branch").search([])
if b_ids:
return b_ids[0]
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(),
2015-01-14 13:36:23 +00:00
'branch_id': _get_branch,
2014-12-09 07:29:30 +00:00
'date': _get_date,
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=[
2015-01-16 11:19:49 +00:00
('schedule_uniq','unique (date,company_id,branch_id,department_id)','Date should be unique'),
2014-11-14 01:58:12 +00:00
]
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]
2015-01-16 11:19:49 +00:00
branch=obj.branch_id
department=obj.department_id
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
2015-01-16 11:19:49 +00:00
dom=[
['date','=',date],
['cycle_id','=',cycle.id],
['branch_id','=',branch.id],
['department_id','=',department.id],
]
item_objs=get_model("clinic.cycle.item").search_browse(dom)
2014-11-26 16:04:58 +00:00
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,
2015-01-16 11:19:49 +00:00
'branch_id': branch.id,
'department_id': department.id,
2014-11-25 01:41:49 +00:00
})
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, lines in items.items():
2014-11-22 05:44:41 +00:00
item=get_model("clinic.cycle.item").browse(item_id)
for line in item.lines:
line.delete()
2014-11-22 05:44:41 +00:00
item.write({
'lines': lines,
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
2015-01-22 10:54:51 +00:00
line['state']=nurse.state
2014-11-22 05:44:41 +00:00
return data
2015-01-22 07:20:04 +00:00
def create(self,vals,**kw):
id=super().create(vals,**kw)
self.function_store([id])
2015-01-22 08:07:22 +00:00
return id
2015-01-22 07:20:04 +00:00
def write(self,ids,vals,**kw):
super().write(ids,vals,**kw)
self.function_store(ids)
def onchange_branch(self,context={}):
data=context['data']
data['department_id']=None
return data
2014-11-22 05:44:41 +00:00
2014-10-28 15:16:16 +00:00
Schedule.register()
2014-11-14 01:58:12 +00:00