tender_info.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 advanceConst = require('../const/audit').advance;
  13. const auditConst = require('../const/audit');
  14. const measureType = require('../const/tender').measureType;
  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. async getDefaultInfo (tenderId) {
  28. const tender = await this.ctx.service.tender.getTender(tenderId);
  29. return tender.measure_type === measureType.tz.value ? infoConst.tzDefaultInfo : infoConst.gclDefaultInfo;
  30. }
  31. /**
  32. * 新增 标段相关信息
  33. *
  34. * @param tenderId - 标段Id
  35. * @param projectId - 项目Id
  36. * @param transaction - 事务
  37. * @return {Promise<void>}
  38. */
  39. async addTenderInfo(tenderId, projectId, transaction) {
  40. const defaultInfo = await this.getDefaultInfo(tenderId);
  41. const info = JSON.parse(JSON.stringify(defaultInfo));
  42. info.tid = tenderId;
  43. info.pid = projectId;
  44. for (const pi of parseInfo) {
  45. info[pi] = JSON.stringify(info[pi]);
  46. }
  47. for (const pi of arrayInfo) {
  48. info[pi] = JSON.stringify(info[pi]);
  49. }
  50. if (transaction) {
  51. await transaction.insert(this.tableName, info);
  52. } else {
  53. await this.db.insert(this.tableName, info);
  54. }
  55. return info;
  56. }
  57. /**
  58. * 获取标段相关信息
  59. * @param tenderId
  60. * @return {Promise<void>}
  61. */
  62. async getTenderInfo(tenderId, projectId) {
  63. const defaultInfo = await this.getDefaultInfo(tenderId);
  64. let info = await this.getDataByCondition({ tid: tenderId });
  65. // 兼容不存在info的情况
  66. if (!info) info = await this.addTenderInfo(tenderId, projectId || this.ctx.session.sessionProject.id);
  67. for (const pi of parseInfo) {
  68. info[pi] = !info[pi] || info[pi] === '' ? (pi === 'shenpi' ? JSON.parse(JSON.stringify(defaultInfo[pi])) : defaultInfo[pi]) : JSON.parse(info[pi]);
  69. this.ctx.helper._.defaultsDeep(info[pi], defaultInfo[pi]);
  70. }
  71. for (const ai of arrayInfo) {
  72. info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
  73. }
  74. if (info.decimal) {
  75. info.decimal._pay_tp = info.decimal.pay ? info.decimal.payTp : info.decimal.tp;
  76. }
  77. info.checkOverInfo = infoConst.transOverRangeCheck(this.ctx.helper, info.over_range_check);
  78. return info;
  79. }
  80. /**
  81. * 获取标段相关信息(报表用)
  82. * @param tenderId
  83. * @return {Promise<void>}
  84. */
  85. async getTenderInfoEx(tenderId) {
  86. const defaultInfo = await this.getDefaultInfo(tenderId);
  87. const sql = 'select t2.name, t1.* from zh_tender_info t1 inner join zh_tender t2 on t2.id = t1.tid where t1.tid = ?';
  88. const sqlParam = [tenderId];
  89. const list = await this.db.query(sql, sqlParam);
  90. const info = list[0];
  91. const len = info.deal_info.length;
  92. info.deal_info = info.deal_info.slice(0, len - 1) + ',"name":"' + info.name + '"' + info.deal_info.slice(len - 1);
  93. for (const pi of parseInfo) {
  94. info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
  95. this.ctx.helper._.defaults(info[pi], defaultInfo[pi]);
  96. }
  97. for (const ai of arrayInfo) {
  98. info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
  99. }
  100. if (info.decimal) {
  101. info.decimal._pay_tp = info.decimal.pay ? info.decimal.payTp : info.decimal.tp;
  102. }
  103. return info;
  104. }
  105. /**
  106. * 保存标段相关信息
  107. *
  108. * @param data
  109. * @return {Promise<void>}
  110. */
  111. async saveTenderInfo(tenderId, data) {
  112. for (const di in data) {
  113. if (parseInfo.indexOf(di) >= 0 || arrayInfo.indexOf(di) >= 0) {
  114. data[di] = JSON.stringify(data[di]);
  115. }
  116. }
  117. await this.db.update(this.tableName, data, { where: { tid: tenderId } });
  118. }
  119. async _getLedgerService() {
  120. try {
  121. if (this.ctx.tender.data.ledger_status === auditConst.ledger.status.checked) {
  122. const revise = await this.ctx.service.ledgerRevise.getLastestRevise(this.ctx.tender.id);
  123. if (revise.status === auditConst.revise.status.uncheck || revise.status === auditConst.revise.status.checkNo) {
  124. return [this.ctx.service.reviseBills, this.ctx.service.revisePos];
  125. }
  126. } else {
  127. return [this.ctx.service.ledger, this.ctx.service.pos];
  128. }
  129. } catch(err) {
  130. }
  131. return [];
  132. }
  133. async savePrecision(tenderId, newPrecision, oldPrecision, decimal) {
  134. const changePrecision = [];
  135. const units = await this.ctx.service.ledger.getTenderUsedUnits(tenderId);
  136. const defUnits = this._.map(newPrecision, 'units');
  137. const otherUnits = units.filter(function(u) { return defUnits.indexOf(u) === -1; });
  138. let changeUnits = [];
  139. for (const prop in newPrecision) {
  140. if (oldPrecision[prop]) {
  141. if (newPrecision[prop].value < oldPrecision[prop].value) {
  142. changePrecision.push(newPrecision[prop]);
  143. }
  144. } else {
  145. changePrecision.push(newPrecision[prop]);
  146. }
  147. }
  148. for (const cp of changePrecision) {
  149. if (cp.unit) {
  150. changeUnits.push(cp.unit);
  151. } else {
  152. changeUnits.push(otherUnits);
  153. }
  154. }
  155. changeUnits = this._.flatten(changeUnits);
  156. const [billsService, posService] = await this._getLedgerService();
  157. if (changeUnits.length > 0 && billsService && posService) {
  158. const bills = await billsService.getAllDataByCondition({
  159. columns: ['id', 'unit', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'ex_qty1'],
  160. where: { tender_id: tenderId, unit: changeUnits, is_leaf: true },
  161. });
  162. const pos = changeUnits.length > 0 ? await posService.getPosDataByUnits(tenderId, changeUnits) : [];
  163. for (const b of bills) {
  164. const precision = this.ctx.helper.findPrecision(newPrecision, b.unit);
  165. const bPos = this._.filter(pos, { lid: b.id });
  166. if (bPos.length > 0) {
  167. let sgfh_qty = 0, sjcl_qty = 0, qtcl_qty = 0, quantity = 0, ex_qty1 = 0;
  168. for (const p of bPos) {
  169. this.ctx.helper.checkFieldPrecision(p, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'ex_qty1'], 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. ex_qty1 = this.ctx.helper.add(quantity, p.ex_qty1);
  176. }
  177. b.sgfh_qty = sgfh_qty;
  178. b.sjcl_qty = sjcl_qty;
  179. b.qtcl_qty = qtcl_qty;
  180. b.quantity = quantity;
  181. b.ex_qty1 = ex_qty1;
  182. // this.ctx.helper.checkFieldPrecision(b, ['deal_qty'], precision.value);
  183. } else {
  184. this.ctx.helper.checkFieldPrecision(b, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'ex_qty1'], precision.value);
  185. }
  186. b.quantity = this.ctx.helper.add(this.ctx.helper.add(b.sgfh_qty, b.sjcl_qty), b.qtcl_qty);
  187. b.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, b.unit_price, decimal.tp);
  188. b.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, b.unit_price, decimal.tp);
  189. b.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, b.unit_price, decimal.tp);
  190. b.deal_tp = this.ctx.helper.mul(b.deal_qty, b.unit_price, decimal.tp);
  191. b.total_price = this.ctx.helper.mul(b.quantity, b.unit_price, decimal.tp);
  192. b.ex_tp1 = this.ctx.helper.mul(b.ex_qty1, b.unit_price, decimal.tp);
  193. }
  194. const transaction = await this.db.beginTransaction();
  195. try {
  196. await transaction.update(this.tableName,
  197. { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
  198. if (bills.length > 0) await transaction.updateRows(billsService.tableName, bills);
  199. if (pos.length > 0) await transaction.updateRows(posService.tableName, pos);
  200. await transaction.commit();
  201. } catch (err) {
  202. await transaction.rollback();
  203. throw err;
  204. }
  205. } else {
  206. await this.db.update(this.tableName,
  207. { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
  208. }
  209. }
  210. async _reCalcLedger(tenderId, billsService, newDecimal, oldDecimal) {
  211. if (!billsService) return [];
  212. const changeBills = [];
  213. const calcUp = newDecimal.up < oldDecimal.up, calcTp = newDecimal.tp !== oldDecimal.tp;
  214. if (calcUp || calcTp) {
  215. const bills = await billsService.getAllDataByCondition({
  216. columns: ['id', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'quantity', 'ex_qty1'],
  217. where: { tender_id: tenderId, is_leaf: true },
  218. });
  219. for (const b of bills) {
  220. const cb = { id: b.id };
  221. cb.unit_price = calcUp ? this.ctx.helper.round(b.unit_price, newDecimal.up) : b.unit_price;
  222. cb.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, cb.unit_price, newDecimal.tp);
  223. cb.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, cb.unit_price, newDecimal.tp);
  224. cb.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, cb.unit_price, newDecimal.tp);
  225. cb.deal_tp = this.ctx.helper.mul(b.deal_qty, cb.unit_price, newDecimal.tp);
  226. cb.total_price = this.ctx.helper.mul(b.quantity, cb.unit_price, newDecimal.tp);
  227. cb.ex_tp1 = this.ctx.helper.mul(b.ex_qty1, cb.unit_price, newDecimal.tp);
  228. changeBills.push(cb);
  229. }
  230. }
  231. return changeBills;
  232. }
  233. async _reCalcStageBills(tenderId, newDecimal, oldDecimal) {
  234. const updateStageBills = [], insertStageBills = [];
  235. const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
  236. if (stages.length === 0 || newDecimal.tp === oldDecimal.tp) return [updateStageBills, insertStageBills];
  237. for (const stage of stages) {
  238. const stageBills = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  239. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  240. columns: ['id', 'unit_price'],
  241. where: { tender_id: tenderId, is_leaf: true },
  242. });
  243. for (const sb of stageBills) {
  244. const b = bills.find(x => {return x.id === sb.lid});
  245. if (!b) continue;
  246. const contract_tp = this.ctx.helper.mul(b.unit_price, sb.contract_qty, newDecimal.tp);
  247. const qc_tp = this.ctx.helper.mul(b.unit_price, sb.qc_qty, newDecimal.tp);
  248. const ex_stage_tp1 = this.ctx.helper.mul(b.unit_price, sb.ex_stage_qty1, newDecimal.tp);
  249. if (contract_tp == sb.contract_tp && qc_tp === sb.qc_tp && ex_stage_tp1 === sb.ex_stage_tp1) continue;
  250. if (sb.times === stage.times && sb.order === 0) {
  251. updateStageBills.push({
  252. id: sb.id, contract_tp, qc_tp, ex_stage_tp1,
  253. });
  254. } else {
  255. insertStageBills.push({
  256. tid: stage.tid, lid: sb.lid, sid: stage.id, said: this.ctx.session.sessionUser.accountId,
  257. times: stage.times, order: 0,
  258. contract_qty: sb.contract_qty, contract_expr: sb.contract_expr, contract_tp,
  259. qc_qty: sb.qc_qty, qc_tp,
  260. ex_stage_qty1: sb.ex_stage_qty1, ex_stage_tp1,
  261. postil: sb.postil,
  262. });
  263. }
  264. }
  265. }
  266. return [updateStageBills, insertStageBills];
  267. }
  268. async _reCalcStageExtra(tenderId, newDecimal, oldDecimal) {
  269. const changeSj = [], changeSb = [], changeSo = [];
  270. const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
  271. if (stages.length === 0) return [changeSj, changeSb, changeSo];
  272. const upDecimal = newDecimal.up, tpDecimal = newDecimal.extra ? newDecimal.extraTp : newDecimal.tp;
  273. const calcUp = upDecimal < oldDecimal.up,
  274. calcTp = tpDecimal < (oldDecimal.extra ? oldDecimal.extraTp : oldDecimal.tp);
  275. for (const stage of stages) {
  276. if (calcUp || calcTp) {
  277. const stageJgcl = await this.ctx.service.stageJgcl.getAllDataByCondition({
  278. columns: ['id', 'unit_price', 'arrive_qty', 'deduct_qty'],
  279. where: { sid: stage.id }
  280. });
  281. for (const sj of stageJgcl) {
  282. const cj = { id: sj.id };
  283. cj.unit_price = calcUp ? this.ctx.helper.round(sj.unit_price, upDecimal) : sj.unit_price;
  284. cj.arrive_tp = this.ctx.helper.mul(sj.arrive_qty, sj.unit_price, tpDecimal);
  285. cj.deduct_tp = this.ctx.helper.mul(sj.deduct_qty, sj.unit_price, tpDecimal);
  286. changeSj.push(cj);
  287. }
  288. }
  289. if (calcTp) {
  290. const stageChangeSb = await this.ctx.service.stageBonus.getAllDataByCondition({
  291. columns: ['id', 'tp'],
  292. where: { sid: stage.id }
  293. });
  294. for (const cb of stageChangeSb) {
  295. cb.tp = this.ctx.helper.round(cb.tp, tpDecimal);
  296. }
  297. changeSb.push(...stageChangeSb);
  298. const stageChangeSo = await this.ctx.service.stageOther.getAllDataByCondition({
  299. columns: ['id', 'total_price', 'tp'],
  300. where: { sid: stage.id }
  301. });
  302. for (const co of stageChangeSo) {
  303. if (stage.order === 1) co.total_price = this.ctx.helper.round(co.total_price, tpDecimal);
  304. co.tp = this.ctx.helper.round(co.tp, tpDecimal);
  305. }
  306. changeSo.push(...stageChangeSo);
  307. }
  308. }
  309. return [changeSj, changeSb, changeSo];
  310. }
  311. async saveDecimal(tenderId, newDecimal, oldDecimal) {
  312. const changeAdvanceBills = [];
  313. const caclPayTp = (newDecimal.pay ? newDecimal.payTp : newDecimal.tp) < (oldDecimal.pay ? oldDecimal.payTp : oldDecimal.tp);
  314. if (caclPayTp) {
  315. // 获取预付款需要修改的相关记录
  316. const ad_bills = await this.ctx.service.advance.getAllDataByCondition({
  317. columns: ['id', 'cur_amount', 'prev_amount', 'prev_total_amount'],
  318. where: { status: [advanceConst.status.uncheck, advanceConst.status.checkNo], tid: tenderId },
  319. });
  320. const decimal = newDecimal.pay ? newDecimal.payTp : newDecimal.tp;
  321. // 根据精度重新计算相关金额
  322. for (const ad of ad_bills) {
  323. const cb = { id: ad.id };
  324. const cur_amount = this.ctx.helper.round(ad.cur_amount, decimal);
  325. cb.cur_amount = cur_amount;
  326. cb.prev_total_amount = this.ctx.helper.add(cur_amount, ad.prev_amount);
  327. changeAdvanceBills.push(cb);
  328. }
  329. }
  330. const [billsService] = await this._getLedgerService();
  331. const changeBills = await this._reCalcLedger(tenderId, billsService, newDecimal, oldDecimal);
  332. const [updateStageBills, insertStageBills] = await this._reCalcStageBills(tenderId, newDecimal, oldDecimal);
  333. const [changeSj, changeSb, changeSo] = await this._reCalcStageExtra(tenderId, newDecimal, oldDecimal);
  334. if (changeBills.length > 0 ||
  335. changeAdvanceBills.length > 0 ||
  336. updateStageBills.length > 0 || insertStageBills.length > 0 ||
  337. changeSj.length > 0 || changeSb.length > 0 || changeSo.length > 0) {
  338. const transaction = await this.db.beginTransaction();
  339. try {
  340. await transaction.update(this.tableName,
  341. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  342. if (changeBills.length > 0) await transaction.updateRows(billsService.tableName, changeBills);
  343. if (updateStageBills.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, updateStageBills);
  344. if (insertStageBills.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, insertStageBills);
  345. if (changeSj.length > 0) await transaction.updateRows(this.ctx.service.stageJgcl.tableName, changeSj);
  346. if (changeSb.length > 0) await transaction.updateRows(this.ctx.service.stageBonus.tableName, changeSb);
  347. if (changeSo.length > 0) await transaction.updateRows(this.ctx.service.stageOther.tableName, changeSo);
  348. if (changeAdvanceBills.length) await transaction.updateRows(this.ctx.service.advance.tableName, changeAdvanceBills);
  349. await transaction.commit();
  350. } catch (error) {
  351. await transaction.rollback();
  352. throw error;
  353. }
  354. } else {
  355. await this.db.update(this.tableName,
  356. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  357. }
  358. }
  359. /**
  360. * 获取标段审批相关信息 (审批设置用)
  361. * @param tenderId
  362. * @return {Promise<void>}
  363. */
  364. async getTenderShenpiInfo(tenderId) {
  365. const defaultInfo = await this.getDefaultInfo(tenderId);
  366. const info = await this.getDataByCondition({ tid: tenderId });
  367. // 还没选择模式的标段不应该可以选择审批流程设置
  368. if (!info) {
  369. return false;
  370. }
  371. const defaultShenpiInfo = JSON.parse(JSON.stringify(defaultInfo.shenpi));
  372. const shenpiInfo = !info.shenpi || info.shenpi === null || info.shenpi === '' ? defaultShenpiInfo : JSON.parse(info.shenpi);
  373. // 查漏补缺
  374. for (const sp in defaultShenpiInfo) {
  375. if (!shenpiInfo[sp]) {
  376. shenpiInfo[sp] = defaultShenpiInfo[sp];
  377. }
  378. }
  379. return shenpiInfo;
  380. }
  381. /**
  382. * 拷贝标段数据至当前标段
  383. * @param {number} id - 当前标段id
  384. * @param {number} copy_id - 被拷贝的标段id
  385. * @param {Array} typeArr - 需要拷贝的类型数组
  386. */
  387. async copyTenderHandler(id, copy_id, typeArr) {
  388. const columns = [];
  389. typeArr.forEach(item => {
  390. if (item === 'tender') {
  391. columns.push('deal_info', 'construction_unit', 'tech_param', 'bid_info');
  392. } else if (item === 'chapter') {
  393. columns.push('chapter');
  394. } else if (item === 'pay_account') {
  395. columns.push('pay_account');
  396. }
  397. });
  398. if (!columns.length) throw '未选择需要拷贝的设置';
  399. const [data] = await this.getAllDataByCondition({
  400. where: {
  401. tid: copy_id,
  402. },
  403. columns,
  404. });
  405. const isUpdate = await this.update(data, { tid: id });
  406. if (isUpdate) {
  407. await this.ctx.service.tender.update({ copy_id }, { id });
  408. }
  409. }
  410. }
  411. return TenderInfo;
  412. };