change_pos.js 21 KB

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