rpt_gather_memory.js 37 KB

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