rpt_gather_memory.js 40 KB

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