stage_im.js 48 KB

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