tender_info.js 21 KB

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