stage_bills.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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.round(
  110. this.ctx.helper.times(d.contract_qty, ledgerData.unit_price), info.decimal.tp);
  111. }
  112. if (insertData.qc_qty) {
  113. d.qc_qty = this.round(insertData.qc_qty, precision.value);
  114. d.qc_tp = this.round(
  115. this.ctx.helper.times(d.qc_qty, ledgerData.unit_price), info.decimal.tp);
  116. }
  117. if (insertData.postil) {
  118. d.postil = insertData.postil;
  119. }
  120. await transaction.insert(this.tableName, d);
  121. }
  122. /**
  123. * 前端提交数据
  124. * @param {Object|Array} data - 提交的数据
  125. * @returns {Promise<void>}
  126. */
  127. async updateStageData(data) {
  128. const info = this.ctx.tender.info;
  129. const datas = data instanceof Array ? data : [data];
  130. const transaction = await this.db.beginTransaction();
  131. try {
  132. for (const d of datas) {
  133. const stageBills = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, d.lid);
  134. const ledgerBills = await this.ctx.service.ledger.getDataById(d.lid);
  135. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  136. if (d.contract_qty !== undefined) {
  137. d.contract_qty = this.round(d.contract_qty, precision.value);
  138. d.contract_tp = this.round(this.ctx.helper.times(d.contract_qty, ledgerBills.unit_price), info.decimal.tp);
  139. }
  140. if (d.qc_qty !== undefined) {
  141. d.qc_qty = this.round(d.qc_qty, precision.value);
  142. d.qc_tp = this.round(this.ctx.helper.times(d.qc_qty, ledgerBills.unit_price), info.decimal.tp);
  143. }
  144. if (!stageBills) {
  145. d.tid = this.ctx.tender.id;
  146. d.sid = this.ctx.stage.id;
  147. d.said = this.ctx.session.sessionUser.accountId;
  148. d.times = this.ctx.stage.curTimes;
  149. d.order = this.ctx.stage.curOrder;
  150. await transaction.insert(this.tableName, d);
  151. } else {
  152. d.id = stageBills.id;
  153. await transaction.update(this.tableName, d);
  154. }
  155. }
  156. await transaction.commit();
  157. } catch (err) {
  158. await transaction.rollback();
  159. throw err;
  160. }
  161. return await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, this._.map(datas, 'lid'));
  162. }
  163. /**
  164. * 根据
  165. * @param transaction
  166. * @param ledgerBills
  167. * @param stageBills
  168. * @param data
  169. * @returns {Promise<void>}
  170. * @private
  171. */
  172. async updateStageBillsQty(transaction, ledgerBills, stageBills, data) {
  173. const info = this.ctx.tender.info;
  174. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  175. const updateData = {};
  176. if (data.contract_qty) {
  177. updateData.contract_qty = this.round(data.contract_qty, precision.value);
  178. updateData.contract_tp = this.round(this.ctx.helper.times(updateData.contract_qty, ledgerBills.unit_price), info.decimal.tp);
  179. }
  180. if (data.qc_qty) {
  181. updateData.qc_qty = this.round(data.qc_qty, precision.value);
  182. updateData.qc_tp = this.round(this.ctx.helper.times(updateData.qc_qty, ledgerBills.unit_price), info.decimal.tp);
  183. }
  184. if (stageBills) {
  185. if ((updateData.contract_qty === undefined || stageBills.contract_qty !== updateData.contract_qty) ||
  186. (updateData.qc_qty === undefined || stageBills.qc_qty !== updateData.qc_qty)) {
  187. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  188. updateData.id = stageBills.id;
  189. await transaction.update(this.tableName, updateData);
  190. } else {
  191. await this._insertStageBillsData(transaction, updateData, ledgerBills);
  192. }
  193. }
  194. } else {
  195. await this._insertStageBillsData(transaction, updateData, ledgerBills);
  196. }
  197. };
  198. /**
  199. * 重算 本期计量 数量 (根据部位明细)
  200. * @param {Number} tid - 标段id
  201. * @param {Number} id - 需要计算的节点的id
  202. * @param {Number} lid - 台账id
  203. * @param {Object} transaction - 操作所属事务
  204. * @returns {Promise<void>}
  205. */
  206. async calc(tid, sid, lid, transaction) {
  207. const info = this.ctx.tender.info;
  208. const stageBills = await this.getLastestStageData(tid, sid, lid);
  209. const ledgerBills = await this.ctx.service.ledger.getDataById(lid);
  210. if (!ledgerBills) {
  211. throw '提交数据错误';
  212. }
  213. const posGather = await this.ctx.service.stagePos.getPosGather(tid, sid, lid, transaction);
  214. if (!posGather) { return; }
  215. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  216. // 计算
  217. if (posGather.contract_qty !== undefined) {
  218. posGather.contract_qty = this.round(posGather.contract_qty, precision.value);
  219. posGather.contract_tp = this.round(this.ctx.helper.times(posGather.contract_qty, ledgerBills.unit_price), info.decimal.tp);
  220. }
  221. if (posGather.qc_qty !== undefined) {
  222. posGather.qc_qty = this.round(posGather.qc_qty, precision.value);
  223. posGather.qc_tp = this.round(this.ctx.helper.times(posGather.qc_qty, ledgerBills.unit_price), info.decimal.tp);
  224. }
  225. if (stageBills) {
  226. if (stageBills.contract_qty === posGather.contract_qty && stageBills.qc_qty === posGather.qc_qty) {
  227. return;
  228. } else {
  229. const curOrder = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  230. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  231. posGather.id = stageBills.id;
  232. await transaction.update(this.tableName, posGather);
  233. } else {
  234. await this._insertStageBillsData(transaction, posGather, ledgerBills);
  235. }
  236. }
  237. } else {
  238. await this._insertStageBillsData(transaction, posGather, ledgerBills);
  239. }
  240. }
  241. async getSumTotalPrice(stage) {
  242. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp` FROM ' + this.tableName + ' As Bills ' +
  243. ' INNER JOIN ( ' +
  244. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` From ' + this.tableName +
  245. ' WHERE `times` <= ? AND `order` <= ?' +
  246. ' GROUP BY `lid`' +
  247. ' ) As MaxFilter ' +
  248. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid' +
  249. ' WHERE Bills.sid = ?';
  250. const sqlParam = [stage.curTimes, stage.curOrder, stage.id];
  251. const result = await this.db.queryOne(sql, sqlParam);
  252. return result;
  253. }
  254. async getSumTotalPriceFilter(stage, operate, filter) {
  255. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp`' +
  256. ' FROM ' + this.tableName + ' As Bills ' +
  257. ' INNER JOIN ( ' +
  258. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` From ' + this.tableName +
  259. ' WHERE `times` <= ? AND `order` <= ?' +
  260. ' GROUP BY `lid`' +
  261. ' ) As MaxFilter ' +
  262. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid ' +
  263. ' INNER JOIN ' + this.ctx.service.ledger.tableName + ' As Ledger ON Bills.lid = Ledger.id' +
  264. ' WHERE Bills.sid = ? And Ledger.b_code ' + operate + ' ?';
  265. const sqlParam = [stage.times, stage.curAuditor ? stage.curAuditor.order : 0, stage.id, filter];
  266. const result = await this.db.queryOne(sql, sqlParam);
  267. return result;
  268. }
  269. async getSumTotalPriceGcl(stage, regText) {
  270. if (regText) {
  271. return await this.getSumTotalPriceFilter(stage, 'REGEXP', regText);
  272. } else {
  273. return await this.getSumTotalPriceFilter(stage, '<>', this.db.escape(''));
  274. }
  275. }
  276. async getSumTotalPriceNotGcl(stage) {
  277. return await this.getSumTotalPriceFilter(stage, '=', this.db.escape(''));
  278. }
  279. }
  280. return StageBills;
  281. };