rpt_gather_memory.js 42 KB

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