tender_info.js 20 KB

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