schedule_controller.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. if (!ctx.tender.user_permission.schedule.edit) {
  34. throw '权限不足,无法修改进度数据';
  35. }
  36. }
  37. async tender(ctx) {
  38. try {
  39. if (!ctx.subProject.page_show.xxjd) throw '该功能已关闭';
  40. const renderData = {
  41. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.tender),
  42. };
  43. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  44. renderData.accountList = accountList;
  45. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  46. const accountGroupList = unitList.map(item => {
  47. const groupList = accountList.filter(item1 => item1.company === item.name);
  48. return { groupName: item.name, groupList };
  49. }).filter(x => { return x.groupList.length > 0; });
  50. // const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  51. // renderData.accountGroup = unitList.map(item => {
  52. // const groupList = accountList.filter(item1 => item1.company === item.name);
  53. // return { groupName: item.name, groupList };
  54. // });
  55. renderData.accountGroup = accountGroupList;
  56. renderData.accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  57. renderData.tenderList = await ctx.service.tender.getSpecList(ctx.service.tenderPermission, 'schedule', ctx.session.sessionUser.is_admin ? 'all' : '');
  58. renderData.categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  59. renderData.selfCategoryLevel = this.ctx.subProject.permission.self_category_level;
  60. renderData.permissionConst = ctx.service.tenderPermission.partPermissionConst('schedule');
  61. renderData.permissionBlock = ctx.service.tenderPermission.partPermissionBlock('schedule');
  62. await this.layout('schedule/tender.ejs', renderData, 'schedule/tender_modal.ejs');
  63. } catch (err) {
  64. ctx.log(err);
  65. ctx.postError(err, '无法查看标段进度数据');
  66. ctx.redirect(`/sp/${ctx.subProject.id}/dashboard`);
  67. }
  68. }
  69. async index(ctx) {
  70. try {
  71. const schedule = await ctx.service.schedule.getDataByCondition({ tid: ctx.tender.id });
  72. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: ctx.tender.id }, orders: [['yearmonth', 'asc']] });
  73. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: ctx.tender.id }, orders: [['yearmonth', 'asc']] });
  74. // 汇总并统计前几个计划月总计划额
  75. for (const i in scheduleStage) {
  76. let nowIndex = 0;
  77. let lastIndex = 0;
  78. if (i > 0) {
  79. nowIndex = _.findIndex(scheduleMonth, { yearmonth: scheduleStage[i].yearmonth }) + 1;
  80. lastIndex = _.findIndex(scheduleMonth, { yearmonth: scheduleStage[i - 1].yearmonth }) + 1;
  81. } else {
  82. nowIndex = _.findIndex(scheduleMonth, { yearmonth: scheduleStage[i].yearmonth }) + 1;
  83. }
  84. // 获取新计划月数组
  85. const newSm = scheduleMonth.slice(lastIndex, nowIndex);
  86. scheduleStage[i].plan_tp = _.sumBy(newSm, 'plan_tp');
  87. }
  88. const renderData = {
  89. schedule,
  90. scheduleMonth,
  91. scheduleStage,
  92. tender: ctx.tender.data,
  93. tenderMenu: this.menu.tenderMenu,
  94. planMonth: await this._getLastPlanMonth(ctx),
  95. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  96. preUrl: '/tender/' + ctx.tender.id,
  97. scPermission: scheduleConst.permission,
  98. revising: await this._getLastReviseStatus(ctx),
  99. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.index),
  100. };
  101. await this.layout('schedule/index.ejs', renderData, 'schedule/modal.ejs');
  102. } catch (err) {
  103. this.log(err);
  104. ctx.redirect(this.menu.menu.dashboard.url);
  105. }
  106. }
  107. async ledger(ctx) {
  108. const tender = ctx.tender;
  109. const schedule = await ctx.service.schedule.getDataByCondition({ tid: ctx.tender.id });
  110. const scheduleLedgerList = await this._getSelectedLedgerList(ctx);
  111. const allSlmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  112. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: tender.id }, orders: [['order', 'desc']] });
  113. const hadDataLidList = [];
  114. for (const sl of scheduleLedgerList) {
  115. const info = _.find(allSlmList, function(item) {
  116. return item.lid === sl && ((item.plan_tp !== null && item.plan_tp !== 0) ||
  117. (item.plan_gcl !== null && item.plan_gcl !== 0) ||
  118. (item.sj_tp !== null && item.sj_tp !== 0) ||
  119. (item.sj_gcl !== null && item.sj_gcl !== 0));
  120. });
  121. if (info) {
  122. hadDataLidList.push(info.lid);
  123. }
  124. }
  125. const renderData = {
  126. schedule,
  127. tender: tender.data,
  128. tenderInfo: tender.info,
  129. measureType,
  130. scheduleLedgerList,
  131. hadDataLidList,
  132. scheduleStage,
  133. scPermission: scheduleConst.permission,
  134. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.ledger),
  135. revising: await this._getLastReviseStatus(ctx),
  136. };
  137. await this.layout('schedule/ledger.ejs', renderData, 'schedule/modal.ejs');
  138. }
  139. async plan(ctx) {
  140. const tender = ctx.tender;
  141. const schedule = await ctx.service.schedule.getDataByCondition({ tid: tender.id });
  142. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  143. const renderData = {
  144. tender: tender.data,
  145. tenderInfo: tender.info,
  146. schedule,
  147. scheduleMonth,
  148. planMonth: await this._getLastPlanMonth(ctx),
  149. measureType,
  150. mode: scheduleConst.plan_mode,
  151. scPermission: scheduleConst.permission,
  152. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  153. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.plan),
  154. revising: await this._getLastReviseStatus(ctx),
  155. };
  156. await this.layout('schedule/plan.ejs', renderData, 'schedule/plan_modal.ejs');
  157. }
  158. async stageTp(ctx) {
  159. const tender = ctx.tender;
  160. const schedule = await ctx.service.schedule.getDataByCondition({ tid: tender.id });
  161. const { slmList, nextSlmList, endSlmList, yearSlmList, curYearStageData,
  162. scheduleMonth, stageOrderList, scheduleStage, curScheduleStage } = await this._getStageAndPlanData(ctx);
  163. const renderData = {
  164. tender: tender.data,
  165. tenderInfo: tender.info,
  166. schedule,
  167. scheduleMonth,
  168. measureType,
  169. stageOrderList,
  170. scheduleStage,
  171. curScheduleStage,
  172. slmList,
  173. nextSlmList,
  174. endSlmList,
  175. yearSlmList,
  176. curYearStageData,
  177. scPermission: scheduleConst.permission,
  178. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  179. revising: await this._getLastReviseStatus(ctx),
  180. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.stageTp),
  181. };
  182. await this.layout('schedule/stage_tp.ejs', renderData, 'schedule/stage_tp_modal.ejs');
  183. }
  184. async stageGcl(ctx) {
  185. const tender = ctx.tender;
  186. const schedule = await ctx.service.schedule.getDataByCondition({ tid: tender.id });
  187. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  188. // const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: tender.id }, orders: [['order', 'desc']] });
  189. const gclScheduleMonth = _.orderBy(_.filter(scheduleMonth, { stage_gcl_used: 1 }), ['yearmonth'], ['desc']);
  190. const curScheduleMonth = scheduleMonth.length > 0 ? _.maxBy(gclScheduleMonth, 'yearmonth') : null;
  191. const renderData = {
  192. tender: tender.data,
  193. tenderInfo: tender.info,
  194. schedule,
  195. scheduleMonth,
  196. measureType,
  197. // scheduleStage,
  198. curScheduleMonth,
  199. gclScheduleMonth,
  200. scPermission: scheduleConst.permission,
  201. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  202. revising: await this._getLastReviseStatus(ctx),
  203. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.stageGcl),
  204. };
  205. await this.layout('schedule/stage_gcl.ejs', renderData, 'schedule/stage_gcl_modal.ejs');
  206. }
  207. /**
  208. * 获取金额模式下台账数据(Ajax)
  209. *
  210. * @param ctx
  211. * @return {Promise<void>}
  212. */
  213. async loadTpLedgerData(ctx) {
  214. try {
  215. const ledgerData = await this._getStageLedgerData(ctx, ctx.params.order);
  216. const postData = { ledgerData };
  217. const data = JSON.parse(ctx.request.body.data);
  218. if (data.filter && data.filter === 'gcl') {
  219. const { slmList, nextSlmList, endSlmList, yearSlmList, curYearStageData } = await this._getStageAndPlanData(ctx);
  220. _.assignIn(postData, { slmList, nextSlmList, endSlmList, yearSlmList, curYearStageData });
  221. }
  222. ctx.body = { err: 0, msg: '', data: postData };
  223. } catch (err) {
  224. this.log(err);
  225. ctx.body = { err: 1, msg: err.toString(), data: [] };
  226. }
  227. }
  228. /**
  229. * 获取工程量模式下台账数据(Ajax)
  230. *
  231. * @param ctx
  232. * @return {Promise<void>}
  233. */
  234. async loadGclLedgerData(ctx) {
  235. try {
  236. const ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
  237. const postData = { ledgerData };
  238. const { slmList, nextSlmList, endSlmList, yearSlmList } = await this._getGclAndPlanData(ctx);
  239. _.assignIn(postData, { slmList, nextSlmList, endSlmList, yearSlmList });
  240. ctx.body = { err: 0, msg: '', data: postData };
  241. } catch (err) {
  242. this.log(err);
  243. ctx.body = { err: 1, msg: err.toString(), data: [] };
  244. }
  245. }
  246. /**
  247. * 获取所有期下台账数据(Ajax)
  248. *
  249. * @param ctx
  250. * @return {Promise<void>}
  251. */
  252. async _getAllStageLedgerData(ctx) {
  253. let ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
  254. const stageList = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: ctx.tender.id }, orders: [['order', 'desc']] });
  255. if (stageList.length > 0) {
  256. const dgnData = await ctx.service.stageBillsDgn.getDgnData(ctx.tender.id);
  257. for (const d of dgnData) {
  258. const l = ctx.app._.find(ledgerData, { id: d.id });
  259. ctx.app._.assignIn(l, d);
  260. }
  261. for (const s of stageList) {
  262. const stageInfo = await ctx.service.stage.getDataByCondition({
  263. tid: ctx.tender.id,
  264. order: s.order,
  265. });
  266. // let preStageData;
  267. // 当前操作人查看最新数据,其他人查看历史数据
  268. const curStageData = await ctx.service.stageBills.getLastestStageData2(ctx.tender.id, stageInfo.id);
  269. // // 查询截止上期数据
  270. // if (stageInfo.order > 1) {
  271. // preStageData = await ctx.service.stageBillsFinal.getFinalData(ctx.tender.data, stageInfo.order - 1);
  272. // } else {
  273. // preStageData = [];
  274. // }
  275. this.ctx.helper.assignRelaData(ledgerData, [
  276. { data: curStageData, fields: ['contract_tp', 'qc_tp'], prefix: s.order + '_', relaId: 'lid' },
  277. // { data: preStageData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'used'], prefix: s.order + '_pre_', relaId: 'lid' },
  278. ]);
  279. }
  280. ledgerData = await this._addFinalStageData(ctx, ledgerData);
  281. }
  282. return ledgerData;
  283. }
  284. async _getStageLedgerData(ctx, stageOrder) {
  285. let ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
  286. const dgnData = await ctx.service.stageBillsDgn.getDgnData(ctx.tender.id);
  287. for (const d of dgnData) {
  288. const l = ctx.app._.find(ledgerData, { id: d.id });
  289. ctx.app._.assignIn(l, d);
  290. }
  291. const stageInfo = await ctx.service.stage.getDataByCondition({
  292. tid: ctx.tender.id,
  293. order: stageOrder,
  294. });
  295. await ctx.service.stage.loadPreCheckedStage(stageInfo);
  296. // 当前操作人查看最新数据,其他人查看历史数据
  297. const curStageData = await ctx.service.stageBills.getLastestStageData2(ctx.tender.id, stageInfo.id);
  298. const preStageData = stageInfo.preCheckedStage ? await ctx.service.stageBillsFinal.getFinalData(ctx.tender.data, stageInfo.preCheckedStage.order) : [];
  299. const pcData = await ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stageInfo.id } });
  300. this.ctx.helper.assignRelaData(ledgerData, [
  301. { data: curStageData, fields: ['contract_qty', 'contract_expr', 'contract_tp', 'qc_qty', 'qc_tp', 'postil'], prefix: '', relaId: 'lid' },
  302. { data: preStageData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'used'], prefix: 'pre_', relaId: 'lid' },
  303. { data: pcData, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp', 'org_price'], prefix: '', relaId: 'lid' },
  304. ]);
  305. // 获取进度最新期的数据
  306. ledgerData = await this._addFinalStageData(ctx, ledgerData);
  307. return ledgerData;
  308. }
  309. /**
  310. * 添加最新一期的进度期下期数据到台账数据中(Ajax)
  311. *
  312. * @param ctx
  313. * @return {Promise<void>}
  314. */
  315. async _addFinalStageData(ctx, ledgerData) {
  316. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: ctx.tender.id }, orders: [['order', 'desc']] });
  317. if (scheduleStage && scheduleStage.length > 0) {
  318. let prefinalStageData;
  319. const finalStageInfo = await ctx.service.stage.getDataByCondition({
  320. tid: ctx.tender.id,
  321. order: scheduleStage[0].order,
  322. });
  323. const finalStageData = await ctx.service.stageBills.getLastestStageData2(ctx.tender.id, finalStageInfo.id);
  324. if (finalStageInfo.order > 1) {
  325. prefinalStageData = await ctx.service.stageBillsFinal.getFinalData(ctx.tender.data, finalStageInfo.order - 1);
  326. } else {
  327. prefinalStageData = [];
  328. }
  329. const finalPcData = await ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: finalStageInfo.id } });
  330. this.ctx.helper.assignRelaData(ledgerData, [
  331. { data: finalStageData, fields: ['contract_qty', 'contract_expr', 'contract_tp', 'qc_qty', 'qc_tp', 'postil'], prefix: 'final_', relaId: 'lid' },
  332. { data: prefinalStageData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'used'], prefix: 'pre_final_', relaId: 'lid' },
  333. { data: finalPcData, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp', 'org_price'], prefix: 'final_', relaId: 'lid' },
  334. ]);
  335. }
  336. return ledgerData;
  337. }
  338. /**
  339. * 获取本期下台账计量和计划数据(Ajax)
  340. *
  341. * @param ctx
  342. * @return {Promise<void>}
  343. */
  344. async _getStageAndPlanData(ctx) {
  345. const tender = ctx.tender;
  346. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  347. const stageOrderList = await ctx.service.stage.getAllDataByCondition({ columns: ['id', 's_time', 'order'], where: { tid: tender.id } });
  348. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'desc']] });
  349. let curScheduleStage = scheduleStage.length > 0 ? _.maxBy(scheduleStage, 'yearmonth') : null;
  350. if (ctx.params.order && scheduleStage.length > 0) {
  351. curScheduleStage = _.find(scheduleStage, { order: parseInt(ctx.params.order) });
  352. }
  353. let slmList = [];
  354. let nextSlmList = [];
  355. let endSlmList = [];
  356. let yearSlmList = [];
  357. let curYearStageData = [];
  358. if (curScheduleStage) {
  359. const newSM = _.sortBy(scheduleMonth, 'yearmonth');
  360. const nowScheduleStage = _.findIndex(newSM, { yearmonth: curScheduleStage.yearmonth });
  361. slmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: curScheduleStage.yearmonth } });
  362. const nextScheduleStage = nowScheduleStage >= 0 && nowScheduleStage + 1 <= newSM.length - 1 ? newSM[nowScheduleStage + 1] : null;
  363. if (nextScheduleStage) nextSlmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: nextScheduleStage.yearmonth } });
  364. if (nowScheduleStage === 0) {
  365. endSlmList = slmList;
  366. } else if (nowScheduleStage > 0) {
  367. const endYearmonthCollection = _.map(_.take(newSM, nowScheduleStage + 1), 'yearmonth');
  368. endSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, endYearmonthCollection);
  369. }
  370. const yearConllection = _.map(_.filter(newSM, function(item) {
  371. return item.yearmonth.indexOf(curScheduleStage.yearmonth.split('-')[0]) !== -1;
  372. }), 'yearmonth');
  373. yearSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, yearConllection);
  374. // 获取本年完成计量数据
  375. const curStage = _.find(stageOrderList, { order: curScheduleStage.order });
  376. const stageList = _.filter(stageOrderList, function(item) {
  377. return item.s_time.indexOf(curStage.s_time.split('-')[0]) !== -1;
  378. });
  379. // const newSS = _.sortBy(scheduleStage, 'yearmonth');
  380. // const stageIdList = _.map(_.filter(stageList, function(item) {
  381. // return _.find(newSS, { order: item.order });
  382. // }), 'id');
  383. const stageIdList = _.map(stageList, 'id');
  384. curYearStageData = await ctx.service.stageBills.getStagesData(tender.id, stageIdList.join(','));
  385. const curYearStagePcData = await ctx.service.stageBillsPc.getStagesData(tender.id, stageIdList.join(','));
  386. for (const cys of curYearStageData) {
  387. const cyspc = ctx.helper._.find(curYearStagePcData, { lid: cys.lid });
  388. if (cyspc) ctx.helper._.assign(cys, cyspc);
  389. }
  390. }
  391. return { slmList, nextSlmList, endSlmList, yearSlmList, curYearStageData, scheduleMonth, stageOrderList, scheduleStage, curScheduleStage };
  392. }
  393. /**
  394. * 获取工程量模式汇总下台账的计划数据(Ajax)
  395. *
  396. * @param ctx
  397. * @return {Promise<void>}
  398. */
  399. async _getGclAndPlanData(ctx) {
  400. const tender = ctx.tender;
  401. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  402. let curScheduleMonth = scheduleMonth.length > 0 ? _.maxBy(scheduleMonth, 'order') : null;
  403. const gclScheduleMonth = _.filter(scheduleMonth, { stage_gcl_used: 1 });
  404. if (ctx.params.order && gclScheduleMonth.length > 0) {
  405. curScheduleMonth = _.find(gclScheduleMonth, { id: parseInt(ctx.params.order) });
  406. }
  407. let slmList = [];
  408. let nextSlmList = [];
  409. let endSlmList = [];
  410. let yearSlmList = [];
  411. if (curScheduleMonth) {
  412. const newSM = _.sortBy(scheduleMonth, 'yearmonth');
  413. const nowScheduleMonth = _.findIndex(newSM, { yearmonth: curScheduleMonth.yearmonth });
  414. slmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: curScheduleMonth.yearmonth } });
  415. const nextScheduleMonth = nowScheduleMonth >= 0 && nowScheduleMonth + 1 <= newSM.length - 1 ? newSM[nowScheduleMonth + 1] : null;
  416. if (nextScheduleMonth) nextSlmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: nextScheduleMonth.yearmonth } });
  417. if (nowScheduleMonth === 0) {
  418. endSlmList = slmList;
  419. } else if (nowScheduleMonth > 0) {
  420. const endYearmonthCollection = _.map(_.take(newSM, nowScheduleMonth + 1), 'yearmonth');
  421. endSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, endYearmonthCollection);
  422. }
  423. const yearConllection = _.map(_.filter(newSM, function(item) {
  424. return item.yearmonth.indexOf(curScheduleMonth.yearmonth.split('-')[0]) !== -1;
  425. }), 'yearmonth');
  426. yearSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, yearConllection);
  427. }
  428. return { slmList, nextSlmList, endSlmList, yearSlmList, scheduleMonth, curScheduleMonth };
  429. }
  430. /**
  431. * 获取台账数据(Ajax)
  432. *
  433. * @param ctx
  434. * @return {Promise<void>}
  435. */
  436. async loadLedgerData(ctx) {
  437. try {
  438. // const ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
  439. const ledgerData = await this._getAllStageLedgerData(ctx);
  440. // const posData = ctx.tender.data.measure_type === measureType.tz.value
  441. // ? await ctx.service.pos.getPosData({ tid: ctx.tender.id }) : [];
  442. // const convert = new billsPosConvert(ctx);
  443. // convert.loadData(ledgerData, posData, []);
  444. // const result = await convert.convert();
  445. const scheduleLedgerMonthData = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  446. const scheduleLedgerHistoryData = await ctx.service.scheduleLedgerHistory.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  447. ctx.body = { err: 0, msg: '', data: { bills: ledgerData, slm: scheduleLedgerMonthData, slh: scheduleLedgerHistoryData } };
  448. } catch (err) {
  449. this.log(err);
  450. ctx.body = { err: 1, msg: err.toString(), data: [] };
  451. }
  452. }
  453. /**
  454. * 台账选中提交(Ajax)
  455. *
  456. * @param ctx
  457. * @return {Promise<void>}
  458. */
  459. async saveLedger(ctx) {
  460. try {
  461. await this._checkScheduleCanModify(ctx);
  462. const data = JSON.parse(ctx.request.body.data);
  463. const result = await ctx.service.scheduleLedger.saveLedger(data);
  464. ctx.body = { err: 0, msg: '', data: result };
  465. } catch (err) {
  466. this.log(err);
  467. ctx.body = { err: 1, msg: err.toString(), data: [] };
  468. }
  469. }
  470. /**
  471. * 计划进度计算方式提交(Ajax)
  472. *
  473. * @param ctx
  474. * @return {Promise<void>}
  475. */
  476. async savePlan(ctx) {
  477. try {
  478. await this._checkScheduleCanModify(ctx);
  479. const data = JSON.parse(ctx.request.body.data);
  480. const responseData = {
  481. err: 0,
  482. msg: '',
  483. data: {},
  484. };
  485. switch (data.type) {
  486. case 'mode':
  487. responseData.data = await ctx.service.schedule.saveMode(data.postData);
  488. break;
  489. case 'addmonth':
  490. responseData.data = await ctx.service.scheduleMonth.add(data.postData);
  491. break;
  492. case 'delmonth':
  493. responseData.data = await ctx.service.scheduleMonth.del(data.postData);
  494. break;
  495. case 'ledger_edit':
  496. responseData.data = await ctx.service.scheduleLedgerMonth.save(data.postData);
  497. break;
  498. case 'ledger_paste':
  499. responseData.data = await ctx.service.scheduleLedgerMonth.saveDatas(data.postData);
  500. break;
  501. default: throw '参数有误';
  502. }
  503. ctx.body = responseData;
  504. } catch (err) {
  505. this.log(err);
  506. ctx.body = { err: 1, msg: err.toString(), data: null };
  507. }
  508. }
  509. /**
  510. * 计量进度金额模式计算方式提交(Ajax)
  511. *
  512. * @param ctx
  513. * @return {Promise<void>}
  514. */
  515. async saveStageTp(ctx) {
  516. try {
  517. await this._checkScheduleCanModify(ctx);
  518. const data = JSON.parse(ctx.request.body.data);
  519. const responseData = {
  520. err: 0,
  521. msg: '',
  522. data: {},
  523. };
  524. switch (data.type) {
  525. case 'add_stage':
  526. responseData.data = await ctx.service.scheduleStage.add(data.postData);
  527. break;
  528. case 'del_stage':
  529. responseData.data = await ctx.service.scheduleStage.del(data.postData);
  530. break;
  531. case 'reload_stage':
  532. responseData.data = await ctx.service.scheduleStage.changeOrder(data.postData);
  533. break;
  534. case 'update_tp':
  535. responseData.data = await ctx.service.scheduleStage.updateOneTp(data.postData);
  536. break;
  537. default: throw '参数有误';
  538. }
  539. ctx.body = responseData;
  540. } catch (err) {
  541. this.log(err);
  542. ctx.body = { err: 1, msg: err.toString(), data: null };
  543. }
  544. }
  545. /**
  546. * 计量进度工程量模式计算方式提交(Ajax)
  547. *
  548. * @param ctx
  549. * @return {Promise<void>}
  550. */
  551. async saveStageGcl(ctx) {
  552. try {
  553. await this._checkScheduleCanModify(ctx);
  554. const data = JSON.parse(ctx.request.body.data);
  555. const responseData = {
  556. err: 0,
  557. msg: '',
  558. data: {},
  559. };
  560. switch (data.type) {
  561. case 'add_stage':
  562. responseData.data = await ctx.service.scheduleMonth.addStageUsed(data.postData);
  563. break;
  564. case 'del_stage':
  565. responseData.data = await ctx.service.scheduleMonth.delStageUsed(data.postData);
  566. break;
  567. case 'ledger_edit':
  568. responseData.data = await ctx.service.scheduleLedgerMonth.saveSj(data.postData);
  569. break;
  570. case 'ledger_paste':
  571. responseData.data = await ctx.service.scheduleLedgerMonth.saveSjDatas(data.postData);
  572. break;
  573. default: throw '参数有误';
  574. }
  575. ctx.body = responseData;
  576. } catch (err) {
  577. this.log(err);
  578. ctx.body = { err: 1, msg: err.toString(), data: null };
  579. }
  580. }
  581. async saveAudit(ctx) {
  582. try {
  583. const data = JSON.parse(ctx.request.body.data);
  584. if (!data) {
  585. throw '提交数据错误';
  586. }
  587. // 判断修改权限
  588. if (ctx.session.sessionUser.is_admin === 0) {
  589. throw '你没有权限修改标段进度';
  590. }
  591. let info = '';
  592. switch (data.type) {
  593. case 'add':
  594. const result = await ctx.service.scheduleAudit.addAudit(data);
  595. if (!result) {
  596. throw '添加审批人失败';
  597. }
  598. info = result;
  599. break;
  600. case 'del':
  601. await ctx.service.scheduleAudit.removeAudit(data);
  602. break;
  603. case 'edit':
  604. await ctx.service.scheduleAudit.editAudit(data);
  605. break;
  606. default:break;
  607. }
  608. ctx.body = { err: 0, msg: '', data: info };
  609. } catch (err) {
  610. this.log(err);
  611. ctx.body = this.ajaxErrorBody(err, '保存审批流程设置失败');
  612. }
  613. }
  614. }
  615. return ScheduleController;
  616. };