stage_im.js 53 KB

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