stage_im.js 52 KB

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