'use strict'; /** * * * @author Mai * @date * @version */ const billsPosConvertModel = (function () { // 需要汇总计算的字段 const tpFields = ['total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp', 'end_contract_tp', 'end_qc_tp']; const baseCalcFields = ['quantity', 'contract_qty', 'qc_qty', 'gather_qty', 'pre_contract_qty', 'pre_qc_qty', 'pre_gather_qty', 'end_contract_qty', 'end_qc_qty', 'end_gather_qty']; // 基础数据 const bpcTreeSetting = { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, keys: ['id', 'tender_id', 'ledger_id'], stageId: 'id', updateFields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'] }; const bpcTree = createNewPathTree('stage', bpcTreeSetting); const bpcPosSetting = { id: 'id', ledgerId: 'lid', updateFields: ['contract_qty', 'qc_qty'], }; const bpcPos = new StagePosData(bpcPosSetting); let bpcChange, tpDecimal; // 结果 const resultTreeSetting = { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, fullPath: 'full_path', }; const resultTree = createNewPathTree('filter', resultTreeSetting); // 加载基础数据 function loadLedgerData (ledger) { // 加载树结构数据 bpcTree.loadDatas(ledger); } function loadPosData(pos) { bpcPos.loadDatas(pos); } function loadData(ledger, pos, change, decimal) { loadLedgerData(ledger); loadPosData(pos); bpcChange = change; tpDecimal = decimal; } function convertXmj(data) { const xmj = resultTree.addData(data, ['ledger_id', 'ledger_pid', 'order', 'level', 'tender_id', 'full_path', 'code', 'name', 'unit', 'dgn_qty1', 'dgn_qty2', 'drawing_code', 'postil', 'memo']); return xmj; } // v1 function loadField(node, data, fields) { for (const prop in data) { if (fields.indexOf(prop) >= 0) node[prop] = data[prop]; } } // v2 function loadCalcFields(node, data) { node.quantity = ZhCalc.add(node.quantity, data.quantity); node.total_price = ZhCalc.add(node.total_price, ZhCalc.mul(node.unit_price, data.quantity, tpDecimal)); node.contract_qty = ZhCalc.add(node.contract_qty, data.contract_qty); node.contract_tp = ZhCalc.add(node.contract_tp, ZhCalc.mul(node.unit_price, data.contract_qty, tpDecimal)); node.qc_qty = ZhCalc.add(node.qc_qty, data.qc_qty); node.qc_tp = ZhCalc.add(node.qc_tp, ZhCalc.mul(node.unit_price, data.qc_qty, tpDecimal)); node.gather_qty = ZhCalc.add(node.gather_qty, data.gather_qty); node.pre_contract_qty = ZhCalc.add(node.pre_contract_qty, data.pre_contract_qty); node.pre_contract_tp = ZhCalc.add(node.pre_contract_tp, ZhCalc.mul(node.unit_price, data.pre_contract_qty, tpDecimal)); node.pre_qc_qty = ZhCalc.add(node.pre_qc_qty, data.pre_qc_qty); node.pre_qc_tp = ZhCalc.add(node.pre_qc_tp, ZhCalc.mul(node.unit_price, data.pre_qc_qty, tpDecimal)); node.pre_gather_qty = ZhCalc.add(node.pre_gather_qty, data.pre_gather_qty); node.end_contract_qty = ZhCalc.add(node.end_contract_qty, data.end_contract_qty); node.end_contract_tp = ZhCalc.add(node.end_contract_tp, ZhCalc.mul(node.unit_price, data.end_contract_qty, tpDecimal)); node.end_qc_qty = ZhCalc.add(node.end_qc_qty, data.end_qc_qty); node.end_qc_tp = ZhCalc.add(node.end_qc_tp, ZhCalc.mul(node.unit_price, data.end_qc_qty, tpDecimal)); node.end_gather_qty = ZhCalc.add(node.end_gather_qty, data.end_gather_qty); } function convertGcl(node, xmj) { if (!xmj) return; if (!xmj.unitTree) { xmj.unitTree = createNewPathTree('gather', resultTreeSetting); } const pos = bpcPos.getLedgerPos(node.id); if (pos && pos.length > 0) { for (const p of pos) { const posUnit = xmj.unitTree.addNode({pos_name: p.name, b_code: null, name: null, unit: null, unit_price: null}); const gclUnit = xmj.unitTree.addNode({pos_name: null, b_code: node.b_code, name: node.name, unit: node.unit, unit_price: node.unit_price }, posUnit); //loadField(gclUnit, p, baseCalcFields); loadCalcFields(gclUnit, p); if (!gclUnit.changes) gclUnit.changes = []; for (const c of bpcChange) { if (c.lid === node.id && c.pid === p.id && c.qty && c.qty !== 0) { gclUnit.changes.push(c); } } } } else { const unit = xmj.unitTree.addNode({pos_name: null, b_code: node.b_code, name: node.name, unit: node.unit, unit_price: node.unit_price}); //loadField(unit, node, baseCalcFields); loadCalcFields(unit, node); if (!unit.changes) unit.changes = []; for (const c of bpcChange) { if (c.lid === node.id && c.pid == -1 && c.qty && c.qty !== 0) { unit.changes.push(c); } } } } function recursiveConvertNode(nodes, xmj = null) { if (!nodes || !nodes instanceof Array || nodes.length === 0) return; for (const node of nodes) { if (node.b_code && node.b_code !== '') { if (!node.children || node.children.length === 0) { convertGcl(node, xmj); } recursiveConvertNode(node.children, xmj); } else { recursiveConvertNode(node.children, convertXmj(node)); } } } function calculateChild(child) { child.gather_qty = ZhCalc.add(child.contract_qty, child.qc_qty); child.pre_gather_qty = ZhCalc.add(child.pre_contract_qty, child.pre_gather_qty); child.end_contract_qty = ZhCalc.add(child.contract_qty, child.pre_contract_qty); child.end_qc_qty = ZhCalc.add(child.qc_qty, child.pre_qc_qty); child.end_gather_qty = ZhCalc.add(child.gather_qty, child.pre_gather_qty); // v1 // child.total_price = ZhCalc.mul(child.unit_price, child.quantity, tpDecimal); // child.contract_tp = ZhCalc.mul(child.unit_price, child.contract_qty, tpDecimal); // child.qc_tp = ZhCalc.mul(child.unit_price, child.qc_qty, tpDecimal); // child.gather_tp = ZhCalc.mul(child.unit_price, child.gather_qty, tpDecimal); // child.pre_contract_tp = ZhCalc.mul(child.unit_price, child.pre_contract_qty, tpDecimal); // child.pre_qc_tp = ZhCalc.mul(child.unit_price, child.pre_qc_qty, tpDecimal); // child.pre_gather_tp = ZhCalc.mul(child.unit_price, child.pre_gather_qty, tpDecimal); // child.end_contract_tp = ZhCalc.mul(child.unit_price, child.end_contract_qty, tpDecimal); // child.end_qc_tp = ZhCalc.mul(child.unit_price, child.end_qc_qty, tpDecimal); // child.end_gather_tp = ZhCalc.mul(child.unit_price, child.end_gather_qty, tpDecimal); // v2 child.gather_tp = ZhCalc.add(child.contract_tp, child.qc_tp); child.pre_gather_tp = ZhCalc.add(child.pre_contract_tp, child.pre_qc_tp); child.end_gather_tp = ZhCalc.add(child.end_contract_tp, child.end_qc_tp); child.final_tp = ZhCalc.add(child.total_price, child.end_qc_tp); child.end_gather_percent = ZhCalc.mul(ZhCalc.div(child.end_gather_tp, child.final_tp, 4), 100); child.bgl_code = _.uniq(_.map(child.changes, 'c_code')).join(';'); } function calculateNode(node, children) { for (const child of children) { node.total_price = ZhCalc.add(node.total_price, child.total_price); node.contract_tp = ZhCalc.add(node.contract_tp, child.contract_tp); node.qc_tp = ZhCalc.add(node.qc_tp, child.qc_tp); node.gather_tp = ZhCalc.add(node.gather_tp, child.gather_tp); node.pre_contract_tp = ZhCalc.add(node.pre_contract_tp, child.pre_contract_tp); node.pre_qc_tp = ZhCalc.add(node.pre_qc_tp, child.pre_qc_tp); node.pre_gather_tp = ZhCalc.add(node.pre_gather_tp, child.pre_gather_tp); node.end_contract_tp = ZhCalc.add(node.end_contract_tp, child.end_contract_tp); node.end_qc_tp = ZhCalc.add(node.end_qc_tp, child.end_qc_tp); node.end_gather_tp = ZhCalc.add(node.end_gather_tp, child.end_gather_tp); } node.final_tp = ZhCalc.add(node.total_price, node.end_qc_tp); node.end_gather_percent = ZhCalc.mul(ZhCalc.div(node.end_gather_tp, node.final_tp, 4), 100); } function recursiveCalcUnitNodes(nodes) { if (!nodes || !nodes instanceof Array || nodes.length === 0) return; for (const node of nodes) { recursiveCalcUnitNodes(node.children); if (node.children && node.children.length > 0) { calculateNode(node, node.children); } else { calculateChild(node); } } } function recursiveCalculateAndSort(nodes) { if (!nodes || !nodes instanceof Array || nodes.length === 0) return; for (const node of nodes) { recursiveCalculateAndSort(node.children); if (node.unitTree) { node.unitTree.sortTreeNodeCustom('b_code', function (a, b) { function compareCode(code1, code2) { if (numReg.test(code1)) { if (numReg.test(code2)) { return _.toNumber(code1) - _.toNumber(code2); } else { return -1 } } else { if (numReg.test(code2)) { return 1; } else { return code1 === code2 ? 0 : (code1 < code2 ? -1 : 1); } } } const numReg = /^[0-9]+$/; const aCodes = a ? a.split('-') : [], bCodes = b ? b.split('-') : []; for (let i = 0, iLength = Math.min(aCodes.length, bCodes.length); i < iLength; ++i) { const iCompare = compareCode(aCodes[i], bCodes[i]); if (iCompare !== 0) { return iCompare; } } }, false); recursiveCalcUnitNodes(node.unitTree.children); calculateNode(node, node.unitTree.children, tpFields); } else if (node.children && node.children.length > 0) { calculateNode(node, node.children, tpFields); } } } function CalculateAndSortResult() { resultTree.sortTreeNode(); recursiveCalculateAndSort(resultTree.children); } // 转换数据 function convert() { recursiveConvertNode(bpcTree.children); CalculateAndSortResult(); return resultTree; } return { loadData, convert } })();