material_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author EllisRan
  6. * @date 2018/6/20
  7. * @version
  8. */
  9. const moment = require('moment');
  10. const auditConst = require('../const/audit').material;
  11. const auditStageConst = require('../const/audit').stage;
  12. const spreadConst = require('../const/spread');
  13. const tenderConst = require('../const/tender');
  14. const measureType = tenderConst.measureType;
  15. const accountGroup = require('../const/account_group').group;
  16. module.exports = app => {
  17. class MaterialController extends app.BaseController {
  18. /**
  19. * 构造函数
  20. *
  21. * @param {Object} ctx - egg全局变量
  22. * @return {void}
  23. */
  24. constructor(ctx) {
  25. super(ctx);
  26. ctx.showProject = true;
  27. ctx.showTender = true;
  28. ctx.showTitle = true;
  29. }
  30. /**
  31. * 期列表(Get)
  32. * @param ctx
  33. * @returns {Promise<void>}
  34. */
  35. async index(ctx) {
  36. try {
  37. const renderData = {
  38. tender: ctx.tender.data,
  39. tenderMenu: this.menu.tenderMenu,
  40. preUrl: '/tender/' + ctx.tender.id,
  41. auditConst,
  42. auditConst2: JSON.stringify(auditConst),
  43. };
  44. renderData.materials = await ctx.service.material.getValidMaterials(ctx.tender.id);
  45. // 获取未选中和已完成的计量期
  46. const stages = await ctx.service.stage.getAllDataByCondition({ where: { tid: ctx.tender.id, status: auditStageConst.status.checked } });
  47. for (const s of renderData.materials) {
  48. // s.curAuditor = null;
  49. // 根据期状态返回展示用户
  50. s.curAuditor = await ctx.service.materialAudit.getAuditorByStatus(s.id, s.status, s.times);
  51. const materialStageList = s.stage_id.split(',');
  52. for (const ms of materialStageList) {
  53. const index = stages.findIndex(function(item) {
  54. return item.id === parseInt(ms);
  55. });
  56. stages.splice(index, 1);
  57. }
  58. }
  59. renderData.stages = stages;
  60. await this.layout('material/index.ejs', renderData, 'material/modal.ejs');
  61. } catch (err) {
  62. this.log(err);
  63. ctx.redirect(this.menu.menu.dashboard.url);
  64. }
  65. }
  66. /**
  67. * 期审批流程(Get)
  68. * @param ctx
  69. * @returns {Promise<void>}
  70. */
  71. async materialAuditors(ctx) {
  72. try {
  73. const responseData = {
  74. err: 0, msg: '', data: {},
  75. };
  76. const order = JSON.parse(ctx.request.body.data).order;
  77. const tenderId = ctx.params.id;
  78. const materialInfo = await ctx.service.material.getDataByCondition({ tid: tenderId, order });
  79. // 获取审批流程中右边列表
  80. const auditHistory = [];
  81. const times = materialInfo.status === auditConst.status.checkNo ? materialInfo.times - 1 : materialInfo.times;
  82. if (times >= 1) {
  83. for (let i = 1; i <= times; i++) {
  84. auditHistory.push(await ctx.service.materialAudit.getAuditors(materialInfo.id, i));
  85. }
  86. }
  87. responseData.data.auditHistory = auditHistory;
  88. // 获取审批流程中左边列表
  89. responseData.data.auditors = await ctx.service.materialAudit.getAuditGroupByList(materialInfo.id, times);
  90. // 获取原报信息
  91. const materialAuditor = await ctx.service.projectAccount.getAccountInfoById(materialInfo.user_id);
  92. responseData.data.materialAuditor = materialAuditor;
  93. ctx.body = responseData;
  94. } catch (error) {
  95. this.log(error);
  96. ctx.body = { err: 1, msg: error.toString(), data: null };
  97. }
  98. }
  99. /**
  100. * 新增期(Post)
  101. * @param ctx
  102. * @returns {Promise<void>}
  103. */
  104. async add(ctx) {
  105. try {
  106. if (ctx.session.sessionUser.accountId !== ctx.tender.data.user_id) {
  107. throw '您无权创建材料调差期';
  108. }
  109. const data = ctx.request.body;
  110. const newMaterial = await ctx.service.material.addMaterial(ctx.tender.id, data);
  111. if (!newMaterial) {
  112. throw '新增材料调差期失败,请重试';
  113. }
  114. ctx.redirect('/tender/' + ctx.tender.id + '/measure/material/' + newMaterial.order);
  115. } catch (err) {
  116. this.log(err);
  117. ctx.redirect(ctx.request.header.referer);
  118. }
  119. }
  120. /**
  121. * 删除期(Post)
  122. * @param ctx
  123. * @returns {Promise<void>}
  124. */
  125. async delete(ctx) {
  126. try {
  127. const material_id = ctx.request.body.material_id;
  128. const materialInfo = await ctx.service.material.getDataById(material_id);
  129. // 获取最新的期数
  130. const material_highOrder = await ctx.service.material.count({
  131. tid: ctx.tender.id,
  132. });
  133. if (materialInfo === undefined || ctx.session.sessionUser.accountId !== materialInfo.user_id || material_highOrder !== materialInfo.order) {
  134. throw '您无权删除材料调差期';
  135. }
  136. const result = await ctx.service.material.deleteMaterial(material_id);
  137. if (!result) {
  138. throw '删除材料调差期失败,请重试';
  139. }
  140. ctx.redirect('/tender/' + ctx.tender.id + '/measure/material/');
  141. } catch (err) {
  142. this.log(err);
  143. console.log(err);
  144. ctx.redirect(ctx.request.header.referer);
  145. }
  146. }
  147. /**
  148. * 获取通用的renderData(用于layout, Menu, subMenu部分)
  149. * @param ctx
  150. * @returns {{tender, tenderMenu, auditConst}}
  151. * @private
  152. */
  153. async _getDefaultRenderData(ctx) {
  154. const data = {
  155. tender: ctx.tender.data,
  156. tenderMenu: JSON.parse(JSON.stringify(this.menu.stageMenu)),
  157. auditConst,
  158. measureType,
  159. preUrl: '/tender/' + ctx.tender.id + '/measure/material/' + ctx.params.order,
  160. material: ctx.material,
  161. };
  162. if ((ctx.material.status === auditConst.status.uncheck || ctx.material.status === auditConst.status.checkNo) && ctx.session.sessionUser.accountId === ctx.material.user_id) {
  163. data.accountGroup = accountGroup;
  164. // 获取所有项目参与者
  165. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  166. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  167. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group'],
  168. });
  169. data.accountList = accountList;
  170. }
  171. data.tenderMenu.back.children[0].url = '/tender/' + ctx.tender.id + '/measure/material';
  172. return data;
  173. }
  174. _materialReadOnly(material) {
  175. return material.status === auditConst.status.checked;
  176. }
  177. /**
  178. * 获取SpreadSetting
  179. * @private
  180. */
  181. _getSpreadSetting() {
  182. const _ = this.app._;
  183. function removeFieldCols(setting, cols) {
  184. _.remove(setting.cols, function (c) {
  185. return cols.indexOf(c.field) > -1;
  186. });
  187. }
  188. function setColFormat(cols, field, formatter) {
  189. const filterCols = cols.filter(function (c) {
  190. return c.field.search(field) !== -1;
  191. });
  192. for (const col of filterCols) {
  193. col.formatter = formatter;
  194. }
  195. }
  196. const billsSetting = spreadConst.material;
  197. const ledger = JSON.parse(JSON.stringify(billsSetting.bill));
  198. if (this.ctx.material.readOnly) {
  199. ledger.readOnly = true;
  200. }
  201. return [ledger];
  202. }
  203. /**
  204. * 获取审批界面所需的 原报、审批人数据等
  205. * @param ctx
  206. * @returns {Promise<void>}
  207. * @private
  208. */
  209. async _getMaterialAuditViewData(ctx) {
  210. const times = ctx.material.status === auditConst.status.checkNo ? ctx.material.times - 1 : ctx.material.times;
  211. ctx.material.user = await ctx.service.projectAccount.getAccountInfoById(ctx.material.user_id);
  212. ctx.material.auditHistory = [];
  213. if (ctx.material.times > 1) {
  214. for (let i = 1; i < ctx.material.times; i++) {
  215. ctx.material.auditHistory.push(await ctx.service.materialAudit.getAuditors(ctx.material.id, i));
  216. }
  217. }
  218. // 获取审批流程中左边列表
  219. ctx.material.auditors2 = await ctx.service.materialAudit.getAuditGroupByList(ctx.material.id, times);
  220. if (ctx.material.status === auditConst.status.uncheck || ctx.material.status === auditConst.status.checkNo) {
  221. ctx.material.auditorList = await ctx.service.materialAudit.getAuditors(ctx.material.id, ctx.material.times);
  222. }
  223. }
  224. /**
  225. * 期计量页面 (Get)
  226. * @param {Object} ctx - egg全局变量
  227. * @returns {Promise<void>}
  228. */
  229. async info(ctx) {
  230. try {
  231. await this._getMaterialAuditViewData(ctx);
  232. const renderData = await this._getDefaultRenderData(ctx);
  233. [renderData.billsSpread] = this._getSpreadSetting();
  234. // renderData.ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
  235. renderData.jsFiles = this.app.jsFiles.common.concat(this.app.jsFiles.material.info);
  236. await this.layout('material/info.ejs', renderData, 'material/info_modal.ejs');
  237. } catch (err) {
  238. this.log(err);
  239. ctx.redirect('/tender/' + ctx.tender.id + '/measure/material');
  240. }
  241. }
  242. // 审批相关
  243. /**
  244. * 添加审批人
  245. * @param ctx
  246. * @returns {Promise<void>}
  247. */
  248. async addAudit(ctx) {
  249. try {
  250. const data = JSON.parse(ctx.request.body.data);
  251. const id = this.app._.toInteger(data.auditorId);
  252. if (isNaN(id) || id <= 0) {
  253. throw '参数错误';
  254. }
  255. // 检查权限等
  256. if (ctx.material.user_id !== ctx.session.sessionUser.accountId) {
  257. throw '您无权添加审核人';
  258. }
  259. if (ctx.material.status === auditConst.status.checking || ctx.material.status === auditConst.status.checked) {
  260. throw '当前不允许添加审核人';
  261. }
  262. ctx.material.auditorList = await ctx.service.materialAudit.getAuditors(ctx.material.id, ctx.material.times);
  263. // 检查审核人是否已存在
  264. const exist = this.app._.find(ctx.material.auditorList, {aid: id});
  265. if (exist) {
  266. throw '该审核人已存在,请勿重复添加';
  267. }
  268. const result = await ctx.service.materialAudit.addAuditor(ctx.material.id, id, ctx.material.times);
  269. if (!result) {
  270. throw '添加审核人失败';
  271. }
  272. const audit = await ctx.service.materialAudit.getAuditor(ctx.material.id, id, ctx.material.times);
  273. ctx.body = {err: 0, msg: '', data: audit};
  274. } catch (err) {
  275. this.log(err);
  276. ctx.body = {err: 1, msg: err.toString(), data: null};
  277. }
  278. }
  279. /**
  280. * 移除审批人
  281. * @param ctx
  282. * @returns {Promise<void>}
  283. */
  284. async deleteAudit(ctx) {
  285. try {
  286. const data = JSON.parse(ctx.request.body.data);
  287. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  288. if (isNaN(id) || id <= 0) {
  289. throw '参数错误';
  290. }
  291. const result = await ctx.service.materialAudit.deleteAuditor(ctx.material.id, id, ctx.material.times);
  292. if (!result) {
  293. throw '移除审核人失败';
  294. }
  295. const auditors = await ctx.service.materialAudit.getAuditors(ctx.material.id, ctx.material.times);
  296. ctx.body = {err: 0, msg: '', data: auditors};
  297. } catch (err) {
  298. ctx.body = {err: 1, msg: err.toString(), data: null};
  299. }
  300. }
  301. /**
  302. * 上报
  303. * @param ctx
  304. * @returns {Promise<void>}
  305. */
  306. async startAudit(ctx) {
  307. try {
  308. // 检查权限等
  309. if (!ctx.material) {
  310. throw '数据错误';
  311. }
  312. if (ctx.material.user_id !== ctx.session.sessionUser.accountId) {
  313. throw '您无权上报该期数据';
  314. }
  315. if (ctx.material.status === auditConst.status.checking || ctx.material.status === auditConst.status.checked) {
  316. throw '该材料调差期数据当前无法上报';
  317. }
  318. await ctx.service.materialAudit.start(ctx.material.id, ctx.material.times);
  319. ctx.redirect(ctx.request.header.referer);
  320. } catch (err) {
  321. this.log(err);
  322. ctx.session.postError = err.toString();
  323. ctx.redirect(ctx.request.header.referer);
  324. }
  325. }
  326. /**
  327. * 审批
  328. * @param ctx
  329. * @returns {Promise<void>}
  330. */
  331. async checkAudit(ctx) {
  332. try {
  333. if (!this.ctx.material || this.ctx.material.status !== auditConst.status.checking) {
  334. throw '当前材料调差期数据有误';
  335. }
  336. if (!this.ctx.material.curAuditor || this.ctx.material.curAuditor.aid !== ctx.session.sessionUser.accountId) {
  337. throw '您无权进行该操作';
  338. }
  339. const data = {
  340. checkType: parseInt(ctx.request.body.checkType),
  341. opinion: ctx.request.body.opinion,
  342. };
  343. if (!data.checkType || isNaN(data.checkType)) {
  344. throw '提交数据错误';
  345. }
  346. if (data.checkType === auditConst.status.checkNo) {
  347. if (!data.checkType || isNaN(data.checkType)) {
  348. throw '提交数据错误';
  349. }
  350. }
  351. await ctx.service.materialAudit.check(ctx.material.id, data, ctx.material.times);
  352. ctx.redirect(ctx.request.header.referer);
  353. } catch (err) {
  354. console.log(err);
  355. this.log(err);
  356. ctx.session.postError = err.toString();
  357. ctx.redirect(ctx.request.header.referer);
  358. }
  359. }
  360. }
  361. return MaterialController;
  362. };