sum_load.js 28 KB

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