pay_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 renderData = {
  84. pays,
  85. calcBase,
  86. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.phasePay.detail)
  87. };
  88. await this.layout('phase_pay/detail.ejs', renderData, 'phase_pay/detail_modal.ejs');
  89. } catch (err) {
  90. ctx.helper.log(err);
  91. ctx.postError(err, '读取合同支付数据错误');
  92. ctx.redirect(ctx.request.headers.referer);
  93. }
  94. }
  95. async detailUpdate(ctx) {
  96. try {
  97. const data = JSON.parse(ctx.request.body.data);
  98. if (!data.postType || !data.postData) throw '数据错误';
  99. const responseData = { err: 0, msg: '', data: {} };
  100. switch (data.postType) {
  101. case 'add':
  102. responseData.data = await this.ctx.service.phasePayDetail.addDetailNode(ctx.phasePay, data.postData.id, data.postData.count || 1);
  103. break;
  104. case 'delete':
  105. await this.ctx.service.phasePayDetail.delete(ctx.phasePay.id, data.postData.id, data.postData.count || 1);
  106. await this.ctx.service.phasePayDetail.calculateSave(ctx.phasePay);
  107. responseData.data.reload = await this.ctx.service.phasePayDetail.getDetailData(ctx.phasePay);
  108. case 'up-move':
  109. responseData.data = await this.ctx.service.phasePayDetail.upMoveNode(ctx.phasePay.id, data.postData.id, data.postData.count || 1);
  110. break;
  111. case 'down-move':
  112. responseData.data = await this.ctx.service.phasePayDetail.downMoveNode(ctx.phasePay.id, data.postData.id, data.postData.count || 1);
  113. break;
  114. case 'update':
  115. await this.ctx.service.phasePayDetail.updateCalc(ctx.phasePay, data.postData);
  116. responseData.data.reload = await this.ctx.service.phasePayDetail.calculateSave(ctx.phasePay);
  117. break;
  118. case 'calc':
  119. await this.ctx.service.phasePayDetail.calculateSave(ctx.phasePay);
  120. responseData.data.reload = await this.ctx.service.phasePayDetail.getDetailData(ctx.phasePay);
  121. case 'refreshBase':
  122. await this.ctx.service.phasePay.refreshCalcBase()
  123. default:
  124. throw '未知操作';
  125. }
  126. ctx.body = responseData;
  127. } catch (err) {
  128. this.log(err);
  129. ctx.body = this.ajaxErrorBody(err, '数据错误');
  130. }
  131. }
  132. async uploadFile(ctx) {
  133. let stream;
  134. try {
  135. const parts = ctx.multipart({ autoFields: true });
  136. let index = 0;
  137. const create_time = Date.parse(new Date()) / 1000;
  138. let stream = await parts();
  139. const bonus = await ctx.service.stageBonus.getStageDataById(parts.field.bonus_id);
  140. //if (!bonus || bonus.sid !== ctx.stage.id) throw '该奖罚金,当前不允许上传附件';
  141. while (stream !== undefined) {
  142. if (!stream.filename) {
  143. throw '未发现上传文件!';
  144. }
  145. const fileInfo = path.parse(stream.filename);
  146. const dirName = 'app/public/upload/extra/' + moment().format('YYYYMMDD');
  147. const fileName = create_time + '_' + index + fileInfo.ext;
  148. // 保存文件
  149. await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
  150. await sendToWormhole(stream);
  151. // 插入到stage_pay对应的附件列表中
  152. bonus.proof_file.push({
  153. filename: fileInfo.name,
  154. fileext: fileInfo.ext,
  155. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  156. filepath: path.join(dirName, fileName),
  157. uid: ctx.session.sessionUser.accountId,
  158. in_time: moment(create_time * 1000).format('YYYY-MM-DD'),
  159. renew: bonus.sid === ctx.stage.id ? ctx.stage.status === auditConst.status.checked : true,
  160. });
  161. ++index;
  162. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  163. stream = await parts();
  164. } else {
  165. stream = undefined;
  166. }
  167. }
  168. const result = await ctx.service.stageBonus.updateDatas({
  169. update: [
  170. { id: bonus.id, proof_file: bonus.proof_file },
  171. ]
  172. });
  173. for (const pf of bonus.proof_file) {
  174. pf.username = (await ctx.service.projectAccount.getAccountInfoById(pf.uid)).name;
  175. if (ctx.helper.canPreview(pf.fileext)){
  176. pf.viewpath = pf.filepath.substr(3, pf.filepath.length - 3);
  177. }
  178. delete pf.filepath;
  179. }
  180. ctx.body = {err: 0, msg: '', data: bonus.proof_file};
  181. } catch (error) {
  182. ctx.helper.log(error);
  183. // 失败需要消耗掉stream 以防卡死
  184. if (stream) {
  185. await sendToWormhole(stream);
  186. }
  187. ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
  188. }
  189. }
  190. async deleteFile(ctx) {
  191. try {
  192. const data = JSON.parse(ctx.request.body.data);
  193. const bonus = await ctx.service.stageBonus.getStageDataById(data.b_id);
  194. if (!bonus || !bonus.proof_file || !bonus.proof_file[data.index]) throw '删除的文件不存在';
  195. const fileInfo = bonus.proof_file[data.index];
  196. if (fileInfo.uid !== ctx.session.sessionUser.accountId) throw '您无权删除该文件';
  197. const deleteFilePermission = PermissionCheck.delFile(this.ctx.session.sessionUser.permission);
  198. if (ctx.stage.status === auditConst.status.checked && !fileInfo.renew && !deleteFilePermission) throw '不可删除该文件';
  199. // 先删除文件
  200. await fs.unlinkSync(path.join(this.app.baseDir, fileInfo.filepath));
  201. // 再删除数据库
  202. bonus.proof_file.splice(data.index, 1);
  203. const result = await ctx.service.stageBonus.updateDatas({
  204. update: [
  205. { id: bonus.id, proof_file: bonus.proof_file },
  206. ]
  207. });
  208. for (const pf of bonus.proof_file) {
  209. delete pf.filepath;
  210. pf.username = (await ctx.service.projectAccount.getAccountInfoById(pf.uid)).name;
  211. }
  212. ctx.body = {err: 0, msg: '', data: bonus.proof_file};
  213. } catch (err) {
  214. this.log(err);
  215. this.ctx.ajaxErrorBody(err, '删除文件失败');
  216. }
  217. }
  218. }
  219. return PayController;
  220. };