rpt_gather_memory.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. info.deal_param.contractPriceWithoutZL = this.ctx.helper.sub(info.deal_param.contractPrice,
  412. info.deal_param.zanLiePrice);
  413. return info;
  414. }
  415. async _getStageTenderInfo(stage, info) {
  416. if (stage) {
  417. const helper = this.ctx.helper;
  418. await this.ctx.service.stage.doCheckStage(stage);
  419. await this.ctx.service.stage.checkStageGatherData(stage);
  420. info.pre_contract_tp = stage.pre_contract_tp;
  421. info.pre_qc_tp = stage.pre_qc_tp;
  422. info.pre_gather_tp = helper.add(info.pre_contract_tp, info.pre_qc_tp);
  423. info.contract_tp = stage.contract_tp;
  424. info.qc_tp = stage.qc_tp;
  425. info.gather_tp = helper.add(info.contract_tp, info.qc_tp);
  426. info.end_contract_tp = helper.add(info.pre_contract_tp, info.contract_tp);
  427. info.end_qc_tp = helper.add(info.pre_qc_tp, info.qc_tp);
  428. info.end_gather_tp = helper.add(info.pre_gather_tp, info.gather_tp);
  429. info.yf_tp = stage.yf_tp;
  430. info.pre_yf_tp = stage.pre_yf_tp;
  431. info.end_yf_tp = helper.add(stage.yf_tp, stage.pre_yf_tp);
  432. }
  433. }
  434. async _gatherMonthTenderInfo(sTender, index, month, hasPre) {
  435. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  436. const info = await this._getBaseTenderInfo(tender);
  437. const stages = await this._getValidStages(tender.id);
  438. const stage = this.ctx.helper._.find(stages, {s_time: month});
  439. await this._getStageTenderInfo(stage, info);
  440. this.resultTenderInfo.push(info);
  441. }
  442. async _gatherZoneTenderInfo(sTender, index, zone) {
  443. const helper = this.ctx.helper;
  444. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  445. const info = await this._getBaseTenderInfo(tender);
  446. const times = zone.split(' - ');
  447. if (times.length !== 2) throw '选择的汇总周期无效';
  448. const beginTime = moment(times[0], 'YYYY-MM');
  449. const endTime = moment(times[1], 'YYYY-MM');
  450. const stages = await this._getValidStages(tender.id, true);
  451. for (const stage of stages) {
  452. const sTime = moment(stage.s_time, 'YYYY-MM');
  453. if (sTime.isBetween(beginTime, endTime, null, '[]')) {
  454. await this.ctx.service.stage.doCheckStage(stage);
  455. await this.ctx.service.stage.checkStageGatherData(stage);
  456. info.contract_tp = helper.add(info.contract_tp, stage.contract_tp);
  457. info.qc_tp = helper.add(info.qc_tp, stage.qc_tp);
  458. info.yf_tp = helper.add(info.yf_tp, stage.yf_tp);
  459. }
  460. }
  461. info.gather_tp = helper.add(info.contract_tp, info.qc_tp);
  462. this.resultTenderInfo.push(info);
  463. }
  464. async _gatherFinalTenderInfo(sTender, index, hasPre) {
  465. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  466. const info = await this._getBaseTenderInfo(tender);
  467. const stages = await this._getValidStages(tender.id);
  468. await this._getStageTenderInfo(stages[0], info);
  469. this.resultTenderInfo.push(info);
  470. }
  471. async _gatherCheckedFinalTenderInfo(sTender, index, hasPre) {
  472. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  473. const info = await this._getBaseTenderInfo(tender);
  474. const stages = await this._getCheckedStages(tender.id);
  475. await this._getStageTenderInfo(stages[0], info);
  476. this.resultTenderInfo.push(info);
  477. }
  478. async _gatherLedgerTenderInfo(sTender, index) {
  479. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  480. const info = await this._getBaseTenderInfo(tender);
  481. this.resultTenderInfo.push(info);
  482. }
  483. async _gatherSpecialTenderInfo(sTender, sKey) {
  484. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  485. const info = await this._getBaseTenderInfo(tender);
  486. info.spec = sKey;
  487. this.resultTenderInfo.push(info);
  488. }
  489. async getGatherTenderInfo(memFieldKeys, gsDefine, gsCustom) {
  490. if (!gsDefine || !gsDefine.enable) return [];
  491. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  492. this.resultTenderInfo = [];
  493. const gsSetting = JSON.parse(gsDefine.setting);
  494. let commonIndex = 0;
  495. for (const tender of gsCustom.tenders) {
  496. const specialKey = this._checkSpecialTender(tender, gsSetting.special);
  497. if (specialKey === '') {
  498. switch (gsSetting.type) {
  499. case 'month':
  500. await this._gatherMonthTenderInfo(tender, commonIndex, gsCustom.month, gsSetting.hasPre);
  501. break;
  502. case 'zone':
  503. await this._gatherZoneTenderInfo(tender, commonIndex, gsCustom.zone);
  504. break;
  505. case 'final':
  506. await this._gatherFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);
  507. break;
  508. case 'checked-final':
  509. await this._gatherCheckedFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);
  510. break;
  511. case 'ledger':
  512. await this._gatherLedgerTenderInfo(tender, commonIndex);
  513. break;
  514. }
  515. commonIndex++;
  516. } else {
  517. await this._gatherSpecialTenderInfo(tender, specialKey);
  518. }
  519. }
  520. return this.resultTenderInfo;
  521. }
  522. /**
  523. * 合同支付
  524. */
  525. _gatherPayRecord(dealPay, fun) {
  526. let rdp;
  527. if (dealPay.ptype !== payConst.payType.normal) {
  528. rdp = this.ctx.helper._.find(this.resultDealPay, {ptype: dealPay.ptype});
  529. } else {
  530. rdp = this.ctx.helper._.find(this.resultDealPay, {
  531. name: dealPay.name,
  532. minus: dealPay.minus ? true : false,
  533. is_yf: dealPay.is_yf ? true : false,
  534. });
  535. }
  536. if (!rdp) {
  537. rdp = {
  538. ptype: dealPay.ptype,
  539. name: dealPay.name,
  540. minus: dealPay.minus ? true : false,
  541. is_yf: dealPay.is_yf ? true : false,
  542. };
  543. this.resultDealPay.push(rdp);
  544. }
  545. if (fun) fun(rdp, dealPay);
  546. }
  547. async _checkStagePayCalc(tender, stage, dealPay) {
  548. if (!stage.readOnly) {
  549. // 计算 本期金额
  550. const payCalculator = new PayCalculator(this.ctx, stage, tender.info);
  551. await payCalculator.calculateAll(dealPay);
  552. }
  553. }
  554. async _gatherStagePay(completeData, tender, stage, hasPre) {
  555. if (stage) {
  556. completeData.id = tender.id;
  557. completeData.name = tender.name;
  558. const helper = this.ctx.helper;
  559. await this.ctx.service.stage.doCheckStage(stage);
  560. const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
  561. await this._checkStagePayCalc(tender, stage, dealPay);
  562. for (const dp of dealPay) {
  563. dp.end_tp = helper.add(dp.pre_tp, dp.tp);
  564. this._gatherPayRecord(dp, function (gatherData, sourceData) {
  565. gatherData[completeData.prefix + 'id'] = tender.id;
  566. gatherData[completeData.prefix + 'name'] = tender.name;
  567. gatherData[completeData.prefix + 'tp'] = helper.add(gatherData[completeData.prefix + 'tp'], sourceData.tp);
  568. gatherData[completeData.prefix + 'pre_tp'] = helper.add(gatherData[completeData.prefix + 'pre_tp'], sourceData.pre_tp);
  569. gatherData[completeData.prefix + 'end_tp'] = helper.add(gatherData[completeData.prefix + 'end_tp'], sourceData.end_tp);
  570. gatherData['s_' + 'tp'] = helper.add(gatherData['s_' + 'tp'], sourceData.tp);
  571. gatherData['s_' + 'pre_tp'] = helper.add(gatherData['s_' + 'pre_tp'], sourceData.pre_tp);
  572. gatherData['s_' + 'end_tp'] = helper.add(gatherData['s_' + 'end_tp'], sourceData.end_tp);
  573. });
  574. }
  575. }
  576. }
  577. async _gatherMonthStagePay(sTender, completeData, month, hasPre) {
  578. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  579. const stages = await this._getValidStages(tender.id);
  580. const stage = this.ctx.helper._.find(stages, {s_time: month});
  581. await this._gatherStagePay(completeData, tender, stage, hasPre);
  582. }
  583. async _gatherZoneStagePay(sTender, completeData, zone) {
  584. const helper = this.ctx.helper;
  585. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  586. completeData.id = tender.id;
  587. completeData.name = tender.name;
  588. const stages = await this._getValidStages(tender.id);
  589. const times = zone.split(' - ');
  590. if (times.length !== 2) throw '选择的汇总周期无效';
  591. const beginTime = moment(times[0], 'YYYY-MM');
  592. const endTime = moment(times[1], 'YYYY-MM');
  593. for (const stage of stages) {
  594. const sTime = moment(stage.s_time, 'YYYY-MM');
  595. if (sTime.isBetween(beginTime, endTime, null, '[]')) {
  596. await this.ctx.service.stage.doCheckStage(stage);
  597. const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
  598. await this._checkStagePayCalc(tender, stage, dealPay);
  599. for (const dp of dealPay) {
  600. dp.end_tp = helper.add(dp.pre_tp, dp.tp);
  601. this._gatherPayRecord(dp, function (gatherData, sourceData) {
  602. gatherData[completeData.prefix + 'id'] = tender.id;
  603. gatherData[completeData.prefix + 'name'] = tender.name;
  604. gatherData[completeData.prefix + 'tp'] = helper.add(gatherData[completeData.prefix + 'tp'], sourceData.tp);
  605. gatherData['s_' + 'tp'] = helper.add(gatherData['s_' + 'tp'], sourceData.tp);
  606. });
  607. }
  608. }
  609. }
  610. }
  611. async _gatherFinalStagePay(sTender, completeData, hasPre) {
  612. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  613. const stages = await this._getValidStages(tender.id);
  614. await this._gatherStagePay(completeData, tender, stages[0], hasPre);
  615. }
  616. async _gatherCheckedFinalStagePay(sTender, completeData, hasPre) {
  617. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  618. const stages = await this._getCheckedStages(tender.id);
  619. await this._gatherStagePay(completeData, tender, stages[0], hasPre);
  620. }
  621. async getGatherStagePay(memFieldKeys, gsDefine, gsCustom) {
  622. if (!gsDefine || !gsDefine.enable) return [];
  623. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  624. this.resultTenderInfo = [];
  625. const gsSetting = JSON.parse(gsDefine.setting);
  626. let commonIndex = 0;
  627. const completeDatas = [];
  628. for (const tender of gsCustom.tenders) {
  629. const specialKey = this._checkSpecialTender(tender, gsSetting.special);
  630. if (specialKey === '') {
  631. const completeData = {
  632. prefix: 't_' + commonIndex + '_',
  633. };
  634. completeDatas.push(completeData);
  635. switch (gsSetting.type) {
  636. case 'month':
  637. await this._gatherMonthStagePay(tender, completeData, gsCustom.month, gsSetting.hasPre);
  638. break;
  639. case 'zone':
  640. await this._gatherZoneStagePay(tender, completeData, gsCustom.zone);
  641. break;
  642. case 'final':
  643. await this._gatherFinalStagePay(tender, completeData, gsSetting.hasPre);
  644. break;
  645. case 'checked-final':
  646. await this._gatherCheckedFinalStagePay(tender, completeData, gsSetting.hasPre);
  647. break;
  648. }
  649. commonIndex++;
  650. }
  651. // 合同支付,只有在每一期中有数据,故特殊标段直接不汇总合同支付
  652. }
  653. gatherUtils.completeGatherData(this.resultDealPay, completeDatas);
  654. return this.resultDealPay;
  655. }
  656. /**
  657. * 签约清单
  658. */
  659. async _gatherDealBills(tender, fun) {
  660. const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({
  661. where: { tender_id: tender.id }
  662. });
  663. for (const db of dealBills) {
  664. let rdb = this.ctx.helper._.find(this.resultDealBills, {
  665. code: db.code,
  666. name: db.name,
  667. unit: db.unit,
  668. unit_price: db.unit_price ? db.unit_price: 0,
  669. });
  670. if (!rdb) {
  671. rdb = {
  672. code: db.code,
  673. name: db.name,
  674. unit: db.unit,
  675. unit_price: db.unit_price ? db.unit_price: 0,
  676. };
  677. this.resultDealBills.push(rdb);
  678. }
  679. if (fun) fun(rdb, db);
  680. }
  681. }
  682. async _gatherCommonDealBills(sTender, completeData) {
  683. const self = this;
  684. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  685. completeData.id = tender.id;
  686. completeData.name = tender.name;
  687. await this._gatherDealBills(tender, function (gatherData, sourceData) {
  688. gatherData[completeData.prefix + 'id'] = tender.id;
  689. gatherData[completeData.prefix + 'name'] = tender.name;
  690. gatherData[completeData.prefix + 'qty'] = sourceData.quantity;
  691. gatherData[completeData.prefix + 'tp'] = sourceData.total_price;
  692. gatherData['s_' + 'qty'] = self.ctx.helper.add(gatherData['s_' + 'qty'], sourceData.quantity);
  693. gatherData['s_' + 'tp'] = self.ctx.helper.add(gatherData['s_' + 'tp'], sourceData.total_price);
  694. });
  695. }
  696. async _gatherSpecialDealBills(sTender, sKey) {
  697. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  698. await this._gatherDealBills(tender, function (gatherData, sourceData) {
  699. const prefix = 'ts_' + sKey + '_';
  700. gatherData[prefix + 'qty'] = sourceData.quantity;
  701. gatherData[prefix + 'tp'] = sourceData.total_price;
  702. });
  703. }
  704. async getGatherDealBills(memFieldKeys, gsDefine, gsCustom) {
  705. if (!gsDefine || !gsDefine.enable) return [];
  706. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  707. this.resultTenderInfo = [];
  708. const gsSetting = JSON.parse(gsDefine.setting);
  709. let commonIndex = 0;
  710. const completeDatas = [];
  711. for (const tender of gsCustom.tenders) {
  712. const specialKey = this._checkSpecialTender(tender, gsSetting.special);
  713. if (specialKey === '') {
  714. const completeData = {
  715. prefix: 't_' + commonIndex + '_',
  716. };
  717. completeDatas.push(completeData);
  718. await this._gatherCommonDealBills(tender, completeData);
  719. commonIndex++;
  720. } else {
  721. await this._gatherSpecialDealBills(tender, specialKey);
  722. }
  723. }
  724. gatherUtils.completeGatherData(this.resultDealBills, completeDatas);
  725. return this.resultDealBills;
  726. }
  727. }
  728. return RptGatherMemory;
  729. };