2014-10-26 08:48:51 +00:00
|
|
|
import time
|
|
|
|
|
2014-11-02 07:13:20 +00:00
|
|
|
from netforce.model import Model, fields, get_model
|
2014-11-21 16:11:57 +00:00
|
|
|
from netforce.access import get_active_company, get_active_user
|
2014-11-21 08:34:30 +00:00
|
|
|
from netforce.utils import get_data_path
|
2014-10-26 08:48:51 +00:00
|
|
|
|
|
|
|
class CycleItem(Model):
|
|
|
|
_name="clinic.cycle.item"
|
|
|
|
_string="Cycle Item"
|
2014-10-26 10:11:19 +00:00
|
|
|
_name_field="name"
|
2015-01-09 05:19:52 +00:00
|
|
|
_multi_company=True
|
2014-10-26 08:48:51 +00:00
|
|
|
|
2015-01-20 13:58:38 +00:00
|
|
|
def _get_store(self,ids,context={}):
|
2014-10-26 08:48:51 +00:00
|
|
|
res={}
|
|
|
|
for obj in self.browse(ids):
|
2014-11-30 06:30:09 +00:00
|
|
|
cycle=obj.cycle_id
|
|
|
|
cycle_name=''
|
|
|
|
if cycle:
|
|
|
|
cycle_name=cycle.name
|
2015-01-18 10:31:54 +00:00
|
|
|
dpt=obj.department_id
|
|
|
|
name="%s-%s-%s"%(cycle_name,obj.date,dpt.name)
|
2014-11-21 02:39:26 +00:00
|
|
|
res[obj.id]={
|
2014-11-21 08:34:30 +00:00
|
|
|
'name': name,
|
2015-01-16 15:34:20 +00:00
|
|
|
'sequence':'%s-%s-%s-%s'%(obj.date,cycle.sequence,obj.branch_id.id,obj.department_id.id),
|
2014-11-21 02:39:26 +00:00
|
|
|
}
|
|
|
|
return res
|
2014-11-21 08:34:30 +00:00
|
|
|
|
2015-01-20 13:58:38 +00:00
|
|
|
def _get_all(self,ids,context={}):
|
|
|
|
res={}
|
|
|
|
for obj in self.browse(ids):
|
|
|
|
res[obj.id]={
|
|
|
|
'nurse_total': len(obj.lines),
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
|
2014-10-26 08:48:51 +00:00
|
|
|
_fields={
|
2015-01-20 13:58:38 +00:00
|
|
|
'name': fields.Char("Name",function="_get_store",function_multi=True,store=True),
|
2014-10-26 08:48:51 +00:00
|
|
|
'date': fields.Date("Date",search=True),
|
2015-01-22 08:07:22 +00:00
|
|
|
'date_validate': fields.DateTime("Date Validate"),
|
2014-11-17 00:59:19 +00:00
|
|
|
'cycle_id': fields.Many2One("clinic.cycle", "Cycle",search=True),
|
2014-11-01 08:49:27 +00:00
|
|
|
'visits': fields.One2Many("clinic.visit","cycle_item_id", "Visits"),
|
2014-11-17 00:59:19 +00:00
|
|
|
'hd_cases': fields.One2Many("clinic.hd.case","cycle_item_id", "HD Cases"),
|
2015-01-17 13:34:00 +00:00
|
|
|
'lines': fields.One2Many("clinic.cycle.item.line",'cycle_item_id','Nurses'),
|
2015-01-20 13:58:38 +00:00
|
|
|
'sequence': fields.Char("Sequence",function="_get_store",function_multi=True,store=True),
|
2014-12-08 11:35:39 +00:00
|
|
|
'user_id': fields.Many2One("base.user","Validator"),
|
2015-01-11 06:09:40 +00:00
|
|
|
'company_id': fields.Many2One("company", "Company"),
|
2015-01-19 01:27:10 +00:00
|
|
|
'branch_id': fields.Many2One("clinic.branch", "Branch",search=True),
|
|
|
|
'department_id': fields.Many2One("clinic.department", "Department",search=True),
|
2015-01-17 13:34:00 +00:00
|
|
|
"comments": fields.One2Many("message","related_id","Comments"), "company_id": fields.Many2One("company","Company"),
|
|
|
|
"state": fields.Selection([("draft","Draft"),("validated","Validated")],"Status",required=True),
|
2015-01-22 08:07:22 +00:00
|
|
|
'nurse_total': fields.Integer("Nurses",function="_get_all",function_multi=True),
|
2014-10-26 08:48:51 +00:00
|
|
|
}
|
2014-11-21 02:39:26 +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]
|
|
|
|
|
2015-01-16 11:19:49 +00:00
|
|
|
def _get_department(self,context={}):
|
|
|
|
dpt_ids=get_model('clinic.department').search([])
|
|
|
|
if dpt_ids:
|
|
|
|
return dpt_ids[0]
|
|
|
|
|
2014-10-26 08:48:51 +00:00
|
|
|
_defaults={
|
|
|
|
'state': 'draft',
|
|
|
|
'date': lambda *a: time.strftime("%Y-%m-%d"),
|
2014-11-21 16:11:57 +00:00
|
|
|
'user_id': lambda *a: get_active_user(),
|
2015-01-17 13:34:00 +00:00
|
|
|
'company_id': lambda *a: get_active_company(),
|
2015-01-14 13:36:23 +00:00
|
|
|
'branch_id': _get_branch,
|
2015-01-16 11:19:49 +00:00
|
|
|
'department_id': _get_department,
|
2014-10-26 08:48:51 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 01:27:10 +00:00
|
|
|
_order="date desc,department_id,cycle_id"
|
2014-11-11 00:51:44 +00:00
|
|
|
_sql_constraints=[
|
2015-01-16 11:19:49 +00:00
|
|
|
("cycle_item_uniq","unique (cycle_id,date,company_id,branch_id,department_id)","Cycle item should be unique"),
|
2014-11-11 00:51:44 +00:00
|
|
|
]
|
2014-11-17 12:13:03 +00:00
|
|
|
|
2014-11-02 07:13:20 +00:00
|
|
|
def create(self, vals,**kw):
|
2014-12-19 16:57:34 +00:00
|
|
|
new_id=super().create(vals,**kw)
|
|
|
|
self.function_store([new_id])
|
|
|
|
return new_id
|
2014-11-02 07:13:20 +00:00
|
|
|
|
|
|
|
def write(self,ids,vals,**kw):
|
|
|
|
super().write(ids,vals,**kw)
|
2014-12-19 16:57:34 +00:00
|
|
|
self.function_store(ids)
|
2014-11-03 00:21:37 +00:00
|
|
|
|
2015-01-19 13:34:43 +00:00
|
|
|
def validate_all(self,ids,context={}):
|
|
|
|
for obj in self.browse(ids):
|
|
|
|
obj.validate()
|
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'clinic_cycle_item',
|
|
|
|
'mode': 'list',
|
|
|
|
},
|
|
|
|
'flash': 'Cycle Item has been validated.',
|
|
|
|
}
|
|
|
|
|
2014-11-03 00:21:37 +00:00
|
|
|
def validate(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
2014-12-08 11:35:39 +00:00
|
|
|
for obj_id in ids:
|
|
|
|
lcost_ids=get_model("clinic.labor.cost").search([['cycle_item_id','=',obj_id]])
|
|
|
|
labor_cost=get_model("clinic.labor.cost")
|
|
|
|
if not lcost_ids:
|
|
|
|
st=get_model("clinic.setting").browse(1)
|
|
|
|
lc_id=labor_cost.create({
|
|
|
|
'cycle_item_id': obj_id,
|
|
|
|
'var_k': st.var_k,
|
|
|
|
})
|
|
|
|
lc=labor_cost.browse(lc_id)
|
|
|
|
lc.compute()
|
|
|
|
else:
|
|
|
|
lc=labor_cost.search_browse([['cycle_item_id','=',obj.id]])
|
|
|
|
if lc:
|
|
|
|
lc.compute()
|
|
|
|
obj.write({
|
2014-12-08 14:54:25 +00:00
|
|
|
'state': 'validated',
|
2015-01-22 08:07:22 +00:00
|
|
|
'date_validate': time.strftime("%Y-%m-%d %H:%M:%S"),
|
2014-11-03 00:21:37 +00:00
|
|
|
})
|
|
|
|
|
2014-11-21 16:11:57 +00:00
|
|
|
return {
|
|
|
|
'next': {
|
2014-12-08 14:54:25 +00:00
|
|
|
'name': 'clinic_cycle_item',
|
2014-11-21 16:11:57 +00:00
|
|
|
'mode': 'form',
|
2014-12-08 14:54:25 +00:00
|
|
|
'active_id': obj.id,
|
2014-11-21 16:11:57 +00:00
|
|
|
},
|
2014-12-08 14:54:25 +00:00
|
|
|
'flash': 'Cycle Item has been validated.',
|
2014-11-21 16:11:57 +00:00
|
|
|
}
|
|
|
|
|
2014-11-03 00:21:37 +00:00
|
|
|
def to_draft(self,ids,context={}):
|
2015-01-19 13:34:43 +00:00
|
|
|
for obj in self.browse(ids):
|
|
|
|
obj.write({
|
|
|
|
'state': 'draft',
|
|
|
|
})
|
2014-11-21 08:34:30 +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']
|
2014-11-25 11:39:53 +00:00
|
|
|
nurse=get_model('clinic.staff').browse(nurse_id)
|
2014-11-21 08:34:30 +00:00
|
|
|
line['level_id']=nurse.level_id.id
|
|
|
|
return data
|
2014-11-22 05:44:41 +00:00
|
|
|
|
|
|
|
def view_schedule(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
2015-01-17 13:34:00 +00:00
|
|
|
company=obj.company_id
|
|
|
|
branch=obj.branch_id
|
|
|
|
department=obj.department_id
|
|
|
|
cycle=obj.cycle_id
|
2014-11-22 05:44:41 +00:00
|
|
|
date=obj.date
|
2015-01-17 13:34:00 +00:00
|
|
|
dom=[
|
|
|
|
['date','=',date],
|
|
|
|
['company_id','=',company.id],
|
|
|
|
['branch_id','=',branch.id],
|
|
|
|
['department_id','=',department.id],
|
|
|
|
]
|
|
|
|
schd_ids=get_model('clinic.schedule').search(dom)
|
2014-11-22 05:44:41 +00:00
|
|
|
schedule_id=None
|
|
|
|
if schd_ids:
|
|
|
|
schedule_id=schd_ids[0]
|
2014-12-09 07:29:30 +00:00
|
|
|
if not schedule_id:
|
2014-12-09 07:48:33 +00:00
|
|
|
lines=[]
|
2015-01-17 13:34:00 +00:00
|
|
|
for line in obj.lines:
|
|
|
|
nurse=line.nurse_id
|
2014-12-09 07:48:33 +00:00
|
|
|
level=nurse.level_id
|
|
|
|
lines.append(('create',{
|
2015-01-17 13:34:00 +00:00
|
|
|
'cycle_id': cycle.id,
|
2014-12-09 07:48:33 +00:00
|
|
|
'cycle_item_id': obj.id,
|
|
|
|
'nurse_id': nurse.id,
|
|
|
|
'level_id': level.id ,
|
|
|
|
'note': '',
|
|
|
|
}))
|
2014-12-09 07:29:30 +00:00
|
|
|
schedule_id=get_model('clinic.schedule').create({
|
2015-01-17 13:34:00 +00:00
|
|
|
'branch_id': branch.id,
|
|
|
|
'department_id': department.id,
|
|
|
|
'company_id': company.id,
|
2014-12-09 07:29:30 +00:00
|
|
|
'date': date,
|
2015-01-17 13:34:00 +00:00
|
|
|
'time_start': '%s %s'%(date,cycle.time_start),
|
|
|
|
'time_stop': '%s %s'%(date,cycle.time_stop),
|
2014-12-09 07:48:33 +00:00
|
|
|
'lines': lines,
|
2014-12-09 07:29:30 +00:00
|
|
|
})
|
2014-11-22 05:44:41 +00:00
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'clinic_schedule',
|
|
|
|
'mode': 'form',
|
|
|
|
'active_id': schedule_id,
|
2014-12-09 07:29:30 +00:00
|
|
|
},
|
2014-11-22 05:44:41 +00:00
|
|
|
}
|
2014-11-23 15:39:41 +00:00
|
|
|
|
|
|
|
def load_nurse_from_schedule(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
#TODO check cycle number & date from schedule
|
|
|
|
schedules=get_model("clinic.schedule").search_browse([['date','=',obj.date]])
|
2015-01-18 10:31:54 +00:00
|
|
|
lines=[]
|
2014-11-23 15:39:41 +00:00
|
|
|
for schedule in schedules:
|
|
|
|
for line in schedule.lines:
|
|
|
|
cycle=line.cycle_id
|
|
|
|
if obj.cycle_id.id==cycle.id:
|
|
|
|
nurse=line.nurse_id
|
|
|
|
level=line.level_id
|
2015-01-18 10:31:54 +00:00
|
|
|
lines.append(('create',{
|
2014-11-23 15:39:41 +00:00
|
|
|
'nurse_id': nurse.id,
|
|
|
|
'level_id': level.id,
|
|
|
|
}))
|
2015-01-18 10:31:54 +00:00
|
|
|
for line in obj.lines:
|
|
|
|
line.delete()
|
|
|
|
if not lines:
|
|
|
|
raise Exception("Nurse not found in schedule %s"%obj.date)
|
2014-11-23 15:39:41 +00:00
|
|
|
obj.write({
|
2015-01-18 10:31:54 +00:00
|
|
|
'lines': lines,
|
2014-11-23 15:39:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'clinic_cycle_item',
|
|
|
|
'mode': 'form',
|
|
|
|
'active_id': obj.id,
|
|
|
|
},
|
|
|
|
'flash': 'Load nurse from schedule to cycle item successfully',
|
|
|
|
}
|
2015-01-18 10:31:54 +00:00
|
|
|
|
|
|
|
def update_hdcase(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
dpt=obj.department_id
|
|
|
|
branch=obj.branch_id
|
|
|
|
cycle=obj.cycle_id
|
|
|
|
company=obj.company_id
|
|
|
|
date=obj.date
|
|
|
|
dom=[
|
|
|
|
['cycle_id','=',cycle.id],
|
|
|
|
['date','=',date],
|
|
|
|
['company_id','=',company.id],
|
|
|
|
['branch_id','=',branch.id],
|
|
|
|
['department_id','=',dpt.id],
|
|
|
|
]
|
|
|
|
for hdcase in get_model("clinic.hd.case").search_browse(dom):
|
|
|
|
hdcase.write({
|
|
|
|
'cycle_item_id': obj.id,
|
|
|
|
})
|
|
|
|
vs=hdcase.visit_id
|
|
|
|
vs.write({
|
|
|
|
'cycle_item_id': obj.id,
|
|
|
|
})
|
2015-01-19 01:27:10 +00:00
|
|
|
|
2015-01-18 10:31:54 +00:00
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'clinic_cycle_item',
|
|
|
|
'mode': 'form',
|
|
|
|
'active_id': obj.id,
|
|
|
|
},
|
|
|
|
'flash': 'Update HDCase successfully',
|
|
|
|
}
|
2015-01-22 01:34:22 +00:00
|
|
|
|
|
|
|
def cycle_item_copy(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'clinic_cycle_item_copy',
|
|
|
|
'refer_id': obj.id,
|
|
|
|
},
|
|
|
|
}
|
2014-11-01 17:30:48 +00:00
|
|
|
|
2014-10-26 08:48:51 +00:00
|
|
|
CycleItem.register()
|