sum_load.js 26 KB

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