schedule_controller.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Ellisran
  6. * @date 2020/7/2
  7. * @version
  8. */
  9. const moment = require('moment');
  10. const reviseStatus = require('../const/audit').revise.status;
  11. const measureType = require('../const/tender').measureType;
  12. const scheduleConst = require('../const/schedule');
  13. const billsPosConvert = require('../lib/bills_pos_convert');
  14. const _ = require('lodash');
  15. module.exports = app => {
  16. class ScheduleController extends app.BaseController {
  17. async _getSelectedLedgerList(ctx) {
  18. const scheduleLedgerList = await ctx.service.scheduleLedger.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  19. return _.map(scheduleLedgerList, 'ledger_id');
  20. }
  21. async _getLastPlanMonth(ctx) {
  22. const lastMonth = await ctx.service.scheduleMonth.getLastPlanMonth();
  23. return lastMonth && lastMonth[0] && lastMonth[0].yearmonth ? lastMonth[0].yearmonth : null;
  24. }
  25. async _getLastReviseStatus(ctx) {
  26. const lastRevise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
  27. return (lastRevise && lastRevise.status !== reviseStatus.checked) || false;
  28. }
  29. async _checkScheduleCanModify(ctx) {
  30. if (await this._getLastReviseStatus(ctx)) {
  31. throw '台账修订中,请勿修改提交进度数据';
  32. }
  33. }
  34. async index(ctx) {
  35. try {
  36. const schedule = await ctx.service.schedule.getDataByCondition({ tid: ctx.tender.id });
  37. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: ctx.tender.id }, orders: [['yearmonth', 'asc']] });
  38. const renderData = {
  39. schedule,
  40. scheduleMonth,
  41. tender: ctx.tender.data,
  42. tenderMenu: this.menu.tenderMenu,
  43. planMonth: await this._getLastPlanMonth(ctx),
  44. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  45. preUrl: '/tender/' + ctx.tender.id,
  46. revising: await this._getLastReviseStatus(ctx),
  47. };
  48. await this.layout('schedule/index.ejs', renderData, 'schedule/modal.ejs');
  49. } catch (err) {
  50. this.log(err);
  51. ctx.redirect(this.menu.menu.dashboard.url);
  52. }
  53. }
  54. async ledger(ctx) {
  55. const tender = ctx.tender;
  56. const schedule = await ctx.service.schedule.getDataByCondition({ tid: ctx.tender.id });
  57. const scheduleLedgerList = await this._getSelectedLedgerList(ctx);
  58. const allSlmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  59. const hadDataLidList = [];
  60. for (const sl of scheduleLedgerList) {
  61. const info = _.find(allSlmList, function(item) {
  62. return item.lid === sl && ((item.plan_tp !== null && item.plan_tp !== 0) ||
  63. (item.plan_gcl !== null && item.plan_gcl !== 0) ||
  64. (item.sj_tp !== null && item.sj_tp !== 0) ||
  65. (item.sj_gcl !== null && item.sj_gcl !== 0));
  66. });
  67. if (info) {
  68. hadDataLidList.push(info.lid);
  69. }
  70. }
  71. const renderData = {
  72. schedule,
  73. tender: tender.data,
  74. tenderInfo: tender.info,
  75. measureType,
  76. scheduleLedgerList,
  77. hadDataLidList,
  78. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.ledger),
  79. revising: await this._getLastReviseStatus(ctx),
  80. };
  81. await this.layout('schedule/ledger.ejs', renderData, 'schedule/modal.ejs');
  82. }
  83. async plan(ctx) {
  84. const tender = ctx.tender;
  85. const schedule = await ctx.service.schedule.getDataByCondition({ tid: tender.id });
  86. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  87. const renderData = {
  88. tender: tender.data,
  89. tenderInfo: tender.info,
  90. schedule,
  91. scheduleMonth,
  92. planMonth: await this._getLastPlanMonth(ctx),
  93. measureType,
  94. mode: scheduleConst.plan_mode,
  95. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  96. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.plan),
  97. revising: await this._getLastReviseStatus(ctx),
  98. };
  99. await this.layout('schedule/plan.ejs', renderData, 'schedule/plan_modal.ejs');
  100. }
  101. async stageTp(ctx) {
  102. const tender = ctx.tender;
  103. const schedule = await ctx.service.schedule.getDataByCondition({ tid: tender.id });
  104. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  105. const stageOrderList = await ctx.service.stage.getAllDataByCondition({ columns: ['id', 's_time', 'order'], where: { tid: tender.id } });
  106. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: tender.id }, orders: [['order', 'desc']] });
  107. let curScheduleStage = scheduleStage.length > 0 ? _.maxBy(scheduleStage, 'order') : null;
  108. let slmList = [];
  109. let nextSlmList = [];
  110. let endSlmList = [];
  111. let yearSlmList = [];
  112. let curYearStageData = [];
  113. if (ctx.params.order && scheduleStage.length > 0) {
  114. curScheduleStage = _.find(scheduleStage, { order: parseInt(ctx.params.order) });
  115. }
  116. if (curScheduleStage) {
  117. // const newSS = _.sortBy(scheduleStage, 'yearmonth');
  118. const newSM = _.sortBy(scheduleMonth, 'yearmonth');
  119. const nowScheduleStage = _.findIndex(newSM, { yearmonth: curScheduleStage.yearmonth });
  120. slmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: curScheduleStage.yearmonth } });
  121. const nextScheduleStage = nowScheduleStage >= 0 && nowScheduleStage + 1 <= newSM.length - 1 ? newSM[nowScheduleStage + 1] : null;
  122. if (nextScheduleStage) nextSlmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: nextScheduleStage.yearmonth } });
  123. if (nowScheduleStage === 0) {
  124. endSlmList = slmList;
  125. } else if (nowScheduleStage > 0) {
  126. const endYearmonthCollection = _.map(_.take(newSM, nowScheduleStage + 1), 'yearmonth');
  127. endSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, endYearmonthCollection);
  128. }
  129. const yearConllection = _.map(_.filter(newSM, function(item) {
  130. return item.yearmonth.indexOf(curScheduleStage.yearmonth.split('-')[0]) !== -1;
  131. }), 'yearmonth');
  132. yearSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, yearConllection);
  133. // 获取本年完成计量数据
  134. const curStage = _.find(stageOrderList, { order: curScheduleStage.order });
  135. const stageList = _.filter(stageOrderList, function(item) {
  136. return item.s_time.indexOf(curStage.s_time.split('-')[0]) !== -1;
  137. });
  138. const newSS = _.sortBy(scheduleStage, 'yearmonth');
  139. const stageIdList = _.map(_.filter(stageList, function(item) {
  140. return _.find(newSS, { order: item.order });
  141. }), 'id');
  142. curYearStageData = await ctx.service.stageBills.getStagesData(ctx.tender.id, stageIdList.join(','));
  143. }
  144. const renderData = {
  145. tender: tender.data,
  146. tenderInfo: tender.info,
  147. schedule,
  148. scheduleMonth,
  149. measureType,
  150. stageOrderList,
  151. scheduleStage,
  152. curScheduleStage,
  153. slmList,
  154. nextSlmList,
  155. endSlmList,
  156. yearSlmList,
  157. curYearStageData,
  158. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  159. revising: await this._getLastReviseStatus(ctx),
  160. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.stageTp),
  161. };
  162. await this.layout('schedule/stage_tp.ejs', renderData, 'schedule/stage_tp_modal.ejs');
  163. }
  164. async stageGcl(ctx) {
  165. const tender = ctx.tender;
  166. const schedule = await ctx.service.schedule.getDataByCondition({ tid: tender.id });
  167. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  168. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: tender.id }, orders: [['order', 'desc']] });
  169. const curScheduleStage = scheduleStage.length > 0 ? _.maxBy(scheduleStage, 'order') : null;
  170. const renderData = {
  171. tender: tender.data,
  172. tenderInfo: tender.info,
  173. schedule,
  174. scheduleMonth,
  175. measureType,
  176. scheduleStage,
  177. curScheduleStage,
  178. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  179. revising: await this._getLastReviseStatus(ctx),
  180. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.stageGcl),
  181. };
  182. await this.layout('schedule/stage_gcl.ejs', renderData, 'schedule/stage_gcl_modal.ejs');
  183. }
  184. /**
  185. * 获取金额模式下台账数据(Ajax)
  186. *
  187. * @param ctx
  188. * @return {Promise<void>}
  189. */
  190. async loadTpLedgerData(ctx) {
  191. try {
  192. const tender = ctx.tender;
  193. // const ledgerData = await ctx.controller.stage._getStageLedgerData(ctx);
  194. // console.log(ledgerData);
  195. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  196. const stageOrderList = await ctx.service.stage.getAllDataByCondition({ columns: ['id', 's_time', 'order'], where: { tid: tender.id } });
  197. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: tender.id }, orders: [['order', 'desc']] });
  198. const curScheduleStage = _.find(scheduleStage, { order: parseInt(ctx.params.order) });
  199. let slmList = [];
  200. let nextSlmList = [];
  201. let endSlmList = [];
  202. let yearSlmList = [];
  203. let curYearStageData = [];
  204. if (curScheduleStage) {
  205. const newSM = _.sortBy(scheduleMonth, 'yearmonth');
  206. const nowScheduleStage = _.findIndex(newSM, { yearmonth: curScheduleStage.yearmonth });
  207. slmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: curScheduleStage.yearmonth } });
  208. const nextScheduleStage = nowScheduleStage >= 0 && nowScheduleStage + 1 <= newSM.length - 1 ? newSM[nowScheduleStage + 1] : null;
  209. if (nextScheduleStage) nextSlmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: nextScheduleStage.yearmonth } });
  210. if (nowScheduleStage === 0) {
  211. endSlmList = slmList;
  212. } else if (nowScheduleStage > 0) {
  213. const endYearmonthCollection = _.map(_.take(newSM, nowScheduleStage + 1), 'yearmonth');
  214. endSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, endYearmonthCollection);
  215. }
  216. const yearConllection = _.map(_.filter(newSM, function(item) {
  217. return item.yearmonth.indexOf(curScheduleStage.yearmonth.split('-')[0]) !== -1;
  218. }), 'yearmonth');
  219. yearSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, yearConllection);
  220. // 获取本年完成计量数据
  221. const curStage = _.find(stageOrderList, { order: curScheduleStage.order });
  222. const stageList = _.filter(stageOrderList, function(item) {
  223. return item.s_time.indexOf(curStage.s_time.split('-')[0]) !== -1;
  224. });
  225. const newSS = _.sortBy(scheduleStage, 'yearmonth');
  226. const stageIdList = _.map(_.filter(stageList, function(item) {
  227. return _.find(newSS, { order: item.order });
  228. }), 'id');
  229. curYearStageData = await ctx.service.stageBills.getStagesData(tender.id, stageIdList.join(','));
  230. }
  231. ctx.body = { err: 0, msg: '', data: {
  232. // ledgerData,
  233. slmList,
  234. nextSlmList,
  235. endSlmList,
  236. yearSlmList,
  237. curYearStageData,
  238. } };
  239. } catch (err) {
  240. this.log(err);
  241. ctx.body = { err: 1, msg: err.toString(), data: [] };
  242. }
  243. }
  244. /**
  245. * 获取台账数据(Ajax)
  246. *
  247. * @param ctx
  248. * @return {Promise<void>}
  249. */
  250. async loadLedgerData(ctx) {
  251. try {
  252. const ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
  253. // const posData = ctx.tender.data.measure_type === measureType.tz.value
  254. // ? await ctx.service.pos.getPosData({ tid: ctx.tender.id }) : [];
  255. // const convert = new billsPosConvert(ctx);
  256. // convert.loadData(ledgerData, posData, []);
  257. // const result = await convert.convert();
  258. const scheduleLedgerMonthData = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  259. const scheduleLedgerHistoryData = await ctx.service.scheduleLedgerHistory.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  260. ctx.body = { err: 0, msg: '', data: { bills: ledgerData, slm: scheduleLedgerMonthData, slh: scheduleLedgerHistoryData } };
  261. } catch (err) {
  262. this.log(err);
  263. ctx.body = { err: 1, msg: err.toString(), data: [] };
  264. }
  265. }
  266. /**
  267. * 台账选中提交(Ajax)
  268. *
  269. * @param ctx
  270. * @return {Promise<void>}
  271. */
  272. async saveLedger(ctx) {
  273. try {
  274. await this._checkScheduleCanModify(ctx);
  275. const data = JSON.parse(ctx.request.body.data);
  276. const result = await ctx.service.scheduleLedger.saveLedger(data);
  277. ctx.body = { err: 0, msg: '', data: result };
  278. } catch (err) {
  279. this.log(err);
  280. ctx.body = { err: 1, msg: err.toString(), data: [] };
  281. }
  282. }
  283. /**
  284. * 计划进度计算方式提交(Ajax)
  285. *
  286. * @param ctx
  287. * @return {Promise<void>}
  288. */
  289. async savePlan(ctx) {
  290. try {
  291. await this._checkScheduleCanModify(ctx);
  292. const data = JSON.parse(ctx.request.body.data);
  293. const responseData = {
  294. err: 0,
  295. msg: '',
  296. data: {},
  297. };
  298. switch (data.type) {
  299. case 'mode':
  300. responseData.data = await ctx.service.schedule.saveMode(data.postData);
  301. break;
  302. case 'addmonth':
  303. responseData.data = await ctx.service.scheduleMonth.add(data.postData);
  304. break;
  305. case 'delmonth':
  306. responseData.data = await ctx.service.scheduleMonth.del(data.postData);
  307. break;
  308. case 'ledger_edit':
  309. responseData.data = await ctx.service.scheduleLedgerMonth.save(data.postData);
  310. break;
  311. default: throw '参数有误';
  312. }
  313. ctx.body = responseData;
  314. } catch (err) {
  315. this.log(err);
  316. ctx.body = { err: 1, msg: err.toString(), data: null };
  317. }
  318. }
  319. /**
  320. * 计量进度金额模式计算方式提交(Ajax)
  321. *
  322. * @param ctx
  323. * @return {Promise<void>}
  324. */
  325. async saveStageTp(ctx) {
  326. try {
  327. await this._checkScheduleCanModify(ctx);
  328. const data = JSON.parse(ctx.request.body.data);
  329. const responseData = {
  330. err: 0,
  331. msg: '',
  332. data: {},
  333. };
  334. switch (data.type) {
  335. case 'add_stage':
  336. responseData.data = await ctx.service.scheduleStage.add(data.postData);
  337. break;
  338. case 'del_stage':
  339. responseData.data = await ctx.service.scheduleStage.del(data.postData);
  340. break;
  341. case 'reload_stage':
  342. responseData.data = await ctx.service.scheduleStage.changeOrder(data.postData);
  343. break;
  344. default: throw '参数有误';
  345. }
  346. ctx.body = responseData;
  347. } catch (err) {
  348. this.log(err);
  349. ctx.body = { err: 1, msg: err.toString(), data: null };
  350. }
  351. }
  352. /**
  353. * 计量进度工程量模式计算方式提交(Ajax)
  354. *
  355. * @param ctx
  356. * @return {Promise<void>}
  357. */
  358. async saveStageGcl(ctx) {
  359. try {
  360. await this._checkScheduleCanModify(ctx);
  361. const data = JSON.parse(ctx.request.body.data);
  362. const responseData = {
  363. err: 0,
  364. msg: '',
  365. data: {},
  366. };
  367. switch (data.type) {
  368. case 'add_stage':
  369. responseData.data = await ctx.service.scheduleMonth.addStageUsed(data.postData);
  370. break;
  371. case 'del_stage':
  372. responseData.data = await ctx.service.scheduleMonth.delStageUsed(data.postData);
  373. break;
  374. case 'ledger_edit':
  375. responseData.data = await ctx.service.scheduleLedgerMonth.saveSj(data.postData);
  376. break;
  377. default: throw '参数有误';
  378. }
  379. ctx.body = responseData;
  380. } catch (err) {
  381. this.log(err);
  382. ctx.body = { err: 1, msg: err.toString(), data: null };
  383. }
  384. }
  385. }
  386. return ScheduleController;
  387. };