sum_load.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('../lib/ledger');
  10. const auditConst = require('../const/audit');
  11. class loadGclBaseTree {
  12. /**
  13. * 构造函数
  14. * @param {Array} tempData - 清单模板数据
  15. */
  16. constructor (ctx, setting) {
  17. this.ctx = ctx;
  18. this.parent = setting.parent;
  19. this.parent.children = [];
  20. this.defaultData = setting.defaultData;
  21. // 常量
  22. this.splitChar = '-';
  23. // 索引
  24. // 仅通过addNode或addNodeWithoutParent添加节点
  25. this.items = [];
  26. // this.parent下原来的节点
  27. this.baseNodes = [];
  28. this.ignoreParent = setting.ignoreParent;
  29. // 缓存
  30. this.keyNodeId = setting.maxId ? setting.maxId + 1 : 1;
  31. }
  32. /**
  33. * 根据 编号 查找 父项项目节
  34. * @param {String} code - 子项编号
  35. * @returns {*}
  36. */
  37. findNode(node, parent, check) {
  38. parent = parent || this.parent;
  39. if (!parent.children) return null;
  40. for (const child of parent.children) {
  41. const checkLeaf = (child.is_leaf && node.is_leaf) || (!child.is_leaf && !node.is_leaf);
  42. if (child.b_code === node.b_code && child.name === node.name && child.unit === node.unit
  43. && checkLeaf && !child.hasPos && (!check || check(child, node))) {
  44. return child;
  45. }
  46. }
  47. return null;
  48. }
  49. /**
  50. * 添加 树节点 并完善该节点的树结构
  51. * @param {Object} node - 添加节点
  52. * @param {Object} parent - 父项
  53. * @returns {*}
  54. */
  55. addNode(source, parent, check) {
  56. parent = parent ? parent : this.parent;
  57. let node = this.findNode(source, parent, check);
  58. if (!node) {
  59. if (!parent.children) parent.children = [];
  60. node = {
  61. id: this.ctx.app.uuid.v4(),
  62. tender_id: this.ctx.tender.id,
  63. ledger_id: this.keyNodeId,
  64. ledger_pid: parent.ledger_id,
  65. level: parent.level +1,
  66. full_path: parent.full_path + '-' + this.keyNodeId,
  67. order: parent.children.length + 1,
  68. children: [],
  69. b_code: source.b_code,
  70. name: source.name,
  71. unit: source.unit,
  72. deal_qty: 0,
  73. sgfh_qty: 0,
  74. qtcl_qty: 0,
  75. sjcl_qyt: 0,
  76. quantity: 0,
  77. is_leaf: source.is_leaf,
  78. hasPos: false,
  79. };
  80. this.keyNodeId += 1;
  81. parent.children.push(node);
  82. this.items.push(node);
  83. }
  84. return node;
  85. }
  86. findLeaf(node, check) {
  87. for (const bn of this.baseNodes) {
  88. if (bn.b_code === node.b_code && bn.name === node.name && bn.unit === node.unit
  89. && bn.is_leaf && !bn.hasPos && (!check || check(bn, node))) return bn;
  90. }
  91. for (const i of this.items) {
  92. if (i.b_code === node.b_code && i.name === node.name && i.unit === node.unit
  93. && i.is_leaf && !i.hasPos && (!check || check(i, node))) return i;
  94. }
  95. return null;
  96. }
  97. addNodeWithoutParent(source, check) {
  98. let node = this.findLeaf(source, check);
  99. if (!node) {
  100. node = {
  101. id: this.ctx.app.uuid.v4(),
  102. tender_id: this.ctx.tender.id,
  103. ledger_id: this.keyNodeId,
  104. ledger_pid: this.parent.ledger_id,
  105. level: this.parent.level + 1,
  106. full_path: this.parent.full_path + '-' + this.keyNodeId,
  107. order: this.parent.children.length + 1,
  108. b_code: source.b_code,
  109. name: source.name,
  110. unit: source.unit,
  111. deal_qty: 0,
  112. sgfh_qty: 0,
  113. qtcl_qty: 0,
  114. sjcl_qty: 0,
  115. quantity: 0,
  116. is_leaf: source.is_leaf,
  117. hasPos: false,
  118. };
  119. this.keyNodeId += 1;
  120. this.parent.children.push(node);
  121. this.items.push(node);
  122. }
  123. return node;
  124. }
  125. gather(source, parent) {}
  126. getUpdateData() {}
  127. resortChildrenByCode(node) {
  128. const helper = this.ctx.helper;
  129. if (!node.children || node.children.length === 0) return;
  130. node.children.sort((x, y) => {
  131. return helper.compareCode(x.b_code, y.b_code);
  132. });
  133. for (const [i, c] of node.children.entries()) {
  134. this.resortChildrenByCode(c);
  135. c.order = i + 1;
  136. }
  137. }
  138. resortByCode() {
  139. this.resortChildrenByCode(this.parent);
  140. }
  141. recursiveCalculate(dealBills, node) {
  142. if (node.children && node.children.length > 0) {
  143. for (const child of node.children) {
  144. this.recursiveCalculate(dealBills, child);
  145. }
  146. } else {
  147. const p = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  148. node.deal_qty = this.ctx.helper.round(node.deal_qty, p.value);
  149. node.sgfh_qty = this.ctx.helper.round(node.sgfh_qty, p.value);
  150. node.sjcl_qty = this.ctx.helper.round(node.sjcl_qty, p.value);
  151. node.qtcl_qty = this.ctx.helper.round(node.qtcl_qty, p.value);
  152. node.quantity = this.ctx.helper.round(node.quantity, p.value);
  153. if (!node.unit_price) {
  154. const db = dealBills.find(x => { return x.code === node.b_code && x.name === node.name && x.unit === node.unit });
  155. if (!db) return;
  156. node.unit_price = db.unit_price;
  157. }
  158. node.deal_tp = this.ctx.helper.mul(node.deal_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  159. node.sgfh_tp = this.ctx.helper.mul(node.sgfh_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  160. node.sjcl_tp = this.ctx.helper.mul(node.sjcl_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  161. node.qtcl_tp = this.ctx.helper.mul(node.qtcl_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  162. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  163. }
  164. }
  165. calculateAll(dealBills) {
  166. this.recursiveCalculate(dealBills, this.parent);
  167. }
  168. }
  169. class loadLedgerGclTree extends loadGclBaseTree {
  170. gather(source, parent) {
  171. const node = this.addNode(source, parent);
  172. node.deal_qty = this.ctx.helper.add(node.deal_qty, source.deal_qty);
  173. node.sgfh_qty = this.ctx.helper.add(node.sgfh_qty, source.sgfh_qty);
  174. node.qtcl_qty = this.ctx.helper.add(node.qtcl_qty, source.qtcl_qty);
  175. node.sjcl_qty = this.ctx.helper.add(node.sjcl_qty, source.sjcl_qty);
  176. node.quantity = this.ctx.helper.add(node.quantity, source.quantity);
  177. return node;
  178. }
  179. getUpdateData() {
  180. const update = [];
  181. if (this.items.length > 0) update.push({id: this.parent.id, ledger_id: this.parent.ledger_id, is_leaf: 0});
  182. const create = [];
  183. for (const i of this.items) {
  184. create.push({
  185. id: i.id, tender_id: i.tender_id, ledger_id: i.ledger_id, ledger_pid: i.ledger_pid,
  186. level: i.level, order: i.order, full_path: i.full_path, is_leaf: !i.children || i.children.length === 0,
  187. b_code: i.b_code, name: i.name, unit: i.unit, unit_price: i.unit_price || 0,
  188. deal_qty: i.deal_qty, deal_tp: i.deal_tp || 0,
  189. sgfh_qty: i.sgfh_qty, sjcl_qty: i.sjcl_qty, qtcl_qty: i.qtcl_qty, quantity: i.quantity,
  190. sgfh_tp: i.sgfh_tp || 0, sjcl_tp: i.sjcl_tp || 0, qtcl_tp: i.qtcl_tp || 0, total_price: i.total_price || 0,
  191. });
  192. }
  193. return {update, create};
  194. }
  195. }
  196. class updateReviseGclTree extends loadGclBaseTree {
  197. constructor(ctx, setting) {
  198. super(ctx, setting);
  199. this.errors = [];
  200. }
  201. loadBase(datas, pos) {
  202. datas.sort((x, y) => { return x.level === y.level ? x.order - y.order : x.level - y.level; });
  203. const Index = {};
  204. for (const d of datas) {
  205. const parent = this.parent.ledger_id === d.ledger_pid ? this.parent : Index[d.ledger_pid];
  206. if (!parent) continue;
  207. if (!parent.children) parent.children = [];
  208. const relaPos = pos.filter(x => {return x.lid === d.id});
  209. const baseNode = {
  210. id: d.id,
  211. ledger_id: d.ledger_id,
  212. ledger_pid: d.ledger_pid,
  213. level: d.level,
  214. is_leaf: d.is_leaf,
  215. full_path: d.full_path,
  216. b_code: d.b_code,
  217. name: d.name,
  218. unit: d.unit,
  219. unit_price: d.unit_price,
  220. org_deal_qty: d.deal_qty || 0,
  221. org_sgfh_qty: d.sgfh_qty || 0,
  222. org_sjcl_qty: d.sjcl_qty || 0,
  223. org_qtcl_qty: d.qtcl_qty || 0,
  224. org_qty: d.quantity || 0,
  225. org_order: d.order,
  226. sgfh_qty: 0,
  227. sjcl_qty: 0,
  228. qtcl_qty: 0,
  229. quantity: 0,
  230. hasPos: relaPos.length > 0,
  231. };
  232. parent.children.push(baseNode);
  233. Index[baseNode.ledger_id] = baseNode;
  234. this.baseNodes.push(baseNode);
  235. }
  236. }
  237. gather(source, parent) {
  238. const node = this.addNode(source, parent);
  239. node.deal_qty = this.ctx.helper.add(node.deal_qty, source.deal_qty);
  240. node.sgfh_qty = this.ctx.helper.add(node.sgfh_qty, source.sgfh_qty);
  241. node.qtcl_qty = this.ctx.helper.add(node.qtcl_qty, source.qtcl_qty);
  242. node.sjcl_qty = this.ctx.helper.add(node.sjcl_qty, source.sjcl_qty);
  243. node.quantity = this.ctx.helper.add(node.quantity, source.quantity);
  244. return node;
  245. }
  246. getUpdateData() {
  247. const result = {update: [], errors: [], create: []};
  248. if (this.baseNodes.length === 0) {
  249. if (this.items.length > 0) result.update.push({id: this.parent.id, ledger_id: this.parent.ledger_id, is_leaf: 0});
  250. } else {
  251. for (const bn of this.baseNodes) {
  252. if (bn.children && bn.children.length > 0) {
  253. if (bn.order !== bn.org_order) {
  254. result.update.push({
  255. id: bn.id, ledger_id: bn.ledger_id, order: bn.order,
  256. });
  257. }
  258. continue;
  259. }
  260. 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) {
  261. result.errors.push({
  262. ledger_id: bn.ledger_id,
  263. b_code: bn.b_code, name: bn.name, unit: bn.unit,
  264. 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',
  265. });
  266. if (bn.order !== bn.org_order) {
  267. result.update.push({
  268. id: bn.id, ledger_id: bn.ledger_id, order: bn.order,
  269. });
  270. }
  271. } else if (bn.sjcl_qty !== bn.org_sjcl_qty || bn.qtcl_qty !== bn.org_qtcl_qty || bn.sgfh_qty !== bn.org_sgfh_qty) {
  272. result.update.push({
  273. id: bn.id, ledger_id: bn.ledger_id, unit_price: bn.unit_price || 0, order: bn.order,
  274. deal_qty: bn.deal_qty, deal_tp: bn.deal_tp || 0,
  275. sgfh_qty: bn.sgfh_qty, sjcl_qty: bn.sjcl_qty, qtcl_qty: bn.qtcl_qty, quantity: bn.quantity,
  276. sgfh_tp: bn.sgfh_tp || 0, sjcl_tp: bn.sjcl_tp || 0, qtcl_tp: bn.qtcl_tp || 0, total_price: bn.total_price || 0,
  277. });
  278. } else if (bn.order !== bn.org_order) {
  279. result.update.push({
  280. id: bn.id, ledger_id: bn.ledger_id, order: bn.order,
  281. });
  282. }
  283. }
  284. }
  285. for (const i of this.items) {
  286. result.create.push({
  287. id: i.id, tender_id: i.tender_id, ledger_id: i.ledger_id, ledger_pid: i.ledger_pid,
  288. level: i.level, order: i.order, full_path: i.full_path, is_leaf: !i.children || i.children.length === 0,
  289. b_code: i.b_code, name: i.name, unit: i.unit, unit_price: i.unit_price || 0,
  290. deal_qty: i.deal_qty, deal_tp: i.deal_tp || 0,
  291. sgfh_qty: i.sgfh_qty, sjcl_qty: i.sjcl_qty, qtcl_qty: i.qtcl_qty, quantity: i.quantity,
  292. sgfh_tp: i.sgfh_tp || 0, sjcl_tp: i.sjcl_tp || 0, qtcl_tp: i.qtcl_tp || 0, total_price: i.total_price || 0,
  293. });
  294. }
  295. return result;
  296. }
  297. }
  298. class gatherStageGclTree extends loadGclBaseTree {
  299. constructor(ctx, setting) {
  300. super(ctx, setting);
  301. this.cover = setting.cover;
  302. }
  303. loadBase(datas, pos) {
  304. datas.sort((x, y) => { return x.level === y.level ? x.order - y.order : x.level - y.level; });
  305. const Index = {};
  306. for (const d of datas) {
  307. const parent = this.parent.ledger_id === d.ledger_pid ? this.parent : Index[d.ledger_pid];
  308. if (!parent) continue;
  309. if (!parent.children) parent.children = [];
  310. const relaPos = pos.filter(x => {return x.lid === d.id});
  311. const baseNode = {
  312. id: d.id,
  313. is_import: d.is_import,
  314. ledger_id: d.ledger_id,
  315. ledger_pid: d.ledger_pid,
  316. level: d.level,
  317. is_leaf: d.is_leaf,
  318. full_path: d.full_path,
  319. b_code: d.b_code,
  320. name: d.name,
  321. unit: d.unit,
  322. unit_price: d.unit_price,
  323. org_contract_qty: d.contract_qty || 0,
  324. org_contract_tp: d.contract_tp || 0,
  325. org_qc_qty: d.qc_qty || 0,
  326. org_qc_tp: d.qc_tp || 0,
  327. org_qc_minus_qty: d.qc_minus_qty || 0,
  328. org_positive_qc_qty: d.positive_qc_qty || 0,
  329. org_positive_qc_tp: d.positive_qc_tp || 0,
  330. org_negative_qc_qty: d.negative_qc_qty || 0,
  331. org_negative_qc_tp: d.negative_qc_tp || 0,
  332. org_order: d.order,
  333. contract_qty: 0,
  334. contract_tp: 0,
  335. qc_qty: 0,
  336. qc_tp: 0,
  337. is_tp: d.is_tp,
  338. hasPos: relaPos.length > 0,
  339. };
  340. parent.children.push(baseNode);
  341. Index[baseNode.ledger_id] = baseNode;
  342. this.baseNodes.push(baseNode);
  343. }
  344. }
  345. _gatherChange(node, source) {
  346. if (!source.change_detail || source.change_detail.length === 0) return;
  347. if (!node.change_detail) node.change_detail = [];
  348. for (const cd of source.change_detail) {
  349. if (!cd.qty) continue;
  350. node.change_detail.push(cd);
  351. }
  352. }
  353. gather(source, parent) {
  354. parent = parent ? parent : this.parent;
  355. const checkFun = function (node, source) {
  356. return (source.is_tp && node.is_tp) || (!source.is_tp && !node.is_tp);
  357. };
  358. const node = this.ignoreParent ? this.addNodeWithoutParent(source, checkFun) : this.addNode(source, parent, checkFun);
  359. if (node.is_tp) {
  360. node.contract_tp = this.ctx.helper.add(node.contract_tp, source.contract_tp);
  361. } else {
  362. node.contract_qty = this.ctx.helper.add(node.contract_qty, source.contract_qty);
  363. node.contract_tp = this.ctx.helper.mul(node.unit_price, node.contract_qty, this.ctx.tender.info.decimal.tp);
  364. node.qc_qty = this.ctx.helper.add(node.qc_qty, source.qc_qty);
  365. node.qc_tp = this.ctx.helper.mul(node.unit_price, node.qc_qty, this.ctx.tender.info.decimal.tp);
  366. node.qc_minus_qty = this.ctx.helper.add(node.qc_minus_qty, source.qc_minus_qty);
  367. node.positive_qc_qty = this.ctx.helper.add(node.positive_qc_qty, source.positive_qc_qty);
  368. node.positive_qc_tp = this.ctx.helper.mul(node.unit_price, node.positive_qc_qty, this.ctx.tender.info.decimal.tp);
  369. node.negative_qc_qty = this.ctx.helper.add(node.negative_qc_qty, source.negative_qc_qty);
  370. node.negative_qc_tp = this.ctx.helper.mul(node.unit_price, node.negative_qc_qty, this.ctx.tender.info.decimal.tp);
  371. }
  372. this._gatherChange(node, source);
  373. return node;
  374. }
  375. getUpdateData() {
  376. const result = { update: [], errors: [], qc_detail: [] };
  377. for (const bn of this.baseNodes) {
  378. if (!this.cover && !bn.is_import && !bn.contract_qty && !bn.qc_qty && !bn.contract_tp && !bn.qc_minus_qty) continue;
  379. if (!bn.is_import && bn.org_qc_qty !== 0 && bn.qc_qty !== 0) {
  380. 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'});
  381. continue;
  382. }
  383. 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) {
  384. let data = { lid: bn.id, contract_qty: bn.contract_qty, contract_tp: bn.contract_tp };
  385. if (!bn.is_import && bn.org_qc_qty) {
  386. data.qc_qty = bn.org_qc_qty;
  387. data.qc_tp = bn.org_qc_tp;
  388. data.qc_minus_qty = bn.org_qc_minus_qty || 0;
  389. data.positive_qc_qty = bn.positive_qc_qty || 0;
  390. data.positive_qc_tp = bn.positive_qc_tp || 0;
  391. data.negative_qc_qty = bn.negative_qc_qty || 0;
  392. data.negative_qc_tp = bn.negative_qc_tp || 0;
  393. } else {
  394. data.qc_qty = bn.qc_qty;
  395. data.qc_tp = bn.qc_tp;
  396. data.qc_minus_qty = bn.qc_minus_qty || 0;
  397. data.positive_qc_qty = bn.positive_qc_qty || 0;
  398. data.positive_qc_tp = bn.positive_qc_tp || 0;
  399. data.negative_qc_qty = bn.negative_qc_qty || 0;
  400. data.negative_qc_tp = bn.negative_qc_tp || 0;
  401. }
  402. result.update.push(data);
  403. }
  404. if (bn.change_detail && bn.change_detail.length > 0) {
  405. for (const cd of bn.change_detail) {
  406. result.qc_detail.push({
  407. 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
  408. });
  409. }
  410. }
  411. }
  412. for (const i of this.items) {
  413. 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' });
  414. }
  415. return result;
  416. }
  417. }
  418. class sumLoad {
  419. constructor(ctx) {
  420. this.ctx = ctx;
  421. }
  422. recusiveLoadGatherGcl(node, parent, ignoreParent = false) {
  423. const isLeaf = !node.children || node.children.length === 0;
  424. const cur = (!ignoreParent || isLeaf) && node.b_code ? this.loadTree.gather(node, parent) : parent;
  425. if (isLeaf) return;
  426. for (const child of node.children) {
  427. this.recusiveLoadGatherGcl(child, cur, ignoreParent);
  428. }
  429. }
  430. async loadGatherGcl(select, maxId, tenders, defaultData) {
  431. this.loadTree = new loadLedgerGclTree(this.ctx, {
  432. parent: select, maxId, type: 'ledger', defaultData,
  433. });
  434. for (const tender of tenders) {
  435. const billsData = await this.ctx.service.ledger.getData(tender.tid);
  436. const billsTree = new Ledger.billsTree(this.ctx, {
  437. id: 'ledger_id',
  438. pid: 'ledger_pid',
  439. order: 'order',
  440. level: 'level',
  441. rootId: -1,
  442. keys: ['id', 'tender_id', 'ledger_id'],
  443. stageId: 'id',
  444. });
  445. billsTree.loadDatas(billsData);
  446. for (const top of billsTree.children) {
  447. if ([1].indexOf(top.node_type) < 0) continue;
  448. this.recusiveLoadGatherGcl(top, null);
  449. }
  450. }
  451. this.loadTree.resortByCode();
  452. const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.tender.id } });
  453. this.loadTree.calculateAll(dealBills);
  454. return this.loadTree;
  455. }
  456. async updateGatherGcl(select, maxId, tenders, defaultData) {
  457. this.loadTree = new updateReviseGclTree(this.ctx, {
  458. parent: select, maxId, type: 'ledger', defaultData,
  459. });
  460. const posterity = await this.ctx.service.reviseBills.getPosterityByParentId(this.ctx.tender.id, select.ledger_id);
  461. const pos = await this.ctx.service.revisePos.getData(this.ctx.tender.id);
  462. this.loadTree.loadBase(posterity, pos);
  463. for (const tender of tenders) {
  464. const billsData = await this.ctx.service.ledger.getData(tender.tid);
  465. const billsTree = new Ledger.billsTree(this.ctx, {
  466. id: 'ledger_id',
  467. pid: 'ledger_pid',
  468. order: 'order',
  469. level: 'level',
  470. rootId: -1,
  471. keys: ['id', 'tender_id', 'ledger_id'],
  472. stageId: 'id',
  473. });
  474. billsTree.loadDatas(billsData);
  475. for (const top of billsTree.children) {
  476. if ([1].indexOf(top.node_type) < 0) continue;
  477. this.recusiveLoadGatherGcl(top, null);
  478. }
  479. }
  480. this.loadTree.resortByCode();
  481. const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.tender.id } });
  482. this.loadTree.calculateAll(dealBills);
  483. return this.loadTree;
  484. }
  485. _loadCurStageAndChange(billsData, curStageBills, curStageChange) {
  486. const billsIndex = {};
  487. for (const b of billsData) {
  488. billsIndex[b.id] = b;
  489. }
  490. for (const csb of curStageBills) {
  491. const b = billsIndex[csb.lid];
  492. if (!b) continue;
  493. b.contract_qty = csb.contract_qty;
  494. b.contract_tp = csb.contract_tp;
  495. b.qc_qty = csb.qc_qty;
  496. b.qc_minus_qty = csb.qc_minus_qty;
  497. b.positive_qc_qty = csb.positive_qc_qty;
  498. b.negative_qc_qty = csb.negative_qc_qty;
  499. }
  500. for (const csc of curStageChange) {
  501. if (!csc.qty) continue;
  502. const b = billsIndex[csc.lid];
  503. if (!b) continue;
  504. if (!b.change_detail) b.change_detail = [];
  505. let c = b.change_detail.find(x => { return x.cbid === csc.cbid });
  506. if (!c) {
  507. c = { tid: csc.tid, sid: csc.sid, cid: csc.cid, cbid: csc.cbid, lid: csc.lid, c_code: csc.c_code };
  508. b.change_detail.push(c);
  509. }
  510. c.qty = this.ctx.helper.add(c.qty, csc.qty);
  511. }
  512. }
  513. async stageGatherGcl(select, maxId, tenders, defaultData, cover) {
  514. const ignoreParent = this.ctx.tender.info.fun_rela.sum_load.ignoreParent;
  515. this.loadTree = new gatherStageGclTree(this.ctx, {
  516. parent: select, maxId, type: 'ledger', defaultData, ignoreParent, cover,
  517. });
  518. const posterity = await this.ctx.service.ledger.getPosterityByParentId(this.ctx.tender.id, select.ledger_id);
  519. const extraData = await this.ctx.service.ledgerExtra.getData(this.ctx.tender.id, ['is_tp']);
  520. const stageBills = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
  521. const importLid = await this.ctx.service.stageImportChange.getLeafXmjImportLid(this.ctx.stage.id, select.id);
  522. this.ctx.helper.assignRelaData(posterity, [
  523. { data: extraData, fields: ['is_import'], prefix: '', relaId: 'id' },
  524. { data: importLid, fields: ['is_import'], prefix: '', relaId: 'lid' },
  525. { 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' },
  526. ]);
  527. const pos = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  528. this.loadTree.loadBase(posterity, pos);
  529. for (const tender of tenders) {
  530. if (!tender.stage) continue;
  531. const billsData = await this.ctx.service.ledger.getData(tender.tid);
  532. const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.tid, order: tender.stage});
  533. if (!stage) throw '选择的期不存在';
  534. const curStageData = await this.ctx.service.stageBills.getLastestStageData2(tender.tid, stage.id);
  535. const curStageChange = stage.status === auditConst.stage.status.checked
  536. ? await this.ctx.service.stageChangeFinal.getSumLoadFinalData(stage.id)
  537. : await this.ctx.service.stageChange.getSumLoadFinalData(stage.id);
  538. this._loadCurStageAndChange(billsData, curStageData, curStageChange);
  539. const billsTree = new Ledger.billsTree(this.ctx, {
  540. id: 'ledger_id',
  541. pid: 'ledger_pid',
  542. order: 'order',
  543. level: 'level',
  544. rootId: -1,
  545. keys: ['id', 'tender_id', 'ledger_id'],
  546. stageId: 'id',
  547. });
  548. billsTree.loadDatas(billsData);
  549. for (const top of billsTree.children) {
  550. if ([1].indexOf(top.node_type) < 0) continue;
  551. this.recusiveLoadGatherGcl(top, null, ignoreParent);
  552. }
  553. }
  554. return this.loadTree;
  555. }
  556. }
  557. module.exports = sumLoad;