pos.js 24 KB

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