change_pos.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. 'use strict';
  2. /**
  3. * 变更插入新增 - 部位明细
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class ChangePos extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. // this.depart = 20;
  20. this.tableName = 'change_pos';
  21. }
  22. async getPosData(condition) {
  23. if (!condition.tid) throw '查询计量单元缺少必要信息';
  24. return await this.db.select(this.tableName, {
  25. where: condition,
  26. columns: ['id', 'tid', 'lid', 'name', 'quantity', 'position', 'drawing_code', 'sgfh_qty', 'sjcl_qty',
  27. 'qtcl_qty', 'in_time', 'porder', 'add_stage', 'sgfh_expr', 'sjcl_expr', 'qtcl_expr', 'real_qty',
  28. 'dagl_status', 'dagl_url', 'gxby_status', 'gxby_limit', 'dagl_limit', 'dagl_status',
  29. 'ex_memo1', 'ex_memo2', 'ex_memo3', 'ccid'],
  30. order: [['porder', 'ASC']],
  31. });
  32. }
  33. async _completeInsertPosData(tid, cid, data) {
  34. data.id = this.uuid.v4();
  35. data.tid = tid;
  36. // todo 新增期
  37. data.add_stage = 0;
  38. data.add_times = 0;
  39. data.in_time = new Date();
  40. data.add_user = this.ctx.session.sessionUser.accountId;
  41. data.ccid = cid;
  42. }
  43. async addPos(tid, cid, data) {
  44. if (data instanceof Array) {
  45. for (const d of data) {
  46. this._completeInsertPosData(tid, cid, d);
  47. }
  48. } else {
  49. this._completeInsertPosData(tid, cid, data);
  50. }
  51. const transaction = await this.db.beginTransaction();
  52. try {
  53. await this.ctx.service.changeAuditList.deleteDataByRevise(transaction, tid, [data.lid], 'gcl_id', '');
  54. await transaction.insert(this.tableName, data);
  55. await transaction.commit();
  56. const returnData = { pos: data };
  57. const bills = await this.ctx.service.ledger.getDataById(data.lid);
  58. if (bills) {
  59. bills.cid = 1;
  60. returnData.ledger = {
  61. update: [bills],
  62. };
  63. }
  64. return returnData;
  65. } catch (err) {
  66. await transaction.rollback();
  67. throw err;
  68. }
  69. }
  70. async updatePos(tid, data) {
  71. if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined) {
  72. const op = await this.getDataById(data.id);
  73. let bills = await this.ctx.service.changeLedger.getDataById(op.lid);
  74. let newBills = false;
  75. let billsPos = await this.getAllDataByCondition({ where: { tid: tid, lid: op.lid } });
  76. if (bills) {
  77. newBills = true;
  78. } else {
  79. bills = await this.ctx.service.ledger.getDataById(op.lid);
  80. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tid, lid: op.lid } });
  81. billsPos = this._.concat(posData, billsPos);
  82. }
  83. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  84. if (data.sgfh_qty !== undefined) {
  85. data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
  86. } else if (op) {
  87. data.sgfh_qty = op.sgfh_qty;
  88. }
  89. if (data.sjcl_qty !== undefined) {
  90. data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
  91. } else if (op) {
  92. data.sjcl_qty = op.sjcl_qty;
  93. }
  94. if (data.qtcl_qty !== undefined) {
  95. data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
  96. } else if (op) {
  97. data.qtcl_qty = op.qtcl_qty;
  98. }
  99. data.quantity = this.ctx.helper.sum([data.sgfh_qty, data.qtcl_qty, data.sjcl_qty]);
  100. const updateBills = { id: bills.id };
  101. for (const bp of billsPos) {
  102. const calcData = bp.id === data.id ? data : bp;
  103. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  104. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  105. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  106. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  107. }
  108. const info = this.ctx.tender.info;
  109. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  110. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  111. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  112. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  113. const transaction = await this.db.beginTransaction();
  114. try {
  115. await transaction.update(this.tableName, data);
  116. if (newBills) await transaction.update(this.ctx.service.changeLedger.tableName, updateBills);
  117. await this.ctx.service.changeAuditList.updateDataByRevisePos(transaction, tid, [data]);
  118. await transaction.commit();
  119. updateBills.ledger_id = bills.ledger_id;
  120. return {
  121. ledger: { update: [updateBills] },
  122. pos: data,
  123. };
  124. } catch (err) {
  125. await transaction.rollback();
  126. throw err;
  127. }
  128. } else {
  129. const transaction = await this.db.beginTransaction();
  130. try {
  131. await transaction.update(this.tableName, data, { tid, id: data.id });
  132. await this.ctx.service.changeAuditList.updateDataByRevisePos(transaction, tid, [data]);
  133. await transaction.commit();
  134. return { pos: data };
  135. } catch (err) {
  136. await transaction.rollback();
  137. throw err;
  138. }
  139. }
  140. }
  141. async updatePosArr(tid, data) {
  142. console.log(data);
  143. if (data.length === 0) return;
  144. const op = await this.getDataById(data[0].id) || await this.ctx.service.pos.getDataById(data[0].id);
  145. let bills = await this.ctx.service.changeLedger.getDataById(op.lid);
  146. let newBills = false;
  147. let billsPos = await this.getAllDataByCondition({ where: { tid: tid, lid: op.lid } });
  148. if (bills) {
  149. newBills = true;
  150. } else {
  151. bills = await this.ctx.service.ledger.getDataById(op.lid);
  152. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tid, lid: op.lid } });
  153. billsPos = this._.concat(posData, billsPos);
  154. }
  155. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  156. let needUpdateBills;
  157. for (const d of data) {
  158. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  159. if (d.sgfh_qty !== undefined) {
  160. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  161. } else if (op) {
  162. d.sgfh_qty = op.sgfh_qty;
  163. }
  164. if (d.sjcl_qty !== undefined) {
  165. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  166. } else if (op) {
  167. d.sjcl_qty = op.sjcl_qty;
  168. }
  169. if (d.qtcl_qty !== undefined) {
  170. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  171. } else if (op) {
  172. d.qtcl_qty = op.qtcl_qty;
  173. }
  174. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  175. needUpdateBills = true;
  176. }
  177. }
  178. const updateBills = { id: bills.id };
  179. if (needUpdateBills) {
  180. for (const bp of billsPos) {
  181. const newPos = data.find(function(x) { return x.id === bp.id; });
  182. updateBills.sgfh_qty = newPos && newPos.sgfh_qty !== undefined
  183. ? this.ctx.helper.add(updateBills.sgfh_qty, newPos.sgfh_qty)
  184. : this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  185. updateBills.sjcl_qty = newPos && newPos.sjcl_qty !== undefined
  186. ? this.ctx.helper.add(updateBills.sjcl_qty, newPos.sjcl_qty)
  187. : this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  188. updateBills.qtcl_qty = newPos && newPos.qtcl_qty !== undefined
  189. ? this.ctx.helper.add(updateBills.qtcl_qty, newPos.qtcl_qty)
  190. : this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  191. updateBills.quantity = newPos && newPos.quantity !== undefined
  192. ? this.ctx.helper.add(updateBills.quantity, newPos.quantity)
  193. : this.ctx.helper.add(updateBills.quantity, bp.quantity);
  194. }
  195. const info = this.ctx.tender.info;
  196. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  197. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  198. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  199. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  200. }
  201. const transaction = await this.db.beginTransaction();
  202. try {
  203. for (const d of data) {
  204. // 区分d属于pos还是changePos,分别更新对应的表
  205. const np = await this.getDataById(d.id);
  206. np ? await transaction.update(this.tableName, d) : await transaction.update(this.ctx.service.pos.tableName, d);
  207. }
  208. if (needUpdateBills && newBills) await transaction.update(this.ctx.service.changeLedger.tableName, updateBills);
  209. await this.ctx.service.changeAuditList.updateDataByRevisePos(transaction, tid, data);
  210. await transaction.commit();
  211. updateBills.ledger_id = bills.ledger_id;
  212. return {
  213. ledger: { update: needUpdateBills ? [updateBills] : [] },
  214. pos: data,
  215. };
  216. } catch (err) {
  217. await transaction.rollback();
  218. throw err;
  219. }
  220. }
  221. async deletePos(tid, data) {
  222. if (!data || data.length === 0) {
  223. throw '提交数据错误';
  224. }
  225. const pos = await this.getPosData({tid: tid, id: data});
  226. let bills = await this.ctx.service.changeLedger.getDataById(pos[0].lid);
  227. let newBills = false;
  228. let billsPos = await this.getAllDataByCondition({ where: { tid: tid, lid: pos[0].lid } });
  229. let cid = 1;
  230. if (bills) {
  231. newBills = true;
  232. } else {
  233. bills = await this.ctx.service.ledger.getDataById(pos[0].lid);
  234. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tid, lid: pos[0].lid } });
  235. if (billsPos.length === data.length) {
  236. cid = null;
  237. }
  238. billsPos = this._.concat(posData, billsPos);
  239. }
  240. // const bills = await this.ctx.service.reviseBills.getDataById(pos[0].lid);
  241. // const billsPos = await this.getAllDataByCondition({ where: {tid: tid, lid: bills.id} });
  242. const updateBills = {id: bills.id, sgfh_qty: null, sjcl_qty: null, qtcl_qty: null, quantity: null};
  243. updateBills.cid = cid;
  244. for (const bp of billsPos) {
  245. if (data.indexOf(bp.id) >= 0) continue;
  246. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  247. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  248. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  249. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  250. }
  251. const info = this.ctx.tender.info;
  252. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  253. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  254. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  255. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  256. const transaction = await this.db.beginTransaction();
  257. try {
  258. await transaction.delete(this.tableName, {tid: tid, id: data});
  259. if (newBills) await transaction.update(this.ctx.service.changeLedger.tableName, updateBills);
  260. await this.ctx.service.changeAuditList.deleteDataByRevise(transaction, tid, data, 'mx_id');
  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. /**
  273. * 复制粘贴 部位明细数据
  274. * @param {Array} data - 复制粘贴的数据
  275. * @param {Number} tid - 标段id
  276. * @returns {Promise<{ledger: {}, pos: null}>}
  277. */
  278. async pastePosData(tid, cid, data) {
  279. if (!(data instanceof Array)) throw '提交数据错误';
  280. const transaction = await this.db.beginTransaction();
  281. const result = { ledger: {}, pos: null };
  282. const orgPos = await this.getPosData({ tid: tid, id: this._.map(data, 'id') });
  283. // const bills = await this.ctx.service.reviseBills.getDataById(data[0].lid);
  284. // const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  285. // const updateBills = {id: bills.id};
  286. // const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: bills.id} });
  287. let bills = await this.ctx.service.changeLedger.getDataById(data[0].lid);
  288. let newBills = false;
  289. let billsPos = await this.getAllDataByCondition({ where: { tid: tid, lid: data[0].lid } });
  290. if (bills) {
  291. newBills = true;
  292. } else {
  293. bills = await this.ctx.service.ledger.getDataById(data[0].lid);
  294. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tid, lid: data[0].lid } });
  295. billsPos = this._.concat(posData, billsPos);
  296. }
  297. const updateBills = { id: bills.id };
  298. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  299. let needUpdateBills;
  300. for (const bp of billsPos) {
  301. const d = data.find(function(x) {
  302. return bp.id ? x.id === bp.id : false;
  303. });
  304. if (d) continue;
  305. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  306. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  307. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  308. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  309. }
  310. try {
  311. for (const d of data) {
  312. const op = d.id ? this._.find(orgPos, { id: d.id }) : null;
  313. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  314. if (d.sgfh_qty !== undefined) {
  315. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  316. } else if (op) {
  317. d.sgfh_qty = op.sgfh_qty;
  318. }
  319. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  320. if (d.sjcl_qty !== undefined) {
  321. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  322. } else if (op) {
  323. d.sjcl_qty = op.sjcl_qty;
  324. }
  325. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  326. if (d.qtcl_qty) {
  327. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  328. } else if (op) {
  329. d.qtcl_qty = op.qtcl_qty;
  330. }
  331. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  332. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  333. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  334. needUpdateBills = true;
  335. }
  336. if (d.id) {
  337. await transaction.update(this.tableName, d);
  338. } else {
  339. updateBills.cid = 1;
  340. await this._insertPosData(transaction, d, tid, cid);
  341. }
  342. }
  343. const info = this.ctx.tender.info;
  344. if (needUpdateBills) {
  345. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  346. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  347. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  348. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  349. if (newBills) await transaction.update(this.ctx.service.changeLedger.tableName, updateBills);
  350. updateBills.ledger_id = bills.ledger_id;
  351. }
  352. await this.ctx.service.changeAuditList.updateDataByRevisePos(transaction, tid, data);
  353. await transaction.commit();
  354. } catch (err) {
  355. await transaction.rollback();
  356. throw err;
  357. }
  358. result.pos = data;
  359. result.ledger.update = needUpdateBills ? [updateBills] : updateBills.cid && updateBills.cid === 1 ? [{ id: bills.id, ledger_id: bills.ledger_id, cid: 1 }] : [];
  360. return result;
  361. }
  362. async _insertPosData(transaction, data, tid, cid) {
  363. data.id = this.uuid.v4();
  364. data.tid = tid;
  365. // todo 新增期
  366. data.add_stage = 0;
  367. data.add_times = 0;
  368. data.in_time = new Date();
  369. data.add_user = this.ctx.session.sessionUser.accountId;
  370. data.ccid = cid;
  371. if (data.quantity) {
  372. const bills = await this.ctx.service.changeLedger.getDataById(data.lid) || await this.ctx.service.ledger.getDataById(data.lid);
  373. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  374. data.quantity = this.round(data.quantity, precision.value);
  375. }
  376. const addRst = await transaction.insert(this.tableName, data);
  377. }
  378. /**
  379. * 删除清单下部位明细数据(删除清单时调用)
  380. *
  381. * @param transaction - 事务
  382. * @param tid - 标段id
  383. * @param lid - 清单id
  384. * @returns {Promise<void>}
  385. */
  386. async deletePosData(transaction, tid, lid) {
  387. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  388. }
  389. }
  390. return ChangePos;
  391. };