| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 | 
							- 'use strict';
 
- /**
 
-  *
 
-  *
 
-  * @author Mai
 
-  * @date
 
-  * @version
 
-  */
 
- const Ledger = require('../lib/ledger');
 
- const auditConst = require('../const/audit');
 
- class loadGclBaseTree {
 
-     /**
 
-      * 构造函数
 
-      * @param {Array} tempData - 清单模板数据
 
-      */
 
-     constructor (ctx, setting) {
 
-         this.ctx = ctx;
 
-         this.parent = setting.parent;
 
-         this.parent.children = [];
 
-         this.defaultData = setting.defaultData;
 
-         // 常量
 
-         this.splitChar = '-';
 
-         // 索引
 
-         // 仅通过addNode或addNodeWithoutParent添加节点
 
-         this.items = [];
 
-         // this.parent下原来的节点
 
-         this.baseNodes = [];
 
-         this.ignoreParent = setting.ignoreParent;
 
-         // 缓存
 
-         this.keyNodeId = setting.maxId ? setting.maxId + 1 : 1;
 
-     }
 
-     /**
 
-      * 根据 编号 查找 父项项目节
 
-      * @param {String} code - 子项编号
 
-      * @returns {*}
 
-      */
 
-     findNode(node, parent, check) {
 
-         parent = parent || this.parent;
 
-         if (!parent.children) return null;
 
-         for (const child of parent.children) {
 
-             const checkLeaf = (child.is_leaf && node.is_leaf) || (!child.is_leaf && !node.is_leaf);
 
-             if (child.b_code === node.b_code && child.name === node.name && child.unit === node.unit
 
-                 && checkLeaf && !child.hasPos && (!check || check(child, node))) {
 
-                 return child;
 
-             }
 
-         }
 
-         return null;
 
-     }
 
-     /**
 
-      * 添加 树节点 并完善该节点的树结构
 
-      * @param {Object} node - 添加节点
 
-      * @param {Object} parent - 父项
 
-      * @returns {*}
 
-      */
 
-     addNode(source, parent, check) {
 
-         parent = parent ? parent : this.parent;
 
-         let node = this.findNode(source, parent, check);
 
-         if (!node) {
 
-             if (!parent.children) parent.children = [];
 
-             node = {
 
-                 id: this.ctx.app.uuid.v4(),
 
-                 tender_id: this.ctx.tender.id,
 
-                 ledger_id: this.keyNodeId,
 
-                 ledger_pid: parent.ledger_id,
 
-                 level: parent.level +1,
 
-                 full_path: parent.full_path + '-' + this.keyNodeId,
 
-                 order: parent.children.length + 1,
 
-                 children: [],
 
-                 b_code: source.b_code,
 
-                 name: source.name,
 
-                 unit: source.unit,
 
-                 deal_qty: 0,
 
-                 sgfh_qty: 0,
 
-                 qtcl_qty: 0,
 
-                 sjcl_qyt: 0,
 
-                 quantity: 0,
 
-                 is_leaf: source.is_leaf,
 
-                 hasPos: false,
 
-             };
 
-             this.keyNodeId += 1;
 
-             parent.children.push(node);
 
-             this.items.push(node);
 
-         }
 
-         return node;
 
-     }
 
-     findLeaf(node, check) {
 
-         for (const bn of this.baseNodes) {
 
-             if (bn.b_code === node.b_code && bn.name === node.name && bn.unit === node.unit
 
-                 && bn.is_leaf && !bn.hasPos && (!check || check(bn, node))) return bn;
 
-         }
 
-         for (const i of this.items) {
 
-             if (i.b_code === node.b_code && i.name === node.name && i.unit === node.unit
 
-                 && i.is_leaf && !i.hasPos && (!check || check(i, node))) return i;
 
-         }
 
-         return null;
 
-     }
 
-     addNodeWithoutParent(source, check) {
 
-         let node = this.findLeaf(source, check);
 
-         if (!node) {
 
-             node = {
 
-                 id: this.ctx.app.uuid.v4(),
 
-                 tender_id: this.ctx.tender.id,
 
-                 ledger_id: this.keyNodeId,
 
-                 ledger_pid: this.parent.ledger_id,
 
-                 level: this.parent.level + 1,
 
-                 full_path: this.parent.full_path + '-' + this.keyNodeId,
 
-                 order: this.parent.children.length + 1,
 
-                 b_code: source.b_code,
 
-                 name: source.name,
 
-                 unit: source.unit,
 
-                 deal_qty: 0,
 
-                 sgfh_qty: 0,
 
-                 qtcl_qty: 0,
 
-                 sjcl_qty: 0,
 
-                 quantity: 0,
 
-                 is_leaf: source.is_leaf,
 
-                 hasPos: false,
 
-             };
 
-             this.keyNodeId += 1;
 
-             this.parent.children.push(node);
 
-             this.items.push(node);
 
-         }
 
-         return node;
 
-     }
 
-     gather(source, parent) {}
 
-     getUpdateData() {}
 
-     resortChildrenByCode(node) {
 
-         const helper = this.ctx.helper;
 
-         if (!node.children || node.children.length === 0) return;
 
-         node.children.sort((x, y) => {
 
-             return helper.compareCode(x.b_code, y.b_code);
 
-         });
 
-         for (const [i, c] of node.children.entries()) {
 
-             this.resortChildrenByCode(c);
 
-             c.order = i + 1;
 
-         }
 
-     }
 
-     resortByCode() {
 
-         this.resortChildrenByCode(this.parent);
 
-     }
 
-     recursiveCalculate(dealBills, node) {
 
-         if (node.children && node.children.length > 0) {
 
-             for (const child of node.children) {
 
-                 this.recursiveCalculate(dealBills, child);
 
-             }
 
-         } else {
 
-             const p = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
 
-             node.deal_qty = this.ctx.helper.round(node.deal_qty, p.value);
 
-             node.sgfh_qty = this.ctx.helper.round(node.sgfh_qty, p.value);
 
-             node.sjcl_qty = this.ctx.helper.round(node.sjcl_qty, p.value);
 
-             node.qtcl_qty = this.ctx.helper.round(node.qtcl_qty, p.value);
 
-             node.quantity = this.ctx.helper.round(node.quantity, p.value);
 
-             if (!node.unit_price) {
 
-                 const db = dealBills.find(x => { return x.code === node.b_code && x.name === node.name && x.unit === node.unit });
 
-                 if (!db) return;
 
-                 node.unit_price = db.unit_price;
 
-             }
 
-             node.deal_tp = this.ctx.helper.mul(node.deal_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
 
-             node.sgfh_tp = this.ctx.helper.mul(node.sgfh_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
 
-             node.sjcl_tp = this.ctx.helper.mul(node.sjcl_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
 
-             node.qtcl_tp = this.ctx.helper.mul(node.qtcl_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
 
-             node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
 
-         }
 
-     }
 
-     calculateAll(dealBills) {
 
-         this.recursiveCalculate(dealBills, this.parent);
 
-     }
 
- }
 
- class loadLedgerGclTree extends loadGclBaseTree {
 
-     gather(source, parent) {
 
-         const node = this.addNode(source, parent);
 
-         node.deal_qty = this.ctx.helper.add(node.deal_qty, source.deal_qty);
 
-         node.sgfh_qty = this.ctx.helper.add(node.sgfh_qty, source.sgfh_qty);
 
-         node.qtcl_qty = this.ctx.helper.add(node.qtcl_qty, source.qtcl_qty);
 
-         node.sjcl_qty = this.ctx.helper.add(node.sjcl_qty, source.sjcl_qty);
 
-         node.quantity = this.ctx.helper.add(node.quantity, source.quantity);
 
-         return node;
 
-     }
 
-     getUpdateData() {
 
-         const update = [];
 
-         if (this.items.length > 0) update.push({id: this.parent.id, ledger_id: this.parent.ledger_id, is_leaf: 0});
 
-         const create = [];
 
-         for (const i of this.items) {
 
-             create.push({
 
-                 id: i.id, tender_id: i.tender_id, ledger_id: i.ledger_id, ledger_pid: i.ledger_pid,
 
-                 level: i.level, order: i.order, full_path: i.full_path, is_leaf: !i.children || i.children.length === 0,
 
-                 b_code: i.b_code, name: i.name, unit: i.unit, unit_price: i.unit_price || 0,
 
-                 deal_qty: i.deal_qty, deal_tp: i.deal_tp || 0,
 
-                 sgfh_qty: i.sgfh_qty, sjcl_qty: i.sjcl_qty, qtcl_qty: i.qtcl_qty, quantity: i.quantity,
 
-                 sgfh_tp: i.sgfh_tp || 0, sjcl_tp: i.sjcl_tp || 0, qtcl_tp: i.qtcl_tp || 0, total_price: i.total_price || 0,
 
-             });
 
-         }
 
-         return {update, create};
 
-     }
 
- }
 
- class updateReviseGclTree extends loadGclBaseTree {
 
-     constructor(ctx, setting) {
 
-         super(ctx, setting);
 
-         this.errors = [];
 
-     }
 
-     loadBase(datas, pos) {
 
-         datas.sort((x, y) => { return x.level === y.level ? x.order - y.order : x.level - y.level; });
 
-         const Index = {};
 
-         for (const d of datas) {
 
-             const parent = this.parent.ledger_id === d.ledger_pid ? this.parent : Index[d.ledger_pid];
 
-             if (!parent) continue;
 
-             if (!parent.children) parent.children = [];
 
-             const relaPos = pos.filter(x => {return x.lid === d.id});
 
-             const baseNode = {
 
-                 id: d.id,
 
-                 ledger_id: d.ledger_id,
 
-                 ledger_pid: d.ledger_pid,
 
-                 level: d.level,
 
-                 is_leaf: d.is_leaf,
 
-                 full_path: d.full_path,
 
-                 b_code: d.b_code,
 
-                 name: d.name,
 
-                 unit: d.unit,
 
-                 unit_price: d.unit_price,
 
-                 org_deal_qty: d.deal_qty || 0,
 
-                 org_sgfh_qty: d.sgfh_qty || 0,
 
-                 org_sjcl_qty: d.sjcl_qty || 0,
 
-                 org_qtcl_qty: d.qtcl_qty || 0,
 
-                 org_qty: d.quantity || 0,
 
-                 org_order: d.order,
 
-                 sgfh_qty: 0,
 
-                 sjcl_qty: 0,
 
-                 qtcl_qty: 0,
 
-                 quantity: 0,
 
-                 hasPos: relaPos.length > 0,
 
-             };
 
-             parent.children.push(baseNode);
 
-             Index[baseNode.ledger_id] = baseNode;
 
-             this.baseNodes.push(baseNode);
 
-         }
 
-     }
 
-     gather(source, parent) {
 
-         const node = this.addNode(source, parent);
 
-         node.deal_qty = this.ctx.helper.add(node.deal_qty, source.deal_qty);
 
-         node.sgfh_qty = this.ctx.helper.add(node.sgfh_qty, source.sgfh_qty);
 
-         node.qtcl_qty = this.ctx.helper.add(node.qtcl_qty, source.qtcl_qty);
 
-         node.sjcl_qty = this.ctx.helper.add(node.sjcl_qty, source.sjcl_qty);
 
-         node.quantity = this.ctx.helper.add(node.quantity, source.quantity);
 
-         return node;
 
-     }
 
-     getUpdateData() {
 
-         const result = {update: [], errors: [], create: []};
 
-         if (this.baseNodes.length === 0) {
 
-             if (this.items.length > 0) result.update.push({id: this.parent.id, ledger_id: this.parent.ledger_id, is_leaf: 0});
 
-         } else {
 
-             for (const bn of this.baseNodes) {
 
-                 if (bn.children && bn.children.length > 0) {
 
-                     if (bn.order !== bn.org_order) {
 
-                         result.update.push({
 
-                             id: bn.id, ledger_id: bn.ledger_id, order: bn.order,
 
-                         });
 
-                     }
 
-                     continue;
 
-                 }
 
-                 if (bn.deal_qty < bn.org_deal_qty || bn.sjcl_qty < bn.org_sjcl_qty || bn.qtcl_qty < bn.org_qtcl_qty || bn.sgfh_qty < bn.org_sgfh_qty) {
 
-                     result.errors.push({
 
-                         ledger_id: bn.ledger_id,
 
-                         b_code: bn.b_code, name: bn.name, unit: bn.unit,
 
-                         deal_qty: bn.deal_qty, sgfh_qty: bn.sgfh_qty, sjcl_qty: bn.sjcl_qty, qtcl_qty: bn.qtcl_qty, qty: bn.quantity, type: 'less',
 
-                     });
 
-                     if (bn.order !== bn.org_order) {
 
-                         result.update.push({
 
-                             id: bn.id, ledger_id: bn.ledger_id, order: bn.order,
 
-                         });
 
-                     }
 
-                 } else if (bn.sjcl_qty !== bn.org_sjcl_qty || bn.qtcl_qty !== bn.org_qtcl_qty || bn.sgfh_qty !== bn.org_sgfh_qty) {
 
-                     result.update.push({
 
-                         id: bn.id, ledger_id: bn.ledger_id, unit_price: bn.unit_price || 0, order: bn.order,
 
-                         deal_qty: bn.deal_qty, deal_tp: bn.deal_tp || 0,
 
-                         sgfh_qty: bn.sgfh_qty, sjcl_qty: bn.sjcl_qty, qtcl_qty: bn.qtcl_qty, quantity: bn.quantity,
 
-                         sgfh_tp: bn.sgfh_tp || 0, sjcl_tp: bn.sjcl_tp || 0, qtcl_tp: bn.qtcl_tp || 0, total_price: bn.total_price || 0,
 
-                     });
 
-                 } else if (bn.order !== bn.org_order) {
 
-                     result.update.push({
 
-                         id: bn.id, ledger_id: bn.ledger_id, order: bn.order,
 
-                     });
 
-                 }
 
-             }
 
-         }
 
-         for (const i of this.items) {
 
-             result.create.push({
 
-                 id: i.id, tender_id: i.tender_id, ledger_id: i.ledger_id, ledger_pid: i.ledger_pid,
 
-                 level: i.level, order: i.order, full_path: i.full_path, is_leaf: !i.children || i.children.length === 0,
 
-                 b_code: i.b_code, name: i.name, unit: i.unit, unit_price: i.unit_price || 0,
 
-                 deal_qty: i.deal_qty, deal_tp: i.deal_tp || 0,
 
-                 sgfh_qty: i.sgfh_qty, sjcl_qty: i.sjcl_qty, qtcl_qty: i.qtcl_qty, quantity: i.quantity,
 
-                 sgfh_tp: i.sgfh_tp || 0, sjcl_tp: i.sjcl_tp || 0, qtcl_tp: i.qtcl_tp || 0, total_price: i.total_price || 0,
 
-             });
 
-         }
 
-         return result;
 
-     }
 
- }
 
- class gatherStageGclTree extends loadGclBaseTree {
 
-     constructor(ctx, setting) {
 
-         super(ctx, setting);
 
-         this.cover = setting.cover;
 
-     }
 
-     loadBase(datas, pos) {
 
-         datas.sort((x, y) => { return x.level === y.level ? x.order - y.order : x.level - y.level; });
 
-         const Index = {};
 
-         for (const d of datas) {
 
-             const parent = this.parent.ledger_id === d.ledger_pid ? this.parent : Index[d.ledger_pid];
 
-             if (!parent) continue;
 
-             if (!parent.children) parent.children = [];
 
-             const relaPos = pos.filter(x => {return x.lid === d.id});
 
-             const baseNode = {
 
-                 id: d.id,
 
-                 is_import: d.is_import,
 
-                 ledger_id: d.ledger_id,
 
-                 ledger_pid: d.ledger_pid,
 
-                 level: d.level,
 
-                 is_leaf: d.is_leaf,
 
-                 full_path: d.full_path,
 
-                 b_code: d.b_code,
 
-                 name: d.name,
 
-                 unit: d.unit,
 
-                 unit_price: d.unit_price,
 
-                 org_contract_qty: d.contract_qty || 0,
 
-                 org_contract_tp: d.contract_tp || 0,
 
-                 org_qc_qty: d.qc_qty || 0,
 
-                 org_qc_tp: d.qc_tp || 0,
 
-                 org_qc_minus_qty: d.qc_minus_qty || 0,
 
-                 org_positive_qc_qty: d.positive_qc_qty || 0,
 
-                 org_positive_qc_tp: d.positive_qc_tp || 0,
 
-                 org_negative_qc_qty: d.negative_qc_qty || 0,
 
-                 org_negative_qc_tp: d.negative_qc_tp || 0,
 
-                 org_order: d.order,
 
-                 contract_qty: 0,
 
-                 contract_tp: 0,
 
-                 qc_qty: 0,
 
-                 qc_tp: 0,
 
-                 is_tp: d.is_tp,
 
-                 hasPos: relaPos.length > 0,
 
-             };
 
-             parent.children.push(baseNode);
 
-             Index[baseNode.ledger_id] = baseNode;
 
-             this.baseNodes.push(baseNode);
 
-         }
 
-     }
 
-     _gatherChange(node, source) {
 
-         if (!source.change_detail || source.change_detail.length === 0) return;
 
-         if (!node.change_detail) node.change_detail = [];
 
-         for (const cd of source.change_detail) {
 
-             if (!cd.qty) continue;
 
-             node.change_detail.push(cd);
 
-         }
 
-     }
 
-     gather(source, parent) {
 
-         parent = parent ? parent : this.parent;
 
-         const checkFun = function (node, source) {
 
-             return (source.is_tp && node.is_tp) || (!source.is_tp && !node.is_tp);
 
-         };
 
-         const node = this.ignoreParent ? this.addNodeWithoutParent(source, checkFun) : this.addNode(source, parent, checkFun);
 
-         if (node.is_tp) {
 
-             node.contract_tp = this.ctx.helper.add(node.contract_tp, source.contract_tp);
 
-         } else {
 
-             node.contract_qty = this.ctx.helper.add(node.contract_qty, source.contract_qty);
 
-             node.contract_tp = this.ctx.helper.mul(node.unit_price, node.contract_qty, this.ctx.tender.info.decimal.tp);
 
-             node.qc_qty = this.ctx.helper.add(node.qc_qty, source.qc_qty);
 
-             node.qc_tp = this.ctx.helper.mul(node.unit_price, node.qc_qty, this.ctx.tender.info.decimal.tp);
 
-             node.qc_minus_qty = this.ctx.helper.add(node.qc_minus_qty, source.qc_minus_qty);
 
-             node.positive_qc_qty = this.ctx.helper.add(node.positive_qc_qty, source.positive_qc_qty);
 
-             node.positive_qc_tp = this.ctx.helper.mul(node.unit_price, node.positive_qc_qty, this.ctx.tender.info.decimal.tp);
 
-             node.negative_qc_qty = this.ctx.helper.add(node.negative_qc_qty, source.negative_qc_qty);
 
-             node.negative_qc_tp = this.ctx.helper.mul(node.unit_price, node.negative_qc_qty, this.ctx.tender.info.decimal.tp);
 
-         }
 
-         this._gatherChange(node, source);
 
-         return node;
 
-     }
 
-     getUpdateData() {
 
-         const result = { update: [], errors: [], qc_detail: [] };
 
-         for (const bn of this.baseNodes) {
 
-             if (!this.cover && !bn.is_import && !bn.contract_qty && !bn.qc_qty && !bn.contract_tp && !bn.qc_minus_qty) continue;
 
-             if (!bn.is_import && bn.org_qc_qty !== 0 && bn.qc_qty !== 0) {
 
-                 result.errors.push({ b_code: bn.b_code, name: bn.name, unit: bn.unit, qc_qty: bn.qc_qty, qc_minus_qty: bn.qc_minus_qty, ledger_id: bn.ledger_id, type: 'qc-conflict'});
 
-                 continue;
 
-             }
 
-             if (bn.is_import || this.cover || bn.contract_qty !== bn.org_contract_qty || bn.contract_tp !== bn.org_contract_tp || bn.qc_qty !== bn.org_qc_qty || bn.qc_minus_qty !== bn.org_qc_minus_qty) {
 
-                 let data = { lid: bn.id, contract_qty: bn.contract_qty, contract_tp: bn.contract_tp };
 
-                 if (!bn.is_import && bn.org_qc_qty) {
 
-                     data.qc_qty = bn.org_qc_qty;
 
-                     data.qc_tp = bn.org_qc_tp;
 
-                     data.qc_minus_qty = bn.org_qc_minus_qty || 0;
 
-                     data.positive_qc_qty = bn.positive_qc_qty || 0;
 
-                     data.positive_qc_tp = bn.positive_qc_tp || 0;
 
-                     data.negative_qc_qty = bn.negative_qc_qty || 0;
 
-                     data.negative_qc_tp = bn.negative_qc_tp || 0;
 
-                 } else {
 
-                     data.qc_qty = bn.qc_qty;
 
-                     data.qc_tp = bn.qc_tp;
 
-                     data.qc_minus_qty = bn.qc_minus_qty || 0;
 
-                     data.positive_qc_qty = bn.positive_qc_qty || 0;
 
-                     data.positive_qc_tp = bn.positive_qc_tp || 0;
 
-                     data.negative_qc_qty = bn.negative_qc_qty || 0;
 
-                     data.negative_qc_tp = bn.negative_qc_tp || 0;
 
-                 }
 
-                 result.update.push(data);
 
-             }
 
-             if (bn.change_detail && bn.change_detail.length > 0) {
 
-                 for (const cd of bn.change_detail) {
 
-                     result.qc_detail.push({
 
-                         lid: bn.id, rela_tid: cd.tid, rela_sid: cd.sid, rela_lid: cd.lid, rela_cid: cd.cid, rela_cbid: cd.cbid, rela_qty: cd.qty, rela_minus: cd.minus, rela_no_value: cd.no_value, unit_price: bn.unit_price,
 
-                     });
 
-                 }
 
-             }
 
-         }
 
-         for (const i of this.items) {
 
-             result.errors.push({ b_code: i.b_code, name: i.name, unit: i.unit, qty: i.contract_qty, qc_qty: i.qc_qty, qc_minus_qty: i.qc_minus_qty, type: 'miss' });
 
-         }
 
-         return result;
 
-     }
 
- }
 
- class sumLoad {
 
-     constructor(ctx) {
 
-         this.ctx = ctx;
 
-     }
 
-     recusiveLoadGatherGcl(node, parent, ignoreParent = false) {
 
-         const isLeaf = !node.children || node.children.length === 0;
 
-         const cur = (!ignoreParent || isLeaf) && node.b_code ? this.loadTree.gather(node, parent) : parent;
 
-         if (isLeaf) return;
 
-         for (const child of node.children) {
 
-             this.recusiveLoadGatherGcl(child, cur, ignoreParent);
 
-         }
 
-     }
 
-     async loadGatherGcl(select, maxId, tenders, defaultData) {
 
-         this.loadTree = new loadLedgerGclTree(this.ctx, {
 
-             parent: select, maxId, type: 'ledger', defaultData,
 
-         });
 
-         for (const tender of tenders) {
 
-             const billsData = await this.ctx.service.ledger.getData(tender.tid);
 
-             const billsTree = new Ledger.billsTree(this.ctx, {
 
-                 id: 'ledger_id',
 
-                 pid: 'ledger_pid',
 
-                 order: 'order',
 
-                 level: 'level',
 
-                 rootId: -1,
 
-                 keys: ['id', 'tender_id', 'ledger_id'],
 
-                 stageId: 'id',
 
-             });
 
-             billsTree.loadDatas(billsData);
 
-             for (const top of billsTree.children) {
 
-                 if ([1].indexOf(top.node_type) < 0) continue;
 
-                 this.recusiveLoadGatherGcl(top, null);
 
-             }
 
-         }
 
-         this.loadTree.resortByCode();
 
-         const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.tender.id } });
 
-         this.loadTree.calculateAll(dealBills);
 
-         return this.loadTree;
 
-     }
 
-     async updateGatherGcl(select, maxId, tenders, defaultData) {
 
-         this.loadTree = new updateReviseGclTree(this.ctx, {
 
-             parent: select, maxId, type: 'ledger', defaultData,
 
-         });
 
-         const posterity = await this.ctx.service.reviseBills.getPosterityByParentId(this.ctx.tender.id, select.ledger_id);
 
-         const pos = await this.ctx.service.revisePos.getData(this.ctx.tender.id);
 
-         this.loadTree.loadBase(posterity, pos);
 
-         for (const tender of tenders) {
 
-             const billsData = await this.ctx.service.ledger.getData(tender.tid);
 
-             const billsTree = new Ledger.billsTree(this.ctx, {
 
-                 id: 'ledger_id',
 
-                 pid: 'ledger_pid',
 
-                 order: 'order',
 
-                 level: 'level',
 
-                 rootId: -1,
 
-                 keys: ['id', 'tender_id', 'ledger_id'],
 
-                 stageId: 'id',
 
-             });
 
-             billsTree.loadDatas(billsData);
 
-             for (const top of billsTree.children) {
 
-                 if ([1].indexOf(top.node_type) < 0) continue;
 
-                 this.recusiveLoadGatherGcl(top, null);
 
-             }
 
-         }
 
-         this.loadTree.resortByCode();
 
-         const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.tender.id } });
 
