spss_controller.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. 'use strict';
  2. /**
  3. * 标段对比 控制器
  4. *
  5. * @author Mai
  6. * @date 2020/2/21
  7. * @version
  8. */
  9. const measureType = require('../const/tender').measureType;
  10. const status = require('../const/audit').stage.status;
  11. const moment = require('moment');
  12. const stashCtrl = {
  13. first: true,
  14. };
  15. module.exports = app => {
  16. class SpssController extends app.BaseController {
  17. /**
  18. * 构造函数
  19. *
  20. * @param {Object} ctx - egg全局context
  21. * @return {void}
  22. */
  23. constructor(ctx) {
  24. super(ctx);
  25. ctx.showProject = true;
  26. }
  27. async info(ctx) {
  28. try {
  29. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  30. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'gather_info');
  31. const renderData = {
  32. categoryData,
  33. stashList,
  34. stashCtrl,
  35. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.gatherInfo)
  36. };
  37. await this.layout('spss/gather_info.ejs', renderData, 'spss/gather_info_modal.ejs');
  38. } catch (err) {
  39. ctx.helper.log(err);
  40. }
  41. }
  42. async gatherLedger(ctx) {
  43. try {
  44. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  45. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'gather_ledger');
  46. const renderData = {
  47. categoryData,
  48. stashList,
  49. stashCtrl,
  50. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.gatherLedger)
  51. };
  52. await this.layout('spss/gather_ledger.ejs', renderData, 'spss/spss_modal.ejs');
  53. } catch (err) {
  54. ctx.helper.log(err);
  55. }
  56. }
  57. async gatherStage(ctx) {
  58. try {
  59. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  60. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'gather_stage');
  61. const renderData = {
  62. categoryData,
  63. stashList,
  64. stashCtrl,
  65. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.gatherStage)
  66. };
  67. await this.layout('spss/gather_stage.ejs', renderData, 'spss/spss_modal.ejs');
  68. } catch (err) {
  69. ctx.helper.log(err);
  70. }
  71. }
  72. async gatherStageExtra(ctx) {
  73. try {
  74. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  75. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'gather_stage_extra');
  76. const renderData = {
  77. categoryData,
  78. stashList,
  79. stashCtrl,
  80. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.gatherStageExtra)
  81. };
  82. await this.layout('spss/gather_stage_extra.ejs', renderData, 'spss/spss_modal.ejs');
  83. } catch (err) {
  84. ctx.helper.log(err);
  85. }
  86. }
  87. async gatherStagePay(ctx) {
  88. try {
  89. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  90. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'gather_stage_pay');
  91. const renderData = {
  92. categoryData,
  93. stashList,
  94. stashCtrl,
  95. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.gatherStagePay)
  96. };
  97. await this.layout('spss/gather_stage_pay.ejs', renderData, 'spss/spss_modal.ejs');
  98. } catch(err) {
  99. ctx.log(err);
  100. }
  101. }
  102. async compareLedger(ctx) {
  103. try {
  104. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  105. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'compare_ledger');
  106. const renderData = {
  107. categoryData,
  108. stashList,
  109. stashCtrl,
  110. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.compareLedger)
  111. };
  112. await this.layout('spss/compare_ledger.ejs', renderData, 'spss/spss_modal.ejs');
  113. } catch (err) {
  114. ctx.helper.log(err);
  115. }
  116. }
  117. async compareStage(ctx) {
  118. try {
  119. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  120. const stashList = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, 'compare_stage');
  121. const renderData = {
  122. categoryData,
  123. stashList,
  124. stashCtrl,
  125. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.spss.compareStage),
  126. };
  127. await this.layout('spss/compare_stage.ejs', renderData, 'spss/spss_modal.ejs');
  128. } catch (err) {
  129. ctx.helper.log(err);
  130. }
  131. }
  132. async _loadStageTpData(info, tender, stage, loadPre = true) {
  133. if (!stage) return;
  134. if (stage.readOnly && stage.status === status.uncheck) return;
  135. if (loadPre) {
  136. info.pre_contract_tp = stage.pre_contract_tp;
  137. info.pre_qc_tp = stage.pre_qc_tp;
  138. info.pre_yf_tp = stage.pre_yf_tp;
  139. info.pre_sf_tp = stage.pre_sf_tp;
  140. }
  141. if (!stage.readOnly) await this.ctx.service.stage.checkStageGatherData(stage, this.ctx.session.sessionUser.is_admin);
  142. info.contract_tp = this.ctx.helper.add(stage.contract_tp, info.contract_tp);
  143. info.qc_tp = this.ctx.helper.add(stage.qc_tp, info.qc_tp);
  144. info.contract_pc_tp = this.ctx.helper.add(stage.contract_pc_tp, info.contract_pc_tp);
  145. info.qc_pc_tp = this.ctx.helper.add(stage.qc_pc_tp, info.qc_pc_tp);
  146. info.pc_tp = this.ctx.helper.add(stage.pc_tp, info.pc_tp);
  147. info.yf_tp = this.ctx.helper.add(stage.yf_tp, info.yf_tp);
  148. info.sf_tp = this.ctx.helper.add(stage.sf_tp, info.sf_tp);
  149. }
  150. async _loadFakeStageTpData(info, tender, stage) {
  151. if (!stage) return;
  152. if (!stage.readOnly) await this.ctx.service.stage.checkStageGatherData(stage, this.ctx.session.sessionUser.is_admin);
  153. info.pre_contract_tp = this.ctx.helper.sum([stage.contract_tp, stage.pre_contract_tp, stage.contract_pc_tp]);
  154. info.pre_qc_tp = this.ctx.helper.sum([stage.qc_tp, stage.pre_qc_tp, stage.qc_pc_tp]);
  155. info.pre_yf_tp = this.ctx.helper.add(stage.yf_tp, stage.pre_yf_tp);
  156. info.pre_sf_tp = this.ctx.helper.add(stage.sf_tp, stage.pre_sf_tp);
  157. }
  158. async _loadStagesTpData(info, tender, stages, preStage, endStage) {
  159. if (preStage) {
  160. info.pre_contract_tp = this.ctx.helper.sum([preStage.contract_tp, preStage.pre_contract_tp, preStage.contract_pc_tp]);
  161. info.pre_contract_tp = this.ctx.helper.sum([preStage.qc_tp, preStage.pre_qc_tp, preStage.qc_pc_tp]);
  162. info.pre_yf_tp = this.ctx.helper.add(preStage.yf_tp, preStage.pre_yf_tp);
  163. info.pre_sf_tp = this.ctx.helper.add(preStage.sf_tp, preStage.pre_sf_tp);
  164. }
  165. for (const stage of stages) {
  166. await this.ctx.service.stage.doCheckStage(stage);
  167. await this._loadStageTpData(info, tender, stage, false);
  168. }
  169. }
  170. async _loadInfoData(tender, filter) {
  171. const info = { measure_type_str: '', total_price: tender.total_price };
  172. if (tender.measure_type === measureType.tz.value) info.measure_type_str = measureType.tz.title;
  173. if (tender.measure_type === measureType.gcl.value) info.measure_type_str = measureType.gcl.title;
  174. await this.ctx.service.tenderCache.loadTenderCache(tender, this.ctx.session.sessionUser.accountId);
  175. info.contract_price = tender.contract_price;
  176. info.advance_tp = tender.advance_tp;
  177. info.change_tp = tender.change_tp;
  178. info.progress = tender.progress;
  179. if (filter.type === 'stage') {
  180. if (filter.stage.__fake) {
  181. await this._loadFakeStageTpData(info, tender, filter.stage);
  182. } else {
  183. await this._loadStageTpData(info, tender, filter.stage);
  184. }
  185. } else {
  186. await this._loadStagesTpData(info, tender, filter.stages, filter.preStage, filter.endStage);
  187. }
  188. info.pre_gather_tp = this.ctx.helper.add(info.pre_contract_tp, info.pre_qc_tp);
  189. info.gather_tp = this.ctx.helper.sum([info.contract_tp, info.qc_tp, info.pc_tp]);
  190. info.end_contract_tp = this.ctx.helper.sum([info.contract_tp, info.contract_pc_tp, info.pre_contract_tp]);
  191. info.end_qc_tp = this.ctx.helper.sum([info.qc_tp, info.qc_pc_tp, info.pre_qc_tp]);
  192. info.end_gather_tp = this.ctx.helper.add(info.gather_tp, info.pre_gather_tp);
  193. info.end_sf_tp = this.ctx.helper.add(info.sf_tp, info.pre_sf_tp);
  194. info.end_yf_tp = this.ctx.helper.add(info.yf_tp, info.pre_yf_tp);
  195. return info;
  196. }
  197. async _loadLedgerData(tender) {
  198. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  199. columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'full_path', 'is_leaf', 'order', 'code', 'b_code', 'name', 'unit', 'unit_price', 'quantity', 'total_price'],
  200. where: { tender_id: tender.id },
  201. });
  202. const pos = await this.ctx.service.pos.getAllDataByCondition({
  203. columns: ['id', 'lid', 'name', 'quantity', 'porder'],
  204. where: { tid: tender.id },
  205. });
  206. return [bills, pos];
  207. }
  208. async _loadDealBillsData(tender) {
  209. }
  210. async _getValidStages(tenderId, sort = 'desc', checked = false) {
  211. const accountId = this.ctx.session.sessionUser.accountId;
  212. const condition = { tid: tenderId };
  213. if (checked) condition.status = status.checked;
  214. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: condition, orders: [['order', sort]] });
  215. return stages.filter(s => { return s.status !== status.uncheck || s.user_id === accountId; });
  216. }
  217. async _filterOrder(tender, order, checked) {
  218. const stages = await this._getValidStages(tender.id, 'desc', checked);
  219. const stage = this.ctx.helper._.find(stages, { order });
  220. if (stage) await this.ctx.service.stage.doCheckStage(stage);
  221. return { type: 'stage', stage, filter: `第${order}期` };
  222. }
  223. async _filterMonth(tender, month, checked) {
  224. const stages = await this._getValidStages(tender.id, 'desc', checked);
  225. const stage = this.ctx.helper._.find(stages, { s_time: month });
  226. if (stage) await this.ctx.service.stage.doCheckStage(stage);
  227. return { type: 'stage', stage, filter: month };
  228. }
  229. async _filterUnderMonth(tender, month, checked) {
  230. const stages = await this._getValidStages(tender.id, 'desc', checked);
  231. let stage = this.ctx.helper._.find(stages, { s_time: month });
  232. if (!stage) {
  233. const checkTime = moment(month);
  234. for (const s of stages) {
  235. const rela_time = moment(s.s_time);
  236. if (rela_time.isBefore(checkTime)) {
  237. stage = s;
  238. break;
  239. }
  240. }
  241. }
  242. if (stage) await this.ctx.service.stage.doCheckStage(stage);
  243. return { type: 'stage', stage, filter: stage ? stage.s_time : '' };
  244. }
  245. async _filterFakeMonth(tender, month, checked) {
  246. const stages = await this._getValidStages(tender.id, 'desc', checked);
  247. let stage = this.ctx.helper._.find(stages, { s_time: month });
  248. if (!stage) {
  249. const checkTime = moment(month);
  250. for (const s of stages) {
  251. const rela_time = moment(s.s_time);
  252. if (rela_time.isBefore(checkTime)) {
  253. stage = s;
  254. stage.__fake = true;
  255. break;
  256. }
  257. }
  258. }
  259. if (stage) await this.ctx.service.stage.doCheckStage(stage);
  260. return { type: 'stage', stage, filter: month };
  261. }
  262. async _filterFinal(tender, checked) {
  263. const stages = await this._getValidStages(tender.id, 'desc', checked);
  264. const stage = stages[0];
  265. if (stage) await this.ctx.service.stage.doCheckStage(stage);
  266. return { type: 'stage', stage, filter: `第${stage.order}期` };
  267. }
  268. async _filterCheckedFinal(tender, checked) {
  269. const stages = await this._getValidStages(tender.id, 'desc', checked);
  270. const checkedStages = stages.filter(x => { return x.status === status.checked; });
  271. const stage = checkedStages[0];
  272. if (stage) await this.ctx.service.stage.doCheckStage(stage);
  273. return { type: 'stage', stage, filter: `第${stage.order}期` };
  274. }
  275. async _filterOrderZone(tender, zone, checked) {
  276. let [iBegin, iEnd] = zone.split(':');
  277. iBegin = this.ctx.helper._.toInteger(iBegin) || 0;
  278. iEnd = this.ctx.helper._.toInteger(iEnd) || 0;
  279. const stages = await this._getValidStages(tender.id, 'asc', checked), validStages = [];
  280. let preStage, endStage;
  281. for (const stage of stages) {
  282. if (stage.order < iBegin) {
  283. if (!preStage || preStage.order < stage.order) preStage = stage;
  284. } else if (stage.order > iEnd) {
  285. if (!endStage || endStage.order > stage.order) endStage = stage;
  286. } else {
  287. validStages.push(stage);
  288. }
  289. }
  290. return { type: 'stages', stages: validStages, preStage, endStage, filter: `第${iBegin}期 ~ 第${iEnd}期` };
  291. }
  292. async _filterTimeZone(tender, zone, checked) {
  293. const times = zone.split(' - ');
  294. if (times.length !== 2) throw '选择的汇总周期无效';
  295. const beginTime = moment(times[0], 'YYYY-MM');
  296. const endTime = moment(times[1], 'YYYY-MM');
  297. const stages = await this._getValidStages(tender.id, 'asc', checked), validStages = [];
  298. let preStage, endStage;
  299. for (const stage of stages) {
  300. const sTime = moment(stage.s_time, 'YYYY-MM');
  301. if (sTime.isBetween(beginTime, endTime, null, '[]')) {
  302. validStages.push(stage);
  303. } else if (sTime.isBefore(beginTime)) {
  304. if (!preStage || moment(preStage.s_time, 'YYYY-MM').isBefore(sTime)) preStage = stage;
  305. } else if (sTime.isAfter(endTime)) {
  306. if (!endStage || moment(endStage.s_time, 'YYYY-MM').isAfter(sTime)) endStage = stage;
  307. }
  308. }
  309. return { type: 'stages', stages: validStages, preStage, endStage, filter: zone };
  310. }
  311. async _filterStages(tender, stageInfo) {
  312. switch (stageInfo.type) {
  313. case 'stage':
  314. return await this._filterOrder(tender, stageInfo.stage, stageInfo.checked);
  315. case 'month':
  316. return await this._filterMonth(tender, stageInfo.month, stageInfo.checked);
  317. case 'final':
  318. return await this._filterFinal(tender, stageInfo.checked);
  319. case 'checked-final':
  320. return await this._filterCheckedFinal(tender, stageInfo.checked);
  321. case 'zone':
  322. return await this._filterTimeZone(tender, stageInfo.zone, stageInfo.checked);
  323. case 'stage-zone':
  324. return await this._filterOrderZone(tender, stageInfo.stage_zone, stageInfo.checked);
  325. case 'under-month':
  326. return await this._filterUnderMonth(tender, stageInfo.month, stageInfo.checked);
  327. case 'fake-month':
  328. return await this._filterFakeMonth(tender, stageInfo.month, stageInfo.checked);
  329. default:
  330. return;
  331. }
  332. }
  333. async _loadStageData(tender, stage) {
  334. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  335. columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'full_path', 'is_leaf', 'order', 'code', 'b_code', 'name', 'unit', 'unit_price', 'quantity', 'total_price'],
  336. where: { tender_id: tender.id },
  337. });
  338. const pos = await this.ctx.service.pos.getAllDataByCondition({
  339. columns: ['id', 'lid', 'name', 'quantity', 'porder'],
  340. where: { tid: tender.id },
  341. });
  342. if (!stage) return [bills, pos];
  343. const curStage = stage.readOnly
  344. ? await this.ctx.service.stageBills.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  345. : await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);
  346. const preStage = stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.preCheckedStage.order) : [];
  347. const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stage.id } });
  348. this.ctx.helper.assignRelaData(bills, [
  349. { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
  350. { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
  351. { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', relaId: 'lid' },
  352. ]);
  353. const curStagePos = stage.readOnly
  354. ? await this.ctx.service.stagePos.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  355. : await this.ctx.service.stagePos.getLastestStageData2(tender.id, stage.id);
  356. const preStagePos = stage.preCheckedStage ? await this.ctx.service.stagePosFinal.getFinalData(tender, stage.preCheckedStage.order) : [];
  357. this.ctx.helper.assignRelaData(pos, [
  358. { data: curStagePos, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
  359. { data: preStagePos, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid' },
  360. ]);
  361. return [bills, pos];
  362. }
  363. async _loadFakeStageData(tender, stage) {
  364. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  365. columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'full_path', 'is_leaf', 'order', 'code', 'b_code', 'name', 'unit', 'unit_price', 'quantity', 'total_price'],
  366. where: { tender_id: tender.id },
  367. });
  368. const pos = await this.ctx.service.pos.getAllDataByCondition({
  369. columns: ['id', 'lid', 'name', 'quantity', 'porder'],
  370. where: { tid: tender.id },
  371. });
  372. if (!stage) return [bills, pos];
  373. const preStage = stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.order) : [];
  374. this.ctx.helper.assignRelaData(bills, [
  375. { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
  376. ]);
  377. const preStagePos = stage.preCheckedStage ? await this.ctx.service.stagePosFinal.getFinalData(tender, stage.preCheckedStage.order) : [];
  378. this.ctx.helper.assignRelaData(pos, [
  379. { data: preStagePos, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid' },
  380. ]);
  381. return [bills, pos];
  382. }
  383. async _loadStagesData(tender, stages, preStage, endStage) {
  384. const indexPre = 'id_', helper = this.ctx.helper;
  385. const sumAssignRelaData = function (index, rela) {
  386. const loadFields = function (datas, fields, prefix, relaId) {
  387. for (const d of datas) {
  388. const key = indexPre + d[relaId];
  389. const m = index[key];
  390. if (m) {
  391. for (const f of fields) {
  392. if (d[f] !== undefined) {
  393. m[prefix + f] = helper.add(m[prefix + f], d[f]);
  394. }
  395. }
  396. }
  397. }
  398. };
  399. for (const r of rela) {
  400. loadFields(r.data, r.fields, r.prefix, r.relaId);
  401. }
  402. };
  403. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  404. columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'full_path', 'is_leaf', 'order', 'code', 'b_code', 'name', 'unit', 'unit_price', 'quantity', 'total_price'],
  405. where: { tender_id: tender.id },
  406. });
  407. const pos = await this.ctx.service.pos.getAllDataByCondition({
  408. columns: ['id', 'lid', 'name', 'quantity', 'porder'],
  409. where: { tid: tender.id },
  410. });
  411. let billsIndexData = {};
  412. for (const bd of bills) {
  413. billsIndexData[indexPre + bd.id] = bd;
  414. }
  415. let posIndexData = {};
  416. for (const p of pos) {
  417. posIndexData[indexPre + p.id] = p;
  418. }
  419. if (preStage) {
  420. const endStage = await this.ctx.service.stageBillsFinal.getFinalData(tender, preStage.order);
  421. sumAssignRelaData(billsIndexData, [
  422. { data: endStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid'},
  423. ]);
  424. const endStagePos = await this.ctx.service.stagePosFinal.getFinalData(tender, preStage.order);
  425. sumAssignRelaData(posIndexData, [
  426. {data: endStagePos, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'lid'},
  427. ]);
  428. }
  429. for (const stage of stages) {
  430. await this.ctx.service.stage.doCheckStage(stage);
  431. const curStage = stage.readOnly
  432. ? await this.ctx.service.stageBills.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  433. : await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);
  434. const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stage.id } });
  435. sumAssignRelaData(billsIndexData, [
  436. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'},
  437. { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', relaId: 'lid' },
  438. ]);
  439. const curStagePos = stage.readOnly
  440. ? await this.ctx.service.stagePos.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  441. : await this.ctx.service.stagePos.getLastestStageData2(tender.id, stage.id);
  442. sumAssignRelaData(posIndexData, [
  443. {data: curStagePos, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'},
  444. ]);
  445. }
  446. return [bills, pos];
  447. }
  448. async _loadStageLedgerData(tender, filter) {
  449. return filter.type === 'stage'
  450. ? (filter.stage.__fake ? await this._loadFakeStageData(tender, filter.stage) : await this._loadStageData(tender, filter.stage))
  451. : await this._loadStagesData(tender, filter.stages, filter.preStage, filter.endStage);
  452. }
  453. async _loadStagePayData(tender, stage) {
  454. if (!stage) return [];
  455. const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
  456. if (!stage.readOnly) {
  457. // 计算 本期金额
  458. const PayCalculator = require('../lib/pay_calc');
  459. const payCalculator = new PayCalculator(this.ctx, stage, tender.info);
  460. await payCalculator.calculateAll(dealPay);
  461. }
  462. return dealPay;
  463. }
  464. async _loadFakeStagePayData(tender, stage) {
  465. if (!stage) return [];
  466. const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
  467. if (!stage.readOnly) {
  468. // 计算 本期金额
  469. const PayCalculator = require('../lib/pay_calc');
  470. const payCalculator = new PayCalculator(this.ctx, stage, tender.info);
  471. await payCalculator.calculateAll(dealPay);
  472. }
  473. for (const dp of dealPay) {
  474. dp.pre_tp = dp.end_tp;
  475. dp.tp = 0;
  476. }
  477. return dealPay;
  478. }
  479. async _loadStagesPayData(tender, stages, preStage, endStage) {
  480. // todo
  481. return [];
  482. }
  483. async _loadPayData(tender, filter) {
  484. return filter.type === 'stage'
  485. ? (filter.stage.__fake ? await this._loadFakeStagePayData(tender, filter.stage) : await this._loadStagePayData(tender, filter.stage))
  486. : await this._loadStagesPayData(tender, filter.stage, filter.preStage, filter.endStage);
  487. }
  488. async _loadStageJgclData(tender, stage) {
  489. if (!stage) return [];
  490. const data = await this.ctx.service.stageJgcl.getStageData(stage);
  491. const preData = stage.preCheckedStage ? await this.ctx.service.stageJgcl.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  492. for (const d of data) {
  493. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  494. if (pd) {
  495. d.pre_arrive_qty = pd.arrive_qty;
  496. d.pre_arrive_tp = pd.arrive_tp;
  497. d.pre_deduct_qty = pd.deduct_qty;
  498. d.pre_deduct_tp = pd.deduct_tp;
  499. }
  500. d.end_arrive_qty = this.ctx.helper.add(d.pre_arrive_qty, d.arrive_qty);
  501. d.end_arrive_tp = this.ctx.helper.add(d.pre_arrive_tp, d.arrive_tp);
  502. d.end_deduct_qty = this.ctx.helper.add(d.pre_deduct_qty, d.deduct_qty);
  503. d.end_deduct_tp = this.ctx.helper.add(d.pre_deduct_tp, d.deduct_tp);
  504. }
  505. return data;
  506. }
  507. async _loadFakeStageJgclData(tender, stage) {
  508. if (!stage) return [];
  509. const data = await this.ctx.service.stageJgcl.getStageData(stage);
  510. for (const d of data) {
  511. d.pre_arrive_qty = d.arrive_qty;
  512. d.pre_arrive_tp = d.arrive_tp;
  513. d.pre_deduct_qty = d.deduct_qty;
  514. d.pre_deduct_tp = d.deduct_tp;
  515. d.end_arrive_qty = d.arrive_qty;
  516. d.end_arrive_tp = d.arrive_tp;
  517. d.end_deduct_qty = d.deduct_qty;
  518. d.end_deduct_tp = d.deduct_tp;
  519. d.arrive_qty = 0;
  520. d.arrive_tp = 0;
  521. d.deduct_qty = 0;
  522. d.deduct_tp = 0;
  523. }
  524. return data;
  525. }
  526. async _loadStagesJgclData(tender, stages, preStage, endStage) {
  527. // todo
  528. return [];
  529. }
  530. async _loadJgclData(tender, filter) {
  531. return filter.type === 'stage'
  532. ? (filter.stage.__fake ? await this._loadFakeStageJgclData(tender, filter.stage) : await this._loadStageJgclData(tender, filter.stage))
  533. : await this._loadStagesJgclData(tender, filter.stages, filter.preStage, filter.endStage);
  534. };
  535. async _loadStageYgclData(tender, stage) {
  536. if (!stage) return [];
  537. const data = await this.ctx.service.stageYjcl.getStageData(stage);
  538. for (const d of data) {
  539. d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  540. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  541. }
  542. return data;
  543. }
  544. async _loadFakeStageYgclData(tender, stage) {
  545. if (!stage) return [];
  546. const data = await this.ctx.service.stageYjcl.getStageData(stage);
  547. for (const d of data) {
  548. d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  549. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  550. d.pre_qty = d.end_qty;
  551. d.pre_tp = d.pre_tp;
  552. d.qty = 0;
  553. d.tp = 0;
  554. }
  555. return data;
  556. }
  557. async _loadStagesYgclData(tender, stages, preStage, endStage) {
  558. // todo
  559. return [];
  560. }
  561. async _loadYjclData(tender, filter) {
  562. return filter.type === 'stage'
  563. ? (filter.stage.__fake ? await this._loadFakeStageYgclData(tender, filter.stage) : await this._loadStageYgclData(tender, filter.stage))
  564. : await this._loadStagesYgclData(tender, filter.stages, filter.preStage, filter.endStage);
  565. };
  566. async _loadStageBonusData(tender, stage) {
  567. if (!stage) return [];
  568. const data = await this.ctx.service.stageBonus.getEndStageData(tender.id, stage.order);
  569. return data;
  570. }
  571. async _loadStagesBonusData(tender, stages, preStage, endStage) {
  572. // todo
  573. return [];
  574. }
  575. async _loadBonusData(tender, filter) {
  576. return filter.type === 'stage'
  577. ? await this._loadStageBonusData(tender, filter.stage)
  578. : await this._loadStagesBonusData(tender, filter.stages, filter.preStage, filter.endStage);
  579. };
  580. async _loadStageSafeProdData(tender, stage) {
  581. if (!stage) return [];
  582. const data = await this.ctx.service.stageSafeProd.getStageData(stage);
  583. const preData = stage.preCheckedStage ? await this.ctx.service.stageSafeProd.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  584. for (const d of data) {
  585. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  586. if (pd) {
  587. d.pre_qty = pd.qty;
  588. d.pre_tp = pd.tp;
  589. }
  590. d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  591. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  592. }
  593. return data;
  594. }
  595. async _loadFakeStageSafeProdData(tender, stage) {
  596. if (!stage) return [];
  597. const data = await this.ctx.service.stageSafeProd.getStageData(stage);
  598. for (const d of data) {
  599. d.pre_qty = d.qty;
  600. d.pre_tp = d.tp;
  601. d.end_qty = d.qty;
  602. d.end_tp = d.tp;
  603. d.qty = 0;
  604. d.tp = 0;
  605. }
  606. return data;
  607. }
  608. async _loadStagesSafeProdData(tender, stages, preStage, endStage) {
  609. // todo
  610. return [];
  611. }
  612. async _loadSafeProdData(tender, filter) {
  613. return filter.type === 'stage'
  614. ? (filter.stage.__fake ? await this._loadFakeStageSafeProdData(tender, filter.stage) : await this._loadStageSafeProdData(tender, filter.stage))
  615. : await this._loadStagesSafeProdData(tender, filter.stages, filter.preStage, filter.endStage);
  616. };
  617. async _loadStageTempLandData(tender, stage) {
  618. if (!stage) return [];
  619. const data = await this.ctx.service.stageTempLand.getStageData(stage);
  620. const preData = stage.preCheckedStage ? await this.ctx.service.stageTempLand.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  621. for (const d of data) {
  622. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  623. if (pd) {
  624. d.pre_qty = pd.qty;
  625. d.pre_tp = pd.tp;
  626. }
  627. d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  628. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  629. }
  630. return data;
  631. }
  632. async _loadFakeStageTempLandData(tender, stage) {
  633. if (!stage) return [];
  634. const data = await this.ctx.service.stageTempLand.getStageData(stage);
  635. for (const d of data) {
  636. d.pre_qty = d.qty;
  637. d.pre_tp = d.tp;
  638. d.end_qty = d.qty;
  639. d.end_tp = d.tp;
  640. d.qty = 0;
  641. d.tp = 0;
  642. }
  643. return data;
  644. }
  645. async _loadStagesTempLandData(tender, stages, preStage, endStage) {
  646. // todo
  647. return [];
  648. }
  649. async _loadTempLandData(tender, filter) {
  650. return filter.type === 'stage'
  651. ? (filter.stage.__fake ? await this._loadFakeStageTempLandData(tender, filter.stage) : await this._loadStageTempLandData(tender, filter.stage))
  652. : await this._loadStagesTempLandData(tender, filter.stages, filter.preStage, filter.endStage);
  653. };
  654. async _loadStageOtherData(tender, stage) {
  655. if (!stage) return [];
  656. const data = await this.ctx.service.stageOther.getStageData(stage);
  657. const preData = stage.preCheckedStage ? await this.ctx.service.stageOther.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  658. for (const d of data) {
  659. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  660. if (pd) {
  661. d.pre_tp = pd.tp;
  662. }
  663. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  664. }
  665. return data;
  666. }
  667. async _loadFakeStageOtherData(tender, stage) {
  668. if (!stage) return [];
  669. const data = await this.ctx.service.stageOther.getStageData(stage);
  670. for (const d of data) {
  671. d.pre_tp = pd.tp;
  672. d.end_tp = pd.tp;
  673. pd.tp = 0;
  674. }
  675. return data;
  676. }
  677. async _loadStagesOtherData(tender, stages, preStage, endStage) {
  678. // todo
  679. return [];
  680. }
  681. async _loadOtherData(tender, filter) {
  682. return filter.type === 'stage'
  683. ? (filter.stage.__fake ? await this._loadFakeStageOtherData(tender, filter.stage) : await this._loadStageOtherData(tender, filter.stage))
  684. : await this._loadStagesOtherData(tender, filter.stages, filter.preStage, filter.endStage);
  685. };
  686. async _loadTenderData(tid, filter, stageInfo) {
  687. this.ctx.tender = null;
  688. const tender = await this.ctx.service.tender.checkTender(tid);
  689. const result = { id: tender.id, name: tender.name, category: tender.category };
  690. let stageFilter = null;
  691. for (const f of filter) {
  692. switch (f) {
  693. case 'info':
  694. if (!stageFilter) {
  695. stageFilter = await this._filterStages(tender, stageInfo);
  696. result.stage_filter = stageFilter.filter;
  697. }
  698. result.info = await this._loadInfoData(tender, stageFilter);
  699. break;
  700. case 'ledger':
  701. if (filter.indexOf('stage') < 0) [result.bills, result.pos] = await this._loadLedgerData(tender);
  702. break;
  703. case 'stage':
  704. if (!stageFilter) {
  705. stageFilter = await this._filterStages(tender, stageInfo);
  706. result.stage_filter = stageFilter.filter;
  707. }
  708. [result.bills, result.pos] = await this._loadStageLedgerData(tender, stageFilter);
  709. break;
  710. case 'pay':
  711. if (!stageFilter) {
  712. stageFilter = await this._filterStages(tender, stageInfo);
  713. result.stage_filter = stageFilter.filter;
  714. }
  715. result.pay = await this._loadPayData(tender, stageFilter);
  716. break;
  717. case 'jgcl':
  718. if (!stageFilter) {
  719. stageFilter = await this._filterStages(tender, stageInfo);
  720. result.stage_filter = stageFilter.filter;
  721. }
  722. result.jgcl = await this._loadJgclData(tender, stageFilter);
  723. break;
  724. case 'yjcl':
  725. if (!stageFilter) {
  726. stageFilter = await this._filterStages(tender, stageInfo);
  727. result.stage_filter = stageFilter.filter;
  728. }
  729. result.yjcl = await this._loadYjclData(tender, stageFilter);
  730. break;
  731. case 'bonus':
  732. if (!stageFilter) {
  733. stageFilter = await this._filterStages(tender, stageInfo);
  734. result.stage_filter = stageFilter.filter;
  735. }
  736. result.bonus = await this._loadBonusData(tender, stageFilter);
  737. break;
  738. case 'safeProd':
  739. if (!stageFilter) {
  740. stageFilter = await this._filterStages(tender, stageInfo);
  741. result.stage_filter = stageFilter.filter;
  742. }
  743. result.safeProd = await this._loadSafeProdData(tender, stageFilter);
  744. break;
  745. case 'tempLand':
  746. if (!stageFilter) {
  747. stageFilter = await this._filterStages(tender, stageInfo);
  748. result.stage_filter = stageFilter.filter;
  749. }
  750. result.tempLand = await this._loadTempLandData(tender, stageFilter);
  751. break;
  752. case 'other':
  753. if (!stageFilter) {
  754. stageFilter = await this._filterStages(tender, stageInfo);
  755. result.stage_filter = stageFilter.filter;
  756. }
  757. result.other = await this._loadOtherData(tender, stageFilter);
  758. break;
  759. default:
  760. throw '查询的数据类型未定义';
  761. }
  762. }
  763. return result;
  764. }
  765. async load(ctx) {
  766. try {
  767. const data = JSON.parse(ctx.request.body.data);
  768. const filter = data.filter.split(';');
  769. const result = [];
  770. for (const t of data.tender) {
  771. result.push(await this._loadTenderData(t.tid, filter, t.stageInfo));
  772. }
  773. ctx.body = { err: 0, msg: '', data: result };
  774. } catch (err) {
  775. ctx.log(err);
  776. ctx.ajaxErrorBody(err, '加载数据错误');
  777. }
  778. }
  779. async _getTzData(tid, includePos = false) {
  780. const tender = await this.ctx.service.tender.getTender(tid);
  781. if (!tender || tender.project_id !== this.ctx.session.sessionProject.id) {
  782. throw '不存在该标段';
  783. }
  784. const bills = await this.ctx.service.ledger.getData(tid);
  785. const pos = tender.measure_type === measureType.tz.value || includePos
  786. ? await this.ctx.service.pos.getPosData({tid: tid})
  787. : [];
  788. return { id: tid, name: tender.name, bills: bills, pos: pos };
  789. }
  790. async _checkStage(tid, sorder) {
  791. const stage = await this.service.stage.getDataByCondition({ tid: tid, order: sorder });
  792. if (!stage) throw '期数据错误';
  793. await this.service.stage.doCheckStage(stage);
  794. return stage;
  795. }
  796. async _getStageData(tid, sorder) {
  797. const data = await this._getTzData(tid, true);
  798. const stage = await this._checkStage(tid, sorder);
  799. const bills = await this.ctx.service.stageBills.getAuditorStageData2(tid, stage.id, stage.curTimes, stage.curOrder);
  800. const pos = await this.ctx.service.stagePos.getAuditorStageData2(tid, stage.id, stage.curTimes, stage.curOrder);
  801. data.stage = {
  802. sid: stage.id, sorder: stage.order, curTimes: stage.curTimes, curOrder: stage.curOrder,
  803. bills: bills, pos: pos
  804. };
  805. return data;
  806. }
  807. /**
  808. * 检测台账 页面
  809. *
  810. * @param {Object} ctx - egg全局变量
  811. * @return {void}
  812. */
  813. async checkTz(ctx) {
  814. try {
  815. const renderData = {
  816. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.tools.checkTz)
  817. };
  818. await this.layout('spss/check_tz.ejs', renderData);
  819. } catch (err) {
  820. ctx.helper.log(err);
  821. }
  822. }
  823. /**
  824. * 获取 期计量 汇总 数据(Ajax)
  825. * @param ctx
  826. * @returns {Promise<void>}
  827. */
  828. async loadBaseData(ctx) {
  829. try {
  830. const data = JSON.parse(ctx.request.body.data);
  831. const responseData = {err: 0, msg: '', data: []};
  832. responseData.data = await this._getTzData(data.id);
  833. ctx.body = responseData;
  834. } catch (err) {
  835. ctx.helper.log(err);
  836. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  837. }
  838. }
  839. async stash(ctx) {
  840. try {
  841. const data = JSON.parse(ctx.request.body.data);
  842. const responseData = { err: 0, msg: '', data: [] };
  843. if (!data.type || !data.action) throw '参数错误';
  844. switch (data.action) {
  845. case 'load':
  846. responseData.data = await this.ctx.service.spssStash.getSpssStash(data.id);
  847. break;
  848. case 'list':
  849. responseData.data = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, data.type);
  850. break;
  851. case 'add':
  852. await this.ctx.service.spssStash.addSpssStash(data.type, data.select, data.result);
  853. responseData.data = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, data.type);
  854. break;
  855. case 'first':
  856. await this.ctx.service.spssStash.firstSpssStash(this.ctx.subProject.id, data.type, data.id);
  857. responseData.data = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, data.type);
  858. break;
  859. default: throw '未知操作';
  860. }
  861. ctx.body = responseData;
  862. } catch (err) {
  863. ctx.log(err);
  864. ctx.ajaxErrorBody(err, '暂存数据错误');
  865. }
  866. }
  867. async test(ctx) {
  868. try {
  869. const sourceType = require('../const/source_type');
  870. await this.layout('spss/dev_test.ejs', { sourceType });
  871. } catch (err) {
  872. ctx.helper.log(err);
  873. }
  874. }
  875. async _testLoadReport(data) {
  876. const reportData = await this.ctx.service.report.getReportData(data.source_type, data.params, data.source_filters);
  877. this.ctx.body = { err: 0, msg: '', data: reportData };
  878. }
  879. async testLoad(ctx) {
  880. try {
  881. const data = JSON.parse(ctx.request.body.data);
  882. if (!data.type && !data.detail) throw '参数错误';
  883. switch(data.type) {
  884. case 'report':
  885. await this._testLoadReport(data.detail);
  886. break;
  887. default: throw '未知参数';
  888. }
  889. } catch (err) {
  890. ctx.log(err);
  891. ctx.ajaxErrorBody(err, '查询数据错误');
  892. }
  893. }
  894. }
  895. return SpssController;
  896. };