rpt_gather_memory.js 38 KB

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