stage_change.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 sql = 'SELECT c.*, ' +
  323. ' 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`,' +
  324. ' 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`, ' +
  325. ' l.drawing_code As `l_drawing_code`, ' +
  326. ' p.name As `p_name`, p.drawing_code As `p_drawing_code`, p.`quantity` As `p_qty`' +
  327. ' FROM ' + this.tableName + ' As c ' +
  328. ' INNER JOIN ( ' +
  329. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, `lid`, `pid`, `cbid`, `sid` From ' + this.tableName +
  330. ' WHERE sid = ? And cid = ?' +
  331. ' GROUP By `lid`, `pid`, `cbid`, `no_value`' +
  332. ' ) As m ' +
  333. ' 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' +
  334. ' LEFT JOIN ' + this.ctx.service.ledger.tableName + ' As l ON c.lid = l.id' +
  335. ' LEFT JOIN ' + this.ctx.service.pos.tableName + ' As p ON c.pid = p.id';
  336. const sqlParam = [sid, cid];
  337. return await this.db.query(sql, sqlParam);
  338. }
  339. /**
  340. * 获取 本期 使用的变更令
  341. * @param sid
  342. * @return {Promise<void>}
  343. */
  344. async getStageUsedChangeId(sid) {
  345. const sql = 'SELECT lid, pid, cid, cbid, qty, stimes, sorder FROM ' + this.tableName + ' WHERE sid = ?';
  346. const curAll = await this.db.query(sql, [sid]);
  347. const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  348. return this._.map(this._.filter(cur, 'qty'), 'cid');
  349. }
  350. async getFinalStageData(tid, sid) {
  351. const data = await this.getAllDataByCondition({ where: { tid, sid } });
  352. return this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  353. }
  354. async getSumLoadFinalData(sid) {
  355. 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' +
  356. ' FROM ' + this.tableName + ' cf' +
  357. ' Left Join ' + this.ctx.service.change.tableName + ' c ON cf.cid = c.cid' +
  358. ' Where cf.sid = ?';
  359. const result = await this.db.query(sql, [sid]);
  360. return this.ctx.helper.filterLastestData(result, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  361. }
  362. async _getTender(stage) {
  363. if (this.ctx.tender) return this.ctx.tender;
  364. const tender = { id: stage.tid };
  365. tender.data = await this.ctx.service.tender.getTender(stage.tid);
  366. tender.info = await this.service.tenderInfo.getTenderInfo(tender.id);
  367. return tender;
  368. }
  369. async getSubtotal(stage) {
  370. const helper = this.ctx.helper;
  371. const tender = await this._getTender(stage);
  372. const sql = 'SELECT sc.*, c.quality FROM ' + this.tableName + ' sc' +
  373. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON sc.cid = c.cid' +
  374. ' WHERE sid = ? ' + (stage.readOnly ? ` and (stimes < ${stage.curTimes} or (stimes = ${stage.curTimes} and sorder <= ${stage.curOrder}))` : '');
  375. let data = await this.db.query(sql, [stage.id]);
  376. data = helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  377. const bqData = [];
  378. for (const d of data) {
  379. if (!d.qty) continue;
  380. let bd = bqData.find(x => { return x.lid === d.lid && x.quality === d.quality; });
  381. if (!bd) {
  382. const bills = await this.db.get(this.ctx.service.ledger.departTableName(tender.id), { id: d.lid });
  383. if (!bills) continue;
  384. bd = { lid: d.lid, quality: d.quality, unit_price: bills.unit_price };
  385. bqData.push(bd);
  386. }
  387. const tp = this.ctx.helper.mul(d.qty, bd.unit_price, tender.info.decimal.tp);
  388. bd.tp = this.ctx.helper.add(bd.tp, tp);
  389. }
  390. const result = {};
  391. result.common = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.common.value; }), 'tp'));
  392. result.more = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.more.value; }), 'tp'));
  393. result.great = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.great.value; }), 'tp'));
  394. return result;
  395. }
  396. async _getChangeBillsWithUsedInfo(stage) {
  397. if (stage.status === audit.stage.status.checked) {
  398. const sql = 'SELECT scf.* ' +
  399. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  400. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  401. ' WHERE scf.tid = ? And s.order <= ?';
  402. const result = await this.db.query(sql, [stage.tid, stage.order]);
  403. return result;
  404. } else {
  405. const preSql = 'SELECT scf.* ' +
  406. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  407. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  408. ' WHERE scf.tid = ? And s.order < ?';
  409. const pre = await this.db.query(preSql, [stage.tid, stage.order]);
  410. const sql = 'SELECT * FROM ' + this.tableName + ' WHERE sid = ? AND (stimes * 100 + sorder) <= (? * 100 + ?)';
  411. const curAll = await this.db.query(sql, [stage.id, stage.curTimes, stage.curOrder]);
  412. const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  413. return [...pre, ...cur];
  414. }
  415. }
  416. async getChangeWithUsedInfo(stage) {
  417. const change = await this.ctx.service.change.getAllDataByCondition({
  418. where: { tid: stage.tid, status: audit.flow.status.checked },
  419. orders: [['code', 'asc']],
  420. });
  421. if (change.length === 0) return [];
  422. const changeBills = await this.ctx.service.changeAuditList.getAllDataByCondition({
  423. where: { cid: change.map(x => { return x.cid; }) }
  424. });
  425. const changeBillsIndex = {}, changeBillsPart = {};
  426. for (const cb of changeBills) {
  427. changeBillsIndex[cb.id] = cb;
  428. if (!changeBillsPart[cb.cid]) changeBillsPart[cb.cid] = [];
  429. changeBillsPart[cb.cid].push(cb);
  430. }
  431. const stageChangeBills = await this._getChangeBillsWithUsedInfo(stage);
  432. for (const scb of stageChangeBills) {
  433. if (!scb.qty) continue;
  434. const cb = changeBillsIndex[scb.cbid];
  435. if (cb) cb.used_qty = this.ctx.helper.add(cb.used_qty, scb.qty);
  436. }
  437. for (const cid in changeBillsPart) {
  438. const c = change.find(x => { return x.cid === cid });
  439. if (!c) continue;
  440. for (const cb of changeBillsPart[cid]) {
  441. cb.tp = this.ctx.helper.mul(cb.spamount, cb.unit_price, c.tp_decimal || this.ctx.tender.info.decimal.tp);
  442. cb.used_tp = this.ctx.helper.mul(cb.used_qty, cb.unit_price, this.ctx.tender.info.decimal.tp);
  443. c.used_tp = this.ctx.helper.add(c.used_tp, cb.used_tp);
  444. if (cb.spamount > 0) {
  445. c.p_tp = this.ctx.helper.add(c.p_tp, cb.tp);
  446. c.p_used_tp = this.ctx.helper.add(c.p_used_tp, cb.used_tp);
  447. } else if (cb.spamount < 0){
  448. c.n_tp = this.ctx.helper.add(c.n_tp, cb.tp);
  449. c.n_used_tp = this.ctx.helper.add(c.n_used_tp, cb.used_tp);
  450. }
  451. }
  452. c.used_pt = c.total_price ? this.ctx.helper.mul(this.ctx.helper.div(c.used_tp, c.total_price, 4), 100) : 0;
  453. 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;
  454. 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;
  455. }
  456. return change;
  457. }
  458. async getStageMinusChange(stage) {
  459. const data = await this.getAllDataByCondition({ where: { sid: stage.id, minus: 1 } });
  460. return this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  461. };
  462. async getBillsMinusQty(stage, lid) {
  463. const data = await this.getAllDataByCondition({ where: { sid: stage.id, lid, minus: 1 } });
  464. const filter = this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  465. return { lid, qty: this.ctx.helper.sum(filter.map(x => { return x.qty; })) };
  466. };
  467. async getPosMinusQty(stage, pid) {
  468. const data = await this.getAllDataByCondition({ where: { sid: stage.id, pid, minus: 1 } });
  469. const filter = this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  470. return { pid, qty: this.ctx.helper.sum(filter.map(x => { return x.qty; })) };
  471. };
  472. }
  473. return StageChange;
  474. };