clinic/netforce_clinic/models/cycle_item.py

315 lines
9.9 KiB
Python
Raw Normal View History

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"
2014-10-26 08:48:51 +00:00
2014-11-21 02:39:26 +00:00
def _get_all(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
name="%s-%s"%(cycle_name,obj.date)
2014-11-21 02:39:26 +00:00
res[obj.id]={
2014-11-21 08:34:30 +00:00
'name': name,
2014-11-21 02:39:26 +00:00
}
return res
2014-11-21 08:34:30 +00:00
2014-10-26 08:48:51 +00:00
_fields={
2014-11-17 00:59:19 +00:00
'name': fields.Char("Name", function="_get_all",function_multi=True),
2014-10-26 08:48:51 +00:00
'date': fields.Date("Date",search=True),
2014-11-17 00:59:19 +00:00
'cycle_id': fields.Many2One("clinic.cycle", "Cycle",search=True),
2014-11-27 00:47:38 +00:00
'cycle_daily_id': fields.Many2One("clinic.cycle.daily", "Cycle Daily",search=True),
2014-11-21 02:39:26 +00:00
'lines': fields.One2Many("clinic.cycle.item.line","item_id", "Lines"),
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"),
2014-11-01 18:17:40 +00:00
'nurses': fields.One2Many("clinic.cycle.item.nurse",'cycle_item_id','Nurses'),
2014-12-04 02:52:49 +00:00
'nurse_id': fields.Many2One("clinic.staff","Nurse",domain=[['type','=','nurse']]), #XXX
2014-11-17 00:59:19 +00:00
'sequence': fields.Char("Sequence"), # for sort item
'company_id': fields.Many2One("company", "Company"),
2014-12-08 11:35:39 +00:00
"state": fields.Selection([("draft","Draft"),("validated","Validated")],"Status",required=True),
'user_id': fields.Many2One("base.user","Validator"),
2014-10-26 08:48:51 +00:00
}
2014-11-21 02:39:26 +00:00
def _get_vark(self,context={}):
st=get_model("clinic.setting").browse(1)
return st.var_k or 0
2014-10-26 08:48:51 +00:00
_defaults={
'state': 'draft',
'company_id': lambda *a: get_active_company(),
'date': lambda *a: time.strftime("%Y-%m-%d"),
2014-11-21 16:11:57 +00:00
'user_id': lambda *a: get_active_user(),
2014-11-21 02:39:26 +00:00
'var_k': _get_vark,
2014-10-26 08:48:51 +00:00
}
2014-11-22 05:44:41 +00:00
_order="date desc"
2014-11-11 00:51:44 +00:00
_sql_constraints=[
("cycle_item_uniq","unique (cycle_id,date,company_id)","Cycle item should be unique"),
]
2014-11-17 00:59:19 +00:00
2014-11-27 00:47:38 +00:00
def get_cycle_daily(self,date):
2014-11-17 00:59:19 +00:00
dom=[]
print("get ", date)
dom.append(['date','=',date])
2014-11-27 00:47:38 +00:00
cd_ids=get_model('clinic.cycle.daily').search(dom)
2014-11-17 00:59:19 +00:00
cd_id=None
if cd_ids:
cd_id=cd_ids[0]
else:
2014-11-27 00:47:38 +00:00
cd_id=get_model('clinic.cycle.daily').create({
2014-11-17 00:59:19 +00:00
'date':date,
'name':date,
})
return cd_id
2014-11-17 12:13:03 +00:00
2014-11-02 07:13:20 +00:00
def create(self, vals,**kw):
date=vals['date']
cycle_id=vals['cycle_id']
cycle=get_model("clinic.cycle").browse(cycle_id)
vals['sequence']='%s-%s'%(date,cycle.sequence) #date-sequence
obj_id=super().create(vals,**kw)
return obj_id
def write(self,ids,vals,**kw):
obj=self.browse(ids)[0]
cycle=obj.cycle_id
vals['sequence']='%s-%s'%(obj.date,cycle.sequence) #date-sequence
super().write(ids,vals,**kw)
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({
'state': 'validate',
})
return
2014-11-21 16:11:57 +00:00
levels={}
for line in obj.lines:
level=line.level_id
2014-11-27 15:14:31 +00:00
levels[level.id]={
'amount': line.amount or 0,
'qty': line.qty,
'level_id': level.id,
}
2014-11-21 16:11:57 +00:00
lines=[]
# cost's nurses
for nr in obj.nurses:
nurse=nr.nurse_id
level=nr.level_id
2014-11-27 15:14:31 +00:00
vals=levels.get(level.id)
rate,amt,qty=0.0,0.0,0
level_id=level.id
if vals:
level_id=vals['level_id']
amt=vals['amount']
qty=vals['qty']
if qty:
rate=amt/qty
2014-11-21 16:11:57 +00:00
lines.append(('create',{
'cycle_id': obj.cycle_id.id,
'staff_id': nurse.id,
2014-11-27 15:14:31 +00:00
'level_id': level_id,
'rate': rate,
2014-11-21 16:11:57 +00:00
'type': 'nurse',
2014-11-23 15:39:41 +00:00
'qty': 1,
2014-11-21 16:11:57 +00:00
}))
# cost's doctor
st=get_model('clinic.setting').browse(1)
cost_per_case=st.cost_per_case or 0
staff_total={}
2014-11-21 16:11:57 +00:00
for hd_case in obj.hd_cases:
staffs=hd_case.staffs
for ps in staffs:
staff=ps.staff_id
if not staff:
continue
base=staff.base
2014-11-21 16:11:57 +00:00
if not base:
base=cost_per_case
if not staff_total.get(staff.id):
staff_total[staff.id]={
2014-11-21 16:11:57 +00:00
'base': 0,
'level_id': staff.level_id.id,
2014-11-21 16:11:57 +00:00
'type': ps.type,
2014-11-23 15:39:41 +00:00
'qty': 0,
2014-11-21 16:11:57 +00:00
}
staff_total[staff.id]['base']=base
staff_total[staff.id]['qty']+=1
2014-11-21 16:11:57 +00:00
for doctor_id, value in staff_total.items():
2014-11-21 16:11:57 +00:00
base=value['base']
type=value['type']
2014-11-23 15:39:41 +00:00
qty=value['qty']
level_id=value['level_id']
2014-11-21 16:11:57 +00:00
lines.append(('create',{
'cycle_id': obj.cycle_id.id,
'staff_id': doctor_id,
2014-11-23 15:39:41 +00:00
'level_id': level_id,
'rate': base,
'qty': qty,
2014-11-21 16:11:57 +00:00
'type': type,
}))
2014-11-27 00:47:38 +00:00
cycle_daily=obj.cycle_daily_id
2014-11-21 16:11:57 +00:00
# clear cost line
2014-11-27 00:47:38 +00:00
for line in cycle_daily.lines:
2014-11-21 16:11:57 +00:00
cycle=line.cycle_id
if obj.cycle_id.id==cycle.id: # only own cycle
line.delete()
# group staff and cycle date
2014-11-23 13:44:25 +00:00
glines={}
for line in lines:
mode,vals=line
cycle_id=vals['cycle_id']
staff_id=vals['staff_id']
2014-11-23 15:39:41 +00:00
#amount=vals['amount'] or 0
rate=vals['rate'] or 0
qty=vals['qty'] or 0
amount=qty*rate
key=(cycle_id,staff_id)
2014-11-23 13:44:25 +00:00
if not key in glines.keys():
glines[key]={
2014-11-23 15:39:41 +00:00
'amount': amount,
2014-11-23 13:44:25 +00:00
'type': vals['type'],
2014-11-23 15:39:41 +00:00
'level_id': vals['level_id'],
'rate': rate,
'qty': qty,
2014-11-23 13:44:25 +00:00
}
continue
2014-11-23 15:39:41 +00:00
glines[key]['amount']+=amount
glines[key]['qty']+=qty
2014-11-23 13:44:25 +00:00
lines=[]
for key,vals in glines.items():
cycle_id,staff_id=key
2014-11-23 13:44:25 +00:00
line={
'cycle_id': cycle_id,
'staff_id': staff_id,
2014-11-24 04:00:58 +00:00
'date': obj.date,
2014-11-23 13:44:25 +00:00
}
line.update(vals)
lines.append(('create',line))
# call from outside
if context.get('called'):
return lines
2014-11-27 00:47:38 +00:00
cycle_daily.write({
2014-11-21 16:11:57 +00:00
'lines': lines,
})
2014-11-03 00:21:37 +00:00
obj.write({
'state': 'done',
})
2014-11-21 16:11:57 +00:00
return {
'next': {
2014-11-27 00:47:38 +00:00
'name': 'clinic_cycle_daily',
2014-11-21 16:11:57 +00:00
'mode': 'form',
2014-11-27 00:47:38 +00:00
'active_id': cycle_daily.id,
2014-11-21 16:11:57 +00:00
},
2014-11-26 16:04:58 +00:00
'flash': 'Cycle Item has been validated, please see the detail of cost below.',
2014-11-21 16:11:57 +00:00
}
2014-11-03 00:21:37 +00:00
def to_draft(self,ids,context={}):
obj=self.browse(ids)[0]
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']
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]
date=obj.date
schd_ids=get_model('clinic.schedule').search([['date','=',date]])
schedule_id=None
if schd_ids:
schedule_id=schd_ids[0]
return {
'next': {
'name': 'clinic_schedule',
'mode': 'form',
'active_id': schedule_id,
}
}
2014-11-26 16:04:58 +00:00
2014-11-27 00:47:38 +00:00
def view_cycle_daily(self,ids,context={}):
2014-11-26 16:04:58 +00:00
obj=self.browse(ids)[0]
2014-11-27 00:47:38 +00:00
if not obj.cycle_daily_id:
2014-11-26 16:04:58 +00:00
raise Exception("Please validate cycle item")
return {
'next': {
2014-11-27 00:47:38 +00:00
'name': 'clinic_cycle_daily',
2014-11-26 16:04:58 +00:00
'mode': 'form',
2014-11-27 00:47:38 +00:00
'active_id': obj.cycle_daily_id.id,
2014-11-26 16:04:58 +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]])
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',
}
2014-11-01 17:30:48 +00:00
2014-10-26 08:48:51 +00:00
CycleItem.register()