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-10-26 10:11:19 +00:00
|
|
|
name="%s-%s"%(obj.cycle_id.name,obj.date)
|
2014-11-21 02:39:26 +00:00
|
|
|
total_case=len(obj.hd_cases)
|
2014-11-21 08:34:30 +00:00
|
|
|
total_a,total_b=0,0
|
|
|
|
total=0.0
|
|
|
|
for line in obj.lines:
|
|
|
|
total+=line.amount or 0
|
|
|
|
total_a+=line.var_a
|
|
|
|
total_b+=line.var_b
|
|
|
|
var_ptx=total_case*(obj.var_k or 0)
|
|
|
|
var_fml1='PTxK=%sX%s'%(total_a,total_b)
|
|
|
|
total_bstr=total_b < 0 and "+%s"%(abs(total_b)) or "-%s"%total_b
|
|
|
|
var_x=0.0
|
|
|
|
if total_a:
|
|
|
|
var_x=eval('(%s%s)/%s'%(var_ptx,total_bstr,total_a))
|
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
|
|
|
'var_pt': total_case,
|
2014-11-21 08:34:30 +00:00
|
|
|
'var_ptx': total_case*(obj.var_k or 0),
|
|
|
|
'var_fml1': '%s'%(var_fml1),
|
|
|
|
'var_fml2': '%s=%sX%s'%(var_ptx,total_a,total_b),
|
|
|
|
'var_fml3': '(%s%s)/%s'%(var_ptx,total_bstr,total_a),
|
|
|
|
'var_x': round(var_x,2),
|
|
|
|
'total': total,
|
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),
|
|
|
|
'cycle_dialy_id': fields.Many2One("clinic.cycle.dialy", "Cycle Dialy",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-11-17 00:59:19 +00:00
|
|
|
'sequence': fields.Char("Sequence"), # for sort item
|
|
|
|
'company_id': fields.Many2One("company", "Company"),
|
|
|
|
"state": fields.Selection([("draft","Draft"),("done","Done")],"Status",required=True),
|
2014-11-21 02:39:26 +00:00
|
|
|
'var_k': fields.Float("K"),
|
2014-11-21 08:34:30 +00:00
|
|
|
'var_pt': fields.Integer("PT", function="_get_all",function_multi=True),
|
|
|
|
'var_ptx': fields.Char("PT x K:", function="_get_all",function_multi=True),
|
2014-11-21 16:11:57 +00:00
|
|
|
'var_fml1': fields.Char("Step 1:",function="_get_all",function_multi=True),
|
|
|
|
'var_fml2': fields.Char("Step 2:",function="_get_all",function_multi=True),
|
|
|
|
'var_fml3': fields.Char("X:",function="_get_all",function_multi=True),
|
2014-11-21 08:34:30 +00:00
|
|
|
'var_x': fields.Char("X:",function="_get_all",function_multi=True),
|
|
|
|
'total': fields.Float("Total",function="_get_all",function_multi=True),
|
|
|
|
'manual': fields.Boolean("Manual"),
|
2014-11-21 16:11:57 +00:00
|
|
|
'user_id': fields.Many2One("base.user","User"),
|
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
|
|
|
|
|
|
|
def get_cycle_diary(self,date):
|
|
|
|
dom=[]
|
|
|
|
print("get ", date)
|
|
|
|
dom.append(['date','=',date])
|
|
|
|
cd_ids=get_model('clinic.cycle.dialy').search(dom)
|
|
|
|
cd_id=None
|
|
|
|
if cd_ids:
|
|
|
|
cd_id=cd_ids[0]
|
|
|
|
else:
|
|
|
|
cd_id=get_model('clinic.cycle.dialy').create({
|
|
|
|
'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']
|
2014-11-17 00:59:19 +00:00
|
|
|
vals['cycle_dialy_id']=self.get_cycle_diary(date)
|
|
|
|
|
2014-11-02 07:13:20 +00:00
|
|
|
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
|
2014-11-17 00:59:19 +00:00
|
|
|
date=vals.get('date') and vals.get('date') or obj.date
|
|
|
|
vals['cycle_dialy_id']=self.get_cycle_diary(date)
|
2014-11-02 07:13:20 +00:00
|
|
|
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-11-21 16:11:57 +00:00
|
|
|
# copy list of nurse to cycle.dialy.line
|
|
|
|
levels={}
|
|
|
|
for line in obj.lines:
|
|
|
|
level=line.level_id
|
|
|
|
levels[level.id]=line.amount or 0
|
|
|
|
|
|
|
|
lines=[]
|
|
|
|
# cost's nurses
|
|
|
|
for nr in obj.nurses:
|
|
|
|
nurse=nr.nurse_id
|
|
|
|
level=nr.level_id
|
|
|
|
lines.append(('create',{
|
|
|
|
'cycle_id': obj.cycle_id.id,
|
|
|
|
'personal_id': nurse.id,
|
2014-11-23 15:39:41 +00:00
|
|
|
'level_id': nurse.level_id.id,
|
|
|
|
'rate': levels[level.id] or 0.0,
|
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
|
|
|
|
personal_total={}
|
|
|
|
for hd_case in obj.hd_cases:
|
|
|
|
personals=hd_case.personals
|
|
|
|
for ps in personals:
|
|
|
|
personal=ps.personal_id
|
|
|
|
base=personal.base
|
|
|
|
if not base:
|
|
|
|
base=cost_per_case
|
|
|
|
if not personal_total.get(personal.id):
|
|
|
|
personal_total[personal.id]={
|
|
|
|
'base': 0,
|
2014-11-23 15:39:41 +00:00
|
|
|
'level_id': personal.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
|
|
|
}
|
2014-11-23 15:39:41 +00:00
|
|
|
personal_total[personal.id]['base']=base
|
|
|
|
personal_total[personal.id]['qty']+=1
|
2014-11-21 16:11:57 +00:00
|
|
|
|
|
|
|
for doctor_id, value in personal_total.items():
|
|
|
|
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,
|
|
|
|
'personal_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,
|
|
|
|
}))
|
|
|
|
|
|
|
|
cycle_dialy=obj.cycle_dialy_id
|
|
|
|
# clear cost line
|
|
|
|
for line in cycle_dialy.lines:
|
|
|
|
cycle=line.cycle_id
|
|
|
|
if obj.cycle_id.id==cycle.id: # only own cycle
|
|
|
|
line.delete()
|
|
|
|
|
2014-11-23 13:44:25 +00:00
|
|
|
# group personal and cycle date
|
|
|
|
glines={}
|
|
|
|
for line in lines:
|
|
|
|
mode,vals=line
|
|
|
|
cycle_id=vals['cycle_id']
|
|
|
|
personal_id=vals['personal_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
|
2014-11-23 13:44:25 +00:00
|
|
|
key=(cycle_id,personal_id)
|
|
|
|
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,personal_id=key
|
|
|
|
line={
|
|
|
|
'cycle_id': cycle_id,
|
|
|
|
'personal_id': personal_id,
|
|
|
|
}
|
|
|
|
line.update(vals)
|
|
|
|
lines.append(('create',line))
|
|
|
|
|
2014-11-21 16:11:57 +00:00
|
|
|
cycle_dialy.write({
|
|
|
|
'lines': lines,
|
|
|
|
})
|
|
|
|
|
2014-11-03 00:21:37 +00:00
|
|
|
obj.write({
|
|
|
|
'state': 'done',
|
|
|
|
})
|
|
|
|
|
2014-11-21 16:11:57 +00:00
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'clinic_cycle_dialy',
|
|
|
|
'mode': 'form',
|
|
|
|
'active_id': cycle_dialy.id,
|
|
|
|
},
|
|
|
|
'flash': 'Cycle Item has been confirmed, please see detail of cost below.',
|
|
|
|
}
|
|
|
|
|
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 compute(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
if not obj.manual:
|
|
|
|
for line in obj.lines:
|
|
|
|
line.delete()
|
|
|
|
|
|
|
|
levels={}
|
|
|
|
for level_id in get_model('clinic.personal.level').search([['type','=','nurse']]):
|
|
|
|
vals={
|
|
|
|
level_id: {
|
|
|
|
'total': 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
levels.update(vals)
|
|
|
|
for nurse in obj.nurses:
|
|
|
|
level=nurse.level_id
|
|
|
|
levels[level.id]['total']+=1
|
|
|
|
|
|
|
|
st_levels={}
|
|
|
|
st=get_model("clinic.setting").browse(1)
|
|
|
|
for line in st.levels:
|
|
|
|
level=line.level_id
|
|
|
|
st_levels[level.id]={
|
|
|
|
'var_a': line.var_a or 0,
|
|
|
|
'var_b': line.var_b or 0,
|
|
|
|
'op': line.op or "",
|
|
|
|
'formular': line.formular or "",
|
|
|
|
}
|
|
|
|
|
|
|
|
lines=[]
|
|
|
|
print("="*20)
|
|
|
|
total_a,total_b=0,0
|
|
|
|
for level_id, value in levels.items():
|
|
|
|
qty=value['total']
|
|
|
|
st_level=st_levels.get(level_id,None)
|
|
|
|
a,b=0,0
|
|
|
|
state="fail"
|
|
|
|
if st_level:
|
|
|
|
state="success"
|
|
|
|
var_a=float(st_level['var_a'] or "0")
|
|
|
|
var_b=float(st_level['var_b'] or "0")
|
|
|
|
fml_org=st_level['formular']
|
|
|
|
a=qty*var_a
|
|
|
|
a_str='%sX'%(a)
|
|
|
|
total_a+=a
|
|
|
|
|
|
|
|
op=st_level['op']
|
|
|
|
sign=op=='-' and -1 or 1
|
|
|
|
op_str=sign==1 and op or ""
|
|
|
|
|
|
|
|
b=(qty*var_b)*sign
|
|
|
|
b_str=(str(b) if b else "")
|
|
|
|
total_b+=b
|
|
|
|
|
|
|
|
fml=''.join([a_str, op_str, b_str])
|
|
|
|
lines.append(('create',{
|
|
|
|
'level_id': level_id,
|
|
|
|
'var_a': a,
|
|
|
|
'var_b': b,
|
|
|
|
'qty': qty,
|
|
|
|
'formular_org': fml_org,
|
|
|
|
'formular': fml if qty else "0",
|
|
|
|
'state': state,
|
|
|
|
}))
|
|
|
|
|
|
|
|
if not obj.manual:
|
|
|
|
var_pt=len(obj.hd_cases)
|
|
|
|
var_ptx=var_pt*(obj.var_k or 0)
|
|
|
|
total_bstr=total_b < 0 and "+%s"%(abs(total_b)) or "-%s"%total_b
|
2014-11-23 15:39:41 +00:00
|
|
|
var_x=0
|
|
|
|
if total_a:
|
|
|
|
var_x=eval('(%s%s)/%s'%(var_ptx,total_bstr,total_a))
|
2014-11-21 08:34:30 +00:00
|
|
|
for line in lines:
|
|
|
|
vals=line[1]
|
|
|
|
fml=vals['formular'].replace("X","*%s")
|
|
|
|
vals['amount']=0
|
|
|
|
if vals['var_a'] or vals['var_b']:
|
|
|
|
vals['amount']=eval(fml%var_x)
|
|
|
|
else:
|
|
|
|
# update qty (a)
|
|
|
|
lines=[]
|
|
|
|
for line in obj.lines:
|
|
|
|
level=line.level_id
|
|
|
|
qty=line.qty or 0
|
|
|
|
st_level=st_levels.get(level.id,None)
|
|
|
|
a,b=0,0
|
|
|
|
state="fail"
|
|
|
|
if st_level:
|
|
|
|
state="success"
|
|
|
|
var_a=float(st_level['var_a'] or "0")
|
|
|
|
var_b=float(st_level['var_b'] or "0")
|
|
|
|
fml_org=st_level['formular']
|
|
|
|
a=qty*var_a
|
|
|
|
a_str='%sX'%(a)
|
|
|
|
|
|
|
|
op=st_level['op']
|
|
|
|
sign=op=='-' and -1 or 1
|
|
|
|
op_str=sign==1 and op or ""
|
|
|
|
|
|
|
|
b=(qty*var_b)*sign
|
|
|
|
b_str=(str(b) if b else "")
|
|
|
|
else:
|
|
|
|
print("not found in setting ", level.id)
|
|
|
|
|
|
|
|
fml=''.join([a_str, op_str, b_str])
|
|
|
|
lines.append(('write',[line.id],{
|
|
|
|
'level_id': level.id,
|
|
|
|
'var_a': a,
|
|
|
|
'var_b': b,
|
|
|
|
'qty': qty,
|
|
|
|
'formular': fml if qty else "0",
|
|
|
|
'formular_org': fml_org,
|
|
|
|
'state': state,
|
|
|
|
}))
|
|
|
|
|
|
|
|
obj.write({
|
|
|
|
'lines': lines,
|
|
|
|
})
|
|
|
|
|
|
|
|
if obj.manual:
|
|
|
|
for line in obj.lines:
|
|
|
|
fml=(line.formular or "").replace("X","*%s")
|
|
|
|
if line.var_a or line.var_b:
|
|
|
|
amt=eval(fml%obj.var_x)
|
|
|
|
else:
|
|
|
|
amt=0
|
|
|
|
line.write({
|
|
|
|
'amount': amt,
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'clinic_cycle_item',
|
|
|
|
'mode': 'form',
|
|
|
|
'active_id': obj.id,
|
|
|
|
},
|
|
|
|
'flash':'Compute successfully',
|
|
|
|
}
|
|
|
|
|
|
|
|
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.personal').browse(nurse_id)
|
|
|
|
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-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()
|