report_memory.js 28 KB

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