schedule_controller.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: ctx.tender.id }, orders: [['yearmonth', 'asc']] });
  39. // 汇总并统计前几个计划月总计划额
  40. for (const i in scheduleStage) {
  41. let nowIndex = 0;
  42. let lastIndex = 0;
  43. if (i > 0) {
  44. nowIndex = _.findIndex(scheduleMonth, { yearmonth: scheduleStage[i].yearmonth }) + 1;
  45. lastIndex = _.findIndex(scheduleMonth, { yearmonth: scheduleStage[i - 1].yearmonth }) + 1;
  46. } else {
  47. nowIndex = _.findIndex(scheduleMonth, { yearmonth: scheduleStage[i].yearmonth }) + 1;
  48. }
  49. // 获取新计划月数组
  50. const newSm = scheduleMonth.slice(lastIndex, nowIndex);
  51. scheduleStage[i].plan_tp = _.sumBy(newSm, 'plan_tp');
  52. }
  53. const renderData = {
  54. schedule,
  55. scheduleMonth,
  56. scheduleStage,
  57. tender: ctx.tender.data,
  58. tenderMenu: this.menu.tenderMenu,
  59. planMonth: await this._getLastPlanMonth(ctx),
  60. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  61. preUrl: '/tender/' + ctx.tender.id,
  62. revising: await this._getLastReviseStatus(ctx),
  63. };
  64. await this.layout('schedule/index.ejs', renderData, 'schedule/modal.ejs');
  65. } catch (err) {
  66. this.log(err);
  67. ctx.redirect(this.menu.menu.dashboard.url);
  68. }
  69. }
  70. async ledger(ctx) {
  71. const tender = ctx.tender;
  72. const schedule = await ctx.service.schedule.getDataByCondition({ tid: ctx.tender.id });
  73. const scheduleLedgerList = await this._getSelectedLedgerList(ctx);
  74. const allSlmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  75. const hadDataLidList = [];
  76. for (const sl of scheduleLedgerList) {
  77. const info = _.find(allSlmList, function(item) {
  78. return item.lid === sl && ((item.plan_tp !== null && item.plan_tp !== 0) ||
  79. (item.plan_gcl !== null && item.plan_gcl !== 0) ||
  80. (item.sj_tp !== null && item.sj_tp !== 0) ||
  81. (item.sj_gcl !== null && item.sj_gcl !== 0));
  82. });
  83. if (info) {
  84. hadDataLidList.push(info.lid);
  85. }
  86. }
  87. const renderData = {
  88. schedule,
  89. tender: tender.data,
  90. tenderInfo: tender.info,
  91. measureType,
  92. scheduleLedgerList,
  93. hadDataLidList,
  94. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.ledger),
  95. revising: await this._getLastReviseStatus(ctx),
  96. };
  97. await this.layout('schedule/ledger.ejs', renderData, 'schedule/modal.ejs');
  98. }
  99. async plan(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 renderData = {
  104. tender: tender.data,
  105. tenderInfo: tender.info,
  106. schedule,
  107. scheduleMonth,
  108. planMonth: await this._getLastPlanMonth(ctx),
  109. measureType,
  110. mode: scheduleConst.plan_mode,
  111. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  112. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.plan),
  113. revising: await this._getLastReviseStatus(ctx),
  114. };
  115. await this.layout('schedule/plan.ejs', renderData, 'schedule/plan_modal.ejs');
  116. }
  117. async stageTp(ctx) {
  118. const tender = ctx.tender;
  119. const schedule = await ctx.service.schedule.getDataByCondition({ tid: tender.id });
  120. const { slmList, nextSlmList, endSlmList, yearSlmList, curYearStageData,
  121. scheduleMonth, stageOrderList, scheduleStage, curScheduleStage } = await this._getStageAndPlanData(ctx);
  122. const renderData = {
  123. tender: tender.data,
  124. tenderInfo: tender.info,
  125. schedule,
  126. scheduleMonth,
  127. measureType,
  128. stageOrderList,
  129. scheduleStage,
  130. curScheduleStage,
  131. slmList,
  132. nextSlmList,
  133. endSlmList,
  134. yearSlmList,
  135. curYearStageData,
  136. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  137. revising: await this._getLastReviseStatus(ctx),
  138. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.stageTp),
  139. };
  140. await this.layout('schedule/stage_tp.ejs', renderData, 'schedule/stage_tp_modal.ejs');
  141. }
  142. async stageGcl(ctx) {
  143. const tender = ctx.tender;
  144. const schedule = await ctx.service.schedule.getDataByCondition({ tid: tender.id });
  145. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  146. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: tender.id }, orders: [['order', 'desc']] });
  147. const curScheduleStage = scheduleStage.length > 0 ? _.maxBy(scheduleStage, 'order') : null;
  148. const renderData = {
  149. tender: tender.data,
  150. tenderInfo: tender.info,
  151. schedule,
  152. scheduleMonth,
  153. measureType,
  154. scheduleStage,
  155. curScheduleStage,
  156. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  157. revising: await this._getLastReviseStatus(ctx),
  158. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.stageGcl),
  159. };
  160. await this.layout('schedule/stage_gcl.ejs', renderData, 'schedule/stage_gcl_modal.ejs');
  161. }
  162. /**
  163. * 获取金额模式下台账数据(Ajax)
  164. *
  165. * @param ctx
  166. * @return {Promise<void>}
  167. */
  168. async loadTpLedgerData(ctx) {
  169. try {
  170. const ledgerData = await this._getStageLedgerData(ctx, ctx.params.order);
  171. const postData = { ledgerData };
  172. const data = JSON.parse(ctx.request.body.data);
  173. if (data.filter && data.filter === 'gcl') {
  174. const { slmList, nextSlmList, endSlmList, yearSlmList, curYearStageData } = await this._getStageAndPlanData(ctx);
  175. _.assignIn(postData, { slmList, nextSlmList, endSlmList, yearSlmList, curYearStageData });
  176. }
  177. ctx.body = { err: 0, msg: '', data: postData };
  178. } catch (err) {
  179. this.log(err);
  180. ctx.body = { err: 1, msg: err.toString(), data: [] };
  181. }
  182. }
  183. async _getStageLedgerData(ctx, stageOrder) {
  184. const ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
  185. const dgnData = await ctx.service.stageBillsDgn.getDgnData(ctx.tender.id);
  186. for (const d of dgnData) {
  187. const l = ctx.app._.find(ledgerData, { id: d.id });
  188. ctx.app._.assignIn(l, d);
  189. }
  190. const stageInfo = await ctx.service.stage.getDataByCondition({
  191. tid: ctx.tender.id,
  192. order: stageOrder,
  193. });
  194. let preStageData;
  195. // 当前操作人查看最新数据,其他人查看历史数据
  196. const curStageData = await ctx.service.stageBills.getLastestStageData(ctx.tender.id, stageInfo.id);
  197. // 查询截止上期数据
  198. if (stageInfo.order > 1) {
  199. preStageData = await ctx.service.stageBillsFinal.getFinalData(ctx.tender.data, stageInfo.order - 1);
  200. } else {
  201. preStageData = [];
  202. }
  203. this.ctx.helper.assignRelaData(ledgerData, [
  204. { data: curStageData, fields: ['contract_qty', 'contract_expr', 'contract_tp', 'qc_qty', 'qc_tp', 'postil'], prefix: '', relaId: 'lid' },
  205. { data: preStageData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'used'], prefix: 'pre_', relaId: 'lid' },
  206. ]);
  207. return ledgerData;
  208. }
  209. /**
  210. * 获取本期下台账计量和计划数据(Ajax)
  211. *
  212. * @param ctx
  213. * @return {Promise<void>}
  214. */
  215. async _getStageAndPlanData(ctx) {
  216. const tender = ctx.tender;
  217. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  218. const stageOrderList = await ctx.service.stage.getAllDataByCondition({ columns: ['id', 's_time', 'order'], where: { tid: tender.id } });
  219. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: tender.id }, orders: [['order', 'desc']] });
  220. let curScheduleStage = scheduleStage.length > 0 ? _.maxBy(scheduleStage, 'order') : null;
  221. if (ctx.params.order && scheduleStage.length > 0) {
  222. curScheduleStage = _.find(scheduleStage, { order: parseInt(ctx.params.order) });
  223. }
  224. let slmList = [];
  225. let nextSlmList = [];
  226. let endSlmList = [];
  227. let yearSlmList = [];
  228. let curYearStageData = [];
  229. if (curScheduleStage) {
  230. const newSM = _.sortBy(scheduleMonth, 'yearmonth');
  231. const nowScheduleStage = _.findIndex(newSM, { yearmonth: curScheduleStage.yearmonth });
  232. slmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: curScheduleStage.yearmonth } });
  233. const nextScheduleStage = nowScheduleStage >= 0 && nowScheduleStage + 1 <= newSM.length - 1 ? newSM[nowScheduleStage + 1] : null;
  234. if (nextScheduleStage) nextSlmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: nextScheduleStage.yearmonth } });
  235. if (nowScheduleStage === 0) {
  236. endSlmList = slmList;
  237. } else if (nowScheduleStage > 0) {
  238. const endYearmonthCollection = _.map(_.take(newSM, nowScheduleStage + 1), 'yearmonth');
  239. endSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, endYearmonthCollection);
  240. }
  241. const yearConllection = _.map(_.filter(newSM, function(item) {
  242. return item.yearmonth.indexOf(curScheduleStage.yearmonth.split('-')[0]) !== -1;
  243. }), 'yearmonth');
  244. yearSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, yearConllection);
  245. // 获取本年完成计量数据
  246. const curStage = _.find(stageOrderList, { order: curScheduleStage.order });
  247. const stageList = _.filter(stageOrderList, function(item) {
  248. return item.s_time.indexOf(curStage.s_time.split('-')[0]) !== -1;
  249. });
  250. const newSS = _.sortBy(scheduleStage, 'yearmonth');
  251. const stageIdList = _.map(_.filter(stageList, function(item) {
  252. return _.find(newSS, { order: item.order });
  253. }), 'id');
  254. curYearStageData = await ctx.service.stageBills.getStagesData(tender.id, stageIdList.join(','));
  255. }
  256. return { slmList, nextSlmList, endSlmList, yearSlmList, curYearStageData, scheduleMonth, stageOrderList, scheduleStage, curScheduleStage };
  257. }
  258. /**
  259. * 获取台账数据(Ajax)
  260. *
  261. * @param ctx
  262. * @return {Promise<void>}
  263. */
  264. async loadLedgerData(ctx) {
  265. try {
  266. const ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
  267. // const posData = ctx.tender.data.measure_type === measureType.tz.value
  268. // ? await ctx.service.pos.getPosData({ tid: ctx.tender.id }) : [];
  269. // const convert = new billsPosConvert(ctx);
  270. // convert.loadData(ledgerData, posData, []);
  271. // const result = await convert.convert();
  272. const scheduleLedgerMonthData = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  273. const scheduleLedgerHistoryData = await ctx.service.scheduleLedgerHistory.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  274. ctx.body = { err: 0, msg: '', data: { bills: ledgerData, slm: scheduleLedgerMonthData, slh: scheduleLedgerHistoryData } };
  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 saveLedger(ctx) {
  287. try {
  288. await this._checkScheduleCanModify(ctx);
  289. const data = JSON.parse(ctx.request.body.data);
  290. const result = await ctx.service.scheduleLedger.saveLedger(data);
  291. ctx.body = { err: 0, msg: '', data: result };
  292. } catch (err) {
  293. this.log(err);
  294. ctx.body = { err: 1, msg: err.toString(), data: [] };
  295. }
  296. }
  297. /**
  298. * 计划进度计算方式提交(Ajax)
  299. *
  300. * @param ctx
  301. * @return {Promise<void>}
  302. */
  303. async savePlan(ctx) {
  304. try {
  305. await this._checkScheduleCanModify(ctx);
  306. const data = JSON.parse(ctx.request.body.data);
  307. const responseData = {
  308. err: 0,
  309. msg: '',
  310. data: {},
  311. };
  312. switch (data.type) {
  313. case 'mode':
  314. responseData.data = await ctx.service.schedule.saveMode(data.postData);
  315. break;
  316. case 'addmonth':
  317. responseData.data = await ctx.service.scheduleMonth.add(data.postData);
  318. break;
  319. case 'delmonth':
  320. responseData.data = await ctx.service.scheduleMonth.del(data.postData);
  321. break;
  322. case 'ledger_edit':
  323. responseData.data = await ctx.service.scheduleLedgerMonth.save(data.postData);
  324. break;
  325. default: throw '参数有误';
  326. }
  327. ctx.body = responseData;
  328. } catch (err) {
  329. this.log(err);
  330. ctx.body = { err: 1, msg: err.toString(), data: null };
  331. }
  332. }
  333. /**
  334. * 计量进度金额模式计算方式提交(Ajax)
  335. *
  336. * @param ctx
  337. * @return {Promise<void>}
  338. */
  339. async saveStageTp(ctx) {
  340. try {
  341. await this._checkScheduleCanModify(ctx);
  342. const data = JSON.parse(ctx.request.body.data);
  343. const responseData = {
  344. err: 0,
  345. msg: '',
  346. data: {},
  347. };
  348. switch (data.type) {
  349. case 'add_stage':
  350. responseData.data = await ctx.service.scheduleStage.add(data.postData);
  351. break;
  352. case 'del_stage':
  353. responseData.data = await ctx.service.scheduleStage.del(data.postData);
  354. break;
  355. case 'reload_stage':
  356. responseData.data = await ctx.service.scheduleStage.changeOrder(data.postData);
  357. break;
  358. case 'update_tp':
  359. responseData.data = await ctx.service.scheduleStage.updateOneTp(data.postData);
  360. break;
  361. default: throw '参数有误';
  362. }
  363. ctx.body = responseData;
  364. } catch (err) {
  365. this.log(err);
  366. ctx.body = { err: 1, msg: err.toString(), data: null };
  367. }
  368. }
  369. /**
  370. * 计量进度工程量模式计算方式提交(Ajax)
  371. *
  372. * @param ctx
  373. * @return {Promise<void>}
  374. */
  375. async saveStageGcl(ctx) {
  376. try {
  377. await this._checkScheduleCanModify(ctx);
  378. const data = JSON.parse(ctx.request.body.data);
  379. const responseData = {
  380. err: 0,
  381. msg: '',
  382. data: {},
  383. };
  384. switch (data.type) {
  385. case 'add_stage':
  386. responseData.data = await ctx.service.scheduleMonth.addStageUsed(data.postData);
  387. break;
  388. case 'del_stage':
  389. responseData.data = await ctx.service.scheduleMonth.delStageUsed(data.postData);
  390. break;
  391. case 'ledger_edit':
  392. responseData.data = await ctx.service.scheduleLedgerMonth.saveSj(data.postData);
  393. break;
  394. default: throw '参数有误';
  395. }
  396. ctx.body = responseData;
  397. } catch (err) {
  398. this.log(err);
  399. ctx.body = { err: 1, msg: err.toString(), data: null };
  400. }
  401. }
  402. }
  403. return ScheduleController;
  404. };