-         this.loadTree.calculateAll(dealBills);
 
-         return this.loadTree;
 
-     }
 
-     _loadCurStageAndChange(billsData, curStageBills, curStageChange) {
 
-         const billsIndex = {};
 
-         for (const b of billsData) {
 
-             billsIndex[b.id] = b;
 
-         }
 
-         for (const csb of curStageBills) {
 
-             const b = billsIndex[csb.lid];
 
-             if (!b) continue;
 
-             b.contract_qty = csb.contract_qty;
 
-             b.contract_tp = csb.contract_tp;
 
-             b.qc_qty = csb.qc_qty;
 
-             b.qc_minus_qty = csb.qc_minus_qty;
 
-             b.positive_qc_qty = csb.positive_qc_qty;
 
-             b.negative_qc_qty = csb.negative_qc_qty;
 
-         }
 
-         for (const csc of curStageChange) {
 
-             if (!csc.qty) continue;
 
-             const b = billsIndex[csc.lid];
 
-             if (!b) continue;
 
-             if (!b.change_detail) b.change_detail = [];
 
-             let c = b.change_detail.find(x => { return x.cbid === csc.cbid });
 
-             if (!c) {
 
-                 c = { tid: csc.tid, sid: csc.sid, cid: csc.cid, cbid: csc.cbid, lid: csc.lid, c_code: csc.c_code, minus: csc.minus, no_value: csc.no_value };
 
-                 b.change_detail.push(c);
 
-             }
 
-             c.qty = this.ctx.helper.add(c.qty, csc.qty);
 
-         }
 
-     }
 
