tender_info.js 16 KB

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