stage_bills.js 28 KB

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