-     async stageGatherGcl(select, maxId, tenders, defaultData, cover) {
 
-         const ignoreParent = this.ctx.tender.info.fun_rela.sum_load.ignoreParent;
 
-         this.loadTree = new gatherStageGclTree(this.ctx, {
 
-             parent: select, maxId, type: 'ledger', defaultData, ignoreParent, cover,
 
-         });
 
-         const posterity = await this.ctx.service.ledger.getPosterityByParentId(this.ctx.tender.id, select.ledger_id);
 
-         const extraData = await this.ctx.service.ledgerExtra.getData(this.ctx.tender.id, ['is_tp']);
 
-         const stageBills = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
 
-         const importLid = await this.ctx.service.stageImportChange.getLeafXmjImportLid(this.ctx.stage.id, select.id);
 
-         this.ctx.helper.assignRelaData(posterity, [
 
-             { data: extraData, fields: ['is_tp'], prefix: '', relaId: 'id' },
 
-             { data: importLid, fields: ['is_import'], prefix: '', relaId: 'lid' },
 
-             { data: stageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty', 'positive_qc_qty', 'positive_qc_tp', 'negative_qc_qty', 'negative_qc_tp'], prefix: '', relaId: 'lid' },
 
-         ]);
 
-         const pos = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
 
-         this.loadTree.loadBase(posterity, pos);
 
-         for (const tender of tenders) {
 
-             if (!tender.stage) continue;
 
-             const billsData = await this.ctx.service.ledger.getData(tender.tid);
 
-             const billsExtraData = await this.ctx.service.ledgerExtra.getData(tender.tid, ['is_tp']);
 
-             this.ctx.helper.assignRelaData(billsData, [
 
-                 { data: billsExtraData, fields: ['is_tp'], prefix: '', relaId: 'id' },
 
-             ]);
 
-             const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.tid, order: tender.stage});
 
-             if (!stage) throw '选择的期不存在';
 
-             const curStageData = await this.ctx.service.stageBills.getLastestStageData2(tender.tid, stage.id);
 
-             const curStageChange = stage.status === auditConst.stage.status.checked
 
-                 ? await this.ctx.service.stageChangeFinal.getSumLoadFinalData(stage.id)
 
-                 : await this.ctx.service.stageChange.getSumLoadFinalData(stage.id);
 
-             this._loadCurStageAndChange(billsData, curStageData, curStageChange);
 
-             const billsTree = new Ledger.billsTree(this.ctx, {
 
-                 id: 'ledger_id',
 
-                 pid: 'ledger_pid',
 
-                 order: 'order',
 
-                 level: 'level',
 
-                 rootId: -1,
 
-                 keys: ['id', 'tender_id', 'ledger_id'],
 
-                 stageId: 'id',
 
-             });
 
-             billsTree.loadDatas(billsData);
 
-             for (const top of billsTree.children) {
 
-                 if ([1].indexOf(top.node_type) < 0) continue;
 
-                 this.recusiveLoadGatherGcl(top, null, ignoreParent);
 
-             }
 
-         }
 
-         return this.loadTree;
 
-     }
 
- }
 
- module.exports = sumLoad;
 
 
  |