schedule_controller.js 22 KB

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