clinic/netforce_clinic/models/cycle_item.py

208 lines
6.9 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_all(self,ids,context={}):
res={}
for obj in self.browse(ids):
cycle=obj.cycle_id
cycle_name=''
if cycle:
cycle_name=cycle.name
name="%s-%s"%(cycle_name,obj.date)
res[obj.id]={
'name': name,
'sequence':'%s-%s'%(obj.date,cycle.sequence),
}
return res
_fields={
'name': fields.Char("Name",function="_get_all",function_multi=True,store=True),
'date': fields.Date("Date",search=True),
'cycle_id': fields.Many2One("clinic.cycle", "Cycle",search=True),
'lines': fields.One2Many("clinic.cycle.item.line","item_id", "Lines"),
'visits': fields.One2Many("clinic.visit","cycle_item_id", "Visits"),
'hd_cases': fields.One2Many("clinic.hd.case","cycle_item_id", "HD Cases"),
'nurses': fields.One2Many("clinic.cycle.item.nurse",'cycle_item_id','Nurses'),
'nurse_id': fields.Many2One("clinic.staff","Nurse",domain=[['type','=','nurse']]),
'sequence': fields.Char("Sequence",function="_get_all",function_multi=True,store=True),
"state": fields.Selection([("draft","Draft"),("validated","Validated")],"Status",required=True),
'user_id': fields.Many2One("base.user","Validator"),
"comments": fields.One2Many("message","related_id","Comments"), "company_id": fields.Many2One("company","Company"),
'company_id': fields.Many2One("company", "Company"),
'branch_id': fields.Many2One("clinic.branch","Branch"),
}
def _get_vark(self,context={}):
st=get_model("clinic.setting").browse(1)
return st.var_k or 0
_defaults={
'state': 'draft',
'company_id': lambda *a: get_active_company(),
'date': lambda *a: time.strftime("%Y-%m-%d"),
'user_id': lambda *a: get_active_user(),
'var_k': _get_vark,
}
_order="date desc"
_sql_constraints=[
("cycle_item_uniq","unique (cycle_id,date,company_id)","Cycle item should be unique"),
]
def get_cycle_daily(self,date):
dom=[]
print("get ", date)
dom.append(['date','=',date])
cd_ids=get_model('clinic.cycle.daily').search(dom)
cd_id=None
if cd_ids:
cd_id=cd_ids[0]
else:
cd_id=get_model('clinic.cycle.daily').create({
'date':date,
'name':date,
})
return cd_id
def create(self, vals,**kw):
print('kw ', 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(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',
})
return {
'next': {
'name': 'clinic_cycle_item',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'Cycle Item has been validated.',
}
def to_draft(self,ids,context={}):
obj=self.browse(ids)[0]
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]
date=obj.date
schd_ids=get_model('clinic.schedule').search([['date','=',date]])
schedule_id=None
if schd_ids:
schedule_id=schd_ids[0]
if not schedule_id:
lines=[]
for lnurse in obj.nurses:
nurse=lnurse.nurse_id
level=nurse.level_id
lines.append(('create',{
'cycle_id': obj.cycle_id.id,
'cycle_item_id': obj.id,
'nurse_id': nurse.id,
'level_id': level.id ,
'note': '',
}))
schedule_id=get_model('clinic.schedule').create({
'date': date,
'time_start': '%s 08:00:00'%date, #XXX
'time_stop': '%s 21:00:00'%date, #XXX
'lines': lines,
})
return {
'next': {
'name': 'clinic_schedule',
'mode': 'form',
'active_id': schedule_id,
},
}
def view_cycle_daily(self,ids,context={}):
obj=self.browse(ids)[0]
if not obj.cycle_daily_id:
raise Exception("Please validate cycle item")
return {
'next': {
'name': 'clinic_cycle_daily',
'mode': 'form',
'active_id': obj.cycle_daily_id.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]])
nurses=[]
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
nurses.append(('create',{
'nurse_id': nurse.id,
'level_id': level.id,
}))
for nurse in obj.nurses:
nurse.delete()
obj.write({
'nurses': nurses,
})
return {
'next': {
'name': 'clinic_cycle_item',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'Load nurse from schedule to cycle item successfully',
}
CycleItem.register()