stage_im.js 50 KB

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