pay_controller.js 11 KB

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