revise_pos.js 17 KB

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