phase_pay.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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},
  17. {name: '本期合同计量', code: 'bqht', limit: true, sort: 10},
  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 getNewOrder(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) {
  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;
  115. result.qd_qc_tp = qdSum.qc_tp;
  116. result.qd_pc_tp = qdSum.pc_tp;
  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;
  122. result.more_bg_tp = bg.more;
  123. result.great_bg_tp = bg.great;
  124. }
  125. result.gather_tp = this.ctx.helper.sum([result.contract_tp, result.qc_tp, result.pc_tp]);
  126. result.qd_gather_tp = this.ctx.helper.sum([result.qd_contract_tp, result.qd_qc_tp, result.qd_pc_tp]);
  127. const bonusSum = await this.ctx.service.stageBonus.getSumTp(relaStage);
  128. result.bonus_positive_tp = bonusSum.positive_tp;
  129. result.bonus_negative_tp = bonusSum.negative_tp;
  130. result.bonus_tp = bonusSum.sum_tp;
  131. const jgclSum = await this.ctx.service.stageJgcl.getSumTp(relaStage);
  132. result.jgcl_tp = jgclSum.sum_tp;
  133. const otherSum = await this.ctx.service.stageOther.getSumTp(relaStage);
  134. result.other_tp = otherSum.sum_tp;
  135. const safeProdSum = await this.ctx.service.stageSafeProd.getSumTp(relaStage);
  136. result.safe_prod_tp = safeProdSum.sum_tp;
  137. const tempLandSum = await this.ctx.service.stageTempLand.getSumTp(relaStage);
  138. result.temp_land_tp = tempLandSum.sum_tp;
  139. return result;
  140. }
  141. /**
  142. * 获取 当期的 计算基数
  143. * @return {Promise<any>}
  144. */
  145. getPhasePayCalcBase(phasePay, tenderInfo) {
  146. const payCalcBase = JSON.parse(JSON.stringify(calcBase));
  147. for (const cb of payCalcBase) {
  148. switch (cb.code) {
  149. case 'htj':
  150. cb.value = tenderInfo.deal_param.contractPrice;
  151. break;
  152. case 'zlje':
  153. cb.value = tenderInfo.deal_param.zanLiePrice;
  154. break;
  155. case 'htjszl':
  156. cb.value = this.ctx.helper.sub(tenderInfo.deal_param.contractPrice, tenderInfo.deal_param.zanLiePrice);
  157. break;
  158. case 'kgyfk':
  159. cb.value = tenderInfo.deal_param.startAdvance;
  160. break;
  161. case 'clyfk':
  162. cb.value = tenderInfo.deal_param.materialAdvance;
  163. break;
  164. case 'bqwc':
  165. cb.value = phasePay.calc_base.gather_tp;
  166. break;
  167. case 'bqht':
  168. cb.value = phasePay.calc_base.contract_tp;
  169. break;
  170. case 'bqbg':
  171. cb.value = phasePay.calc_base.qc_tp;
  172. break;
  173. case 'bqqdwc':
  174. cb.value = phasePay.qd_gather_tp;
  175. break;
  176. case 'bqqdht':
  177. cb.value = phasePay.qd_contract_tp;
  178. break;
  179. case 'bqqdbg':
  180. cb.value = phasePay.qd_qc_tp;
  181. break;
  182. case 'ybbqwc':
  183. cb.value = phasePay.gather_100_tp;
  184. break;
  185. case 'ybbqbg':
  186. cb.value = phasePay.common_bg_tp;
  187. break;
  188. case 'jdbqbg':
  189. cb.value = phasePay.more_bg_tp;
  190. break;
  191. case 'zdbqbg':
  192. cb.value = phasePay.great_bg_tp;
  193. break;
  194. case 'bonus':
  195. cb.value = phasePay.bonus_positive_tp;
  196. break;
  197. case 'fine':
  198. cb.value = phasePay.bonus_negative_tp;
  199. break;
  200. case 'jgcl':
  201. cb.value = phasePay.jgcl_tp;
  202. break;
  203. case 'aqsc':
  204. cb.value = phasePay.safe_prod_tp;
  205. break;
  206. case 'lsyd':
  207. cb.value = phasePay.temp_land_tp;
  208. break;
  209. default:
  210. cb.value = 0;
  211. }
  212. }
  213. return payCalcBase;
  214. }
  215. async add(tid, relaStage, phaseDate, memo) {
  216. if (!tid) throw '数据错误';
  217. const user_id = this.ctx.session.sessionUser.accountId;
  218. const maxOrder = await this.getNewOrder();
  219. const data = {
  220. id: this.uuid.v4(), tid: tid, create_user_id: user_id, update_user_id: user_id,
  221. phase_order: maxOrder + 1, phase_date: phaseDate, memo,
  222. audit_times: 1, audit_status: audit.status.uncheck,
  223. rela_stage: JSON.stringify(relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; })),
  224. };
  225. if (await this._checkRelaStageConflict(relaStage, data)) throw '选择的计量期,已被调用,请刷新页面后选择计量期新增合同支付';
  226. const calcBase = await this.getCalcBase(relaStage);
  227. data.calc_base = JSON.stringify(calcBase);
  228. const transaction = await this.db.beginTransaction();
  229. try {
  230. const result = await transaction.insert(this.tableName, data);
  231. if (result.affectedRows !== 1) throw '新增合同支付失败';
  232. await this.ctx.service.phasePayDetail.initPhaseData(transaction, data);
  233. await transaction.commit();
  234. return data;
  235. } catch(err) {
  236. await transaction.rollback();
  237. throw err;
  238. }
  239. }
  240. async refreshCalcBase(id) {
  241. const curPay = await this.getPhasePay(id);
  242. if (!curPay) throw '合同支付不存在, 请刷新页面重试';
  243. const calcBase = await this.getCalcBase(relaStage);
  244. await this.defaultUpdate({
  245. id, update_user_id: this.ctx.session.sessionUser.accountId,
  246. calc_base: JSON.stringify(calcBase),
  247. rela_stage: JSON.stringify(relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; })),
  248. calc_base_time: new Date()
  249. });
  250. }
  251. async resetRelaStageId(id, relaStage) {
  252. const curPay = await this.getPhasePay(id);
  253. if (!curPay) throw '合同支付不存在, 请刷新页面重试';
  254. if (await this._checkRelaStageConflict(relaStage, curPay)) throw '选择的计量期,已被调用,请刷新页面后选择计量期新增合同支付';
  255. const calcBase = await this.getCalcBase(relaStage);
  256. await this.defaultUpdate({
  257. id, update_user_id: this.ctx.session.sessionUser.accountId,
  258. calc_base: JSON.stringify(calcBase),
  259. rela_stage: JSON.stringify(relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; })),
  260. calc_base_time: new Date()
  261. });
  262. }
  263. async loadUser(phasePay) {
  264. // todo 加载审批人
  265. phasePay.user = await this.ctx.service.projectAccount.getAccountInfoById(phasePay.create_user_id);
  266. phasePay.curAuditors = [];
  267. // phasePay.curAuditors = await this.ctx.service.phasePayAudit.getAllDataByCondition({
  268. // where: { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_status: audit.status.checking }
  269. // });
  270. phasePay.curAuditorIds = phasePay.curAuditors.map(x => { return x.audit_id; });
  271. phasePay.flowAuditors = phasePay.curAuditors.length === 0 ? []
  272. : await this.ctx.service.phasePayAudit.getAllDataByCondition({
  273. where: { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_sort: phasePay.curAuditors[0].audit_sort }
  274. });
  275. phasePay.flowAuditorIds = phasePay.curAuditors.map(x => { return x.audit_id; });
  276. }
  277. async doCheckPhase(phasePay) {
  278. await this.loadUser(phasePay);
  279. const accountId = this.ctx.session.sessionUser.accountId;
  280. if (phasePay.audit_status === audit.status.uncheck) {
  281. phasePay.readOnly = accountId !== phasePay.create_user_id;
  282. phasePay.curTimes = phasePay.audit_times;
  283. phasePay.curOrder = 0;
  284. } else if (phasePay.audit_status === audit.status.checkNo) {
  285. phasePay.readOnly = accountId !== phasePay.create_user_id;
  286. if (!phasePay.readOnly) {
  287. phasePay.curTimes = phasePay.times;
  288. phasePay.curOrder = 0;
  289. } else {
  290. const checkNoAudit = await this.service.phasePayAudit.getDataByCondition({
  291. phase_id: phasePay.id, audit_times: phasePay.audit_times - 1, status: audit.status.checkNo,
  292. });
  293. phasePay.curTimes = phasePay.times - 1;
  294. phasePay.curOrder = checkNoAudit.audit_order;
  295. }
  296. } else if (phasePay.audit_status === audit.status.checked) {
  297. phasePay.readOnly = true;
  298. phasePay.curTimes = phasePay.audit_times;
  299. phasePay.curOrder = phasePay.audit_max_order;
  300. } else {
  301. // 会签,会签人部分审批通过时,只读,但是curOrder需按原来的取值
  302. phasePay.curTimes = phasePay.audit_times;
  303. phasePay.curOrder = phasePay.flowAuditorIds.indexOf(accountId) < 0 ? phasePay.curAuditors[0].order : phasePay.curAuditors[0].order - 1;
  304. phasePay.readOnly = !_.isEqual(stage.flowAuditorIds, stage.curAuditorIds);
  305. phasePay.canCheck = phasePay.readOnly && stage.curAuditorIds.indexOf(accountId) > 0;
  306. }
  307. }
  308. }
  309. return PhasePay;
  310. };