stage_bills.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. async getLastestStageData2(tid, sid, lid) {
  87. let lidSql = '', result;
  88. if (lid) {
  89. if (lid instanceof Array) {
  90. lidSql = lid.length > 0 ? ' And lid in (' + this.ctx.helper.getInArrStrSqlFilter(lid) + ')' : '';
  91. } else {
  92. lidSql = ' And lid in (' + this.db.escape(lid) + ')';
  93. }
  94. }
  95. const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills ' +
  96. ' INNER JOIN ( ' +
  97. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  98. ' WHERE tid = ? And sid = ?' + lidSql +
  99. ' GROUP BY `lid`' +
  100. ' ) As MaxFilter ' +
  101. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`';
  102. const sqlParam = [tid, sid];
  103. if (!lid) {
  104. return await this.db.query(sql, sqlParam);
  105. } else if (lid instanceof Array) {
  106. return await this.db.query(sql, sqlParam);
  107. } else {
  108. return await this.db.queryOne(sql, sqlParam);
  109. }
  110. }
  111. async getStageUsedBills(tid, sid) {
  112. const sql = 'SELECT Bills.lid, (Bills.contract_qty <> 0 or Bills.qc_qty <> 0) As used FROM ' + this.tableName + ' As Bills ' +
  113. ' INNER JOIN ( ' +
  114. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  115. ' WHERE tid = ? And sid = ?' +
  116. ' GROUP BY `lid`' +
  117. ' ) As MaxFilter ' +
  118. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`';
  119. const sqlParam = [tid, sid];
  120. console.log(this.db.format(sql, sqlParam));
  121. const stageBills = await this.db.query(sql, sqlParam);
  122. return this._.map(this._.filter(stageBills, 'used'), 'lid');
  123. }
  124. /**
  125. * 获取截止本期数据
  126. * @param {Number} tid - 标段id
  127. * @param {Number} sorder - 截止期序号
  128. * @param {String|Array[String]} lid - 台账id
  129. * @returns {Promise<*>}
  130. */
  131. async getEndStageData(tid, sorder, lid) {
  132. let lidSql = '';
  133. if (lid) {
  134. if (lid instanceof Array) {
  135. lidSql = lid.length > 0 ? ' And lid in (' + this.ctx.helper.getInArrStrSqlFilter(lid) + ')' : '';
  136. } else {
  137. lidSql = ' And lid in (' + this.db.escape(lid) + ')';
  138. }
  139. }
  140. const sql = 'SELECT Bills.tid, Bills.lid,' +
  141. ' Sum(Bills.contract_qty) As contract_qty, Sum(Bills.contract_tp) As contract_tp,' +
  142. ' Sum(Bills.qc_qty) As qc_qty, Sum(Bills.qc_tp) As qc_tp FROM ' + this.tableName + ' As Bills ' +
  143. ' INNER JOIN ( ' +
  144. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  145. ' WHERE tid = ? ' + lidSql +
  146. ' GROUP BY `lid`, `sid`' +
  147. ' ) As MaxFilter ' +
  148. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`' +
  149. ' INNER JOIN ' + this.ctx.service.stage.tableName + ' As Stage' +
  150. ' ON Bills.sid = Stage.id' +
  151. ' WHERE Stage.order <= ?' +
  152. ' GROUP BY `lid`';
  153. const sqlParam = [tid, sorder];
  154. if (!lid) {
  155. return await this.db.query(sql, sqlParam);
  156. } else if (lid instanceof Array) {
  157. return await this.db.query(sql, sqlParam);
  158. } else {
  159. return await this.db.queryOne(sql, sqlParam);
  160. }
  161. }
  162. async getStageBills(tid, sid, lid) {
  163. const sql = 'SELECT Stage.*, Ledger.unit_price FROM ?? As Stage, ?? As Ledger ' +
  164. ' Where Stage.tid = ?, Stage.sid = ?, Stage.lid = ?, Stage.lid = Ledger.id ' +
  165. ' Order Stage.time DESC, Stage.order DESC ';
  166. const sqlParam = [this.tableName, this.ctx.service.ledger.tableName];
  167. sqlParam.push(this.db.escape(tid));
  168. sqlParam.push(this.db.escape(sid));
  169. sqlParam.push(this.db.escape(lid));
  170. return await this.db.queryOne(sql, sqlParam);
  171. }
  172. async _insertStageBillsData(transaction, insertData, orgData, ledgerData) {
  173. const info = this.ctx.tender.info;
  174. const d = {
  175. tid: this.ctx.tender.id,
  176. lid: ledgerData.id,
  177. sid: this.ctx.stage.id,
  178. times: this.ctx.stage.curTimes,
  179. order: this.ctx.stage.curOrder,
  180. said: this.ctx.session.sessionUser.accountId,
  181. };
  182. if (orgData) {
  183. d.contract_qty = orgData.contract_qty;
  184. d.contract_tp = orgData.contract_tp;
  185. d.qc_qty = orgData.qc_qty;
  186. d.qc_tp = orgData.qc_tp;
  187. d.postil = orgData.postil;
  188. }
  189. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
  190. if (insertData.contract_qty !== undefined) {
  191. d.contract_qty = this.round(insertData.contract_qty, precision.value);
  192. d.contract_tp = this.ctx.helper.mul(d.contract_qty, ledgerData.unit_price, info.decimal.tp);
  193. }
  194. if (insertData.qc_qty !== undefined) {
  195. d.qc_qty = this.round(insertData.qc_qty, precision.value);
  196. d.qc_tp = this.ctx.helper.mul(d.qc_qty, ledgerData.unit_price, info.decimal.tp);
  197. }
  198. if (insertData.postil) {
  199. d.postil = insertData.postil;
  200. }
  201. await transaction.insert(this.tableName, d);
  202. }
  203. /**
  204. * 前端提交数据
  205. * @param {Object|Array} data - 提交的数据
  206. * @returns {Promise<void>}
  207. */
  208. async updateStageData(data) {
  209. const info = this.ctx.tender.info;
  210. const datas = data instanceof Array ? data : [data];
  211. const transaction = await this.db.beginTransaction();
  212. try {
  213. for (const d of datas) {
  214. const stageBills = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, d.lid);
  215. const ledgerBills = await this.ctx.service.ledger.getDataById(d.lid);
  216. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  217. if (d.contract_qty !== undefined) {
  218. d.contract_qty = this.round(d.contract_qty, precision.value);
  219. d.contract_tp = this.ctx.helper.mul(d.contract_qty, ledgerBills.unit_price, info.decimal.tp);
  220. }
  221. if (d.qc_qty !== undefined) {
  222. d.qc_qty = this.round(d.qc_qty, precision.value);
  223. d.qc_tp = this.ctx.helper.mul(d.qc_qty, ledgerBills.unit_price, info.decimal.tp);
  224. }
  225. if (!stageBills || stageBills.times !== this.ctx.stage.curTimes || stageBills.order !== this.ctx.stage.curOrder) {
  226. await this._insertStageBillsData(transaction, d, stageBills, ledgerBills);
  227. } else {
  228. d.id = stageBills.id;
  229. await transaction.update(this.tableName, d);
  230. }
  231. }
  232. await transaction.commit();
  233. } catch (err) {
  234. await transaction.rollback();
  235. throw err;
  236. }
  237. return await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, this._.map(datas, 'lid'));
  238. }
  239. /**
  240. * 根据
  241. * @param transaction
  242. * @param ledgerBills
  243. * @param stageBills
  244. * @param data
  245. * @returns {Promise<void>}
  246. * @private
  247. */
  248. async updateStageBillsQty(transaction, ledgerBills, stageBills, data) {
  249. const info = this.ctx.tender.info;
  250. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  251. const updateData = {};
  252. if (data.contract_qty !== undefined) {
  253. updateData.contract_qty = this.round(data.contract_qty, precision.value);
  254. updateData.contract_tp = this.ctx.helper.mul(updateData.contract_qty, ledgerBills.unit_price, info.decimal.tp);
  255. }
  256. if (data.qc_qty !== undefined) {
  257. updateData.qc_qty = this.round(data.qc_qty, precision.value);
  258. updateData.qc_tp = this.ctx.helper.mul(updateData.qc_qty, ledgerBills.unit_price, info.decimal.tp);
  259. }
  260. if (stageBills) {
  261. if ((updateData.contract_qty === undefined || stageBills.contract_qty !== updateData.contract_qty) ||
  262. (updateData.qc_qty === undefined || stageBills.qc_qty !== updateData.qc_qty)) {
  263. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  264. updateData.id = stageBills.id;
  265. await transaction.update(this.tableName, updateData);
  266. } else {
  267. await this._insertStageBillsData(transaction, updateData, stageBills, ledgerBills);
  268. }
  269. }
  270. } else {
  271. await this._insertStageBillsData(transaction, updateData, stageBills, ledgerBills);
  272. }
  273. };
  274. /**
  275. * 重算 本期计量 数量 (根据部位明细)
  276. * @param {Number} tid - 标段id
  277. * @param {Number} id - 需要计算的节点的id
  278. * @param {Number} lid - 台账id
  279. * @param {Object} transaction - 操作所属事务
  280. * @returns {Promise<void>}
  281. */
  282. async calc(tid, sid, lid, transaction) {
  283. const info = this.ctx.tender.info;
  284. const stageBills = await this.getLastestStageData(tid, sid, lid);
  285. const ledgerBills = await this.ctx.service.ledger.getDataById(lid);
  286. if (!ledgerBills) {
  287. throw '提交数据错误';
  288. }
  289. const posGather = await this.ctx.service.stagePos.getPosGather(tid, sid, lid, transaction);
  290. if (!posGather) { return; }
  291. const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
  292. // 计算
  293. if (posGather.contract_qty !== undefined) {
  294. posGather.contract_qty = this.round(posGather.contract_qty, precision.value);
  295. posGather.contract_tp = this.ctx.helper.mul(posGather.contract_qty, ledgerBills.unit_price, info.decimal.tp);
  296. }
  297. if (posGather.qc_qty !== undefined) {
  298. posGather.qc_qty = this.round(posGather.qc_qty, precision.value);
  299. posGather.qc_tp = this.ctx.helper.mul(posGather.qc_qty, ledgerBills.unit_price, info.decimal.tp);
  300. }
  301. if (stageBills) {
  302. if (stageBills.contract_qty === posGather.contract_qty && stageBills.qc_qty === posGather.qc_qty) {
  303. return;
  304. } else {
  305. const curOrder = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  306. if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  307. posGather.id = stageBills.id;
  308. await transaction.update(this.tableName, posGather);
  309. } else {
  310. await this._insertStageBillsData(transaction, posGather, stageBills, ledgerBills);
  311. }
  312. }
  313. } else {
  314. await this._insertStageBillsData(transaction, posGather, stageBills, ledgerBills);
  315. }
  316. }
  317. async getSumTotalPrice(stage) {
  318. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp` FROM ' + this.tableName + ' As Bills ' +
  319. ' INNER JOIN ( ' +
  320. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` From ' + this.tableName +
  321. ' WHERE `times` <= ? AND `order` <= ?' +
  322. ' GROUP BY `lid`' +
  323. ' ) As MaxFilter ' +
  324. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid' +
  325. ' WHERE Bills.sid = ?';
  326. const sqlParam = [stage.curTimes, stage.curOrder, stage.id];
  327. const result = await this.db.queryOne(sql, sqlParam);
  328. return result;
  329. }
  330. async getSumTotalPriceFilter(stage, operate, filter) {
  331. const sql = 'SELECT Sum(`contract_tp`) As `contract_tp`, Sum(`qc_tp`) As `qc_tp`' +
  332. ' FROM ' + this.tableName + ' As Bills ' +
  333. ' INNER JOIN ( ' +
  334. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` From ' + this.tableName +
  335. ' WHERE `times` <= ? AND `order` <= ?' +
  336. ' GROUP BY `lid`' +
  337. ' ) As MaxFilter ' +
  338. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.flow And Bills.lid = MaxFilter.lid ' +
  339. ' INNER JOIN ' + this.ctx.service.ledger.tableName + ' As Ledger ON Bills.lid = Ledger.id' +
  340. ' WHERE Bills.sid = ? And Ledger.b_code ' + operate + ' ?';
  341. const sqlParam = [stage.times, stage.curAuditor ? stage.curAuditor.order : 0, stage.id, filter];
  342. const result = await this.db.queryOne(sql, sqlParam);
  343. return result;
  344. }
  345. async getSumTotalPriceGcl(stage, regText) {
  346. if (regText) {
  347. return await this.getSumTotalPriceFilter(stage, 'REGEXP', regText);
  348. } else {
  349. return await this.getSumTotalPriceFilter(stage, '<>', this.db.escape(''));
  350. }
  351. }
  352. async getSumTotalPriceNotGcl(stage) {
  353. return await this.getSumTotalPriceFilter(stage, '=', this.db.escape(''));
  354. }
  355. /**
  356. * 多期清单数据整合 (材料调差调用)
  357. * @param {Number} tid - 标段id
  358. * @param {String} stage_id_list - 期id列表
  359. * @returns {Promise<void>}
  360. */
  361. async getStagesData(tid, stage_id_list) {
  362. let stage_id_listSql = '';
  363. stage_id_list = stage_id_list.indexOf(',') !== -1 ? stage_id_list.split(',') : stage_id_list;
  364. if (stage_id_list) {
  365. if (stage_id_list instanceof Array) {
  366. stage_id_listSql = stage_id_list.length > 0 ? ' And sid in (' + this.ctx.helper.getInArrStrSqlFilter(stage_id_list) + ')' : '';
  367. } else {
  368. stage_id_listSql = ' And sid in (' + this.db.escape(stage_id_list) + ')';
  369. }
  370. }
  371. const sql = 'SELECT `lid`, `tid`, `sid`, SUM(`contract_qty`) as `contract_qty`, SUM(`contract_tp`) as `contract_tp`, ' +
  372. 'SUM(`qc_qty`) as `qc_qty`, SUM(`qc_tp`) as `qc_tp` FROM ' + this.tableName + ' WHERE `tid` = ? ' +
  373. stage_id_listSql + ' GROUP BY `lid`';
  374. const sqlParam = [tid];
  375. const result = await this.db.query(sql, sqlParam);
  376. return result;
  377. }
  378. /**
  379. * 获取多期(合同和数量变更相加)计量-小计(材料调差调用)
  380. * @param {Number} tid - 标段id
  381. * @param {String} stage_id_list - 期id列表
  382. * @param {String} lid - 台账id
  383. * @param {String} xid - 项目节id
  384. * @returns {Promise<void>}
  385. */
  386. async getGatherQtyByMaterial(tid, stage_id_list, lid) {
  387. stage_id_list = stage_id_list !== null ? stage_id_list.split(',') : [];
  388. let gather_qty = 0;
  389. for (const sid of stage_id_list) {
  390. const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills ' +
  391. ' INNER JOIN ( ' +
  392. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
  393. ' WHERE tid = ? And sid = ?' +
  394. ' GROUP BY `lid`' +
  395. ' ) As MaxFilter ' +
  396. ' ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`' +
  397. ' WHERE Bills.lid = ?';
  398. const sqlParam = [tid, sid, lid];
  399. const result = await this.db.queryOne(sql, sqlParam);
  400. gather_qty = this.ctx.helper.add(gather_qty, this.ctx.helper.add(result.contract_qty, result.qc_qty));
  401. }
  402. return gather_qty !== 0 ? gather_qty : null;
  403. }
  404. }
  405. return StageBills;
  406. };