rpt_gather_memory.js 38 KB

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