pay_controller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. 'use strict';
  2. /**
  3. * 合同支付
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const audit = require('../const/audit');
  10. module.exports = app => {
  11. class PayController extends app.BaseController {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. }
  21. async index(ctx) {
  22. try {
  23. const phasePays = await this.ctx.service.phasePay.getAllPhasePay(ctx.tender.id, 'DESC');
  24. const relaStage = [];
  25. for (const p of phasePays) {
  26. // todo 加载当前审批人
  27. // if (p.audit_status !== checked) await this.ctx.service.phasePay.loadUser(p);
  28. p.curAuditors = [];
  29. relaStage.push(...p.rela_stage);
  30. }
  31. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: ctx.tender.id }, orders: [['order', 'AEC']] });
  32. const validStages = stages.filter(s => {
  33. return !relaStage.find(r => { return s.id === r.id; });
  34. });
  35. this.ctx.service.phasePay.calculatePhasePay(phasePays);
  36. const renderData = {
  37. phasePays,
  38. validStages,
  39. auditConst: audit.common,
  40. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.phasePay.list)
  41. };
  42. await this.layout('phase_pay/index.ejs', renderData, 'phase_pay/modal.ejs');
  43. } catch (err) {
  44. ctx.helper.log(err);
  45. }
  46. }
  47. async add(ctx) {
  48. try {
  49. if (ctx.session.sessionUser.accountId !== ctx.tender.data.user_id && ctx.tender.userAssistsId.indexOf(ctx.session.sessionUser.accountId) < 0) {
  50. throw '您无权创建计量期';
  51. }
  52. const date = ctx.request.body.date;
  53. if (!date) throw '请选择支付年月';
  54. const stage = ctx.request.body.stage;
  55. if (!stage) throw '请选择计量期';
  56. const memo = ctx.request.body.memo;
  57. const pays = await ctx.service.phasePay.getAllPhasePay(ctx.tender.id, 'DESC');
  58. const unCompleteCount = pays.filter(s => { return s.status !== audit.common.status.checked; }).length;
  59. if (unCompleteCount.length > 0) throw `最新一起未审批通过,请审批通过后再新增`;
  60. // 预留可以关联多期
  61. const stages = await ctx.service.stage.getAllDataByCondition({ where: { tid: ctx.tender.id, order: stage } });
  62. const newPhase = await ctx.service.phasePay.add(ctx.tender.id, stages, date, memo);
  63. if (!newPhase) throw '新增期失败';
  64. newPhase.curTimes = 1;
  65. newPhase.curOrder = 0;
  66. await ctx.service.phasePayDetail.calculateSave(newPhase);
  67. ctx.redirect('/tender/' + ctx.tender.id + '/pay' + newPhase.phase_order);
  68. } catch (err) {
  69. this.log(err);
  70. ctx.postError(err, '新增期失败');
  71. ctx.redirect(ctx.request.header.referer);
  72. }
  73. }
  74. async del() {
  75. }
  76. async save() {
  77. }
  78. async detail(ctx) {
  79. try {
  80. // await this.ctx.service.phasePayDetail.calculateSave(ctx.phasePay);
  81. const pays = await this.ctx.service.phasePayDetail.getDetailData(ctx.phasePay);
  82. const calcBase = this.ctx.service.phasePay.getPhasePayCalcBase(ctx.phasePay, ctx.tender.info);
  83. const projectFunInfo = await this.ctx.service.project.getFunRela(ctx.session.sessionProject.id);
  84. const lastStage = await this.ctx.service.stage.getLastestCompleteStage(ctx.tender.id);
  85. const renderData = {
  86. pays,
  87. calcBase,
  88. lockPayExpr: projectFunInfo.lockPayExpr,
  89. auditConst: audit.common,
  90. deadlineType: this.ctx.service.phasePayDetail.deadlineType,
  91. maxStageOrder: lastStage.order,
  92. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.phasePay.detail)
  93. };
  94. await this.layout('phase_pay/detail.ejs', renderData, 'phase_pay/detail_modal.ejs');
  95. } catch (err) {
  96. ctx.helper.log(err);
  97. ctx.postError(err, '读取合同支付数据错误');
  98. ctx.redirect(ctx.request.headers.referer);
  99. }
  100. }
  101. async detailUpdate(ctx) {
  102. try {
  103. const data = JSON.parse(ctx.request.body.data);
  104. if (!data.postType || !data.postData) throw '数据错误';
  105. const responseData = { err: 0, msg: '', data: {} };
  106. switch (data.postType) {
  107. case 'add':
  108. responseData.data = await this.ctx.service.phasePayDetail.addDetailNode(ctx.phasePay, data.postData.id, data.postData.count || 1);
  109. break;
  110. case 'delete':
  111. await this.ctx.service.phasePayDetail.deleteDetailNode(ctx.phasePay, data.postData.id, data.postData.count || 1);
  112. await this.ctx.service.phasePayDetail.calculateSave(ctx.phasePay);
  113. responseData.data.reload = await this.ctx.service.phasePayDetail.getDetailData(ctx.phasePay);
  114. break;
  115. case 'up-move':
  116. responseData.data = await this.ctx.service.phasePayDetail.upMoveDetailNode(ctx.phasePay, data.postData.id, data.postData.count || 1);
  117. break;
  118. case 'down-move':
  119. responseData.data = await this.ctx.service.phasePayDetail.downMoveDetailNode(ctx.phasePay, data.postData.id, data.postData.count || 1);
  120. break;
  121. case 'update':
  122. const updateDetail = await this.ctx.service.phasePayDetail.updateDetail(ctx.phasePay, data.postData);
  123. if (this.ctx.service.phasePayDetail.checkCalc(data.postData)) {
  124. await this.ctx.service.phasePayDetail.calculateSave(ctx.phasePay);
  125. responseData.data.reload = await this.ctx.service.phasePayDetail.getDetailData(ctx.phasePay);
  126. } else {
  127. responseData.data.update = updateDetail;
  128. }
  129. break;
  130. case 'calc':
  131. await this.ctx.service.phasePayDetail.calculateSave(ctx.phasePay);
  132. responseData.data.reload = await this.ctx.service.phasePayDetail.getDetailData(ctx.phasePay);
  133. break;
  134. case 'refreshBase':
  135. await this.ctx.service.phasePay.refreshCalcBase(ctx.phasePay);
  136. responseData.data.reload = await this.ctx.service.phasePayDetail.getDetailData(ctx.phasePay);
  137. responseData.data.calcBase = this.ctx.service.phasePay.getPhasePayCalcBase(ctx.phasePay, ctx.tender.info);
  138. responseData.data.calcBase.forEach(x => { x.formatValue = ctx.tender.info.display.thousandth ? ctx.helper.formatNum(x.value, '#,##0.######') : x.value; });
  139. responseData.data.addBase = ctx.phasePay.calc_base;
  140. break;
  141. default:
  142. throw '未知操作';
  143. }
  144. ctx.body = responseData;
  145. } catch (err) {
  146. console.log(err);
  147. this.log(err);
  148. ctx.body = this.ajaxErrorBody(err, '数据错误');
  149. }
  150. }
  151. async uploadFile(ctx) {
  152. let stream;
  153. try {
  154. const parts = ctx.multipart({ autoFields: true });
  155. let index = 0;
  156. const create_time = Date.parse(new Date()) / 1000;
  157. let stream = await parts();
  158. const bonus = await ctx.service.stageBonus.getStageDataById(parts.field.bonus_id);
  159. //if (!bonus || bonus.sid !== ctx.stage.id) throw '该奖罚金,当前不允许上传附件';
  160. while (stream !== undefined) {
  161. if (!stream.filename) {
  162. throw '未发现上传文件!';
  163. }
  164. const fileInfo = path.parse(stream.filename);
  165. const dirName = 'app/public/upload/extra/' + moment().format('YYYYMMDD');
  166. const fileName = create_time + '_' + index + fileInfo.ext;
  167. // 保存文件
  168. await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
  169. await sendToWormhole(stream);
  170. // 插入到stage_pay对应的附件列表中
  171. bonus.proof_file.push({
  172. filename: fileInfo.name,
  173. fileext: fileInfo.ext,
  174. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  175. filepath: path.join(dirName, fileName),
  176. uid: ctx.session.sessionUser.accountId,
  177. in_time: moment(create_time * 1000).format('YYYY-MM-DD'),
  178. renew: bonus.sid === ctx.stage.id ? ctx.stage.status === auditConst.status.checked : true,
  179. });
  180. ++index;
  181. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  182. stream = await parts();
  183. } else {
  184. stream = undefined;
  185. }
  186. }
  187. const result = await ctx.service.stageBonus.updateDatas({
  188. update: [
  189. { id: bonus.id, proof_file: bonus.proof_file },
  190. ]
  191. });
  192. for (const pf of bonus.proof_file) {
  193. pf.username = (await ctx.service.projectAccount.getAccountInfoById(pf.uid)).name;
  194. if (ctx.helper.canPreview(pf.fileext)){
  195. pf.viewpath = pf.filepath.substr(3, pf.filepath.length - 3);
  196. }
  197. delete pf.filepath;
  198. }
  199. ctx.body = {err: 0, msg: '', data: bonus.proof_file};
  200. } catch (error) {
  201. ctx.helper.log(error);
  202. // 失败需要消耗掉stream 以防卡死
  203. if (stream) {
  204. await sendToWormhole(stream);
  205. }
  206. ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
  207. }
  208. }
  209. async deleteFile(ctx) {
  210. try {
  211. const data = JSON.parse(ctx.request.body.data);
  212. const bonus = await ctx.service.stageBonus.getStageDataById(data.b_id);
  213. if (!bonus || !bonus.proof_file || !bonus.proof_file[data.index]) throw '删除的文件不存在';
  214. const fileInfo = bonus.proof_file[data.index];
  215. if (fileInfo.uid !== ctx.session.sessionUser.accountId) throw '您无权删除该文件';
  216. const deleteFilePermission = PermissionCheck.delFile(this.ctx.session.sessionUser.permission);
  217. if (ctx.stage.status === auditConst.status.checked && !fileInfo.renew && !deleteFilePermission) throw '不可删除该文件';
  218. // 先删除文件
  219. await fs.unlinkSync(path.join(this.app.baseDir, fileInfo.filepath));
  220. // 再删除数据库
  221. bonus.proof_file.splice(data.index, 1);
  222. const result = await ctx.service.stageBonus.updateDatas({
  223. update: [
  224. { id: bonus.id, proof_file: bonus.proof_file },
  225. ]
  226. });
  227. for (const pf of bonus.proof_file) {
  228. delete pf.filepath;
  229. pf.username = (await ctx.service.projectAccount.getAccountInfoById(pf.uid)).name;
  230. }
  231. ctx.body = {err: 0, msg: '', data: bonus.proof_file};
  232. } catch (err) {
  233. this.log(err);
  234. this.ctx.ajaxErrorBody(err, '删除文件失败');
  235. }
  236. }
  237. }
  238. return PayController;
  239. };