revise_pos.js 18 KB

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