stage_change.js 26 KB

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