stage_bills.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 = '', result;
  33. if (lid) {
  34. if (lid instanceof Array) {
  35. lidSql = lid.length > 0 ? ' And lid in (' + this.ctx.helper.getInArrStrSqlFilter(lid) + ')' : '';
  36. } else {
  37. lidSql = ' And lid in (' + this.db.escape(lid) + ')';
  38. }
  39. }
  40. const sql = 'SELECT Bills.* 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 Bills.* 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. /**
  87. * 获取截止本期数据
  88. * @param {Number} tid - 标段id
  89. * @param {Number} sorder - 截止期序号
  90. * @param {String|Array[String]} lid - 台账id
  91. * @returns {Promise<*>}
  92. */
  93. async getEndStageData(tid, sorder, lid) {
  94. let lidSql = '';
  95. if (lid) {
  96. if (lid instanceof Array) {
  97. lidSql = lid.length > 0 ? ' And lid in (' + this.ctx.helper.getInArrStrSqlFilter(lid) + ')' : '';
  98. } else {
  99. lidSql = ' And lid in (' + this.db.escape(lid) + ')';
  100. }
  101. }
  102. const sql = 'SELECT Bills.tid, Bills.lid,' +
  103. ' Sum(Bills.contract_qty) As contract_qty, Sum(Bills.contract_tp) As contract_tp,' +
  104. ' Sum(Bills.qc_qty) As qc_qty, Sum(Bills.qc_tp) As qc_tp FROM ' + this.tableName + ' As Bills ' +
  105. ' INNER JOIN ( ' +
  106. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  107. ' WHERE tid = ? ' + lidSql +
  108. ' GROUP BY `lid`, `sid`' +
  109. ' ) As MaxFilter ' +
  110. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`' +
  111. ' INNER JOIN ' + this.ctx.service.stage.tableName + ' As Stage' +
  112. ' ON Bills.sid = Stage.id' +
  113. ' WHERE Stage.order <= ?' +
  114. ' GROUP BY `lid`';
  115. const sqlParam = [tid, sorder];
  116. if (!lid) {
  117. return await this.db.query(sql, sqlParam);
  118. } else if (lid instanceof Array) {
  119. return await this.db.query(sql, sqlParam);
  120. } else {
  121. return await this.db.queryOne(sql, sqlParam);
  122. }
  123. }
  124. async getStageBills(tid, sid, lid) {
  125. const sql = 'SELECT Stage.*, Ledger.unit_price FROM ?? As Stage, ?? As Ledger ' +
  126. ' Where Stage.tid = ?, Stage.sid = ?, Stage.lid = ?, Stage.lid = Ledger.id ' +
  127. ' Order Stage.time DESC, Stage.order DESC ';
  128. const sqlParam = [this.tableName, this.ctx.service.ledger.tableName];
  129. sqlParam.push(this.db.escape(tid));
  130. sqlParam.push(this.db.escape(sid));
  131. sqlParam.push(this.db.escape(lid));
  132. return await this.db.queryOne(sql, sqlParam);
  133. }
  134. async _insertStageBillsData(transaction, insertData, ledgerData) {
  135. const info = this.ctx.tender.info;
  136. const d = {
  137. tid: this.ctx.tender.id,
  138. lid: ledgerData.id,
  139. sid: this.ctx.stage.id,
  140. times: this.ctx.stage.curTimes,
  141. order: this.ctx.stage.curOrder,
  142. said: this.ctx.session.sessionUser.accountId,
  143. };
  144. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
  145. if (insertData.contract_qty) {
  146. d.contract_qty = this.round(insertData.contract_qty, precision.value);
  147. d.contract_tp = this.ctx.helper.mul(d.contract_qty, ledgerData.unit_price, info.decimal.tp);
  148. }
  149. if (insertData.qc_qty) {
  150. d.qc_qty = this.round(insertData.qc_qty, precision.value);
  151. d.qc_tp = this.ctx.helper.mul(d.qc_qty, ledgerData.unit_price, info.decimal.tp);
  152. }
  153. if (insertData.postil) {
  154. d.postil = insertData.postil;
  155. }
  156. await transaction.insert(this.tableName, d);
  157. }
  158. /**
  159. * 前端提交数据
  160. * @param {Object|Array} data - 提交的数据
  161. * @returns {Promise<void>}
  162. */
  163. async updateStageData(data) {
  164. const info = this.ctx.tender.info;
  165. const datas = data instanceof Array ? data : [data];
  166. const transaction = await this.db.beginTransaction();
  167. try {
  168. for (const d of datas) {
  169. const stageBills = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, d.lid);
  170. const ledgerBills = await this.ctx.service.ledger.getDataById(d.lid);
  171. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  172. if (d.contract_qty !== undefined) {
  173. d.contract_qty = this.round(d.contract_qty, precision.value);
  174. d.contract_tp = this.ctx.helper.mul(d.contract_qty, ledgerBills.unit_price, info.decimal.tp);
  175. }
  176. if (d.qc_qty !== undefined) {
  177. d.qc_qty = this.round(d.qc_qty, precision.value);
  178. d.qc_tp = this.ctx.helper.mul(d.qc_qty, ledgerBills.unit_price, info.decimal.tp);
  179. }
  180. if (!stageBills) {
  181. d.tid = this.ctx.tender.id;
  182. d.sid = this.ctx.stage.id;
  183. d.said = this.ctx.session.sessionUser.accountId;
  184. d.times = this.ctx.stage.curTimes;
  185. d.order = this.ctx.stage.curOrder;
  186. await transaction.insert(this.tableName, d);
  187. } else {
  188. d.id = stageBills.id;
  189. await transaction.update(this.tableName, d);
  190. }
  191. }
  192. await transaction.commit();
  193. } catch (err) {
  194. await transaction.rollback();
  195. throw err;
  196. }
  197. return await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, this._.map(datas, 'lid'));
  198. }
  199. /**
  200. * 根据
  201. * @param transaction
  202. * @param ledgerBills
  203. * @param stageBills
  204. * @param data
  205. * @returns {Promise<void>}
  206. * @private
  207. */
  208. async updateStageBillsQty(transaction, ledgerBills, stageBills, data) {
  209. const info = this.ctx.tender.info;
  210. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  211. const updateData = {};
  212. if (data.contract_qty) {
  213. updateData.contract_qty = this.round(data.contract_qty, precision.value);
  214. updateData.contract_tp = this.ctx.helper.mul(updateData.contract_qty, ledgerBills.unit_price, info.decimal.tp);
  215. }
  216. if (data.qc_qty) {
  217. updateData.qc_qty = this.round(data.qc_qty, precision.value);
  218. updateData.qc_tp = this.ctx.helper.mul(updateData.qc_qty, ledgerBills.unit_price, info.decimal.tp);
  219. }
  220. if (stageBills) {
  221. if ((updateData.contract_qty === undefined || stageBills.contract_qty !== updateData.contract_qty) ||
  222. (updateData.qc_qty === undefined || stageBills.qc_qty !== updateData.qc_qty)) {
  223. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  224. updateData.id = stageBills.id;
  225. await transaction.update(this.tableName, updateData);
  226. } else {
  227. await this._insertStageBillsData(transaction, updateData, ledgerBills);
  228. }
  229. }
  230. } else {
  231. await this._insertStageBillsData(transaction, updateData, ledgerBills);
  232. }
  233. };
  234. /**
  235. * 重算 本期计量 数量 (根据部位明细)
  236. * @param {Number} tid - 标段id
  237. * @param {Number} id - 需要计算的节点的id
  238. * @param {Number} lid - 台账id
  239. * @param {Object} transaction - 操作所属事务
  240. * @returns {Promise<void>}
  241. */
  242. async calc(tid, sid, lid, transaction) {
  243. const info = this.ctx.tender.info;
  244. const stageBills = await this.getLastestStageData(tid, sid, lid);
  245. const ledgerBills = await this.ctx.service.ledger.getDataById(lid);
  246. if (!ledgerBills) {
  247. throw '提交数据错误';
  248. }
  249. const posGather = await this.ctx.service.stagePos.getPosGather(tid, sid, lid, transaction);
  250. if (!posGather) { return; }
  251. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  252. // 计算
  253. if (posGather.contract_qty !== undefined) {
  254. posGather.contract_qty = this.round(posGather.contract_qty, precision.value);
  255. posGather.contract_tp = this.ctx.helper.mul(posGather.contract_qty, ledgerBills.unit_price, info.decimal.tp);
  256. }
  257. if (posGather.qc_qty !== undefined) {
  258. posGather.qc_qty = this.round(posGather.qc_qty, precision.value);
  259. posGather.qc_tp = this.ctx.helper.mul(posGather.qc_qty, ledgerBills.unit_price, info.decimal.tp);
  260. }
  261. if (stageBills) {
  262. if (stageBills.contract_qty === posGather.contract_qty && stageBills.qc_qty === posGather.qc_qty) {
  263. return;
  264. } else {
  265. const curOrder = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  266. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  267. posGather.id = stageBills.id;
  268. await transaction.update(this.tableName, posGather);
  269. } else {
  270. await this._insertStageBillsData(transaction, posGather, ledgerBills);
  271. }
  272. }
  273. } else {
  274. await this._insertStageBillsData(transaction, posGather, ledgerBills);
  275. }
  276. }
  277. async getSumTotalPrice(stage) {
  278. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp` FROM ' + this.tableName + ' As Bills ' +
  279. ' INNER JOIN ( ' +
  280. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` From ' + this.tableName +
  281. ' WHERE `times` <= ? AND `order` <= ?' +
  282. ' GROUP BY `lid`' +
  283. ' ) As MaxFilter ' +
  284. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid' +
  285. ' WHERE Bills.sid = ?';
  286. const sqlParam = [stage.curTimes, stage.curOrder, stage.id];
  287. const result = await this.db.queryOne(sql, sqlParam);
  288. return result;
  289. }
  290. async getSumTotalPriceFilter(stage, operate, filter) {
  291. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp`' +
  292. ' FROM ' + this.tableName + ' As Bills ' +
  293. ' INNER JOIN ( ' +
  294. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` From ' + this.tableName +
  295. ' WHERE `times` <= ? AND `order` <= ?' +
  296. ' GROUP BY `lid`' +
  297. ' ) As MaxFilter ' +
  298. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid ' +
  299. ' INNER JOIN ' + this.ctx.service.ledger.tableName + ' As Ledger ON Bills.lid = Ledger.id' +
  300. ' WHERE Bills.sid = ? And Ledger.b_code ' + operate + ' ?';
  301. const sqlParam = [stage.times, stage.curAuditor ? stage.curAuditor.order : 0, stage.id, filter];
  302. const result = await this.db.queryOne(sql, sqlParam);
  303. return result;
  304. }
  305. async getSumTotalPriceGcl(stage, regText) {
  306. if (regText) {
  307. return await this.getSumTotalPriceFilter(stage, 'REGEXP', regText);
  308. } else {
  309. return await this.getSumTotalPriceFilter(stage, '<>', this.db.escape(''));
  310. }
  311. }
  312. async getSumTotalPriceNotGcl(stage) {
  313. return await this.getSumTotalPriceFilter(stage, '=', this.db.escape(''));
  314. }
  315. }
  316. return StageBills;
  317. };