pay_controller.js 12 KB

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