stage_im.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. _recursiveGenerateTzGclBills(node, im) {
  295. for (const p of node.children) {
  296. if (p.children && p.children.length > 0) {
  297. this._recursiveGenerateTzGclBills(p, im);
  298. } else {
  299. if ((!p.b_code || p.b_code === '') || (p.children && p.children.length > 0)) {
  300. continue;
  301. }
  302. if ((!p.contract_tp || p.contract_tp === 0) && (!p.qc_tp || p.qc_tp === 0)) {
  303. continue;
  304. }
  305. let b = this._.find(im.gclBills, {bid: p.id});
  306. if (!b) {
  307. b = {imid: im.id, bid: p.id, b_code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price};
  308. im.gclBills.push(b);
  309. this.ImBillsData.push(b);
  310. }
  311. b.quantity = this.ctx.helper.add(b.quantity, p.quantity);
  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. }
  325. _generateTzGclBillsData(node, im) {
  326. if (!im.gclBills) {
  327. im.gclBills = [];
  328. }
  329. this._recursiveGenerateTzGclBills(node, im);
  330. }
  331. _generateTzChangeData(node, im) {
  332. if (!im.changes) {
  333. im.changes = [];
  334. }
  335. const posterity = this.billsTree.getPosterity(node);
  336. for (const p of posterity) {
  337. if (p.children && p.children.length > 0) {
  338. continue;
  339. }
  340. if ((!p.qc_tp || p.qc_tp === 0)) {
  341. continue;
  342. }
  343. const posRange = this.pos.getLedgerPos(p.id);
  344. if (!posRange) {
  345. for (const c of this.changes) {
  346. if (c.lid === p.id && c.pid == -1 && c.qty && c.qty !== 0) {
  347. im.changes.push(c);
  348. }
  349. }
  350. } else {
  351. for (const pp of posRange) {
  352. if ((!pp.qc_qty || pp.qc_qty === 0)) {
  353. continue;
  354. }
  355. for (const c of this.changes) {
  356. if (c.lid === p.id && c.pid === pp.id && c.qty && c.qty !== 0) {
  357. im.changes.push(c);
  358. }
  359. }
  360. }
  361. }
  362. }
  363. }
  364. /**
  365. * 生成 0号台账 中间计量数据
  366. * @param {Object} node - 生成中间计量表的节点
  367. */
  368. _generateTzImData (node) {
  369. if (node.gather_tp) {
  370. const peg = this._getPegNode(node);
  371. const im = {
  372. id: this.ImData.length + 1,
  373. lid: node.id, pid: '', code: node.code,
  374. jl: node.gather_tp, contract_jl: node.contract_tp, qc_jl: node.qc_tp,
  375. pre_jl: node.pre_gather_tp, pre_contract_jl: node.pre_contract_tp, pre_qc_jl: node.pre_qc_tp,
  376. end_jl: node.end_gather_tp, end_contract_jl: node.end_contract_tp, end_qc_jl: node.end_qc_tp,
  377. peg: peg ? this._getPegStr(peg.name) : '', drawing_code: this._getDrawingCode(node),
  378. };
  379. if (this.ctx.stage.im_gather && node.check) {
  380. im.bw = this._getZlGatherBw(node, peg);
  381. im.xm = '';
  382. } else {
  383. im.bw = this._getZlNormalBw(node, peg);
  384. im.xm = node.name;
  385. }
  386. this._checkCustomDetail(im);
  387. im.check = this.ctx.stage.im_gather && node.check;
  388. this._generateTzGclBillsData(node, im);
  389. this.ImData.push(im);
  390. this._generateTzChangeData(node, im);
  391. }
  392. }
  393. _generateZlPosData(node, lx) {
  394. if (!lx.pos) {
  395. lx.pos = [];
  396. }
  397. const posRange = this.pos.getLedgerPos(node.id);
  398. if (!posRange) { return }
  399. for (const p of posRange) {
  400. if (!p.gather_qty || this.ctx.helper.checkZero(p.gather_qty)) { continue; }
  401. let lp = this._.find(lx.pos, {name: p.name});
  402. if (!lp) {
  403. lp = {name: p.name};
  404. lx.pos.push(lp);
  405. }
  406. lp.jl = this.ctx.helper.add(lp.jl, p.gather_qty);
  407. lp.contract_jl = this.ctx.helper.add(lp.contract_jl, p.contract_qty);
  408. lp.qc_jl = this.ctx.helper.add(lp.qc_jl, p.qc_qty);
  409. }
  410. }
  411. _generateZlLeafXmjData(node, im) {
  412. if (!im.leafXmjs) {
  413. im.leafXmjs = [];
  414. }
  415. const leafXmj = this.billsTree.getLeafXmjParent(node);
  416. if (!leafXmj) { return }
  417. let lx = this._.find(im.leafXmjs, {lxid: leafXmj.id});
  418. if (!lx) {
  419. lx = {
  420. lxid: leafXmj.id,
  421. code: leafXmj.code,
  422. name: leafXmj.name
  423. };
  424. im.leafXmjs.push(lx);
  425. }
  426. lx.jl = this.ctx.helper.add(lx.jl, node.gather_qty);
  427. lx.contract_jl = this.ctx.helper.add(lx.contract_jl, node.contract_qty);
  428. lx.qc_jl = this.ctx.helper.add(lx.qc_jl, node.qc_qty);
  429. this._generateZlPosData(node, lx);
  430. }
  431. _generateZlChangeData(node, im) {
  432. if (!im.changes) {
  433. im.changes = [];
  434. }
  435. if ((!node.qc_qty || node.qc_qty === 0)) {
  436. return;
  437. }
  438. const posRange = this.pos.getLedgerPos(node.id);
  439. if (!posRange) {
  440. for (const c of this.changes) {
  441. if (c.lid === node.id && c.pid == -1 && c.qty && c.qty !== 0) {
  442. im.changes.push(c);
  443. }
  444. }
  445. } else {
  446. for (const p of posRange) {
  447. if ((!p.qc_qty || p.qc_qty === 0)) {
  448. continue;
  449. }
  450. for (const c of this.changes) {
  451. if (c.lid === node.id && c.pid === p.id && c.qty && c.qty !== 0) {
  452. im.changes.push(c);
  453. }
  454. }
  455. }
  456. }
  457. }
  458. /**
  459. * 生成 总量控制 中间计量数据
  460. * @param {Object} node - 生成中间计量表的节点
  461. */
  462. _generateZlImData (node) {
  463. const self = this;
  464. const nodeImData = [], posterity = this.billsTree.getPosterity(node);
  465. for (const p of posterity) {
  466. if (p.children && p.children.length > 0 ) { continue; }
  467. if (!p.b_code || p.b_code === '') { continue }
  468. if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.qc_qty) ) { continue; }
  469. let im = nodeImData.find(function (d) {
  470. return d.lid === node.id &&
  471. d.code === p.b_code && p.name === d.name && p.unit === d.unit &&
  472. this.ctx.helper.checkZero(self.ctx.helper.sub(p.unit_price, d.unit_price));
  473. });
  474. if (!im) {
  475. const peg = this._getPegNode(node);
  476. im = {
  477. id: this.ImData.length + 1,
  478. lid: node.id, pid: '', code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price,
  479. quantity: 0,
  480. jl: 0, contract_jl: 0, qc_jl: 0,
  481. pre_jl: 0, pre_contract_jl: 0, pre_qc_jl: 0,
  482. end_jl: 0, end_contract_jl: 0, end_qc_jl: 0,
  483. peg: peg ? this._getPegStr(peg.name) : ''
  484. };
  485. if (this.ctx.stage.im_gather && node.check) {
  486. im.check = true;
  487. im.bw = this._getZlGatherBw(node, peg);
  488. im.xm = '';
  489. im.drawing_code = this._getDrawingCode(node);
  490. } else {
  491. im.check = false;
  492. im.bw = this._getZlNormalBw(node, peg);
  493. im.xm = node.name;
  494. im.drawing_code = this._getDrawingCode(p);
  495. }
  496. nodeImData.push(im);
  497. this._checkCustomDetail(im);
  498. this.ImData.push(im);
  499. }
  500. if (!this.ctx.stage.im_gather || !node.check) {
  501. this._generateZlLeafXmjData(p, im, 'gather_qty');
  502. }
  503. this._generateZlChangeData(p, im);
  504. im.quantity = this.ctx.helper.add(im.quantity, p.quantity);
  505. im.jl = this.ctx.helper.add(im.jl, p.gather_qty);
  506. im.contract_jl = this.ctx.helper.add(im.contract_jl, p.contract_qty);
  507. im.qc_jl = this.ctx.helper.add(im.qc_jl, p.qc_qty);
  508. im.pre_jl = this.ctx.helper.add(im.pre_jl, p.pre_gather_qty);
  509. im.pre_contract_jl = this.ctx.helper.add(im.pre_contract_jl, p.pre_contract_qty);
  510. im.pre_qc_jl = this.ctx.helper.add(im.pre_qc_jl, p.pre_qc_qty);
  511. im.end_jl = this.ctx.helper.add(im.end_jl, p.end_gather_qty);
  512. im.end_contract_jl = this.ctx.helper.add(im.end_contract_jl, p.end_contract_qty);
  513. im.end_qc_jl = this.ctx.helper.add(im.end_qc_jl, p.end_qc_qty);
  514. }
  515. }
  516. _generateBwImData (node) {
  517. const posterity = this.billsTree.getPosterity(node);
  518. for (const p of posterity) {
  519. if (p.children && p.children.length > 0 ) { continue; }
  520. if (!p.b_code || p.b_code === '') { continue }
  521. const peg = this._getPegNode(node);
  522. const pPos = this.pos.getLedgerPos(p.id);
  523. const bw = this._getZlNormalBw(node, peg);
  524. if (pPos && pPos.length > 0) {
  525. for (const pp of pPos) {
  526. if (this.ctx.helper.checkZero(pp.contract_qty) && this.ctx.helper.checkZero(pp.qc_qty)) { continue }
  527. const im = {
  528. id: this.ImData.length + 1,
  529. lid: node.id, code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price, pid: pp.id,
  530. quantity: pp.quantity,
  531. jl: pp.gather_qty, contract_jl: pp.contract_qty, qc_jl: pp.qc_qty,
  532. pre_jl: pp.pre_gather_qty, pre_contract_jl: pp.pre_contract_qty, pre_qc_jl: pp.pre_qc_qty,
  533. end_jl: pp.end_gather_qty, end_contract_jl: pp.end_contract_qty, end_qc_jl: pp.end_qc_qty,
  534. bw: bw,
  535. peg: this._checkPeg(pp.name) ? this._getPegStr(pp.name) : (peg ? this._getPegStr(peg.name) : ''),
  536. xm: pp.name,
  537. drawing_code: pp.drawing_code,
  538. changes: [],
  539. };
  540. im.calc_memo = '本期计量:' + im.jl + ' ' + im.unit;
  541. this._checkCustomDetail(im);
  542. this.ImData.push(im);
  543. if (pp.qc_qty && pp.qc_qty !== 0) {
  544. for (const c of this.changes) {
  545. if (c.lid === p.id && c.pid === pp.id && c.qty && c.qty !== 0) {
  546. im.changes.push(c);
  547. }
  548. }
  549. }
  550. }
  551. } else {
  552. if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.qc_qty)) { continue }
  553. const im = {
  554. id: this.ImData.length + 1,
  555. lid: node.id, code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price, pid: '',
  556. quantity: p.quantity,
  557. jl: p.gather_qty, contract_jl: p.contract_qty, qc_jl: p.qc_qty,
  558. pre_jl: p.pre_gather_qty, pre_contract_jl: p.pre_contract_qty, pre_qc_jl: p.pre_qc_qty,
  559. end_jl: p.end_gather_qty, end_contract_jl: p.end_contract_qty, end_qc_jl: p.end_qc_qty,
  560. bw: bw,
  561. peg: peg ? this._getPegStr(peg.name) : '',
  562. xm: node.name,
  563. drawing_code: this._getDrawingCode(p),
  564. changes: [],
  565. };
  566. im.calc_memo = '本期计量:' + im.jl + ' ' + im.unit;
  567. this._checkCustomDetail(im);
  568. this.ImData.push(im);
  569. if (p.qc_qty && p.qc_qty !== 0) {
  570. for (const c of this.changes) {
  571. if (c.lid === p.id && c.pid == -1 && c.qty && c.qty !== 0) {
  572. im.changes.push(c);
  573. }
  574. }
  575. }
  576. }
  577. }
  578. }
  579. /**
  580. * 递归 生成中间计量表
  581. * @param {Array} nodes
  582. */
  583. _recursiveBuildImData (nodes) {
  584. if (!nodes || nodes.length === 0) { return; }
  585. for (const node of nodes) {
  586. if (this.billsTree.isLeafXmj(node) || (this.ctx.stage.im_type !== imType.bw.value && this.ctx.stage.im_gather && node.check)) {
  587. if (this.ctx.stage.im_type === imType.tz.value) {
  588. this._generateTzImData(node);
  589. } else if (this.ctx.stage.im_type === imType.zl.value) {
  590. this._generateZlImData(node);
  591. } else if (this.ctx.stage.im_type === imType.bw.value) {
  592. this._generateBwImData(node);
  593. }
  594. } else {
  595. this._recursiveBuildImData(node.children);
  596. }
  597. }
  598. }
  599. // 生成中间计量数据
  600. async buildImData () {
  601. const self = this;
  602. // 初始化
  603. await this._loadMainData();
  604. await this._loadRelaData();
  605. if (this.ctx.stage.im_gather) {
  606. this._checkGather();
  607. }
  608. // 生成数据
  609. this._recursiveBuildImData(this.billsTree.children);
  610. // 排序
  611. if (this.ctx.stage.im_type !== imType.tz.value) {
  612. this.ImData.sort(function (x, y) {
  613. return self.ctx.helper.compareCode(x.code, y.code);
  614. });
  615. }
  616. // 生成数据(需要缓存,并清理缓存)
  617. const pre = (this.ctx.stage.im_pre && this.ctx.stage.im_pre !== '') ? this.ctx.stage.im_pre + this.splitChar : '';
  618. for (const [i, im] of this.ImData.entries()) {
  619. im.im_code = pre + this._getNumberFormat(this.ctx.stage.order, 2) + this.splitChar + this._getNumberFormat(i + 1, 3);
  620. if (im.gclBills) {
  621. for (const b of im.gclBills) {
  622. b.im_code = im.im_code;
  623. }
  624. }
  625. this._getCalcMemo(im);
  626. delete im.leafXmjs;
  627. delete im.gclBills;
  628. this._getChangeInfo(im);
  629. delete im.changes;
  630. }
  631. }
  632. }
  633. module.exports = StageIm;