stage_bills.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. module.exports = app => {
  12. class StageBills extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'stage_bills';
  22. }
  23. /**
  24. * 查询期计量最后审核人数据
  25. * @param {Number} tid - 标段id
  26. * @param {Number} sid - 期id
  27. * @param {Number|Array} lid - 台账节点id(可以为空)
  28. * @returns {Promise<*>}
  29. */
  30. async getLastestStageData(tid, sid, lid) {
  31. const lidSql = lid ? ' And lid in (?)' : '';
  32. const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
  33. ' INNER JOIN ( ' +
  34. ' SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `lid`, `sid` From ' + this.tableName +
  35. ' WHERE tid = ? And sid = ?' + lidSql +
  36. ' GROUP BY `lid`' +
  37. ' ) As MaxFilter ' +
  38. ' ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`';
  39. const sqlParam = [tid, sid];
  40. if (!lid) {
  41. return await this.db.query(sql, sqlParam);
  42. } else if (lid instanceof Array) {
  43. sqlParam.push(lid.join(', '));
  44. return await this.db.query(sql, sqlParam);
  45. } else {
  46. sqlParam.push(lid);
  47. return await this.db.queryOne(sql, sqlParam);
  48. }
  49. }
  50. /**
  51. * 查询 某期 某轮审批 某人数据
  52. * @param {Number} tid - 标段id
  53. * @param {Number} sid - 期id
  54. * @param {Number} times - 第几轮
  55. * @param {Number} order - 流程
  56. * @param {Number|Array} lid - 台账节点id(可以为空)
  57. * @returns {Promise<*>}
  58. */
  59. async getAuditorStageData(tid, sid, times, order, lid) {
  60. const lidSql = lid ? ' And Bills.lid in (?)' : '';
  61. const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
  62. ' INNER JOIN ( ' +
  63. ' SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `lid` From ' + this.tableName +
  64. ' WHERE `times` < ? OR (`times` = ? AND `order` <= ?)' +
  65. ' GROUP BY `lid`' +
  66. ' ) As MaxFilter ' +
  67. ' ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid' +
  68. ' WHERE Bills.tid = ? And Bills.sid = ?' + lidSql;
  69. const sqlParam = [times, times, order, tid, sid];
  70. if (!lid) {
  71. return await this.db.query(sql, sqlParam);
  72. } else if (lid instanceof Array) {
  73. sqlParam.push(lid.join(', '));
  74. return await this.db.query(sql, sqlParam);
  75. } else {
  76. sqlParam.push(lid);
  77. return await this.db.queryOne(sql, sqlParam);
  78. }
  79. }
  80. async getStageBills(tid, sid, lid) {
  81. const sql = 'SELECT Stage.*, Ledger.unit_price FROM ?? As Stage, ?? As Ledger ' +
  82. ' Where Stage.tid = ?, Stage.sid = ?, Stage.lid = ?, Stage.lid = Ledger.id ' +
  83. ' Order Stage.time DESC, Stage.order DESC ';
  84. const sqlParam = [this.tableName, this.ctx.service.ledger.tableName];
  85. sqlParam.push(this.db.escape(tid));
  86. sqlParam.push(this.db.escape(sid));
  87. sqlParam.push(this.db.escape(lid));
  88. return await this.db.queryOne(sql, sqlParam);
  89. }
  90. async _insertStageBillsData(transaction, insertData, ledgerData) {
  91. const d = {
  92. tid: this.ctx.tender.id,
  93. lid: ledgerData.id,
  94. sid: this.ctx.stage.id,
  95. times: this.ctx.stage.curTimes,
  96. order: this.ctx.stage.curOrder,
  97. said: this.ctx.session.sessionUser.accountId,
  98. };
  99. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
  100. if (insertData.contract_qty) {
  101. d.contract_qty = this.round(insertData.contract_qty, precision.value);
  102. d.contract_tp = d.contract_qty * ledgerData.unit_price;
  103. }
  104. if (insertData.qc_qty) {
  105. d.qc_qty = this.round(insertData.qc_qty, precision.value);
  106. d.qc_tp = d.qc_qty * ledgerData.unit_price;
  107. }
  108. if (insertData.postil) {
  109. d.postil = insertData.postil;
  110. }
  111. await transaction.insert(this.tableName, d);
  112. }
  113. /**
  114. * 前端提交数据
  115. * @param {Object|Array} data - 提交的数据
  116. * @returns {Promise<void>}
  117. */
  118. async updateStageData(data) {
  119. const datas = data instanceof Array ? data : [data];
  120. const transaction = await this.db.beginTransaction();
  121. try {
  122. for (const d of datas) {
  123. const stageBills = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, d.lid);
  124. const ledgerBills = await this.ctx.service.ledger.getDataById(d.lid);
  125. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  126. if (d.contract_qty !== undefined) {
  127. d.contract_qty = this.round(d.contract_qty, precision.value);
  128. d.contract_tp = d.contract_qty * ledgerBills.unit_price;
  129. }
  130. if (d.qc_qty !== undefined) {
  131. d.qc_qty = this.round(d.qc_qty, precision.value);
  132. d.qc_tp = d.qc_qty * ledgerBills.unit_price;
  133. }
  134. if (!stageBills) {
  135. d.tid = this.ctx.tender.id;
  136. d.sid = this.ctx.stage.id;
  137. d.said = this.ctx.session.sessionUser.accountId;
  138. d.times = this.ctx.stage.curTimes;
  139. d.order = this.ctx.stage.curOrder;
  140. await transaction.insert(this.tableName, d);
  141. } else {
  142. d.id = stageBills.id;
  143. await transaction.update(this.tableName, d);
  144. }
  145. }
  146. await transaction.commit();
  147. } catch (err) {
  148. await transaction.rollback();
  149. throw err;
  150. }
  151. return await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, this._.map(datas, 'lid'));
  152. }
  153. /**
  154. * 根据
  155. * @param transaction
  156. * @param ledgerBills
  157. * @param stageBills
  158. * @param data
  159. * @returns {Promise<void>}
  160. * @private
  161. */
  162. async updateStageBillsQty(transaction, ledgerBills, stageBills, data) {
  163. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  164. const updateData = {};
  165. if (data.contract_qty) {
  166. updateData.contract_qty = this.round(data.contract_qty, precision.value);
  167. updateData.contract_tp = this.round(updateData.contract_qty * ledgerBills.unit_price);
  168. }
  169. if (data.qc_qty) {
  170. updateData.qc_qty = this.round(data.qc_qty, precision.value);
  171. updateData.qc_tp = this.round(updateData.qc_qty * ledgerBills.unit_price);
  172. }
  173. if (stageBills) {
  174. if ((updateData.contract_qty === undefined || stageBills.contract_qty !== updateData.contract_qty) ||
  175. (updateData.qc_qty === undefined || stageBills.qc_qty !== updateData.qc_qty)) {
  176. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  177. updateData.id = stageBills.id;
  178. await transaction.update(this.tableName, updateData);
  179. } else {
  180. await this._insertStageBillsData(transaction, updateData, ledgerBills);
  181. }
  182. }
  183. } else {
  184. await this._insertStageBillsData(transaction, updateData, ledgerBills);
  185. }
  186. };
  187. /**
  188. * 重算 本期计量 数量 (根据部位明细)
  189. * @param {Number} tid - 标段id
  190. * @param {Number} id - 需要计算的节点的id
  191. * @param {Number} lid - 台账id
  192. * @param {Object} transaction - 操作所属事务
  193. * @returns {Promise<void>}
  194. */
  195. async calc(tid, sid, lid, transaction) {
  196. const stageBills = await this.getLastestStageData(tid, sid, lid);
  197. const ledgerBills = await this.ctx.service.ledger.getDataById(lid);
  198. if (!ledgerBills) {
  199. throw '提交数据错误';
  200. }
  201. const posGather = await this.ctx.service.stagePos.getPosGather(tid, sid, lid, transaction);
  202. if (!posGather) { return; }
  203. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  204. // 计算
  205. if (posGather.contract_qty !== undefined) {
  206. posGather.contract_qty = this.round(posGather.contract_qty, precision.value);
  207. posGather.contract_tp = this.round(posGather.contract_qty * ledgerBills.unit_price);
  208. }
  209. if (posGather.qc_qty !== undefined) {
  210. posGather.qc_qty = this.round(posGather.qc_qty, precision.value);
  211. posGather.qc_tp = this.round(posGather.qc_qty * ledgerBills.unit_price);
  212. }
  213. if (stageBills) {
  214. if (stageBills.contract_qty === posGather.contract_qty && stageBills.qc_qty === posGather.qc_qty) {
  215. return;
  216. } else {
  217. const curOrder = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  218. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  219. posGather.id = stageBills.id;
  220. await transaction.update(this.tableName, posGather);
  221. } else {
  222. await this._insertStageBillsData(transaction, posGather, ledgerBills);
  223. }
  224. }
  225. } else {
  226. await this._insertStageBillsData(transaction, posGather, ledgerBills);
  227. }
  228. }
  229. async getSumTotalPrice(stage) {
  230. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp` FROM ' + this.tableName + ' As Bills ' +
  231. ' INNER JOIN ( ' +
  232. ' SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `lid` From ' + this.tableName +
  233. ' WHERE `times` <= ? AND `order` <= ?' +
  234. ' GROUP BY `lid`' +
  235. ' ) As MaxFilter ' +
  236. ' ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid' +
  237. ' WHERE Bills.sid = ?';
  238. const sqlParam = [stage.curTimes, stage.curOrder, stage.id];
  239. const result = await this.db.queryOne(sql, sqlParam);
  240. return result;
  241. }
  242. async getSumTotalPriceFilter(stage, operate, filter) {
  243. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp` FROM ( ' + this.tableName + ' As Bills ' +
  244. ' INNER JOIN ( ' +
  245. ' SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `lid` From ' + this.tableName +
  246. ' WHERE `times` <= ? AND `order` <= ?' +
  247. ' GROUP BY `lid`' +
  248. ' ) As MaxFilter ' +
  249. ' ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid, ' +
  250. ' ' + this.ctx.service.ledger.tableName + ' As Ledger ) ' +
  251. ' WHERE Bills.sid = ? AND Bills.lid = Ledger.id And Ledger.b_code ' + operate + ' ?';
  252. const sqlParam = [stage.times, stage.curAuditor ? stage.curAuditor.order : 0, stage.id, filter];
  253. const result = await this.db.queryOne(sql, sqlParam);
  254. return result;
  255. //return this._.add(result.contract_tp, result.qc_tp);
  256. }
  257. async getSumTotalPriceGcl(stage, regText) {
  258. if (regText) {
  259. return await this.getSumTotalPriceFilter(stage, 'REGEXP', regText);
  260. } else {
  261. return await this.getSumTotalPriceFilter(stage, '<>', this.db.escape(''));
  262. }
  263. }
  264. async getSumTotalPriceNotGcl(stage) {
  265. return await this.getSumTotalPriceFilter(stage, '=', this.db.escape(''));
  266. }
  267. }
  268. return StageBills;
  269. };