change_pos.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 = ctx.app.config.table_depart.heavy;
  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. async deleteBySettle(transaction, tid, posDatas) {
  275. if (!posDatas || posDatas.length === 0) {
  276. return;
  277. }
  278. await transaction.delete(this.tableName, { id: this._.map(posDatas, 'id') });
  279. await this.ctx.service.changeAuditList.deleteDataByRevise(transaction, tid, this._.map(posDatas, 'id'), 'mx_id');
  280. }
  281. /**
  282. * 复制粘贴 部位明细数据
  283. * @param {Array} data - 复制粘贴的数据
  284. * @param {Number} tid - 标段id
  285. * @returns {Promise<{ledger: {}, pos: null}>}
  286. */
  287. async pastePosData(tid, cid, data) {
  288. if (!(data instanceof Array)) throw '提交数据错误';
  289. const transaction = await this.db.beginTransaction();
  290. const result = { ledger: {}, pos: null };
  291. const orgPos = await this.getPosData({ tid: tid, id: this._.map(data, 'id') });
  292. // const bills = await this.ctx.service.reviseBills.getDataById(data[0].lid);
  293. // const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  294. // const updateBills = {id: bills.id};
  295. // const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: bills.id} });
  296. let bills = await this.ctx.service.changeLedger.getDataById(data[0].lid);
  297. let newBills = false;
  298. let billsPos = await this.getAllDataByCondition({ where: { tid: tid, lid: data[0].lid } });
  299. if (bills) {
  300. newBills = true;
  301. } else {
  302. bills = await this.ctx.service.ledger.getDataById(data[0].lid);
  303. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tid, lid: data[0].lid } });
  304. billsPos = this._.concat(posData, billsPos);
  305. }
  306. const updateBills = { id: bills.id };
  307. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  308. let needUpdateBills;
  309. for (const bp of billsPos) {
  310. const d = data.find(function(x) {
  311. return bp.id ? x.id === bp.id : false;
  312. });
  313. if (d) continue;
  314. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  315. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  316. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  317. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  318. }
  319. try {
  320. for (const d of data) {
  321. const op = d.id ? this._.find(orgPos, { id: d.id }) : null;
  322. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  323. if (d.sgfh_qty !== undefined) {
  324. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  325. } else if (op) {
  326. d.sgfh_qty = op.sgfh_qty;
  327. }
  328. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  329. if (d.sjcl_qty !== undefined) {
  330. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  331. } else if (op) {
  332. d.sjcl_qty = op.sjcl_qty;
  333. }
  334. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  335. if (d.qtcl_qty) {
  336. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  337. } else if (op) {
  338. d.qtcl_qty = op.qtcl_qty;
  339. }
  340. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  341. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  342. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  343. needUpdateBills = true;
  344. }
  345. if (d.id) {
  346. await transaction.update(this.tableName, d);
  347. } else {
  348. updateBills.cid = 1;
  349. await this._insertPosData(transaction, d, tid, cid);
  350. }
  351. }
  352. const info = this.ctx.tender.info;
  353. if (needUpdateBills) {
  354. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  355. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  356. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  357. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  358. if (newBills) await transaction.update(this.ctx.service.changeLedger.tableName, updateBills);
  359. updateBills.ledger_id = bills.ledger_id;
  360. }
  361. await this.ctx.service.changeAuditList.updateDataByRevisePos(transaction, tid, data);
  362. await transaction.commit();
  363. } catch (err) {
  364. await transaction.rollback();
  365. throw err;
  366. }
  367. result.pos = data;
  368. result.ledger.update = needUpdateBills ? [updateBills] : updateBills.cid && updateBills.cid === 1 ? [{ id: bills.id, ledger_id: bills.ledger_id, cid: 1 }] : [];
  369. return result;
  370. }
  371. async _insertPosData(transaction, data, tid, cid) {
  372. data.id = this.uuid.v4();
  373. data.tid = tid;
  374. // todo 新增期
  375. data.add_stage = 0;
  376. data.add_stage_order = 0;
  377. data.add_times = 0;
  378. data.in_time = new Date();
  379. data.add_user = this.ctx.session.sessionUser.accountId;
  380. data.ccid = cid;
  381. data.formc = 1;
  382. if (data.quantity) {
  383. const bills = await this.ctx.service.changeLedger.getDataById(data.lid) || await this.ctx.service.ledger.getDataById(data.lid);
  384. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  385. data.quantity = this.round(data.quantity, precision.value);
  386. }
  387. const addRst = await transaction.insert(this.tableName, data);
  388. }
  389. /**
  390. * 删除清单下部位明细数据(删除清单时调用)
  391. *
  392. * @param transaction - 事务
  393. * @param tid - 标段id
  394. * @param lid - 清单id
  395. * @returns {Promise<void>}
  396. */
  397. async deletePosData(transaction, tid, lid) {
  398. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  399. }
  400. }
  401. return ChangePos;
  402. };