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