pay_controller.js 10 KB

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