stage_im.js 56 KB

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