stage_im.js 27 KB

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