phase_pay.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const audit = require('../const/audit').common;
  10. const calcBase = [
  11. {name: '签约合同价', code: 'htj', sort: 10},
  12. {name: '暂列金额', code: 'zlje', sort: 2},
  13. {name: '签约合同价(不含暂列金)', code: 'htjszl', sort: 1},
  14. {name: '签约开工预付款', code: 'kgyfk', sort: 2},
  15. {name: '签约材料预付款', code: 'clyfk', sort: 2},
  16. {name: '本期完成计量', code: 'bqwc', limit: true, sort: 10, checkStart: 'gather_tp'},
  17. {name: '本期合同计量', code: 'bqht', limit: true, sort: 10, checkStart: 'contract_tp'},
  18. {name: '本期变更计量', code: 'bqbg', limit: true, sort: 10},
  19. {name: '本期清单完成计量', code: 'bqqdwc', limit: true, sort: 10},
  20. {name: '本期清单合同计量', code: 'bqqdht', limit: true, sort: 10},
  21. {name: '本期清单变更计量', code: 'bqqdbg', limit: true, sort: 10},
  22. {name: '本期一般变更计量', code: 'ybbqbg', limit: true, sort: 5},
  23. {name: '本期较大变更计量', code: 'jdbqbg', limit: true, sort: 5},
  24. {name: '本期重大变更计量', code: 'zdbqbg', limit: true, sort: 5},
  25. {name: '100章本期完成计量', code: 'ybbqwc', limit: true, sort: 1},
  26. {name: '本期应付', code: 'bqyf', limit: true, ptNormalLimit: true, sort: 20},
  27. {name: '奖金', code: 'bonus', limit: true, sort: 1},
  28. {name: '罚金', code: 'fine', limit: true, sort: 1},
  29. {name: '甲供材料', code: 'jgcl', limit: true, sort: 1},
  30. ];
  31. module.exports = app => {
  32. class PhasePay extends app.BaseService {
  33. /**
  34. * 构造函数
  35. *
  36. * @param {Object} ctx - egg全局变量
  37. * @return {void}
  38. */
  39. constructor(ctx) {
  40. super(ctx);
  41. this.tableName = 'phase_pay';
  42. }
  43. analysisPhasePay(data) {
  44. if (!data) return;
  45. const datas = data instanceof Array ? data : [data];
  46. datas.forEach(x => {
  47. x.rela_stage = x.rela_stage ? JSON.parse(x.rela_stage) : [];
  48. x.calc_base = x.calc_base ? JSON.parse(x.calc_base) : [];
  49. });
  50. }
  51. calculatePhasePay(data) {
  52. const helper = this.ctx.helper;
  53. const datas = data instanceof Array ? data : [data];
  54. const formatNum = this.ctx.tender.info.display.thousandth ? this.ctx.helper.formatNum : function(num) { return num ? num + '' : ''; };
  55. datas.forEach(x => {
  56. x.end_calc_tp = helper.add(x.calc_tp, x.pre_calc_tp);
  57. x.end_pay_tp = helper.add(x.pay_tp, x.pre_pay_tp);
  58. x.end_cut_tp = helper.add(x.cut_tp, x.pre_cut_tp);
  59. x.end_sf_tp = helper.add(x.sf_tp, x.pre_sf_tp);
  60. x.end_yf_tp = helper.add(x.yf_tp, x.pre_yf_tp);
  61. for (const prop in x) {
  62. if (prop.indexOf('_tp') > 0) {
  63. x['display_' + prop] = formatNum(x[prop]);
  64. }
  65. }
  66. });
  67. }
  68. /**
  69. * 获取全部修订
  70. * @param tid
  71. * @returns {Promise<*>}
  72. */
  73. async getAllPhasePay (tid, sort = 'ASC') {
  74. const result = await this.getAllDataByCondition({
  75. where: {tid: tid},
  76. orders: [['phase_order', sort]],
  77. });
  78. this.analysisPhasePay(result);
  79. return result;
  80. }
  81. async getPhasePay(id) {
  82. const result = await this.getDataById(id);
  83. this.analysisPhasePay(result);
  84. return result;
  85. }
  86. async getPhasePayByOrder(tid, phaseOrder) {
  87. const result = await this.getDataByCondition({ tid, phase_order: phaseOrder });
  88. this.analysisPhasePay(result);
  89. return result;
  90. }
  91. async getMaxOrder(tid) {
  92. const sql = 'SELECT Max(`phase_order`) As max_order FROM ' + this.tableName + ' Where `tid` = ?';
  93. const sqlParam = [tid];
  94. const result = await this.db.queryOne(sql, sqlParam);
  95. return result.max_order || 0;
  96. }
  97. async _checkRelaStageConflict(relaStage, phasePay) {
  98. const pays = await this.getAllPhasePay(phasePay.tid);
  99. for (const p of pays) {
  100. if (p.id === phasePay.id) continue;
  101. for (const s of relaStage) {
  102. if (p.rela_stage.find(x => { return x.id === s.id; })) return true;
  103. }
  104. }
  105. return false;
  106. }
  107. async getCalcBase(relaStage, prePhase) {
  108. const result = {};
  109. for (const stage of relaStage) {
  110. result.contract_tp = this.ctx.helper.add(result.contract_tp, stage.contract_tp);
  111. result.qc_tp = this.ctx.helper.add(result.contract_tp, stage.qc_tp);
  112. result.pc_tp = this.ctx.helper.add(result.pc_tp, stage.pc_tp);
  113. const qdSum = await this.ctx.service.stageBills.getSumTotalPriceGcl(stage);
  114. result.qd_contract_tp = qdSum.contract_tp || 0;
  115. result.qd_qc_tp = qdSum.qc_tp || 0;
  116. result.qd_pc_tp = qdSum.pc_tp || 0;
  117. const sumGcl = await this.ctx.service.stageBills.getSumTotalPriceGcl(stage, '^[^0-9]*1[0-9]{2}(-|$)');
  118. const sumPc = await this.ctx.service.stageBillsPc.getSumTotalPriceGcl(stage, '^[^0-9]*1[0-9]{2}(-|$)');
  119. result.gather_100_tp = this.ctx.helper.sum([sumGcl.contract_tp, sumGcl.qc_tp, sumPc.pc_tp]);
  120. const bg = await this.ctx.service.stage.getChangeSubtotal(stage);
  121. result.common_bg_tp = bg.common || 0;
  122. result.more_bg_tp = bg.more || 0;
  123. result.great_bg_tp = bg.great || 0;
  124. }
  125. result.gather_tp = this.ctx.helper.sum([result.contract_tp, result.qc_tp, result.pc_tp]) || 0;
  126. result.qd_gather_tp = this.ctx.helper.sum([result.qd_contract_tp, result.qd_qc_tp, result.qd_pc_tp]) || 0;
  127. const bonusSum = await this.ctx.service.stageBonus.getSumTp(relaStage);
  128. result.bonus_positive_tp = bonusSum.positive_tp || 0;
  129. result.bonus_negative_tp = bonusSum.negative_tp || 0;
  130. result.bonus_tp = bonusSum.sum_tp || 0;
  131. const jgclSum = await this.ctx.service.stageJgcl.getSumTp(relaStage);
  132. result.jgcl_tp = jgclSum.sum_tp || 0;
  133. const otherSum = await this.ctx.service.stageOther.getSumTp(relaStage);
  134. result.other_tp = otherSum.sum_tp || 0;
  135. const safeProdSum = await this.ctx.service.stageSafeProd.getSumTp(relaStage);
  136. result.safe_prod_tp = safeProdSum.sum_tp || 0;
  137. const tempLandSum = await this.ctx.service.stageTempLand.getSumTp(relaStage);
  138. result.temp_land_tp = tempLandSum.sum_tp || 0;
  139. if (prePhase && prePhase.calc_base) {
  140. result.pre_contract_tp = this.ctx.helper.add(prePhase.calc_base.contract_tp, prePhase.calc_base.pre_contract_tp);
  141. result.pre_qc_tp = this.ctx.helper.add(prePhase.calc_base.qc_tp, prePhase.calc_base.pre_qc_tp);
  142. result.pre_pc_tp = this.ctx.helper.add(prePhase.calc_base.pc_tp, prePhase.calc_base.pre_pc_tp);
  143. result.pre_gather_tp = this.ctx.helper.add(prePhase.calc_base.gather_tp, prePhase.calc_base.pre_gather_tp);
  144. result.pre_qd_contract_tp = this.ctx.helper.add(prePhase.calc_base.qd_contract_tp, prePhase.calc_base.pre_qd_contract_tp);
  145. result.pre_qd_qc_tp = this.ctx.helper.add(prePhase.calc_base.qd_qc_tp, prePhase.calc_base.pre_qd_qc_tp);
  146. result.pre_qd_pc_tp = this.ctx.helper.add(prePhase.calc_base.qd_pc_tp, prePhase.calc_base.pre_qd_pc_tp);
  147. result.pre_qd_gather_tp = this.ctx.helper.add(prePhase.calc_base.qd_gather_tp, prePhase.calc_base.pre_qd_gather_tp);
  148. result.pre_bonus_positive_tp = this.ctx.helper.add(prePhase.calc_base.bonus_positive_tp, prePhase.calc_base.pre_bonus_positive_tp);
  149. result.pre_bonus_negative_tp = this.ctx.helper.add(prePhase.calc_base.bonus_negative_tp, prePhase.calc_base.pre_bonus_negative_tp);
  150. result.pre_bonus_tp = this.ctx.helper.add(prePhase.calc_base.bonus_tp, prePhase.calc_base.pre_bonus_tp);
  151. result.pre_jgcl_tp = this.ctx.helper.add(prePhase.calc_base.jgcl_tp, prePhase.calc_base.pre_jgcl_tp);
  152. result.pre_other_tp = this.ctx.helper.add(prePhase.calc_base.other_tp, prePhase.calc_base.pre_other_tp);
  153. result.pre_safe_prod_tp = this.ctx.helper.add(prePhase.calc_base.safe_prod_tp, prePhase.calc_base.pre_safe_prod_tp);
  154. result.pre_temp_land_tp = this.ctx.helper.add(prePhase.calc_base.temp_land_tp, prePhase.calc_base.pre_temp_land_tp);
  155. }
  156. return result;
  157. }
  158. /**
  159. * 获取 当期的 计算基数
  160. * @return {Promise<any>}
  161. */
  162. getPhasePayCalcBase(phasePay, tenderInfo) {
  163. const payCalcBase = JSON.parse(JSON.stringify(calcBase));
  164. for (const cb of payCalcBase) {
  165. switch (cb.code) {
  166. case 'htj':
  167. cb.value = tenderInfo.deal_param.contractPrice;
  168. break;
  169. case 'zlje':
  170. cb.value = tenderInfo.deal_param.zanLiePrice;
  171. break;
  172. case 'htjszl':
  173. cb.value = this.ctx.helper.sub(tenderInfo.deal_param.contractPrice, tenderInfo.deal_param.zanLiePrice);
  174. break;
  175. case 'kgyfk':
  176. cb.value = tenderInfo.deal_param.startAdvance;
  177. break;
  178. case 'clyfk':
  179. cb.value = tenderInfo.deal_param.materialAdvance;
  180. break;
  181. case 'bqwc':
  182. cb.value = phasePay.calc_base.gather_tp;
  183. break;
  184. case 'bqht':
  185. cb.value = phasePay.calc_base.contract_tp;
  186. break;
  187. case 'bqbg':
  188. cb.value = phasePay.calc_base.qc_tp;
  189. break;
  190. case 'bqqdwc':
  191. cb.value = phasePay.calc_base.qd_gather_tp;
  192. break;
  193. case 'bqqdht':
  194. cb.value = phasePay.calc_base.qd_contract_tp;
  195. break;
  196. case 'bqqdbg':
  197. cb.value = phasePay.calc_base.qd_qc_tp;
  198. break;
  199. case 'ybbqwc':
  200. cb.value = phasePay.calc_base.gather_100_tp;
  201. break;
  202. case 'ybbqbg':
  203. cb.value = phasePay.calc_base.common_bg_tp;
  204. break;
  205. case 'jdbqbg':
  206. cb.value = phasePay.calc_base.more_bg_tp;
  207. break;
  208. case 'zdbqbg':
  209. cb.value = phasePay.calc_base.great_bg_tp;
  210. break;
  211. case 'bonus':
  212. cb.value = phasePay.calc_base.bonus_positive_tp;
  213. break;
  214. case 'fine':
  215. cb.value = phasePay.calc_base.bonus_negative_tp;
  216. break;
  217. case 'jgcl':
  218. cb.value = phasePay.calc_base.jgcl_tp;
  219. break;
  220. case 'aqsc':
  221. cb.value = phasePay.calc_base.safe_prod_tp;
  222. break;
  223. case 'lsyd':
  224. cb.value = phasePay.calc_base.temp_land_tp;
  225. break;
  226. default:
  227. cb.value = 0;
  228. }
  229. }
  230. return payCalcBase;
  231. }
  232. async add(tid, relaStage, phaseDate, memo) {
  233. if (!tid) throw '数据错误';
  234. const user_id = this.ctx.session.sessionUser.accountId;
  235. const maxOrder = await this.getMaxOrder();
  236. const data = {
  237. id: this.uuid.v4(), tid: tid, create_user_id: user_id, update_user_id: user_id,
  238. phase_order: maxOrder + 1, phase_date: phaseDate, memo,
  239. audit_times: 1, audit_status: audit.status.uncheck,
  240. rela_stage: JSON.stringify(relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; })),
  241. };
  242. if (await this._checkRelaStageConflict(relaStage, data)) throw '选择的计量期,已被调用,请刷新页面后选择计量期新增合同支付';
  243. const prePhase = maxOrder > 0 ? await this.getPhasePayByOrder(tid, maxOrder) : null;
  244. const calcBase = await this.getCalcBase(relaStage, prePhase);
  245. data.calc_base = JSON.stringify(calcBase);
  246. const transaction = await this.db.beginTransaction();
  247. try {
  248. const result = await transaction.insert(this.tableName, data);
  249. if (result.affectedRows !== 1) throw '新增合同支付失败';
  250. await this.ctx.service.phasePayDetail.initPhaseData(transaction, data);
  251. await transaction.commit();
  252. return data;
  253. } catch(err) {
  254. await transaction.rollback();
  255. throw err;
  256. }
  257. }
  258. async refreshCalcBase(phasePay) {
  259. const prePhase = phasePay.phase_order > 1 ? await this.getPhasePayByOrder(phasePay.tid, phasePay.phase_order - 1) : null;
  260. const relaStage = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: phasePay.tid, order: phasePay.rela_stage.map(x => { return x.stage_order; }) } });
  261. const calcBase = await this.getCalcBase(relaStage, prePhase);
  262. const conn = await this.db.beginTransaction();
  263. try {
  264. await conn.update(this.tableName, {
  265. id: phasePay.id, update_user_id: this.ctx.session.sessionUser.accountId,
  266. calc_base: JSON.stringify(calcBase),
  267. calc_base_time: new Date()
  268. });
  269. phasePay.calc_base = calcBase;
  270. await this.ctx.service.phasePayDetail.calculateSave(phasePay, conn);
  271. await conn.commit();
  272. } catch(err) {
  273. await conn.rollback();
  274. throw err;
  275. }
  276. }
  277. async resetRelaStageId(phasePay, relaStage) {
  278. if (await this._checkRelaStageConflict(relaStage, phasePay)) throw '选择的计量期,已被调用,请刷新页面后选择计量期新增合同支付';
  279. const prePhase = prePhase.phase_order > 1 ? await this.getPhasePayByOrder(phasePay.tid, phasePay.phase_order - 1) : null;
  280. const calcBase = await this.getCalcBase(relaStage, prePhase);
  281. const rela_stage = relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; });
  282. const conn = await this.db.beginTransaction();
  283. try {
  284. await conn.update(this.tableName, {
  285. id: phasePay.id, update_user_id: this.ctx.session.sessionUser.accountId,
  286. calc_base: JSON.stringify(calcBase),
  287. rela_stage: JSON.stringify(rela_stage),
  288. calc_base_time: new Date()
  289. });
  290. phasePay.calc_base = calcBase;
  291. phasePay.rela_stage = relaStage;
  292. await this.ctx.service.phasePayDetail.calculateSave(phasePay, conn);
  293. await conn.commit();
  294. } catch(err) {
  295. await conn.rollback();
  296. throw err;
  297. }
  298. }
  299. async loadUser(phasePay) {
  300. phasePay.user = await this.ctx.service.projectAccount.getAccountInfoById(phasePay.create_user_id);
  301. phasePay.auditors = [];
  302. phasePay.curAuditors = phasePay.auditors.filter(x => { return x.audit_status === audit.status.checking; });
  303. phasePay.curAuditorIds = phasePay.curAuditors.map(x => { return x.audit_id; });
  304. phasePay.flowAuditors = phasePay.curAuditors.length === 0 ? [] : phasePay.auditors.filter(x => { return x.audit_sort === phasePay.curAuditors[0].audit_sort; });
  305. phasePay.flowAuditorIds = phasePay.curAuditors.map(x => { return x.audit_id; });
  306. phasePay.nextAuditors = phasePay.curAuditors.length > 0 ? phasePay.auditors.filter(x => { return x.audit_sort === phasePay.curAuditors[0].audit_sort + 1; }) : [];
  307. phasePay.nextAuditorIds = this._.map(phasePay.nextAuditors, 'audit_id');
  308. phasePay.auditorGroups = this.ctx.helper.groupAuditors(phasePay.auditors, 'audit_sort');
  309. phasePay.userGroups = this.ctx.helper.groupAuditorsUniq(phasePay.auditorGroups, 'audit_order');
  310. phasePay.finalAuditorIds = phasePay.userGroups.length > 1 ? phasePay.userGroups[phasePay.userGroups.length - 1].map(x => { return x.audit_id; }) : [];
  311. }
  312. async doCheckPhase(phasePay) {
  313. await this.loadUser(phasePay);
  314. const accountId = this.ctx.session.sessionUser.accountId;
  315. if (phasePay.audit_status === audit.status.uncheck) {
  316. phasePay.readOnly = accountId !== phasePay.create_user_id;
  317. phasePay.curTimes = phasePay.audit_times;
  318. phasePay.curOrder = 0;
  319. } else if (phasePay.audit_status === audit.status.checkNo) {
  320. phasePay.readOnly = accountId !== phasePay.create_user_id;
  321. if (!phasePay.readOnly) {
  322. phasePay.curTimes = phasePay.times;
  323. phasePay.curOrder = 0;
  324. } else {
  325. const checkNoAudit = await this.service.phasePayAudit.getDataByCondition({
  326. phase_id: phasePay.id, audit_times: phasePay.audit_times - 1, status: audit.status.checkNo,
  327. });
  328. phasePay.curTimes = phasePay.times - 1;
  329. phasePay.curOrder = checkNoAudit.audit_order;
  330. }
  331. } else if (phasePay.audit_status === audit.status.checked) {
  332. phasePay.readOnly = true;
  333. phasePay.curTimes = phasePay.audit_times;
  334. phasePay.curOrder = phasePay.audit_max_order;
  335. } else {
  336. // 会签,会签人部分审批通过时,只读,但是curOrder需按原来的取值
  337. phasePay.curTimes = phasePay.audit_times;
  338. phasePay.curOrder = phasePay.flowAuditorIds.indexOf(accountId) < 0 ? phasePay.curAuditors[0].order : phasePay.curAuditors[0].order - 1;
  339. phasePay.readOnly = !_.isEqual(stage.flowAuditorIds, stage.curAuditorIds);
  340. phasePay.canCheck = phasePay.readOnly && stage.curAuditorIds.indexOf(accountId) > 0;
  341. }
  342. }
  343. async delete(id) {
  344. const conn = await this.db.beginTransaction();
  345. try {
  346. await conn.delete(this.tableName, { id });
  347. await conn.delete(this.ctx.service.phasePayDetail.tableName, { phase_id: id });
  348. // todo 不确定是否真删除,是否需要防止用户找回附件 ?
  349. // await conn.delete(this.ctx.service.phasePayAtt.tableName, { phase_id: id });
  350. await conn.delete(this.ctx.service.phasePayAudit.tableName, { phase_id: id });
  351. } catch (err) {
  352. await conn.rollback();
  353. throw err;
  354. }
  355. }
  356. async save(phasePay, data) {
  357. await this.defaultUpdate({id: phasePay.id, phase_date: data.phase_date, memo: data.memo});
  358. }
  359. }
  360. return PhasePay;
  361. };