schedule_controller.js 18 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 { slmList, nextSlmList, endSlmList, yearSlmList, curYearStageData,
  105. scheduleMonth, stageOrderList, scheduleStage, curScheduleStage } = await this._getStageAndPlanData(ctx);
  106. const renderData = {
  107. tender: tender.data,
  108. tenderInfo: tender.info,
  109. schedule,
  110. scheduleMonth,
  111. measureType,
  112. stageOrderList,
  113. scheduleStage,
  114. curScheduleStage,
  115. slmList,
  116. nextSlmList,
  117. endSlmList,
  118. yearSlmList,
  119. curYearStageData,
  120. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  121. revising: await this._getLastReviseStatus(ctx),
  122. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.stageTp),
  123. };
  124. await this.layout('schedule/stage_tp.ejs', renderData, 'schedule/stage_tp_modal.ejs');
  125. }
  126. async stageGcl(ctx) {
  127. const tender = ctx.tender;
  128. const schedule = await ctx.service.schedule.getDataByCondition({ tid: tender.id });
  129. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  130. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: tender.id }, orders: [['order', 'desc']] });
  131. const curScheduleStage = scheduleStage.length > 0 ? _.maxBy(scheduleStage, 'order') : null;
  132. const renderData = {
  133. tender: tender.data,
  134. tenderInfo: tender.info,
  135. schedule,
  136. scheduleMonth,
  137. measureType,
  138. scheduleStage,
  139. curScheduleStage,
  140. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  141. revising: await this._getLastReviseStatus(ctx),
  142. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.stageGcl),
  143. };
  144. await this.layout('schedule/stage_gcl.ejs', renderData, 'schedule/stage_gcl_modal.ejs');
  145. }
  146. /**
  147. * 获取金额模式下台账数据(Ajax)
  148. *
  149. * @param ctx
  150. * @return {Promise<void>}
  151. */
  152. async loadTpLedgerData(ctx) {
  153. try {
  154. const ledgerData = await this._getStageLedgerData(ctx, ctx.params.order);
  155. const postData = { ledgerData };
  156. const data = JSON.parse(ctx.request.body.data);
  157. if (data.filter && data.filter === 'gcl') {
  158. const { slmList, nextSlmList, endSlmList, yearSlmList, curYearStageData } = await this._getStageAndPlanData(ctx);
  159. _.assignIn(postData, { slmList, nextSlmList, endSlmList, yearSlmList, curYearStageData });
  160. }
  161. ctx.body = { err: 0, msg: '', data: postData };
  162. } catch (err) {
  163. this.log(err);
  164. ctx.body = { err: 1, msg: err.toString(), data: [] };
  165. }
  166. }
  167. async _getStageLedgerData(ctx, stageOrder) {
  168. const ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
  169. const dgnData = await ctx.service.stageBillsDgn.getDgnData(ctx.tender.id);
  170. for (const d of dgnData) {
  171. const l = ctx.app._.find(ledgerData, { id: d.id });
  172. ctx.app._.assignIn(l, d);
  173. }
  174. const stageInfo = await ctx.service.stage.getDataByCondition({
  175. tid: ctx.tender.id,
  176. order: stageOrder,
  177. });
  178. let preStageData;
  179. // 当前操作人查看最新数据,其他人查看历史数据
  180. const curStageData = await ctx.service.stageBills.getLastestStageData(ctx.tender.id, stageInfo.id);
  181. // 查询截止上期数据
  182. if (stageInfo.order > 1) {
  183. preStageData = await ctx.service.stageBillsFinal.getFinalData(ctx.tender.data, stageInfo.order - 1);
  184. } else {
  185. preStageData = [];
  186. }
  187. this.ctx.helper.assignRelaData(ledgerData, [
  188. { data: curStageData, fields: ['contract_qty', 'contract_expr', 'contract_tp', 'qc_qty', 'qc_tp', 'postil'], prefix: '', relaId: 'lid' },
  189. { data: preStageData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'used'], prefix: 'pre_', relaId: 'lid' },
  190. ]);
  191. return ledgerData;
  192. }
  193. /**
  194. * 获取本期下台账计量和计划数据(Ajax)
  195. *
  196. * @param ctx
  197. * @return {Promise<void>}
  198. */
  199. async _getStageAndPlanData(ctx) {
  200. const tender = ctx.tender;
  201. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  202. const stageOrderList = await ctx.service.stage.getAllDataByCondition({ columns: ['id', 's_time', 'order'], where: { tid: tender.id } });
  203. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: tender.id }, orders: [['order', 'desc']] });
  204. let curScheduleStage = scheduleStage.length > 0 ? _.maxBy(scheduleStage, 'order') : null;
  205. if (ctx.params.order && scheduleStage.length > 0) {
  206. curScheduleStage = _.find(scheduleStage, { order: parseInt(ctx.params.order) });
  207. }
  208. let slmList = [];
  209. let nextSlmList = [];
  210. let endSlmList = [];
  211. let yearSlmList = [];
  212. let curYearStageData = [];
  213. if (curScheduleStage) {
  214. const newSM = _.sortBy(scheduleMonth, 'yearmonth');
  215. const nowScheduleStage = _.findIndex(newSM, { yearmonth: curScheduleStage.yearmonth });
  216. slmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: curScheduleStage.yearmonth } });
  217. const nextScheduleStage = nowScheduleStage >= 0 && nowScheduleStage + 1 <= newSM.length - 1 ? newSM[nowScheduleStage + 1] : null;
  218. if (nextScheduleStage) nextSlmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: nextScheduleStage.yearmonth } });
  219. if (nowScheduleStage === 0) {
  220. endSlmList = slmList;
  221. } else if (nowScheduleStage > 0) {
  222. const endYearmonthCollection = _.map(_.take(newSM, nowScheduleStage + 1), 'yearmonth');
  223. endSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, endYearmonthCollection);
  224. }
  225. const yearConllection = _.map(_.filter(newSM, function(item) {
  226. return item.yearmonth.indexOf(curScheduleStage.yearmonth.split('-')[0]) !== -1;
  227. }), 'yearmonth');
  228. yearSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, yearConllection);
  229. // 获取本年完成计量数据
  230. const curStage = _.find(stageOrderList, { order: curScheduleStage.order });
  231. const stageList = _.filter(stageOrderList, function(item) {
  232. return item.s_time.indexOf(curStage.s_time.split('-')[0]) !== -1;
  233. });
  234. const newSS = _.sortBy(scheduleStage, 'yearmonth');
  235. const stageIdList = _.map(_.filter(stageList, function(item) {
  236. return _.find(newSS, { order: item.order });
  237. }), 'id');
  238. curYearStageData = await ctx.service.stageBills.getStagesData(tender.id, stageIdList.join(','));
  239. }
  240. return { slmList, nextSlmList, endSlmList, yearSlmList, curYearStageData, scheduleMonth, stageOrderList, scheduleStage, curScheduleStage };
  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. const scheduleLedgerHistoryData = await ctx.service.scheduleLedgerHistory.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  258. ctx.body = { err: 0, msg: '', data: { bills: ledgerData, slm: scheduleLedgerMonthData, slh: scheduleLedgerHistoryData } };
  259. } catch (err) {
  260. this.log(err);
  261. ctx.body = { err: 1, msg: err.toString(), data: [] };
  262. }
  263. }
  264. /**
  265. * 台账选中提交(Ajax)
  266. *
  267. * @param ctx
  268. * @return {Promise<void>}
  269. */
  270. async saveLedger(ctx) {
  271. try {
  272. await this._checkScheduleCanModify(ctx);
  273. const data = JSON.parse(ctx.request.body.data);
  274. const result = await ctx.service.scheduleLedger.saveLedger(data);
  275. ctx.body = { err: 0, msg: '', data: result };
  276. } catch (err) {
  277. this.log(err);
  278. ctx.body = { err: 1, msg: err.toString(), data: [] };
  279. }
  280. }
  281. /**
  282. * 计划进度计算方式提交(Ajax)
  283. *
  284. * @param ctx
  285. * @return {Promise<void>}
  286. */
  287. async savePlan(ctx) {
  288. try {
  289. await this._checkScheduleCanModify(ctx);
  290. const data = JSON.parse(ctx.request.body.data);
  291. const responseData = {
  292. err: 0,
  293. msg: '',
  294. data: {},
  295. };
  296. switch (data.type) {
  297. case 'mode':
  298. responseData.data = await ctx.service.schedule.saveMode(data.postData);
  299. break;
  300. case 'addmonth':
  301. responseData.data = await ctx.service.scheduleMonth.add(data.postData);
  302. break;
  303. case 'delmonth':
  304. responseData.data = await ctx.service.scheduleMonth.del(data.postData);
  305. break;
  306. case 'ledger_edit':
  307. responseData.data = await ctx.service.scheduleLedgerMonth.save(data.postData);
  308. break;
  309. default: throw '参数有误';
  310. }
  311. ctx.body = responseData;
  312. } catch (err) {
  313. this.log(err);
  314. ctx.body = { err: 1, msg: err.toString(), data: null };
  315. }
  316. }
  317. /**
  318. * 计量进度金额模式计算方式提交(Ajax)
  319. *
  320. * @param ctx
  321. * @return {Promise<void>}
  322. */
  323. async saveStageTp(ctx) {
  324. try {
  325. await this._checkScheduleCanModify(ctx);
  326. const data = JSON.parse(ctx.request.body.data);
  327. const responseData = {
  328. err: 0,
  329. msg: '',
  330. data: {},
  331. };
  332. switch (data.type) {
  333. case 'add_stage':
  334. responseData.data = await ctx.service.scheduleStage.add(data.postData);
  335. break;
  336. case 'del_stage':
  337. responseData.data = await ctx.service.scheduleStage.del(data.postData);
  338. break;
  339. case 'reload_stage':
  340. responseData.data = await ctx.service.scheduleStage.changeOrder(data.postData);
  341. break;
  342. default: throw '参数有误';
  343. }
  344. ctx.body = responseData;
  345. } catch (err) {
  346. this.log(err);
  347. ctx.body = { err: 1, msg: err.toString(), data: null };
  348. }
  349. }
  350. /**
  351. * 计量进度工程量模式计算方式提交(Ajax)
  352. *
  353. * @param ctx
  354. * @return {Promise<void>}
  355. */
  356. async saveStageGcl(ctx) {
  357. try {
  358. await this._checkScheduleCanModify(ctx);
  359. const data = JSON.parse(ctx.request.body.data);
  360. const responseData = {
  361. err: 0,
  362. msg: '',
  363. data: {},
  364. };
  365. switch (data.type) {
  366. case 'add_stage':
  367. responseData.data = await ctx.service.scheduleMonth.addStageUsed(data.postData);
  368. break;
  369. case 'del_stage':
  370. responseData.data = await ctx.service.scheduleMonth.delStageUsed(data.postData);
  371. break;
  372. case 'ledger_edit':
  373. responseData.data = await ctx.service.scheduleLedgerMonth.saveSj(data.postData);
  374. break;
  375. default: throw '参数有误';
  376. }
  377. ctx.body = responseData;
  378. } catch (err) {
  379. this.log(err);
  380. ctx.body = { err: 1, msg: err.toString(), data: null };
  381. }
  382. }
  383. }
  384. return ScheduleController;
  385. };