stage_bills.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. 'use strict';
  2. /**
  3. * 期计量 - 部位明细计量
  4. *
  5. * @author Mai
  6. * @date 2018/12/8
  7. * @version
  8. */
  9. const calcFields = ['contract_qty', 'qc_qty'];
  10. const auditConst = require('../const/audit');
  11. const timesLen = auditConst.stage.timesLen;
  12. const SumLoad = require('../lib/sum_load');
  13. module.exports = app => {
  14. class StageBills extends app.BaseService {
  15. /**
  16. * 构造函数
  17. *
  18. * @param {Object} ctx - egg全局变量
  19. * @return {void}
  20. */
  21. constructor(ctx) {
  22. super(ctx);
  23. this.depart = 10;
  24. this.tableName = 'stage_bills';
  25. }
  26. /**
  27. * 查询期计量最后审核人数据
  28. * @param {Number} tid - 标段id
  29. * @param {Number} sid - 期id
  30. * @param {Number|Array} lid - 台账节点id(可以为空)
  31. * @return {Promise<*>}
  32. */
  33. async getLastestStageData(tid, sid, lid) {
  34. let lidSql = '',
  35. result;
  36. if (lid) {
  37. if (lid instanceof Array) {
  38. lidSql = lid.length > 0 ? ' And lid in (' + this.ctx.helper.getInArrStrSqlFilter(lid) + ')' : '';
  39. } else {
  40. lidSql = ' And lid in (' + this.db.escape(lid) + ')';
  41. }
  42. }
  43. const sql = 'SELECT Bills.* FROM ' + this.departTableName(tid) + ' As Bills ' +
  44. ' INNER JOIN ( ' +
  45. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.departTableName(tid) +
  46. ' WHERE tid = ? And sid = ?' + lidSql +
  47. ' GROUP BY `lid`' +
  48. ' ) As MaxFilter ' +
  49. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`';
  50. const sqlParam = [tid, sid];
  51. if (!lid) {
  52. return await this.db.query(sql, sqlParam);
  53. } else if (lid instanceof Array) {
  54. return await this.db.query(sql, sqlParam);
  55. }
  56. return await this.db.queryOne(sql, sqlParam);
  57. }
  58. /**
  59. * 查询 某期 某轮审批 某人数据
  60. * @param {Number} tid - 标段id
  61. * @param {Number} sid - 期id
  62. * @param {Number} times - 第几轮
  63. * @param {Number} order - 流程
  64. * @param {Number|Array} lid - 台账节点id(可以为空)
  65. * @return {Promise<*>}
  66. */
  67. async getAuditorStageData(tid, sid, times, order, lid) {
  68. const lidSql = lid ? ' And Bills.lid in (?)' : '';
  69. const sql = 'SELECT Bills.* FROM ' + this.departTableName(tid) + ' As Bills ' +
  70. ' INNER JOIN ( ' +
  71. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `tid`, `sid` From ' + this.departTableName(tid) +
  72. ' WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) And tid = ? And sid = ?' + lidSql +
  73. ' GROUP BY `lid`' +
  74. ' ) As MaxFilter ' +
  75. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid' +
  76. ' AND Bills.sid = MaxFilter.sid';
  77. const sqlParam = [times, times, order, tid, sid];
  78. if (!lid) {
  79. return await this.db.query(sql, sqlParam);
  80. } else if (lid instanceof Array) {
  81. sqlParam.push(lid.join(', '));
  82. return await this.db.query(sql, sqlParam);
  83. }
  84. sqlParam.push(lid);
  85. return await this.db.queryOne(sql, sqlParam);
  86. }
  87. _getFilterSql(where, asTable = '') {
  88. let whereSql = '';
  89. if (!where) return whereSql;
  90. if (where.lid) {
  91. if (where.lid instanceof Array) {
  92. whereSql += ' And ' + asTable + 'lid in (' + this.ctx.helper.getInArrStrSqlFilter(where.lid) + ')';
  93. } else if (typeof where.lid === "string") {
  94. whereSql += ' And ' + asTable + 'lid = ' + this.db.escape(where.lid);
  95. }
  96. }
  97. return whereSql;
  98. }
  99. async getLastestStageData2(tid, sid, lid) {
  100. const filterSql = lid ? this._getFilterSql({lid}) : '';
  101. const sql = 'Select * From ' + this.departTableName(tid) +
  102. ' Where tid = ? and sid = ? ' + filterSql;
  103. const result = await this.db.query(sql, [tid, sid]);
  104. const stageBills = this.ctx.helper.filterLastestData(result, ['lid']);
  105. if (!lid || lid instanceof Array) return stageBills;
  106. return stageBills[0];
  107. }
  108. async getAuditorStageData2(tid, sid, times, order, lid) {
  109. const filterSql = lid ? this._getFilterSql({lid}) : '';
  110. const sql = 'Select * From ' + this.departTableName(tid) +
  111. ' Where tid = ? and sid = ? And (`times` < ? OR (`times` = ? AND `order` <= ?)) ' + filterSql;
  112. const result = await this.db.query(sql, [tid, sid, times, times, order]);
  113. const stageBills = this.ctx.helper.filterLastestData(result, ['lid']);
  114. if (!lid || lid instanceof Array) return stageBills;
  115. return stageBills[0];
  116. }
  117. async getStageUsedBills(tid, sid) {
  118. const sql = 'SELECT Bills.lid, ((not IsNull(Bills.contract_qty) and Bills.contract_qty <> 0) or (not IsNull(Bills.qc_qty) and Bills.qc_qty <> 0)) As used' +
  119. ' FROM ' + this.tableName + ' As Bills ' +
  120. ' INNER JOIN ( ' +
  121. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  122. ' WHERE tid = ? And sid = ?' +
  123. ' GROUP BY `lid`' +
  124. ' ) As MaxFilter ' +
  125. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`';
  126. const sqlParam = [tid, sid];
  127. const stageBills = await this.db.query(sql, sqlParam);
  128. return this._.map(this._.filter(stageBills, 'used'), 'lid');
  129. }
  130. /**
  131. * 获取截止本期数据
  132. * @param {Number} tid - 标段id
  133. * @param {Number} sorder - 截止期序号
  134. * @param {String|Array[String]} lid - 台账id
  135. * @returns {Promise<*>}
  136. */
  137. async getEndStageData(tid, sorder, lid) {
  138. let lidSql = '';
  139. if (lid) {
  140. if (lid instanceof Array) {
  141. lidSql = lid.length > 0 ? ' And lid in (' + this.ctx.helper.getInArrStrSqlFilter(lid) + ')' : '';
  142. } else {
  143. lidSql = ' And lid in (' + this.db.escape(lid) + ')';
  144. }
  145. }
  146. const sql = 'SELECT Bills.tid, Bills.lid,' +
  147. ' Sum(Bills.contract_qty) As contract_qty, Sum(Bills.contract_tp) As contract_tp,' +
  148. ' Sum(Bills.qc_qty) As qc_qty, Sum(Bills.qc_tp) As qc_tp FROM ' + this.tableName + ' As Bills ' +
  149. ' INNER JOIN ( ' +
  150. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  151. ' WHERE tid = ? ' + lidSql +
  152. ' GROUP BY `lid`, `sid`' +
  153. ' ) As MaxFilter ' +
  154. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`' +
  155. ' INNER JOIN ' + this.ctx.service.stage.tableName + ' As Stage' +
  156. ' ON Bills.sid = Stage.id' +
  157. ' WHERE Stage.order <= ?' +
  158. ' GROUP BY `lid`';
  159. const sqlParam = [tid, sorder];
  160. if (!lid) {
  161. return await this.db.query(sql, sqlParam);
  162. } else if (lid instanceof Array) {
  163. return await this.db.query(sql, sqlParam);
  164. }
  165. return await this.db.queryOne(sql, sqlParam);
  166. }
  167. async getStageBills(tid, sid, lid) {
  168. const sql = 'SELECT Stage.*, Ledger.unit_price FROM ?? As Stage, ?? As Ledger ' +
  169. ' Where Stage.tid = ?, Stage.sid = ?, Stage.lid = ?, Stage.lid = Ledger.id ' +
  170. ' Order Stage.time DESC, Stage.order DESC ';
  171. const sqlParam = [this.tableName, this.ctx.service.ledger.tableName];
  172. sqlParam.push(this.db.escape(tid));
  173. sqlParam.push(this.db.escape(sid));
  174. sqlParam.push(this.db.escape(lid));
  175. return await this.db.queryOne(sql, sqlParam);
  176. }
  177. _calcStageBillsData(data, orgData, ledgerData) {
  178. const info = this.ctx.tender.info;
  179. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
  180. if (data.contract_qty !== undefined) {
  181. data.contract_qty = this.round(data.contract_qty, precision.value);
  182. data.contract_tp = this.ctx.helper.mul(data.contract_qty, ledgerData.unit_price, info.decimal.tp);
  183. }
  184. if (data.qc_qty !== undefined) {
  185. data.qc_qty = this.round(data.qc_qty, precision.value);
  186. data.qc_tp = this.ctx.helper.mul(data.qc_qty, ledgerData.unit_price, info.decimal.tp);
  187. }
  188. if (ledgerData.is_tp && data.contract_tp !== undefined) {
  189. data.contract_tp = this.ctx.helper.round(data.contract_tp, info.decimal.tp);
  190. }
  191. }
  192. async _insertStageBillsData(transaction, insertData, orgData, ledgerData) {
  193. const info = this.ctx.tender.info;
  194. const d = {
  195. tid: this.ctx.tender.id,
  196. lid: ledgerData.id,
  197. sid: this.ctx.stage.id,
  198. times: this.ctx.stage.curTimes,
  199. order: this.ctx.stage.curOrder,
  200. said: this.ctx.session.sessionUser.accountId,
  201. };
  202. if (orgData) {
  203. d.contract_qty = orgData.contract_qty;
  204. d.contract_tp = orgData.contract_tp;
  205. d.qc_qty = orgData.qc_qty;
  206. d.qc_tp = orgData.qc_tp;
  207. d.postil = orgData.postil;
  208. }
  209. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
  210. if (insertData.contract_qty !== undefined) {
  211. d.contract_qty = this.round(insertData.contract_qty, precision.value);
  212. d.contract_tp = this.ctx.helper.mul(d.contract_qty, ledgerData.unit_price, info.decimal.tp);
  213. }
  214. if (insertData.contract_expr !== undefined) d.contract_expr = insertData.contract_expr;
  215. if (insertData.qc_qty !== undefined) {
  216. d.qc_qty = this.round(insertData.qc_qty, precision.value);
  217. d.qc_tp = this.ctx.helper.mul(d.qc_qty, ledgerData.unit_price, info.decimal.tp);
  218. }
  219. if (insertData.postil) {
  220. d.postil = insertData.postil;
  221. }
  222. if (ledgerData.is_tp && insertData.contract_tp !== undefined) {
  223. d.contract_tp = this.ctx.helper.round(insertData.contract_tp, info.decimal.tp);
  224. }
  225. await transaction.insert(this.tableName, d);
  226. }
  227. /**
  228. * 前端提交数据
  229. * @param {Object|Array} data - 提交的数据
  230. * @return {Promise<void>}
  231. */
  232. async updateStageData(data) {
  233. const datas = data instanceof Array ? data : [data];
  234. const transaction = await this.db.beginTransaction();
  235. try {
  236. for (const d of datas) {
  237. const stageBills = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, d.lid);
  238. const ledgerBills = await this.ctx.service.ledger.getDataById(d.lid);
  239. if (!stageBills || stageBills.times !== this.ctx.stage.curTimes || stageBills.order !== this.ctx.stage.curOrder) {
  240. await this._insertStageBillsData(transaction, d, stageBills, ledgerBills);
  241. } else {
  242. d.id = stageBills.id;
  243. this._calcStageBillsData(d, stageBills, ledgerBills);
  244. await transaction.update(this.tableName, d);
  245. }
  246. }
  247. await transaction.commit();
  248. } catch (err) {
  249. await transaction.rollback();
  250. throw err;
  251. }
  252. return await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, this._.map(datas, 'lid'));
  253. }
  254. /**
  255. * 根据
  256. * @param transaction
  257. * @param ledgerBills
  258. * @param stageBills
  259. * @param data
  260. * @return {Promise<void>}
  261. * @private
  262. */
  263. async updateStageBillsQty(transaction, ledgerBills, stageBills, data) {
  264. if (stageBills) {
  265. if ((data.contract_qty === undefined || stageBills.contract_qty !== data.contract_qty) ||
  266. (data.qc_qty === undefined || stageBills.qc_qty !== data.qc_qty)) {
  267. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  268. data.id = stageBills.id;
  269. this._calcStageBillsData(data, stageBills, ledgerBills);
  270. await transaction.update(this.tableName, data);
  271. } else {
  272. await this._insertStageBillsData(transaction, data, stageBills, ledgerBills);
  273. }
  274. }
  275. } else {
  276. await this._insertStageBillsData(transaction, data, stageBills, ledgerBills);
  277. }
  278. }
  279. /**
  280. * 重算 本期计量 数量 (根据部位明细)
  281. * @param {Number} tid - 标段id
  282. * @param {Number} id - 需要计算的节点的id
  283. * @param {Number} lid - 台账id
  284. * @param {Object} transaction - 操作所属事务
  285. * @return {Promise<void>}
  286. */
  287. async calc(tid, sid, lid, transaction) {
  288. const info = this.ctx.tender.info;
  289. const stageBills = await this.getLastestStageData(tid, sid, lid);
  290. const ledgerBills = await this.ctx.service.ledger.getDataById(lid);
  291. if (!ledgerBills) {
  292. throw '提交数据错误';
  293. }
  294. const posGather = await this.ctx.service.stagePos.getPosGather(tid, sid, lid, transaction);
  295. if (!posGather) { return; }
  296. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  297. // 计算
  298. if (posGather.contract_qty !== undefined) {
  299. posGather.contract_qty = this.round(posGather.contract_qty, precision.value);
  300. posGather.contract_tp = this.ctx.helper.mul(posGather.contract_qty, ledgerBills.unit_price, info.decimal.tp);
  301. }
  302. if (posGather.qc_qty !== undefined) {
  303. posGather.qc_qty = this.round(posGather.qc_qty, precision.value);
  304. posGather.qc_tp = this.ctx.helper.mul(posGather.qc_qty, ledgerBills.unit_price, info.decimal.tp);
  305. }
  306. if (stageBills) {
  307. if (stageBills.contract_qty === posGather.contract_qty && stageBills.qc_qty === posGather.qc_qty) {
  308. return;
  309. }
  310. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  311. posGather.id = stageBills.id;
  312. await transaction.update(this.tableName, posGather);
  313. } else {
  314. await this._insertStageBillsData(transaction, posGather, stageBills, ledgerBills);
  315. }
  316. } else {
  317. await this._insertStageBillsData(transaction, posGather, stageBills, ledgerBills);
  318. }
  319. }
  320. async updateStageBillsCalcType(data) {
  321. const stageBills = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, data.id);
  322. const updateData = { contract_qty: null, contract_tp: null };
  323. const transaction = await this.db.beginTransaction();
  324. try {
  325. await transaction.update(this.ctx.service.ledger.tableName, data);
  326. if (stageBills) {
  327. if (stageBills.times !== this.ctx.stage.curTimes || stageBills.order !== this.ctx.stage.curOrder) {
  328. const ledgerBills = await this.ctx.service.ledger.getDataById(data.id);
  329. await this._insertStageBillsData(transaction, updateData, stageBills, ledgerBills);
  330. } else {
  331. updateData.id = stageBills.id;
  332. await transaction.update(this.tableName, updateData);
  333. }
  334. }
  335. await transaction.commit();
  336. } catch (err) {
  337. await transaction.rollback();
  338. throw err;
  339. }
  340. const bills = await this.ctx.service.ledger.getDataById(data.id);
  341. const curStageData = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [data.id]);
  342. return { bills: [bills], curStageData };
  343. }
  344. async getSumTotalPrice(stage) {
  345. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp` FROM ' + this.departTableName(stage.tid) + ' As Bills ' +
  346. ' INNER JOIN ( ' +
  347. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid`, `sid` From ' + this.departTableName(stage.tid) +
  348. ' WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) AND `sid` = ?' +
  349. ' GROUP BY `lid`' +
  350. ' ) As MaxFilter ' +
  351. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid And Bills.sid = MaxFilter.sid';
  352. const sqlParam = [stage.curTimes, stage.curTimes, stage.curOrder, stage.id];
  353. const result = await this.db.queryOne(sql, sqlParam);
  354. return result;
  355. }
  356. async getSumTotalPriceFilter(stage, operate, filter) {
  357. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp`' +
  358. ' FROM ' + this.departTableName(stage.tid) + ' As Bills ' +
  359. ' INNER JOIN ( ' +
  360. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` From ' + this.departTableName(stage.tid) +
  361. ' WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) AND `sid` = ?' +
  362. ' GROUP BY `lid`' +
  363. ' ) As MaxFilter ' +
  364. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid ' +
  365. ' INNER JOIN ' + this.ctx.service.ledger.departTableName(stage.tid) + ' As Ledger ON Bills.lid = Ledger.id' +
  366. ' WHERE Bills.sid = ? And Ledger.b_code ' + operate + ' ?';
  367. const sqlParam = [stage.times, stage.curTimes, stage.curOrder, stage.id, stage.id, filter];
  368. const result = await this.db.queryOne(sql, sqlParam);
  369. return result;
  370. }
  371. async getSumTotalPriceGcl(stage, regText) {
  372. if (regText) {
  373. return await this.getSumTotalPriceFilter(stage, 'REGEXP', regText);
  374. }
  375. return await this.getSumTotalPriceFilter(stage, '<>', this.db.escape(''));
  376. }
  377. async getSumTotalPriceNotGcl(stage) {
  378. return await this.getSumTotalPriceFilter(stage, '=', this.db.escape(''));
  379. }
  380. /**
  381. * 多期清单数据整合 (材料调差调用)
  382. * @param {Number} tid - 标段id
  383. * @param {String} stage_id_list - 期id列表
  384. * @return {Promise<void>}
  385. */
  386. async getStagesData(tid, stage_id_list) {
  387. const whereSql = this.ctx.helper.whereSql({tid: tid, sid: stage_id_list.split(',')});
  388. const sql = 'SELECT Bills.lid, Bills.tid, Bills.sid,' +
  389. ' Sum(Bills.contract_qty) As contract_qty, Sum(Bills.contract_tp) As contract_tp,' +
  390. ' Sum(Bills.qc_qty) As qc_qty, Sum(Bills.qc_tp) As qc_tp' +
  391. ' FROM ' + this.tableName + ' As Bills ' +
  392. ' INNER JOIN ( ' +
  393. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `tid`, `lid`, `sid` From ' + this.tableName + whereSql + ' GROUP BY `lid`, `sid`' +
  394. ' ) As MaxFilter ' +
  395. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.tid = MaxFilter.tid And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`' +
  396. ' GROUP BY Bills.lid';
  397. const result = await this.db.query(sql);
  398. return result;
  399. }
  400. /**
  401. * 获取多期(合同和数量变更相加)计量-小计(材料调差调用)
  402. * @param {Number} tid - 标段id
  403. * @param {String} stage_id_list - 期id列表
  404. * @param {String} lid - 台账id
  405. * @param {String} xid - 项目节id
  406. * @return {Promise<void>}
  407. */
  408. async getGatherQtyByMaterial(tid, stage_id_list, lid) {
  409. stage_id_list = stage_id_list !== null ? stage_id_list.split(',') : [];
  410. let gather_qty = 0;
  411. for (const sid of stage_id_list) {
  412. const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills ' +
  413. ' INNER JOIN ( ' +
  414. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  415. ' WHERE tid = ? And sid = ?' +
  416. ' GROUP BY `lid`' +
  417. ' ) As MaxFilter ' +
  418. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`' +
  419. ' WHERE Bills.lid = ?';
  420. const sqlParam = [tid, sid, lid];
  421. const result = await this.db.queryOne(sql, sqlParam);
  422. if (result) {
  423. gather_qty = this.ctx.helper.add(gather_qty, this.ctx.helper.add(result.contract_qty, result.qc_qty));
  424. }
  425. }
  426. return gather_qty !== 0 ? gather_qty : null;
  427. }
  428. async getSumTotalPriceByMaterial(stage_list) {
  429. let contract_tp = 0;
  430. let qc_tp = 0;
  431. for (const stage of stage_list) {
  432. const result = await this.getSumTotalPrice(stage);
  433. if (result) {
  434. contract_tp = this.ctx.helper.add(contract_tp, result.contract_tp);
  435. qc_tp = this.ctx.helper.add(qc_tp, result.qc_tp);
  436. }
  437. }
  438. return { contract_tp, qc_tp };
  439. }
  440. async getSumTotalPriceGclByMaterial(stage_list, regText) {
  441. let contract_tp = 0;
  442. let qc_tp = 0;
  443. for (const stage of stage_list) {
  444. const result = await this.getSumTotalPriceGcl(stage, regText);
  445. if (result) {
  446. contract_tp = this.ctx.helper.add(contract_tp, result.contract_tp);
  447. qc_tp = this.ctx.helper.add(qc_tp, result.qc_tp);
  448. }
  449. }
  450. return { contract_tp, qc_tp };
  451. }
  452. async sumLoad(lid, tenders) {
  453. const conn = await this.db.beginTransaction();
  454. try {
  455. const maxId = await this.ctx.service.ledger._getMaxLid(this.ctx.tender.id);
  456. const select = await this.ctx.service.ledger.getDataById(lid);
  457. const sumLoad = new SumLoad(this.ctx);
  458. const loadTree = await sumLoad.stageGatherGcl(select, maxId, tenders);
  459. const result = loadTree.getUpdateData();
  460. if (result.errors.length > 100) throw '您导入的数据存在大量数据错误,请您仔细检查';
  461. const stageBills = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id);
  462. const updateStageBills = [], insertStageBills = [];
  463. for (const u of result.update) {
  464. const sb = stageBills.find(x => { return x.lid === u.lid; });
  465. if (!sb || sb.times !== this.ctx.stage.curTimes || sb.order !== this.ctx.stage.curOrder) {
  466. u.qc_qty = sb ? sb.qc_qty : null;
  467. u.qc_tp = sb ? sb.qc_tp : null;
  468. u.postil = sb ? sb.postil : null;
  469. u.tid = this.ctx.tender.id;
  470. u.sid = this.ctx.stage.id;
  471. u.said = this.ctx.session.sessionUser.accountId;
  472. u.times = this.ctx.stage.curTimes;
  473. u.order = this.ctx.stage.curOrder;
  474. insertStageBills.push(u);
  475. } else {
  476. u.id = sb.id;
  477. updateStageBills.push(u);
  478. }
  479. }
  480. const his = await this.ctx.service.sumLoadHistory.saveStageHistory(this.ctx.tender.id, this.ctx.stage.id, lid, tenders, result.errors);
  481. if (updateStageBills.length > 0) await conn.updateRows(this.tableName, updateStageBills);
  482. if (insertStageBills.length > 0) await conn.insert(this.tableName, insertStageBills);
  483. await conn.commit();
  484. return { curStageData: result.update, sumLoadHis: his };
  485. } catch (err) {
  486. this.ctx.helper.log(err);
  487. await conn.rollback();
  488. throw (err.stack ? '导入工程量数据出错' : err);
  489. }
  490. }
  491. }
  492. return StageBills;
  493. };