stage_bills.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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, orgData, 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. if (orgData) {
  145. d.contract_qty = orgData.contract_qty;
  146. d.contract_tp = orgData.contract_tp;
  147. d.qc_qty = orgData.qc_qty;
  148. d.qc_tp = orgData.qc_tp;
  149. d.postil = orgData.postil;
  150. }
  151. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
  152. if (insertData.contract_qty) {
  153. d.contract_qty = this.round(insertData.contract_qty, precision.value);
  154. d.contract_tp = this.ctx.helper.mul(d.contract_qty, ledgerData.unit_price, info.decimal.tp);
  155. }
  156. if (insertData.qc_qty) {
  157. d.qc_qty = this.round(insertData.qc_qty, precision.value);
  158. d.qc_tp = this.ctx.helper.mul(d.qc_qty, ledgerData.unit_price, info.decimal.tp);
  159. }
  160. if (insertData.postil) {
  161. d.postil = insertData.postil;
  162. }
  163. await transaction.insert(this.tableName, d);
  164. }
  165. /**
  166. * 前端提交数据
  167. * @param {Object|Array} data - 提交的数据
  168. * @returns {Promise<void>}
  169. */
  170. async updateStageData(data) {
  171. const info = this.ctx.tender.info;
  172. const datas = data instanceof Array ? data : [data];
  173. const transaction = await this.db.beginTransaction();
  174. try {
  175. for (const d of datas) {
  176. const stageBills = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, d.lid);
  177. const ledgerBills = await this.ctx.service.ledger.getDataById(d.lid);
  178. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  179. if (d.contract_qty !== undefined) {
  180. d.contract_qty = this.round(d.contract_qty, precision.value);
  181. d.contract_tp = this.ctx.helper.mul(d.contract_qty, ledgerBills.unit_price, info.decimal.tp);
  182. }
  183. if (d.qc_qty !== undefined) {
  184. d.qc_qty = this.round(d.qc_qty, precision.value);
  185. d.qc_tp = this.ctx.helper.mul(d.qc_qty, ledgerBills.unit_price, info.decimal.tp);
  186. }
  187. if (!stageBills || stageBills.times !== this.ctx.stage.curTimes || stageBills.order !== this.ctx.stage.curOrder) {
  188. await this._insertStageBillsData(transaction, d, stageBills, ledgerBills);
  189. // d.tid = this.ctx.tender.id;
  190. // d.sid = this.ctx.stage.id;
  191. // d.said = this.ctx.session.sessionUser.accountId;
  192. // d.times = this.ctx.stage.curTimes;
  193. // d.order = this.ctx.stage.curOrder;
  194. // await transaction.insert(this.tableName, d);
  195. } else {
  196. d.id = stageBills.id;
  197. await transaction.update(this.tableName, d);
  198. }
  199. }
  200. await transaction.commit();
  201. } catch (err) {
  202. await transaction.rollback();
  203. throw err;
  204. }
  205. return await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, this._.map(datas, 'lid'));
  206. }
  207. /**
  208. * 根据
  209. * @param transaction
  210. * @param ledgerBills
  211. * @param stageBills
  212. * @param data
  213. * @returns {Promise<void>}
  214. * @private
  215. */
  216. async updateStageBillsQty(transaction, ledgerBills, stageBills, data) {
  217. const info = this.ctx.tender.info;
  218. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  219. const updateData = {};
  220. if (data.contract_qty !== undefined) {
  221. updateData.contract_qty = this.round(data.contract_qty, precision.value);
  222. updateData.contract_tp = this.ctx.helper.mul(updateData.contract_qty, ledgerBills.unit_price, info.decimal.tp);
  223. }
  224. if (data.qc_qty !== undefined) {
  225. updateData.qc_qty = this.round(data.qc_qty, precision.value);
  226. updateData.qc_tp = this.ctx.helper.mul(updateData.qc_qty, ledgerBills.unit_price, info.decimal.tp);
  227. }
  228. if (stageBills) {
  229. if ((updateData.contract_qty === undefined || stageBills.contract_qty !== updateData.contract_qty) ||
  230. (updateData.qc_qty === undefined || stageBills.qc_qty !== updateData.qc_qty)) {
  231. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  232. updateData.id = stageBills.id;
  233. await transaction.update(this.tableName, updateData);
  234. } else {
  235. await this._insertStageBillsData(transaction, updateData, stageBills, ledgerBills);
  236. }
  237. }
  238. } else {
  239. await this._insertStageBillsData(transaction, updateData, stageBills, ledgerBills);
  240. }
  241. };
  242. /**
  243. * 重算 本期计量 数量 (根据部位明细)
  244. * @param {Number} tid - 标段id
  245. * @param {Number} id - 需要计算的节点的id
  246. * @param {Number} lid - 台账id
  247. * @param {Object} transaction - 操作所属事务
  248. * @returns {Promise<void>}
  249. */
  250. async calc(tid, sid, lid, transaction) {
  251. const info = this.ctx.tender.info;
  252. const stageBills = await this.getLastestStageData(tid, sid, lid);
  253. const ledgerBills = await this.ctx.service.ledger.getDataById(lid);
  254. if (!ledgerBills) {
  255. throw '提交数据错误';
  256. }
  257. const posGather = await this.ctx.service.stagePos.getPosGather(tid, sid, lid, transaction);
  258. if (!posGather) { return; }
  259. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  260. // 计算
  261. if (posGather.contract_qty !== undefined) {
  262. posGather.contract_qty = this.round(posGather.contract_qty, precision.value);
  263. posGather.contract_tp = this.ctx.helper.mul(posGather.contract_qty, ledgerBills.unit_price, info.decimal.tp);
  264. }
  265. if (posGather.qc_qty !== undefined) {
  266. posGather.qc_qty = this.round(posGather.qc_qty, precision.value);
  267. posGather.qc_tp = this.ctx.helper.mul(posGather.qc_qty, ledgerBills.unit_price, info.decimal.tp);
  268. }
  269. if (stageBills) {
  270. if (stageBills.contract_qty === posGather.contract_qty && stageBills.qc_qty === posGather.qc_qty) {
  271. return;
  272. } else {
  273. const curOrder = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  274. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  275. posGather.id = stageBills.id;
  276. await transaction.update(this.tableName, posGather);
  277. } else {
  278. await this._insertStageBillsData(transaction, posGather, stageBills, ledgerBills);
  279. }
  280. }
  281. } else {
  282. await this._insertStageBillsData(transaction, posGather, stageBills, ledgerBills);
  283. }
  284. }
  285. async getSumTotalPrice(stage) {
  286. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp` FROM ' + this.tableName + ' As Bills ' +
  287. ' INNER JOIN ( ' +
  288. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` From ' + this.tableName +
  289. ' WHERE `times` <= ? AND `order` <= ?' +
  290. ' GROUP BY `lid`' +
  291. ' ) As MaxFilter ' +
  292. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid' +
  293. ' WHERE Bills.sid = ?';
  294. const sqlParam = [stage.curTimes, stage.curOrder, stage.id];
  295. const result = await this.db.queryOne(sql, sqlParam);
  296. return result;
  297. }
  298. async getSumTotalPriceFilter(stage, operate, filter) {
  299. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp`' +
  300. ' FROM ' + this.tableName + ' As Bills ' +
  301. ' INNER JOIN ( ' +
  302. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` From ' + this.tableName +
  303. ' WHERE `times` <= ? AND `order` <= ?' +
  304. ' GROUP BY `lid`' +
  305. ' ) As MaxFilter ' +
  306. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid ' +
  307. ' INNER JOIN ' + this.ctx.service.ledger.tableName + ' As Ledger ON Bills.lid = Ledger.id' +
  308. ' WHERE Bills.sid = ? And Ledger.b_code ' + operate + ' ?';
  309. const sqlParam = [stage.times, stage.curAuditor ? stage.curAuditor.order : 0, stage.id, filter];
  310. const result = await this.db.queryOne(sql, sqlParam);
  311. return result;
  312. }
  313. async getSumTotalPriceGcl(stage, regText) {
  314. if (regText) {
  315. return await this.getSumTotalPriceFilter(stage, 'REGEXP', regText);
  316. } else {
  317. return await this.getSumTotalPriceFilter(stage, '<>', this.db.escape(''));
  318. }
  319. }
  320. async getSumTotalPriceNotGcl(stage) {
  321. return await this.getSumTotalPriceFilter(stage, '=', this.db.escape(''));
  322. }
  323. }
  324. return StageBills;
  325. };