revise_pos.js 18 KB

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