tender_info.js 20 KB

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