From c06b7fa06ca4396c64ae30381d35b2b5a7767599 Mon Sep 17 00:00:00 2001 From: "watcha.h" Date: Fri, 21 Nov 2014 15:34:30 +0700 Subject: [PATCH] compute cost --- .../layouts/clinic_cycle_item_form.xml | 21 +- netforce_clinic/models/cycle_item.py | 188 ++++++++++++++++-- netforce_clinic/models/cycle_item_line.py | 6 +- netforce_clinic/models/setting_level.py | 2 +- 4 files changed, 198 insertions(+), 19 deletions(-) diff --git a/netforce_clinic/layouts/clinic_cycle_item_form.xml b/netforce_clinic/layouts/clinic_cycle_item_form.xml index 7eab044..7ef03c0 100644 --- a/netforce_clinic/layouts/clinic_cycle_item_form.xml +++ b/netforce_clinic/layouts/clinic_cycle_item_form.xml @@ -2,6 +2,7 @@ @@ -11,7 +12,7 @@ - + @@ -20,15 +21,27 @@ - - + - + + + + + + + + + + + + + + diff --git a/netforce_clinic/models/cycle_item.py b/netforce_clinic/models/cycle_item.py index f988047..5e5c6ea 100644 --- a/netforce_clinic/models/cycle_item.py +++ b/netforce_clinic/models/cycle_item.py @@ -2,6 +2,7 @@ import time from netforce.model import Model, fields, get_model from netforce.access import get_active_company +from netforce.utils import get_data_path class CycleItem(Model): _name="clinic.cycle.item" @@ -12,21 +13,31 @@ class CycleItem(Model): res={} for obj in self.browse(ids): name="%s-%s"%(obj.cycle_id.name,obj.date) + total_case=len(obj.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 + 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)) res[obj.id]={ 'name': name, + '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 - def _get_pt(self,ids,context={}): - res={} - for obj in self.browse(ids): - total_case=len(obj.hd_cases) - res[obj.id]={ - 'var_pt': total_case, - 'var_ptx': total_case*(obj.var_k or 0) - } - return res - _fields={ 'name': fields.Char("Name", function="_get_all",function_multi=True), 'date': fields.Date("Date",search=True), @@ -40,9 +51,14 @@ class CycleItem(Model): 'company_id': fields.Many2One("company", "Company"), "state": fields.Selection([("draft","Draft"),("done","Done")],"Status",required=True), 'var_k': fields.Float("K"), - 'var_x': fields.Float("X"), - 'var_pt': fields.Integer("PT", function="_get_pt",function_multi=True), - 'var_ptx': fields.Float("PT x K", function="_get_pt",function_multi=True), + '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("FML1:",function="_get_all",function_multi=True), + 'var_fml2': fields.Char("FML2:",function="_get_all",function_multi=True), + 'var_fml3': fields.Char("FML3:",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"), } def _get_vark(self,context={}): @@ -106,5 +122,151 @@ class CycleItem(Model): obj.write({ 'state': 'draft', }) + + 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 + var_x=eval('(%s%s)/%s'%(var_ptx,total_bstr,total_a)) + 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 CycleItem.register() diff --git a/netforce_clinic/models/cycle_item_line.py b/netforce_clinic/models/cycle_item_line.py index c741654..fb407e5 100644 --- a/netforce_clinic/models/cycle_item_line.py +++ b/netforce_clinic/models/cycle_item_line.py @@ -16,11 +16,15 @@ class CycleItemLine(Model): _fields={ 'item_id': fields.Many2One("clinic.cycle.item", "Cycle Item"), 'level_id': fields.Many2One("clinic.personal.level", "Level",domain=[['type','=','nurse']]), - 'formular': fields.Char("Formular", size=100), 'qty': fields.Integer("Qty"), + 'var_a': fields.Float("A"), + 'var_b': fields.Float("B"), + 'formular_org': fields.Char("Formular Original", size=100), + 'formular': fields.Char("Formular", size=100), 'rate': fields.Float("Rate"), 'amount': fields.Float("Amount"), 'company_id': fields.Many2One('company','Company'), + "state": fields.Selection([["fail","Fail"],["success","Success"]],"Status",), } _defaults={ diff --git a/netforce_clinic/models/setting_level.py b/netforce_clinic/models/setting_level.py index 82af751..e817c60 100644 --- a/netforce_clinic/models/setting_level.py +++ b/netforce_clinic/models/setting_level.py @@ -7,7 +7,7 @@ class SettingLevel(Model): def _get_formular(self,ids,context={}): res={} for obj in self.browse(ids): - res[obj.id]=''.join(['%sx'%obj.var_a or '', obj.op or '', obj.var_b or '']) + res[obj.id]=''.join(['%sX'%obj.var_a or '', obj.op or '', obj.var_b or '']) return res _fields={