stage_im.js 52 KB

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