stage_bills.js 14 KB

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