stage_bills.js 13 KB

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