revise_pos.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class RevisePos extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'revise_pos';
  20. }
  21. async getPosData(condition) {
  22. const sql = 'SELECT p.id, p.tid, p.lid, p.name, p.quantity, p.position, p.drawing_code, p.sgfh_qty, ' +
  23. ' p.sjcl_qty, p.qtcl_qty, p.porder, p.add_stage, p.add_times, p.add_user, s.order As add_stage_order,' +
  24. ' p.sgfh_expr, p.sjcl_expr, p.qtcl_expr ' +
  25. ' FROM ' + this.tableName + ' p ' +
  26. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s' +
  27. ' ON add_stage = s.id'
  28. + this.ctx.helper.whereSql(condition, 'p') +
  29. ' ORDER By p.porder ASC';
  30. return await this.db.query(sql);
  31. }
  32. /**
  33. * 获取 修订 清单数据
  34. * @param {Number}tid - 标段id
  35. * @param {uuid}rid - 修订id
  36. * @returns {Promise<void>}
  37. */
  38. async getData(tid) {
  39. return await this.db.select(this.tableName, {
  40. where: {tid: tid}
  41. });
  42. }
  43. async getDataByLid(tid, lid) {
  44. return await this.db.select(this.tableName, {
  45. where: {tid: tid, lid: lid}
  46. });
  47. }
  48. async insertLedgerPosData(transaction, tid, rid, bills, data) {
  49. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  50. const insertDatas = [];
  51. for (const d of data) {
  52. const inD = {
  53. id: this.uuid.v4(), tid: tid, lid: bills.id, crid: rid,
  54. add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  55. name: d.name, drawing_code: d.drawing_code,
  56. };
  57. if (d.quantity) {
  58. inD.sgfh_qty = this.round(d.quantity, precision.value);
  59. inD.quantity = inD.sgfh_qty;
  60. }
  61. insertDatas.push(inD);
  62. }
  63. await transaction.insert(this.tableName, insertDatas);
  64. }
  65. /**
  66. * 删除清单下部位明细数据(删除清单时调用)
  67. *
  68. * @param transaction - 事务
  69. * @param tid - 标段id
  70. * @param lid - 清单id
  71. * @returns {Promise<void>}
  72. */
  73. async deletePosData(transaction, tid, lid) {
  74. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  75. }
  76. async _insertPosData(transaction, data, tid, rid) {
  77. data.id = this.uuid.v4();
  78. data.tid = tid;
  79. // todo 新增期
  80. data.add_stage = 0;
  81. data.add_times = 0;
  82. data.in_time = new Date();
  83. data.add_user = this.ctx.session.sessionUser.accountId;
  84. data.crid = rid;
  85. if (data.quantity) {
  86. const bills = await this.ctx.service.reviseBills.getDataById(data.lid);
  87. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  88. data.quantity = this.round(data.quantity, precision.value);
  89. }
  90. const addRst = await transaction.insert(this.tableName, data);
  91. }
  92. async _completeInsertPosData(tid, rid, data) {
  93. data.id = this.uuid.v4();
  94. data.tid = tid;
  95. // todo 新增期
  96. data.add_stage = 0;
  97. data.add_times = 0;
  98. data.in_time = new Date();
  99. data.add_user = this.ctx.session.sessionUser.accountId;
  100. data.crid = rid;
  101. }
  102. async addPos(tid, rid, data) {
  103. if (data instanceof Array) {
  104. for (const d of data) {
  105. this._completeInsertPosData(tid, rid, d);
  106. }
  107. } else {
  108. this._completeInsertPosData(tid, rid, data);
  109. }
  110. this.db.insert(this.tableName, data);
  111. return {pos: data};
  112. }
  113. async updatePos(tid, data) {
  114. if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined) {
  115. const op = await this.getDataById(data.id);
  116. const bills = await this.ctx.service.reviseBills.getDataById(op.lid);
  117. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  118. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  119. if (data.sgfh_qty !== undefined) {
  120. data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
  121. } else if (op) {
  122. data.sgfh_qty = op.sgfh_qty;
  123. }
  124. if (data.sjcl_qty !== undefined) {
  125. data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
  126. } else if (op) {
  127. data.sjcl_qty = op.sjcl_qty;
  128. }
  129. if (data.qtcl_qty !== undefined) {
  130. data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
  131. } else if (op) {
  132. data.qtcl_qty = op.qtcl_qty;
  133. }
  134. data.quantity = this.ctx.helper.sum([data.sgfh_qty, data.qtcl_qty, data.sjcl_qty]);
  135. const updateBills = {id: bills.id};
  136. for (const bp of billsPos) {
  137. const calcData = bp.id === data.id ? data : bp;
  138. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  139. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  140. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  141. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  142. }
  143. const info = this.ctx.tender.info;
  144. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  145. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  146. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  147. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  148. const transaction = await this.db.beginTransaction();
  149. try {
  150. transaction.update(this.tableName, data);
  151. transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  152. await transaction.commit();
  153. updateBills.ledger_id = bills.ledger_id;
  154. return {
  155. ledger: { update: [updateBills] },
  156. pos: data,
  157. }
  158. } catch(err) {
  159. await transaction.rollback();
  160. throw err;
  161. }
  162. } else {
  163. await this.db.update(this.tableName, data, {tid: tid, id: data.id});
  164. return {pos: data};
  165. }
  166. }
  167. async updatePosArr(tid, data) {
  168. if (data.length === 0) return;
  169. const op = await this.getDataById(data[0].id);
  170. const bills = await this.ctx.service.reviseBills.getDataById(op.lid);
  171. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  172. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  173. let needUpdateBills;
  174. for (const d of data) {
  175. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  176. if (d.sgfh_qty !== undefined) {
  177. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  178. } else if (op) {
  179. d.sgfh_qty = op.sgfh_qty;
  180. }
  181. if (d.sjcl_qty !== undefined) {
  182. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  183. } else if (op) {
  184. d.sjcl_qty = op.sjcl_qty;
  185. }
  186. if (d.qtcl_qty !== undefined) {
  187. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  188. } else if (op) {
  189. d.qtcl_qty = op.qtcl_qty;
  190. }
  191. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  192. needUpdateBills = true;
  193. }
  194. }
  195. const updateBills = {id: bills.id};
  196. if (needUpdateBills) {
  197. for (const bp of billsPos) {
  198. const newPos = data.find(function (x) { return x.id === bp.id });
  199. updateBills.sgfh_qty = newPos && newPos.sgfh_qty !== undefined
  200. ? this.ctx.helper.add(updateBills.sgfh_qty, newPos.sgfh_qty)
  201. : this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  202. updateBills.sjcl_qty = newPos && newPos.sjcl_qty !== undefined
  203. ? this.ctx.helper.add(updateBills.sjcl_qty, newPos.sjcl_qty)
  204. : this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  205. updateBills.qtcl_qty = newPos && newPos.qtcl_qty !== undefined
  206. ? this.ctx.helper.add(updateBills.qtcl_qty, newPos.qtcl_qty)
  207. : this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  208. updateBills.quantity = newPos && newPos.quantity !== undefined
  209. ? this.ctx.helper.add(updateBills.quantity, newPos.quantity)
  210. : this.ctx.helper.add(updateBills.quantity, bp.quantity);
  211. }
  212. const info = this.ctx.tender.info;
  213. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  214. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  215. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  216. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  217. }
  218. const transaction = await this.db.beginTransaction();
  219. try {
  220. for (const d of data) {
  221. if (needUpdateBills) await transaction.update(this.tableName, d);
  222. }
  223. if (needUpdateBills) await transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  224. await transaction.commit();
  225. updateBills.ledger_id = bills.ledger_id;
  226. return {
  227. ledger: { update: [updateBills] },
  228. pos: data,
  229. }
  230. } catch(err) {
  231. await transaction.rollback();
  232. throw err;
  233. }
  234. }
  235. async deletePos(tid, data) {
  236. if (!data || data.length === 0) {
  237. throw '提交数据错误';
  238. }
  239. const pos = await this.getPosData({tid: tid, id: data});
  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. for (const bp of billsPos) {
  244. if (data.indexOf(bp.id) >= 0) continue;
  245. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  246. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  247. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  248. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  249. }
  250. const info = this.ctx.tender.info;
  251. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  252. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  253. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  254. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  255. const transaction = await this.db.beginTransaction();
  256. try {
  257. await transaction.delete(this.tableName, {tid: tid, id: data});
  258. await transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  259. await transaction.commit();
  260. updateBills.ledger_id = bills.ledger_id;
  261. return { ledger: { update: [updateBills] }, pos: data };
  262. } catch(err) {
  263. await transaction.rollback();
  264. throw err;
  265. }
  266. }
  267. /**
  268. * 复制粘贴 部位明细数据
  269. * @param {Array} data - 复制粘贴的数据
  270. * @param {Number} tid - 标段id
  271. * @returns {Promise<{ledger: {}, pos: null}>}
  272. */
  273. async pastePosData(tid, rid, data) {
  274. if (!(data instanceof Array)) throw '提交数据错误';
  275. const transaction = await this.db.beginTransaction();
  276. const result = { ledger: {}, pos: null };
  277. const orgPos = await this.getPosData({tid: tid, id: this._.map(data, 'id')});
  278. const bills = await this.ctx.service.reviseBills.getDataById(data[0].lid);
  279. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  280. const updateBills = {id: bills.id};
  281. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: bills.id} });
  282. for (const bp of billsPos) {
  283. const d = data.find(function (x) {
  284. return bp.id ? x.id === bp.id : false;
  285. });
  286. if (d) continue;
  287. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  288. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  289. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  290. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  291. }
  292. try {
  293. for (const d of data) {
  294. const op = d.id ? this._.find(orgPos, {id: d.id}) : null;
  295. if (d.sgfh_qty || d.sjcl_qty || d.qtcl_qty) {
  296. if (d.sgfh_qty !== undefined) {
  297. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  298. } else if (op) {
  299. d.sgfh_qty = op.sgfh_qty;
  300. }
  301. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  302. if (d.sjcl_qty !== undefined) {
  303. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  304. } else if (op) {
  305. d.sjcl_qty = op.sjcl_qty;
  306. }
  307. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  308. if (d.qtcl_qty) {
  309. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  310. } else if (op) {
  311. d.qtcl_qty = op.qtcl_qty;
  312. }
  313. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  314. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  315. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  316. }
  317. if (d.id) {
  318. await transaction.update(this.tableName, d);
  319. } else {
  320. this._insertPosData(transaction, d, tid, rid);
  321. }
  322. }
  323. const info = this.ctx.tender.info;
  324. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  325. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  326. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  327. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  328. await transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  329. updateBills.ledger_id = bills.ledger_id;
  330. await transaction.commit();
  331. } catch (err) {
  332. await transaction.rollback();
  333. throw err;
  334. }
  335. result.pos = data;
  336. result.ledger.update = [updateBills];
  337. return result;
  338. }
  339. }
  340. return RevisePos;
  341. };