material_controller.js 16 KB

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