tender_info.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/10/30
  7. * @version
  8. */
  9. const infoConst = require('../const/tender_info');
  10. const parseInfo = infoConst.parseInfo;
  11. const arrayInfo = infoConst.arrayInfo;
  12. const defaultInfo = infoConst.defaultInfo;
  13. const advanceConst = require('../const/audit').advance;
  14. module.exports = app => {
  15. class TenderInfo extends app.BaseService {
  16. /**
  17. * 构造函数
  18. *
  19. * @param {Object} ctx - egg全局变量
  20. * @return {void}
  21. */
  22. constructor(ctx) {
  23. super(ctx);
  24. this.tableName = 'tender_info';
  25. }
  26. /**
  27. * 新增 标段相关信息
  28. *
  29. * @param tenderId - 标段Id
  30. * @param projectId - 项目Id
  31. * @param transaction - 事务
  32. * @return {Promise<void>}
  33. */
  34. async addTenderInfo(tenderId, projectId, transaction) {
  35. const info = JSON.parse(JSON.stringify(defaultInfo));
  36. info.tid = tenderId;
  37. info.pid = projectId;
  38. for (const pi of parseInfo) {
  39. info[pi] = JSON.stringify(info[pi]);
  40. }
  41. for (const pi of arrayInfo) {
  42. info[pi] = JSON.stringify(info[pi]);
  43. }
  44. if (transaction) {
  45. await transaction.insert(this.tableName, info);
  46. } else {
  47. await this.db.insert(this.tableName, info);
  48. }
  49. return info;
  50. }
  51. /**
  52. * 获取标段相关信息
  53. * @param tenderId
  54. * @return {Promise<void>}
  55. */
  56. async getTenderInfo(tenderId) {
  57. let info = await this.getDataByCondition({ tid: tenderId });
  58. // 兼容不存在info的情况
  59. if (!info) {
  60. info = await this.addTenderInfo(tenderId, this.ctx.session.sessionProject.id);
  61. }
  62. for (const pi of parseInfo) {
  63. info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
  64. this.ctx.helper._.defaults(info[pi], defaultInfo[pi]);
  65. }
  66. for (const ai of arrayInfo) {
  67. info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
  68. }
  69. if (info.decimal) {
  70. info.decimal._pay_tp = info.decimal.pay ? info.decimal.payTp : info.decimal.tp;
  71. }
  72. return info;
  73. }
  74. /**
  75. * 获取标段相关信息(报表用)
  76. * @param tenderId
  77. * @return {Promise<void>}
  78. */
  79. async getTenderInfoEx(tenderId) {
  80. const sql = 'select t2.name, t1.* from zh_tender_info t1 inner join zh_tender t2 on t2.id = t1.tid where t1.tid = ?';
  81. const sqlParam = [tenderId];
  82. const list = await this.db.query(sql, sqlParam);
  83. const info = list[0];
  84. const len = info.deal_info.length;
  85. info.deal_info = info.deal_info.slice(0, len - 1) + ',"name":"' + info.name + '"' + info.deal_info.slice(len - 1);
  86. for (const pi of parseInfo) {
  87. info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
  88. this.ctx.helper._.defaults(info[pi], defaultInfo[pi]);
  89. }
  90. for (const ai of arrayInfo) {
  91. info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
  92. }
  93. if (info.decimal) {
  94. info.decimal._pay_tp = info.decimal.pay ? info.decimal.payTp : info.decimal.tp;
  95. }
  96. return info;
  97. }
  98. /**
  99. * 保存标段相关信息
  100. *
  101. * @param data
  102. * @return {Promise<void>}
  103. */
  104. async saveTenderInfo(tenderId, data) {
  105. for (const di in data) {
  106. if (parseInfo.indexOf(di) >= 0 || arrayInfo.indexOf(di) >= 0) {
  107. data[di] = JSON.stringify(data[di]);
  108. }
  109. }
  110. await this.db.update(this.tableName, data, { where: { tid: tenderId } });
  111. }
  112. async savePrecision(tenderId, newPrecision, oldPrecision, decimal) {
  113. const changePrecision = [];
  114. const units = await this.ctx.service.ledger.getTenderUsedUnits(tenderId);
  115. const defUnits = this._.map(newPrecision, 'units');
  116. const otherUnits = units.filter(function(u) { return defUnits.indexOf(u) === -1; });
  117. let changeUnits = [];
  118. for (const prop in newPrecision) {
  119. if (oldPrecision[prop]) {
  120. if (newPrecision[prop].value < oldPrecision[prop].value) {
  121. changePrecision.push(newPrecision[prop]);
  122. }
  123. } else {
  124. changePrecision.push(newPrecision[prop]);
  125. }
  126. }
  127. for (const cp of changePrecision) {
  128. if (cp.unit) {
  129. changeUnits.push(cp.unit);
  130. } else {
  131. changeUnits.push(otherUnits);
  132. }
  133. }
  134. changeUnits = this._.flatten(changeUnits);
  135. if (changeUnits.length > 0) {
  136. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  137. columns: ['id', 'unit', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'],
  138. where: { tender_id: tenderId, unit: changeUnits, is_leaf: true },
  139. });
  140. const pos = changeUnits.length > 0 ? await this.ctx.service.pos.getPosDataByUnits(tenderId, changeUnits) : [];
  141. for (const b of bills) {
  142. const precision = this.ctx.helper.findPrecision(newPrecision, b.unit);
  143. const bPos = this._.filter(pos, { lid: b.id });
  144. if (bPos.length > 0) {
  145. let sgfh_qty = 0,
  146. sjcl_qty = 0,
  147. qtcl_qty = 0,
  148. quantity = 0;
  149. for (const p of bPos) {
  150. this.ctx.helper.checkFieldPrecision(p, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty'], precision.value);
  151. p.quantity = this.ctx.helper.add(this.ctx.helper.add(p.sgfh_qty, p.sjcl_qty), p.qtcl_qty);
  152. sgfh_qty = this.ctx.helper.add(sgfh_qty, p.sgfh_qty);
  153. sjcl_qty = this.ctx.helper.add(sjcl_qty, p.sjcl_qty);
  154. qtcl_qty = this.ctx.helper.add(qtcl_qty, p.qtcl_qty);
  155. quantity = this.ctx.helper.add(quantity, p.quantity);
  156. }
  157. b.sgfh_qty = sgfh_qty;
  158. b.sjcl_qty = sjcl_qty;
  159. b.qtcl_qty = qtcl_qty;
  160. b.quantity = quantity;
  161. } else {
  162. this.ctx.helper.checkFieldPrecision(b, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'], precision.value);
  163. }
  164. b.quantity = this.ctx.helper.add(this.ctx.helper.add(b.sgfh_qty, b.sjcl_qty), b.qtcl_qty);
  165. b.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, b.unit_price, decimal.tp);
  166. b.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, b.unit_price, decimal.tp);
  167. b.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, b.unit_price, decimal.tp);
  168. b.deal_tp = this.ctx.helper.mul(b.deal_qty, b.unit_price, decimal.tp);
  169. b.total_price = this.ctx.helper.mul(b.quantity, b.unit_price, decimal.tp);
  170. }
  171. const transaction = await this.db.beginTransaction();
  172. try {
  173. await transaction.update(this.tableName,
  174. { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
  175. if (bills.length > 0) await transaction.updateRows(this.ctx.service.ledger.tableName, bills);
  176. if (pos.length > 0) await transaction.updateRows(this.ctx.service.pos.tableName, pos);
  177. await transaction.commit();
  178. } catch (err) {
  179. await transaction.rollback();
  180. throw err;
  181. }
  182. } else {
  183. await this.db.update(this.tableName,
  184. { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
  185. }
  186. }
  187. async _reCalcLedger(tenderId, newDecimal, oldDecimal) {
  188. const changeBills = [];
  189. const calcUp = newDecimal.up < oldDecimal.up, calcTp = newDecimal.tp !== oldDecimal.tp;
  190. if (calcUp || calcTp) {
  191. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  192. columns: ['id', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'quantity'],
  193. where: { tender_id: tenderId, is_leaf: true },
  194. });
  195. for (const b of bills) {
  196. const cb = { id: b.id };
  197. cb.unit_price = calcUp ? this.ctx.helper.round(b.unit_price, newDecimal.up) : b.unit_price;
  198. cb.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, cb.unit_price, newDecimal.tp);
  199. cb.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, cb.unit_price, newDecimal.tp);
  200. cb.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, cb.unit_price, newDecimal.tp);
  201. cb.deal_tp = this.ctx.helper.mul(b.deal_qty, cb.unit_price, newDecimal.tp);
  202. cb.total_price = this.ctx.helper.mul(b.quantity, cb.unit_price, newDecimal.tp);
  203. changeBills.push(cb);
  204. }
  205. }
  206. return changeBills;
  207. }
  208. async _reCalcStageExtra(tenderId, newDecimal, oldDecimal) {
  209. let changeSj = [], changeSb = [], changeSo = [];
  210. const stage = await this.ctx.service.stage.getLastestStage(tenderId, true);
  211. if (!stage) return [changeSj, changeSb, changeSo];
  212. const upDecimal = newDecimal.up, tpDecimal = newDecimal.extra ? newDecimal.extraTp : newDecimal.tp;
  213. const calcUp = upDecimal < oldDecimal.up,
  214. calcTp = tpDecimal < (oldDecimal.extra ? oldDecimal.extraTp : oldDecimal.tp);
  215. console.log(calcUp, calcTp);
  216. if (calcUp || calcTp) {
  217. const stageJgcl = await this.ctx.service.stageJgcl.getAllDataByCondition({
  218. columns: ['id', 'unit_price', 'arrive_qty', 'deduct_qty'],
  219. where: { sid: stage.id }
  220. });
  221. for (const sj of stageJgcl) {
  222. const cj = { id: sj.id };
  223. cj.unit_price = calcUp ? this.ctx.helper.round(sj.unit_price, upDecimal) : sj.unit_price;
  224. cj.arrive_tp = this.ctx.helper.mul(sj.arrive_qty, sj.unit_price, tpDecimal);
  225. cj.deduct_tp = this.ctx.helper.mul(sj.deduct_qty, sj.unit_price, tpDecimal);
  226. changeSj.push(cj);
  227. }
  228. }
  229. if (calcTp) {
  230. changeSb = await this.ctx.service.stageBonus.getAllDataByCondition({
  231. columns: ['id', 'tp'],
  232. where: { sid: stage.id }
  233. });
  234. for (const cb of changeSb) {
  235. cb.tp = this.ctx.helper.round(cb.tp, tpDecimal);
  236. }
  237. changeSo = await this.ctx.service.stageOther.getAllDataByCondition({
  238. columns: ['id', 'total_price', 'tp'],
  239. where: { sid: stage.id }
  240. });
  241. for (const co of changeSo) {
  242. if (stage.order === 1) co.total_price = this.ctx.helper.round(co.total_price, tpDecimal);
  243. co.tp = this.ctx.helper.round(co.tp, tpDecimal);
  244. }
  245. }
  246. return [changeSj, changeSb, changeSo];
  247. }
  248. async saveDecimal(tenderId, newDecimal, oldDecimal) {
  249. const changeAdvanceBills = [];
  250. const caclPayTp = (newDecimal.pay ? newDecimal.payTp : newDecimal.tp) < (oldDecimal.pay ? oldDecimal.payTp : oldDecimal.tp);
  251. if (caclPayTp) {
  252. // 获取预付款需要修改的相关记录
  253. const ad_bills = await this.ctx.service.advance.getAllDataByCondition({
  254. columns: ['id', 'cur_amount', 'prev_amount', 'prev_total_amount'],
  255. where: { status: [advanceConst.status.uncheck, advanceConst.status.checkNo], tid: tenderId },
  256. });
  257. const decimal = newDecimal.pay ? newDecimal.payTp : newDecimal.tp;
  258. // 根据精度重新计算相关金额
  259. for (const ad of ad_bills) {
  260. const cb = { id: ad.id };
  261. const cur_amount = this.ctx.helper.round(ad.cur_amount, decimal);
  262. cb.cur_amount = cur_amount;
  263. cb.prev_total_amount = this.ctx.helper.add(cur_amount, ad.prev_amount);
  264. changeAdvanceBills.push(cb);
  265. }
  266. }
  267. const changeBills = await this._reCalcLedger(tenderId, newDecimal, oldDecimal);
  268. const [changeSj, changeSb, changeSo] = await this._reCalcStageExtra(tenderId, newDecimal, oldDecimal);
  269. if (changeBills.length > 0 ||
  270. changeAdvanceBills.length > 0 ||
  271. changeSj.length > 0 || changeSb.length > 0 || changeSo.length > 0) {
  272. const transaction = await this.db.beginTransaction();
  273. try {
  274. await transaction.update(this.tableName,
  275. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  276. if (changeBills.length > 0) await transaction.updateRows(this.ctx.service.ledger.tableName, changeBills);
  277. if (changeSj.length > 0) await transaction.updateRows(this.ctx.service.stageJgcl.tableName, changeSj);
  278. if (changeSb.length > 0) await transaction.updateRows(this.ctx.service.stageBonus.tableName, changeSb);
  279. if (changeSo.length > 0) await transaction.updateRows(this.ctx.service.stageOther.tableName, changeSo);
  280. if (changeAdvanceBills.length) await transaction.updateRows(this.ctx.service.advance.tableName, changeAdvanceBills);
  281. await transaction.commit();
  282. } catch (error) {
  283. await transaction.rollback();
  284. throw error;
  285. }
  286. } else {
  287. await this.db.update(this.tableName,
  288. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  289. }
  290. }
  291. }
  292. return TenderInfo;
  293. };