schedule_controller.js 19 KB

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