pos.js 23 KB

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