revise_pos.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. console.log(data);
  169. if (data.length === 0) return;
  170. const op = await this.getDataById(data[0].id);
  171. const bills = await this.ctx.service.reviseBills.getDataById(op.lid);
  172. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  173. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  174. let needUpdateBills;
  175. for (const d of data) {
  176. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  177. if (d.sgfh_qty !== undefined) {
  178. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  179. } else if (op) {
  180. d.sgfh_qty = op.sgfh_qty;
  181. }
  182. if (d.sjcl_qty !== undefined) {
  183. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  184. } else if (op) {
  185. d.sjcl_qty = op.sjcl_qty;
  186. }
  187. if (d.qtcl_qty !== undefined) {
  188. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  189. } else if (op) {
  190. d.qtcl_qty = op.qtcl_qty;
  191. }
  192. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  193. needUpdateBills = true;
  194. }
  195. }
  196. const updateBills = {id: bills.id};
  197. if (needUpdateBills) {
  198. for (const bp of billsPos) {
  199. const newPos = data.find(function (x) { return x.id === bp.id });
  200. updateBills.sgfh_qty = newPos && newPos.sgfh_qty !== undefined
  201. ? this.ctx.helper.add(updateBills.sgfh_qty, newPos.sgfh_qty)
  202. : this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  203. updateBills.sjcl_qty = newPos && newPos.sjcl_qty !== undefined
  204. ? this.ctx.helper.add(updateBills.sjcl_qty, newPos.sjcl_qty)
  205. : this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  206. updateBills.qtcl_qty = newPos && newPos.qtcl_qty !== undefined
  207. ? this.ctx.helper.add(updateBills.qtcl_qty, newPos.qtcl_qty)
  208. : this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  209. updateBills.quantity = newPos && newPos.quantity !== undefined
  210. ? this.ctx.helper.add(updateBills.quantity, newPos.quantity)
  211. : this.ctx.helper.add(updateBills.quantity, bp.quantity);
  212. }
  213. const info = this.ctx.tender.info;
  214. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  215. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  216. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  217. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  218. }
  219. const transaction = await this.db.beginTransaction();
  220. try {
  221. for (const d of data) {
  222. await transaction.update(this.tableName, d);
  223. }
  224. if (needUpdateBills) await transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  225. await transaction.commit();
  226. updateBills.ledger_id = bills.ledger_id;
  227. return {
  228. ledger: { update: needUpdateBills ? [updateBills] : [] },
  229. pos: data,
  230. }
  231. } catch(err) {
  232. await transaction.rollback();
  233. throw err;
  234. }
  235. }
  236. async deletePos(tid, data) {
  237. if (!data || data.length === 0) {
  238. throw '提交数据错误';
  239. }
  240. const pos = await this.getPosData({tid: tid, id: data});
  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. await transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  260. await transaction.commit();
  261. updateBills.ledger_id = bills.ledger_id;
  262. return {
  263. ledger: { update: [updateBills] },
  264. pos: data
  265. };
  266. } catch(err) {
  267. await transaction.rollback();
  268. throw err;
  269. }
  270. }
  271. /**
  272. * 复制粘贴 部位明细数据
  273. * @param {Array} data - 复制粘贴的数据
  274. * @param {Number} tid - 标段id
  275. * @returns {Promise<{ledger: {}, pos: null}>}
  276. */
  277. async pastePosData(tid, rid, data) {
  278. if (!(data instanceof Array)) throw '提交数据错误';
  279. const transaction = await this.db.beginTransaction();
  280. const result = { ledger: {}, pos: null };
  281. const orgPos = await this.getPosData({tid: tid, id: this._.map(data, 'id')});
  282. const bills = await this.ctx.service.reviseBills.getDataById(data[0].lid);
  283. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  284. const updateBills = {id: bills.id};
  285. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: bills.id} });
  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. this._insertPosData(transaction, d, tid, rid);
  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. await transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  336. updateBills.ledger_id = bills.ledger_id;
  337. }
  338. await transaction.commit();
  339. } catch (err) {
  340. await transaction.rollback();
  341. throw err;
  342. }
  343. result.pos = data;
  344. result.ledger.update = needUpdateBills ? [updateBills] : [];
  345. return result;
  346. }
  347. }
  348. return RevisePos;
  349. };