change_pos.js 21 KB

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