spss_controller.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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 && 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 = 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 = await this.ctx.service.stagePosFinal.getFinalData(tender, stage.order);
  378. console.log(stage.in_time, preStage.length, preStagePos.length);
  379. this.ctx.helper.assignRelaData(pos, [
  380. { data: preStagePos, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid' },
  381. ]);
  382. return [bills, pos];
  383. }
  384. async _loadStagesData(tender, stages, preStage, endStage) {
  385. const indexPre = 'id_', helper = this.ctx.helper;
  386. const sumAssignRelaData = function (index, rela) {
  387. const loadFields = function (datas, fields, prefix, relaId) {
  388. for (const d of datas) {
  389. const key = indexPre + d[relaId];
  390. const m = index[key];
  391. if (m) {
  392. for (const f of fields) {
  393. if (d[f] !== undefined) {
  394. m[prefix + f] = helper.add(m[prefix + f], d[f]);
  395. }
  396. }
  397. }
  398. }
  399. };
  400. for (const r of rela) {
  401. loadFields(r.data, r.fields, r.prefix, r.relaId);
  402. }
  403. };
  404. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  405. columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'full_path', 'is_leaf', 'order', 'code', 'b_code', 'name', 'unit', 'unit_price', 'quantity', 'total_price'],
  406. where: { tender_id: tender.id },
  407. });
  408. const pos = await this.ctx.service.pos.getAllDataByCondition({
  409. columns: ['id', 'lid', 'name', 'quantity', 'porder'],
  410. where: { tid: tender.id },
  411. });
  412. let billsIndexData = {};
  413. for (const bd of bills) {
  414. billsIndexData[indexPre + bd.id] = bd;
  415. }
  416. let posIndexData = {};
  417. for (const p of pos) {
  418. posIndexData[indexPre + p.id] = p;
  419. }
  420. if (preStage) {
  421. const endStage = await this.ctx.service.stageBillsFinal.getFinalData(tender, preStage.order);
  422. sumAssignRelaData(billsIndexData, [
  423. { data: endStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid'},
  424. ]);
  425. const endStagePos = await this.ctx.service.stagePosFinal.getFinalData(tender, preStage.order);
  426. sumAssignRelaData(posIndexData, [
  427. {data: endStagePos, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'lid'},
  428. ]);
  429. }
  430. for (const stage of stages) {
  431. await this.ctx.service.stage.doCheckStage(stage);
  432. const curStage = stage.readOnly
  433. ? await this.ctx.service.stageBills.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  434. : await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);
  435. const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stage.id } });
  436. sumAssignRelaData(billsIndexData, [
  437. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'},
  438. { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', relaId: 'lid' },
  439. ]);
  440. const curStagePos = stage.readOnly
  441. ? await this.ctx.service.stagePos.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  442. : await this.ctx.service.stagePos.getLastestStageData2(tender.id, stage.id);
  443. sumAssignRelaData(posIndexData, [
  444. {data: curStagePos, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'},
  445. ]);
  446. }
  447. return [bills, pos];
  448. }
  449. async _loadStageLedgerData(tender, filter) {
  450. return filter.type === 'stage'
  451. ? (filter.stage && filter.stage.__fake ? await this._loadFakeStageData(tender, filter.stage) : await this._loadStageData(tender, filter.stage))
  452. : await this._loadStagesData(tender, filter.stages, filter.preStage, filter.endStage);
  453. }
  454. async _loadStagePayData(tender, stage) {
  455. if (!stage) return [];
  456. const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
  457. if (!stage.readOnly) {
  458. // 计算 本期金额
  459. const PayCalculator = require('../lib/pay_calc');
  460. const payCalculator = new PayCalculator(this.ctx, stage, tender.info);
  461. await payCalculator.calculateAll(dealPay);
  462. }
  463. return dealPay;
  464. }
  465. async _loadFakeStagePayData(tender, stage) {
  466. if (!stage) return [];
  467. const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
  468. if (!stage.readOnly) {
  469. // 计算 本期金额
  470. const PayCalculator = require('../lib/pay_calc');
  471. const payCalculator = new PayCalculator(this.ctx, stage, tender.info);
  472. await payCalculator.calculateAll(dealPay);
  473. }
  474. for (const dp of dealPay) {
  475. dp.pre_tp = dp.end_tp;
  476. dp.tp = 0;
  477. }
  478. return dealPay;
  479. }
  480. async _loadStagesPayData(tender, stages, preStage, endStage) {
  481. // todo
  482. return [];
  483. }
  484. async _loadPayData(tender, filter) {
  485. return filter.type === 'stage'
  486. ? (filter.stage && filter.stage.__fake ? await this._loadFakeStagePayData(tender, filter.stage) : await this._loadStagePayData(tender, filter.stage))
  487. : await this._loadStagesPayData(tender, filter.stage, filter.preStage, filter.endStage);
  488. }
  489. async _loadStageJgclData(tender, stage) {
  490. if (!stage) return [];
  491. const data = await this.ctx.service.stageJgcl.getStageData(stage);
  492. const preData = stage.preCheckedStage ? await this.ctx.service.stageJgcl.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  493. for (const d of data) {
  494. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  495. if (pd) {
  496. d.pre_arrive_qty = pd.arrive_qty;
  497. d.pre_arrive_tp = pd.arrive_tp;
  498. d.pre_deduct_qty = pd.deduct_qty;
  499. d.pre_deduct_tp = pd.deduct_tp;
  500. }
  501. d.end_arrive_qty = this.ctx.helper.add(d.pre_arrive_qty, d.arrive_qty);
  502. d.end_arrive_tp = this.ctx.helper.add(d.pre_arrive_tp, d.arrive_tp);
  503. d.end_deduct_qty = this.ctx.helper.add(d.pre_deduct_qty, d.deduct_qty);
  504. d.end_deduct_tp = this.ctx.helper.add(d.pre_deduct_tp, d.deduct_tp);
  505. }
  506. return data;
  507. }
  508. async _loadFakeStageJgclData(tender, stage) {
  509. if (!stage) return [];
  510. const data = await this.ctx.service.stageJgcl.getStageData(stage);
  511. const preData = stage.preCheckedStage ? await this.ctx.service.stageJgcl.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  512. for (const d of data) {
  513. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  514. if (pd) {
  515. d.pre_arrive_qty = this.ctx.helper.add(pd.arrive_qty, d.arrive_qty);
  516. d.pre_arrive_tp = this.ctx.helper.add(pd.arrive_tp, d.arrive_tp);
  517. d.pre_deduct_qty = this.ctx.helper.add(pd.deduct_qty, d.deduct_qty);
  518. d.pre_deduct_tp = this.ctx.helper.add(pd.deduct_tp, d.deduct_tp);
  519. }
  520. d.end_arrive_qty = d.pre_arrive_qty;
  521. d.end_arrive_tp = d.pre_arrive_tp;
  522. d.end_deduct_qty = d.pre_deduct_qty;
  523. d.end_deduct_tp = d.pre_deduct_tp;
  524. d.arrive_qty = 0;
  525. d.arrive_tp = 0;
  526. d.deduct_qty = 0;
  527. d.deduct_tp = 0;
  528. }
  529. return data;
  530. }
  531. async _loadStagesJgclData(tender, stages, preStage, endStage) {
  532. // todo
  533. return [];
  534. }
  535. async _loadJgclData(tender, filter) {
  536. return filter.type === 'stage'
  537. ? (filter.stage && filter.stage.__fake ? await this._loadFakeStageJgclData(tender, filter.stage) : await this._loadStageJgclData(tender, filter.stage))
  538. : await this._loadStagesJgclData(tender, filter.stages, filter.preStage, filter.endStage);
  539. };
  540. async _loadStageYgclData(tender, stage) {
  541. if (!stage) return [];
  542. const data = await this.ctx.service.stageYjcl.getStageData(stage);
  543. for (const d of data) {
  544. d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  545. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  546. }
  547. return data;
  548. }
  549. async _loadFakeStageYgclData(tender, stage) {
  550. if (!stage) return [];
  551. const data = await this.ctx.service.stageYjcl.getStageData(stage);
  552. for (const d of data) {
  553. d.pre_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  554. d.pre_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  555. d.end_qty = d.pre_qty;
  556. d.end_tp = d.pre_tp;
  557. d.qty = 0;
  558. d.tp = 0;
  559. }
  560. return data;
  561. }
  562. async _loadStagesYgclData(tender, stages, preStage, endStage) {
  563. // todo
  564. return [];
  565. }
  566. async _loadYjclData(tender, filter) {
  567. return filter.type === 'stage'
  568. ? (filter.stage && filter.stage.__fake ? await this._loadFakeStageYgclData(tender, filter.stage) : await this._loadStageYgclData(tender, filter.stage))
  569. : await this._loadStagesYgclData(tender, filter.stages, filter.preStage, filter.endStage);
  570. };
  571. async _loadStageBonusData(tender, stage) {
  572. if (!stage) return [];
  573. const data = await this.ctx.service.stageBonus.getEndStageData(tender.id, stage.order);
  574. return data;
  575. }
  576. async _loadStagesBonusData(tender, stages, preStage, endStage) {
  577. // todo
  578. return [];
  579. }
  580. async _loadBonusData(tender, filter) {
  581. return filter.type === 'stage'
  582. ? await this._loadStageBonusData(tender, filter.stage)
  583. : await this._loadStagesBonusData(tender, filter.stages, filter.preStage, filter.endStage);
  584. };
  585. async _loadStageSafeProdData(tender, stage) {
  586. if (!stage) return [];
  587. const data = await this.ctx.service.stageSafeProd.getStageData(stage);
  588. const preData = stage.preCheckedStage ? await this.ctx.service.stageSafeProd.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  589. for (const d of data) {
  590. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  591. if (pd) {
  592. d.pre_qty = pd.qty;
  593. d.pre_tp = pd.tp;
  594. }
  595. d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  596. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  597. }
  598. return data;
  599. }
  600. async _loadFakeStageSafeProdData(tender, stage) {
  601. if (!stage) return [];
  602. const data = await this.ctx.service.stageSafeProd.getStageData(stage);
  603. for (const d of data) {
  604. d.pre_qty = this.ctx.helper.add(d.qty, d.pre_qty);
  605. d.pre_tp = this.ctx.helper.add(d.tp, d.pre_tp);
  606. d.end_qty = d.pre_qty;
  607. d.end_tp = d.pre_tp;
  608. d.qty = 0;
  609. d.tp = 0;
  610. }
  611. return data;
  612. }
  613. async _loadStagesSafeProdData(tender, stages, preStage, endStage) {
  614. // todo
  615. return [];
  616. }
  617. async _loadSafeProdData(tender, filter) {
  618. return filter.type === 'stage'
  619. ? (filter.stage && filter.stage.__fake ? await this._loadFakeStageSafeProdData(tender, filter.stage) : await this._loadStageSafeProdData(tender, filter.stage))
  620. : await this._loadStagesSafeProdData(tender, filter.stages, filter.preStage, filter.endStage);
  621. };
  622. async _loadStageTempLandData(tender, stage) {
  623. if (!stage) return [];
  624. const data = await this.ctx.service.stageTempLand.getStageData(stage);
  625. const preData = stage.preCheckedStage ? await this.ctx.service.stageTempLand.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  626. for (const d of data) {
  627. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  628. if (pd) {
  629. d.pre_qty = pd.qty;
  630. d.pre_tp = pd.tp;
  631. }
  632. d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  633. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  634. }
  635. return data;
  636. }
  637. async _loadFakeStageTempLandData(tender, stage) {
  638. if (!stage) return [];
  639. const data = await this.ctx.service.stageTempLand.getStageData(stage);
  640. for (const d of data) {
  641. d.pre_qty = this.ctx.helper.add(d.qty, d.pre_qty);
  642. d.pre_tp = this.ctx.helper.add(d.tp, d.pre_tp);
  643. d.end_qty = d.pre_qty;
  644. d.end_tp = d.pre_tp;
  645. d.qty = 0;
  646. d.tp = 0;
  647. }
  648. return data;
  649. }
  650. async _loadStagesTempLandData(tender, stages, preStage, endStage) {
  651. // todo
  652. return [];
  653. }
  654. async _loadTempLandData(tender, filter) {
  655. return filter.type === 'stage'
  656. ? (filter.stage && filter.stage.__fake ? await this._loadFakeStageTempLandData(tender, filter.stage) : await this._loadStageTempLandData(tender, filter.stage))
  657. : await this._loadStagesTempLandData(tender, filter.stages, filter.preStage, filter.endStage);
  658. };
  659. async _loadStageOtherData(tender, stage) {
  660. if (!stage) return [];
  661. const data = await this.ctx.service.stageOther.getStageData(stage);
  662. const preData = stage.preCheckedStage ? await this.ctx.service.stageOther.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  663. for (const d of data) {
  664. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  665. if (pd) {
  666. d.pre_tp = pd.tp;
  667. }
  668. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  669. }
  670. return data;
  671. }
  672. async _loadFakeStageOtherData(tender, stage) {
  673. if (!stage) return [];
  674. const data = await this.ctx.service.stageOther.getStageData(stage);
  675. for (const d of data) {
  676. d.pre_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  677. d.end_tp = d.pre_tp;
  678. d.tp = 0;
  679. }
  680. return data;
  681. }
  682. async _loadStagesOtherData(tender, stages, preStage, endStage) {
  683. // todo
  684. return [];
  685. }
  686. async _loadOtherData(tender, filter) {
  687. return filter.type === 'stage'
  688. ? (filter.stage && filter.stage.__fake ? await this._loadFakeStageOtherData(tender, filter.stage) : await this._loadStageOtherData(tender, filter.stage))
  689. : await this._loadStagesOtherData(tender, filter.stages, filter.preStage, filter.endStage);
  690. };
  691. async _loadTenderData(tid, filter, stageInfo) {
  692. this.ctx.tender = null;
  693. const tender = await this.ctx.service.tender.checkTender(tid);
  694. const result = { id: tender.id, name: tender.name, category: tender.category };
  695. let stageFilter = null;
  696. for (const f of filter) {
  697. switch (f) {
  698. case 'info':
  699. if (!stageFilter) {
  700. stageFilter = await this._filterStages(tender, stageInfo);
  701. result.stage_filter = stageFilter.filter;
  702. }
  703. result.info = await this._loadInfoData(tender, stageFilter);
  704. break;
  705. case 'ledger':
  706. if (filter.indexOf('stage') < 0) [result.bills, result.pos] = await this._loadLedgerData(tender);
  707. break;
  708. case 'stage':
  709. if (!stageFilter) {
  710. stageFilter = await this._filterStages(tender, stageInfo);
  711. result.stage_filter = stageFilter.filter;
  712. }
  713. [result.bills, result.pos] = await this._loadStageLedgerData(tender, stageFilter);
  714. break;
  715. case 'pay':
  716. if (!stageFilter) {
  717. stageFilter = await this._filterStages(tender, stageInfo);
  718. result.stage_filter = stageFilter.filter;
  719. }
  720. result.pay = await this._loadPayData(tender, stageFilter);
  721. break;
  722. case 'jgcl':
  723. if (!stageFilter) {
  724. stageFilter = await this._filterStages(tender, stageInfo);
  725. result.stage_filter = stageFilter.filter;
  726. }
  727. result.jgcl = await this._loadJgclData(tender, stageFilter);
  728. break;
  729. case 'yjcl':
  730. if (!stageFilter) {
  731. stageFilter = await this._filterStages(tender, stageInfo);
  732. result.stage_filter = stageFilter.filter;
  733. }
  734. result.yjcl = await this._loadYjclData(tender, stageFilter);
  735. break;
  736. case 'bonus':
  737. if (!stageFilter) {
  738. stageFilter = await this._filterStages(tender, stageInfo);
  739. result.stage_filter = stageFilter.filter;
  740. }
  741. result.bonus = await this._loadBonusData(tender, stageFilter);
  742. break;
  743. case 'safeProd':
  744. if (!stageFilter) {
  745. stageFilter = await this._filterStages(tender, stageInfo);
  746. result.stage_filter = stageFilter.filter;
  747. }
  748. result.safeProd = await this._loadSafeProdData(tender, stageFilter);
  749. break;
  750. case 'tempLand':
  751. if (!stageFilter) {
  752. stageFilter = await this._filterStages(tender, stageInfo);
  753. result.stage_filter = stageFilter.filter;
  754. }
  755. result.tempLand = await this._loadTempLandData(tender, stageFilter);
  756. break;
  757. case 'other':
  758. if (!stageFilter) {
  759. stageFilter = await this._filterStages(tender, stageInfo);
  760. result.stage_filter = stageFilter.filter;
  761. }
  762. result.other = await this._loadOtherData(tender, stageFilter);
  763. break;
  764. default:
  765. throw '查询的数据类型未定义';
  766. }
  767. }
  768. return result;
  769. }
  770. async load(ctx) {
  771. try {
  772. const data = JSON.parse(ctx.request.body.data);
  773. const filter = data.filter.split(';');
  774. const result = [];
  775. for (const t of data.tender) {
  776. result.push(await this._loadTenderData(t.tid, filter, t.stageInfo));
  777. }
  778. ctx.body = { err: 0, msg: '', data: result };
  779. } catch (err) {
  780. ctx.log(err);
  781. ctx.ajaxErrorBody(err, '加载数据错误');
  782. }
  783. }
  784. async _getTzData(tid, includePos = false) {
  785. const tender = await this.ctx.service.tender.getTender(tid);
  786. if (!tender || tender.project_id !== this.ctx.session.sessionProject.id) {
  787. throw '不存在该标段';
  788. }
  789. const bills = await this.ctx.service.ledger.getData(tid);
  790. const pos = tender.measure_type === measureType.tz.value || includePos
  791. ? await this.ctx.service.pos.getPosData({tid: tid})
  792. : [];
  793. return { id: tid, name: tender.name, bills: bills, pos: pos };
  794. }
  795. async _checkStage(tid, sorder) {
  796. const stage = await this.service.stage.getDataByCondition({ tid: tid, order: sorder });
  797. if (!stage) throw '期数据错误';
  798. await this.service.stage.doCheckStage(stage);
  799. return stage;
  800. }
  801. async _getStageData(tid, sorder) {
  802. const data = await this._getTzData(tid, true);
  803. const stage = await this._checkStage(tid, sorder);
  804. const bills = await this.ctx.service.stageBills.getAuditorStageData2(tid, stage.id, stage.curTimes, stage.curOrder);
  805. const pos = await this.ctx.service.stagePos.getAuditorStageData2(tid, stage.id, stage.curTimes, stage.curOrder);
  806. data.stage = {
  807. sid: stage.id, sorder: stage.order, curTimes: stage.curTimes, curOrder: stage.curOrder,
  808. bills: bills, pos: pos
  809. };
  810. return data;
  811. }
  812. /**
  813. * 检测台账 页面
  814. *
  815. * @param {Object} ctx - egg全局变量
  816. * @return {void}
  817. */
  818. async checkTz(ctx) {
  819. try {
  820. const renderData = {
  821. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.tools.checkTz)
  822. };
  823. await this.layout('spss/check_tz.ejs', renderData);
  824. } catch (err) {
  825. ctx.helper.log(err);
  826. }
  827. }
  828. /**
  829. * 获取 期计量 汇总 数据(Ajax)
  830. * @param ctx
  831. * @returns {Promise<void>}
  832. */
  833. async loadBaseData(ctx) {
  834. try {
  835. const data = JSON.parse(ctx.request.body.data);
  836. const responseData = {err: 0, msg: '', data: []};
  837. responseData.data = await this._getTzData(data.id);
  838. ctx.body = responseData;
  839. } catch (err) {
  840. ctx.helper.log(err);
  841. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  842. }
  843. }
  844. async stash(ctx) {
  845. try {
  846. const data = JSON.parse(ctx.request.body.data);
  847. const responseData = { err: 0, msg: '', data: [] };
  848. if (!data.type || !data.action) throw '参数错误';
  849. switch (data.action) {
  850. case 'load':
  851. responseData.data = await this.ctx.service.spssStash.getSpssStash(data.id);
  852. break;
  853. case 'list':
  854. responseData.data = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, data.type);
  855. break;
  856. case 'add':
  857. await this.ctx.service.spssStash.addSpssStash(data.type, data.select, data.result);
  858. responseData.data = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, data.type);
  859. break;
  860. case 'first':
  861. await this.ctx.service.spssStash.firstSpssStash(this.ctx.subProject.id, data.type, data.id);
  862. responseData.data = await this.ctx.service.spssStash.getSpssStashList(this.ctx.subProject.id, data.type);
  863. break;
  864. default: throw '未知操作';
  865. }
  866. ctx.body = responseData;
  867. } catch (err) {
  868. ctx.log(err);
  869. ctx.ajaxErrorBody(err, '暂存数据错误');
  870. }
  871. }
  872. async test(ctx) {
  873. try {
  874. const sourceType = require('../const/source_type');
  875. await this.layout('spss/dev_test.ejs', { sourceType });
  876. } catch (err) {
  877. ctx.helper.log(err);
  878. }
  879. }
  880. async _testLoadReport(data) {
  881. const reportData = await this.ctx.service.report.getReportData(data.source_type, data.params, data.source_filters);
  882. this.ctx.body = { err: 0, msg: '', data: reportData };
  883. }
  884. async testLoad(ctx) {
  885. try {
  886. const data = JSON.parse(ctx.request.body.data);
  887. if (!data.type && !data.detail) throw '参数错误';
  888. switch(data.type) {
  889. case 'report':
  890. await this._testLoadReport(data.detail);
  891. break;
  892. default: throw '未知参数';
  893. }
  894. } catch (err) {
  895. ctx.log(err);
  896. ctx.ajaxErrorBody(err, '查询数据错误');
  897. }
  898. }
  899. }
  900. return SpssController;
  901. };