stage_bills.js 13 KB

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