report_memory.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const _ = require('lodash');
  10. const StageIm = require('../lib/stage_im');
  11. const imType = require('../const/tender').imType;
  12. const audit = require('../const/audit');
  13. // const path = require('path');
  14. // const fs = require('fs');
  15. const stageImTz = 'mem_stage_im_tz';
  16. const stageImTzBills = 'mem_stage_im_tz_bills';
  17. const stageImZl = 'mem_stage_im_zl';
  18. const stageImVersion = '1.0';
  19. const Ledger = require('../lib/ledger');
  20. const curFields = ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp', 'postil'];
  21. const preFields = ['pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp'];
  22. const endFields = ['end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp'];
  23. const finalFields = ['final_tp', 'final_ratio'];
  24. const stageFields = curFields.concat(preFields, endFields, finalFields);
  25. const stageEndFields = preFields.concat(endFields, finalFields);
  26. const bglFields = ['qc_bgl_code'];
  27. module.exports = app => {
  28. class ReportMemory extends app.BaseService {
  29. /**
  30. * 构造函数
  31. *
  32. * @param {Object} ctx - egg全局context
  33. * @return {void}
  34. */
  35. constructor(ctx) {
  36. super(ctx);
  37. const self = this;
  38. this.tableName = 'report_memory';
  39. // 基础数据类
  40. // mainData
  41. this.billsTree = new Ledger.billsTree(this.ctx, {
  42. id: 'ledger_id',
  43. pid: 'ledger_pid',
  44. order: 'order',
  45. level: 'level',
  46. rootId: -1,
  47. keys: ['id', 'tender_id', 'ledger_id'],
  48. stageId: 'id',
  49. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp'],
  50. calc: function (node) {
  51. if (node.children && node.children.length === 0) {
  52. node.pre_gather_qty = self.ctx.helper.add(node.pre_contract_qty, node.pre_qc_qty);
  53. node.gather_qty = self.ctx.helper.add(node.contract_qty, node.qc_qty);
  54. node.end_contract_qty = self.ctx.helper.add(node.pre_contract_qty, node.contract_qty);
  55. node.end_qc_qty = self.ctx.helper.add(node.pre_qc_qty, node.qc_qty);
  56. node.end_gather_qty = self.ctx.helper.add(node.pre_gather_qty, node.gather_qty);
  57. }
  58. node.pre_gather_tp = self.ctx.helper.add(node.pre_contract_tp, node.pre_qc_tp);
  59. node.gather_tp = self.ctx.helper.add(node.contract_tp, node.qc_tp);
  60. node.end_contract_tp = self.ctx.helper.add(node.pre_contract_tp, node.contract_tp);
  61. node.end_qc_tp = self.ctx.helper.add(node.pre_qc_tp, node.qc_tp);
  62. node.end_gather_tp = self.ctx.helper.add(node.pre_gather_tp, node.gather_tp);
  63. node.final_tp = self.ctx.helper.add(node.total_price, node.end_qc_tp);
  64. node.final_ratio = self.ctx.helper.mul(self.ctx.helper.div(node.end_gather_tp, node.final_tp, 4), 100);
  65. }
  66. });
  67. this.pos = new Ledger.pos({
  68. id: 'id', ledgerId: 'lid',
  69. updateFields: ['contract_qty', 'qc_qty', 'postil'],
  70. calc: function (p) {
  71. p.pre_gather_qty = ctx.helper.add(p.pre_contract_qty, p.pre_qc_qty);
  72. p.gather_qty = ctx.helper.add(p.contract_qty, p.qc_qty);
  73. p.end_contract_qty = self.ctx.helper.add(p.pre_contract_qty, p.contract_qty);
  74. p.end_qc_qty = self.ctx.helper.add(p.pre_qc_qty, p.qc_qty);
  75. p.end_gather_qty = self.ctx.helper.add(p.pre_gather_qty, p.gather_qty);
  76. }
  77. });
  78. // 需要缓存的数据
  79. this.stageImData = null;
  80. }
  81. _checkFieldsExist(source, check) {
  82. for (const s of source) {
  83. if (check.indexOf(s) >= 0) {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. // build-time: 162-384ms, redis-cache: 0-41ms, mysql + IO: 116-146ms
  90. // 一定程度上算是大Value缓存,数据多了以后:
  91. // 1. 达到redis内存阈值时,数据会swap到磁盘,此时将消耗IO时间
  92. // 2. redis单独服务器
  93. // 3. redis集群
  94. async _getReportMemoryCache(name, tid, sid, time, version = '') {
  95. // redis
  96. const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '') + version;
  97. const data = await this.cache.get(cacheKey);
  98. if (data) {
  99. return eval(data);
  100. } else {
  101. return null;
  102. }
  103. // mysql + IO
  104. // const rm = await this.getDataByCondition({
  105. // tid: tid, sid: sid, name: name, time: time
  106. // });
  107. // if (rm && rm.file) {
  108. // const file = path.join(this.ctx.app.config.filePath, 'report', 'cache', rm.file);
  109. // if (fs.existsSync(file)) {
  110. // const data = await fs.readFileSync(file, 'utf8');
  111. // return eval(data);
  112. // } else {
  113. // return null;
  114. // }
  115. // }
  116. }
  117. async _setReportMemoryCache(name, tid, sid, time, data, version = '') {
  118. // redis
  119. const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '') + version;
  120. this.cache.set(cacheKey, JSON.stringify(data), 'EX', this.ctx.app.config.cacheTime);
  121. // mysql + IO
  122. // const file = path.join('report', 'cache', 'rm' + (new Date()).getTime() + '.json');
  123. // await this.ctx.helper.saveBufferFile(JSON.stringify(data), path.join(this.ctx.app.config.filePath, file));
  124. // const rm = await this.getDataByCondition({
  125. // tid: tid, sid: sid, name: name, time: time
  126. // });
  127. // if (rm) {
  128. // await this.db.update(this.tableName, {id: rm.id, file: file});
  129. // } else {
  130. // await this.db.insert(this.tableName, {tid: tid, sid: sid, name: name, time: time, file: file});
  131. // }
  132. }
  133. async _generateStageIm(tid, sid, isTz = true) {
  134. if (isTz && this.ctx.stage.im_type !== imType.tz.value) {
  135. throw '您查看的报表跟设置不符,请查看“总量控制”的报表';
  136. } else if (!isTz && this.ctx.stage.im_type === imType.tz.value) {
  137. throw '您查看的报表跟设置不符,请查看“0号台账”的报表';
  138. }
  139. const stageIm = new StageIm(this.ctx);
  140. await stageIm.buildImData();
  141. this.stageImData.main = stageIm.ImData;
  142. if (isTz) {
  143. this.stageImData.bills = stageIm.ImBillsData;
  144. await this._setReportMemoryCache(stageImTz, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main, stageImVersion);
  145. await this._setReportMemoryCache(stageImTzBills, tid, sid, this.ctx.stage.cacheTime, this.stageImData.bills, stageImVersion);
  146. } else {
  147. await this._setReportMemoryCache(stageImZl, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main, stageImVersion);
  148. }
  149. }
  150. async getStageImTzNoReturn(tid, sid) {
  151. // 备注:单独拎出以下几行代码一个是为了提高效率(跟getStageImTzDataDirectlyByKey方法协作使用)
  152. // 二是如果出现并行查询(台账及台账清单)情况下,会出现干扰(已验证过),导致数据丢失
  153. if (!this.stageImData) {
  154. this.stageImData = {};
  155. }
  156. try {
  157. await this._generateStageIm(tid, sid);
  158. } catch (err) {
  159. this.stageImData.main = [];
  160. this.stageImData.bills = [];
  161. }
  162. }
  163. getStageImTzDataDirectlyByKey(key) {
  164. let rst = [];
  165. if (key === 'mem_stage_im_tz') {
  166. rst = this.stageImData.main;
  167. } else {
  168. rst = this.stageImData.bills;
  169. }
  170. return rst;
  171. }
  172. async getStageImTzData(tid, sid, fields) {
  173. await this.ctx.service.tender.checkTender(tid);
  174. await this.ctx.service.stage.checkStage(sid);
  175. const cache = await this._getReportMemoryCache('mem_stage_im_tz', tid, sid, this.ctx.stage.cacheTime, stageImVersion);
  176. if (cache) {
  177. // console.log('cache');
  178. return cache;
  179. }
  180. // console.log('build');
  181. if (!this.stageImData) {
  182. this.stageImData = {};
  183. try {
  184. await this._generateStageIm(tid, sid);
  185. } catch (err) {
  186. if (err.statck) {
  187. this.ctx.logger.error(err);
  188. }
  189. this.stageImData.main = err.statck ? '数据错误' : err;
  190. this.stageImData.bills = this.stageImData.main;
  191. }
  192. }
  193. return this.stageImData.main;
  194. }
  195. async getStageImTzBillsData(tid, sid, fields) {
  196. await this.ctx.service.tender.checkTender(tid);
  197. await this.ctx.service.stage.checkStage(sid);
  198. const cache = await this._getReportMemoryCache('mem_stage_im_tz_bills', tid, sid, this.ctx.stage.cacheTime, stageImVersion);
  199. if (cache) return cache;
  200. if (!this.stageImData) {
  201. this.stageImData = {};
  202. try {
  203. await this._generateStageIm(tid, sid);
  204. } catch (err) {
  205. if (err.statck) {
  206. this.ctx.logger.error(err);
  207. }
  208. this.stageImData.main = err.statck ? '数据错误' : err;
  209. this.stageImData.bills = this.stageImData.main;
  210. }
  211. }
  212. return this.stageImData.bills;
  213. }
  214. async getStageImZlData(tid, sid, fields) {
  215. await this.ctx.service.tender.checkTender(tid);
  216. await this.ctx.service.stage.checkStage(sid);
  217. const cache = await this._getReportMemoryCache('mem_stage_im_zl', tid, sid, this.ctx.stage.cacheTime, stageImVersion);
  218. if (cache) return cache;
  219. this.stageImData = {};
  220. try {
  221. await this._generateStageIm(tid, sid, false);
  222. } catch (err) {
  223. if (err.statck) {
  224. this.ctx.logger.error(err);
  225. }
  226. this.stageImData.main = err.statck ? '数据错误' : err;
  227. }
  228. return this.stageImData.main;
  229. }
  230. async getMonthProgress(tid, fields) {
  231. const helper = this.ctx.helper;
  232. await this.ctx.service.tender.checkTender(tid);
  233. const tender = this.ctx.tender;
  234. const stages = await this.ctx.service.stage.getValidStages(tender.id);
  235. const lastStage = stages.length > 0 ? stages[0] : null;
  236. if (lastStage) {
  237. await this.ctx.service.stage.checkStageGatherData(lastStage);
  238. tender.gather_tp = helper.add(lastStage.contract_tp, lastStage.qc_tp);
  239. tender.end_contract_tp = helper.add(lastStage.contract_tp, lastStage.pre_contract_tp);
  240. tender.end_qc_tp = helper.add(lastStage.qc_tp, lastStage.pre_qc_tp);
  241. tender.end_gather_tp = helper.add(tender.end_contract_tp, tender.end_qc_tp);
  242. tender.pre_gather_tp = helper.add(lastStage.pre_contract_tp, lastStage.pre_qc_tp);
  243. tender.yf_tp = lastStage.yf_tp;
  244. tender.qc_ratio = helper.mul(helper.div(tender.end_qc_tp, tender.info.deal_param.contractPrice, 2), 100);
  245. tender.sum = helper.add(tender.total_price, tender.end_qc_tp);
  246. tender.pre_ratio = helper.mul(helper.div(tender.pre_gather_tp, tender.sum, 2), 100);
  247. tender.cur_ratio = helper.mul(helper.div(tender.gather_tp, tender.sum, 2), 100);
  248. tender.other_tp = helper.sub(helper.sub(tender.sum, tender.pre_gather_tp), tender.gather_tp);
  249. tender.other_ratio = Math.max(0, 100 - tender.pre_ratio - tender.cur_ratio);
  250. }
  251. const monthProgress = [];
  252. for (const s of stages) {
  253. if (s.s_time) {
  254. let progress = monthProgress.find(function (x) {
  255. return x.month === s.s_time;
  256. });
  257. if (!progress) {
  258. progress = {month: s.s_time};
  259. monthProgress.push(progress);
  260. }
  261. progress.tp = helper.add(helper.add(progress.tp, s.contract_tp), s.qc_tp);
  262. }
  263. }
  264. monthProgress.sort(function (x, y) {
  265. return Date.parse(x.month) - Date.parse(y.month);
  266. });
  267. let sum = 0;
  268. for (const p of monthProgress) {
  269. p.ratio = helper.mul(helper.div(p.tp, tender.sum, 4), 100);
  270. sum = helper.add(sum, p.tp);
  271. p.end_tp = sum;
  272. p.end_ratio = helper.mul(helper.div(p.end_tp, tender.sum, 4), 100);
  273. }
  274. return monthProgress;
  275. }
  276. async _calcBillsBgl() {
  277. if (!this.ctx.stage) return;
  278. const helper = this.ctx.helper;
  279. const tender = this.ctx.tender;
  280. const stage = this.ctx.stage;
  281. const bglData = this.ctx.stage.readOnly
  282. ? await this.ctx.service.stageChange.getAuditorAllStageData(tender.id, stage.id, stage.curTimes, stage.curOrder)
  283. : await this.ctx.service.stageChange.getLastestAllStageData(tender.id, stage.id);
  284. for (const node of this.billsTree.nodes) {
  285. node.qc_bgl_code = '';
  286. if (node.children && node.children.length > 0) continue;
  287. const nodeBgl = helper._.filter(bglData, {lid: node.id});
  288. if (nodeBgl.length === 0) continue;
  289. helper._.pullAll(bglData, nodeBgl);
  290. const validBgl = helper._.filter(nodeBgl, function (x) {
  291. return !helper.checkZero(x.qty);
  292. });
  293. node.qc_bgl_code = helper._.uniq(helper._.map(validBgl, 'c_code')).join(';');
  294. }
  295. }
  296. async getStageBillsData(tid, sid, fields) {
  297. await this.ctx.service.tender.checkTender(tid);
  298. if (sid) {
  299. await this.ctx.service.stage.checkStage(sid);
  300. }
  301. const billsData = await this.ctx.service.ledger.getData(this.ctx.tender.id);
  302. if (this._checkFieldsExist(fields, stageFields)) {
  303. if (this.ctx.stage.readOnly) {
  304. const curStage = await this.ctx.service.stageBills.getAuditorStageData(this.ctx.tender.id,
  305. this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
  306. this.ctx.helper.assignRelaData(billsData, [
  307. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  308. ]);
  309. } else {
  310. const curStage = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id);
  311. this.ctx.helper.assignRelaData(billsData, [
  312. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  313. ]);
  314. }
  315. }
  316. if (this._checkFieldsExist(fields, preFields)) {
  317. const preStage = this.ctx.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
  318. this.ctx.helper.assignRelaData(billsData, [
  319. {data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid'}
  320. ]);
  321. }
  322. this.billsTree.loadDatas(billsData);
  323. this.billsTree.calculateAll();
  324. if (this._checkFieldsExist(fields, bglFields)) {
  325. await this._calcBillsBgl();
  326. }
  327. return this.billsTree.getDatas([
  328. 'id', 'tender_id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf',
  329. 'code', 'b_code', 'name', 'unit', 'unit_price',
  330. 'deal_qty', 'deal_tp',
  331. 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'quantity', 'total_price',
  332. 'dgn_qty1', 'dgn_qty2',
  333. 'drawing_code', 'memo', 'node_type', 'is_tp',
  334. 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp', 'postil',
  335. 'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp',
  336. 'end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp',
  337. 'final_tp', 'final_ratio',
  338. 'qc_bgl_code',
  339. 'chapter',
  340. ]);
  341. }
  342. async getStagePosData(tid, sid, fields) {
  343. await this.ctx.service.tender.checkTender(tid);
  344. await this.ctx.service.stage.checkStage(sid);
  345. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: this.ctx.tender.id }});
  346. if (this.ctx.stage.readOnly) {
  347. const curPosStage = await this.ctx.service.stagePos.getAuditorStageData2(this.ctx.tender.id,
  348. this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
  349. this.ctx.helper.assignRelaData(posData, [
  350. {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'}
  351. ]);
  352. } else {
  353. const curPosStage = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
  354. this.ctx.helper.assignRelaData(posData, [
  355. {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'}
  356. ]);
  357. }
  358. const prePosStage = this.ctx.stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
  359. this.ctx.helper.assignRelaData(posData, [
  360. {data: prePosStage, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid'}
  361. ]);
  362. this.pos.loadDatas(posData);
  363. this.pos.calculateAll();
  364. return this.pos.getDatas();
  365. }
  366. _getStageValidRole () {
  367. if (!this.ctx.stage) throw '期数据错误,请重试';
  368. const result = [{dataOrder: 0, flowOrder: 0, uid: this.ctx.stage.user_id}];
  369. for (const auditor of this.ctx.stage.auditors) {
  370. if (auditor.status === audit.stage.status.checked ||
  371. (auditor.status === audit.stage.status.checking && !this.ctx.stage.readOnly)) {
  372. const role = result.find(function (r) {
  373. return r.uid === auditor.aid;
  374. });
  375. if (role) {
  376. role.dataOrder = auditor.order;
  377. } else {
  378. result.push({
  379. dataOrder: auditor.order,
  380. flowOrder: result.length,
  381. uid: auditor.aid
  382. })
  383. }
  384. }
  385. }
  386. return result;
  387. };
  388. async getStageBillsCompareData(tid, sid, fields) {
  389. await this.ctx.service.tender.checkTender(tid);
  390. await this.ctx.service.stage.checkStage(sid);
  391. const stage = this.ctx.stage, helper = this.ctx.helper;
  392. const validRole = this._getStageValidRole();
  393. const billsData = await this.ctx.service.ledger.getData(this.ctx.tender.id);
  394. const allStageBills = await this.ctx.service.stageBills.getAllDataByCondition({where: {sid: sid}});
  395. const stageBillsIndex = {}, timesLen = 100;
  396. for (const role of validRole) {
  397. const stageBills = this.ctx.helper._.filter(allStageBills, function (x) {
  398. return x.times < stage.curTimes || (x.times === stage.curTimes && x.order <= role.dataOrder);
  399. });
  400. this.ctx.helper._.pullAll(allStageBills, stageBills);
  401. for (const sb of stageBills) {
  402. const key = 'sb-' + sb.lid;
  403. const sbi = stageBillsIndex[key];
  404. if (sbi) {
  405. if ((sbi.times * timesLen + sbi.order) < (sb.times * timesLen + sb.order)) stageBillsIndex[key] = sb;
  406. } else {
  407. stageBillsIndex[key] = sb;
  408. }
  409. }
  410. const filterStageBills = [];
  411. for (const prop in stageBillsIndex) {
  412. filterStageBills.push(stageBillsIndex[prop]);
  413. }
  414. this.ctx.helper.assignRelaData(billsData, [
  415. {data: filterStageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'r' + role.flowOrder + '_', relaId: 'lid'}
  416. ]);
  417. }
  418. if (this._checkFieldsExist(fields, preFields)) {
  419. const preStage = this.ctx.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
  420. this.ctx.helper.assignRelaData(billsData, [
  421. {data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid'}
  422. ]);
  423. }
  424. this.billsTree.loadDatas(billsData);
  425. this.billsTree.setting.calcFields = ['deal_tp', 'total_price', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'];
  426. for (const role of validRole) {
  427. const prefix = 'r' + role.flowOrder + '_';
  428. this.billsTree.setting.calcFields.push(prefix + 'contract_tp', prefix + 'qc_tp', prefix + 'gather_tp');
  429. }
  430. this.billsTree.calculateAll(function(node) {
  431. let prefix = '';
  432. if (node.children && node.children.length === 0) {
  433. node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty);
  434. for (const role of validRole) {
  435. prefix = 'r' + role.flowOrder + '_';
  436. node[prefix + 'gather_qty'] = helper.add(node[prefix + 'contract_qty'], node[prefix + 'qc_qty']);
  437. }
  438. }
  439. node.pre_gather_tp = helper.add(node.pre_contract_tp, node.pre_qc_tp);
  440. for (const role of validRole) {
  441. prefix = 'r' + role.flowOrder + '_';
  442. node[prefix + 'gather_tp'] = helper.add(node[prefix + 'contract_tp'], node[prefix + 'qc_tp']);
  443. }
  444. });
  445. return this.billsTree.getDefaultDatas();
  446. // return this.billsTree.getDatas([
  447. // 'id', 'tender_id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf', //8
  448. // 'code', 'b_code', 'name', 'unit', 'unit_price', //5
  449. // 'deal_qty', 'deal_tp', 'quantity', 'total_price', 'dgn_qty1', 'dgn_qty2', //6
  450. // 'drawing_code', 'memo', 'node_type', 'is_tp', //4
  451. // 'r0_contract_qty', 'r0_contract_tp', 'r0_qc_qty', 'r0_qc_tp', 'r0_gather_qty', 'r0_gather_tp', //6
  452. // 'r1_contract_qty', 'r1_contract_tp', 'r1_qc_qty', 'r1_qc_tp', 'r1_gather_qty', 'r1_gather_tp',
  453. // 'r2_contract_qty', 'r2_contract_tp', 'r2_qc_qty', 'r2_qc_tp', 'r2_gather_qty', 'r2_gather_tp',
  454. // 'r3_contract_qty', 'r3_contract_tp', 'r3_qc_qty', 'r3_qc_tp', 'r3_gather_qty', 'r3_gather_tp',
  455. // 'r4_contract_qty', 'r4_contract_tp', 'r4_qc_qty', 'r4_qc_tp', 'r4_gather_qty', 'r4_gather_tp',
  456. // 'r5_contract_qty', 'r5_contract_tp', 'r5_qc_qty', 'r5_qc_tp', 'r5_gather_qty', 'r5_gather_tp',
  457. // 'r6_contract_qty', 'r6_contract_tp', 'r6_qc_qty', 'r6_qc_tp', 'r6_gather_qty', 'r6_gather_tp',
  458. // 'r7_contract_qty', 'r7_contract_tp', 'r7_qc_qty', 'r7_qc_tp', 'r7_gather_qty', 'r7_gather_tp',
  459. // 'r8_contract_qty', 'r8_contract_tp', 'r8_qc_qty', 'r8_qc_tp', 'r8_gather_qty', 'r8_gather_tp',
  460. // 'r9_contract_qty', 'r9_contract_tp', 'r9_qc_qty', 'r9_qc_tp', 'r9_gather_qty', 'r9_gather_tp',
  461. // 'r10_contract_qty', 'r10_contract_tp', 'r10_qc_qty', 'r10_qc_tp', 'r10_gather_qty', 'r10_gather_tp',
  462. // 'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp',
  463. // 'chapter', //1
  464. // ]);
  465. }
  466. async getStagePayData(tid, sid, fields) {
  467. await this.ctx.service.tender.checkTender(tid);
  468. await this.ctx.service.stage.checkStage(sid);
  469. const dealPay = await this.ctx.service.stagePay.getStagePays(ctx.stage);
  470. if (ctx.stage.order > 1) {
  471. renderData.pre = await ctx.service.stageBillsFinal.getSumTotalPrice(ctx.stage.tid, ctx.stage.order - 1);
  472. renderData.pre.gather_tp = ctx.helper.add(renderData.pre.contract_tp, renderData.pre.qc_tp);
  473. } else {
  474. renderData.pre = {contract_tp: null, qc_tp: null, gather_tp: null};
  475. }
  476. if (!this.ctx.stage.readOnly && this.ctx.stage.check_calc) {
  477. // 计算 本期金额
  478. const PayCalculator = require('../lib/pay_calc');
  479. const payCalculator = new PayCalculator(this.ctx, this.ctx.stage, this.ctx.tender.info);
  480. await payCalculator.calculateAll(dealPay);
  481. }
  482. }
  483. }
  484. return ReportMemory;
  485. };