clinic/netforce_clinic/models/labor_cost.py

199 lines
6.9 KiB
Python
Raw Normal View History

2014-12-08 11:35:39 +00:00
from netforce.model import Model, fields, get_model
class LaborCost(Model):
_name="clinic.labor.cost"
_string="Labor Cost"
_name_field="cycle_item_id"
_key=['cycle_item_id']
def _get_all(self,ids,context={}):
res={}
for obj in self.browse(ids):
hd_cases=obj.cycle_item_id.hd_cases or []
total_case=len(hd_cases)
total_a,total_b=0,0
total=0.0
for line in obj.lines:
total+=line.amount or 0
total_a+=line.var_a or 0
total_b+=line.var_b or 0
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))
res[obj.id]={
'var_pt': total_case,
'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,
}
return res
_fields={
"cycle_item_id": fields.Many2One("clinic.cycle.item","Cycle Item",required=True),
'var_k': fields.Float("K"),
'var_pt': fields.Integer("PT", function="_get_all",function_multi=True),
'var_ptx': fields.Char("PT x K:", function="_get_all",function_multi=True),
'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),
'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"),
"formulars": fields.One2Many("clinic.labor.cost.formular", "labor_cost_id", "Formulars"),
"staffs": fields.One2Many("clinic.labor.cost.staff", "labor_cost_id", "Staffs"),
"lines": fields.One2Many("clinic.labor.cost.line", "labor_cost_id", "Lines"),
}
def compute(self,ids,context={}):
obj=self.browse(ids)[0]
if not obj.manual:
for fline in obj.formulars:
fline.delete()
levels={}
for level_id in get_model('clinic.staff.level').search([['type','=','nurse']]):
vals={
level_id: {
'total': 0,
}
}
levels.update(vals)
item=obj.cycle_item_id
for ns in item.nurses:
level=ns.level_id
if not level:
raise Exception("Please specify level for %s"%ns.nurse_id.name)
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 "",
}
formulars=[]
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
else:
# XXX prevent to show all level if not configuration from setting
continue
fml=''.join([a_str, op_str, b_str])
formulars.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(item.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
var_x=0
if total_a:
var_x=eval('(%s%s)/%s'%(var_ptx,total_bstr,total_a))
for fline in formulars:
vals=fline[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)
formulars=[]
for fline in obj.formulars:
level=fline.level_id
qty=fline.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])
formulars.append(('write',[fline.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({
'formulars': formulars,
})
if obj.manual:
for fline in obj.formulars:
fml=(fline.formular or "").replace("X","*%s")
if fline.var_a or fline.var_b:
amt=eval(fml%obj.var_x)
else:
amt=0
fline.write({
'amount': amt,
})
return {
'next': {
'name': 'clinic_labor_cost',
'mode': 'form',
'active_id': obj.id,
},
'flash':'Compute successfully',
}
LaborCost.register()