change_pos.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. return { pos: data };
  57. } catch (err) {
  58. await transaction.rollback();
  59. throw err;
  60. }
  61. }
  62. async updatePos(tid, data) {
  63. if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined) {
  64. const op = await this.getDataById(data.id);
  65. let bills = await this.ctx.service.changeLedger.getDataById(op.lid);
  66. let newBills = false;
  67. let billsPos = await this.getAllDataByCondition({ where: { tid: tid, lid: op.lid } });
  68. if (bills) {
  69. newBills = true;
  70. } else {
  71. bills = await this.ctx.service.ledger.getDataById(op.lid);
  72. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tid, lid: op.lid } });
  73. billsPos = this._.concat(posData, billsPos);
  74. }
  75. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  76. if (data.sgfh_qty !== undefined) {
  77. data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
  78. } else if (op) {
  79. data.sgfh_qty = op.sgfh_qty;
  80. }
  81. if (data.sjcl_qty !== undefined) {
  82. data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
  83. } else if (op) {
  84. data.sjcl_qty = op.sjcl_qty;
  85. }
  86. if (data.qtcl_qty !== undefined) {
  87. data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
  88. } else if (op) {
  89. data.qtcl_qty = op.qtcl_qty;
  90. }
  91. data.quantity = this.ctx.helper.sum([data.sgfh_qty, data.qtcl_qty, data.sjcl_qty]);
  92. const updateBills = { id: bills.id };
  93. for (const bp of billsPos) {
  94. const calcData = bp.id === data.id ? data : bp;
  95. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  96. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  97. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  98. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  99. }
  100. const info = this.ctx.tender.info;
  101. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  102. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  103. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  104. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  105. const transaction = await this.db.beginTransaction();
  106. try {
  107. await transaction.update(this.tableName, data);
  108. if (newBills) await transaction.update(this.ctx.service.changeLedger.tableName, updateBills);
  109. await this.ctx.service.changeAuditList.updateDataByRevisePos(transaction, tid, [data]);
  110. await transaction.commit();
  111. updateBills.ledger_id = bills.ledger_id;
  112. return {
  113. ledger: { update: [updateBills] },
  114. pos: data,
  115. };
  116. } catch (err) {
  117. await transaction.rollback();
  118. throw err;
  119. }
  120. } else {
  121. const transaction = await this.db.beginTransaction();
  122. try {
  123. await transaction.update(this.tableName, data, { tid, id: data.id });
  124. await this.ctx.service.changeAuditList.updateDataByRevisePos(transaction, tid, [data]);
  125. await transaction.commit();
  126. return { pos: data };
  127. } catch (err) {
  128. await transaction.rollback();
  129. throw err;
  130. }
  131. }
  132. }
  133. async updatePosArr(tid, data) {
  134. console.log(data);
  135. if (data.length === 0) return;
  136. const op = await this.getDataById(data[0].id) || await this.ctx.service.pos.getDataById(data[0].id);
  137. let bills = await this.ctx.service.changeLedger.getDataById(op.lid);
  138. let newBills = false;
  139. let billsPos = await this.getAllDataByCondition({ where: { tid: tid, lid: op.lid } });
  140. if (bills) {
  141. newBills = true;
  142. } else {
  143. bills = await this.ctx.service.ledger.getDataById(op.lid);
  144. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tid, lid: op.lid } });
  145. billsPos = this._.concat(posData, billsPos);
  146. }
  147. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  148. let needUpdateBills;
  149. for (const d of data) {
  150. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  151. if (d.sgfh_qty !== undefined) {
  152. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  153. } else if (op) {
  154. d.sgfh_qty = op.sgfh_qty;
  155. }
  156. if (d.sjcl_qty !== undefined) {
  157. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  158. } else if (op) {
  159. d.sjcl_qty = op.sjcl_qty;
  160. }
  161. if (d.qtcl_qty !== undefined) {
  162. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  163. } else if (op) {
  164. d.qtcl_qty = op.qtcl_qty;
  165. }
  166. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  167. needUpdateBills = true;
  168. }
  169. }
  170. const updateBills = { id: bills.id };
  171. if (needUpdateBills) {
  172. for (const bp of billsPos) {
  173. const newPos = data.find(function(x) { return x.id === bp.id; });
  174. updateBills.sgfh_qty = newPos && newPos.sgfh_qty !== undefined
  175. ? this.ctx.helper.add(updateBills.sgfh_qty, newPos.sgfh_qty)
  176. : this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  177. updateBills.sjcl_qty = newPos && newPos.sjcl_qty !== undefined
  178. ? this.ctx.helper.add(updateBills.sjcl_qty, newPos.sjcl_qty)
  179. : this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  180. updateBills.qtcl_qty = newPos && newPos.qtcl_qty !== undefined
  181. ? this.ctx.helper.add(updateBills.qtcl_qty, newPos.qtcl_qty)
  182. : this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  183. updateBills.quantity = newPos && newPos.quantity !== undefined
  184. ? this.ctx.helper.add(updateBills.quantity, newPos.quantity)
  185. : this.ctx.helper.add(updateBills.quantity, bp.quantity);
  186. }
  187. const info = this.ctx.tender.info;
  188. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  189. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  190. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  191. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  192. }
  193. const transaction = await this.db.beginTransaction();
  194. try {
  195. for (const d of data) {
  196. // 区分d属于pos还是changePos,分别更新对应的表
  197. const np = await this.getDataById(d.id);
  198. np ? await transaction.update(this.tableName, d) : await transaction.update(this.ctx.service.pos.tableName, d);
  199. }
  200. if (needUpdateBills && newBills) await transaction.update(this.ctx.service.changeLedger.tableName, updateBills);
  201. await this.ctx.service.changeAuditList.updateDataByRevisePos(transaction, tid, data);
  202. await transaction.commit();
  203. updateBills.ledger_id = bills.ledger_id;
  204. return {
  205. ledger: { update: needUpdateBills ? [updateBills] : [] },
  206. pos: data,
  207. };
  208. } catch (err) {
  209. await transaction.rollback();
  210. throw err;
  211. }
  212. }
  213. async deletePos(tid, data) {
  214. if (!data || data.length === 0) {
  215. throw '提交数据错误';
  216. }
  217. const pos = await this.getPosData({tid: tid, id: data});
  218. let bills = await this.ctx.service.changeLedger.getDataById(pos[0].lid);
  219. let newBills = false;
  220. let billsPos = await this.getAllDataByCondition({ where: { tid: tid, lid: pos[0].lid } });
  221. if (bills) {
  222. newBills = true;
  223. } else {
  224. bills = await this.ctx.service.ledger.getDataById(pos[0].lid);
  225. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tid, lid: pos[0].lid } });
  226. billsPos = this._.concat(posData, billsPos);
  227. }
  228. // const bills = await this.ctx.service.reviseBills.getDataById(pos[0].lid);
  229. // const billsPos = await this.getAllDataByCondition({ where: {tid: tid, lid: bills.id} });
  230. const updateBills = {id: bills.id, sgfh_qty: null, sjcl_qty: null, qtcl_qty: null, quantity: null};
  231. for (const bp of billsPos) {
  232. if (data.indexOf(bp.id) >= 0) continue;
  233. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  234. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  235. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  236. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  237. }
  238. const info = this.ctx.tender.info;
  239. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  240. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  241. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  242. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  243. const transaction = await this.db.beginTransaction();
  244. try {
  245. await transaction.delete(this.tableName, {tid: tid, id: data});
  246. if (newBills) await transaction.update(this.ctx.service.changeLedger.tableName, updateBills);
  247. await this.ctx.service.changeAuditList.deleteDataByRevise(transaction, tid, data, 'mx_id');
  248. await transaction.commit();
  249. updateBills.ledger_id = bills.ledger_id;
  250. return {
  251. ledger: { update: [updateBills] },
  252. pos: data,
  253. };
  254. } catch (err) {
  255. await transaction.rollback();
  256. throw err;
  257. }
  258. }
  259. /**
  260. * 复制粘贴 部位明细数据
  261. * @param {Array} data - 复制粘贴的数据
  262. * @param {Number} tid - 标段id
  263. * @returns {Promise<{ledger: {}, pos: null}>}
  264. */
  265. async pastePosData(tid, cid, data) {
  266. if (!(data instanceof Array)) throw '提交数据错误';
  267. const transaction = await this.db.beginTransaction();
  268. const result = { ledger: {}, pos: null };
  269. const orgPos = await this.getPosData({ tid: tid, id: this._.map(data, 'id') });
  270. // const bills = await this.ctx.service.reviseBills.getDataById(data[0].lid);
  271. // const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  272. // const updateBills = {id: bills.id};
  273. // const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: bills.id} });
  274. let bills = await this.ctx.service.changeLedger.getDataById(data[0].lid);
  275. let newBills = false;
  276. let billsPos = await this.getAllDataByCondition({ where: { tid: tid, lid: data[0].lid } });
  277. if (bills) {
  278. newBills = true;
  279. } else {
  280. bills = await this.ctx.service.ledger.getDataById(data[0].lid);
  281. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tid, lid: data[0].lid } });
  282. billsPos = this._.concat(posData, billsPos);
  283. }
  284. const updateBills = { id: bills.id };
  285. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  286. let needUpdateBills;
  287. for (const bp of billsPos) {
  288. const d = data.find(function(x) {
  289. return bp.id ? x.id === bp.id : false;
  290. });
  291. if (d) continue;
  292. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  293. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  294. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  295. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  296. }
  297. try {
  298. for (const d of data) {
  299. const op = d.id ? this._.find(orgPos, { id: d.id }) : null;
  300. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  301. if (d.sgfh_qty !== undefined) {
  302. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  303. } else if (op) {
  304. d.sgfh_qty = op.sgfh_qty;
  305. }
  306. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  307. if (d.sjcl_qty !== undefined) {
  308. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  309. } else if (op) {
  310. d.sjcl_qty = op.sjcl_qty;
  311. }
  312. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  313. if (d.qtcl_qty) {
  314. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  315. } else if (op) {
  316. d.qtcl_qty = op.qtcl_qty;
  317. }
  318. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  319. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  320. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  321. needUpdateBills = true;
  322. }
  323. if (d.id) {
  324. await transaction.update(this.tableName, d);
  325. } else {
  326. await this._insertPosData(transaction, d, tid, cid);
  327. }
  328. }
  329. const info = this.ctx.tender.info;
  330. if (needUpdateBills) {
  331. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  332. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  333. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  334. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  335. if (newBills) await transaction.update(this.ctx.service.changeLedger.tableName, updateBills);
  336. updateBills.ledger_id = bills.ledger_id;
  337. }
  338. await this.ctx.service.changeAuditList.updateDataByRevisePos(transaction, tid, data);
  339. await transaction.commit();
  340. } catch (err) {
  341. await transaction.rollback();
  342. throw err;
  343. }
  344. result.pos = data;
  345. result.ledger.update = needUpdateBills ? [updateBills] : [];
  346. return result;
  347. }
  348. async _insertPosData(transaction, data, tid, cid) {
  349. data.id = this.uuid.v4();
  350. data.tid = tid;
  351. // todo 新增期
  352. data.add_stage = 0;
  353. data.add_times = 0;
  354. data.in_time = new Date();
  355. data.add_user = this.ctx.session.sessionUser.accountId;
  356. data.ccid = cid;
  357. if (data.quantity) {
  358. const bills = await this.ctx.service.changeLedger.getDataById(data.lid) || await this.ctx.service.ledger.getDataById(data.lid);
  359. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  360. data.quantity = this.round(data.quantity, precision.value);
  361. }
  362. const addRst = await transaction.insert(this.tableName, data);
  363. }
  364. /**
  365. * 删除清单下部位明细数据(删除清单时调用)
  366. *
  367. * @param transaction - 事务
  368. * @param tid - 标段id
  369. * @param lid - 清单id
  370. * @returns {Promise<void>}
  371. */
  372. async deletePosData(transaction, tid, lid) {
  373. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  374. }
  375. }
  376. return ChangePos;
  377. };