rpt_gather_memory.js 38 KB

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