material_controller.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 billsSetting = spreadConst.material;
  187. const ledger = JSON.parse(JSON.stringify(billsSetting.bill));
  188. if (this.ctx.material.readOnly) {
  189. ledger.readOnly = true;
  190. }
  191. return [ledger];
  192. }
  193. /**
  194. * 获取审批界面所需的 原报、审批人数据等
  195. * @param ctx
  196. * @returns {Promise<void>}
  197. * @private
  198. */
  199. async _getMaterialAuditViewData(ctx) {
  200. const times = ctx.material.status === auditConst.status.checkNo ? ctx.material.times - 1 : ctx.material.times;
  201. ctx.material.user = await ctx.service.projectAccount.getAccountInfoById(ctx.material.user_id);
  202. ctx.material.auditHistory = [];
  203. if (ctx.material.times > 1) {
  204. for (let i = 1; i < ctx.material.times; i++) {
  205. ctx.material.auditHistory.push(await ctx.service.materialAudit.getAuditors(ctx.material.id, i));
  206. }
  207. }
  208. // 获取审批流程中左边列表
  209. ctx.material.auditors2 = await ctx.service.materialAudit.getAuditGroupByList(ctx.material.id, times);
  210. if (ctx.material.status === auditConst.status.uncheck || ctx.material.status === auditConst.status.checkNo) {
  211. ctx.material.auditorList = await ctx.service.materialAudit.getAuditors(ctx.material.id, ctx.material.times);
  212. }
  213. }
  214. /**
  215. * 调差工料页面 (Get)
  216. * @param {Object} ctx - egg全局变量
  217. * @returns {Promise<void>}
  218. */
  219. async info(ctx) {
  220. try {
  221. await this._getMaterialAuditViewData(ctx);
  222. const renderData = await this._getDefaultRenderData(ctx);
  223. // [renderData.billsSpread] = this._getSpreadSetting();
  224. renderData.materialBillsData = await ctx.service.materialBills.getAllDataByCondition({ where: { mid: ctx.material.id } });
  225. renderData.materialType = JSON.stringify(materialConst);
  226. renderData.jsFiles = this.app.jsFiles.common.concat(this.app.jsFiles.material.info);
  227. await this.layout('material/info.ejs', renderData, 'material/info_modal.ejs');
  228. } catch (err) {
  229. this.log(err);
  230. ctx.redirect('/tender/' + ctx.tender.id + '/measure/material');
  231. }
  232. }
  233. /**
  234. * 调差工料 - 编辑工料项 (Ajax)
  235. * @param ctx
  236. * @returns {Promise<void>}
  237. */
  238. async saveBillsData(ctx) {
  239. try {
  240. const data = JSON.parse(ctx.request.body.data);
  241. const responseData = {
  242. err: 0,
  243. msg: '',
  244. data: {},
  245. };
  246. switch (data.type) {
  247. case 'add':
  248. responseData.data = await ctx.service.materialBills.add();
  249. break;
  250. case 'del':
  251. await ctx.service.materialBills.del(data.id);
  252. break;
  253. case 'update':
  254. if (data.updateData.code === '' || data.updateData.code === null) {
  255. throw '请先输入编号';
  256. }
  257. // 判断编号为纯数字时,不能为小数
  258. if (!isNaN(data.updateData.code) && data.updateData.code.indexOf('.') !== -1) {
  259. throw '编号为纯数字时,不能为小数';
  260. }
  261. if (data.updateData.code.length > 15) {
  262. throw '长度不超过15个字符';
  263. }
  264. // 判断编号是否已存在
  265. const billData = await ctx.service.materialBills.getAllDataByCondition({ where: { mid: ctx.material.id, code: data.updateData.code } });
  266. if (billData.length > 1 || (billData.length > 0 && billData[0].id !== data.updateData.id)) {
  267. throw '该编号已存在,请重新输入。';
  268. }
  269. await ctx.service.materialBills.save(data.updateData);
  270. break;
  271. default: throw '参数有误';
  272. }
  273. ctx.body = responseData;
  274. } catch (err) {
  275. this.log(err);
  276. ctx.body = { err: 1, msg: err.toString(), data: null };
  277. }
  278. }
  279. // 审批相关
  280. /**
  281. * 添加审批人
  282. * @param ctx
  283. * @returns {Promise<void>}
  284. */
  285. async addAudit(ctx) {
  286. try {
  287. const data = JSON.parse(ctx.request.body.data);
  288. const id = this.app._.toInteger(data.auditorId);
  289. if (isNaN(id) || id <= 0) {
  290. throw '参数错误';
  291. }
  292. // 检查权限等
  293. if (ctx.material.user_id !== ctx.session.sessionUser.accountId) {
  294. throw '您无权添加审核人';
  295. }
  296. if (ctx.material.status === auditConst.status.checking || ctx.material.status === auditConst.status.checked) {
  297. throw '当前不允许添加审核人';
  298. }
  299. ctx.material.auditorList = await ctx.service.materialAudit.getAuditors(ctx.material.id, ctx.material.times);
  300. // 检查审核人是否已存在
  301. const exist = this.app._.find(ctx.material.auditorList, {aid: id});
  302. if (exist) {
  303. throw '该审核人已存在,请勿重复添加';
  304. }
  305. const result = await ctx.service.materialAudit.addAuditor(ctx.material.id, id, ctx.material.times);
  306. if (!result) {
  307. throw '添加审核人失败';
  308. }
  309. const audit = await ctx.service.materialAudit.getAuditor(ctx.material.id, id, ctx.material.times);
  310. ctx.body = {err: 0, msg: '', data: audit};
  311. } catch (err) {
  312. this.log(err);
  313. ctx.body = {err: 1, msg: err.toString(), data: null};
  314. }
  315. }
  316. /**
  317. * 移除审批人
  318. * @param ctx
  319. * @returns {Promise<void>}
  320. */
  321. async deleteAudit(ctx) {
  322. try {
  323. const data = JSON.parse(ctx.request.body.data);
  324. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  325. if (isNaN(id) || id <= 0) {
  326. throw '参数错误';
  327. }
  328. const result = await ctx.service.materialAudit.deleteAuditor(ctx.material.id, id, ctx.material.times);
  329. if (!result) {
  330. throw '移除审核人失败';
  331. }
  332. const auditors = await ctx.service.materialAudit.getAuditors(ctx.material.id, ctx.material.times);
  333. ctx.body = {err: 0, msg: '', data: auditors};
  334. } catch (err) {
  335. ctx.body = {err: 1, msg: err.toString(), data: null};
  336. }
  337. }
  338. /**
  339. * 上报
  340. * @param ctx
  341. * @returns {Promise<void>}
  342. */
  343. async startAudit(ctx) {
  344. try {
  345. // 检查权限等
  346. if (!ctx.material) {
  347. throw '数据错误';
  348. }
  349. if (ctx.material.user_id !== ctx.session.sessionUser.accountId) {
  350. throw '您无权上报该期数据';
  351. }
  352. if (ctx.material.status === auditConst.status.checking || ctx.material.status === auditConst.status.checked) {
  353. throw '该材料调差期数据当前无法上报';
  354. }
  355. await ctx.service.materialAudit.start(ctx.material.id, ctx.material.times);
  356. ctx.redirect(ctx.request.header.referer);
  357. } catch (err) {
  358. this.log(err);
  359. ctx.session.postError = err.toString();
  360. ctx.redirect(ctx.request.header.referer);
  361. }
  362. }
  363. /**
  364. * 审批
  365. * @param ctx
  366. * @returns {Promise<void>}
  367. */
  368. async checkAudit(ctx) {
  369. try {
  370. if (!this.ctx.material || this.ctx.material.status !== auditConst.status.checking) {
  371. throw '当前材料调差期数据有误';
  372. }
  373. if (!this.ctx.material.curAuditor || this.ctx.material.curAuditor.aid !== ctx.session.sessionUser.accountId) {
  374. throw '您无权进行该操作';
  375. }
  376. const data = {
  377. checkType: parseInt(ctx.request.body.checkType),
  378. opinion: ctx.request.body.opinion,
  379. };
  380. if (!data.checkType || isNaN(data.checkType)) {
  381. throw '提交数据错误';
  382. }
  383. if (data.checkType === auditConst.status.checkNo) {
  384. if (!data.checkType || isNaN(data.checkType)) {
  385. throw '提交数据错误';
  386. }
  387. }
  388. await ctx.service.materialAudit.check(ctx.material.id, data, ctx.material.times);
  389. ctx.redirect(ctx.request.header.referer);
  390. } catch (err) {
  391. console.log(err);
  392. this.log(err);
  393. ctx.session.postError = err.toString();
  394. ctx.redirect(ctx.request.header.referer);
  395. }
  396. }
  397. }
  398. return MaterialController;
  399. };