stage_im.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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(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(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_memo_org', 'calc_img_remark', 'calc_img', 'position', 'jldy'];
  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.stage.im_gather_node ? this.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. }
  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. }
  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. _getPegNode(node) {
  157. if (node) {
  158. if (this._checkPeg(node.name)) {
  159. return node;
  160. }
  161. const parent = this.billsTree.getParent(node);
  162. return parent ? this._getPegNode(parent) : null;
  163. }
  164. }
  165. _getDrawingCode(node) {
  166. if (!node) {
  167. return '';
  168. } else if (node.drawing_code) {
  169. return node.drawing_code;
  170. }
  171. const parent = this.billsTree.getParent(node);
  172. return parent ? this._getDrawingCode(parent) : '';
  173. }
  174. _getZlNormalBw(node, peg) {
  175. if (peg) {
  176. const subPeg1 = this._getNodeByLevel(node, peg.level + 1);
  177. const subPeg2 = this._getNodeByLevel(node, peg.level + 2);
  178. let result = peg.name;
  179. if (subPeg1 && subPeg1.id !== peg.id && subPeg1.id !== node.id) {
  180. result = result + mergeChar + subPeg1.name;
  181. if (subPeg2 && subPeg2.id !== subPeg1.id && subPeg2.id !== node.id) {
  182. result = result + mergeChar + subPeg2.name;
  183. }
  184. }
  185. return result;
  186. }
  187. if (node.level === 2 || node.level === 3) {
  188. return node.name;
  189. } else if (node.level >= 4) {
  190. let parent = this.billsTree.getParent(node),
  191. result = parent.name;
  192. while (parent.level > 3 && parent) {
  193. parent = this._getNodeByLevel(node, parent.level - 1);
  194. result = parent.name + mergeChar + result;
  195. }
  196. return result;
  197. }
  198. return '';
  199. }
  200. _getZlGatherBw(node, peg) {
  201. if (peg) {
  202. const subPeg1 = this._getNodeByLevel(node, peg.level + 1);
  203. let result = peg.name;
  204. if (subPeg1 && subPeg1.id !== peg.id) {
  205. result = result + mergeChar + subPeg1.name;
  206. }
  207. return result;
  208. }
  209. if (node.level < 3) {
  210. return node.name;
  211. }
  212. let parent = node,
  213. result = parent.name;
  214. while (parent.level > 3 && parent) {
  215. parent = this._getNodeByLevel(node, parent.level - 1);
  216. result = parent.name + mergeChar + result;
  217. }
  218. return result;
  219. }
  220. _getBwBillsBw(node) {
  221. if (node.level < 3) {
  222. return '';
  223. } else {
  224. let parent = node, result = parent.name;
  225. while (parent.level > 3 && parent) {
  226. parent = this._getNodeByLevel(node, parent.level - 1);
  227. result = parent.name + mergeChar + result;
  228. }
  229. return result;
  230. }
  231. }
  232. /**
  233. * 获取 单位工程
  234. *
  235. * @param xmj - 计量单元(最底层项目节)
  236. * @returns {string}
  237. */
  238. _getDwgc(peg, xmj) {
  239. if (peg) {
  240. return peg.name;
  241. } else {
  242. const node = this._getNodeByLevel(xmj, 2);
  243. return node ? node.name : '';
  244. }
  245. }
  246. /**
  247. * 获取 分部工程
  248. *
  249. * @param peg - 桩号节点
  250. * @param xmj - 计量单元(最底层项目节)
  251. * @returns {string}
  252. */
  253. _getFbgc(peg, xmj) {
  254. if (peg && peg.id !== xmj.id) {
  255. const node = this._getNodeByLevel(xmj, peg.level + 1);
  256. return node ? node.name : '';
  257. } else {
  258. const node = this._getNodeByLevel(xmj, 3);
  259. return node ? node.name : '';
  260. }
  261. }
  262. /**
  263. * 获取 分项工程
  264. *
  265. * @param peg - 桩号节点
  266. * @param xmj - 计量单元(最底层项目节)
  267. * @returns {string}
  268. */
  269. _getFxgc(peg, xmj) {
  270. if (!peg) {
  271. const node = this._getNodeByLevel(xmj, 4);
  272. return node ? node.name : '';
  273. } else if (peg.id === xmj.id) {
  274. if (xmj.level > 4) {
  275. let value = '';
  276. for (let level = 4; level < xmj.level; level++) {
  277. const node = this._getNodeByLevel(xmj, level);
  278. value = value === '' ? node.name : value + mergeChar + node.name;
  279. }
  280. return value;
  281. } else {
  282. return '';
  283. }
  284. } else {
  285. if (peg.level + 2 < xmj.level) {
  286. let value = '';
  287. for (let level = peg.level + 2; level < xmj.level; level++) {
  288. const node = this._getNodeByLevel(xmj, level);
  289. value = value === '' ? node.name : value + mergeChar + node.name;
  290. }
  291. return value;
  292. } else {
  293. return '';
  294. }
  295. }
  296. }
  297. _checkCustomDetail(im) {
  298. const self = this;
  299. let cd;
  300. switch (this.stage.im_type) {
  301. case imType.tz.value:
  302. cd = this._.find(this.details, function (d) {
  303. return im.lid === d.lid &&
  304. (!im.code || im.code === d.code) &&
  305. (!im.name || im.name === d.name) &&
  306. (!im.unit || im.unit === d.unit) &&
  307. (!im.pid || im.pid === d.pid) &&
  308. (!im.pos_name || im.pos_name === d.pos_name);
  309. });
  310. case imType.zl.value:
  311. cd = this._.find(this.details, function (d) {
  312. return im.lid === d.lid &&
  313. (!im.code || im.code === d.code) &&
  314. (!im.name || im.name === d.name) &&
  315. (!im.unit || im.unit === d.unit) &&
  316. self.ctx.helper.checkZero(self.ctx.helper.sub(im.unit_price, d.unit_price)) &&
  317. (!im.pid || im.pid === d.pid) &&
  318. (!im.pos_name || im.pos_name === d.pos_name);
  319. });
  320. case imType.bw.value:
  321. cd = this._.find(this.details, function (d) {
  322. return im.lid === d.lid &&
  323. (!im.code || im.code === d.code) &&
  324. (!im.name || im.name === d.name) &&
  325. (!im.unit || im.unit === d.unit) &&
  326. self.ctx.helper.checkZero(self.ctx.helper.sub(im.unit_price, d.unit_price)) &&
  327. (!im.pid || im.pid === d.pid) &&
  328. (!im.pos_name || im.pos_name === d.pos_name);
  329. });
  330. case imType.bb.value:
  331. cd = this._.find(this.details, function (d) {
  332. return im.lid === d.lid &&
  333. (!im.name || im.name === d.name) &&
  334. (!im.unit || im.unit === d.unit) &&
  335. (!im.pid || im.pid === d.pid) &&
  336. (!im.pos_name || im.pos_name === d.pos_name);
  337. });
  338. }
  339. if (cd) {
  340. im.custom_define = im.custom_define !== null ? (cd.custom_define ? cd.custom_define.split(',') : []) : this.imFields;
  341. this._.assignInWith(im, cd, function(oV, sV, key) { return im.custom_define.indexOf(key) > -1 ? sV : oV; });
  342. }
  343. }
  344. _getCalcMemo(im) {
  345. if (im.gclBills && im.gclBills.length > 0) {
  346. const self = this;
  347. im.gclBills.sort(function (x, y) {
  348. return self.ctx.helper.compareCode(x.b_code, y.b_code);
  349. });
  350. for (const b of im.gclBills) {
  351. b.imid = im.id;
  352. this.ImBillsData.push(b);
  353. }
  354. }
  355. if (im.calc_memo !== undefined && im.calc_memo !== null && im.calc_memo !== '') return;
  356. if (im.leafXmjs && im.leafXmjs.length > 0) {
  357. const memo = ['本期计量:' + (this.ctx.helper.checkZero(im.jl) ? 0 : im.jl) + ' ' + im.unit];
  358. for (const lx of im.leafXmjs) {
  359. for (const p of lx.pos) {
  360. memo.push(p.name + ':' + p.jl + ' ' + im.unit);
  361. }
  362. }
  363. im.calc_memo = memo.join('\n');
  364. } else if (im.gclBills && im.gclBills.length > 0) {
  365. const memo = [];
  366. for (const [i, b] of im.gclBills.entries()) {
  367. if (b.pos && b.pos.length > 0) {
  368. memo.push('清单' + (i + 1) + ':' + b.b_code + ' ' + b.name);
  369. for (const p of b.pos) {
  370. memo.push(p.name + ':' + p.jl + ' ' + b.unit);
  371. }
  372. } else {
  373. memo.push('清单' + (i + 1) + ':' + b.b_code + ' ' + b.name + ':' + (this.ctx.helper.checkZero(b.jl) ? 0 : b.jl) + ' ' + b.unit);
  374. }
  375. }
  376. im.calc_memo = memo.join('\n');
  377. } else {
  378. im.calc_memo = '';
  379. }
  380. }
  381. _getChangeInfo(im) {
  382. if (im.changes && im.changes.length > 0) {
  383. const code = this._.uniq(this._.map(im.changes, 'c_code'));
  384. if (!im.bgl_code || im.bgl_code === '') im.bgl_code = code.join(';');
  385. const new_code = this._.uniq(this._.map(im.changes, 'c_new_code'));
  386. if (!im.bgl_drawing_code || im.bgl_drawing_code === '') im.bgl_drawing_code = new_code.join(';');
  387. }
  388. }
  389. _generateTzPosData(node, gclBills) {
  390. if (!gclBills.pos) {
  391. gclBills.pos = [];
  392. }
  393. const posRange = this.pos.getLedgerPos(node.id);
  394. if (!posRange) { return; }
  395. for (const p of posRange) {
  396. if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.contract_qty)) { continue; }
  397. let lp = this._.find(gclBills.pos, { name: p.name });
  398. if (!lp) {
  399. lp = { name: p.name };
  400. gclBills.pos.push(lp);
  401. }
  402. lp.jl = this.ctx.helper.add(lp.jl, p.gather_qty);
  403. lp.contract_jl = this.ctx.helper.add(lp.contract_jl, p.contract_qty);
  404. lp.qc_jl = this.ctx.helper.add(lp.qc_jl, p.qc_qty);
  405. }
  406. }
  407. _recursiveGenerateTzGclBills(node, im) {
  408. for (const p of node.children) {
  409. if (p.children && p.children.length > 0) {
  410. this._recursiveGenerateTzGclBills(p, im);
  411. } else {
  412. if ((!p.b_code || p.b_code === '') || (p.children && p.children.length > 0)) continue;
  413. if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.contract_tp) &&
  414. this.ctx.helper.checkZero(p.qc_qty) && this.ctx.helper.checkZero(p.qc_tp)) {
  415. continue;
  416. }
  417. let b = this._.find(im.gclBills, { bid: p.id });
  418. if (!b) {
  419. b = { imid: im.id, bid: p.id, b_code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price };
  420. im.gclBills.push(b);
  421. }
  422. b.quantity = this.ctx.helper.add(b.quantity, p.quantity);
  423. b.total_price = this.ctx.helper.add(b.total_price, p.total_price);
  424. b.jl = this.ctx.helper.add(b.jl, p.gather_qty);
  425. b.contract_jl = this.ctx.helper.add(b.contract_jl, p.contract_qty);
  426. b.qc_jl = this.ctx.helper.add(b.qc_jl, p.qc_qty);
  427. b.tp = this.ctx.helper.add(b.tp, p.gather_tp);
  428. b.contract_tp = this.ctx.helper.add(b.contract_tp, p.contract_tp);
  429. b.qc_tp = this.ctx.helper.add(b.qc_tp, p.qc_tp);
  430. b.pre_jl = this.ctx.helper.add(b.pre_jl, p.pre_gather_qty);
  431. b.pre_contract_jl = this.ctx.helper.add(b.pre_contract_jl, p.pre_contract_qty);
  432. b.pre_qc_jl = this.ctx.helper.add(b.pre_qc_jl, p.pre_qc_qty);
  433. b.pre_tp = this.ctx.helper.add(b.pre_tp, p.pre_gather_tp);
  434. b.pre_contract_tp = this.ctx.helper.add(b.pre_contract_tp, p.pre_contract_tp);
  435. b.pre_qc_tp = this.ctx.helper.add(b.pre_qc_tp, p.pre_qc_tp);
  436. b.end_jl = this.ctx.helper.add(b.end_jl, p.end_gather_qty);
  437. b.end_contract_jl = this.ctx.helper.add(b.end_contract_jl, p.end_contract_qty);
  438. b.end_qc_jl = this.ctx.helper.add(b.end_qc_jl, p.end_qc_qty);
  439. b.end_tp = this.ctx.helper.add(b.end_tp, p.end_gather_tp);
  440. b.end_contract_tp = this.ctx.helper.add(b.end_contract_tp, p.end_contract_tp);
  441. b.end_qc_tp = this.ctx.helper.add(b.end_qc_tp, p.end_qc_tp);
  442. this._generateTzPosData(p, b);
  443. }
  444. }
  445. }
  446. _generateTzGclBillsData(node, im) {
  447. if (!im.gclBills) {
  448. im.gclBills = [];
  449. }
  450. this._recursiveGenerateTzGclBills(node, im);
  451. }
  452. _generateTzChangeData(node, im) {
  453. if (!im.changes) {
  454. im.changes = [];
  455. }
  456. const posterity = this.billsTree.getPosterity(node);
  457. for (const p of posterity) {
  458. if (p.children && p.children.length > 0) {
  459. continue;
  460. }
  461. if ((!p.qc_tp || p.qc_tp === 0)) {
  462. continue;
  463. }
  464. const posRange = this.pos.getLedgerPos(p.id);
  465. if (!posRange) {
  466. for (const c of this.changes) {
  467. if (c.lid === p.id && c.pid == -1 && c.qty && c.qty !== 0) {
  468. im.changes.push(c);
  469. }
  470. }
  471. } else {
  472. for (const pp of posRange) {
  473. if ((!pp.qc_qty || pp.qc_qty === 0)) {
  474. continue;
  475. }
  476. for (const c of this.changes) {
  477. if (c.lid === p.id && c.pid === pp.id && c.qty && c.qty !== 0) {
  478. im.changes.push(c);
  479. }
  480. }
  481. }
  482. }
  483. }
  484. }
  485. _checkUsed(node) {
  486. const helper = this.ctx.helper;
  487. if (node.children && node.children.length > 0) {
  488. const posterity = this.billsTree.getPosterity(node);
  489. for (const p of posterity) {
  490. if (p.children && p.children.length > 0) continue;
  491. if (!helper.checkZero(p.contract_qty) || !helper.checkZero(p.contract_tp) ||
  492. !helper.checkZero(p.qc_qty) || !helper.checkZero(p.qc_tp))
  493. return true;
  494. const pPos = this.pos.getLedgerPos(p.id);
  495. if (!pPos || pPos.length === 0) continue;
  496. for (const pp of pPos) {
  497. if (!helper.checkZero(pp.contract_qty) || !helper.checkZero(pp.qc_qty)) return true;
  498. }
  499. }
  500. return false;
  501. } else {
  502. if (!helper.checkZero(node.contract_qty) || !helper.checkZero(node.contract_tp) ||
  503. !helper.checkZero(node.qc_qty) || !helper.checkZero(node.qc_tp))
  504. return true;
  505. const pPos = this.pos.getLedgerPos(node.id);
  506. if (!pPos || pPos.length === 0) return false;
  507. for (const pp of pPos) {
  508. if (!helper.checkZero(pp.contract_qty) || !helper.checkZero(pp.qc_qty)) return true;
  509. }
  510. }
  511. }
  512. /**
  513. * 生成 0号台账 中间计量数据
  514. * @param {Object} node - 生成中间计量表的节点
  515. */
  516. _generateTzImData(node) {
  517. if (!this._checkUsed(node)) return;
  518. const nodeIndex = this.billsTree.getNodeSerialNo(node);
  519. const peg = this._getPegNode(node);
  520. const im = {
  521. id: this.ImData.length + 1,
  522. lid: node.id, pid: '', code: node.code,
  523. jl: node.gather_tp, contract_jl: node.contract_tp, qc_jl: node.qc_tp,
  524. pre_jl: node.pre_gather_tp, pre_contract_jl: node.pre_contract_tp, pre_qc_jl: node.pre_qc_tp,
  525. end_jl: node.end_gather_tp, end_contract_jl: node.end_contract_tp, end_qc_jl: node.end_qc_tp,
  526. peg: peg ? this._getPegStr(peg.name) : '', drawing_code: this._getDrawingCode(node),
  527. dwgc: this._getDwgc(peg, node), fbgc: this._getFbgc(peg, node), fxgc: this._getFxgc(peg, node),
  528. position: '',
  529. lIndex: nodeIndex,
  530. };
  531. if (this.stage.im_gather && node.check) {
  532. im.bw = this._getZlGatherBw(node, peg);
  533. im.xm = '';
  534. } else {
  535. im.bw = this._getZlNormalBw(node, peg);
  536. im.xm = node.name;
  537. }
  538. im.check = this.stage.im_gather && node.check;
  539. this._generateTzGclBillsData(node, im);
  540. this.ImData.push(im);
  541. this._generateTzChangeData(node, im);
  542. }
  543. _addBwBillsGclBills(im, bills, pos) {
  544. const helper = this.ctx.helper;
  545. const tp_decimal = this.ctx.tender.info.decimal.tp;
  546. if (!im.gclBills) im.gclBills = [];
  547. let gcl = im.gclBills.find(function (x) {
  548. return (!bills.b_code || bills.b_code === x.b_code) &&
  549. (!bills.name || bills.name === x.name) &&
  550. (!bills.unit || bills.unit === x.unit) &&
  551. helper.checkZero(helper.sub(bills.unit_price, x.unit_price));
  552. });
  553. if (!gcl) {
  554. gcl = {b_code: bills.b_code, name: bills.name, unit: bills.unit, unit_price: bills.unit_price};
  555. im.gclBills.push(gcl);
  556. }
  557. if (pos) {
  558. gcl.quantity = helper.add(gcl.quantity, pos.quantity);
  559. gcl.total_price = helper.mul(gcl.unit_price, gcl.quantity, tp_decimal);
  560. gcl.jl = helper.add(gcl.jl, pos.gather_qty);
  561. gcl.contract_jl = helper.add(gcl.contract_jl, pos.contract_qty);
  562. gcl.qc_jl = helper.add(gcl.qc_jl, pos.qc_qty);
  563. gcl.tp = helper.mul(gcl.unit_price, gcl.jl, tp_decimal);
  564. gcl.contract_tp = helper.mul(gcl.unit_price, gcl.contract_jl, tp_decimal);
  565. gcl.qc_tp = helper.mul(gcl.unit_price, gcl.qc_jl, tp_decimal);
  566. gcl.pre_jl = helper.add(gcl.pre_jl, pos.pre_gather_qty);
  567. gcl.pre_contract_jl = helper.add(gcl.pre_contract_jl, pos.pre_contract_qty);
  568. gcl.pre_qc_jl = helper.add(gcl.pre_qc_jl, pos.pre_qc_qty);
  569. gcl.pre_tp = helper.mul(gcl.unit_price, gcl.pre_jl, tp_decimal);
  570. gcl.pre_contract_tp = helper.mul(gcl.unit_price, gcl.pre_contract_jl, tp_decimal);
  571. gcl.pre_qc_tp = helper.mul(gcl.unit_price, gcl.pre_qc_jl, tp_decimal);
  572. gcl.end_jl = helper.add(gcl.end_jl, pos.end_gather_qty);
  573. gcl.end_contract_jl = helper.add(gcl.end_contract_jl, pos.end_contract_qty);
  574. gcl.end_qc_jl = helper.add(gcl.end_qc_jl, pos.end_qc_qty);
  575. gcl.end_tp = helper.mul(gcl.unit_price, gcl.end_jl, tp_decimal);
  576. gcl.end_contract_tp = helper.mul(gcl.unit_price, gcl.end_contract_jl, tp_decimal);
  577. gcl.end_qc_tp = helper.mul(gcl.unit_price, gcl.end_qc_jl, tp_decimal);
  578. } else {
  579. gcl.quantity = helper.add(gcl.quantity, bills.quantity);
  580. gcl.total_price = helper.add(gcl.total_price, bills.total_price);
  581. gcl.jl = helper.add(gcl.jl, bills.gather_qty);
  582. gcl.contract_jl = helper.add(gcl.contract_jl, bills.contract_qty);
  583. gcl.qc_jl = helper.add(gcl.qc_jl, bills.qc_qty);
  584. gcl.tp = helper.add(gcl.tp, bills.gather_tp);
  585. gcl.contract_tp = helper.add(gcl.contract_tp, bills.contract_tp);
  586. gcl.qc_tp = helper.add(gcl.qc_tp, bills.qc_tp);
  587. gcl.pre_jl = helper.add(gcl.pre_jl, bills.pre_gather_qty);
  588. gcl.pre_contract_jl = helper.add(gcl.pre_contract_jl, bills.pre_contract_qty);
  589. gcl.pre_qc_jl = helper.add(gcl.pre_qc_jl, bills.pre_qc_qty);
  590. gcl.pre_tp = helper.add(gcl.pre_tp, bills.pre_gather_tp);
  591. gcl.pre_contract_tp = helper.add(gcl.pre_contract_tp, bills.pre_contract_tp);
  592. gcl.pre_qc_tp = helper.add(gcl.pre_qc_tp, bills.pre_qc_tp);
  593. gcl.end_jl = helper.add(gcl.end_jl, bills.end_gather_qty);
  594. gcl.end_contract_jl = helper.add(gcl.end_contract_jl, bills.end_contract_qty);
  595. gcl.end_qc_jl = helper.add(gcl.end_qc_jl, bills.end_qc_qty);
  596. gcl.end_tp = helper.add(gcl.end_tp, bills.end_gather_tp);
  597. gcl.end_contract_tp = helper.add(gcl.end_contract_tp, bills.end_contract_tp);
  598. gcl.end_qc_tp = helper.add(gcl.end_qc_tp, bills.end_qc_tp);
  599. }
  600. }
  601. _calculateBwBillsIm(im) {
  602. const tp_decimal = this.ctx.tender.info.decimal.tp;
  603. im.contract_jl = 0;
  604. im.qc_jl = 0;
  605. for (const b of im.gclBills) {
  606. im.contract_jl = this.ctx.helper.add(im.contract_jl, this.ctx.helper.mul(b.contract_jl, b.unit_price, tp_decimal));
  607. im.qc_jl = this.ctx.helper.add(im.qc_jl, this.ctx.helper.mul(b.qc_jl, b.unit_price, tp_decimal));
  608. }
  609. im.jl = this.ctx.helper.add(im.contract_jl, im.qc_jl);
  610. }
  611. _getBwBillsIm(node, peg, index, bw) {
  612. const im = {
  613. lid: node.id, pid: '', code: node.code, name: node.name, pos_name: '',
  614. peg: peg ? this._getPegStr(peg.name) : '',
  615. drawing_code: this._getDrawingCode(node),
  616. position: '',
  617. lIndex: index,
  618. bw: bw, jldy: node.name,
  619. changes: [], gclBills: [],
  620. dwgc: this._getDwgc(peg, node), fbgc: this._getFbgc(peg, node), fxgc: this._getFxgc(peg, node),
  621. };
  622. return im;
  623. }
  624. _getBwBillsPosIm(imArr, node, peg, index, bw, posName) {
  625. let im = imArr.find(function (d) {
  626. return d.lid === node.id && d.pos_name === posName;
  627. });
  628. if (!im) {
  629. im = {
  630. lid: node.id, pid: '',
  631. code: node.code + '-' + (imArr.length + 1), name: node.name, pos_name: posName,
  632. peg: this._checkPeg(posName) ? posName : (peg ? this._getPegStr(peg.name) : ''),
  633. drawing_code: [], position: [],
  634. lIndex: index,
  635. bw: bw, jldy: posName,
  636. changes: [], gclBills: [],
  637. };
  638. imArr.push(im);
  639. }
  640. return im;
  641. }
  642. _generateBwBillsImData (node) {
  643. if (!this._checkUsed(node)) return;
  644. const nodeIndex = this.billsTree.getNodeSerialNo(node);
  645. const peg = this._getPegNode(node);
  646. const nodeImData = [], posterity = this.billsTree.getPosterity(node);
  647. const bw = this._getBwBillsBw(node);
  648. const imDefault = this._getBwBillsIm(node, peg, nodeIndex, bw);
  649. for (const p of posterity) {
  650. if (p.children && p.children.length > 0) continue;
  651. if (!p.b_code || p.b_code === '') continue;
  652. if (!this._checkUsed(p)) continue;
  653. const pPos = this.pos.getLedgerPos(p.id);
  654. if (pPos && pPos.length > 0) {
  655. for (const pp of pPos) {
  656. if (!pp.gather_qty && !pp.contract_qty && !pp.qc_qty) continue;
  657. const im = this._getBwBillsPosIm(nodeImData, node, peg, nodeIndex, bw, pp.name);
  658. this._addBwBillsGclBills(im, p, pp);
  659. for (const c of this.changes) {
  660. if (c.lid === p.id && c.pid == pp.id && c.qty && c.qty !== 0) {
  661. im.changes.push(c);
  662. }
  663. }
  664. if (pp.drawing_code && im.drawing_code instanceof Array) im.drawing_code.push(pp.drawing_code);
  665. if (pp.position && im.position instanceof Array) im.position.push(pp.position);
  666. }
  667. } else {
  668. for (const c of this.changes) {
  669. if (c.lid === p.id && c.pid == -1 && c.qty && c.qty !== 0) {
  670. imDefault.changes.push(c);
  671. }
  672. }
  673. imDefault.contract_jl = this.ctx.helper.add(imDefault.contract_jl, p.contract_qty);
  674. imDefault.qc_jl = this.ctx.helper.add(imDefault.qc_jl, p.qc_qty);
  675. imDefault.used = true;
  676. this._addBwBillsGclBills(imDefault, p);
  677. }
  678. }
  679. if (imDefault.used) {
  680. imDefault.jl = this.ctx.helper.add(imDefault.contract_jl, imDefault.qc_jl);
  681. imDefault.id = this.ImData.length + 1;
  682. this.ImData.push(imDefault);
  683. }
  684. for (const im of nodeImData) {
  685. this._calculateBwBillsIm(im);
  686. im.drawing_code = im.drawing_code instanceof Array
  687. ? this.ctx.helper._.uniq(im.drawing_code).join(mergeChar) : im.drawing_code;
  688. im.position = im.position instanceof Array
  689. ? this.ctx.helper._.uniq(im.position).join(mergeChar): im.position;
  690. im.id = this.ImData.length + 1;
  691. this.ImData.push(im);
  692. }
  693. }
  694. _generateZlPosData(node, lx) {
  695. if (!lx.pos) {
  696. lx.pos = [];
  697. }
  698. const posRange = this.pos.getLedgerPos(node.id);
  699. if (!posRange) { return; }
  700. for (const p of posRange) {
  701. if (!p.gather_qty || this.ctx.helper.checkZero(p.gather_qty)) { continue; }
  702. let lp = this._.find(lx.pos, { name: p.name });
  703. if (!lp) {
  704. lp = { name: p.name };
  705. lx.pos.push(lp);
  706. }
  707. lp.jl = this.ctx.helper.add(lp.jl, p.gather_qty);
  708. lp.contract_jl = this.ctx.helper.add(lp.contract_jl, p.contract_qty);
  709. lp.qc_jl = this.ctx.helper.add(lp.qc_jl, p.qc_qty);
  710. }
  711. }
  712. _generateZlLeafXmjData(node, im) {
  713. if (!im.leafXmjs) {
  714. im.leafXmjs = [];
  715. }
  716. const leafXmj = this.billsTree.getLeafXmjParent(node);
  717. if (!leafXmj) { return; }
  718. let lx = this._.find(im.leafXmjs, { lxid: leafXmj.id });
  719. if (!lx) {
  720. lx = {
  721. lxid: leafXmj.id,
  722. code: leafXmj.code,
  723. name: leafXmj.name,
  724. };
  725. im.leafXmjs.push(lx);
  726. }
  727. lx.jl = this.ctx.helper.add(lx.jl, node.gather_qty);
  728. lx.contract_jl = this.ctx.helper.add(lx.contract_jl, node.contract_qty);
  729. lx.qc_jl = this.ctx.helper.add(lx.qc_jl, node.qc_qty);
  730. this._generateZlPosData(node, lx);
  731. }
  732. _generateZlChangeData(node, im) {
  733. if (!im.changes) {
  734. im.changes = [];
  735. }
  736. if ((!node.qc_qty || node.qc_qty === 0)) {
  737. return;
  738. }
  739. const posRange = this.pos.getLedgerPos(node.id);
  740. if (!posRange) {
  741. for (const c of this.changes) {
  742. if (c.lid === node.id && c.pid == -1 && c.qty && c.qty !== 0) {
  743. im.changes.push(c);
  744. }
  745. }
  746. } else {
  747. for (const p of posRange) {
  748. if ((!p.qc_qty || p.qc_qty === 0)) {
  749. continue;
  750. }
  751. for (const c of this.changes) {
  752. if (c.lid === node.id && c.pid === p.id && c.qty && c.qty !== 0) {
  753. im.changes.push(c);
  754. }
  755. }
  756. }
  757. }
  758. }
  759. /**
  760. * 生成 总量控制 中间计量数据
  761. * @param {Object} node - 生成中间计量表的节点
  762. */
  763. _generateZlImData(node) {
  764. const self = this;
  765. const nodeImData = [], posterity = this.billsTree.getPosterity(node);
  766. const nodeIndex = this.billsTree.getNodeSerialNo(node);
  767. for (const p of posterity) {
  768. if (p.children && p.children.length > 0) { continue; }
  769. if (!p.b_code || p.b_code === '') { continue; }
  770. if (!this._checkUsed(p)) { continue; }
  771. // if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.qc_qty)) { continue; }
  772. let im = nodeImData.find(function(d) {
  773. return d.lid === node.id &&
  774. d.code === p.b_code && p.name === d.name && p.unit === d.unit &&
  775. self.ctx.helper.checkZero(self.ctx.helper.sub(p.unit_price, d.unit_price));
  776. });
  777. if (!im) {
  778. const peg = this._getPegNode(node);
  779. im = {
  780. id: this.ImData.length + 1,
  781. lid: node.id, pid: '', code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price,
  782. quantity: 0, total_price: 0,
  783. jl: 0, contract_jl: 0, qc_jl: 0,
  784. pre_jl: 0, pre_contract_jl: 0, pre_qc_jl: 0,
  785. end_jl: 0, end_contract_jl: 0, end_qc_jl: 0,
  786. tp: 0, contract_tp: 0, qc_tp: 0,
  787. pre_tp: 0, pre_contract_tp: 0, pre_qc_tp: 0,
  788. end_tp: 0, end_contract_tp: 0, end_qc_tp: 0,
  789. peg: peg ? this._getPegStr(peg.name) : '',
  790. position: '',
  791. lIndex: nodeIndex,
  792. dwgc: this._getDwgc(peg, node), fbgc: this._getFbgc(peg, node), fxgc: this._getFxgc(peg, node),
  793. };
  794. if (this.stage.im_gather && node.check) {
  795. im.check = true;
  796. im.bw = this._getZlGatherBw(node, peg);
  797. im.xm = '';
  798. im.drawing_code = this._getDrawingCode(node);
  799. } else {
  800. im.check = false;
  801. im.bw = this._getZlNormalBw(node, peg);
  802. im.xm = node.name;
  803. im.drawing_code = this._getDrawingCode(p);
  804. }
  805. nodeImData.push(im);
  806. this.ImData.push(im);
  807. }
  808. // if (!this.stage.im_gather || !node.check) {
  809. this._generateZlLeafXmjData(p, im, 'gather_qty');
  810. // }
  811. this._generateZlChangeData(p, im);
  812. im.quantity = this.ctx.helper.add(im.quantity, p.quantity);
  813. im.total_price = this.ctx.helper.add(im.total_price, p.total_price);
  814. im.jl = this.ctx.helper.add(im.jl, p.gather_qty);
  815. im.contract_jl = this.ctx.helper.add(im.contract_jl, p.contract_qty);
  816. im.qc_jl = this.ctx.helper.add(im.qc_jl, p.qc_qty);
  817. im.tp = this.ctx.helper.add(im.tp, p.gather_tp);
  818. im.contract_tp = this.ctx.helper.add(im.contract_tp, p.contract_tp);
  819. im.qc_tp = this.ctx.helper.add(im.qc_tp, p.qc_tp);
  820. im.pre_jl = this.ctx.helper.add(im.pre_jl, p.pre_gather_qty);
  821. im.pre_contract_jl = this.ctx.helper.add(im.pre_contract_jl, p.pre_contract_qty);
  822. im.pre_qc_jl = this.ctx.helper.add(im.pre_qc_jl, p.pre_qc_qty);
  823. im.pre_tp = this.ctx.helper.add(im.pre_tp, p.pre_gather_tp);
  824. im.pre_contract_tp = this.ctx.helper.add(im.pre_contract_tp, p.pre_contract_tp);
  825. im.pre_qc_tp = this.ctx.helper.add(im.pre_qc_tp, p.pre_qc_tp);
  826. im.end_jl = this.ctx.helper.add(im.end_jl, p.end_gather_qty);
  827. im.end_contract_jl = this.ctx.helper.add(im.end_contract_jl, p.end_contract_qty);
  828. im.end_qc_jl = this.ctx.helper.add(im.end_qc_jl, p.end_qc_qty);
  829. im.end_tp = this.ctx.helper.add(im.end_tp, p.end_gather_tp);
  830. im.end_contract_tp = this.ctx.helper.add(im.end_contract_tp, p.end_contract_tp);
  831. im.end_qc_tp = this.ctx.helper.add(im.end_qc_tp, p.end_qc_tp);
  832. }
  833. }
  834. _generateBwImData(node) {
  835. const tp_decimal = this.ctx.tender.info.decimal.tp;
  836. const posterity = this.billsTree.getPosterity(node);
  837. for (const p of posterity) {
  838. if (p.children && p.children.length > 0) { continue; }
  839. if (!p.b_code || p.b_code === '') { continue; }
  840. const peg = this._getPegNode(node);
  841. const pPos = this.pos.getLedgerPos(p.id);
  842. const bw = this._getZlNormalBw(node, peg);
  843. if (pPos && pPos.length > 0) {
  844. const nodeIndex = this.billsTree.getNodeSerialNo(node);
  845. for (const pp of pPos) {
  846. if (this.ctx.helper.checkZero(pp.contract_qty) && this.ctx.helper.checkZero(pp.qc_qty)) { continue; }
  847. const im = {
  848. id: this.ImData.length + 1,
  849. lid: node.id, code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price, pid: pp.id,
  850. quantity: pp.quantity,
  851. jl: pp.gather_qty, contract_jl: pp.contract_qty, qc_jl: pp.qc_qty,
  852. pre_jl: pp.pre_gather_qty, pre_contract_jl: pp.pre_contract_qty, pre_qc_jl: pp.pre_qc_qty,
  853. end_jl: pp.end_gather_qty, end_contract_jl: pp.end_contract_qty, end_qc_jl: pp.end_qc_qty,
  854. bw,
  855. peg: this._checkPeg(pp.name) ? this._getPegStr(pp.name) : (peg ? this._getPegStr(peg.name) : ''),
  856. dwgc: this._getDwgc(peg, node), fbgc: this._getFbgc(peg, node), fxgc: this._getFxgc(peg, node),
  857. xm: pp.name, jldy: pp.name,
  858. drawing_code: pp.drawing_code,
  859. changes: [],
  860. position: pp.position,
  861. lIndex: nodeIndex * 1000 + pp.porder,
  862. };
  863. im.total_price = this.ctx.helper.mul(im.quantity, im.unit_price, tp_decimal);
  864. im.tp = this.ctx.helper.mul(im.jl, im.unit_price, tp_decimal);
  865. im.contract_tp = this.ctx.helper.mul(im.contract_jl, im.unit_price, tp_decimal);
  866. im.qc_tp = this.ctx.helper.mul(im.qc_jl, im.unit_price, tp_decimal);
  867. im.pre_tp = this.ctx.helper.mul(im.pre_jl, im.unit_price, tp_decimal);
  868. im.pre_contract_tp = this.ctx.helper.mul(im.pre_contract_jl, im.unit_price, tp_decimal);
  869. im.pre_qc_tp = this.ctx.helper.mul(im.pre_qc_jl, im.unit_price, tp_decimal);
  870. im.end_tp = this.ctx.helper.mul(im.end_jl, im.unit_price, tp_decimal);
  871. im.end_contract_tp = this.ctx.helper.mul(im.end_contract_jl, im.unit_price, tp_decimal);
  872. im.end_qc_tp = this.ctx.helper.mul(im.end_qc_jl, im.unit_price, tp_decimal);
  873. im.calc_memo = '本期计量:' + (this.ctx.helper.checkZero(im.jl) ? 0 : im.jl) + ' ' + im.unit;
  874. this.ImData.push(im);
  875. if (pp.qc_qty && pp.qc_qty !== 0) {
  876. for (const c of this.changes) {
  877. if (c.lid === p.id && c.pid === pp.id && c.qty && c.qty !== 0) {
  878. im.changes.push(c);
  879. }
  880. }
  881. }
  882. }
  883. } else {
  884. if (this.ctx.helper.checkZero(p.gather_qty) && this.ctx.helper.checkZero(p.gather_tp)) { continue; }
  885. const im = {
  886. id: this.ImData.length + 1,
  887. lid: node.id, code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price, pid: '',
  888. quantity: p.quantity, total_price: p.total_price,
  889. jl: p.gather_qty, contract_jl: p.contract_qty, qc_jl: p.qc_qty,
  890. pre_jl: p.pre_gather_qty, pre_contract_jl: p.pre_contract_qty, pre_qc_jl: p.pre_qc_qty,
  891. end_jl: p.end_gather_qty, end_contract_jl: p.end_contract_qty, end_qc_jl: p.end_qc_qty,
  892. tp: p.gather_tp, contract_tp: p.contract_tp, qc_tp: p.qc_tp,
  893. pre_tp: p.pre_gather_tp, pre_contract_tp: p.pre_contract_tp, pre_qc_tp: p.pre_qc_tp,
  894. end_tp: p.end_gather_tp, end_contract_tp: p.end_contract_tp, end_qc_tp: p.end_qc_tp,
  895. bw,
  896. peg: peg ? this._getPegStr(peg.name) : '',
  897. dwgc: this._getDwgc(peg, node), fbgc: this._getFbgc(peg, node), fxgc: this._getFxgc(peg, node),
  898. xm: node.name,
  899. drawing_code: this._getDrawingCode(p),
  900. changes: [],
  901. position: '',
  902. lIndex: this.billsTree.getNodeSerialNo(node),
  903. };
  904. im.calc_memo = '本期计量:' + (this.ctx.helper.checkZero(im.jl) ? 0 : im.jl) + ' ' + im.unit;
  905. this.ImData.push(im);
  906. if (p.qc_qty && p.qc_qty !== 0) {
  907. for (const c of this.changes) {
  908. if (c.lid === p.id && c.pid == -1 && c.qty && c.qty !== 0) {
  909. im.changes.push(c);
  910. }
  911. }
  912. }
  913. }
  914. }
  915. }
  916. /**
  917. * 递归 生成中间计量表
  918. * @param {Array} nodes
  919. */
  920. _recursiveBuildImData(nodes) {
  921. if (!nodes || nodes.length === 0) { return; }
  922. for (const node of nodes) {
  923. if (this.billsTree.isLeafXmj(node) ||
  924. ((this.stage.im_type !== imType.bw.value && this.stage.im_type !== imType.bb.value) && this.stage.im_gather && node.check)
  925. ) {
  926. if (this.stage.im_type === imType.tz.value) {
  927. this._generateTzImData(node);
  928. } else if (this.stage.im_type === imType.zl.value) {
  929. this._generateZlImData(node);
  930. } else if (this.stage.im_type === imType.bw.value) {
  931. this._generateBwImData(node);
  932. } else if (this.stage.im_type === imType.bb.value) {
  933. this._generateBwBillsImData(node);
  934. }
  935. } else {
  936. this._recursiveBuildImData(node.children);
  937. }
  938. }
  939. }
  940. // 生成中间计量数据
  941. async buildImData() {
  942. const self = this;
  943. this.stage = this.ctx.stage;
  944. // 初始化
  945. await this._loadMainData();
  946. await this._loadRelaData();
  947. if (this.stage.im_gather) {
  948. this._checkGather();
  949. }
  950. // 生成数据
  951. this._recursiveBuildImData(this.billsTree.children);
  952. // 排序
  953. if (this.stage.im_type !== imType.tz.value) {
  954. this.ImData.sort(function(x, y) {
  955. const iCode = self.ctx.helper.compareCode(x.code, y.code);
  956. return iCode === 0 ? x.lIndex - y.lIndex : iCode;
  957. });
  958. }
  959. // 生成数据(需要缓存,并清理缓存)
  960. const pre = (this.stage.im_pre && this.stage.im_pre !== '') ? this.stage.im_pre + this.splitChar : '';
  961. for (const [i, im] of this.ImData.entries()) {
  962. im.im_code = pre + this._getNumberFormat(this.stage.order, 2) + this.splitChar + this._getNumberFormat(i + this.stage.im_start_num + 1, 3);
  963. if (im.gclBills) {
  964. for (const b of im.gclBills) {
  965. b.im_code = im.im_code;
  966. }
  967. }
  968. this._getCalcMemo(im);
  969. this._checkCustomDetail(im);
  970. delete im.leafXmjs;
  971. delete im.gclBills;
  972. this._getChangeInfo(im);
  973. delete im.changes;
  974. }
  975. }
  976. buildRelaStageIm(stage, billsTree, pos, details, changes) {
  977. this.billsTree = billsTree;
  978. this.pos = pos;
  979. this.stage = stage;
  980. this.details = details;
  981. this.changes = changes;
  982. const self = this;
  983. // 初始化
  984. if (stage.im_gather) this._checkGather();
  985. // 生成数据
  986. this._recursiveBuildImData(this.billsTree.children);
  987. // 排序
  988. if (this.stage.im_type !== imType.tz.value) {
  989. this.ImData.sort(function(x, y) {
  990. const iCode = self.ctx.helper.compareCode(x.code, y.code);
  991. return iCode === 0 ? x.lIndex - y.lIndex : iCode;
  992. });
  993. }
  994. // 生成数据(需要缓存,并清理缓存)
  995. const pre = (this.stage.im_pre && this.stage.im_pre !== '') ? this.stage.im_pre + this.splitChar : '';
  996. for (const [i, im] of this.ImData.entries()) {
  997. im.im_code = pre + this._getNumberFormat(this.stage.order, 2) + this.splitChar + this._getNumberFormat(i + 1, 3);
  998. if (im.gclBills) {
  999. for (const b of im.gclBills) {
  1000. b.im_code = im.im_code;
  1001. }
  1002. }
  1003. this._getCalcMemo(im);
  1004. this._checkCustomDetail(im);
  1005. delete im.leafXmjs;
  1006. delete im.gclBills;
  1007. this._getChangeInfo(im);
  1008. delete im.changes;
  1009. }
  1010. }
  1011. }
  1012. module.exports = StageIm;