rpt_gather_memory.js 39 KB

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