stage_bills.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 = ctx.app.config.table_depart.heavy;
  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, newTimes, newOrder, 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. times: newTimes,
  134. order: newOrder,
  135. }, { where: { id: this._.map(oldSBLists, 'id') } });
  136. }
  137. }
  138. async getStageUsedBills(tid, sid) {
  139. 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' +
  140. ' FROM ' + this.tableName + ' As Bills ' +
  141. ' INNER JOIN ( ' +
  142. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  143. ' WHERE tid = ? And sid = ?' +
  144. ' GROUP BY `lid`' +
  145. ' ) As MaxFilter ' +
  146. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`';
  147. const sqlParam = [tid, sid];
  148. const stageBills = await this.db.query(sql, sqlParam);
  149. return this._.map(this._.filter(stageBills, 'used'), 'lid');
  150. }
  151. async getStageBills(tid, sid, lid) {
  152. const sql = 'SELECT Stage.*, Ledger.unit_price FROM ?? As Stage, ?? As Ledger ' +
  153. ' Where Stage.tid = ?, Stage.sid = ?, Stage.lid = ?, Stage.lid = Ledger.id ' +
  154. ' Order Stage.time DESC, Stage.order DESC ';
  155. const sqlParam = [this.tableName, this.ctx.service.ledger.tableName];
  156. sqlParam.push(this.db.escape(tid));
  157. sqlParam.push(this.db.escape(sid));
  158. sqlParam.push(this.db.escape(lid));
  159. return await this.db.queryOne(sql, sqlParam);
  160. }
  161. _calcContractTpByTp(bills, preStageBills, stageBills, decimal) {
  162. let activeQty = this.ctx.helper.add(bills.quantity, stageBills.qc_minus_qty);
  163. let end_contract_qty = stageBills.contract_qty;
  164. if (preStageBills) {
  165. activeQty = this.ctx.helper.add(activeQty, preStageBills.qc_minus_qty);
  166. end_contract_qty = this.ctx.helper.add(end_contract_qty, preStageBills.contract_qty);
  167. }
  168. const end_contract_tp = this.ctx.helper.mul(this.ctx.helper.div(end_contract_qty, activeQty), bills.total_price, decimal.tp);
  169. return preStageBills ? this.ctx.helper.sub(end_contract_tp, preStageBills.contract_tp) : end_contract_tp;
  170. }
  171. _calcContractTpByCorrect(bills,preStageBills, stageBills, decimal) {
  172. let activeQty = this.ctx.helper.add(bills.quantity, stageBills.qc_minus_qty);
  173. let end_contract_qty = stageBills.contract_qty;
  174. if (preStageBills) {
  175. activeQty = this.ctx.helper.add(activeQty, preStageBills.qc_minus_qty);
  176. end_contract_qty = this.ctx.helper.add(end_contract_qty, preStageBills.contract_qty);
  177. }
  178. const end_contract_tp = this.ctx.helper.mul(end_contract_qty, bills.unit_price, decimal.tp);
  179. return activeQty === end_contract_qty ? this.ctx.helper.sub(end_contract_tp, preStageBills.contract_tp) : this.ctx.helper.mul(stageBills.contract_qty, bills.unit_price, info.decimal.tp);
  180. }
  181. async calcContractTp(info, stage, bills, stageBills) {
  182. const correct = this.ctx.subProject.page_show.correctCalcContractTp;
  183. if (info.calc_type === 'tp') {
  184. const preStageBills = await this.ctx.service.stageBillsFinal.getDataByCondition({ tid: stage.tid, sorder: stage.order - 1, lid: bills.id });
  185. return this._calcContractTpByTp(bills, preStageBills, stageBills, info.decimal);
  186. } else if (info.calc_type === 'up') {
  187. if (correct) {
  188. const preStageBills = await this.ctx.service.stageBillsFinal.getDataByCondition({ tid: stage.tid, sorder: stage.order - 1, lid: bills.id });
  189. return this._calcContractTpByCorrect(bills, preStageBills, stageBills, info.decimal);
  190. } else {
  191. return this.ctx.helper.mul(stageBills.contract_qty, bills.unit_price, info.decimal.tp);
  192. }
  193. }
  194. }
  195. async _calcStageBillsData(data, orgData, ledgerData) {
  196. const info = this.ctx.tender.info;
  197. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
  198. if (data.qc_minus_qty !== undefined) {
  199. data.qc_minus_qty = data.qc_minus_qty ? this.round(data.qc_minus_qty, precision.value) : 0;
  200. } else if (data.contract_qty !== undefined) {
  201. data.qc_minus_qty = orgData.qc_minus_qty;
  202. }
  203. if (data.contract_qty !== undefined) {
  204. data.contract_qty = this.round(data.contract_qty, precision.value);
  205. data.contract_tp = await this.calcContractTp(info, this.ctx.stage, ledgerData, data);
  206. } else if (data.qc_minus_qty !== undefined) {
  207. data.contract_qty = orgData.contract_qty;
  208. data.contract_tp = await this.calcContractTp(info, this.ctx.stage, ledgerData, data);
  209. }
  210. if (data.qc_qty !== undefined) {
  211. data.qc_qty = this.round(data.qc_qty, precision.value);
  212. data.qc_tp = this.ctx.helper.mul(data.qc_qty, ledgerData.unit_price, info.decimal.tp);
  213. } else if (data.qc_tp !== undefined) {
  214. data.qc_tp = this.ctx.helper.round(data.qc_tp, info.decimal.tp);
  215. }
  216. if (data.positive_qc_qty !== undefined) {
  217. data.positive_qc_qty = this.round(data.positive_qc_qty, precision.value);
  218. data.positive_qc_tp = this.ctx.helper.mul(data.positive_qc_qty, ledgerData.unit_price, info.decimal.tp);
  219. }
  220. if (data.negative_qc_qty !== undefined) {
  221. data.negative_qc_qty = this.round(data.negative_qc_qty, precision.value);
  222. data.negative_qc_tp = this.ctx.helper.mul(data.negative_qc_qty, ledgerData.unit_price, info.decimal.tp);
  223. }
  224. if (data.ex_stage_qty1 !== undefined) {
  225. data.ex_stage_qty1 = this.round(data.ex_stage_qty1, precision.value);
  226. data.ex_stage_tp1 = this.ctx.helper.mul(data.ex_stage_qty1, ledgerData.unit_price, info.decimal.tp);
  227. }
  228. if (ledgerData.is_tp) {
  229. if (data.contract_tp !== undefined) data.contract_tp = this.ctx.helper.round(data.contract_tp, info.decimal.tp);
  230. if (data.ex_stage_tp1 !== undefined) data.ex_stage_tp1 = this.ctx.helper.round(data.ex_stage_tp1, info.decimal.tp);
  231. }
  232. }
  233. async _insertStageBillsData(transaction, insertData, orgData, ledgerData) {
  234. const info = this.ctx.tender.info;
  235. const d = {
  236. tid: this.ctx.tender.id,
  237. lid: ledgerData.id,
  238. sid: this.ctx.stage.id,
  239. times: this.ctx.stage.curTimes,
  240. order: this.ctx.stage.curOrder,
  241. said: this.ctx.session.sessionUser.accountId,
  242. };
  243. if (orgData) {
  244. d.contract_qty = orgData.contract_qty;
  245. d.contract_tp = orgData.contract_tp;
  246. d.qc_qty = orgData.qc_qty;
  247. d.qc_tp = orgData.qc_tp;
  248. d.qc_minus_qty = orgData.qc_minus_qty;
  249. d.positive_qc_qty = orgData.positive_qc_qty;
  250. d.negative_qc_qty = orgData.negative_qc_qty;
  251. d.postil = orgData.postil;
  252. d.ex_stage_qty1 = orgData.ex_stage_qty1;
  253. d.ex_stage_tp1 = orgData.ex_stage_tp1;
  254. }
  255. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
  256. if (insertData.qc_minus_qty !== undefined) {
  257. d.qc_minus_qty = insertData.qc_minus_qty ? this.round(insertData.qc_minus_qty, precision.value) : 0;
  258. }
  259. if (insertData.contract_qty !== undefined) {
  260. d.contract_qty = this.round(insertData.contract_qty, precision.value);
  261. d.contract_tp = await this.calcContractTp(info, this.ctx.stage, ledgerData, d);
  262. }
  263. if (insertData.contract_expr !== undefined) d.contract_expr = insertData.contract_expr;
  264. if (insertData.qc_qty !== undefined) {
  265. d.qc_qty = this.round(insertData.qc_qty, precision.value);
  266. d.qc_tp = this.ctx.helper.mul(d.qc_qty, ledgerData.unit_price, info.decimal.tp);
  267. } else if (insertData.qc_tp !== undefined) {
  268. d.qc_tp = this.ctx.helper.round(insertData.qc_tp, info.decimal.tp);
  269. }
  270. if (insertData.positive_qc_qty !== undefined) {
  271. d.positive_qc_qty = this.round(insertData.positive_qc_qty, precision.value);
  272. d.positive_qc_tp = this.ctx.helper.mul(d.positive_qc_qty, ledgerData.unit_price, info.decimal.tp);
  273. }
  274. if (insertData.negative_qc_qty !== undefined) {
  275. d.negative_qc_qty = this.round(insertData.negative_qc_qty, precision.value);
  276. d.negative_qc_tp = this.ctx.helper.mul(d.negative_qc_qty, ledgerData.unit_price, info.decimal.tp);
  277. }
  278. if (insertData.ex_stage_qty1 !== undefined) {
  279. d.ex_stage_qty1 = this.round(insertData.ex_stage_qty1, precision.value);
  280. d.ex_stage_tp1 = this.ctx.helper.mul(d.ex_stage_qty1, ledgerData.unit_price, info.decimal.tp);
  281. }
  282. if (insertData.postil) {
  283. d.postil = insertData.postil;
  284. }
  285. if (ledgerData.is_tp) {
  286. if (insertData.contract_tp !== undefined) d.contract_tp = this.ctx.helper.round(insertData.contract_tp, info.decimal.tp);
  287. if (insertData.ex_stage_tp1 !== undefined) d.ex_stage_tp1 = this.ctx.helper.round(insertData.ex_stage_tp1, info.decimal.tp);
  288. }
  289. await transaction.insert(this.tableName, d);
  290. }
  291. /**
  292. * 前端提交数据
  293. * @param {Object|Array} data - 提交的数据
  294. * @return {Promise<void>}
  295. */
  296. async updateStageData(data) {
  297. const datas = data instanceof Array ? data : [data];
  298. const transaction = await this.db.beginTransaction();
  299. try {
  300. for (const d of datas) {
  301. const stageBills = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, d.lid);
  302. const ledgerBills = await this.ctx.service.ledger.getCompleteDataById(d.lid);
  303. if (!stageBills || stageBills.times !== this.ctx.stage.curTimes || stageBills.order !== this.ctx.stage.curOrder) {
  304. await this._insertStageBillsData(transaction, d, stageBills, ledgerBills);
  305. } else {
  306. d.id = stageBills.id;
  307. d.said = this.ctx.session.sessionUser.accountId;
  308. await this._calcStageBillsData(d, stageBills, ledgerBills);
  309. await transaction.update(this.tableName, d);
  310. }
  311. }
  312. await transaction.commit();
  313. } catch (err) {
  314. await transaction.rollback();
  315. throw err;
  316. }
  317. return await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, this._.map(datas, 'lid'));
  318. }
  319. /**
  320. * 根据
  321. * @param transaction
  322. * @param ledgerBills
  323. * @param stageBills
  324. * @param data
  325. * @return {Promise<void>}
  326. * @private
  327. */
  328. async updateStageBillsQty(transaction, ledgerBills, stageBills, data) {
  329. if (stageBills) {
  330. if ((data.contract_qty === undefined || stageBills.contract_qty !== data.contract_qty) ||
  331. (data.qc_qty === undefined || stageBills.qc_qty !== data.qc_qty) ||
  332. (data.qc_minus_qty === undefined || stageBills.qc_minus_qty !== data.qc_minus_qty) ||
  333. (data.ex_stage_qty1 === undefined || stageBills.ex_stage_qty1 !== data.ex_stage_qty1)
  334. ) {
  335. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  336. data.id = stageBills.id;
  337. data.said = this.ctx.session.sessionUser.accountId;
  338. await this._calcStageBillsData(data, stageBills, ledgerBills);
  339. await transaction.update(this.tableName, data);
  340. } else {
  341. await this._insertStageBillsData(transaction, data, stageBills, ledgerBills);
  342. }
  343. }
  344. } else {
  345. await this._insertStageBillsData(transaction, data, stageBills, ledgerBills);
  346. }
  347. }
  348. /**
  349. * 重算 本期计量 数量 (根据部位明细)
  350. * @param {Number} tid - 标段id
  351. * @param {Number} id - 需要计算的节点的id
  352. * @param {Number} lid - 台账id
  353. * @param {Object} transaction - 操作所属事务
  354. * @return {Promise<void>}
  355. */
  356. async calc(tid, stage, lid, transaction) {
  357. const info = this.ctx.tender.info;
  358. const stageBills = await this.getLastestStageData2(tid, stage.id, lid);
  359. const ledgerBills = await this.ctx.service.ledger.getCompleteDataById(lid);
  360. if (!ledgerBills) {
  361. throw '提交数据错误';
  362. }
  363. const posGather = await this.ctx.service.stagePos.getPosGather(tid, stage.id, lid, transaction);
  364. if (!posGather) { return; }
  365. if (posGather.qc_minus_qty !== undefined) {
  366. posGather.qc_minus_qty = posGather.qc_minus_qty || 0;
  367. } else if (posGather.contract_qty !== undefined) {
  368. posGather.qc_minus_qty = stageBills.qc_minus_qty;
  369. }
  370. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  371. // 计算
  372. if (posGather.contract_qty !== undefined) {
  373. posGather.contract_qty = this.round(posGather.contract_qty, precision.value);
  374. posGather.contract_tp = await this.calcContractTp(info, stage, ledgerBills, posGather);
  375. } else if (posGather.qc_minus_qty) {
  376. posGather.contract_qty = stageBills.contract_qty;
  377. posGather.contract_tp = await this.calcContractTp(info, stage, ledgerBills, posGather);
  378. }
  379. if (posGather.qc_qty !== undefined) {
  380. posGather.qc_qty = this.round(posGather.qc_qty, precision.value);
  381. posGather.qc_tp = this.ctx.helper.mul(posGather.qc_qty, ledgerBills.unit_price, info.decimal.tp);
  382. }
  383. if (posGather.positive_qc_qty !== undefined) {
  384. posGather.positive_qc_qty = this.round(posGather.positive_qc_qty, precision.value);
  385. posGather.positive_qc_tp = this.ctx.helper.mul(posGather.positive_qc_qty, ledgerBills.unit_price, info.decimal.tp);
  386. }
  387. if (posGather.negative_qc_qty !== undefined) {
  388. posGather.negative_qc_qty = this.round(posGather.negative_qc_qty, precision.value);
  389. posGather.negative_qc_tp = this.ctx.helper.mul(posGather.negative_qc_qty, ledgerBills.unit_price, info.decimal.tp);
  390. }
  391. if (posGather.ex_stage_qty1 !== undefined) {
  392. posGather.ex_stage_qty1 = this.round(posGather.ex_stage_qty1, precision.value);
  393. posGather.ex_stage_tp1 = this.ctx.helper.mul(posGather.ex_stage_qty1, ledgerBills.unit_price, info.decimal.tp);
  394. }
  395. if (stageBills) {
  396. if (stageBills.contract_qty === posGather.contract_qty && stageBills.qc_qty === posGather.qc_qty && stageBills.qc_minus_qty === posGather.qc_minus_qty && stageBills.ex_stage_qty1 === posGather.ex_stage_qty1) {
  397. return;
  398. }
  399. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  400. posGather.id = stageBills.id;
  401. await transaction.update(this.tableName, posGather);
  402. } else {
  403. await this._insertStageBillsData(transaction, posGather, stageBills, ledgerBills);
  404. }
  405. } else {
  406. await this._insertStageBillsData(transaction, posGather, stageBills, ledgerBills);
  407. }
  408. }
  409. async updateStageBillsCalcType(data) {
  410. const stageBills = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, data.id);
  411. const le = await this.ctx.service.ledgerExtra.getDataById(data.id);
  412. const updateData = { contract_qty: 0, contract_tp: 0, ex_stage_qty1: 0, ex_stage_tp1: 0 };
  413. const transaction = await this.db.beginTransaction();
  414. try {
  415. if (le) {
  416. await transaction.update(this.ctx.service.ledgerExtra.tableName, data);
  417. } else {
  418. data.tid = this.ctx.tender.id;
  419. await transaction.insert(this.ctx.service.ledgerExtra.tableName, data);
  420. }
  421. if (stageBills) {
  422. if (stageBills.times !== this.ctx.stage.curTimes || stageBills.order !== this.ctx.stage.curOrder) {
  423. const ledgerBills = await this.ctx.service.ledger.getCompleteDataById(data.id);
  424. await this._insertStageBillsData(transaction, updateData, stageBills, ledgerBills);
  425. } else {
  426. updateData.id = stageBills.id;
  427. updateData.said = this.ctx.session.sessionUser.accountId;
  428. await transaction.update(this.tableName, updateData);
  429. }
  430. }
  431. await transaction.commit();
  432. } catch (err) {
  433. await transaction.rollback();
  434. throw err;
  435. }
  436. const bills = await this.ctx.service.ledger.getCompleteDataById(data.id);
  437. const curStageData = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, [data.id]);
  438. return { bills: [bills], curStageData };
  439. }
  440. async getSumTotalPrice(stage) {
  441. 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 ' +
  442. ' INNER JOIN ( ' +
  443. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid`, `sid` From ' + this.departTableName(stage.tid) +
  444. ' WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) AND `sid` = ?' +
  445. ' GROUP BY `lid`' +
  446. ' ) As MaxFilter ' +
  447. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid And Bills.sid = MaxFilter.sid';
  448. const sqlParam = [stage.curTimes, stage.curTimes, stage.curOrder, stage.id];
  449. const result = await this.db.queryOne(sql, sqlParam);
  450. return result;
  451. }
  452. async getLatestSumTotalPrice(tid, sid) {
  453. 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(tid) + ' As Bills ' +
  454. ' INNER JOIN ( ' +
  455. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid`, `sid` From ' + this.departTableName(tid) +
  456. ' WHERE `sid` = ?' +
  457. ' GROUP BY `lid`' +
  458. ' ) As MaxFilter ' +
  459. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid And Bills.sid = MaxFilter.sid';
  460. const sqlParam = [sid];
  461. const result = await this.db.queryOne(sql, sqlParam);
  462. return result;
  463. }
  464. async getSumTotalPriceFilter(stage, operate, filter) {
  465. 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' +
  466. ' FROM ' + this.departTableName(stage.tid) + ' As Bills ' +
  467. ' INNER JOIN ( ' +
  468. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` From ' + this.departTableName(stage.tid) +
  469. ' WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) AND `sid` = ?' +
  470. ' GROUP BY `lid`' +
  471. ' ) As MaxFilter ' +
  472. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid ' +
  473. ' INNER JOIN ' + this.ctx.service.ledger.departTableName(stage.tid) + ' As Ledger ON Bills.lid = Ledger.id' +
  474. ' WHERE Bills.sid = ? And Ledger.b_code ' + operate + ' ?';
  475. const sqlParam = [stage.times, stage.curTimes, stage.curOrder, stage.id, stage.id, filter];
  476. const result = await this.db.queryOne(sql, sqlParam);
  477. return result;
  478. }
  479. async getSumTotalPriceGcl(stage, regText) {
  480. if (regText) {
  481. return await this.getSumTotalPriceFilter(stage, 'REGEXP', regText);
  482. }
  483. return await this.getSumTotalPriceFilter(stage, '<>', this.db.escape(''));
  484. }
  485. async getSumTotalPriceNotGcl(stage) {
  486. return await this.getSumTotalPriceFilter(stage, '=', this.db.escape(''));
  487. }
  488. /**
  489. * 多期清单数据整合 (材料调差调用)
  490. * @param {Number} tid - 标段id
  491. * @param {String} stage_id_list - 期id列表
  492. * @return {Promise<void>}
  493. */
  494. async getStagesData(tid, stage_id_list) {
  495. const whereSql = this.ctx.helper.whereSql({ tid, sid: stage_id_list.split(',') });
  496. const sql = 'SELECT Bills.lid, Bills.tid, Bills.sid,' +
  497. ' Sum(Bills.contract_qty) As contract_qty, Sum(Bills.contract_tp) As contract_tp,' +
  498. ' Sum(Bills.qc_minus_qty) As qc_minus_qty,' +
  499. ' Sum(Bills.qc_qty) As qc_qty, Sum(Bills.qc_tp) As qc_tp' +
  500. ' FROM ' + this.tableName + ' As Bills ' +
  501. ' INNER JOIN ( ' +
  502. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `tid`, `lid`, `sid` From ' + this.tableName + whereSql + ' GROUP BY `lid`, `sid`' +
  503. ' ) As MaxFilter ' +
  504. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.tid = MaxFilter.tid And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`' +
  505. ' GROUP BY Bills.lid';
  506. const result = await this.db.query(sql);
  507. return result;
  508. }
  509. /**
  510. * 获取多期(合同和数量变更相加)计量-小计(材料调差调用)
  511. * @param {Number} tid - 标段id
  512. * @param {String} stage_id_list - 期id列表
  513. * @param {String} lid - 台账id
  514. * @param {String} xid - 项目节id
  515. * @return {Promise<void>}
  516. */
  517. async getGatherQtyByMaterial(tid, stage_id_list, lid) {
  518. stage_id_list = stage_id_list !== null ? stage_id_list.split(',') : [];
  519. const qtys = {
  520. gather_qty: 0,
  521. contract_qty: 0,
  522. qc_qty: 0,
  523. qc_minus_qty: 0,
  524. };
  525. for (const sid of stage_id_list) {
  526. const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills ' +
  527. ' INNER JOIN ( ' +
  528. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  529. ' WHERE tid = ? And sid = ?' +
  530. ' GROUP BY `lid`' +
  531. ' ) As MaxFilter ' +
  532. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`' +
  533. ' WHERE Bills.lid = ?';
  534. const sqlParam = [tid, sid, lid];
  535. const result = await this.db.queryOne(sql, sqlParam);
  536. if (result) {
  537. qtys.contract_qty = this.ctx.helper.add(qtys.contract_qty, result.contract_qty);
  538. qtys.qc_qty = this.ctx.helper.add(qtys.qc_qty, result.qc_qty);
  539. qtys.qc_minus_qty = this.ctx.helper.add(qtys.qc_minus_qty, result.qc_minus_qty);
  540. qtys.gather_qty = this.ctx.helper.add(qtys.gather_qty, this.ctx.helper.add(result.contract_qty, result.qc_qty));
  541. }
  542. }
  543. return this.ctx.helper.resetQtys(qtys);
  544. }
  545. async getSumTotalPriceByMaterial(stage_list) {
  546. let contract_tp = 0;
  547. let qc_tp = 0;
  548. for (const stage of stage_list) {
  549. const result = await this.getSumTotalPrice(stage);
  550. if (result) {
  551. contract_tp = this.ctx.helper.add(contract_tp, result.contract_tp);
  552. qc_tp = this.ctx.helper.add(qc_tp, result.qc_tp);
  553. }
  554. }
  555. return { contract_tp, qc_tp };
  556. }
  557. async getSumTotalPriceGclByMaterial(stage_list, regText) {
  558. let contract_tp = 0;
  559. let qc_tp = 0;
  560. for (const stage of stage_list) {
  561. const result = await this.getSumTotalPriceGcl(stage, regText);
  562. if (result) {
  563. contract_tp = this.ctx.helper.add(contract_tp, result.contract_tp);
  564. qc_tp = this.ctx.helper.add(qc_tp, result.qc_tp);
  565. }
  566. }
  567. return { contract_tp, qc_tp };
  568. }
  569. async sumLoad(lid, tenders, cover, ignore, loadChange) {
  570. const conn = await this.db.beginTransaction();
  571. try {
  572. const maxId = await this.ctx.service.ledger._getMaxLid(this.ctx.tender.id);
  573. const select = await this.ctx.service.ledger.getCompleteDataById(lid);
  574. const sumLoad = new SumLoad(this.ctx);
  575. const loadTree = await sumLoad.stageGatherGcl(select, maxId, tenders, null, cover, ignore, loadChange);
  576. const result = loadTree.getUpdateData();
  577. // if (result.errors.length > 100) throw '您导入的数据存在大量数据错误,请您仔细检查';
  578. const stageBills = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
  579. const updateStageBills = [], insertStageBills = [];
  580. for (const u of result.update) {
  581. const sb = stageBills.find(x => { return x.lid === u.lid; });
  582. if (!sb || sb.times !== this.ctx.stage.curTimes || sb.order !== this.ctx.stage.curOrder) {
  583. u.postil = sb ? sb.postil : null;
  584. u.tid = this.ctx.tender.id;
  585. u.sid = this.ctx.stage.id;
  586. u.said = this.ctx.session.sessionUser.accountId;
  587. u.times = this.ctx.stage.curTimes;
  588. u.order = this.ctx.stage.curOrder;
  589. insertStageBills.push(u);
  590. } else {
  591. u.id = sb.id;
  592. updateStageBills.push(u);
  593. }
  594. }
  595. for (const cd of result.qc_detail) {
  596. cd.tid = this.ctx.tender.id;
  597. cd.sid = this.ctx.stage.id;
  598. cd.import_lid = lid;
  599. }
  600. const his = await this.ctx.service.sumLoadHistory.saveStageHistory(this.ctx.tender.id, this.ctx.stage.id, lid, tenders, result.errors, cover);
  601. if (updateStageBills.length > 0) await conn.updateRows(this.tableName, updateStageBills);
  602. if (insertStageBills.length > 0) await conn.insert(this.tableName, insertStageBills);
  603. if (loadChange) {
  604. await conn.delete(this.ctx.service.stageImportChange.tableName, { import_lid: lid, sid: this.ctx.stage.id });
  605. if (result.qc_detail.length > 0) await conn.insert(this.ctx.service.stageImportChange.tableName, result.qc_detail);
  606. }
  607. await conn.commit();
  608. return { curStageData: result.update, sumLoadHis: his };
  609. } catch (err) {
  610. this.ctx.helper.log(err);
  611. await conn.rollback();
  612. throw (err.stack ? '导入工程量数据出错' : err);
  613. }
  614. }
  615. }
  616. return StageBills;
  617. };