stage_pos.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const measureType = require('../const/tender').measureType;
  10. const timesLen = require('../const/audit').stage.timesLen;
  11. module.exports = app => {
  12. class StagePos extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.depart = ctx.app.config.table_depart.heavy;
  22. this.tableName = 'stage_pos';
  23. this.qtyFields = ['contract_qty', 'qc_qty', 'qc_minus_qty', 'positive_qc_qty', 'negative_qc_qty', 'ex_stage_qty1']
  24. }
  25. _getPosFilterSql(where, asTable = '') {
  26. let whereSql = '';
  27. if (!where) return whereSql;
  28. if (where.pid) {
  29. if (where.pid instanceof Array) {
  30. whereSql += ' And ' + asTable + 'pid in (' + this.ctx.helper.getInArrStrSqlFilter(where.pid) + ')';
  31. } else if (typeof where.pid === "string") {
  32. whereSql += ' And ' + asTable + 'pid = ' + this.db.escape(where.pid);
  33. }
  34. }
  35. if (where.lid) {
  36. if (where.lid instanceof Array) {
  37. whereSql += ' And ' + asTable + 'lid in (' + this.ctx.helper.getInArrStrSqlFilter(where.lid) + ')';
  38. } else if (typeof where.lid === "string") {
  39. whereSql += ' And ' + asTable + 'lid = ' + this.db.escape(where.lid);
  40. }
  41. }
  42. return whereSql;
  43. }
  44. _filterLastestData(stagePos) {
  45. const stagePosIndex = {};
  46. for (const sp of stagePos) {
  47. const key = 'sp-' + sp.pid;
  48. const spi = stagePosIndex[key];
  49. if (spi) {
  50. if ((spi.times * timesLen + spi.order) < (sp.times * timesLen + sp.order)) stagePosIndex[key] = sp;
  51. } else {
  52. stagePosIndex[key] = sp;
  53. }
  54. }
  55. const result = [];
  56. for (const prop in stagePosIndex) {
  57. result.push(stagePosIndex[prop]);
  58. }
  59. return result;
  60. }
  61. async getLastestStageData2(tid, sid, where) {
  62. const filterSql = this._getPosFilterSql(where);
  63. const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, qc_minus_qty, positive_qc_qty, negative_qc_qty, postil, `times`, `order`, `contract_expr`, ex_stage_qty1' +
  64. ' FROM ' + this.departTableName(tid) +
  65. ' WHERE tid = ? And sid = ? ' + filterSql;
  66. const sqlParam = [tid, sid];
  67. const stagePos = await this.db.query(sql, sqlParam);
  68. return this._filterLastestData(stagePos);
  69. }
  70. async getAuditorStageData2(tid, sid, times, order, where) {
  71. const filterSql = this._getPosFilterSql(where);
  72. const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, qc_minus_qty, positive_qc_qty, negative_qc_qty, postil, `times`, `order`, `contract_expr`, ex_stage_qty1' +
  73. ' FROM ' + this.departTableName(tid) +
  74. ' WHERE tid = ? And sid = ? And (`times` < ? OR (`times` = ? AND `order` <= ?)) ' + filterSql;
  75. const sqlParam = [tid, sid, times, times, order];
  76. const stagePos = await this.db.query(sql, sqlParam);
  77. return this._filterLastestData(stagePos);
  78. }
  79. async updateStagePos4CheckCancel(sid, newTimes, newOrder, oldTimes, oldOrder, transaction) {
  80. const oldSBLists = await this.getAllDataByCondition({ where: { sid, times: oldTimes, order: oldOrder } });
  81. const newSBLists = await this.getAllDataByCondition({ where: { sid, times: newTimes, order: newOrder } });
  82. const delidList = [];
  83. for (const o of oldSBLists) {
  84. const newSBInfo = this._.find(newSBLists, { lid: o.lid, pid: o.pid });
  85. if (newSBInfo) {
  86. // 删除
  87. delidList.push(newSBInfo.id);
  88. }
  89. }
  90. if (delidList.length > 0) await transaction.delete(this.tableName, { id: delidList });
  91. if (oldSBLists.length > 0) {
  92. await transaction.update(this.tableName, {
  93. times: newTimes,
  94. order: newOrder,
  95. }, { where: { id: this._.map(oldSBLists, 'id') } });
  96. }
  97. }
  98. async getStageUsedPos(tid, sid, where) {
  99. const self = this;
  100. const stagePos = await this.getLastestStageData2(tid, sid, where);
  101. const pids = this._.map(stagePos, function (sp) {
  102. if (!self.ctx.helper.checkZero(sp.contract_qty) || !self.ctx.helper.checkZero(sp.qc_qty) || !self.ctx.helper.checkZero(sp.qc_minus_qty)) {
  103. return sp.pid;
  104. } else {
  105. return -1;
  106. }
  107. });
  108. return this._.pull(pids, -1);
  109. }
  110. /**
  111. * 新增部位明细数据(仅供updateStageData调用)
  112. *
  113. * @param transaction - 事务
  114. * @param data - 新增数据
  115. * @returns {Promise<{}>}
  116. * @private
  117. */
  118. async _addStagePosData(data) {
  119. let bills , precision;
  120. const result = {pos: [], ledger: []};
  121. const datas = data instanceof Array ? data : [data], calcStageBills = [];
  122. if (datas[0].sgfh_qty !== undefined || datas[0].sjcl_qty !== undefined || datas[0].qtcl_qty !== undefined
  123. || datas[0].contract_qty !== undefined || datas[0].qc_qty !== undefined || datas[0].real_qty !== undefined
  124. || datas[0].ex_qty1 !== undefined || datas[0].ex_stage_qty1 !== undefined
  125. ) {
  126. bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
  127. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  128. result.ledger.push(bills.id);
  129. }
  130. const insertPos = [], insertPosStage = [];
  131. for (const d of datas) {
  132. const p = {
  133. id: this.uuid.v4(), tid: this.ctx.tender.id, lid: d.lid, name: d.name, porder: d.porder, position: d.position,
  134. add_stage: this.ctx.stage.id, add_stage_order: this.ctx.stage.order,
  135. add_times: this.ctx.stage.curTimes,
  136. add_user: this.ctx.session.sessionUser.accountId,
  137. };
  138. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined
  139. || d.sgfh_expr !== undefined || d.sjcl_expr !== undefined || d.qtcl_expr !== undefined
  140. || d.ex_qty1 !== undefined
  141. ) {
  142. if (d.sgfh_qty!== undefined) p.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  143. if (d.sjcl_qty!== undefined) p.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  144. if (d.qtcl_qty!== undefined) p.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  145. if (d.sgfh_expr !== undefined) p.sgfh_expr = d.sgfh_expr;
  146. if (d.sjcl_expr !== undefined) p.sjcl_expr = d.sjcl_expr;
  147. if (d.qtcl_expr !== undefined) p.qtcl_expr = d.qtcl_expr;
  148. p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
  149. if (d.ex_qty1!== undefined) p.ex_qty1 = this.round(d.ex_qty1, precision.value);
  150. }
  151. if (d.real_qty!== undefined) p.real_qty = this.round(d.real_qty, precision.value);
  152. insertPos.push(p);
  153. result.pos.push(p.id);
  154. if (d.contract_qty!== undefined || d.qc_qty!== undefined || d.postil!== undefined || data.contract_expr !== undefined || d.ex_stage_qty1 !== undefined) {
  155. result.stageUpdate = true;
  156. const ps = {
  157. pid: p.id,
  158. lid: d.lid,
  159. tid: this.ctx.tender.id,
  160. sid: this.ctx.stage.id,
  161. said: this.ctx.session.sessionUser.accountId,
  162. times: this.ctx.stage.curTimes,
  163. order: this.ctx.stage.curOrder,
  164. };
  165. if (d.contract_qty !== undefined) ps.contract_qty = this.round(d.contract_qty, precision.value);
  166. if (d.contract_expr !== undefined) ps.contract_expr = d.contract_expr;
  167. if (d.postil!== undefined) ps.postil = d.postil;
  168. if (d.ex_stage_qty1 !== undefined) ps.ex_stage_qty1 = this.round(d.ex_stage_qty1, precision.value);
  169. insertPosStage.push(ps);
  170. if ((d.contract_qty || d.ex_stage_qty1) && calcStageBills.indexOf(ps.lid) === -1) {
  171. calcStageBills.push(ps.lid);
  172. }
  173. }
  174. }
  175. const transaction = await this.db.beginTransaction();
  176. try {
  177. if (insertPos.length > 0) await transaction.insert(this.ctx.service.pos.tableName, insertPos);
  178. if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
  179. for (const lid of calcStageBills) {
  180. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage, lid, transaction);
  181. }
  182. await transaction.commit();
  183. return result;
  184. } catch(err) {
  185. await transaction.rollback();
  186. throw err;
  187. }
  188. }
  189. /**
  190. * 更新部位明细数据(仅供updateStageData调用)
  191. *
  192. * @param transaction - 事务
  193. * @param data - 更新数据(允许一次性提交多条)
  194. * @returns {Promise<{ledger: Array, pos: Array}>}
  195. * @private
  196. */
  197. async _updateStagePosData(data) {
  198. let bills, precision, updateBills = null;
  199. const datas = data instanceof Array ? data : [data];
  200. const orgPos = await this.ctx.service.pos.getPosDataByIds(this.ctx.tender.id, this._.map(datas, 'pid'));
  201. const result = {ledger: [], pos: [], stageUpdate: true};
  202. const orgStagePos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id,
  203. {pid: this._.map(datas, 'pid')});
  204. if (datas[0].sgfh_qty !== undefined || datas[0].qtcl_qty !== undefined || datas[0].sjcl_qty !== undefined
  205. || datas[0].contract_qty !== undefined || datas[0].qc_qty !== undefined || datas[0].real_qty !== undefined
  206. || datas[0].ex_qty1 !== undefined || datas[0].ex_stage_qty1 !== undefined
  207. ) {
  208. bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
  209. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  210. result.ledger.push(bills.id);
  211. }
  212. const updatePos = [], updatePosStage = [], insertPosStage = [];
  213. for (const d of datas) {
  214. if (d.name !== undefined || d.drawing_code !== undefined || d.position !== undefined
  215. || d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined
  216. || d.real_qty !== undefined || d.ex_qty1 !== undefined
  217. || d.ex_memo1 !== undefined || d.ex_memo2 !== undefined || d.ex_memo3 !== undefined
  218. ) {
  219. const op = this._.find(orgPos, {id: d.pid});
  220. if (op.add_stage !== this.ctx.stage.id && (
  221. d.name !== undefined || d.drawing_code !== undefined || d.position !== undefined
  222. || d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined
  223. )) throw '不可修改数据';
  224. const p = {id: op.id};
  225. if (d.name !== undefined) p.name = d.name;
  226. if (d.position !== undefined) p.position = d.position;
  227. if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined || d.ex_qty1 !== undefined) {
  228. p.sgfh_qty = d.sgfh_qty !== undefined ? this.ctx.helper.round(d.sgfh_qty, precision.value) : op.sgfh_qty;
  229. p.sjcl_qty = d.sjcl_qty !== undefined ? this.ctx.helper.round(d.sjcl_qty, precision.value) : op.sjcl_qty;
  230. p.qtcl_qty = d.qtcl_qty !== undefined ? this.ctx.helper.round(d.qtcl_qty, precision.value) : op.qtcl_qty;
  231. if (d.sgfh_expr !== undefined) p.sgfh_expr = d.sgfh_expr;
  232. if (d.sjcl_expr !== undefined) p.sjcl_expr = d.sjcl_expr;
  233. if (d.qtcl_expr !== undefined) p.qtcl_expr = d.qtcl_expr;
  234. p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
  235. p.ex_qty1 = d.ex_qty1 !== undefined ? this.ctx.helper.round(d.ex_qty1, precision.value) : op.ex_qty1;
  236. if (!updateBills) updateBills = {id: bills.id};
  237. }
  238. if (d.drawing_code !== undefined) p.drawing_code = d.drawing_code;
  239. if (d.real_qty !== undefined) p.real_qty = this.ctx.helper.round(d.real_qty, precision.value);
  240. if (d.ex_memo1 !== undefined) p.ex_memo1 = d.ex_memo1;
  241. if (d.ex_memo2 !== undefined) p.ex_memo2 = d.ex_memo2;
  242. if (d.ex_memo3 !== undefined) p.ex_memo3 = d.ex_memo3;
  243. updatePos.push(p);
  244. }
  245. if (d.contract_qty !== undefined || d.qc_qty !== undefined || d.postil !== undefined || d.ex_stage_qty1 !== undefined) {
  246. const osp = this._.find(orgStagePos, function (p) { return p.pid === d.pid; });
  247. if (osp && osp.times === this.ctx.stage.curTimes && osp.order === this.ctx.stage.curOrder) {
  248. const sp = { id: osp.id, said: this.ctx.session.sessionUser.accountId };
  249. if (d.contract_qty !== undefined) {
  250. sp.contract_qty = this.ctx.helper.round(d.contract_qty, precision.value);
  251. }
  252. if (d.contract_expr !== undefined) sp.contract_expr = d.contract_expr;
  253. if (d.qc_qty !== undefined) {
  254. sp.qc_qty = this.ctx.helper.round(d.qc_qty, precision.value);
  255. }
  256. if (d.postil !== undefined) {
  257. sp.postil = d.postil;
  258. }
  259. if (d.ex_stage_qty1 !== undefined) sp.ex_stage_qty1 = this.ctx.helper.round(d.ex_stage_qty1, precision.value);
  260. updatePosStage.push(sp);
  261. } else {
  262. const sp = {
  263. pid: d.pid, lid: d.lid,
  264. tid: this.ctx.tender.id, sid: this.ctx.stage.id,
  265. said: this.ctx.session.sessionUser.accountId,
  266. times: this.ctx.stage.curTimes, order: this.ctx.stage.curOrder
  267. };
  268. if (d.contract_qty !== undefined) {
  269. sp.contract_qty = this.ctx.helper.round(d.contract_qty, precision.value) || 0;
  270. } else {
  271. sp.contract_qty = osp ? osp.contract_qty || 0 : 0;
  272. }
  273. if (d.contract_expr !== undefined) sp.contract_expr = d.contract_expr;
  274. if (d.postil || osp) {
  275. sp.postil = d.postil === undefined && osp ? osp.postil : d.postil;
  276. }
  277. if (d.ex_stage_qty1 !== undefined) {
  278. sp.ex_stage_qty1 = this.ctx.helper.round(d.ex_stage_qty1, precision.value) || 0;
  279. } else {
  280. sp.ex_stage_qty1 = osp ? osp.ex_stage_qty1 || 0 : 0;
  281. }
  282. sp.qc_qty = osp ? osp.qc_qty || 0 : 0;
  283. sp.qc_minus_qty = osp ? osp.qc_minus_qty || 0 : 0;
  284. sp.positive_qc_qty = osp ? osp.positive_qc_qty || 0 : 0;
  285. sp.negative_qc_qty = osp ? osp.negative_qc_qty || 0 : 0;
  286. insertPosStage.push(sp);
  287. }
  288. }
  289. result.pos.push(d.pid);
  290. }
  291. if (updateBills) {
  292. const billsPos = await this.ctx.service.pos.getAllDataByCondition({where: {tid: bills.tender_id, lid: bills.id} });
  293. for (const bp of billsPos) {
  294. const newPos = updatePos.find(function (x) { return x.id === bp.id });
  295. const calcData = newPos ? newPos : bp;
  296. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  297. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  298. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  299. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  300. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, calcData.ex_qty1);
  301. }
  302. const info = this.ctx.tender.info;
  303. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  304. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  305. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  306. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  307. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  308. }
  309. const transaction = await this.db.beginTransaction();
  310. try {
  311. if (updatePos.length > 0) await transaction.updateRows(this.ctx.service.pos.tableName, updatePos);
  312. if (updateBills) await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  313. if (updatePosStage.length > 0) await transaction.updateRows(this.tableName, updatePosStage);
  314. if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
  315. if (bills) await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage, bills.id, transaction);
  316. await transaction.commit();
  317. return result;
  318. } catch (err) {
  319. await transaction.rollback();
  320. throw err;
  321. }
  322. }
  323. /**
  324. * 更新部位明细数据(仅供updateStageData调用)
  325. *
  326. * @param transaction - 事务
  327. * @param data - 更新数据(允许一次性提交多条)
  328. * @returns {Promise<{ledger: Array, pos: Array}>}
  329. * @private
  330. */
  331. async _batchUpdateStagePosData(data) {
  332. const datas = data instanceof Array ? data : [data];
  333. const result = {
  334. ledger: this._.uniq(this._.map(datas, 'lid')),
  335. pos: this._.map(datas, 'pid'),
  336. stageUpdate: true
  337. };
  338. const orgStagePos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id,
  339. {pid: this._.map(datas, 'pid')});
  340. const bills = await this.ctx.service.ledger.getAllDataByCondition({where: {id: result.ledger}});
  341. for (const b of bills) {
  342. b.precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, b.unit);
  343. }
  344. const updatePosStage = [], insertPosStage = [];
  345. for (const d of datas) {
  346. const b = this._.find(bills, {id: d.lid});
  347. const osp = this._.find(orgStagePos, function (p) { return p.pid === d.pid; });
  348. if (osp && osp.times === this.ctx.stage.curTimes && osp.order === this.ctx.stage.curOrder) {
  349. const sp = { id: osp.id, pid: osp.pid, said: this.ctx.session.sessionUser.accountId };
  350. if (d.contract_qty !== undefined) {
  351. sp.contract_qty = this.ctx.helper.round(d.contract_qty, b.precision.value);
  352. }
  353. sp.contract_expr = d.contract_expr;
  354. updatePosStage.push(sp);
  355. } else {
  356. const sp = {
  357. pid: d.pid, lid: d.lid,
  358. tid: this.ctx.tender.id, sid: this.ctx.stage.id,
  359. said: this.ctx.session.sessionUser.accountId,
  360. times: this.ctx.stage.curTimes, order: this.ctx.stage.curOrder
  361. };
  362. if (d.contract_qty !== undefined || osp) {
  363. sp.contract_qty = d.contract_qty === undefined && osp
  364. ? osp.contract_qty
  365. : this.ctx.helper.round(d.contract_qty, b.precision.value);
  366. sp.contract_expr = d.contract_expr;
  367. if (osp) {
  368. sp.qc_qty = osp.qc_qty || 0;
  369. sp.qc_minus_qty = osp.qc_minus_qty || 0;
  370. sp.positive_qc_qty = osp.positive_qc_qty || 0;
  371. sp.negative_qc_qty = osp.positive_qc_qty || 0;
  372. sp.ex_stage_qty1 = osp.ex_stage_qty1 || 0;
  373. } else {
  374. sp.qc_qty = 0;
  375. sp.qc_minus_qty = 0;
  376. sp.positive_qc_qty = 0;
  377. sp.negative_qc_qty = 0;
  378. sp.ex_stage_qty1 = 0;
  379. }
  380. }
  381. insertPosStage.push(sp);
  382. }
  383. result.pos.push(d.pid);
  384. }
  385. const updateBillsStage = [], insertBillsStage = [], info = this.ctx.tender.info;
  386. for (const b of bills) {
  387. const stageBills = await this.ctx.service.stageBills.getLastestStageData2(b.tender_id, this.ctx.stage.id, b.id);
  388. const posStage = await this.getLastestStageData2(b.tender_id, this.ctx.stage.id, {lid: b.id});
  389. let contract_qty = 0;
  390. const newPosRange = insertPosStage.filter(x => {return x.lid === b.id});
  391. for (const nps of newPosRange) {
  392. contract_qty = this.ctx.helper.add(contract_qty, nps.contract_qty);
  393. }
  394. for (const ps of posStage) {
  395. const ips = this._.find(insertPosStage, {pid: ps.pid});
  396. if (!ips) {
  397. const ups = this._.find(updatePosStage, {id: ps.id});
  398. contract_qty = this.ctx.helper.add(contract_qty, ups ? ups.contract_qty : ps.contract_qty);
  399. }
  400. }
  401. if (stageBills && stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  402. if (contract_qty === stageBills.contract_qty) continue;
  403. const contract_tp = await this.ctx.service.stageBills.calcContractTp(this.ctx.tender.info, this.ctx.stage, b, { contract_qty, qc_minus_qty: stageBills.qc_minus_qty });
  404. updateBillsStage.push({
  405. id: stageBills.id,
  406. said: this.ctx.session.sessionUser.accountId,
  407. contract_qty: contract_qty, contract_tp,
  408. });
  409. } else {
  410. const contract_tp = await this.ctx.service.stageBills.calcContractTp(this.ctx.tender.info, this.ctx.stage, b, { contract_qty, qc_minus_qty: 0 });
  411. const ibs = {
  412. tid: this.ctx.tender.id,
  413. lid: b.id,
  414. sid: this.ctx.stage.id,
  415. times: this.ctx.stage.curTimes,
  416. order: this.ctx.stage.curOrder,
  417. said: this.ctx.session.sessionUser.accountId,
  418. contract_qty: contract_qty, contract_tp,
  419. };
  420. if (stageBills) {
  421. ibs.qc_qty = stageBills.qc_qty || 0;
  422. ibs.qc_tp = stageBills.qc_tp || 0;
  423. ibs.qc_minus_qty = stageBills.qc_minus_qty || 0;
  424. ibs.positive_qc_qty = stageBills.positive_qc_qty || 0;
  425. ibs.positive_qc_tp = stageBills.positive_qc_tp || 0;
  426. ibs.negative_qc_qty = stageBills.negative_qc_qty || 0;
  427. ibs.negative_qc_tp = stageBills.negative_qc_tp || 0;
  428. ibs.ex_stage_qty1 = stageBills.ex_stage_qty1 || 0;
  429. ibs.ex_stage_tp1 = stageBills.ex_stage_tp1 || 0;
  430. }
  431. insertBillsStage.push(ibs);
  432. }
  433. }
  434. const transaction = await this.db.beginTransaction();
  435. try {
  436. if (updatePosStage.length > 0) await transaction.updateRows(this.tableName, updatePosStage);
  437. if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
  438. if (updateBillsStage.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, updateBillsStage);
  439. if (insertBillsStage.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, insertBillsStage);
  440. await transaction.commit();
  441. return result;
  442. } catch (err) {
  443. await transaction.rollback();
  444. throw err;
  445. }
  446. }
  447. /**
  448. * 删除部位明细数据(仅供updateStageData调用)
  449. *
  450. * @param transaction - 事务
  451. * @param data - 删除的部位明细(允许一次提醒多条,也允许跨清单(但前端操作不允许))
  452. * @returns {Promise<{}>}
  453. * @private
  454. */
  455. async _deleteStagePosData(data) {
  456. const pos = await this.ctx.service.pos.getPosData({tid: this.ctx.tender.id, id: data});
  457. const result = { pos: data, isDeletePos: true };
  458. if (pos instanceof Array) {
  459. for (const p of pos) {
  460. if (p.add_stage !== this.ctx.stage.id /*|| p.add_times !== this.ctx.stage.curTimes || p.add_user !== this.ctx.session.sessionUser.accountId*/) {
  461. throw '不可删除该数据';
  462. }
  463. if (p.lid !== pos[0].lid) {
  464. throw '提交数据错误';
  465. }
  466. }
  467. } else if (pos.add_stage !== this.ctx.stage.id /*|| pos.add_times !== this.ctx.stage.curTimes || pos.add_user !== this.ctx.session.sessionUser.accountId*/) {
  468. throw '不可删除该数据';
  469. }
  470. const transaction = await this.db.beginTransaction();
  471. try {
  472. // 删除部位明细
  473. await transaction.delete(this.ctx.service.pos.tableName, {tid: this.ctx.tender.id, id: data});
  474. // 删除部位明细计量数据
  475. await transaction.delete(this.tableName, {tid: this.ctx.tender.id, lid: data});
  476. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage, pos[0].lid, transaction);
  477. await transaction.commit();
  478. // 获取需要更新的数据
  479. result.ledger = [pos[0].lid];
  480. result.stageUpdate = true;
  481. return result;
  482. } catch (err) {
  483. await transaction.rollback();
  484. throw err;
  485. }
  486. }
  487. /**
  488. * 根据前端提交数据,更新并计算
  489. *
  490. * @param data
  491. * @returns {Promise<{ledger: {}, pos: {}}>}
  492. */
  493. async updateStageData(data) {
  494. if ((data.updateType === 'add' || data.upateType === 'delete') && this.ctx.tender.measure_type === measureType.tz) {
  495. throw '台账模式下,不可在计量中新增或删除部位明细,如需操作,请进行台账修订';
  496. }
  497. let refreshData;
  498. if (data.updateType === 'add') {
  499. refreshData = await this._addStagePosData(data.updateData);
  500. } else if (data.updateType === 'update') {
  501. refreshData = await this._updateStagePosData(data.updateData);
  502. } else if (data.updateType === 'batchUpdate') {
  503. refreshData = await this._batchUpdateStagePosData(data.updateData);
  504. } else if (data.updateType === 'delete') {
  505. if (!data.updateData || data.updateData.length === 0) {
  506. throw '提交数据错误';
  507. }
  508. refreshData = await this._deleteStagePosData(data.updateData);
  509. } else {
  510. throw '提交数据错误';
  511. }
  512. try {
  513. const result = {ledger: {}, pos: {}};
  514. if (refreshData.ledger && refreshData.ledger.length > 0) {
  515. result.ledger.bills = await this.ctx.service.ledger.getDataByIds(refreshData.ledger);
  516. if (refreshData.stageUpdate) {
  517. result.ledger.curStageData = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, refreshData.ledger);
  518. }
  519. }
  520. if (refreshData.pos) {
  521. if (refreshData.isDeletePos) {
  522. result.pos.pos = refreshData.pos;
  523. } else if (refreshData.pos.length > 0) {
  524. result.pos.pos = await this.ctx.service.pos.getPosDataWithAddStageOrder({tid: this.ctx.tender.id, id: refreshData.pos});
  525. if (refreshData.stageUpdate) {
  526. result.pos.curStageData = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: refreshData.pos});
  527. }
  528. }
  529. }
  530. return result;
  531. } catch(err) {
  532. throw '获取数据异常,请刷新页面。';
  533. }
  534. }
  535. async updateChangeQuantity(transaction, pos, qty, noValue, positiveQty, negativeQty) {
  536. let orgPos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
  537. if (orgPos.length > 1) {
  538. throw '数据错误';
  539. } else {
  540. orgPos = orgPos[0];
  541. }
  542. if (orgPos && orgPos.times === this.ctx.stage.curTimes && orgPos.order === this.ctx.stage.curOrder) {
  543. await transaction.update(this.tableName, noValue
  544. ? { id: orgPos.id, qc_minus_qty: qty || 0, said: this.ctx.session.sessionUser.accountId, }
  545. : { id: orgPos.id, qc_qty: qty, positive_qc_qty: positiveQty, negative_qc_qty: negativeQty, said: this.ctx.session.sessionUser.accountId }
  546. );
  547. } else {
  548. await transaction.insert(this.tableName, {
  549. tid: this.ctx.tender.id,
  550. sid: this.ctx.stage.id,
  551. lid: pos.lid,
  552. pid: pos.id,
  553. said: this.ctx.session.sessionUser.accountId,
  554. times: this.ctx.stage.curTimes,
  555. order: this.ctx.stage.curOrder,
  556. contract_qty: orgPos ? orgPos.contract_qty : 0,
  557. qc_qty: !noValue ? qty : (orgPos ? orgPos.qc_qty : 0),
  558. qc_minus_qty: noValue ? qty : (orgPos ? orgPos.qc_minus_qty : 0),
  559. positive_qc_qty: !noValue ? positiveQty : (orgPos ? orgPos.positive_qc_qty : 0),
  560. negative_qc_qty: !noValue ? negativeQty : (orgPos ? orgPos.negative_qc_qty : 0),
  561. ex_stage_qty1: orgPos ? orgPos.ex_stage_qty1 || 0 : 0,
  562. });
  563. }
  564. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage, pos.lid, transaction);
  565. }
  566. /**
  567. * 统计清单下部位明细合计
  568. * @param {Number} tid - 标段id
  569. * @param {Number} sid - 期id
  570. * @param {Number} lid - 清单节点id
  571. * @param transaction - 事务(不为空则在事务中查询,反之在数据库中查询)
  572. * @returns {Promise<*>}
  573. */
  574. async getPosGather(tid, sid, lid, transaction) {
  575. const calcQtySql = 'SELECT SUM(`contract_qty`) As `contract_qty`, SUM(`qc_qty`) As `qc_qty`, SUM(`qc_minus_qty`) AS `qc_minus_qty`, SUM(`positive_qc_qty`) As `positive_qc_qty`, SUM(`negative_qc_qty`) As `negative_qc_qty`, SUM(`ex_stage_qty1`) As ex_stage_qty1 FROM (' +
  576. ' SELECT `contract_qty`, `qc_qty`, `qc_minus_qty`, `positive_qc_qty`, `negative_qc_qty`, ex_stage_qty1 FROM ' + this.ctx.service.stagePos.tableName + ' As Pos ' +
  577. ' INNER JOIN (' +
  578. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid` ' +
  579. ' FROM ' + this.ctx.service.stagePos.tableName +
  580. ' WHERE `tid` = ? And sid = ? And `lid` = ? ' +
  581. ' GROUP BY `pid`' +
  582. ' ) As MaxFilter ' +
  583. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid ' +
  584. ' WHERE Pos.tid = ? And Pos.sid = ? And Pos.lid = ?' +
  585. ' ) As Gather';
  586. const param = [tid, sid, lid];
  587. const sqlParam = param.concat(param);
  588. if (transaction) {
  589. return await transaction.queryOne(calcQtySql, sqlParam);
  590. } else {
  591. return await this.db.queryOne(calcQtySql, sqlParam);
  592. }
  593. }
  594. /**
  595. * 多期清单数据整合 (材料调差调用)
  596. * @param {Number} tid - 标段id
  597. * @param {String} stage_id_list - 期id列表
  598. * @param {String} comefrom - 来源(部分不调用计量不获取)
  599. * @returns {Promise<void>}
  600. */
  601. async getStagesData(tid, stage_id_list, comefrom = '') {
  602. const sids = stage_id_list.split(',');
  603. const result = [];
  604. for (const sid of sids) {
  605. const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, `qc_minus_qty`, postil, `times`, `order`, `contract_expr`' +
  606. ' FROM ' + this.tableName +
  607. ' WHERE tid = ? And sid = ? ';
  608. const sqlParam = [tid, sid];
  609. const stagePos = await this.db.query(sql, sqlParam);
  610. const stagePosFilter = this._filterLastestData(stagePos);
  611. for (const sp of stagePosFilter) {
  612. const rsp = result.find(function (x) { return x.pid === sp.pid});
  613. if (rsp) {
  614. rsp.contract_qty = this.ctx.helper.add(rsp.contract_qty, sp.contract_qty);
  615. rsp.qc_qty = this.ctx.helper.add(rsp.qc_qty, sp.qc_qty);
  616. rsp.qc_minus_qty = this.ctx.helper.add(rsp.qc_minus_qty, sp.qc_minus_qty);
  617. } else if (!comefrom || (comefrom === 'list' && (sp.contract_qty || sp.qc_qty || sp.qc_minus_qty))) {
  618. result.push({
  619. id: sp.id, tid: sp.tid, lid: sp.lid, pid: sp.pid,
  620. contract_qty: sp.contract_qty, qc_qty: sp.qc_qty, qc_minus_qty: sp.qc_minus_qty,
  621. });
  622. }
  623. }
  624. }
  625. return result;
  626. }
  627. /**
  628. * 获取多期(合同和数量变更相加)计量-小计(材料调差调用)
  629. * @param {Number} tid - 标段id
  630. * @param {String} stage_id_list - 期id列表
  631. * @param {String} lid - 台账id
  632. * @param {String} pid - 部位id
  633. * @returns {Promise<void>}
  634. */
  635. async getGatherQtyByMaterial(tid, stage_id_list, lid, pid) {
  636. stage_id_list = stage_id_list !== null ? stage_id_list.split(',') : [];
  637. const qtys = {
  638. gather_qty: 0,
  639. contract_qty: 0,
  640. qc_qty: 0,
  641. qc_minus_qty: 0,
  642. };
  643. for (const sid of stage_id_list) {
  644. const sql = 'SELECT Pos.contract_qty, Pos.qc_qty FROM ' +
  645. ' (SELECT * FROM ' + this.tableName + ' WHERE tid = ? AND sid = ?) As Pos ' +
  646. ' INNER JOIN ( ' +
  647. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `tid`, `sid`, `pid` From ' + this.tableName +
  648. ' WHERE `tid` = ? AND sid = ?' +
  649. ' GROUP BY `pid`' +
  650. ' ) As MaxFilter ' +
  651. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid' +
  652. ' WHERE Pos.lid = ? AND Pos.pid = ?';
  653. const sqlParam = [tid, sid, tid, sid, lid, pid];
  654. const result = await this.db.queryOne(sql, sqlParam);
  655. if (result) {
  656. qtys.contract_qty = this.ctx.helper.add(qtys.contract_qty, result.contract_qty);
  657. qtys.qc_qty = this.ctx.helper.add(qtys.qc_qty, result.qc_qty);
  658. qtys.qc_minus_qty = this.ctx.helper.add(qtys.qc_minus_qty, result.qc_minus_qty);
  659. qtys.gather_qty = this.ctx.helper.add(qtys.gather_qty, this.ctx.helper.add(result.contract_qty, result.qc_qty));
  660. }
  661. }
  662. return this.ctx.helper.resetQtys(qtys);
  663. }
  664. }
  665. return StagePos;
  666. };