rpt_gather_memory.js 39 KB

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