stage.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. 'use strict';
  2. /**
  3. * 期计量 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. const payConst = require('../const/deal_pay.js');
  11. const roleRelSvr = require('./role_rpt_rel');
  12. const fs = require('fs');
  13. const path = require('path');
  14. const _ = require('lodash');
  15. module.exports = app => {
  16. class Stage extends app.BaseService {
  17. /**
  18. * 构造函数
  19. *
  20. * @param {Object} ctx - egg全局变量
  21. * @return {void}
  22. */
  23. constructor(ctx) {
  24. super(ctx);
  25. this.tableName = 'stage';
  26. }
  27. /**
  28. * 根据id查找数据
  29. *
  30. * @param {Number} id - 数据库中的id
  31. * @return {Object} - 返回单条数据
  32. */
  33. async getDataById(id) {
  34. const result = await this.db.get(this.tableName, { id });
  35. if (result) { result.tp_history = result.tp_history ? JSON.parse(result.tp_history) : []; }
  36. return result;
  37. }
  38. /**
  39. * 根据条件查找单条数据
  40. *
  41. * @param {Object} condition - 筛选条件
  42. * @return {Object} - 返回单条数据
  43. */
  44. async getDataByCondition(condition) {
  45. const result = await this.db.get(this.tableName, condition);
  46. if (result) { result.tp_history = result.tp_history ? JSON.parse(result.tp_history) : []; }
  47. return result;
  48. }
  49. /**
  50. * 根据条件查找列表数据
  51. *
  52. * @param {Object} condition - 筛选条件
  53. * @return {Array} - 返回数据
  54. */
  55. async getAllDataByCondition(condition) {
  56. const result = await this.db.select(this.tableName, condition);
  57. for (const r of result) {
  58. r.tp_history = r.tp_history ? JSON.parse(r.tp_history) : [];
  59. }
  60. return result;
  61. }
  62. async doCheckStage(stage) {
  63. const status = auditConst.status;
  64. stage.auditors = await this.ctx.service.stageAudit.getAuditors(stage.id, stage.times);
  65. stage.curAuditor = await this.ctx.service.stageAudit.getCurAuditor(stage.id, stage.times);
  66. const accountId = this.ctx.session.sessionUser.accountId,
  67. auditorIds = this._.map(stage.auditors, 'aid'),
  68. shareIds = [];
  69. const permission = this.ctx.session.sessionUser.permission;
  70. if (accountId === stage.user_id) { // 原报
  71. if (stage.curAuditor) {
  72. stage.readOnly = stage.curAuditor.aid !== accountId;
  73. } else {
  74. stage.readOnly = stage.status !== status.uncheck && stage.status !== status.checkNo;
  75. }
  76. stage.curTimes = stage.times;
  77. if (stage.status === status.uncheck || stage.status === status.checkNo) {
  78. stage.curOrder = 0;
  79. } else if (stage.status === status.checked) {
  80. stage.curOrder = this._.max(this._.map(stage.auditors, 'order'));
  81. } else {
  82. stage.curOrder = stage.curAuditor.aid === accountId ? stage.curAuditor.order : stage.curAuditor.order - 1;
  83. }
  84. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  85. if (stage.status === status.uncheck) {
  86. throw '您无权查看该数据';
  87. }
  88. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  89. if (stage.status === status.checked) {
  90. stage.curOrder = this._.max(this._.map(stage.auditors, 'order'));
  91. } else if (stage.status === status.checkNo) {
  92. const audit = await this.service.stageAudit.getDataByCondition({
  93. sid: stage.id, times: stage.times - 1, status: status.checkNo,
  94. });
  95. stage.curOrder = audit.order;
  96. } else {
  97. stage.curOrder = accountId === stage.curAuditor.aid ? stage.curAuditor.order : stage.curAuditor.order - 1;
  98. }
  99. stage.readOnly = (stage.status !== status.checking && stage.status !== status.checkNoPre) || accountId !== stage.curAuditor.aid;
  100. } else if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) { // 分享人
  101. if (stage.status === status.uncheck) {
  102. throw '您无权查看该数据';
  103. }
  104. stage.readOnly = true;
  105. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  106. if (stage.status === status.checkNo) {
  107. const audit = await this.service.stageAudit.getDataByCondition({
  108. sid: stage.id, times: stage.times - 1, status: status.checkNo,
  109. });
  110. stage.curOrder = audit.order;
  111. } else {
  112. stage.curOrder = stage.status === status.checked ? this._.max(this._.map(stage.auditors, 'order')) : stage.curAuditor.order - 1;
  113. }
  114. }
  115. let time = stage.readOnly ? stage.cache_time_r : stage.cache_time_l;
  116. if (!time) {
  117. time = stage.in_time ? stage.in_time : new Date();
  118. }
  119. stage.cacheTime = time.getTime();// this.ctx.stage.readOnly ? (this.ctx.stage.cache_time_r).getTime(): (this.ctx.stage.cache_time_l).getTime();
  120. return stage;
  121. }
  122. async checkStage(sid) {
  123. if (!this.ctx.stage) {
  124. const stage = await this.ctx.service.stage.getDataById(sid);
  125. await this.doCheckStage(stage);
  126. this.ctx.stage = stage;
  127. }
  128. }
  129. /**
  130. * 获取 最新一期 期计量
  131. * @param tenderId
  132. * @param includeUnCheck
  133. * @return {Promise<*>}
  134. */
  135. async getLastestStage(tenderId, includeUnCheck = false) {
  136. this.initSqlBuilder();
  137. this.sqlBuilder.setAndWhere('tid', {
  138. value: tenderId,
  139. operate: '=',
  140. });
  141. if (!includeUnCheck) {
  142. this.sqlBuilder.setAndWhere('status', {
  143. value: auditConst.status.uncheck,
  144. operate: '!=',
  145. });
  146. }
  147. this.sqlBuilder.orderBy = [['order', 'desc']];
  148. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  149. const stage = await this.db.queryOne(sql, sqlParam);
  150. if (stage) stage.tp_history = stage.tp_history ? JSON.parse(stage.tp_history) : [];
  151. return stage;
  152. }
  153. /**
  154. * 获取 最新一期 审批完成的 期计量
  155. * @param tenderId
  156. * @return {Promise<*>}
  157. */
  158. async getLastestCompleteStage(tenderId) {
  159. this.initSqlBuilder();
  160. this.sqlBuilder.setAndWhere('tid', {
  161. value: tenderId,
  162. operate: '=',
  163. });
  164. this.sqlBuilder.setAndWhere('status', {
  165. value: auditConst.status.checked,
  166. operate: '=',
  167. });
  168. this.sqlBuilder.orderBy = [['order', 'desc']];
  169. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  170. const stage = await this.db.queryOne(sql, sqlParam);
  171. if (stage) stage.tp_history = stage.tp_history ? JSON.parse(stage.tp_history) : [];
  172. return stage;
  173. }
  174. /**
  175. * 获取标段下的期列表(报表选择用,只需要一些key信息,不需要计算详细数据,也懒得一个个字段写了,用*来处理)
  176. * @param tenderId
  177. * @return {Promise<void>}
  178. */
  179. async getValidStagesShort(tenderId) {
  180. const sql = 'select * from zh_stage where tid = ? order by zh_stage.order';
  181. const sqlParam = [tenderId];
  182. return await this.db.query(sql, sqlParam);
  183. }
  184. /**
  185. * 获取某一期信息(报表用)
  186. * @param stageId
  187. * @return {Promise<void>}
  188. */
  189. async getStageById(stageId) {
  190. const sql = 'select * from zh_stage where id = ? order by zh_stage.order';
  191. const sqlParam = [stageId];
  192. return await this.db.query(sql, sqlParam);
  193. }
  194. async checkStageGatherData(stage) {
  195. // 最新一期计量(未审批完成),当前操作人的期详细数据,应实时计算
  196. if (stage.status !== auditConst.status.checked) {
  197. await this.doCheckStage(stage);
  198. if (!stage.readOnly && stage.check_calc) {
  199. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  200. stage.contract_tp = tpData.contract_tp;
  201. stage.qc_tp = tpData.qc_tp;
  202. const tp = await this.ctx.service.stagePay.getSpecialTotalPrice(stage);
  203. stage.yf_tp = tp.yf;
  204. stage.sf_tp = tp.sf;
  205. await this.update({
  206. check_calc: false,
  207. contract_tp: stage.contract_tp, qc_tp: stage.qc_tp,
  208. yf_tp: stage.yf_tp, sf_tp: stage.sf_tp,
  209. }, { id: stage.id });
  210. } else if (stage.tp_history) {
  211. const his = this.ctx.helper._.find(stage.tp_history, { times: stage.curTimes, order: stage.curOrder });
  212. // console.log(his);
  213. if (his) {
  214. stage.contract_tp = his.contract_tp;
  215. stage.qc_tp = his.qc_tp;
  216. stage.yf_tp = his.yf_tp;
  217. stage.sf_tp = his.sf_tp;
  218. stage.tp = this.ctx.helper.add(stage.contract_tp, stage.qc_tp);
  219. }
  220. }
  221. }
  222. }
  223. /**
  224. * 获取标段下的全部计量期,按倒序
  225. * @param tenderId
  226. * @return {Promise<void>}
  227. */
  228. async getValidStages(tenderId) {
  229. const stages = await this.db.select(this.tableName, {
  230. where: { tid: tenderId },
  231. orders: [['order', 'desc']],
  232. });
  233. for (const s of stages) {
  234. s.tp = this.ctx.helper.add(s.contract_tp, s.qc_tp);
  235. s.pre_tp = this.ctx.helper.add(s.pre_contract_tp, s.pre_qc_tp);
  236. s.end_tp = this.ctx.helper.add(s.pre_tp, s.tp);
  237. s.tp_history = s.tp_history ? JSON.parse(s.tp_history) : [];
  238. }
  239. if (stages.length !== 0) {
  240. const lastStage = stages[0];
  241. if (lastStage.status === auditConst.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
  242. stages.splice(0, 1);
  243. }
  244. }
  245. // 最新一期计量(未审批完成),当前操作人的期详细数据,应实时计算
  246. if (stages.length === 0) return stages;
  247. if (stages[0].status !== auditConst.status.checked) {
  248. const stage = stages[0];
  249. await this.doCheckStage(stage);
  250. if (!stage.readOnly) {
  251. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  252. stage.contract_tp = tpData.contract_tp;
  253. stage.qc_tp = tpData.qc_tp;
  254. const tp = await this.ctx.service.stagePay.getSpecialTotalPrice(stage);
  255. stage.yf_tp = tp.yf;
  256. stage.sf_tp = tp.sf;
  257. stage.tp = this.ctx.helper.add(stage.contract_tp, stage.qc_tp);
  258. stage.end_tp = this.ctx.helper.add(stage.pre_tp, stage.tp);
  259. } else {
  260. const his = this.ctx.helper._.find(stage.tp_history, { times: stage.curTimes, order: stage.curOrder });
  261. if (his) {
  262. stage.contract_tp = his.contract_tp;
  263. stage.qc_tp = his.qc_tp;
  264. stage.yf_tp = his.yf_tp;
  265. stage.sf_tp = his.sf_tp;
  266. stage.tp = this.ctx.helper.add(stage.contract_tp, stage.qc_tp);
  267. stage.end_tp = this.ctx.helper.add(stage.pre_tp, stage.tp);
  268. }
  269. }
  270. }
  271. for (const s of stages) {
  272. if (s.yf_tp && s.sf_tp === 0) {
  273. const sf = await this.ctx.service.stagePay.getHistorySf(s);
  274. if (sf && s.readOnly) {
  275. await this.ctx.service.stage.update({ sf_tp: sf.tp, pre_sf_tp: sf.pre_tp }, { id: s.id });
  276. }
  277. s.sf_tp = sf.tp;
  278. }
  279. }
  280. return stages;
  281. }
  282. async _getSumTp(condition, ...field) {
  283. const fieldSql = [];
  284. for (const f of field) {
  285. fieldSql.push('SUM(`' + f + '`) as ' + '`' + f + '`');
  286. }
  287. const sql = 'SELECT ' + fieldSql.join(', ') + ' FROM ' + this.tableName + ' ' + this.ctx.helper.whereSql(condition);
  288. return await this.db.queryOne(sql);
  289. }
  290. /**
  291. *
  292. * @param tenderId - 标段id
  293. * @param date - 计量年月
  294. * @param period - 开始-截止日期
  295. * @return {Promise<void>}
  296. */
  297. async addStage(tenderId, date, period) {
  298. const stages = await this.getAllDataByCondition({
  299. where: { tid: tenderId },
  300. order: ['order'],
  301. });
  302. const preStage = stages[stages.length - 1];
  303. if (stages.length > 0 && stages[stages.length - 1].status !== auditConst.status.checked) {
  304. throw '上一期未审批通过,请等待上一期审批通过后,再新增数据';
  305. }
  306. const order = stages.length + 1;
  307. const newStage = {
  308. sid: this.uuid.v4(),
  309. tid: tenderId,
  310. order,
  311. in_time: new Date(),
  312. s_time: date,
  313. period,
  314. times: 1,
  315. status: auditConst.status.uncheck,
  316. user_id: this.ctx.session.sessionUser.accountId,
  317. check_calc: false,
  318. };
  319. newStage.cache_time_l = newStage.in_time;
  320. newStage.cache_time_r = newStage.in_time;
  321. if (preStage) {
  322. newStage.im_type = preStage.im_type;
  323. newStage.im_pre = preStage.im_pre;
  324. newStage.im_gather = preStage.im_gather;
  325. newStage.im_gather_node = preStage.im_gather_node;
  326. newStage.pre_contract_tp = this.ctx.helper.add(preStage.pre_contract_tp, preStage.contract_tp);
  327. newStage.pre_qc_tp = this.ctx.helper.add(preStage.pre_qc_tp, preStage.qc_tp);
  328. newStage.pre_yf_tp = this.ctx.helper.add(preStage.pre_yf_tp, preStage.yf_tp);
  329. if (preStage.order === 1 || preStage.pre_sf_tp) {
  330. newStage.pre_sf_tp = this.ctx.helper.add(preStage.pre_sf_tp, preStage.sf_tp);
  331. } else {
  332. const sumTp = await this._getSumTp({tid: preStage.tid}, 'sf_tp');
  333. newStage.pre_sf_tp = sumTp.sf_tp || 0;
  334. }
  335. }
  336. const transaction = await this.db.beginTransaction();
  337. try {
  338. // 新增期记录
  339. const result = await transaction.insert(this.tableName, newStage);
  340. if (result.affectedRows === 1) {
  341. newStage.id = result.insertId;
  342. } else {
  343. throw '新增期数据失败';
  344. }
  345. // 存在上一期时,复制上一期审批流程
  346. if (preStage) {
  347. const auditResult = await this.ctx.service.stageAudit.copyPreStageAuditors(transaction, preStage, newStage);
  348. if (!auditResult) {
  349. throw '复制上一期审批流程失败';
  350. }
  351. }
  352. // 新增期合同支付数据
  353. const dealResult = await this.ctx.service.stagePay.addInitialStageData(newStage, transaction);
  354. if (!dealResult) {
  355. throw '新增期合同支付数据失败';
  356. }
  357. // 新增期其他台账数据
  358. if (preStage) {
  359. const jgclResult = await this.ctx.service.stageJgcl.addInitialStageData(newStage, preStage, transaction);
  360. if (!jgclResult) throw '初始化甲供材料数据失败';
  361. const otherResult = await this.ctx.service.stageOther.addInitialStageData(newStage, preStage, transaction);
  362. if (!otherResult) throw '初始化其他台账数据失败';
  363. }
  364. // 新增期拷贝报表相关配置/签名角色 等
  365. if (preStage) {
  366. const rptResult = await this.ctx.service.rptCustomDefine.addInitialStageData(newStage, preStage, transaction);
  367. await this.ctx.service.roleRptRel.addInitialStageData(tenderId, newStage, preStage);
  368. }
  369. await transaction.commit();
  370. return newStage;
  371. } catch (err) {
  372. await transaction.rollback();
  373. throw err;
  374. }
  375. }
  376. /**
  377. * 编辑计量期
  378. *
  379. * @param {Number} tenderId - 标段Id
  380. * @param {Number} order - 第N期
  381. * @param {String} date - 计量年月
  382. * @param {String} period - 开始-截止时间
  383. * @return {Promise<void>}
  384. */
  385. async saveStage(tenderId, order, date, period) {
  386. await this.db.update(this.tableName, {
  387. s_time: date,
  388. period,
  389. }, { where: { tid: tenderId, order } });
  390. }
  391. /**
  392. * 设置 中间计量 生成规则,并生成数据
  393. * @param {Number} tenderId - 标段id
  394. * @param {Number} order - 期序号
  395. * @param {Number} data - 中间计量生成规则
  396. * @return {Promise<void>}
  397. */
  398. async buildDetailData(tenderId, order, data) {
  399. const conn = await this.db.beginTransaction();
  400. try {
  401. await conn.update(this.tableName, { im_type: data.im_type, im_pre: data.im_pre }, { where: { tid: tenderId, order } });
  402. // to do 生成中间计量数据
  403. await conn.commit();
  404. } catch (err) {
  405. await conn.rollback();
  406. throw err;
  407. }
  408. }
  409. /**
  410. * 获取 当期的 计算基数
  411. * @return {Promise<any>}
  412. */
  413. async getStagePayCalcBase(stage, tenderInfo) {
  414. const calcBase = JSON.parse(JSON.stringify(payConst.calcBase));
  415. const param = tenderInfo.deal_param;
  416. for (const cb of calcBase) {
  417. const sum = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  418. switch (cb.code) {
  419. case 'htj':
  420. cb.value = param.contractPrice;
  421. break;
  422. case 'zlje':
  423. cb.value = param.zanLiePrice;
  424. break;
  425. case 'htjszl':
  426. cb.value = this.ctx.helper.sub(param.contractPrice, param.zanLiePrice);
  427. break;
  428. case 'kgyfk':
  429. cb.value = param.startAdvance;
  430. break;
  431. case 'clyfk':
  432. cb.value = param.materialAdvance;
  433. break;
  434. case 'bqwc':
  435. cb.value = this.ctx.helper.add(sum.contract_tp, sum.qc_tp);
  436. break;
  437. case 'bqht':
  438. cb.value = sum.contract_tp;
  439. break;
  440. case 'bqbg':
  441. cb.value = sum.qc_tp;
  442. break;
  443. case 'ybbqwc':
  444. const sumGcl = await this.ctx.service.stageBills.getSumTotalPriceGcl(stage, '^[^0-9]*1[0-9]{2}-');
  445. cb.value = this.ctx.helper.add(sumGcl.contract_tp, sumGcl.qc_tp);
  446. break;
  447. default:
  448. cb.value = 0;
  449. }
  450. }
  451. return calcBase;
  452. }
  453. async updateCheckCalcFlag(stage, check) {
  454. const result = await this.db.update(this.tableName, { id: stage.id, check_calc: check });
  455. return result.affectedRows === 1;
  456. }
  457. async updateCacheTime(sid) {
  458. const result = await this.db.update(this.tableName, { id: sid, cache_time_l: new Date() });
  459. return result.affectedRows === 1;
  460. }
  461. /**
  462. * 删除计量期
  463. *
  464. * @param {Number} id - 期Id
  465. * @return {Promise<void>}
  466. */
  467. async deleteStage(id) {
  468. const transaction = await this.db.beginTransaction();
  469. try {
  470. const stageInfo = await this.getDataById(id);
  471. await transaction.delete(this.tableName, { id });
  472. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: id });
  473. await transaction.delete(this.ctx.service.stageAudit.tableName, { sid: id });
  474. await transaction.delete(this.ctx.service.stageBills.tableName, { sid: id });
  475. await transaction.delete(this.ctx.service.stageChange.tableName, { sid: id });
  476. await transaction.delete(this.ctx.service.stagePos.tableName, { sid: id });
  477. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid: id });
  478. await transaction.delete(this.ctx.service.stagePosFinal.tableName, { sid: id });
  479. await transaction.delete(this.ctx.service.stageBillsFinal.tableName, { sid: id });
  480. // 删除计量合同支付附件
  481. const payList = await this.ctx.service.stagePay.getAllDataByCondition({ where: { sid: id } });
  482. if (payList) {
  483. for (const pt of payList) {
  484. if (pt.attachment !== null && pt.attachment !== '') {
  485. const payAttList = JSON.parse(pt.attachment);
  486. for (const pat of payAttList) {
  487. if (fs.existsSync(path.join(this.app.baseDir, pat.filepath))) {
  488. await fs.unlinkSync(path.join(this.app.baseDir, pat.filepath));
  489. }
  490. }
  491. }
  492. }
  493. }
  494. await transaction.delete(this.ctx.service.stagePay.tableName, { sid: id });
  495. await transaction.delete(this.ctx.service.pay.tableName, { csid: id });
  496. // 删除计量附件文件
  497. const attList = await this.ctx.service.stageAtt.getAllDataByCondition({ where: { tid: stageInfo.tid, sid: stageInfo.order } });
  498. if (attList.length !== 0) {
  499. for (const att of attList) {
  500. if (fs.existsSync(path.join(this.app.baseDir, att.filepath))) {
  501. await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  502. }
  503. }
  504. }
  505. await transaction.delete(this.ctx.service.stageAtt.tableName, { tid: stageInfo.tid, sid: stageInfo.order });
  506. // 其他台账
  507. await transaction.delete(this.ctx.service.stageJgcl.tableName, { sid: id });
  508. const bonus = await this.ctx.service.stageBonus.getStageData(id);
  509. if (bonus && bonus.length > 0) {
  510. for (const b of bonus) {
  511. for (const f of b.proof_file) {
  512. if (fs.existsSync(path.join(this.app.baseDir, f.filepath))) {
  513. await fs.unlinkSync(path.join(this.app.baseDir, f.filepath));
  514. }
  515. }
  516. }
  517. }
  518. await transaction.delete(this.ctx.service.stageBonus.tableName, { sid: id });
  519. await transaction.delete(this.ctx.service.stageOther.tableName, { sid: id });
  520. await transaction.commit();
  521. return true;
  522. } catch (err) {
  523. await transaction.rollback();
  524. throw err;
  525. }
  526. }
  527. /**
  528. * 获取 多期的 计算基数 -(材料调差调用)
  529. * @return {Promise<any>}
  530. */
  531. async getMaterialCalcBase(stage_list, tenderInfo) {
  532. const calcBase = JSON.parse(JSON.stringify(payConst.calcBase));
  533. const param = tenderInfo.deal_param;
  534. const sum = await this.ctx.service.stageBills.getSumTotalPriceByMaterial(stage_list);
  535. for (const cb of calcBase) {
  536. switch (cb.code) {
  537. case 'htj':
  538. cb.value = param.contractPrice;
  539. break;
  540. case 'zlje':
  541. cb.value = param.zanLiePrice;
  542. break;
  543. case 'htjszl':
  544. cb.value = this.ctx.helper.sub(param.contractPrice, param.zanLiePrice);
  545. break;
  546. case 'kgyfk':
  547. cb.value = param.startAdvance;
  548. break;
  549. case 'clyfk':
  550. cb.value = param.materialAdvance;
  551. break;
  552. case 'bqwc':
  553. cb.value = this.ctx.helper.add(sum.contract_tp, sum.qc_tp);
  554. break;
  555. case 'bqht':
  556. cb.value = sum.contract_tp;
  557. break;
  558. case 'bqbg':
  559. cb.value = sum.qc_tp;
  560. break;
  561. case 'ybbqwc':
  562. const sumGcl = await this.ctx.service.stageBills.getSumTotalPriceGclByMaterial(stage_list, '^[^0-9]*1[0-9]{2}-');
  563. cb.value = this.ctx.helper.add(sumGcl.contract_tp, sumGcl.qc_tp);
  564. break;
  565. case 'bqyf':
  566. cb.value = this.ctx.helper.roundNum(this._.sumBy(stage_list, 'yf_tp'), 2);
  567. break;
  568. default:
  569. cb.value = 0;
  570. }
  571. }
  572. return calcBase;
  573. }
  574. async getSumbqyfByMaterial(stage) {
  575. }
  576. /**
  577. * 获取必要的stage信息调用curTimes, curOrder, id , times, curAuditor(材料调差)
  578. * @param stage_id_list
  579. * @return {Promise<void>}
  580. */
  581. async getStageMsgByStageId(stage_id_list) {
  582. const list = [];
  583. stage_id_list = stage_id_list.split(',');
  584. for (const sid of stage_id_list) {
  585. const stage = await this.getDataById(sid);
  586. stage.auditors = await this.service.stageAudit.getAuditors(stage.id, stage.times);
  587. stage.curAuditor = await this.service.stageAudit.getCurAuditor(stage.id, stage.times);
  588. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  589. stage.curTimes = stage.times;
  590. list.push(stage);
  591. }
  592. return list;
  593. }
  594. }
  595. return Stage;
  596. };