stage.js 30 KB

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