rpt_gather_memory.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('../lib/ledger');
  10. const auditConst = require('../const/audit');
  11. const moment = require('moment');
  12. const indexPre = 'id_';
  13. const gatherUtils = {
  14. gatherStage: function (tender, gatherNode, sourceNode, prefix, helper) {
  15. gatherNode[prefix + 'id'] = tender.id;
  16. gatherNode[prefix + 'name'] = tender.name;
  17. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  18. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  19. gatherNode[prefix + "contract_qty"] = helper.add(gatherNode[prefix + "contract_qty"], sourceNode.contract_qty);
  20. gatherNode[prefix + "contract_tp"] = helper.add(gatherNode[prefix + "contract_tp"], sourceNode.contract_tp);
  21. gatherNode[prefix + "qc_qty"] = helper.add(gatherNode[prefix + "qc_qty"], sourceNode.qc_qty);
  22. gatherNode[prefix + "qc_tp"] = helper.add(gatherNode[prefix + "qc_tp"], sourceNode.qc_tp);
  23. gatherNode[prefix + "gather_qty"] = helper.add(gatherNode[prefix + "gather_qty"], sourceNode.gather_qty);
  24. gatherNode[prefix + "gather_tp"] = helper.add(gatherNode[prefix + "gather_tp"], sourceNode.gather_tp);
  25. gatherNode[prefix + "pre_contract_qty"] = helper.add(gatherNode[prefix + "pre_contract_qty"], sourceNode.pre_contract_qty);
  26. gatherNode[prefix + "pre_contract_tp"] = helper.add(gatherNode[prefix + "pre_contract_tp"], sourceNode.pre_contract_tp);
  27. gatherNode[prefix + "pre_qc_qty"] = helper.add(gatherNode[prefix + "pre_qc_qty"], sourceNode.pre_qc_qty);
  28. gatherNode[prefix + "pre_qc_tp"] = helper.add(gatherNode[prefix + "pre_qc_tp"], sourceNode.pre_qc_tp);
  29. gatherNode[prefix + "pre_gather_qty"] = helper.add(gatherNode[prefix + "pre_gather_qty"], sourceNode.pre_gather_qty);
  30. gatherNode[prefix + "pre_gather_tp"] = helper.add(gatherNode[prefix + "pre_gather_tp"], sourceNode.pre_gather_tp);
  31. gatherNode[prefix + "end_contract_qty"] = helper.add(gatherNode[prefix + "end_contract_qty"], sourceNode.end_contract_qty);
  32. gatherNode[prefix + "end_contract_tp"] = helper.add(gatherNode[prefix + "end_contract_tp"], sourceNode.end_contract_tp);
  33. gatherNode[prefix + "end_qc_qty"] = helper.add(gatherNode[prefix + "end_qc_qty"], sourceNode.end_qc_qty);
  34. gatherNode[prefix + "end_qc_tp"] = helper.add(gatherNode[prefix + "end_qc_tp"], sourceNode.end_qc_tp);
  35. gatherNode[prefix + "end_gather_qty"] = helper.add(gatherNode[prefix + "end_gather_qty"], sourceNode.end_gather_qty);
  36. gatherNode[prefix + "end_gather_tp"] = helper.add(gatherNode[prefix + "end_gather_tp"], sourceNode.end_gather_tp);
  37. gatherNode['s_' + "qty"] = helper.add(gatherNode['s_' + "qty"], sourceNode.quantity);
  38. gatherNode['s_' + "tp"] = helper.add(gatherNode['s_' + "tp"], sourceNode.total_price);
  39. gatherNode['s_' + "contract_qty"] = helper.add(gatherNode['s_' + "contract_qty"], sourceNode.contract_qty);
  40. gatherNode['s_' + "contract_tp"] = helper.add(gatherNode['s_' + "contract_tp"], sourceNode.contract_tp);
  41. gatherNode['s_' + "qc_qty"] = helper.add(gatherNode['s_' + "qc_qty"], sourceNode.qc_qty);
  42. gatherNode['s_' + "qc_tp"] = helper.add(gatherNode['s_' + "qc_tp"], sourceNode.qc_tp);
  43. gatherNode['s_' + "gather_qty"] = helper.add(gatherNode['s_' + "gather_qty"], sourceNode.gather_qty);
  44. gatherNode['s_' + "gather_tp"] = helper.add(gatherNode['s_' + "gather_tp"], sourceNode.gather_tp);
  45. gatherNode['s_' + "pre_contract_qty"] = helper.add(gatherNode['s_' + "pre_contract_qty"], sourceNode.pre_contract_qty);
  46. gatherNode['s_' + "pre_contract_tp"] = helper.add(gatherNode['s_' + "pre_contract_tp"], sourceNode.pre_contract_tp);
  47. gatherNode['s_' + "pre_qc_qty"] = helper.add(gatherNode['s_' + "pre_qc_qty"], sourceNode.pre_qc_qty);
  48. gatherNode['s_' + "pre_qc_tp"] = helper.add(gatherNode['s_' + "pre_qc_tp"], sourceNode.pre_qc_tp);
  49. gatherNode['s_' + "pre_gather_qty"] = helper.add(gatherNode['s_' + "pre_gather_qty"], sourceNode.pre_gather_qty);
  50. gatherNode['s_' + "pre_gather_tp"] = helper.add(gatherNode['s_' + "pre_gather_tp"], sourceNode.pre_gather_tp);
  51. gatherNode['s_' + "end_contract_qty"] = helper.add(gatherNode['s_' + "end_contract_qty"], sourceNode.end_contract_qty);
  52. gatherNode['s_' + "end_contract_tp"] = helper.add(gatherNode['s_' + "end_contract_tp"], sourceNode.end_contract_tp);
  53. gatherNode['s_' + "end_qc_qty"] = helper.add(gatherNode['s_' + "end_qc_qty"], sourceNode.end_qc_qty);
  54. gatherNode['s_' + "end_qc_tp"] = helper.add(gatherNode['s_' + "end_qc_tp"], sourceNode.end_qc_tp);
  55. gatherNode['s_' + "end_gather_qty"] = helper.add(gatherNode['s_' + "end_gather_qty"], sourceNode.end_gather_qty);
  56. gatherNode['s_' + "end_gather_tp"] = helper.add(gatherNode['s_' + "end_gather_tp"], sourceNode.end_gather_tp);
  57. },
  58. gatherZone: function (tender, gatherNode, sourceNode, prefix, helper) {
  59. gatherNode[prefix + 'id'] = tender.id;
  60. gatherNode[prefix + 'name'] = tender.name;
  61. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  62. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  63. gatherNode[prefix + "contract_qty"] = helper.add(gatherNode[prefix + "contract_qty"], sourceNode.contract_qty);
  64. gatherNode[prefix + "contract_tp"] = helper.add(gatherNode[prefix + "contract_tp"], sourceNode.contract_tp);
  65. gatherNode[prefix + "qc_qty"] = helper.add(gatherNode[prefix + "qc_qty"], sourceNode.qc_qty);
  66. gatherNode[prefix + "qc_tp"] = helper.add(gatherNode[prefix + "qc_tp"], sourceNode.qc_tp);
  67. gatherNode[prefix + "gather_qty"] = helper.add(gatherNode[prefix + "gather_qty"], sourceNode.gather_qty);
  68. gatherNode[prefix + "gather_tp"] = helper.add(gatherNode[prefix + "gather_tp"], sourceNode.gather_tp);
  69. gatherNode['s_' + "qty"] = helper.add(gatherNode['s_' + "qty"], sourceNode.quantity);
  70. gatherNode['s_' + "tp"] = helper.add(gatherNode['s_' + "tp"], sourceNode.total_price);
  71. gatherNode['s_' + "contract_qty"] = helper.add(gatherNode['s_' + "contract_qty"], sourceNode.contract_qty);
  72. gatherNode['s_' + "contract_tp"] = helper.add(gatherNode['s_' + "contract_tp"], sourceNode.contract_tp);
  73. gatherNode['s_' + "qc_qty"] = helper.add(gatherNode['s_' + "qc_qty"], sourceNode.qc_qty);
  74. gatherNode['s_' + "qc_tp"] = helper.add(gatherNode['s_' + "qc_tp"], sourceNode.qc_tp);
  75. gatherNode['s_' + "gather_qty"] = helper.add(gatherNode['s_' + "gather_qty"], sourceNode.gather_qty);
  76. gatherNode['s_' + "gather_tp"] = helper.add(gatherNode['s_' + "gather_tp"], sourceNode.gather_tp);
  77. },
  78. gatherSpecial: function (gatherNode, sourceNode, prefix, helper) {
  79. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  80. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  81. },
  82. };
  83. module.exports = app => {
  84. class RptGatherMemory extends app.BaseService {
  85. /**
  86. * 构造函数
  87. *
  88. * @param {Object} ctx - egg全局context
  89. * @return {void}
  90. */
  91. constructor(ctx) {
  92. super(ctx);
  93. this.resultTree = new Ledger.gatherTree(ctx, {
  94. id: 'id',
  95. pid: 'pid',
  96. order: 'order',
  97. level: 'level',
  98. rootId: -1
  99. });
  100. }
  101. _checkSpecialTender(tender, special) {
  102. if (special) {
  103. for (const spec of special) {
  104. if (tender[spec.key] === true) return spec.key;
  105. }
  106. }
  107. return '';
  108. }
  109. async _gatherStageData(index, tender, stage, hasPre) {
  110. const helper = this.ctx.helper;
  111. const billsTree = new Ledger.billsTree(this.ctx, {
  112. id: 'ledger_id',
  113. pid: 'ledger_pid',
  114. order: 'order',
  115. level: 'level',
  116. rootId: -1,
  117. keys: ['id', 'tender_id', 'ledger_id'],
  118. stageId: 'id',
  119. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'],
  120. calc: function (node) {
  121. if (node.children && node.children.length === 0) {
  122. node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty);
  123. node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
  124. node.end_contract_qty = helper.add(node.pre_contract_qty, node.contract_qty);
  125. node.end_qc_qty = helper.add(node.pre_qc_qty, node.qc_qty);
  126. node.end_gather_qty = helper.add(node.pre_gather_qty, node.gather_qty);
  127. }
  128. node.pre_gather_tp = helper.add(node.pre_contract_tp, node.pre_qc_tp);
  129. node.gather_tp = helper.add(node.contract_tp, node.qc_tp);
  130. node.end_contract_tp = helper.add(node.pre_contract_tp, node.contract_tp);
  131. node.end_qc_tp = helper.add(node.pre_qc_tp, node.qc_tp);
  132. node.end_gather_tp = helper.add(node.pre_gather_tp, node.gather_tp);
  133. }
  134. });
  135. const billsData = await this.ctx.service.ledger.getData(tender.id);
  136. if (stage) {
  137. await this.ctx.service.stage.doCheckStage(stage);
  138. if (stage.readOnly) {
  139. const curStage = await this.ctx.service.stageBills.getAuditorStageData(tender.id,
  140. stage.id, stage.curTimes, stage.curOrder);
  141. this.ctx.helper.assignRelaData(billsData, [
  142. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  143. ]);
  144. } else {
  145. const curStage = await this.ctx.service.stageBills.getLastestStageData(tender.id, stage.id);
  146. this.ctx.helper.assignRelaData(billsData, [
  147. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  148. ]);
  149. }
  150. if (hasPre) {
  151. const preStage = stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.order - 1) : [];
  152. this.ctx.helper.assignRelaData(billsData, [
  153. {data: preStage, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'lid'}
  154. ]);
  155. }
  156. }
  157. billsTree.loadDatas(billsData);
  158. billsTree.calculateAll();
  159. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  160. gatherUtils.gatherStage(tender, gatherNode, sourceNode, 't_' + index + '_', helper);
  161. });
  162. }
  163. async _gatherMonthData(sTender, index, month, hasPre) {
  164. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  165. const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
  166. await this._gatherStageData(index, tender, stage, hasPre);
  167. }
  168. async _gatherZoneData(sTender, index, zone) {
  169. const helper = this.ctx.helper;
  170. /**
  171. * 汇总并合并 相关数据
  172. * @param {Array} index - 主数据
  173. * @param {Array[]}rela - 相关数据 {data, fields, prefix, relaId}
  174. */
  175. const sumAssignRelaData = function (index, rela) {
  176. const loadFields = function (datas, fields, prefix, relaId) {
  177. for (const d of datas) {
  178. const key = indexPre + d[relaId];
  179. const m = index[key];
  180. if (m) {
  181. for (const f of fields) {
  182. if (d[f] !== undefined) {
  183. m[prefix + f] = helper.add(m[prefix + f], d[f]);
  184. }
  185. }
  186. }
  187. }
  188. };
  189. for (const r of rela) {
  190. loadFields(r.data, r.fields, r.prefix, r.relaId);
  191. }
  192. };
  193. const billsTree = new Ledger.billsTree(this.ctx, {
  194. id: 'ledger_id',
  195. pid: 'ledger_pid',
  196. order: 'order',
  197. level: 'level',
  198. rootId: -1,
  199. keys: ['id', 'tender_id', 'ledger_id'],
  200. stageId: 'id',
  201. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp'],
  202. calc: function (node) {
  203. if (node.children && node.children.length === 0) {
  204. node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
  205. }
  206. node.gather_tp = helper.add(node.contract_tp, node.qc_tp);
  207. }
  208. });
  209. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  210. const billsData = await this.ctx.service.ledger.getData(tender.id);
  211. let billsIndexData = {};
  212. for (const bd of billsData) {
  213. billsIndexData[indexPre + bd.id] = bd;
  214. }
  215. const times = zone.split(' - ');
  216. if (times.length !== 2) throw '选择的汇总周期无效';
  217. const beginTime = moment(times[0], 'YYYY-MM').date();
  218. const endTime = moment(times[1], 'YYYY-MM').date();
  219. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tender.id } });
  220. for (const stage of stages) {
  221. const sTime = moment(stage.s_time, 'YYYY-MM').date();
  222. if (sTime >= beginTime && sTime <= endTime) {
  223. await this.ctx.service.stage.doCheckStage(stage);
  224. if (stage.readOnly) {
  225. const curStage = await this.ctx.service.stageBills.getAuditorStageData(tender.id,
  226. stage.id, stage.curTimes, stage.curOrder);
  227. sumAssignRelaData(billsIndexData, [
  228. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  229. ]);
  230. } else {
  231. const curStage = await this.ctx.service.stageBills.getLastestStageData(tender.id, stage.id);
  232. sumAssignRelaData(billsIndexData, [
  233. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
  234. ]);
  235. }
  236. }
  237. }
  238. billsTree.loadDatas(billsData);
  239. billsTree.calculateAll();
  240. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  241. gatherUtils.gatherZone(gatherNode, sourceNode, 't_' + index + '_', helper);
  242. });
  243. }
  244. async _gatherFinalData(sTender, index, hasPre) {
  245. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  246. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  247. where: { tid: tender.id },
  248. orders: [['order', 'desc']],
  249. });
  250. if (stages.length !== 0) {
  251. const lastStage = stages[0];
  252. if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
  253. stages.splice(0, 1);
  254. }
  255. }
  256. await this._gatherStageData(index, tender, stages[0], hasPre);
  257. }
  258. async _gatherCheckedFinalData(sTender, index, hasPre) {
  259. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  260. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  261. where: { tid: tender.id },
  262. orders: [['order', 'desc']],
  263. });
  264. if (stages.length !== 0) {
  265. const lastStage = stages[0];
  266. if (lastStage.status !== auditConst.stage.status.checked) {
  267. stages.splice(0, 1);
  268. }
  269. }
  270. await this._gatherStageData(index, tender, stages[0], hasPre);
  271. }
  272. async _gatherSpecialData(sTender, sKey) {
  273. const helper = this.ctx.helper;
  274. const billsTree = new Ledger.billsTree(this.ctx, {
  275. id: 'ledger_id',
  276. pid: 'ledger_pid',
  277. order: 'order',
  278. level: 'level',
  279. rootId: -1,
  280. keys: ['id', 'tender_id', 'ledger_id'],
  281. stageId: 'id',
  282. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'],
  283. });
  284. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  285. const billsData = await this.ctx.service.ledger.getData(tender.id);
  286. billsTree.loadDatas(billsData);
  287. billsTree.calculateAll();
  288. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  289. gatherUtils.gatherSpecial(gatherNode, sourceNode, 'ts_' + sKey + '_', helper);
  290. })
  291. }
  292. async getGatherStageBills(memFieldKeys, gsDefine, gsCustom) {
  293. if (!gsDefine || !gsDefine.enable) return [];
  294. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  295. const gsSetting = JSON.parse(gsDefine.setting);
  296. let commonIndex = 0;
  297. for (const tender of gsCustom.tenders) {
  298. const specialKey = this._checkSpecialTender(tender, gsSetting.special);
  299. if (specialKey === '') {
  300. switch (gsSetting.type) {
  301. case 'month':
  302. await this._gatherMonthData(tender, commonIndex, gsCustom.month, gsSetting.hasPre);
  303. break;
  304. case 'zone':
  305. await this._gatherZoneData(tender, commonIndex, gsCustom.zone);
  306. break;
  307. case 'final':
  308. await this._gatherFinalData(tender, commonIndex, gsSetting.hasPre);
  309. break;
  310. case 'checked-final':
  311. await this._gatherCheckedFinalData(tender, commonIndex, gsSetting.hasPre);
  312. break;
  313. }
  314. commonIndex++;
  315. } else {
  316. await this._gatherSpecialData(tender, specialKey);
  317. }
  318. }
  319. this.resultTree.generateSortNodes();
  320. return this.resultTree.getDefaultDatas();
  321. }
  322. }
  323. return RptGatherMemory;
  324. };