stage_change.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. 'use strict';
  2. /**
  3. * 期 - 变更数据
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const defaultPid = -1; // 非pid
  10. const audit = require('../const/audit');
  11. const timesLen = audit.stage.timesLen;
  12. const changeConst = require('../const/change');
  13. module.exports = app => {
  14. class StageChange extends app.BaseService {
  15. /**
  16. * 构造函数
  17. *
  18. * @param {Object} ctx - egg全局变量
  19. * @return {void}
  20. */
  21. constructor(ctx) {
  22. super(ctx);
  23. this.tableName = 'stage_change';
  24. }
  25. /**
  26. * 查询 调用的变更令 最新数据
  27. * @param {Number} tid - 标段id
  28. * @param {Number} sid - 期id
  29. * @param {Number} lid - 台账节点id
  30. * @param {Number} pid - 部位明细id
  31. * @return {Promise<*>}
  32. */
  33. async getLastestStageData(tid, sid, lid, pid, noValue) {
  34. const filter = noValue !== undefined ? ' And no_value = ?' : '';
  35. const sql = 'SELECT c.*,' +
  36. ' oc.p_code As c_code, oc.new_code As c_new_code' +
  37. ' FROM ' + this.tableName + ' As c ' +
  38. ' INNER JOIN ( ' +
  39. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `progress`, `lid`, `pid`, `sid`, `cid`, `cbid`, `no_value` From ' + this.tableName +
  40. ' WHERE tid = ? And sid = ? And lid = ? And pid = ?' + filter +
  41. ' GROUP By `lid`, `pid`, `cid`, `cbid`, `no_value`' +
  42. ' ) As m ' +
  43. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.progress And c.lid = m.lid And c.pid = m.pid And c.`sid` = m.`sid` And c.`cid` = m.`cid` And c.`cbid` = m.`cbid` And c.`no_value` = m.`no_value`' +
  44. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' As oc' +
  45. ' ON c.cid = oc.cid' +
  46. ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' As ocb' +
  47. ' ON c.cbid = ocb.id' +
  48. ' WHERE not ISNULL(ocb.id)';
  49. const sqlParam = [tid, sid, lid, pid ? pid : -1];
  50. if (noValue !== undefined) sqlParam.push(noValue);
  51. return await this.db.query(sql, sqlParam);
  52. }
  53. /**
  54. * 查询 调用的变更令 某轮 某人的数据
  55. * @param {Number} tid - 标段id
  56. * @param {Number} sid - 期id
  57. * @param {Number} times - 第几轮
  58. * @param {Number} order - 第几人
  59. * @param {Number} lid - 台账节点id
  60. * @param {Number} pid - 部位明细id
  61. * @return {Promise<*>}
  62. */
  63. async getAuditorStageData(tid, sid, times, order, lid, pid, noValue) {
  64. const filter = noValue !== undefined ? ' And no_value = ?' : '';
  65. const sql = 'SELECT c.*,' +
  66. ' oc.p_code As c_code, oc.new_code As c_new_code, oc.quality, ocb.code as b_code, ocb.name, ocb.unit' +
  67. ' FROM ' + this.tableName + ' As c ' +
  68. ' INNER JOIN ( ' +
  69. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `progress`, `lid`, `pid`, `sid`, `cid`, `cbid`, `no_value` From ' + this.tableName +
  70. ' WHERE tid = ? And sid = ? And (`stimes` < ? OR (`stimes` = ? AND `sorder` <= ?)) And lid = ? And pid = ?' + filter +
  71. ' GROUP By `lid`, `pid`, cid, cbid, no_value' +
  72. ' ) As m ' +
  73. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.progress And c.lid = m.lid And c.pid = m.pid And c.`sid` = m.`sid` And c.`cid` = m.`cid` And c.`cbid` = m.`cbid` And c.`no_value` = m.`no_value`' +
  74. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' As oc' +
  75. ' ON c.cid = oc.cid' +
  76. ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' As ocb' +
  77. ' ON c.cbid = ocb.id' +
  78. ' WHERE not ISNULL(ocb.id)';
  79. const sqlParam = [tid, sid, times, times, order, lid, pid ? pid : -1];
  80. if (noValue !== undefined) sqlParam.push(noValue);
  81. return await this.db.query(sql, sqlParam);
  82. }
  83. async getLastestAllStageData(tid, sid) {
  84. const sql = 'SELECT c.*,' +
  85. ' oc.p_code As c_code, oc.new_code As c_new_code, oc.quality, ocb.code as b_code, ocb.name, ocb.unit' +
  86. ' FROM ' + this.tableName + ' As c ' +
  87. ' INNER JOIN ( ' +
  88. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `progress`, `lid`, `pid`, `sid`, `cid`, `cbid`, `no_value` From ' + this.tableName +
  89. ' WHERE tid = ? And sid = ?' +
  90. ' GROUP By `lid`, `pid`, cid, cbid, no_value' +
  91. ' ) As m ' +
  92. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.progress And c.lid = m.lid And c.pid = m.pid And c.`sid` = m.`sid` And c.`cid` = m.`cid` And c.`cbid` = m.`cbid` And c.`no_value` = m.`no_value`' +
  93. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' As oc' +
  94. ' ON c.cid = oc.cid' +
  95. ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' As ocb' +
  96. ' ON c.cbid = ocb.id' +
  97. ' WHERE not ISNULL(ocb.id)';
  98. const sqlParam = [tid, sid];
  99. return await this.db.query(sql, sqlParam);
  100. }
  101. async getAuditorAllStageData(tid, sid, times, order) {
  102. const sql = 'SELECT c.*, ' +
  103. ' oc.p_code As c_code, oc.new_code As c_new_code, oc.quality, ocb.code as b_code, ocb.name, ocb.unit' +
  104. ' FROM ' + this.tableName + ' As c ' +
  105. ' INNER JOIN ( ' +
  106. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `progress`, `lid`, `pid`, `sid`, `cid`, `cbid`, `no_value` From ' + this.tableName +
  107. ' WHERE tid = ? And sid = ? And (`stimes` < ? OR (`stimes` = ? AND `sorder` <= ?))' +
  108. ' GROUP By `lid`, `pid`, cid, cbid, no_value' +
  109. ' ) As m ' +
  110. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.progress And c.lid = m.lid And c.pid = m.pid And c.`sid` = m.`sid` And c.`cid` = m.`cid` And c.`cbid` = m.`cbid` And c.`no_value` = m.`no_value`' +
  111. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' As oc' +
  112. ' ON c.cid = oc.cid' +
  113. ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' As ocb' +
  114. ' ON c.cbid = ocb.id' +
  115. ' WHERE not ISNULL(ocb.id)';
  116. const sqlParam = [tid, sid, times, times, order];
  117. return await this.db.query(sql, sqlParam);
  118. }
  119. /**
  120. * 台账,调用变更令
  121. *
  122. * @param {Object} bills - 台账节点数据
  123. * @param {Array} changes - 调用的变更令
  124. * @return {Promise<void>}
  125. */
  126. async billsChange(bills, noValue, changes) {
  127. const self = this;
  128. function getNewChange(cid, cbid, times, order, qty, minus, no_value) {
  129. return {
  130. tid: self.ctx.tender.id, sid: self.ctx.stage.id,
  131. lid: bills.id, pid: -1,
  132. cid, cbid,
  133. stimes: times, sorder: order,
  134. qty, minus, no_value,
  135. };
  136. }
  137. const ledgerBills = await this.ctx.service.ledger.getCompleteDataById(bills.id);
  138. if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
  139. throw '提交数据错误';
  140. }
  141. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  142. // 获取原变更令
  143. const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id, -1, noValue);
  144. // 获取更新数据
  145. const updateChanges = [],
  146. newChanges = [];
  147. let billsQty = 0;
  148. for (const oc of oldChanges) {
  149. const nc = this._.find(changes, { cid: oc.cid, cbid: oc.cbid });
  150. if (!nc || nc.qty !== oc.qty) {
  151. const qty = nc ? this.round(nc.qty, precision.value) : null;
  152. const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty, nc ? nc.minus : oc.minus, noValue);
  153. billsQty = this.ctx.helper.add(billsQty, change.qty);
  154. if (oc.stimes === this.ctx.stage.curTimes && oc.sorder === this.ctx.stage.curOrder) {
  155. change.id = oc.id;
  156. updateChanges.push(change);
  157. } else {
  158. newChanges.push(change);
  159. }
  160. } else {
  161. billsQty = this.ctx.helper.add(billsQty, oc.qty);
  162. }
  163. }
  164. for (const c of changes) {
  165. const nc = this._.find(oldChanges, { cid: c.cid, cbid: c.cbid });
  166. if (!nc) {
  167. const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value), c.minus, noValue);
  168. billsQty = this.ctx.helper.add(billsQty, change.qty);
  169. newChanges.push(change);
  170. }
  171. }
  172. // 更新数据
  173. const transaction = await this.db.beginTransaction();
  174. try {
  175. if (newChanges.length > 0) {
  176. await transaction.insert(this.tableName, newChanges);
  177. }
  178. for (const c of updateChanges) {
  179. await transaction.update(this.tableName, c);
  180. }
  181. const stageBills = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, bills.id);
  182. const sbUpdate = noValue ? {qc_minus_qty: billsQty} : {qc_qty: billsQty};
  183. await this.ctx.service.stageBills.updateStageBillsQty(transaction, ledgerBills, stageBills, sbUpdate);
  184. await transaction.commit();
  185. } catch (err) {
  186. await transaction.rollback();
  187. throw err;
  188. }
  189. const result = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, [bills.id]);
  190. return { bills: { curStageData: result } };
  191. }
  192. /**
  193. * 部位明细,调用变更令
  194. *
  195. * @param {Object} pos - 部位明细数据
  196. * @param {Array} changes - 调用的变更令
  197. * @return {Promise<{}>}
  198. */
  199. async posChange(pos, noValue, changes) {
  200. const self = this;
  201. function getNewChange(cid, cbid, times, order, qty, minus, no_value) {
  202. return {
  203. tid: self.ctx.tender.id, sid: self.ctx.stage.id,
  204. lid: pos.lid, pid: pos.id,
  205. cid, cbid,
  206. stimes: times, sorder: order,
  207. qty, minus, no_value,
  208. };
  209. }
  210. const ledgerBills = await this.ctx.service.ledger.getCompleteDataById(pos.lid);
  211. if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
  212. throw '提交数据错误';
  213. }
  214. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  215. // 获取原变更令
  216. const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, pos.lid, pos.id, noValue);
  217. const updateChanges = [],
  218. newChanges = [];
  219. let posQty = 0;
  220. for (const oc of oldChanges) {
  221. const nc = this._.find(changes, { cid: oc.cid, cbid: oc.cbid });
  222. if (!nc || nc.qty !== oc.qty) {
  223. const qty = nc ? this.round(nc.qty, precision.value) : null;
  224. const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty, nc ? nc.minus : oc.minus, noValue);
  225. posQty = this.ctx.helper.add(posQty, change.qty);
  226. if (oc.stimes === this.ctx.stage.curTimes && oc.sorder === this.ctx.stage.curOrder) {
  227. change.id = oc.id;
  228. updateChanges.push(change);
  229. } else {
  230. newChanges.push(change);
  231. }
  232. } else {
  233. posQty = this.ctx.helper.add(posQty, oc.qty);
  234. }
  235. }
  236. for (const c of changes) {
  237. const nc = this._.find(oldChanges, { cid: c.cid, cbid: c.cbid });
  238. if (!nc) {
  239. const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value), c.minus, noValue);
  240. posQty = this.ctx.helper.add(posQty, change.qty);
  241. newChanges.push(change);
  242. }
  243. }
  244. // 更新数据
  245. const transaction = await this.db.beginTransaction();
  246. try {
  247. if (newChanges.length > 0) await transaction.insert(this.tableName, newChanges);
  248. if (updateChanges.length > 0) await transaction.updateRows(this.tableName, updateChanges);
  249. await this.ctx.service.stagePos.updateChangeQuantity(transaction, pos, posQty, noValue);
  250. await transaction.commit();
  251. } catch (err) {
  252. await transaction.rollback();
  253. throw err;
  254. }
  255. // 获取返回数据
  256. try {
  257. const data = { bills: {}, pos: {} };
  258. data.bills.curStageData = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, [pos.lid]);
  259. data.pos.curStageData = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, { pid: pos.id });
  260. return data;
  261. } catch (err) {
  262. throw '获取数据错误,请刷新页面';
  263. }
  264. }
  265. /**
  266. * 获取 变更令 - 变更清单 使用情况
  267. * @param {Number} sid - 查询期id
  268. * @param {uuid} cid - 变更令id
  269. * @return {Promise<void>}
  270. */
  271. async getUsedData(tid, cid) {
  272. if (this.ctx.stage.status === audit.stage.status.checked) {
  273. const sql = 'SELECT scf.* ' +
  274. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  275. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  276. ' WHERE scf.tid = ? And scf.cid = ? And s.order <= ?';
  277. const result = await this.db.query(sql, [tid, cid, this.ctx.stage.order]);
  278. return result;
  279. } else {
  280. const preSql = 'SELECT scf.* ' +
  281. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  282. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  283. ' WHERE scf.tid = ? And scf.cid = ? And s.order < ?';
  284. const pre = await this.db.query(preSql, [tid, cid, this.ctx.stage.order]);
  285. const sql = 'SELECT * FROM ' + this.tableName + ' WHERE sid = ?';
  286. const curAll = await this.db.query(sql, [this.ctx.stage.id]);
  287. const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  288. return [...pre, ...cur];
  289. }
  290. }
  291. async getFinalUsedData(tid, cid) {
  292. const stage = await this.ctx.service.stage.getLastestStage(tid, true);
  293. if (!stage) { // 防止未创建期时调用
  294. return [];
  295. }
  296. if (stage.status === audit.stage.status.checked) {
  297. const sql = 'SELECT scf.* ' +
  298. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  299. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  300. ' WHERE scf.tid = ? And scf.cid = ? And s.order <= ?';
  301. const result = await this.db.query(sql, [tid, cid, stage.order]);
  302. return result;
  303. } else {
  304. const preSql = 'SELECT scf.* ' +
  305. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  306. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  307. ' WHERE scf.tid = ? And scf.cid = ? And s.order < ?';
  308. const pre = await this.db.query(preSql, [tid, cid, stage.order]);
  309. const sql = 'SELECT * FROM ' + this.tableName + ' WHERE sid = ? and cid = ?';
  310. const curAll = await this.db.query(sql, [stage.id, cid]);
  311. const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  312. return [...pre, ...cur];
  313. }
  314. }
  315. /**
  316. * 获取 变更令 - 变更清单 当期使用情况
  317. * @param {Number} sid - 查询期id
  318. * @param {uuid} cid - 变更令id
  319. * @return {Promise<*>}
  320. */
  321. async getStageUsedData(sid, cid) {
  322. const data = await this.getAllDataByCondition({ where: { sid, cid } });
  323. const _ = this.ctx.helper._;
  324. const filter = this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  325. if (filter.length === 0) return filter;
  326. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  327. where: { id: _.uniq(_.map(filter, 'lid')) }
  328. });
  329. const pos = await this.ctx.service.pos.getAllDataByCondition({
  330. where: { id: _.uniq(_.map(filter, 'pid')) }
  331. });
  332. return filter.map(x => {
  333. const b = bills.find(y => { return y.id === x.lid });
  334. const p = pos.find(y => { return y.id === x.pid });
  335. return {
  336. ...x,
  337. ledger_id: b.ledger_id, l_code: b.b_code, l_name: b.name, l_unit: b.unit, l_up: b.unit_price,
  338. l_deal_qty: b.deal_qty, l_deal_tp: b.deal_tp, l_qty: b.quantity, l_tp: b.total_price, l_drawing_code: b.drawing_code,
  339. p_name: p ? p.name : '', p_drawing_code: p ? p.drawing_code : '', p_qty: p ? p.quantity : '',
  340. };
  341. });
  342. }
  343. /**
  344. * 获取 本期 使用的变更令
  345. * @param sid
  346. * @return {Promise<void>}
  347. */
  348. async getStageUsedChangeId(sid) {
  349. const sql = 'SELECT lid, pid, cid, cbid, qty, stimes, sorder FROM ' + this.tableName + ' WHERE sid = ?';
  350. const curAll = await this.db.query(sql, [sid]);
  351. const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  352. return this._.map(this._.filter(cur, 'qty'), 'cid');
  353. }
  354. async getFinalStageData(tid, sid) {
  355. const data = await this.getAllDataByCondition({ where: { tid, sid } });
  356. return this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  357. }
  358. async getSumLoadFinalData(sid) {
  359. const sql = 'Select cf.tid, cf.sid, cf.lid, cf.pid, cf.cid, cf.cbid, cf.qty, cf.stimes, cf.sorder, c.code As c_code' +
  360. ' FROM ' + this.tableName + ' cf' +
  361. ' Left Join ' + this.ctx.service.change.tableName + ' c ON cf.cid = c.cid' +
  362. ' Where cf.sid = ?';
  363. const result = await this.db.query(sql, [sid]);
  364. return this.ctx.helper.filterLastestData(result, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  365. }
  366. async _getTender(stage) {
  367. if (this.ctx.tender) return this.ctx.tender;
  368. const tender = { id: stage.tid };
  369. tender.data = await this.ctx.service.tender.getTender(stage.tid);
  370. tender.info = await this.service.tenderInfo.getTenderInfo(tender.id);
  371. return tender;
  372. }
  373. async getSubtotal(stage) {
  374. const helper = this.ctx.helper;
  375. const tender = await this._getTender(stage);
  376. const sql = 'SELECT sc.*, c.quality FROM ' + this.tableName + ' sc' +
  377. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON sc.cid = c.cid' +
  378. ' WHERE sid = ? ' + (stage.readOnly ? ` and (stimes < ${stage.curTimes} or (stimes = ${stage.curTimes} and sorder <= ${stage.curOrder}))` : '');
  379. let data = await this.db.query(sql, [stage.id]);
  380. data = helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  381. const bqData = [];
  382. for (const d of data) {
  383. if (!d.qty) continue;
  384. let bd = bqData.find(x => { return x.lid === d.lid && x.quality === d.quality; });
  385. if (!bd) {
  386. const bills = await this.db.get(this.ctx.service.ledger.departTableName(tender.id), { id: d.lid });
  387. if (!bills) continue;
  388. bd = { lid: d.lid, quality: d.quality, unit_price: bills.unit_price };
  389. bqData.push(bd);
  390. }
  391. const tp = this.ctx.helper.mul(d.qty, bd.unit_price, tender.info.decimal.tp);
  392. bd.tp = this.ctx.helper.add(bd.tp, tp);
  393. }
  394. const result = {};
  395. result.common = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.common.value; }), 'tp'));
  396. result.more = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.more.value; }), 'tp'));
  397. result.great = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.great.value; }), 'tp'));
  398. return result;
  399. }
  400. async _getChangeBillsWithUsedInfo(stage) {
  401. if (stage.status === audit.stage.status.checked) {
  402. const sql = 'SELECT scf.* ' +
  403. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  404. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  405. ' WHERE scf.tid = ? And s.order <= ?';
  406. const result = await this.db.query(sql, [stage.tid, stage.order]);
  407. return result;
  408. } else {
  409. const preSql = 'SELECT scf.* ' +
  410. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  411. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  412. ' WHERE scf.tid = ? And s.order < ?';
  413. const pre = await this.db.query(preSql, [stage.tid, stage.order]);
  414. const sql = 'SELECT * FROM ' + this.tableName + ' WHERE sid = ? AND (stimes * 100 + sorder) <= (? * 100 + ?)';
  415. const curAll = await this.db.query(sql, [stage.id, stage.curTimes, stage.curOrder]);
  416. const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  417. return [...pre, ...cur];
  418. }
  419. }
  420. async getChangeWithUsedInfo(stage) {
  421. const change = await this.ctx.service.change.getAllDataByCondition({
  422. where: { tid: stage.tid, status: audit.flow.status.checked },
  423. orders: [['code', 'asc']],
  424. });
  425. if (change.length === 0) return [];
  426. const changeBills = await this.ctx.service.changeAuditList.getAllDataByCondition({
  427. where: { cid: change.map(x => { return x.cid; }) }
  428. });
  429. const changeBillsIndex = {}, changeBillsPart = {};
  430. for (const cb of changeBills) {
  431. changeBillsIndex[cb.id] = cb;
  432. if (!changeBillsPart[cb.cid]) changeBillsPart[cb.cid] = [];
  433. changeBillsPart[cb.cid].push(cb);
  434. }
  435. const stageChangeBills = await this._getChangeBillsWithUsedInfo(stage);
  436. for (const scb of stageChangeBills) {
  437. if (!scb.qty) continue;
  438. const cb = changeBillsIndex[scb.cbid];
  439. if (cb) cb.used_qty = this.ctx.helper.add(cb.used_qty, scb.qty);
  440. }
  441. for (const cid in changeBillsPart) {
  442. const c = change.find(x => { return x.cid === cid });
  443. if (!c) continue;
  444. for (const cb of changeBillsPart[cid]) {
  445. cb.tp = this.ctx.helper.mul(cb.spamount, cb.unit_price, c.tp_decimal || this.ctx.tender.info.decimal.tp);
  446. cb.used_tp = this.ctx.helper.mul(cb.used_qty, cb.unit_price, this.ctx.tender.info.decimal.tp);
  447. c.used_tp = this.ctx.helper.add(c.used_tp, cb.used_tp);
  448. if (cb.spamount > 0) {
  449. c.p_tp = this.ctx.helper.add(c.p_tp, cb.tp);
  450. c.p_used_tp = this.ctx.helper.add(c.p_used_tp, cb.used_tp);
  451. } else if (cb.spamount < 0){
  452. c.n_tp = this.ctx.helper.add(c.n_tp, cb.tp);
  453. c.n_used_tp = this.ctx.helper.add(c.n_used_tp, cb.used_tp);
  454. }
  455. }
  456. c.used_pt = c.total_price ? this.ctx.helper.mul(this.ctx.helper.div(c.used_tp, c.total_price, 4), 100) : 0;
  457. c.p_used_pt = c.p_tp ? this.ctx.helper.mul(this.ctx.helper.div(c.p_used_tp, c.p_tp, 4), 100) : 0;
  458. c.n_used_pt = c.n_tp ? this.ctx.helper.mul(this.ctx.helper.div(c.n_used_tp, c.n_tp, 4), 100) : 0;
  459. }
  460. return change;
  461. }
  462. async getStageMinusChange(stage) {
  463. const data = await this.getAllDataByCondition({ where: { sid: stage.id, minus: 1 } });
  464. return this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  465. };
  466. async getBillsMinusQty(stage, lid) {
  467. const data = await this.getAllDataByCondition({ where: { sid: stage.id, lid, minus: 1 } });
  468. const filter = this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  469. return { lid, qty: this.ctx.helper.sum(filter.map(x => { return x.qty; })) };
  470. };
  471. async getPosMinusQty(stage, pid) {
  472. const data = await this.getAllDataByCondition({ where: { sid: stage.id, pid, minus: 1 } });
  473. const filter = this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  474. return { pid, qty: this.ctx.helper.sum(filter.map(x => { return x.qty; })) };
  475. };
  476. }
  477. return StageChange;
  478. };