sum_load.js 27 KB

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