stage_pos.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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.id, 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 || osp) {
  269. sp.contract_qty = d.contract_qty === undefined && osp
  270. ? osp.contract_qty
  271. : this.ctx.helper.round(d.contract_qty, precision.value);
  272. }
  273. if (d.contract_expr !== undefined) sp.contract_expr = d.contract_expr;
  274. if (d.qc_qty || osp) {
  275. sp.qc_qty = d.qc_qty === undefined && osp
  276. ? osp.qc_qty
  277. : this.ctx.helper.round(d.qc_qty, precision.value);
  278. }
  279. if (d.postil || osp) {
  280. sp.postil = d.postil === undefined && osp ? osp.postil : d.postil;
  281. }
  282. if (d.ex_stage_qty1 || osp) {
  283. sp.ex_stage_qty1 = d.ex_stage_qty1 === undefined && osp ? osp.ex_stage_qty1 : d.ex_stage_qty1;
  284. }
  285. sp.qc_minus_qty = osp ? osp.qc_minus_qty : 0;
  286. sp.positive_qc_qty = osp ? osp.positive_qc_qty : 0;
  287. sp.negative_qc_qty = osp ? osp.negative_qc_qty : 0;
  288. insertPosStage.push(sp);
  289. }
  290. }
  291. result.pos.push(d.pid);
  292. }
  293. if (updateBills) {
  294. const billsPos = await this.ctx.service.pos.getAllDataByCondition({where: {tid: bills.tender_id, lid: bills.id} });
  295. for (const bp of billsPos) {
  296. const newPos = updatePos.find(function (x) { return x.id === bp.id });
  297. const calcData = newPos ? newPos : bp;
  298. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  299. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  300. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  301. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  302. }
  303. const info = this.ctx.tender.info;
  304. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  305. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  306. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  307. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, 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.id, 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. }
  368. insertPosStage.push(sp);
  369. }
  370. result.pos.push(d.pid);
  371. }
  372. const updateBillsStage = [], insertBillsStage = [], info = this.ctx.tender.info;
  373. for (const b of bills) {
  374. const stageBills = await this.ctx.service.stageBills.getLastestStageData2(b.tender_id, this.ctx.stage.id, b.id);
  375. const posStage = await this.getLastestStageData2(b.tender_id, this.ctx.stage.id, {lid: b.id});
  376. let contract_qty = 0;
  377. const newPosRange = insertPosStage.filter(x => {return x.lid === b.id});
  378. for (const nps of newPosRange) {
  379. contract_qty = this.ctx.helper.add(contract_qty, nps.contract_qty);
  380. }
  381. for (const ps of posStage) {
  382. const ips = this._.find(insertPosStage, {pid: ps.pid});
  383. if (!ips) {
  384. const ups = this._.find(updatePosStage, {id: ps.id});
  385. contract_qty = this.ctx.helper.add(contract_qty, ups ? ups.contract_qty : ps.contract_qty);
  386. }
  387. }
  388. if (stageBills && stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
  389. if (contract_qty === stageBills.contract_qty) continue;
  390. updateBillsStage.push({
  391. id: stageBills.id,
  392. said: this.ctx.session.sessionUser.accountId,
  393. contract_qty: contract_qty,
  394. contract_tp: this.ctx.helper.mul(contract_qty, b.unit_price, info.decimal.tp),
  395. });
  396. } else {
  397. insertBillsStage.push({
  398. tid: this.ctx.tender.id,
  399. lid: b.id,
  400. sid: this.ctx.stage.id,
  401. times: this.ctx.stage.curTimes,
  402. order: this.ctx.stage.curOrder,
  403. said: this.ctx.session.sessionUser.accountId,
  404. contract_qty: contract_qty,
  405. contract_tp: this.ctx.helper.mul(contract_qty, b.unit_price, info.decimal.tp),
  406. })
  407. }
  408. }
  409. const transaction = await this.db.beginTransaction();
  410. try {
  411. if (updatePosStage.length > 0) await transaction.updateRows(this.tableName, updatePosStage);
  412. if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
  413. if (updateBillsStage.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, updateBillsStage);
  414. if (insertBillsStage.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, insertBillsStage);
  415. await transaction.commit();
  416. return result;
  417. } catch (err) {
  418. await transaction.rollback();
  419. throw err;
  420. }
  421. }
  422. /**
  423. * 删除部位明细数据(仅供updateStageData调用)
  424. *
  425. * @param transaction - 事务
  426. * @param data - 删除的部位明细(允许一次提醒多条,也允许跨清单(但前端操作不允许))
  427. * @returns {Promise<{}>}
  428. * @private
  429. */
  430. async _deleteStagePosData(data) {
  431. const pos = await this.ctx.service.pos.getPosData({tid: this.ctx.tender.id, id: data});
  432. const result = { pos: data, isDeletePos: true };
  433. if (pos instanceof Array) {
  434. for (const p of pos) {
  435. if (p.add_stage !== this.ctx.stage.id /*|| p.add_times !== this.ctx.stage.curTimes || p.add_user !== this.ctx.session.sessionUser.accountId*/) {
  436. throw '不可删除该数据';
  437. }
  438. if (p.lid !== pos[0].lid) {
  439. throw '提交数据错误';
  440. }
  441. }
  442. } else if (pos.add_stage !== this.ctx.stage.id /*|| pos.add_times !== this.ctx.stage.curTimes || pos.add_user !== this.ctx.session.sessionUser.accountId*/) {
  443. throw '不可删除该数据';
  444. }
  445. const transaction = await this.db.beginTransaction();
  446. try {
  447. // 删除部位明细
  448. await transaction.delete(this.ctx.service.pos.tableName, {tid: this.ctx.tender.id, id: data});
  449. // 删除部位明细计量数据
  450. await transaction.delete(this.tableName, {tid: this.ctx.tender.id, lid: data});
  451. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, pos[0].lid, transaction);
  452. await transaction.commit();
  453. // 获取需要更新的数据
  454. result.ledger = [pos[0].lid];
  455. result.stageUpdate = true;
  456. return result;
  457. } catch (err) {
  458. await transaction.rollback();
  459. throw err;
  460. }
  461. }
  462. /**
  463. * 根据前端提交数据,更新并计算
  464. *
  465. * @param data
  466. * @returns {Promise<{ledger: {}, pos: {}}>}
  467. */
  468. async updateStageData(data) {
  469. if ((data.updateType === 'add' || data.upateType === 'delete') && this.ctx.tender.measure_type === measureType.tz) {
  470. throw '台账模式下,不可在计量中新增或删除部位明细,如需操作,请进行台账修订';
  471. }
  472. let refreshData;
  473. if (data.updateType === 'add') {
  474. refreshData = await this._addStagePosData(data.updateData);
  475. } else if (data.updateType === 'update') {
  476. refreshData = await this._updateStagePosData(data.updateData);
  477. } else if (data.updateType === 'batchUpdate') {
  478. refreshData = await this._batchUpdateStagePosData(data.updateData);
  479. } else if (data.updateType === 'delete') {
  480. if (!data.updateData || data.updateData.length === 0) {
  481. throw '提交数据错误';
  482. }
  483. refreshData = await this._deleteStagePosData(data.updateData);
  484. } else {
  485. throw '提交数据错误';
  486. }
  487. try {
  488. const result = {ledger: {}, pos: {}};
  489. if (refreshData.ledger && refreshData.ledger.length > 0) {
  490. result.ledger.bills = await this.ctx.service.ledger.getDataByIds(refreshData.ledger);
  491. if (refreshData.stageUpdate) {
  492. result.ledger.curStageData = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, refreshData.ledger);
  493. }
  494. }
  495. if (refreshData.pos) {
  496. if (refreshData.isDeletePos) {
  497. result.pos.pos = refreshData.pos;
  498. } else if (refreshData.pos.length > 0) {
  499. result.pos.pos = await this.ctx.service.pos.getPosDataWithAddStageOrder({tid: this.ctx.tender.id, id: refreshData.pos});
  500. if (refreshData.stageUpdate) {
  501. result.pos.curStageData = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: refreshData.pos});
  502. }
  503. }
  504. }
  505. return result;
  506. } catch(err) {
  507. throw '获取数据异常,请刷新页面。';
  508. }
  509. }
  510. async updateChangeQuantity(transaction, pos, qty, noValue, positiveQty, negativeQty) {
  511. let orgPos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
  512. if (orgPos.length > 1) {
  513. throw '数据错误';
  514. } else {
  515. orgPos = orgPos[0];
  516. }
  517. if (orgPos && orgPos.times === this.ctx.stage.curTimes && orgPos.order === this.ctx.stage.curOrder) {
  518. await transaction.update(this.tableName, noValue
  519. ? { id: orgPos.id, qc_minus_qty: qty || 0, said: this.ctx.session.sessionUser.accountId, }
  520. : { id: orgPos.id, qc_qty: qty, positive_qc_qty: positiveQty, negative_qc_qty: negativeQty, said: this.ctx.session.sessionUser.accountId }
  521. );
  522. } else {
  523. await transaction.insert(this.tableName, {
  524. tid: this.ctx.tender.id,
  525. sid: this.ctx.stage.id,
  526. lid: pos.lid,
  527. pid: pos.id,
  528. said: this.ctx.session.sessionUser.accountId,
  529. times: this.ctx.stage.curTimes,
  530. order: this.ctx.stage.curOrder,
  531. contract_qty: orgPos ? orgPos.contract_qty : 0,
  532. qc_qty: !noValue ? qty : (orgPos ? orgPos.qc_qty : 0),
  533. qc_minus_qty: noValue ? qty : (orgPos ? orgPos.qc_minus_qty : 0),
  534. positive_qc_qty: !noValue ? positiveQty : (orgPos ? orgPos.positive_qc_qty : 0),
  535. negative_qc_qty: !noValue ? negativeQty : (orgPos ? orgPos.negative_qc_qty : 0),
  536. ex_stage_qty1: orgPos ? orgPos.ex_stage_qty1 : 0,
  537. });
  538. }
  539. await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, pos.lid, transaction);
  540. }
  541. /**
  542. * 统计清单下部位明细合计
  543. * @param {Number} tid - 标段id
  544. * @param {Number} sid - 期id
  545. * @param {Number} lid - 清单节点id
  546. * @param transaction - 事务(不为空则在事务中查询,反之在数据库中查询)
  547. * @returns {Promise<*>}
  548. */
  549. async getPosGather(tid, sid, lid, transaction) {
  550. 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` FROM (' +
  551. ' SELECT `contract_qty`, `qc_qty`, `qc_minus_qty`, `positive_qc_qty`, `negative_qc_qty` FROM ' + this.ctx.service.stagePos.tableName + ' As Pos ' +
  552. ' INNER JOIN (' +
  553. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid` ' +
  554. ' FROM ' + this.ctx.service.stagePos.tableName +
  555. ' WHERE `tid` = ? And sid = ? And `lid` = ? ' +
  556. ' GROUP BY `pid`' +
  557. ' ) As MaxFilter ' +
  558. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid ' +
  559. ' WHERE Pos.tid = ? And Pos.sid = ? And Pos.lid = ?' +
  560. ' ) As Gather';
  561. const param = [tid, sid, lid];
  562. const sqlParam = param.concat(param);
  563. if (transaction) {
  564. return await transaction.queryOne(calcQtySql, sqlParam);
  565. } else {
  566. return await this.db.queryOne(calcQtySql, sqlParam);
  567. }
  568. }
  569. /**
  570. * 多期清单数据整合 (材料调差调用)
  571. * @param {Number} tid - 标段id
  572. * @param {String} stage_id_list - 期id列表
  573. * @param {String} comefrom - 来源(部分不调用计量不获取)
  574. * @returns {Promise<void>}
  575. */
  576. async getStagesData(tid, stage_id_list, comefrom = '') {
  577. const sids = stage_id_list.split(',');
  578. const result = [];
  579. for (const sid of sids) {
  580. const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, `qc_minus_qty`, postil, `times`, `order`, `contract_expr`' +
  581. ' FROM ' + this.tableName +
  582. ' WHERE tid = ? And sid = ? ';
  583. const sqlParam = [tid, sid];
  584. const stagePos = await this.db.query(sql, sqlParam);
  585. const stagePosFilter = this._filterLastestData(stagePos);
  586. for (const sp of stagePosFilter) {
  587. const rsp = result.find(function (x) { return x.pid === sp.pid});
  588. if (rsp) {
  589. rsp.contract_qty = this.ctx.helper.add(rsp.contract_qty, sp.contract_qty);
  590. rsp.qc_qty = this.ctx.helper.add(rsp.qc_qty, sp.qc_qty);
  591. rsp.qc_minus_qty = this.ctx.helper.add(rsp.qc_minus_qty, sp.qc_minus_qty);
  592. } else if (!comefrom || (comefrom === 'list' && (sp.contract_qty || sp.qc_qty || sp.qc_minus_qty))) {
  593. result.push({
  594. id: sp.id, tid: sp.tid, lid: sp.lid, pid: sp.pid,
  595. contract_qty: sp.contract_qty, qc_qty: sp.qc_qty, qc_minus_qty: sp.qc_minus_qty,
  596. });
  597. }
  598. }
  599. }
  600. return result;
  601. }
  602. /**
  603. * 获取多期(合同和数量变更相加)计量-小计(材料调差调用)
  604. * @param {Number} tid - 标段id
  605. * @param {String} stage_id_list - 期id列表
  606. * @param {String} lid - 台账id
  607. * @param {String} pid - 部位id
  608. * @returns {Promise<void>}
  609. */
  610. async getGatherQtyByMaterial(tid, stage_id_list, lid, pid) {
  611. stage_id_list = stage_id_list !== null ? stage_id_list.split(',') : [];
  612. const qtys = {
  613. gather_qty: 0,
  614. contract_qty: 0,
  615. qc_qty: 0,
  616. qc_minus_qty: 0,
  617. };
  618. for (const sid of stage_id_list) {
  619. const sql = 'SELECT Pos.contract_qty, Pos.qc_qty FROM ' +
  620. ' (SELECT * FROM ' + this.tableName + ' WHERE tid = ? AND sid = ?) As Pos ' +
  621. ' INNER JOIN ( ' +
  622. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `tid`, `sid`, `pid` From ' + this.tableName +
  623. ' WHERE `tid` = ? AND sid = ?' +
  624. ' GROUP BY `pid`' +
  625. ' ) As MaxFilter ' +
  626. ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid' +
  627. ' WHERE Pos.lid = ? AND Pos.pid = ?';
  628. const sqlParam = [tid, sid, tid, sid, lid, pid];
  629. const result = await this.db.queryOne(sql, sqlParam);
  630. if (result) {
  631. qtys.contract_qty = this.ctx.helper.add(qtys.contract_qty, result.contract_qty);
  632. qtys.qc_qty = this.ctx.helper.add(qtys.qc_qty, result.qc_qty);
  633. qtys.qc_minus_qty = this.ctx.helper.add(qtys.qc_minus_qty, result.qc_minus_qty);
  634. qtys.gather_qty = this.ctx.helper.add(qtys.gather_qty, this.ctx.helper.add(result.contract_qty, result.qc_qty));
  635. }
  636. }
  637. return this.ctx.helper.resetQtys(qtys);
  638. }
  639. }
  640. return StagePos;
  641. };