rpt_gather_memory.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('../lib/ledger');
  10. const PayCalculator = require('../lib/pay_calc');
  11. const auditConst = require('../const/audit');
  12. const payConst = require('../const/deal_pay');
  13. const moment = require('moment');
  14. const indexPre = 'id_';
  15. const gatherUtils = {
  16. gatherStage: function (tender, gatherNode, sourceNode, prefix, helper) {
  17. gatherNode[prefix + 'id'] = tender.id;
  18. gatherNode[prefix + 'name'] = tender.name;
  19. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  20. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  21. gatherNode[prefix + "contract_qty"] = helper.add(gatherNode[prefix + "contract_qty"], sourceNode.contract_qty);
  22. gatherNode[prefix + "contract_tp"] = helper.add(gatherNode[prefix + "contract_tp"], sourceNode.contract_tp);
  23. gatherNode[prefix + "qc_qty"] = helper.add(gatherNode[prefix + "qc_qty"], sourceNode.qc_qty);
  24. gatherNode[prefix + "qc_tp"] = helper.add(gatherNode[prefix + "qc_tp"], sourceNode.qc_tp);
  25. gatherNode[prefix + "gather_qty"] = helper.add(gatherNode[prefix + "gather_qty"], sourceNode.gather_qty);
  26. gatherNode[prefix + "gather_tp"] = helper.add(gatherNode[prefix + "gather_tp"], sourceNode.gather_tp);
  27. gatherNode[prefix + "pre_contract_qty"] = helper.add(gatherNode[prefix + "pre_contract_qty"], sourceNode.pre_contract_qty);
  28. gatherNode[prefix + "pre_contract_tp"] = helper.add(gatherNode[prefix + "pre_contract_tp"], sourceNode.pre_contract_tp);
  29. gatherNode[prefix + "pre_qc_qty"] = helper.add(gatherNode[prefix + "pre_qc_qty"], sourceNode.pre_qc_qty);
  30. gatherNode[prefix + "pre_qc_tp"] = helper.add(gatherNode[prefix + "pre_qc_tp"], sourceNode.pre_qc_tp);
  31. gatherNode[prefix + "pre_gather_qty"] = helper.add(gatherNode[prefix + "pre_gather_qty"], sourceNode.pre_gather_qty);
  32. gatherNode[prefix + "pre_gather_tp"] = helper.add(gatherNode[prefix + "pre_gather_tp"], sourceNode.pre_gather_tp);
  33. gatherNode[prefix + "end_contract_qty"] = helper.add(gatherNode[prefix + "end_contract_qty"], sourceNode.end_contract_qty);
  34. gatherNode[prefix + "end_contract_tp"] = helper.add(gatherNode[prefix + "end_contract_tp"], sourceNode.end_contract_tp);
  35. gatherNode[prefix + "end_qc_qty"] = helper.add(gatherNode[prefix + "end_qc_qty"], sourceNode.end_qc_qty);
  36. gatherNode[prefix + "end_qc_tp"] = helper.add(gatherNode[prefix + "end_qc_tp"], sourceNode.end_qc_tp);
  37. gatherNode[prefix + "end_gather_qty"] = helper.add(gatherNode[prefix + "end_gather_qty"], sourceNode.end_gather_qty);
  38. gatherNode[prefix + "end_gather_tp"] = helper.add(gatherNode[prefix + "end_gather_tp"], sourceNode.end_gather_tp);
  39. gatherNode['s_' + "qty"] = helper.add(gatherNode['s_' + "qty"], sourceNode.quantity);
  40. gatherNode['s_' + "tp"] = helper.add(gatherNode['s_' + "tp"], sourceNode.total_price);
  41. gatherNode['s_' + "contract_qty"] = helper.add(gatherNode['s_' + "contract_qty"], sourceNode.contract_qty);
  42. gatherNode['s_' + "contract_tp"] = helper.add(gatherNode['s_' + "contract_tp"], sourceNode.contract_tp);
  43. gatherNode['s_' + "qc_qty"] = helper.add(gatherNode['s_' + "qc_qty"], sourceNode.qc_qty);
  44. gatherNode['s_' + "qc_tp"] = helper.add(gatherNode['s_' + "qc_tp"], sourceNode.qc_tp);
  45. gatherNode['s_' + "gather_qty"] = helper.add(gatherNode['s_' + "gather_qty"], sourceNode.gather_qty);
  46. gatherNode['s_' + "gather_tp"] = helper.add(gatherNode['s_' + "gather_tp"], sourceNode.gather_tp);
  47. gatherNode['s_' + "pre_contract_qty"] = helper.add(gatherNode['s_' + "pre_contract_qty"], sourceNode.pre_contract_qty);
  48. gatherNode['s_' + "pre_contract_tp"] = helper.add(gatherNode['s_' + "pre_contract_tp"], sourceNode.pre_contract_tp);
  49. gatherNode['s_' + "pre_qc_qty"] = helper.add(gatherNode['s_' + "pre_qc_qty"], sourceNode.pre_qc_qty);
  50. gatherNode['s_' + "pre_qc_tp"] = helper.add(gatherNode['s_' + "pre_qc_tp"], sourceNode.pre_qc_tp);
  51. gatherNode['s_' + "pre_gather_qty"] = helper.add(gatherNode['s_' + "pre_gather_qty"], sourceNode.pre_gather_qty);
  52. gatherNode['s_' + "pre_gather_tp"] = helper.add(gatherNode['s_' + "pre_gather_tp"], sourceNode.pre_gather_tp);
  53. gatherNode['s_' + "end_contract_qty"] = helper.add(gatherNode['s_' + "end_contract_qty"], sourceNode.end_contract_qty);
  54. gatherNode['s_' + "end_contract_tp"] = helper.add(gatherNode['s_' + "end_contract_tp"], sourceNode.end_contract_tp);
  55. gatherNode['s_' + "end_qc_qty"] = helper.add(gatherNode['s_' + "end_qc_qty"], sourceNode.end_qc_qty);
  56. gatherNode['s_' + "end_qc_tp"] = helper.add(gatherNode['s_' + "end_qc_tp"], sourceNode.end_qc_tp);
  57. gatherNode['s_' + "end_gather_qty"] = helper.add(gatherNode['s_' + "end_gather_qty"], sourceNode.end_gather_qty);
  58. gatherNode['s_' + "end_gather_tp"] = helper.add(gatherNode['s_' + "end_gather_tp"], sourceNode.end_gather_tp);
  59. },
  60. gatherZone: function (tender, gatherNode, sourceNode, prefix, helper) {
  61. gatherNode[prefix + 'id'] = tender.id;
  62. gatherNode[prefix + 'name'] = tender.name;
  63. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  64. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  65. gatherNode[prefix + "contract_qty"] = helper.add(gatherNode[prefix + "contract_qty"], sourceNode.contract_qty);
  66. gatherNode[prefix + "contract_tp"] = helper.add(gatherNode[prefix + "contract_tp"], sourceNode.contract_tp);
  67. gatherNode[prefix + "qc_qty"] = helper.add(gatherNode[prefix + "qc_qty"], sourceNode.qc_qty);
  68. gatherNode[prefix + "qc_tp"] = helper.add(gatherNode[prefix + "qc_tp"], sourceNode.qc_tp);
  69. gatherNode[prefix + "gather_qty"] = helper.add(gatherNode[prefix + "gather_qty"], sourceNode.gather_qty);
  70. gatherNode[prefix + "gather_tp"] = helper.add(gatherNode[prefix + "gather_tp"], sourceNode.gather_tp);
  71. gatherNode['s_' + "qty"] = helper.add(gatherNode['s_' + "qty"], sourceNode.quantity);
  72. gatherNode['s_' + "tp"] = helper.add(gatherNode['s_' + "tp"], sourceNode.total_price);
  73. gatherNode['s_' + "contract_qty"] = helper.add(gatherNode['s_' + "contract_qty"], sourceNode.contract_qty);
  74. gatherNode['s_' + "contract_tp"] = helper.add(gatherNode['s_' + "contract_tp"], sourceNode.contract_tp);
  75. gatherNode['s_' + "qc_qty"] = helper.add(gatherNode['s_' + "qc_qty"], sourceNode.qc_qty);
  76. gatherNode['s_' + "qc_tp"] = helper.add(gatherNode['s_' + "qc_tp"], sourceNode.qc_tp);
  77. gatherNode['s_' + "gather_qty"] = helper.add(gatherNode['s_' + "gather_qty"], sourceNode.gather_qty);
  78. gatherNode['s_' + "gather_tp"] = helper.add(gatherNode['s_' + "gather_tp"], sourceNode.gather_tp);
  79. },
  80. gatherLedger: function (tender, gatherNode, sourceNode, prefix, helper) {
  81. gatherNode[prefix + 'id'] = tender.id;
  82. gatherNode[prefix + 'name'] = tender.name;
  83. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  84. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  85. gatherNode['s_' + "qty"] = helper.add(gatherNode['s_' + "qty"], sourceNode.quantity);
  86. gatherNode['s_' + "tp"] = helper.add(gatherNode['s_' + "tp"], sourceNode.total_price);
  87. },
  88. gatherSpecial: function (gatherNode, sourceNode, prefix, helper) {
  89. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  90. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  91. },
  92. };
  93. module.exports = app => {
  94. class RptGatherMemory extends app.BaseService {
  95. /**
  96. * 构造函数
  97. *
  98. * @param {Object} ctx - egg全局context
  99. * @return {void}
  100. */
  101. constructor(ctx) {
  102. super(ctx);
  103. this.resultTree = new Ledger.gatherTree(ctx, {
  104. id: 'id',
  105. pid: 'pid',
  106. order: 'order',
  107. level: 'level',
  108. rootId: -1
  109. });
  110. this.resultTenderInfo = [];
  111. this.resultDealpPay = [];
  112. }
  113. _checkSpecialTender(tender, special) {
  114. if (special) {
  115. for (const spec of special) {
  116. if (tender[spec.key] === true) return spec.key;
  117. }
  118. }
  119. return '';
  120. }
  121. /**
  122. * 台账数据
  123. */
  124. async _gatherStageData(index, tender, stage, hasPre) {
  125. const helper = this.ctx.helper;
  126. const billsTree = new Ledger.billsTree(this.ctx, {
  127. id: 'ledger_id',
  128. pid: 'ledger_pid',
  129. order: 'order',
  130. level: 'level',
  131. rootId: -1,
  132. keys: ['id', 'tender_id', 'ledger_id'],
  133. stageId: 'id',
  134. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'],
  135. calc: function (node) {
  136. if (node.children && node.children.length === 0) {
  137. node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty);
  138. node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
  139. node.end_contract_qty = helper.add(node.pre_contract_qty, node.contract_qty);
  140. node.end_qc_qty = helper.add(node.pre_qc_qty, node.qc_qty);
  141. node.end_gather_qty = helper.add(node.pre_gather_qty, node.gather_qty);
  142. }
  143. node.pre_gather_tp = helper.add(node.pre_contract_tp, node.pre_qc_tp);
  144. node.gather_tp = helper.add(node.contract_tp, node.qc_tp);
  145. node.end_contract_tp = helper.add(node.pre_contract_tp, node.contract_tp);
  146. node.end_qc_tp = helper.add(node.pre_qc_tp, node.qc_tp);
  147. node.end_gather_tp = helper.add(node.pre_gather_tp, node.gather_tp);
  148. }
  149. });
  150. const billsData = await this.ctx.service.ledger.getData(tender.id);
  151. if (stage) {
  152. await this.ctx.service.stage.doCheckStage(stage);
  153. if (stage.readOnly) {
  154. const curStage = await this.ctx.service.stageBills.getAuditorStageData(tender.id,
  155. stage.id, stage.curTimes, stage.curOrder);
  156. this.ctx.helper.assignRelaData(billsData, [
  157. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  158. ]);
  159. } else {
  160. const curStage = await this.ctx.service.stageBills.getLastestStageData(tender.id, stage.id);
  161. this.ctx.helper.assignRelaData(billsData, [
  162. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  163. ]);
  164. }
  165. if (hasPre) {
  166. const preStage = stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.order - 1) : [];
  167. this.ctx.helper.assignRelaData(billsData, [
  168. {data: preStage, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'lid'}
  169. ]);
  170. }
  171. }
  172. billsTree.loadDatas(billsData);
  173. billsTree.calculateAll();
  174. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  175. gatherUtils.gatherStage(tender, gatherNode, sourceNode, 't_' + index + '_', helper);
  176. });
  177. }
  178. async _gatherMonthData(sTender, index, month, hasPre) {
  179. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  180. const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
  181. await this._gatherStageData(index, tender, stage, hasPre);
  182. }
  183. async _gatherZoneData(sTender, index, zone) {
  184. const helper = this.ctx.helper;
  185. /**
  186. * 汇总并合并 相关数据
  187. * @param {Array} index - 主数据
  188. * @param {Array[]}rela - 相关数据 {data, fields, prefix, relaId}
  189. */
  190. const sumAssignRelaData = function (index, rela) {
  191. const loadFields = function (datas, fields, prefix, relaId) {
  192. for (const d of datas) {
  193. const key = indexPre + d[relaId];
  194. const m = index[key];
  195. if (m) {
  196. for (const f of fields) {
  197. if (d[f] !== undefined) {
  198. m[prefix + f] = helper.add(m[prefix + f], d[f]);
  199. }
  200. }
  201. }
  202. }
  203. };
  204. for (const r of rela) {
  205. loadFields(r.data, r.fields, r.prefix, r.relaId);
  206. }
  207. };
  208. const billsTree = new Ledger.billsTree(this.ctx, {
  209. id: 'ledger_id',
  210. pid: 'ledger_pid',
  211. order: 'order',
  212. level: 'level',
  213. rootId: -1,
  214. keys: ['id', 'tender_id', 'ledger_id'],
  215. stageId: 'id',
  216. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp'],
  217. calc: function (node) {
  218. if (node.children && node.children.length === 0) {
  219. node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
  220. }
  221. node.gather_tp = helper.add(node.contract_tp, node.qc_tp);
  222. }
  223. });
  224. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  225. const billsData = await this.ctx.service.ledger.getData(tender.id);
  226. let billsIndexData = {};
  227. for (const bd of billsData) {
  228. billsIndexData[indexPre + bd.id] = bd;
  229. }
  230. const times = zone.split(' - ');
  231. if (times.length !== 2) throw '选择的汇总周期无效';
  232. const beginTime = moment(times[0], 'YYYY-MM').date();
  233. const endTime = moment(times[1], 'YYYY-MM').date();
  234. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tender.id } });
  235. for (const stage of stages) {
  236. const sTime = moment(stage.s_time, 'YYYY-MM').date();
  237. if (sTime >= beginTime && sTime <= endTime) {
  238. await this.ctx.service.stage.doCheckStage(stage);
  239. if (stage.readOnly) {
  240. const curStage = await this.ctx.service.stageBills.getAuditorStageData(tender.id,
  241. stage.id, stage.curTimes, stage.curOrder);
  242. sumAssignRelaData(billsIndexData, [
  243. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  244. ]);
  245. } else {
  246. const curStage = await this.ctx.service.stageBills.getLastestStageData(tender.id, stage.id);
  247. sumAssignRelaData(billsIndexData, [
  248. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  249. ]);
  250. }
  251. }
  252. }
  253. billsTree.loadDatas(billsData);
  254. billsTree.calculateAll();
  255. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  256. gatherUtils.gatherZone(gatherNode, sourceNode, 't_' + index + '_', helper);
  257. });
  258. }
  259. async _gatherFinalData(sTender, index, hasPre) {
  260. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  261. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  262. where: { tid: tender.id },
  263. orders: [['order', 'desc']],
  264. });
  265. if (stages.length !== 0) {
  266. const lastStage = stages[0];
  267. if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
  268. stages.splice(0, 1);
  269. }
  270. }
  271. await this._gatherStageData(index, tender, stages[0], hasPre);
  272. }
  273. async _gatherCheckedFinalData(sTender, index, hasPre) {
  274. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  275. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  276. where: { tid: tender.id },
  277. orders: [['order', 'desc']],
  278. });
  279. if (stages.length !== 0) {
  280. const lastStage = stages[0];
  281. if (lastStage.status !== auditConst.stage.status.checked) {
  282. stages.splice(0, 1);
  283. }
  284. }
  285. await this._gatherStageData(index, tender, stages[0], hasPre);
  286. }
  287. async _gatherLedgerData(sTender, index) {
  288. const helper = this.ctx.helper;
  289. const billsTree = new Ledger.billsTree(this.ctx, {
  290. id: 'ledger_id',
  291. pid: 'ledger_pid',
  292. order: 'order',
  293. level: 'level',
  294. rootId: -1,
  295. keys: ['id', 'tender_id', 'ledger_id'],
  296. stageId: 'id',
  297. calcFields: ['deal_tp', 'total_price'],
  298. });
  299. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  300. const billsData = await this.ctx.service.ledger.getData(tender.id);
  301. billsTree.loadDatas(billsData);
  302. billsTree.calculateAll();
  303. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  304. gatherUtils.gatherLedger(tender, gatherNode, sourceNode, 't_' + index + '_', helper);
  305. })
  306. }
  307. async _gatherSpecialData(sTender, sKey) {
  308. const helper = this.ctx.helper;
  309. const billsTree = new Ledger.billsTree(this.ctx, {
  310. id: 'ledger_id',
  311. pid: 'ledger_pid',
  312. order: 'order',
  313. level: 'level',
  314. rootId: -1,
  315. keys: ['id', 'tender_id', 'ledger_id'],
  316. stageId: 'id',
  317. calcFields: ['deal_tp', 'total_price'],
  318. });
  319. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  320. const billsData = await this.ctx.service.ledger.getData(tender.id);
  321. billsTree.loadDatas(billsData);
  322. billsTree.calculateAll();
  323. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  324. gatherUtils.gatherSpecial(gatherNode, sourceNode, 'ts_' + sKey + '_', helper);
  325. })
  326. }
  327. async getGatherStageBills(memFieldKeys, gsDefine, gsCustom) {
  328. if (!gsDefine || !gsDefine.enable) return [];
  329. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  330. const gsSetting = JSON.parse(gsDefine.setting);
  331. let commonIndex = 0;
  332. for (const tender of gsCustom.tenders) {
  333. const specialKey = this._checkSpecialTender(tender, gsSetting.special);
  334. if (specialKey === '') {
  335. switch (gsSetting.type) {
  336. case 'month':
  337. await this._gatherMonthData(tender, commonIndex, gsCustom.month, gsSetting.hasPre);
  338. break;
  339. case 'zone':
  340. await this._gatherZoneData(tender, commonIndex, gsCustom.zone);
  341. break;
  342. case 'final':
  343. await this._gatherFinalData(tender, commonIndex, gsSetting.hasPre);
  344. break;
  345. case 'checked-final':
  346. await this._gatherCheckedFinalData(tender, commonIndex, gsSetting.hasPre);
  347. break;
  348. case 'ledger':
  349. await this._gatherLedgerData(tender, commonIndex);
  350. break;
  351. }
  352. commonIndex++;
  353. } else {
  354. await this._gatherSpecialData(tender, specialKey);
  355. }
  356. }
  357. this.resultTree.generateSortNodes();
  358. return this.resultTree.getDefaultDatas();
  359. }
  360. /**
  361. * 标段信息
  362. */
  363. async _getBaseTenderInfo(tender) {
  364. const info = {
  365. id: tender.id,
  366. name: tender.name,
  367. deal_tp: tender.deal_tp,
  368. tp: tender.total_price
  369. };
  370. if (tender.ledger_status === auditConst.ledger.status.uncheck || tender.ledger_status === auditConst.ledger.status.checkNo) {
  371. const sum = await this.ctx.service.ledger.addUp({tender_id: tender.id/*, is_leaf: true*/});
  372. info.tp = sum.total_price;
  373. info.deal_tp = sum.deal_tp;
  374. }
  375. return info;
  376. }
  377. async _getStageTenderInfo(stage, info) {
  378. if (stage) {
  379. const helper = this.ctx.helper;
  380. await this.ctx.service.stage.doCheckStage(stage);
  381. await this.ctx.service.stage.checkStageGatherData(stage);
  382. info.pre_contract_tp = stage.pre_contract_tp;
  383. info.pre_qc_tp = stage.pre_qc_tp;
  384. info.pre_gather_tp = helper.add(info.pre_contract_tp, info.pre_qc_tp);
  385. info.contract_tp = stage.contract_tp;
  386. info.qc_tp = stage.qc_tp;
  387. info.gather_tp = helper.add(info.contract_tp, info.qc_tp);
  388. info.end_contract_tp = helper.add(info.pre_contract_tp, info.contract_tp);
  389. info.end_qc_tp = helper.add(info.pre_qc_tp, info.qc_tp);
  390. info.end_gather_tp = helper.add(info.pre_gather_tp, info.gather_tp);
  391. info.yf = stage.yf;
  392. info.pre_yf = stage.pre_yf;
  393. info.end_yf = helper.add(stage.yf, stage.pre_yf);
  394. }
  395. }
  396. async _gatherMonthTenderInfo(sTender, index, month, hasPre) {
  397. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  398. const info = await this._getBaseTenderInfo(tender);
  399. const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
  400. this._getStageTenderInfo(stage, info);
  401. this.resultTenderInfo.push(info);
  402. }
  403. async _gatherZoneTenderInfo(sTender, index, zone) {
  404. const helper = this.ctx.helper;
  405. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  406. const info = await this._getBaseTenderInfo(tender);
  407. const times = zone.split(' - ');
  408. if (times.length !== 2) throw '选择的汇总周期无效';
  409. const beginTime = moment(times[0], 'YYYY-MM').date();
  410. const endTime = moment(times[1], 'YYYY-MM').date();
  411. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tender.id } });
  412. for (const stage of stages) {
  413. const sTime = moment(stage.s_time, 'YYYY-MM').date();
  414. if (sTime >= beginTime && sTime <= endTime) {
  415. await this.ctx.service.stage.doCheckStage(stage);
  416. await this.ctx.service.stage.checkStageGatherData(stage);
  417. info.contract_tp = helper.add(info.contract_tp, stage.contract_tp);
  418. info.qc_tp = helper.add(info.qc_tp, stage.qc_tp);
  419. info.yf = helper.add(info.yf, stage.yf);
  420. }
  421. }
  422. info.gather_tp = helper.add(info.contract_tp, info.qc_tp);
  423. this.resultTenderInfo.push(info);
  424. }
  425. async _gatherFinalTenderInfo(sTender, index, hasPre) {
  426. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  427. const info = await this._getBaseTenderInfo(tender);
  428. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  429. where: { tid: tender.id },
  430. orders: [['order', 'desc']],
  431. });
  432. if (stages.length !== 0) {
  433. const lastStage = stages[0];
  434. if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
  435. stages.splice(0, 1);
  436. }
  437. }
  438. await this._getStageTenderInfo(stages[0], info);
  439. this.resultTenderInfo.push(info);
  440. }
  441. async _gatherCheckedFinalTenderInfo(sTender, index, hasPre) {
  442. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  443. const info = await this._getBaseTenderInfo(tender);
  444. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  445. where: { tid: tender.id },
  446. orders: [['order', 'desc']],
  447. });
  448. if (stages.length !== 0) {
  449. const lastStage = stages[0];
  450. if (lastStage.status !== auditConst.stage.status.checked) {
  451. stages.splice(0, 1);
  452. }
  453. }
  454. await this._getStageTenderInfo(stages[0], info);
  455. this.resultTenderInfo.push(info);
  456. }
  457. async _gatherLedgerTenderInfo(sTender, index) {
  458. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  459. const info = await this._getBaseTenderInfo(tender);
  460. this.resultTenderInfo.push(info);
  461. }
  462. async _gatherSpecialTenderInfo(sTender, sKey) {
  463. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  464. const info = await this._getBaseTenderInfo(tender);
  465. info.spec = sKey;
  466. this.resultTenderInfo.push(info);
  467. }
  468. async getGatherTenderInfo(memFieldKeys, gsDefine, gsCustom) {
  469. if (!gsDefine || !gsDefine.enable) return [];
  470. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  471. this.resultTenderInfo = [];
  472. const gsSetting = JSON.parse(gsDefine.setting);
  473. let commonIndex = 0;
  474. for (const tender of gsCustom.tenders) {
  475. const specialKey = this._checkSpecialTender(tender, gsSetting.special);
  476. if (specialKey === '') {
  477. switch (gsSetting.type) {
  478. case 'month':
  479. await this._gatherMonthTenderInfo(tender, commonIndex, gsCustom.month, gsSetting.hasPre);
  480. break;
  481. case 'zone':
  482. await this._gatherZoneTenderInfo(tender, commonIndex, gsCustom.zone);
  483. break;
  484. case 'final':
  485. await this._gatherFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);
  486. break;
  487. case 'checked-final':
  488. await this._gatherCheckedFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);
  489. break;
  490. case 'ledger':
  491. await this._gatherLedgerTenderInfo(tender, commonIndex);
  492. break;
  493. }
  494. commonIndex++;
  495. } else {
  496. await this._gatherSpecialTenderInfo(tender, specialKey);
  497. }
  498. }
  499. return this.resultTenderInfo;
  500. }
  501. /**
  502. * 合同支付
  503. */
  504. _gatherPayRecord(dealPay, fun) {
  505. let rdp;
  506. if (dealPay.ptype !== payConst.payType.normal) {
  507. rdp = this.ctx.helper._.find(this.resultDealpPay, {ptype: dealPay.ptype});
  508. } else {
  509. rdp = this.ctx.helper._.find(this.resultDealpPay, {
  510. name: dealPay.name,
  511. minus: dealPay.minus ? true : false,
  512. is_yf: dealPay.is_yf ? true : false,
  513. });
  514. }
  515. if (!rdp) {
  516. rdp = {
  517. ptype: dealPay.ptype,
  518. name: dealPay.name,
  519. minus: dealPay.minus ? true : false,
  520. is_yf: dealPay.is_yf ? true : false,
  521. };
  522. this.resultDealpPay.push(rdp);
  523. }
  524. if (fun) fun(rdp, dealPay);
  525. }
  526. async _checkStagePayCalc(tender, stage, dealPay) {
  527. if (!stage.readOnly) {
  528. // 计算 本期金额
  529. const payCalculator = new PayCalculator(this.ctx, stage, tender.info);
  530. await payCalculator.calculateAll(dealPay);
  531. }
  532. }
  533. async _gatherStagePay(index, tender, stage, hasPre) {
  534. if (stage) {
  535. const helper = this.ctx.helper;
  536. await this.ctx.service.stage.doCheckStage(stage);
  537. const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
  538. await this._checkStagePayCalc(tender, stage, dealPay);
  539. for (const dp of dealPay) {
  540. dp.end_tp = helper.add(dp.pre_tp, dp.tp);
  541. this._gatherPayRecord(dp, function (gatherData, sourceData) {
  542. const preFix = 't_' + index + '_';
  543. gatherData[preFix + '_id'] = tender.id;
  544. gatherData[preFix + '_name'] = tender.name;
  545. gatherData[preFix + 'tp'] = helper.add(gatherData[preFix + 'tp'], sourceData.tp);
  546. gatherData[preFix + 'pre_tp'] = helper.add(gatherData[preFix + 'pre_tp'], sourceData.pre_tp);
  547. gatherData[preFix + 'end_tp'] = helper.add(gatherData[preFix + 'end_tp'], sourceData.end_tp);
  548. gatherData['s_' + 'tp'] = helper.add(gatherData['s_' + 'tp'], sourceData.tp);
  549. gatherData['s_' + 'pre_tp'] = helper.add(gatherData['s_' + 'pre_tp'], sourceData.pre_tp);
  550. gatherData['s_' + 'end_tp'] = helper.add(gatherData['s_' + 'end_tp'], sourceData.end_tp);
  551. });
  552. }
  553. }
  554. }
  555. async _gatherMonthStagePay(sTender, index, month, hasPre) {
  556. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  557. const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
  558. await this._gatherStagePay(index, tender, stage, hasPre);
  559. }
  560. async _gatherZoneStagePay(sTender, index, zone) {
  561. const helper = this.ctx.helper;
  562. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  563. const times = zone.split(' - ');
  564. if (times.length !== 2) throw '选择的汇总周期无效';
  565. const beginTime = moment(times[0], 'YYYY-MM').date();
  566. const endTime = moment(times[1], 'YYYY-MM').date();
  567. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tender.id } });
  568. for (const stage of stages) {
  569. const sTime = moment(stage.s_time, 'YYYY-MM').date();
  570. if (sTime >= beginTime && sTime <= endTime) {
  571. await this.ctx.service.stage.doCheckStage(stage);
  572. const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
  573. await this._checkStagePayCalc(tender, stage, dealPay);
  574. for (const dp of dealPay) {
  575. dp.end_tp = helper.add(dp.pre_tp, dp.tp);
  576. this._gatherPayRecord(dp, function (gatherData, sourceData) {
  577. const preFix = 't_' + index + '_';
  578. gatherData[preFix + '_id'] = tender.id;
  579. gatherData[preFix + '_name'] = tender.name;
  580. gatherData[preFix + 'tp'] = helper.add(gatherData[preFix + 'tp'], sourceData.tp);
  581. gatherData['s_' + 'tp'] = helper.add(gatherData['s_' + 'tp'], sourceData.tp);
  582. });
  583. }
  584. }
  585. }
  586. }
  587. async _gatherFinalStagePay(sTender, index, hasPre) {
  588. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  589. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  590. where: { tid: tender.id },
  591. orders: [['order', 'desc']],
  592. });
  593. if (stages.length !== 0) {
  594. const lastStage = stages[0];
  595. if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
  596. stages.splice(0, 1);
  597. }
  598. }
  599. await this._gatherStagePay(index, tender, stages[0], hasPre);
  600. }
  601. async _gatherCheckedFinalStagePay(sTender, index, hasPre) {
  602. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  603. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  604. where: { tid: tender.id },
  605. orders: [['order', 'desc']],
  606. });
  607. if (stages.length !== 0) {
  608. const lastStage = stages[0];
  609. if (lastStage.status !== auditConst.stage.status.checked) {
  610. stages.splice(0, 1);
  611. }
  612. }
  613. await this._gatherStagePay(index, tender, stages[0], hasPre);
  614. }
  615. async getGatherStagePay(memFieldKeys, gsDefine, gsCustom) {
  616. if (!gsDefine || !gsDefine.enable) return [];
  617. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  618. this.resultTenderInfo = [];
  619. const gsSetting = JSON.parse(gsDefine.setting);
  620. let commonIndex = 0;
  621. for (const tender of gsCustom.tenders) {
  622. const specialKey = this._checkSpecialTender(tender, gsSetting.special);
  623. if (specialKey === '') {
  624. switch (gsSetting.type) {
  625. case 'month':
  626. await this._gatherMonthStagePay(tender, commonIndex, gsCustom.month, gsSetting.hasPre);
  627. break;
  628. case 'zone':
  629. await this._gatherZoneStagePay(tender, commonIndex, gsCustom.zone);
  630. break;
  631. case 'final':
  632. await this._gatherFinalStagePay(tender, commonIndex, gsSetting.hasPre);
  633. break;
  634. case 'checked-final':
  635. await this._gatherCheckedFinalStagePay(tender, commonIndex, gsSetting.hasPre);
  636. break;
  637. }
  638. commonIndex++;
  639. }
  640. // 合同支付,只有在每一期中有数据,故特殊标段直接不汇总合同支付
  641. }
  642. return this.resultDealpPay;
  643. }
  644. }
  645. return RptGatherMemory;
  646. };