tender_info.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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'],
  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,
  168. sjcl_qty = 0,
  169. qtcl_qty = 0,
  170. quantity = 0;
  171. for (const p of bPos) {
  172. this.ctx.helper.checkFieldPrecision(p, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty'], precision.value);
  173. p.quantity = this.ctx.helper.add(this.ctx.helper.add(p.sgfh_qty, p.sjcl_qty), p.qtcl_qty);
  174. sgfh_qty = this.ctx.helper.add(sgfh_qty, p.sgfh_qty);
  175. sjcl_qty = this.ctx.helper.add(sjcl_qty, p.sjcl_qty);
  176. qtcl_qty = this.ctx.helper.add(qtcl_qty, p.qtcl_qty);
  177. quantity = this.ctx.helper.add(quantity, p.quantity);
  178. }
  179. b.sgfh_qty = sgfh_qty;
  180. b.sjcl_qty = sjcl_qty;
  181. b.qtcl_qty = qtcl_qty;
  182. b.quantity = quantity;
  183. // this.ctx.helper.checkFieldPrecision(b, ['deal_qty'], precision.value);
  184. } else {
  185. this.ctx.helper.checkFieldPrecision(b, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'], precision.value);
  186. }
  187. b.quantity = this.ctx.helper.add(this.ctx.helper.add(b.sgfh_qty, b.sjcl_qty), b.qtcl_qty);
  188. b.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, b.unit_price, decimal.tp);
  189. b.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, b.unit_price, decimal.tp);
  190. b.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, b.unit_price, decimal.tp);
  191. b.deal_tp = this.ctx.helper.mul(b.deal_qty, b.unit_price, decimal.tp);
  192. b.total_price = this.ctx.helper.mul(b.quantity, 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'],
  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. changeBills.push(cb);
  228. }
  229. }
  230. return changeBills;
  231. }
  232. async _reCalcStageBills(tenderId, newDecimal, oldDecimal) {
  233. const updateStageBills = [], insertStageBills = [];
  234. const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
  235. if (stages.length === 0 || newDecimal.tp === oldDecimal.tp) return [updateStageBills, insertStageBills];
  236. for (const stage of stages) {
  237. const stageBills = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  238. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  239. columns: ['id', 'unit_price'],
  240. where: { tender_id: tenderId, is_leaf: true },
  241. });
  242. for (const sb of stageBills) {
  243. const b = bills.find(x => {return x.id === sb.lid});
  244. if (!b) continue;
  245. const contract_tp = this.ctx.helper.mul(b.unit_price, sb.contract_qty, newDecimal.tp);
  246. const qc_tp = this.ctx.helper.mul(b.unit_price, sb.qc_qty, newDecimal.tp);
  247. if (contract_tp == sb.contract_tp && qc_tp === sb.qc_tp) continue;
  248. if (sb.times === stage.times && sb.order === 0) {
  249. updateStageBills.push({
  250. id: sb.id, contract_tp, qc_tp
  251. });
  252. } else {
  253. insertStageBills.push({
  254. tid: stage.tid, lid: sb.lid, sid: stage.id, said: this.ctx.session.sessionUser.accountId,
  255. times: stage.times, order: 0,
  256. contract_qty: sb.contract_qty, contract_expr: sb.contract_expr, contract_tp,
  257. qc_qty: sb.qc_qty, qc_tp,
  258. postil: sb.postil,
  259. });
  260. }
  261. }
  262. }
  263. return [updateStageBills, insertStageBills];
  264. }
  265. async _reCalcStageExtra(tenderId, newDecimal, oldDecimal) {
  266. const changeSj = [], changeSb = [], changeSo = [];
  267. const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
  268. if (stages.length === 0) return [changeSj, changeSb, changeSo];
  269. const upDecimal = newDecimal.up, tpDecimal = newDecimal.extra ? newDecimal.extraTp : newDecimal.tp;
  270. const calcUp = upDecimal < oldDecimal.up,
  271. calcTp = tpDecimal < (oldDecimal.extra ? oldDecimal.extraTp : oldDecimal.tp);
  272. for (const stage of stages) {
  273. if (calcUp || calcTp) {
  274. const stageJgcl = await this.ctx.service.stageJgcl.getAllDataByCondition({
  275. columns: ['id', 'unit_price', 'arrive_qty', 'deduct_qty'],
  276. where: { sid: stage.id }
  277. });
  278. for (const sj of stageJgcl) {
  279. const cj = { id: sj.id };
  280. cj.unit_price = calcUp ? this.ctx.helper.round(sj.unit_price, upDecimal) : sj.unit_price;
  281. cj.arrive_tp = this.ctx.helper.mul(sj.arrive_qty, sj.unit_price, tpDecimal);
  282. cj.deduct_tp = this.ctx.helper.mul(sj.deduct_qty, sj.unit_price, tpDecimal);
  283. changeSj.push(cj);
  284. }
  285. }
  286. if (calcTp) {
  287. const stageChangeSb = await this.ctx.service.stageBonus.getAllDataByCondition({
  288. columns: ['id', 'tp'],
  289. where: { sid: stage.id }
  290. });
  291. for (const cb of stageChangeSb) {
  292. cb.tp = this.ctx.helper.round(cb.tp, tpDecimal);
  293. }
  294. changeSb.push(...stageChangeSb);
  295. const stageChangeSo = await this.ctx.service.stageOther.getAllDataByCondition({
  296. columns: ['id', 'total_price', 'tp'],
  297. where: { sid: stage.id }
  298. });
  299. for (const co of stageChangeSo) {
  300. if (stage.order === 1) co.total_price = this.ctx.helper.round(co.total_price, tpDecimal);
  301. co.tp = this.ctx.helper.round(co.tp, tpDecimal);
  302. }
  303. changeSo.push(...stageChangeSo);
  304. }
  305. }
  306. return [changeSj, changeSb, changeSo];
  307. }
  308. async saveDecimal(tenderId, newDecimal, oldDecimal) {
  309. const changeAdvanceBills = [];
  310. const caclPayTp = (newDecimal.pay ? newDecimal.payTp : newDecimal.tp) < (oldDecimal.pay ? oldDecimal.payTp : oldDecimal.tp);
  311. if (caclPayTp) {
  312. // 获取预付款需要修改的相关记录
  313. const ad_bills = await this.ctx.service.advance.getAllDataByCondition({
  314. columns: ['id', 'cur_amount', 'prev_amount', 'prev_total_amount'],
  315. where: { status: [advanceConst.status.uncheck, advanceConst.status.checkNo], tid: tenderId },
  316. });
  317. const decimal = newDecimal.pay ? newDecimal.payTp : newDecimal.tp;
  318. // 根据精度重新计算相关金额
  319. for (const ad of ad_bills) {
  320. const cb = { id: ad.id };
  321. const cur_amount = this.ctx.helper.round(ad.cur_amount, decimal);
  322. cb.cur_amount = cur_amount;
  323. cb.prev_total_amount = this.ctx.helper.add(cur_amount, ad.prev_amount);
  324. changeAdvanceBills.push(cb);
  325. }
  326. }
  327. const [billsService] = await this._getLedgerService();
  328. const changeBills = await this._reCalcLedger(tenderId, billsService, newDecimal, oldDecimal);
  329. const [updateStageBills, insertStageBills] = await this._reCalcStageBills(tenderId, newDecimal, oldDecimal);
  330. const [changeSj, changeSb, changeSo] = await this._reCalcStageExtra(tenderId, newDecimal, oldDecimal);
  331. if (changeBills.length > 0 ||
  332. changeAdvanceBills.length > 0 ||
  333. updateStageBills.length > 0 || insertStageBills.length > 0 ||
  334. changeSj.length > 0 || changeSb.length > 0 || changeSo.length > 0) {
  335. const transaction = await this.db.beginTransaction();
  336. try {
  337. await transaction.update(this.tableName,
  338. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  339. if (changeBills.length > 0) await transaction.updateRows(billsService.tableName, changeBills);
  340. if (updateStageBills.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, updateStageBills);
  341. if (insertStageBills.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, insertStageBills);
  342. if (changeSj.length > 0) await transaction.updateRows(this.ctx.service.stageJgcl.tableName, changeSj);
  343. if (changeSb.length > 0) await transaction.updateRows(this.ctx.service.stageBonus.tableName, changeSb);
  344. if (changeSo.length > 0) await transaction.updateRows(this.ctx.service.stageOther.tableName, changeSo);
  345. if (changeAdvanceBills.length) await transaction.updateRows(this.ctx.service.advance.tableName, changeAdvanceBills);
  346. await transaction.commit();
  347. } catch (error) {
  348. await transaction.rollback();
  349. throw error;
  350. }
  351. } else {
  352. await this.db.update(this.tableName,
  353. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  354. }
  355. }
  356. /**
  357. * 获取标段审批相关信息 (审批设置用)
  358. * @param tenderId
  359. * @return {Promise<void>}
  360. */
  361. async getTenderShenpiInfo(tenderId) {
  362. const defaultInfo = await this.getDefaultInfo(tenderId);
  363. const info = await this.getDataByCondition({ tid: tenderId });
  364. // 还没选择模式的标段不应该可以选择审批流程设置
  365. if (!info) {
  366. return false;
  367. }
  368. const defaultShenpiInfo = JSON.parse(JSON.stringify(defaultInfo.shenpi));
  369. const shenpiInfo = !info.shenpi || info.shenpi === null || info.shenpi === '' ? defaultShenpiInfo : JSON.parse(info.shenpi);
  370. // 查漏补缺
  371. for (const sp in defaultShenpiInfo) {
  372. if (!shenpiInfo[sp]) {
  373. shenpiInfo[sp] = defaultShenpiInfo[sp];
  374. }
  375. }
  376. return shenpiInfo;
  377. }
  378. /**
  379. * 拷贝标段数据至当前标段
  380. * @param {number} id - 当前标段id
  381. * @param {number} copy_id - 被拷贝的标段id
  382. * @param {Array} typeArr - 需要拷贝的类型数组
  383. */
  384. async copyTenderHandler(id, copy_id, typeArr) {
  385. const columns = [];
  386. typeArr.forEach(item => {
  387. if (item === 'tender') {
  388. columns.push('deal_info', 'construction_unit', 'tech_param', 'bid_info');
  389. } else if (item === 'chapter') {
  390. columns.push('chapter');
  391. } else if (item === 'pay_account') {
  392. columns.push('pay_account');
  393. }
  394. });
  395. if (!columns.length) throw '未选择需要拷贝的设置';
  396. const [data] = await this.getAllDataByCondition({
  397. where: {
  398. tid: copy_id,
  399. },
  400. columns,
  401. });
  402. const isUpdate = await this.update(data, { tid: id });
  403. if (isUpdate) {
  404. await this.ctx.service.tender.update({ copy_id }, { id });
  405. }
  406. }
  407. }
  408. return TenderInfo;
  409. };