stage_im.js 51 KB

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