tender_info.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 _getLedgerService() {
  114. try {
  115. if (this.ctx.tender.data.ledger_status === auditConst.ledger.status.checked) {
  116. const stageCount = await this.ctx.service.stage.count({tid: this.ctx.tender.id});
  117. if (stageCount === 0) {
  118. const revise = await this.ctx.service.ledgerRevise.getLastestRevise(this.ctx.tender.id);
  119. if (revise.status === auditConst.revise.status.uncheck || revise.status === auditConst.revise.status.checkNo) {
  120. return [this.ctx.service.reviseBills, this.ctx.service.revisePos];
  121. }
  122. }
  123. } else {
  124. return [this.ctx.service.ledger, this.ctx.service.pos];
  125. }
  126. } catch(err) {
  127. }
  128. return [];
  129. }
  130. async savePrecision(tenderId, newPrecision, oldPrecision, decimal) {
  131. const changePrecision = [];
  132. const units = await this.ctx.service.ledger.getTenderUsedUnits(tenderId);
  133. const defUnits = this._.map(newPrecision, 'units');
  134. const otherUnits = units.filter(function(u) { return defUnits.indexOf(u) === -1; });
  135. let changeUnits = [];
  136. for (const prop in newPrecision) {
  137. if (oldPrecision[prop]) {
  138. if (newPrecision[prop].value < oldPrecision[prop].value) {
  139. changePrecision.push(newPrecision[prop]);
  140. }
  141. } else {
  142. changePrecision.push(newPrecision[prop]);
  143. }
  144. }
  145. for (const cp of changePrecision) {
  146. if (cp.unit) {
  147. changeUnits.push(cp.unit);
  148. } else {
  149. changeUnits.push(otherUnits);
  150. }
  151. }
  152. changeUnits = this._.flatten(changeUnits);
  153. const [billsService, posService] = await this._getLedgerService();
  154. if (changeUnits.length > 0 && billsService && posService) {
  155. const bills = await billsService.getAllDataByCondition({
  156. columns: ['id', 'unit', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'],
  157. where: { tender_id: tenderId, unit: changeUnits, is_leaf: true },
  158. });
  159. const pos = changeUnits.length > 0 ? await posService.getPosDataByUnits(tenderId, changeUnits) : [];
  160. for (const b of bills) {
  161. const precision = this.ctx.helper.findPrecision(newPrecision, b.unit);
  162. const bPos = this._.filter(pos, { lid: b.id });
  163. if (bPos.length > 0) {
  164. let sgfh_qty = 0,
  165. sjcl_qty = 0,
  166. qtcl_qty = 0,
  167. quantity = 0;
  168. for (const p of bPos) {
  169. this.ctx.helper.checkFieldPrecision(p, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty'], precision.value);
  170. p.quantity = this.ctx.helper.add(this.ctx.helper.add(p.sgfh_qty, p.sjcl_qty), p.qtcl_qty);
  171. sgfh_qty = this.ctx.helper.add(sgfh_qty, p.sgfh_qty);
  172. sjcl_qty = this.ctx.helper.add(sjcl_qty, p.sjcl_qty);
  173. qtcl_qty = this.ctx.helper.add(qtcl_qty, p.qtcl_qty);
  174. quantity = this.ctx.helper.add(quantity, p.quantity);
  175. }
  176. b.sgfh_qty = sgfh_qty;
  177. b.sjcl_qty = sjcl_qty;
  178. b.qtcl_qty = qtcl_qty;
  179. b.quantity = quantity;
  180. } else {
  181. this.ctx.helper.checkFieldPrecision(b, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'], precision.value);
  182. }
  183. b.quantity = this.ctx.helper.add(this.ctx.helper.add(b.sgfh_qty, b.sjcl_qty), b.qtcl_qty);
  184. b.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, b.unit_price, decimal.tp);
  185. b.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, b.unit_price, decimal.tp);
  186. b.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, b.unit_price, decimal.tp);
  187. b.deal_tp = this.ctx.helper.mul(b.deal_qty, b.unit_price, decimal.tp);
  188. b.total_price = this.ctx.helper.mul(b.quantity, b.unit_price, decimal.tp);
  189. }
  190. const transaction = await this.db.beginTransaction();
  191. try {
  192. await transaction.update(this.tableName,
  193. { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
  194. if (bills.length > 0) await transaction.updateRows(billsService.tableName, bills);
  195. if (pos.length > 0) await transaction.updateRows(posService.tableName, pos);
  196. await transaction.commit();
  197. } catch (err) {
  198. await transaction.rollback();
  199. throw err;
  200. }
  201. } else {
  202. await this.db.update(this.tableName,
  203. { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
  204. }
  205. }
  206. async _reCalcLedger(tenderId, billsService, newDecimal, oldDecimal) {
  207. if (!billsService) return [];
  208. const changeBills = [];
  209. const calcUp = newDecimal.up < oldDecimal.up, calcTp = newDecimal.tp !== oldDecimal.tp;
  210. if (calcUp || calcTp) {
  211. const bills = await billsService.getAllDataByCondition({
  212. columns: ['id', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'quantity'],
  213. where: { tender_id: tenderId, is_leaf: true },
  214. });
  215. for (const b of bills) {
  216. const cb = { id: b.id };
  217. cb.unit_price = calcUp ? this.ctx.helper.round(b.unit_price, newDecimal.up) : b.unit_price;
  218. cb.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, cb.unit_price, newDecimal.tp);
  219. cb.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, cb.unit_price, newDecimal.tp);
  220. cb.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, cb.unit_price, newDecimal.tp);
  221. cb.deal_tp = this.ctx.helper.mul(b.deal_qty, cb.unit_price, newDecimal.tp);
  222. cb.total_price = this.ctx.helper.mul(b.quantity, cb.unit_price, newDecimal.tp);
  223. changeBills.push(cb);
  224. }
  225. }
  226. return changeBills;
  227. }
  228. async _reCalcStageExtra(tenderId, newDecimal, oldDecimal) {
  229. let changeSj = [], changeSb = [], changeSo = [];
  230. const stage = await this.ctx.service.stage.getLastestStage(tenderId, true);
  231. if (!stage) return [changeSj, changeSb, changeSo];
  232. const upDecimal = newDecimal.up, tpDecimal = newDecimal.extra ? newDecimal.extraTp : newDecimal.tp;
  233. const calcUp = upDecimal < oldDecimal.up,
  234. calcTp = tpDecimal < (oldDecimal.extra ? oldDecimal.extraTp : oldDecimal.tp);
  235. if (calcUp || calcTp) {
  236. const stageJgcl = await this.ctx.service.stageJgcl.getAllDataByCondition({
  237. columns: ['id', 'unit_price', 'arrive_qty', 'deduct_qty'],
  238. where: { sid: stage.id }
  239. });
  240. for (const sj of stageJgcl) {
  241. const cj = { id: sj.id };
  242. cj.unit_price = calcUp ? this.ctx.helper.round(sj.unit_price, upDecimal) : sj.unit_price;
  243. cj.arrive_tp = this.ctx.helper.mul(sj.arrive_qty, sj.unit_price, tpDecimal);
  244. cj.deduct_tp = this.ctx.helper.mul(sj.deduct_qty, sj.unit_price, tpDecimal);
  245. changeSj.push(cj);
  246. }
  247. }
  248. if (calcTp) {
  249. changeSb = await this.ctx.service.stageBonus.getAllDataByCondition({
  250. columns: ['id', 'tp'],
  251. where: { sid: stage.id }
  252. });
  253. for (const cb of changeSb) {
  254. cb.tp = this.ctx.helper.round(cb.tp, tpDecimal);
  255. }
  256. changeSo = await this.ctx.service.stageOther.getAllDataByCondition({
  257. columns: ['id', 'total_price', 'tp'],
  258. where: { sid: stage.id }
  259. });
  260. for (const co of changeSo) {
  261. if (stage.order === 1) co.total_price = this.ctx.helper.round(co.total_price, tpDecimal);
  262. co.tp = this.ctx.helper.round(co.tp, tpDecimal);
  263. }
  264. }
  265. return [changeSj, changeSb, changeSo];
  266. }
  267. async saveDecimal(tenderId, newDecimal, oldDecimal) {
  268. const changeAdvanceBills = [];
  269. const caclPayTp = (newDecimal.pay ? newDecimal.payTp : newDecimal.tp) < (oldDecimal.pay ? oldDecimal.payTp : oldDecimal.tp);
  270. if (caclPayTp) {
  271. // 获取预付款需要修改的相关记录
  272. const ad_bills = await this.ctx.service.advance.getAllDataByCondition({
  273. columns: ['id', 'cur_amount', 'prev_amount', 'prev_total_amount'],
  274. where: { status: [advanceConst.status.uncheck, advanceConst.status.checkNo], tid: tenderId },
  275. });
  276. const decimal = newDecimal.pay ? newDecimal.payTp : newDecimal.tp;
  277. // 根据精度重新计算相关金额
  278. for (const ad of ad_bills) {
  279. const cb = { id: ad.id };
  280. const cur_amount = this.ctx.helper.round(ad.cur_amount, decimal);
  281. cb.cur_amount = cur_amount;
  282. cb.prev_total_amount = this.ctx.helper.add(cur_amount, ad.prev_amount);
  283. changeAdvanceBills.push(cb);
  284. }
  285. }
  286. const [billsService] = await this._getLedgerService();
  287. console.log(billsService);
  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. * 获取标段审批相关信息 (审批设置用)
  314. * @param tenderId
  315. * @return {Promise<void>}
  316. */
  317. async getTenderShenpiInfo(tenderId) {
  318. const info = await this.getDataByCondition({ tid: tenderId });
  319. // 还没选择模式的标段不应该可以选择审批流程设置
  320. if (!info) {
  321. return false;
  322. }
  323. const defaultShenpiInfo = JSON.parse(JSON.stringify(defaultInfo.shenpi));
  324. const shenpiInfo = !info.shenpi || info.shenpi === null || info.shenpi === '' ? defaultShenpiInfo : JSON.parse(info.shenpi);
  325. return shenpiInfo;
  326. }
  327. /**
  328. * 拷贝标段数据至当前标段
  329. * @param {number} id - 当前标段id
  330. * @param {number} copy_id - 被拷贝的标段id
  331. * @param {Array} typeArr - 需要拷贝的类型数组
  332. */
  333. async copyTenderHandler(id, copy_id, typeArr) {
  334. const columns = [];
  335. typeArr.forEach(item => {
  336. if (item === 'tender') {
  337. columns.push('deal_info', 'construction_unit', 'tech_param');
  338. } else if (item === 'chapter') {
  339. columns.push('chapter');
  340. } else if (item === 'pay_account') {
  341. columns.push('pay_account');
  342. }
  343. });
  344. if (!columns.length) throw '未选择需要拷贝的设置';
  345. const [data] = await this.getAllDataByCondition({
  346. where: {
  347. tid: copy_id,
  348. },
  349. columns,
  350. });
  351. const isUpdate = await this.update(data, { tid: id });
  352. if (isUpdate) {
  353. await this.ctx.service.tender.update({ copy_id }, { id });
  354. }
  355. }
  356. }
  357. return TenderInfo;
  358. };