stage.js 28 KB

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