rpt_gather_memory.js 39 KB

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