stage_im.js 28 KB

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