stage_im.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('./ledger');
  10. const imType = require('../const/tender').imType;
  11. const mergeChar = ';';
  12. class StageIm {
  13. constructor (ctx) {
  14. const self = this;
  15. this.ctx = ctx;
  16. this._ = this.ctx.helper._;
  17. // mainData
  18. this.billsTree = new Ledger.billsTree(this.ctx, {
  19. id: 'ledger_id',
  20. pid: 'ledger_pid',
  21. order: 'order',
  22. level: 'level',
  23. rootId: -1,
  24. keys: ['id', 'tender_id', 'ledger_id'],
  25. stageId: 'id',
  26. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp'],
  27. calc: function (node) {
  28. if (node.children && node.children.length === 0) {
  29. node.gather_qty = self.ctx.helper.add(node.contract_qty, node.qc_qty);
  30. }
  31. node.gather_tp = self.ctx.helper.add(node.contract_tp, node.qc_tp);
  32. }
  33. });
  34. this.pos = new Ledger.pos({
  35. id: 'id', ledgerId: 'lid',
  36. updateFields: ['contract_qty', 'qc_qty', 'postil'],
  37. calc: function (p) {
  38. p.gather_qty = self.ctx.helper.add(p.contract_qty, p.qc_qty);
  39. }
  40. });
  41. // relaData
  42. this.change = null;
  43. this.details = null;
  44. // result
  45. this.ImData = [];
  46. this.ImBillsData = [];
  47. //
  48. this.imFields = ['uuid', 'doc_code', 'peg', 'bw', 'xm', 'drawing_code', 'calc_memo', 'calc_img'];
  49. this.splitChar = '-';
  50. }
  51. // 加载数据
  52. async _loadMainData () {
  53. const billsData = await this.ctx.service.ledger.getData(this.ctx.tender.id);
  54. if (this.ctx.stage.readOnly) {
  55. const curStage = await this.ctx.service.stageBills.getAuditorStageData(this.ctx.tender.id,
  56. this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
  57. this.ctx.helper.assignRelaData(billsData, [
  58. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  59. ]);
  60. } else {
  61. const curStage = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id);
  62. this.ctx.helper.assignRelaData(billsData, [
  63. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  64. ]);
  65. }
  66. this.billsTree.loadDatas(billsData);
  67. this.billsTree.calculateAll();
  68. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: this.ctx.tender.id }});
  69. if (this.ctx.stage.readOnly) {
  70. const curPosStage = await this.ctx.service.stagePos.getAuditorStageData2(this.ctx.tender.id,
  71. this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
  72. this.ctx.helper.assignRelaData(posData, [
  73. {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'}
  74. ]);
  75. } else {
  76. const curPosStage = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
  77. this.ctx.helper.assignRelaData(posData, [
  78. {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'}
  79. ]);
  80. }
  81. this.pos.loadDatas(posData);
  82. this.pos.calculateAll();
  83. }
  84. async _loadRelaData () {
  85. if (this.ctx.stage.readOnly) {
  86. this.details = await this.ctx.service.stageDetail.getAuditorStageData(this.ctx.tender.id, this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
  87. } else {
  88. this.details = await this.ctx.service.stageDetail.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id);
  89. }
  90. if (this.ctx.stage.readOnly) {
  91. this.changes = await this.ctx.service.stageChange.getAuditorAllStageData(this.ctx.tender.id, this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
  92. } else {
  93. this.changes = await this.ctx.service.stageChange.getLastestAllStageData(this.ctx.tender.id, this.ctx.stage.id);
  94. }
  95. }
  96. // 检查汇总节点
  97. async _checkGather() {
  98. const gatherNodes = this.ctx.stage.im_gather_node ? this.ctx.stage.im_gather_node.split(',') : [];
  99. for (const node of this.billsTree.datas) {
  100. node.check = gatherNodes.indexOf(node.id) !== -1;
  101. }
  102. }
  103. // 辅助取值方法
  104. _getNumberFormat(num, length) {
  105. let s = '0000000000';
  106. s = s + num;
  107. return s.substr(s.length - length);
  108. }
  109. _getNodeByLevel(node, level) {
  110. let cur = node;
  111. while (cur && cur.level > level) {
  112. cur = this.billsTree.getParent(cur);
  113. }
  114. return cur;
  115. }
  116. _checkPeg(text) {
  117. const pegReg = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  118. return pegReg.test(text);
  119. }
  120. _getPegStr(text) {
  121. const pegReg1 = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?[~~—][a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  122. const result1 = text.match(pegReg1);
  123. if (result1) {
  124. return result1[0];
  125. } else {
  126. const pegReg2 = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?-[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  127. const result2 = text.match(pegReg2);
  128. if (result2) {
  129. return result2[0];
  130. } else {
  131. const pegReg3 = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  132. const result3 = text.match(pegReg3);
  133. return result3 ? result3[0] : '';
  134. }
  135. }
  136. }
  137. _getPegNode (node) {
  138. if (node) {
  139. if (this._checkPeg(node.name)) {
  140. return node;
  141. } else {
  142. const parent = this.billsTree.getParent(node);
  143. return parent ? this._getPegNode(parent) : null;
  144. }
  145. }
  146. }
  147. _getDrawingCode(node) {
  148. if (!node) {
  149. return '';
  150. } else if (node.drawing_code) {
  151. return node.drawing_code;
  152. } else {
  153. const parent = this.billsTree.getParent(node);
  154. return parent ? this._getDrawingCode(parent) : '';
  155. }
  156. }
  157. _getZlNormalBw(node, peg) {
  158. if (peg) {
  159. const subPeg1 = this._getNodeByLevel(node, peg.level + 1);
  160. const subPeg2 = this._getNodeByLevel(node, peg.level + 2);
  161. let result = peg.name;
  162. if (subPeg1 && subPeg1.id !== peg.id && subPeg1.id !== node.id) {
  163. result = result + mergeChar + subPeg1.name;
  164. if (subPeg2 && subPeg2.id !== subPeg1.id && subPeg2.id !== node.id) {
  165. result = result + mergeChar + subPeg2.name;
  166. }
  167. }
  168. return result;
  169. } else {
  170. if (node.level === 2 || node.level === 3) {
  171. return node.name;
  172. } else if (node.level >= 4) {
  173. let parent = this.billsTree.getParent(node), result = parent.name;
  174. while (parent.level > 3 && parent) {
  175. parent = this._getNodeByLevel(node, parent.level - 1);
  176. result = parent.name + mergeChar + result;
  177. }
  178. return result;
  179. } else {
  180. return '';
  181. }
  182. }
  183. }
  184. _getZlGatherBw(node, peg) {
  185. if (peg) {
  186. const subPeg1 = this._getNodeByLevel(node, peg.level + 1);
  187. let result = peg.name;
  188. if (subPeg1 && subPeg1.id !== peg.id) {
  189. result = result + mergeChar + subPeg1.name;
  190. }
  191. return result;
  192. } else {
  193. if (node.level < 3) {
  194. return node.name;
  195. } else {
  196. let parent = node, result = parent.name;
  197. while (parent.level > 3 && parent) {
  198. parent = this._getNodeByLevel(node, parent.level - 1);
  199. result = parent.name + mergeChar + result;
  200. }
  201. return result;
  202. }
  203. }
  204. }
  205. _checkCustomDetail(im) {
  206. const self = this;
  207. const cd = this._.find(this.details, function (d) {
  208. return im.lid === d.lid &&
  209. (!im.code || im.code === d.code) &&
  210. (!im.name || im.name === d.name) &&
  211. (!im.unit || im.unit === d.unit) &&
  212. self.ctx.helper.checkZero(self.ctx.helper.sub(im.unit_price, d.unit_price)) &&
  213. (!im.pid || im.pid === d.pid);
  214. });
  215. if (cd) {
  216. this._.assignInWith(im, cd, function (oV, sV, key) {
  217. return self.imFields.indexOf(key) > -1 && sV !== undefined && sV !== null ? sV : oV;
  218. });
  219. }
  220. }
  221. _getCalcMemo(im) {
  222. if (im.calc_memo !== undefined && im.calc_memo !== null && im.calc_memo !== '') return;
  223. if (im.leafXmjs && im.leafXmjs.length > 0) {
  224. const memo = ['本期计量:' + im.jl + ' ' + im.unit];
  225. for (const lx of im.leafXmjs) {
  226. for (const p of lx.pos) {
  227. memo.push(p.name + ':' + p.jl + ' ' + im.unit);
  228. }
  229. }
  230. im.calc_memo = memo.join('\n');
  231. } else if (im.check) {
  232. const memo = [];
  233. for (const [i, b] of im.gclBills.entries()) {
  234. if (b.pos && b.pos.length > 0) {
  235. memo.push('清单' + (i+1) + b.b_code + ' ' + b.name);
  236. for (const p of b.pos) {
  237. memo.push(p.name + ':' + p.jl + ' ' + b.unit);
  238. }
  239. } else {
  240. memo.push('清单' + (i+1) + b.b_code + ' ' + b.name + ':' + b.jl + ' ' + b.unit);
  241. }
  242. }
  243. im.calc_memo = memo.join('\n');
  244. } else {
  245. im.calc_memo = '';
  246. }
  247. }
  248. _getChangeInfo(im) {
  249. if (im.changes && im.changes.length > 0) {
  250. const code = this._.uniq(this._.map(im.changes, 'c_code'));
  251. if (!im.bgl_code || im.bgl_code === '') im.bgl_code = code.join(';');
  252. const new_code = this._.uniq(this._.map(im.changes, 'c_new_code'));
  253. if (!im.bgl_drawing_code || im.bgl_drawing_code === '') im.bgl_drawing_code = new_code.join(';');
  254. }
  255. }
  256. _generateTzPosData(node, gclBills) {
  257. if (!gclBills.pos) {
  258. gclBills.pos = [];
  259. }
  260. const posRange = this.pos.getLedgerPos(node.id);
  261. if (!posRange) { return }
  262. for (const p of posRange) {
  263. if (!p.gather_qty || this.ctx.helper.checkZero(p.gather_qty)) { continue; }
  264. let lp = this._.find(gclBills.pos, {name: p.name});
  265. if (!lp) {
  266. lp = {name: p.name};
  267. gclBills.pos.push(lp);
  268. }
  269. lp.jl = this.ctx.helper.add(lp.jl, p.gather_qty);
  270. lp.contract_jl = this.ctx.helper.add(lp.contract_jl, p.contract_qty);
  271. lp.qc_jl = this.ctx.helper.add(lp.qc_jl, p.qc_qty);
  272. }
  273. }
  274. _generateTzGclBillsData(node, im) {
  275. if (!im.gclBills) {
  276. im.gclBills = [];
  277. }
  278. const posterity = this.billsTree.getPosterity(node);
  279. for (const p of posterity) {
  280. if ((!p.b_code || p.b_code === '') || (p.children && p.children.length > 0)) {
  281. continue;
  282. }
  283. if ((!p.contract_tp || p.contract_tp === 0) && (!p.qc_tp || p.qc_tp === 0)) {
  284. continue;
  285. }
  286. let b = this._.find(im.gclBills, {bid: p.id});
  287. if (!b) {
  288. b = {imid: im.id, bid: p.id, b_code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price};
  289. im.gclBills.push(b);
  290. this.ImBillsData.push(b);
  291. }
  292. b.jl = this.ctx.helper.add(b.jl, p.gather_qty);
  293. b.contract_jl = this.ctx.helper.add(b.contract_jl, p.contract_qty);
  294. b.qc_jl = this.ctx.helper.add(b.qc_jl, p.qc_qty);
  295. this._generateTzPosData(p, b);
  296. }
  297. }
  298. _generateTzChangeData(node, im) {
  299. if (!im.changes) {
  300. im.changes = [];
  301. }
  302. const posterity = this.billsTree.getPosterity(node);
  303. for (const p of posterity) {
  304. if (p.children && p.children.length > 0) {
  305. continue;
  306. }
  307. if ((!p.qc_tp || p.qc_tp === 0)) {
  308. continue;
  309. }
  310. const posRange = this.pos.getLedgerPos(p.id);
  311. if (!posRange) {
  312. for (const c of this.changes) {
  313. if (c.lid === p.id && c.pid == -1 && c.qty && c.qty !== 0) {
  314. im.changes.push(c);
  315. }
  316. }
  317. } else {
  318. for (const pp of posRange) {
  319. if ((!pp.qc_qty || pp.qc_qty === 0)) {
  320. continue;
  321. }
  322. for (const c of this.changes) {
  323. if (c.lid === p.id && c.pid === pp.id && c.qty && c.qty !== 0) {
  324. im.changes.push(c);
  325. }
  326. }
  327. }
  328. }
  329. }
  330. }
  331. /**
  332. * 生成 0号台账 中间计量数据
  333. * @param {Object} node - 生成中间计量表的节点
  334. */
  335. _generateTzImData (node) {
  336. if (node.gather_tp) {
  337. const peg = this._getPegNode(node);
  338. const im = {
  339. id: this.ImData.length + 1,
  340. lid: node.id, pid: '', code: node.code,
  341. jl: node.gather_tp, contract_jl: node.contract_tp, qc_jl: node.qc_tp,
  342. peg: peg ? this._getPegStr(peg.name) : '', drawing_code: this._getDrawingCode(node),
  343. };
  344. if (this.ctx.stage.im_gather && node.check) {
  345. im.bw = this._getZlGatherBw(node, peg);
  346. im.xm = '';
  347. } else {
  348. im.bw = this._getZlNormalBw(node, peg);
  349. im.xm = node.name;
  350. }
  351. this._checkCustomDetail(im);
  352. im.check = this.ctx.stage.im_gather && node.check;
  353. this._generateTzGclBillsData(node, im);
  354. this.ImData.push(im);
  355. this._generateTzChangeData(node, im);
  356. }
  357. }
  358. _generateZlPosData(node, lx) {
  359. if (!lx.pos) {
  360. lx.pos = [];
  361. }
  362. const posRange = this.pos.getLedgerPos(node.id);
  363. if (!posRange) { return }
  364. for (const p of posRange) {
  365. if (!p.gather_qty || this.ctx.helper.checkZero(p.gather_qty)) { continue; }
  366. let lp = this._.find(lx.pos, {name: p.name});
  367. if (!lp) {
  368. lp = {name: p.name};
  369. lx.pos.push(lp);
  370. }
  371. lp.jl = this.ctx.helper.add(lp.jl, p.gather_qty);
  372. lp.contract_jl = this.ctx.helper.add(lp.contract_jl, p.contract_qty);
  373. lp.qc_jl = this.ctx.helper.add(lp.qc_jl, p.qc_qty);
  374. }
  375. }
  376. _generateZlLeafXmjData(node, im) {
  377. if (!im.leafXmjs) {
  378. im.leafXmjs = [];
  379. }
  380. const leafXmj = this.billsTree.getLeafXmjParent(node);
  381. if (!leafXmj) { return }
  382. let lx = this._.find(im.leafXmjs, {lxid: leafXmj.id});
  383. if (!lx) {
  384. lx = {
  385. lxid: leafXmj.id,
  386. code: leafXmj.code,
  387. name: leafXmj.name
  388. };
  389. im.leafXmjs.push(lx);
  390. }
  391. lx.jl = this.ctx.helper.add(lx.jl, node.gather_qty);
  392. lx.contract_jl = this.ctx.helper.add(lx.contract_jl, node.contract_qty);
  393. lx.qc_jl = this.ctx.helper.add(lx.qc_jl, node.qc_qty);
  394. this._generateZlPosData(node, lx);
  395. }
  396. _generateZlChangeData(node, im) {
  397. if (!im.changes) {
  398. im.changes = [];
  399. }
  400. if ((!node.qc_qty || node.qc_qty === 0)) {
  401. return;
  402. }
  403. const posRange = this.pos.getLedgerPos(node.id);
  404. if (!posRange) {
  405. for (const c of this.changes) {
  406. if (c.lid === node.id && c.pid == -1 && c.qty && c.qty !== 0) {
  407. im.changes.push(c);
  408. }
  409. }
  410. } else {
  411. for (const p of posRange) {
  412. if ((!p.qc_qty || p.qc_qty === 0)) {
  413. continue;
  414. }
  415. for (const c of this.changes) {
  416. if (c.lid === node.id && c.pid === p.id && c.qty && c.qty !== 0) {
  417. im.changes.push(c);
  418. }
  419. }
  420. }
  421. }
  422. }
  423. /**
  424. * 生成 总量控制 中间计量数据
  425. * @param {Object} node - 生成中间计量表的节点
  426. */
  427. _generateZlImData (node) {
  428. const self = this;
  429. const nodeImData = [], posterity = this.billsTree.getPosterity(node);
  430. for (const p of posterity) {
  431. if (p.children && p.children.length > 0 ) { continue; }
  432. if (!p.b_code || p.b_code === '') { continue }
  433. if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.qc_qty) ) { continue; }
  434. let im = nodeImData.find(function (d) {
  435. return d.lid === node.id &&
  436. d.code === p.b_code && p.name === d.name && p.unit === d.unit &&
  437. this.ctx.helper.checkZero(self.ctx.helper.sub(p.unit_price, d.unit_price));
  438. });
  439. if (!im) {
  440. const peg = this._getPegNode(node);
  441. im = {
  442. id: this.ImData.length + 1,
  443. lid: node.id, pid: '', code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price,
  444. jl: 0, contract_jl: 0, qc_jl: 0,
  445. peg: peg ? this._getPegStr(peg.name) : ''
  446. };
  447. if (this.ctx.stage.im_gather && node.check) {
  448. im.check = true;
  449. im.bw = this._getZlGatherBw(node, peg);
  450. im.xm = '';
  451. im.drawing_code = this._getDrawingCode(node);
  452. } else {
  453. im.check = false;
  454. im.bw = this._getZlNormalBw(node, peg);
  455. im.xm = node.name;
  456. im.drawing_code = this._getDrawingCode(p);
  457. }
  458. nodeImData.push(im);
  459. this._checkCustomDetail(im);
  460. this.ImData.push(im);
  461. }
  462. if (!this.ctx.stage.im_gather || !node.check) {
  463. this._generateZlLeafXmjData(p, im, 'gather_qty');
  464. }
  465. this._generateZlChangeData(p, im);
  466. im.jl = this.ctx.helper.add(im.jl, p.gather_qty);
  467. im.contract_jl = this.ctx.helper.add(im.contract_jl, p.contract_qty);
  468. im.qc_jl = this.ctx.helper.add(im.qc_jl, p.qc_qty);
  469. }
  470. }
  471. _generateBwImData (node) {
  472. const posterity = this.billsTree.getPosterity(node);
  473. for (const p of posterity) {
  474. if (p.children && p.children.length > 0 ) { continue; }
  475. if (!p.b_code || p.b_code === '') { continue }
  476. const peg = this._getPegNode(node);
  477. const pPos = this.pos.getLedgerPos(p.id);
  478. const bw = this._getZlNormalBw(node, peg);
  479. if (pPos && pPos.length > 0) {
  480. for (const pp of pPos) {
  481. if (this.ctx.helper.checkZero(pp.contract_qty) && this.ctx.helper.checkZero(pp.qc_qty)) { continue }
  482. const im = {
  483. id: this.ImData.length + 1,
  484. lid: node.id, code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price, pid: pp.id,
  485. jl: pp.gather_qty, contract_jl: pp.contract_qty, qc_jl: pp.qc_qty,
  486. bw: bw,
  487. peg: this._checkPeg(pp.name) ? this._getPegStr(pp.name) : (peg ? this._getPegStr(peg.name) : ''),
  488. xm: pp.name,
  489. drawing_code: pp.drawing_code,
  490. changes: [],
  491. };
  492. im.calc_memo = '本期计量:' + im.jl + ' ' + im.unit;
  493. this._checkCustomDetail(im);
  494. this.ImData.push(im);
  495. if (pp.qc_qty && pp.qc_qty !== 0) {
  496. for (const c of this.changes) {
  497. if (c.lid === p.id && c.pid === pp.id && c.qty && c.qty !== 0) {
  498. im.changes.push(c);
  499. }
  500. }
  501. }
  502. }
  503. } else {
  504. if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.qc_qty)) { continue }
  505. const im = {
  506. id: this.ImData.length + 1,
  507. lid: node.id, code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price, pid: '',
  508. jl: p.gather_qty, contract_jl: p.contract_qty, qc_jl: p.qc_qty,
  509. bw: bw,
  510. peg: peg ? this._getPegStr(peg.name) : '',
  511. xm: node.name,
  512. drawing_code: this._getDrawingCode(p),
  513. changes: [],
  514. };
  515. im.calc_memo = '本期计量:' + im.jl + ' ' + im.unit;
  516. this._checkCustomDetail(im);
  517. this.ImData.push(im);
  518. if (p.qc_qty && p.qc_qty !== 0) {
  519. for (const c of this.changes) {
  520. if (c.lid === p.id && c.pid == -1 && c.qty && c.qty !== 0) {
  521. im.changes.push(c);
  522. }
  523. }
  524. }
  525. }
  526. }
  527. }
  528. /**
  529. * 递归 生成中间计量表
  530. * @param {Array} nodes
  531. */
  532. _recursiveBuildImData (nodes) {
  533. if (!nodes || nodes.length === 0) { return; }
  534. for (const node of nodes) {
  535. if (this.billsTree.isLeafXmj(node) || (this.ctx.stage.im_type !== imType.bw.value && this.ctx.stage.im_gather && node.check)) {
  536. if (this.ctx.stage.im_type === imType.tz.value) {
  537. this._generateTzImData(node);
  538. } else if (this.ctx.stage.im_type === imType.zl.value) {
  539. this._generateZlImData(node);
  540. } else if (this.ctx.stage.im_type === imType.bw.value) {
  541. this._generateBwImData(node);
  542. }
  543. } else {
  544. this._recursiveBuildImData(node.children);
  545. }
  546. }
  547. }
  548. // 生成中间计量数据
  549. async buildImData () {
  550. const self = this;
  551. // 初始化
  552. await this._loadMainData();
  553. await this._loadRelaData();
  554. if (this.ctx.stage.im_gather) {
  555. this._checkGather();
  556. }
  557. // 生成数据
  558. this._recursiveBuildImData(this.billsTree.children);
  559. // 排序
  560. if (this.ctx.stage.im_type !== imType.tz.value) {
  561. this.ImData.sort(function (x, y) {
  562. return self.ctx.helper.compareCode(x.code, y.code);
  563. });
  564. }
  565. // 生成数据(需要缓存,并清理缓存)
  566. const pre = (this.ctx.stage.im_pre && this.ctx.stage.im_pre !== '') ? this.ctx.stage.im_pre + splitChar : '';
  567. for (const [i, im] of this.ImData.entries()) {
  568. im.im_code = pre + this._getNumberFormat(this.ctx.stage.order, 2) + this.splitChar + this._getNumberFormat(i + 1, 3);
  569. if (im.gclBills) {
  570. for (const b of im.gclBills) {
  571. b.im_code = im.im_code;
  572. }
  573. }
  574. this._getCalcMemo(im);
  575. delete im.leafXmjs;
  576. delete im.gclBills;
  577. this._getChangeInfo(im);
  578. delete im.changes;
  579. }
  580. }
  581. }
  582. module.exports = StageIm;