stage_pos.js 37 KB

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