/** * Created by Mai on 2017/7/5. */ const rationContent = 0, rationPrice = 1, rationPriceConverse = 2, billsPrice = 3; const sumTotalFeeFlag = 0, totalFeeFlag = 1; const rationContentUnitFeeFlag = 0, averageQtyUnitFeeFlag = 1, billsPriceUnitFeeFlag = 2; let rationContentCalcFields = [ {'type': 'common', 'unitFeeFlag': rationContentUnitFeeFlag, 'totalFeeFlag': totalFeeFlag}, {'type': 'labour', 'unitFeeFlag': rationContentUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag}, {'type': 'material', 'unitFeeFlag': rationContentUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag}, {'type': 'machine', 'unitFeeFlag': rationContentUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag}, ]; let rationPriceCalcFields = [ {'type': 'common', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': totalFeeFlag}, {'type': 'labour', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag}, {'type': 'material', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag}, {'type': 'machine', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag}, ]; let rationPriceConverseCalcFields = [ {'type': 'common', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag}, {'type': 'labour', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag}, {'type': 'material', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag}, {'type': 'machine', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag}, ]; let billsPriceCalcFields = [ {'type': 'common', 'unitFeeFlag': billsPriceUnitFeeFlag, 'totalFeeFlag': totalFeeFlag}, {'type': 'labour', 'unitFeeFlag': billsPriceUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag}, {'type': 'material', 'unitFeeFlag': billsPriceUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag}, {'type': 'machine', 'unitFeeFlag': billsPriceUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag}, ]; let nodeCalcObj = { node: null, digit: 2, field: null, getFee: calcFees.getFee, sumTotalFee: function() { let result = 0, child; for (child of this.node.children) { result += this.getFee(child.data, this.field.totalFee); } return result; }, averageQty: function() { let result = 0, child, qty; result = this.sumTotalFee(this.field); qty = this.getFee(this.node.data, 'quantity'); if (qty !== 0) { result = result / qty; } return result; }, totalFee: function () { return this.getFee(this.node.data, this.field.unitFee) * this.getFee(this.node.data, 'quantity'); }, rationContentUnitFee: function () { let result = 0, child, qty = this.getFee(this.node.data, 'quantity'); if (qty === 0) { qty = 1; } for (child of this.node.children) { result += (this.getFee(child.data, this.field.unitFee) * this.getFee(child.data, 'quantity') / qty).toDecimal(this.digit); } return result; } }; class BillsCalc { constructor (project, CalcFlag) { this.project = project; this.CalcFlag = CalcFlag; this.digit = 2; switch (this.CalcFlag) { case rationContent: this.calcFieldName = rationContentCalcFields; break; case rationPrice: this.calcFieldName = rationPriceCalcFields; break; case rationPriceConverse: this.calcFieldName = rationPriceConverseCalcFields; break; case billsPrice: this.calcFieldName = billsPriceCalcFields; break; default: this.calcFieldName = []; } this.InitFields(this.calcFieldName); }; calcLeaf (node, fields) { nodeCalcObj.node = node; nodeCalcObj.digit = this.digit; calcFees.checkFields(node.data, fields); let nodeCalc = nodeCalcObj, virData= null; // 清单单价:套用定额计算程序 if (this.CalcFlag === billsPrice) { let rations = this.project.Ration.getBillsSortRation(node.source.getID()); rationCalcObj.calcGljs = this.project.ration_glj.getGatherGljArrByRations(rations); rationCalcObj.calcFields = rationCalcFields; virData = rationCalcObj.calculate(); } for (let field of fields) { nodeCalcObj.field = field; switch (field.unitFeeFlag) { case rationContentUnitFeeFlag: node.data.feesIndex[field.type].unitFee = nodeCalcObj.rationContentUnitFee().toDecimal(this.digit); break; case averageQtyUnitFeeFlag: node.data.feesIndex[field.type].unitFee = nodeCalcObj.averageQty().toDecimal(this.digit); break; case billsPriceUnitFeeFlag: node.data.feesIndex[field.type].unitFee = virData[field.type]; break; default: node.data.feesIndex[field.type].unitFee = 0; } switch (field.totalFeeFlag) { case sumTotalFeeFlag: node.data.feesIndex[field.type].totalFee = nodeCalcObj.sumTotalFee().toDecimal(this.digit); break; case totalFeeFlag: node.data.feesIndex[field.type].totalFee = nodeCalcObj.totalFee().toDecimal(this.digit); break; default: node.data.feesIndex[field.type].totalFee = 0; } } }; calcParent (node, fields) { nodeCalcObj.node = node; calcFees.checkFields(node.data, fields); for (let field of fields) { nodeCalcObj.field = field; node.data.feesIndex[field.type].totalFee = nodeCalcObj.sumTotalFee().toDecimal(this.digit); } }; calcNodes (nodes) { for (let node of nodes) { if (node.sourceType !== this.project.Bills.getSourceType()) { return; } if (node.source.children.length > 0) { this.calcNodes(node.children); this.calcParent(node, this.calcFieldName); } else { this.calcLeaf(node, this.calcFieldName); } } }; calcAll () { this.calcNodes(this.project.mainTree.roots); }; InitFields (fields) { for (let field of fields) { if (field.unitFee) return; field.unitFee = 'feesIndex.' + field.type + '.unitFee'; field.unitFeeSplit = field.unitFee.split('.'); field.totalFee = 'feesIndex.' + field.type + '.totalFee'; field.totalFeeSplit = field.totalFee.split('.'); field.tenderUnitFee = 'feesIndex.'+ field.type + '.tenderUnitFee'; field.tenderUnitFeeSplit = field.tenderUnitFee.split('.'); field.tenderTotalFee = 'feesIndex.' + field.type + '.tenderTotalFee'; field.tenderTotalFeeSplit = field.tenderTotalFee.split('.'); } } }