rpt_gather_memory.js 39 KB

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