pos.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. 'use strict';
  2. /**
  3. * 部位明细
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class Pos extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.depart = ctx.app.config.table_depart.heavy;
  20. this.tableName = 'pos';
  21. }
  22. async getPosData(condition, column) {
  23. if (!condition.tid) throw '查询计量单元缺少必要信息';
  24. return await this.db.select(this.departTableName(condition.tid), {
  25. where: condition,
  26. columns: column || ['id', 'tid', 'lid', 'name', 'quantity', 'position', 'drawing_code', 'sgfh_qty', 'sjcl_qty',
  27. 'qtcl_qty', 'in_time', 'porder', 'add_stage', 'add_stage_order', 'sgfh_expr', 'sjcl_expr', 'qtcl_expr', 'real_qty', 'ex_qty1',
  28. 'ex_memo1', 'ex_memo2', 'ex_memo3'],
  29. order: [['porder', 'ASC']],
  30. });
  31. }
  32. async getPosDataWithAddStageOrder(condition) {
  33. if (!condition.tid) throw '查询计量单元缺少必要信息';
  34. const sql = 'SELECT id, tid, lid, name, quantity, position, drawing_code,' +
  35. ' sgfh_qty, sjcl_qty, qtcl_qty, porder, add_stage, add_times, add_user, add_stage_order,' +
  36. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty, ex_qty1, ex_memo1, ex_memo2, ex_memo3' +
  37. ' FROM ' + this.departTableName(condition.tid) + this.ctx.helper.whereSql(condition);
  38. return await this.db.query(sql);
  39. }
  40. async getPosDataByIds(tid, ids) {
  41. if (ids instanceof Array && ids.length > 0) {
  42. const sql = 'SELECT id, tid, lid, name, quantity, position, drawing_code,' +
  43. ' sgfh_qty, sjcl_qty, qtcl_qty, add_stage, add_times, add_user, sgfh_expr, sjcl_expr, qtcl_expr, real_qty, ex_qty1,' +
  44. ' ex_memo1, ex_memo2, ex_memo3' +
  45. ' FROM ' + this.departTableName(tid) +
  46. ' WHERE id in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ')';
  47. return await this.db.query(sql, []);
  48. } else {
  49. return [];
  50. }
  51. // this.initSqlBuilder();
  52. // this.sqlBuilder.setAndWhere('id', {
  53. // operate: 'in',
  54. // value: ids
  55. // });
  56. // this.sqlBuilder.columns = ['id', 'tid', 'lid', 'name', 'quantity', 'drawing_code', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty'];
  57. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName)
  58. // return await this.db.query(sql, sqlParam);
  59. }
  60. async getPosDataByUnits(tenderId, units) {
  61. const sql = 'SELECT p.id, p.lid, p.sgfh_qty, p.sjcl_qty, p.qtcl_qty, p.ex_qty1' +
  62. ' FROM ' + this.departTableName(tenderId) + ' p' +
  63. ' LEFT JOIN ' + this.ctx.service.ledger.tableName + ' b' +
  64. ' ON p.lid = b.id ' +
  65. ' WHERE p.tid = ? and b.unit IN (?)';
  66. const sqlParam = [tenderId, units];
  67. return await this.db.query(sql, sqlParam);
  68. }
  69. async _insertPosData(transaction, data, tid) {
  70. data.id = this.uuid.v4();
  71. data.tid = tid;
  72. // todo 新增期
  73. data.add_stage = 0;
  74. data.add_stage_order = 0;
  75. data.add_times = 0;
  76. data.in_time = new Date();
  77. data.add_user = this.ctx.session.sessionUser.accountId;
  78. if (data.quantity) {
  79. const bills = await this.ctx.service.ledger.getDataById(data.lid);
  80. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  81. data.quantity = this.round(data.quantity, precision.value);
  82. }
  83. if (transaction) {
  84. const addRst = await transaction.insert(this.tableName, data);
  85. } else {
  86. const addRst = await this.db.insert(this.tableName, data);
  87. }
  88. }
  89. async _completeInsertPosData(tid, data) {
  90. data.id = this.uuid.v4();
  91. data.tid = tid;
  92. // todo 新增期
  93. data.add_stage = 0;
  94. data.add_stage_order = 0;
  95. data.add_times = 0;
  96. data.in_time = new Date();
  97. data.add_user = this.ctx.session.sessionUser.accountId;
  98. }
  99. async _getUpdateBills(data) {
  100. const datas = data instanceof Array ? data : [data];
  101. const bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
  102. const billsPos = await this.getAllDataByCondition({where: {tid: bills.tender_id, lid: bills.id} });
  103. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  104. const updateBills = {id: bills.id, ledger_id: bills.ledger_id};
  105. for (const bp of billsPos) {
  106. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  107. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  108. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  109. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  110. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, bp.ex_qty1);
  111. }
  112. for (const d of datas) {
  113. if (d.sgfh_qty) {
  114. d.sgfh_qty = this.ctx.helper.round(d.sgfh_qty, precision.value);
  115. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  116. }
  117. if (d.sjcl_qty) {
  118. d.sjcl_qty = this.ctx.helper.round(d.sjcl_qty, precision.value);
  119. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  120. }
  121. if (d.qtcl_qty) {
  122. d.qtcl_qty = this.ctx.helper.round(d.qtcl_qty, precision.value);
  123. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  124. }
  125. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  126. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  127. if (d.ex_qty1) {
  128. d.ex_qty1 = this.ctx.helper.round(d.ex_qty1, precision.value);
  129. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, d.ex_qty1);
  130. }
  131. }
  132. const info = this.ctx.tender.info;
  133. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  134. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  135. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  136. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  137. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  138. return updateBills;
  139. }
  140. async _addPosData(tid, data) {
  141. let updateBills = null;
  142. if (data instanceof Array) {
  143. for (const d of data) {
  144. this._completeInsertPosData(tid, d);
  145. }
  146. if (data[0].sgfh_qty !== undefined || data[0].sjcl_qty !== undefined || data[0].qtcl_qty !== undefined || data[0].ex_qty1 !== undefined) {
  147. updateBills = await this._getUpdateBills(data);
  148. }
  149. } else {
  150. this._completeInsertPosData(tid, data);
  151. if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined || data.ex_qty1 !== undefined) {
  152. updateBills = await this._getUpdateBills(data);
  153. }
  154. }
  155. if (updateBills) {
  156. const transaction = await this.db.beginTransaction();
  157. try {
  158. await transaction.insert(this.tableName, data);
  159. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  160. await transaction.commit();
  161. } catch (err) {
  162. await transaction.rollback();
  163. throw err;
  164. }
  165. return {
  166. ledger: { update: [updateBills] },
  167. pos: data,
  168. }
  169. } else {
  170. await this.db.insert(this.tableName, data);
  171. return { pos: data }
  172. }
  173. }
  174. async _updatePosData(tid, data) {
  175. if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined || data.ex_qty1 !== undefined) {
  176. const op = await this.getDataById(data.id);
  177. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  178. if (!bills) throw '数据错误';
  179. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  180. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  181. if (data.sgfh_qty !== undefined) {
  182. data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
  183. } else if (op) {
  184. data.sgfh_qty = op.sgfh_qty;
  185. }
  186. if (data.sjcl_qty !== undefined) {
  187. data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
  188. } else if (op) {
  189. data.sjcl_qty = op.sjcl_qty;
  190. }
  191. if (data.qtcl_qty !== undefined) {
  192. data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
  193. } else if (op) {
  194. data.qtcl_qty = op.qtcl_qty;
  195. }
  196. data.quantity = this.ctx.helper.sum([data.sgfh_qty, data.qtcl_qty, data.sjcl_qty]);
  197. if (data.ex_qty1 !== undefined) {
  198. data.ex_qty1 = this.round(data.ex_qty1, precision.value);
  199. } else if (op) {
  200. data.ex_qty1 = op.ex_qty1;
  201. }
  202. const updateBills = {id: bills.id};
  203. for (const bp of billsPos) {
  204. const calcData = bp.id === data.id ? data : bp;
  205. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  206. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  207. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  208. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  209. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, calcData.ex_qty1);
  210. }
  211. const info = this.ctx.tender.info;
  212. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  213. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  214. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  215. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  216. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  217. const transaction = await this.db.beginTransaction();
  218. try {
  219. await transaction.update(this.tableName, data);
  220. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  221. await transaction.commit();
  222. updateBills.ledger_id = bills.ledger_id;
  223. return {
  224. ledger: { update: [updateBills] },
  225. pos: data,
  226. }
  227. } catch(err) {
  228. await transaction.rollback();
  229. throw err;
  230. }
  231. } else {
  232. await this.db.update(this.tableName, data);
  233. return {pos: data};
  234. }
  235. }
  236. async _updatePosDatas(tid, data) {
  237. if (data.length === 0) return;
  238. const op = await this.getDataById(data[0].id);
  239. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  240. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  241. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  242. let needUpdateBills;
  243. for (const d of data) {
  244. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined || d.ex_qty1 !== undefined) {
  245. if (d.sgfh_qty !== undefined) {
  246. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  247. } else if (op) {
  248. d.sgfh_qty = op.sgfh_qty;
  249. }
  250. if (d.sjcl_qty !== undefined) {
  251. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  252. } else if (op) {
  253. d.sjcl_qty = op.sjcl_qty;
  254. }
  255. if (d.qtcl_qty !== undefined) {
  256. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  257. } else if (op) {
  258. d.qtcl_qty = op.qtcl_qty;
  259. }
  260. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  261. if (d.ex_qty1 !== undefined) {
  262. d.ex_qty1 = this.round(d.ex_qty1, precision.value);
  263. } else if (op) {
  264. d.ex_qty1 = op.ex_qty1;
  265. }
  266. needUpdateBills = true;
  267. }
  268. }
  269. const updateBills = {id: bills.id};
  270. if (needUpdateBills) {
  271. for (const bp of billsPos) {
  272. const newPos = data.find(function (x) { return x.id === bp.id });
  273. updateBills.sgfh_qty = newPos && newPos.sgfh_qty !== undefined
  274. ? this.ctx.helper.add(updateBills.sgfh_qty, newPos.sgfh_qty)
  275. : this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  276. updateBills.sjcl_qty = newPos && newPos.sjcl_qty !== undefined
  277. ? this.ctx.helper.add(updateBills.sjcl_qty, newPos.sjcl_qty)
  278. : this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  279. updateBills.qtcl_qty = newPos && newPos.qtcl_qty !== undefined
  280. ? this.ctx.helper.add(updateBills.qtcl_qty, newPos.qtcl_qty)
  281. : this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  282. updateBills.quantity = newPos && newPos.quantity !== undefined
  283. ? this.ctx.helper.add(updateBills.quantity, newPos.quantity)
  284. : this.ctx.helper.add(updateBills.quantity, bp.quantity);
  285. updateBills.ex_qty1 = newPos && newPos.ex_qty1 !== undefined
  286. ? this.ctx.helper.add(updateBills.ex_qty1, newPos.ex_qty1)
  287. : this.ctx.helper.add(updateBills.ex_qty1, bp.ex_qty1);
  288. }
  289. const info = this.ctx.tender.info;
  290. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  291. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  292. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  293. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  294. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  295. }
  296. const transaction = await this.db.beginTransaction();
  297. try {
  298. for (const d of data) {
  299. await transaction.update(this.tableName, d);
  300. }
  301. if (needUpdateBills) await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  302. await transaction.commit();
  303. updateBills.ledger_id = bills.ledger_id;
  304. return {
  305. ledger: { update: [updateBills] },
  306. pos: data,
  307. }
  308. } catch(err) {
  309. await transaction.rollback();
  310. throw err;
  311. }
  312. }
  313. async _deletePosData(tid, data) {
  314. if (!data || data.length === 0) throw '提交数据错误';
  315. const pos = await this.getPosData({tid: tid, id: data});
  316. if (!pos || pos.length === 0) throw '删除的计量单元不存在';
  317. const bills = await this.ctx.service.ledger.getDataById(pos[0].lid);
  318. const billsPos = await this.getAllDataByCondition({ where: {tid: tid, lid: bills.id} });
  319. const updateBills = {id: bills.id, sgfh_qty: 0, sjcl_qty: 0, qtcl_qty: 0, quantity: 0, ex_qty1: 0};
  320. for (const bp of billsPos) {
  321. if (data.indexOf(bp.id) >= 0) continue;
  322. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  323. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  324. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  325. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  326. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, bp.ex_qty1);
  327. }
  328. const info = this.ctx.tender.info;
  329. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  330. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  331. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  332. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  333. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  334. const transaction = await this.db.beginTransaction();
  335. try {
  336. await transaction.delete(this.tableName, {tid: tid, id: data});
  337. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  338. await transaction.commit();
  339. updateBills.ledger_id = bills.ledger_id;
  340. return { ledger: { update: [updateBills] }, pos: data };
  341. } catch(err) {
  342. await transaction.rollback();
  343. throw err;
  344. }
  345. }
  346. async _insertPosData(tid, data) {
  347. if (!data.length) return;
  348. let porder = data[0].porder, lid = data[0].lid;
  349. for (const d of data) {
  350. this._completeInsertPosData(tid, d);
  351. porder = Math.min(porder, d.porder);
  352. }
  353. const transaction = await this.db.beginTransaction();
  354. try {
  355. this.initSqlBuilder();
  356. this.sqlBuilder.setAndWhere('lid', {
  357. value: this.db.escape(lid),
  358. operate: '=',
  359. });
  360. this.sqlBuilder.setAndWhere('porder', {
  361. value: porder,
  362. operate: '>=',
  363. });
  364. this.sqlBuilder.setUpdateData('porder', {
  365. value: data.length,
  366. selfOperate: '+',
  367. });
  368. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  369. const result = await transaction.query(sql, sqlParam);
  370. await transaction.insert(this.tableName, data);
  371. await transaction.commit();
  372. } catch (err) {
  373. await transaction.rollback();
  374. throw err;
  375. }
  376. this.initSqlBuilder();
  377. this.sqlBuilder.setAndWhere('lid', {
  378. value: this.db.escape(lid),
  379. operate: '=',
  380. });
  381. this.sqlBuilder.setAndWhere('porder', {
  382. value: porder,
  383. operate: '>=',
  384. });
  385. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  386. const update = await this.db.query(sql, sqlParam);
  387. return { pos: update }
  388. }
  389. /**
  390. * 保存部位明细数据
  391. * @param data
  392. * @param {Number} tid - 标段id
  393. * @returns {Promise<{ledger: {}, pos: null}>}
  394. */
  395. async savePosData(data, tid) {
  396. switch (data.updateType) {
  397. case 'add':
  398. return await this._addPosData(tid, data.updateData);
  399. case 'update':
  400. if (data.updateData instanceof Array) {
  401. return await this._updatePosDatas(tid, data.updateData);
  402. } else {
  403. return await this._updatePosData(tid, data.updateData);
  404. }
  405. case 'delete':
  406. return await this._deletePosData(tid, data.updateData);
  407. case 'insert':
  408. return await this._insertPosData(tid, data.updateData);
  409. }
  410. }
  411. /**
  412. * 复制粘贴 部位明细数据
  413. * @param {Array} data - 复制粘贴的数据
  414. * @param {Number} tid - 标段id
  415. * @returns {Promise<{ledger: {}, pos: null}>}
  416. */
  417. async pastePosData(data, tid) {
  418. if (!(data instanceof Array)) {
  419. throw '提交数据错误';
  420. }
  421. const transaction = await this.db.beginTransaction();
  422. const result = { ledger: {}, pos: null };
  423. const bills = await this.ctx.service.ledger.getDataById(data[0].lid);
  424. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  425. const updateBills = {id: bills.id};
  426. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: bills.id} });
  427. for (const bp of billsPos) {
  428. const d = data.find(function (x) {
  429. return bp.id ? x.id === bp.id : false;
  430. });
  431. if (d) continue;
  432. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  433. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  434. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  435. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  436. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, bp.ex_qty1);
  437. }
  438. try {
  439. for (const d of data) {
  440. const op = d.id ? this._.find(billsPos, {id: d.id}) : null;
  441. if (d.sgfh_qty !== undefined) {
  442. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  443. } else if (op) {
  444. d.sgfh_qty = op.sgfh_qty;
  445. }
  446. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  447. if (d.sjcl_qty !== undefined) {
  448. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  449. } else if (op) {
  450. d.sjcl_qty = op.sjcl_qty;
  451. }
  452. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  453. if (d.qtcl_qty) {
  454. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  455. } else if (op) {
  456. d.qtcl_qty = op.qtcl_qty;
  457. }
  458. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  459. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  460. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  461. if (d.ex_qty1) {
  462. d.ex_qty1 = this.round(d.ex_qty1, precision.value);
  463. } else if (op) {
  464. d.ex_qty1 = op.ex_qty1;
  465. }
  466. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, d.ex_qty1);
  467. if (d.id) {
  468. await transaction.update(this.tableName, d);
  469. } else {
  470. this._completeInsertPosData(tid, d);
  471. await transaction.insert(this.tableName, d);
  472. }
  473. }
  474. const info = this.ctx.tender.info;
  475. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  476. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  477. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  478. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  479. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  480. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  481. updateBills.ledger_id = bills.ledger_id;
  482. await transaction.commit();
  483. } catch (err) {
  484. await transaction.rollback();
  485. throw err;
  486. }
  487. result.pos = data;
  488. result.ledger.update = [updateBills];
  489. return result;
  490. }
  491. /**
  492. * 删除清单下部位明细数据(删除清单时调用)
  493. *
  494. * @param transaction - 事务
  495. * @param tid - 标段id
  496. * @param lid - 清单id
  497. * @returns {Promise<void>}
  498. */
  499. async deletePosData(transaction, tid, lid) {
  500. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  501. }
  502. async getMemoData(tid, columns) {
  503. if (!columns || columns.length === 0) return [];
  504. return await this.db.select(this.departTableName(tid), {
  505. where: { tid },
  506. columns: ['id', ...columns],
  507. });
  508. }
  509. }
  510. return Pos;
  511. };