stage.js 29 KB

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