clinic/netforce_clinic/models/cycle_item.py

264 lines
8.7 KiB
Python

import time
from netforce.model import Model, fields, get_model
from netforce.access import get_active_company, get_active_user
from netforce.utils import get_data_path
class CycleItem(Model):
_name="clinic.cycle.item"
_string="Cycle Item"
_name_field="name"
_multi_company=True
def _get_store(self,ids,context={}):
res={}
for obj in self.browse(ids):
cycle=obj.cycle_id
cycle_name=''
if cycle:
cycle_name=cycle.name
dpt=obj.department_id
name="%s-%s-%s"%(cycle_name,obj.date,dpt.name)
res[obj.id]={
'name': name,
'sequence':'%s-%s-%s-%s'%(obj.date,cycle.sequence,obj.branch_id.id,obj.department_id.id),
}
return res
def _get_all(self,ids,context={}):
res={}
for obj in self.browse(ids):
res[obj.id]={
'nurse_total': len(obj.lines),
}
return res
_fields={
'name': fields.Char("Name",function="_get_store",function_multi=True,store=True),
'date': fields.Date("Date",search=True),
'date_validate': fields.DateTime("Date Validate"),
'cycle_id': fields.Many2One("clinic.cycle", "Cycle",search=True),
'visits': fields.One2Many("clinic.visit","cycle_item_id", "Visits"),
'hd_cases': fields.One2Many("clinic.hd.case","cycle_item_id", "HD Cases"),
'lines': fields.One2Many("clinic.cycle.item.line",'cycle_item_id','Nurses'),
'sequence': fields.Char("Sequence",function="_get_store",function_multi=True,store=True),
'user_id': fields.Many2One("base.user","Validator"),
'company_id': fields.Many2One("company", "Company"),
'branch_id': fields.Many2One("clinic.branch", "Branch",search=True),
'department_id': fields.Many2One("clinic.department", "Department",search=True),
"comments": fields.One2Many("message","related_id","Comments"), "company_id": fields.Many2One("company","Company"),
"state": fields.Selection([("draft","Draft"),("validated","Validated")],"Status",required=True),
'nurse_total': fields.Integer("Nurses",function="_get_all",function_multi=True),
}
def _get_branch(self,context={}):
b_ids=get_model('clinic.branch').search([])
if b_ids:
return b_ids[0]
def _get_department(self,context={}):
dpt_ids=get_model('clinic.department').search([])
if dpt_ids:
return dpt_ids[0]
_defaults={
'state': 'draft',
'date': lambda *a: time.strftime("%Y-%m-%d"),
'user_id': lambda *a: get_active_user(),
'company_id': lambda *a: get_active_company(),
'branch_id': _get_branch,
'department_id': _get_department,
}
_order="date desc,department_id,cycle_id"
_sql_constraints=[
("cycle_item_uniq","unique (cycle_id,date,company_id,branch_id,department_id)","Cycle item should be unique"),
]
def create(self, vals,**kw):
new_id=super().create(vals,**kw)
self.function_store([new_id])
return new_id
def write(self,ids,vals,**kw):
super().write(ids,vals,**kw)
self.function_store(ids)
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.',
}
def validate(self,ids,context={}):
obj=self.browse(ids)[0]
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({
'state': 'validated',
'date_validate': time.strftime("%Y-%m-%d %H:%M:%S"),
})
return {
'next': {
'name': 'clinic_cycle_item',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'Cycle Item has been validated.',
}
def to_draft(self,ids,context={}):
for obj in self.browse(ids):
obj.write({
'state': 'draft',
})
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)
line['level_id']=nurse.level_id.id
return data
def view_schedule(self,ids,context={}):
obj=self.browse(ids)[0]
company=obj.company_id
branch=obj.branch_id
department=obj.department_id
cycle=obj.cycle_id
date=obj.date
dom=[
['date','=',date],
['company_id','=',company.id],
['branch_id','=',branch.id],
['department_id','=',department.id],
]
schd_ids=get_model('clinic.schedule').search(dom)
schedule_id=None
if schd_ids:
schedule_id=schd_ids[0]
if not schedule_id:
lines=[]
for line in obj.lines:
nurse=line.nurse_id
level=nurse.level_id
lines.append(('create',{
'cycle_id': cycle.id,
'cycle_item_id': obj.id,
'nurse_id': nurse.id,
'level_id': level.id ,
'note': '',
}))
schedule_id=get_model('clinic.schedule').create({
'branch_id': branch.id,
'department_id': department.id,
'company_id': company.id,
'date': date,
'time_start': '%s %s'%(date,cycle.time_start),
'time_stop': '%s %s'%(date,cycle.time_stop),
'lines': lines,
})
return {
'next': {
'name': 'clinic_schedule',
'mode': 'form',
'active_id': schedule_id,
},
}
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]])
lines=[]
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
lines.append(('create',{
'nurse_id': nurse.id,
'level_id': level.id,
}))
for line in obj.lines:
line.delete()
if not lines:
raise Exception("Nurse not found in schedule %s"%obj.date)
obj.write({
'lines': lines,
})
return {
'next': {
'name': 'clinic_cycle_item',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'Load nurse from schedule to cycle item successfully',
}
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,
})
return {
'next': {
'name': 'clinic_cycle_item',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'Update HDCase successfully',
}
def cycle_item_copy(self,ids,context={}):
obj=self.browse(ids)[0]
return {
'next': {
'name': 'clinic_cycle_item_copy',
'refer_id': obj.id,
},
}
CycleItem.register()