|
@@ -9,8 +9,13 @@
|
|
|
*/
|
|
|
|
|
|
const Ledger = require('../lib/ledger');
|
|
|
+const PayCalculator = require('../lib/pay_calc');
|
|
|
+
|
|
|
const auditConst = require('../const/audit');
|
|
|
+const payConst = require('../const/deal_pay');
|
|
|
+
|
|
|
const moment = require('moment');
|
|
|
+
|
|
|
const indexPre = 'id_';
|
|
|
|
|
|
const gatherUtils = {
|
|
@@ -125,6 +130,7 @@ module.exports = app => {
|
|
|
rootId: -1
|
|
|
});
|
|
|
this.resultTenderInfo = [];
|
|
|
+ this.resultDealpPay = [];
|
|
|
}
|
|
|
|
|
|
_checkSpecialTender(tender, special) {
|
|
@@ -434,7 +440,7 @@ module.exports = app => {
|
|
|
|
|
|
async _gatherMonthTenderInfo(sTender, index, month, hasPre) {
|
|
|
const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
|
|
|
- const info = this._getBaseTenderInfo(tender);
|
|
|
+ const info = await this._getBaseTenderInfo(tender);
|
|
|
const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
|
|
|
this._getStageTenderInfo(stage, info);
|
|
|
this.resultTenderInfo.push(info);
|
|
@@ -442,32 +448,8 @@ module.exports = app => {
|
|
|
|
|
|
async _gatherZoneTenderInfo(sTender, index, zone) {
|
|
|
const helper = this.ctx.helper;
|
|
|
- /**
|
|
|
- * 汇总并合并 相关数据
|
|
|
- * @param {Array} index - 主数据
|
|
|
- * @param {Array[]}rela - 相关数据 {data, fields, prefix, relaId}
|
|
|
- */
|
|
|
- const sumAssignRelaData = function (index, rela) {
|
|
|
- const loadFields = function (datas, fields, prefix, relaId) {
|
|
|
- for (const d of datas) {
|
|
|
- const key = indexPre + d[relaId];
|
|
|
- const m = index[key];
|
|
|
- if (m) {
|
|
|
- for (const f of fields) {
|
|
|
- if (d[f] !== undefined) {
|
|
|
- m[prefix + f] = helper.add(m[prefix + f], d[f]);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
- for (const r of rela) {
|
|
|
- loadFields(r.data, r.fields, r.prefix, r.relaId);
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
|
|
|
- const info = this._getBaseTenderInfo(tender);
|
|
|
+ const info = await this._getBaseTenderInfo(tender);
|
|
|
|
|
|
const times = zone.split(' - ');
|
|
|
if (times.length !== 2) throw '选择的汇总周期无效';
|
|
@@ -494,7 +476,7 @@ module.exports = app => {
|
|
|
|
|
|
async _gatherFinalTenderInfo(sTender, index, hasPre) {
|
|
|
const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
|
|
|
- const info = this._getBaseTenderInfo(tender);
|
|
|
+ const info = await this._getBaseTenderInfo(tender);
|
|
|
const stages = await this.db.select(this.ctx.service.stage.tableName, {
|
|
|
where: { tid: tender.id },
|
|
|
orders: [['order', 'desc']],
|
|
@@ -511,7 +493,7 @@ module.exports = app => {
|
|
|
|
|
|
async _gatherCheckedFinalTenderInfo(sTender, index, hasPre) {
|
|
|
const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
|
|
|
- const info = this._getBaseTenderInfo(tender);
|
|
|
+ const info = await this._getBaseTenderInfo(tender);
|
|
|
const stages = await this.db.select(this.ctx.service.stage.tableName, {
|
|
|
where: { tid: tender.id },
|
|
|
orders: [['order', 'desc']],
|
|
@@ -528,14 +510,14 @@ module.exports = app => {
|
|
|
|
|
|
async _gatherLedgerTenderInfo(sTender, index) {
|
|
|
const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
|
|
|
- const info = this._getBaseTenderInfo(tender);
|
|
|
+ const info = await this._getBaseTenderInfo(tender);
|
|
|
this.resultTenderInfo.push(info);
|
|
|
}
|
|
|
|
|
|
async _gatherSpecialTenderInfo(sTender, sKey) {
|
|
|
const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
|
|
|
|
|
|
- const info = this._getBaseTenderInfo(tender);
|
|
|
+ const info = await this._getBaseTenderInfo(tender);
|
|
|
info.spec = sKey;
|
|
|
this.resultTenderInfo.push(info);
|
|
|
}
|
|
@@ -572,6 +554,167 @@ module.exports = app => {
|
|
|
await this._gatherSpecialTenderInfo(tender, specialKey);
|
|
|
}
|
|
|
}
|
|
|
+ return this.resultTenderInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 合同支付
|
|
|
+ */
|
|
|
+ _gatherPayRecord(dealPay, fun) {
|
|
|
+ let rdp;
|
|
|
+ if (dealPay.ptype !== payConst.payType.normal) {
|
|
|
+ rdp = this.ctx.helper._.find(this.resultDealpPay, {ptype: dealPay.ptype});
|
|
|
+ } else {
|
|
|
+ rdp = this.ctx.helper._.find(this.resultDealpPay, {
|
|
|
+ name: dealPay.name,
|
|
|
+ minus: dealPay.minus ? true : false,
|
|
|
+ is_yf: dealPay.is_yf ? true : false,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (!rdp) {
|
|
|
+ rdp = {
|
|
|
+ ptype: dealPay.ptype,
|
|
|
+ name: dealPay.name,
|
|
|
+ minus: dealPay.minus ? true : false,
|
|
|
+ is_yf: dealPay.is_yf ? true : false,
|
|
|
+ };
|
|
|
+ this.resultDealpPay.push(rdp);
|
|
|
+ }
|
|
|
+ if (fun) fun(rdp, dealPay);
|
|
|
+ }
|
|
|
+
|
|
|
+ async _checkStagePayCalc(tender, stage, dealPay) {
|
|
|
+ if (!stage.readOnly) {
|
|
|
+ // 计算 本期金额
|
|
|
+ const payCalculator = new PayCalculator(this.ctx, stage, tender.info);
|
|
|
+ await payCalculator.calculateAll(dealPay);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async _gatherStagePay(index, tender, stage, hasPre) {
|
|
|
+ if (stage) {
|
|
|
+ const helper = this.ctx.helper;
|
|
|
+ await this.ctx.service.stage.doCheckStage(stage);
|
|
|
+
|
|
|
+ const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
|
|
|
+ await this._checkStagePayCalc(tender, stage, dealPay);
|
|
|
+ for (const dp of dealPay) {
|
|
|
+ dp.end_tp = helper.add(dp.pre_tp, dp.tp);
|
|
|
+ this._gatherPayRecord(dp, function (gatherData, sourceData) {
|
|
|
+ const preFix = 't_' + index + '_';
|
|
|
+ gatherData[preFix + '_id'] = tender.id;
|
|
|
+ gatherData[preFix + '_name'] = tender.name;
|
|
|
+
|
|
|
+ gatherData[preFix + 'tp'] = helper.add(gatherData[preFix + 'tp'], sourceData.tp);
|
|
|
+ gatherData[preFix + 'pre_tp'] = helper.add(gatherData[preFix + 'pre_tp'], sourceData.pre_tp);
|
|
|
+ gatherData[preFix + 'end_tp'] = helper.add(gatherData[preFix + 'end_tp'], sourceData.end_tp);
|
|
|
+
|
|
|
+ gatherData['s_' + 'tp'] = helper.add(gatherData['s_' + 'tp'], sourceData.tp);
|
|
|
+ gatherData['s_' + 'pre_tp'] = helper.add(gatherData['s_' + 'pre_tp'], sourceData.pre_tp);
|
|
|
+ gatherData['s_' + 'end_tp'] = helper.add(gatherData['s_' + 'end_tp'], sourceData.end_tp);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async _gatherMonthStagePay(sTender, index, month, hasPre) {
|
|
|
+ const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
|
|
|
+ const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
|
|
|
+ await this._gatherStagePay(index, tender, stage, hasPre);
|
|
|
+ }
|
|
|
+
|
|
|
+ async _gatherZoneStagePay(sTender, index, zone) {
|
|
|
+ const helper = this.ctx.helper;
|
|
|
+
|
|
|
+ const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
|
|
|
+
|
|
|
+ const times = zone.split(' - ');
|
|
|
+ if (times.length !== 2) throw '选择的汇总周期无效';
|
|
|
+ const beginTime = moment(times[0], 'YYYY-MM').date();
|
|
|
+ const endTime = moment(times[1], 'YYYY-MM').date();
|
|
|
+
|
|
|
+ const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tender.id } });
|
|
|
+ for (const stage of stages) {
|
|
|
+ const sTime = moment(stage.s_time, 'YYYY-MM').date();
|
|
|
+ if (sTime >= beginTime && sTime <= endTime) {
|
|
|
+ await this.ctx.service.stage.doCheckStage(stage);
|
|
|
+
|
|
|
+ const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
|
|
|
+ await this._checkStagePayCalc(tender, stage, dealPay);
|
|
|
+ for (const dp of dealPay) {
|
|
|
+ dp.end_tp = helper.add(dp.pre_tp, dp.tp);
|
|
|
+ this._gatherPayRecord(dp, function (gatherData, sourceData) {
|
|
|
+ const preFix = 't_' + index + '_';
|
|
|
+ gatherData[preFix + '_id'] = tender.id;
|
|
|
+ gatherData[preFix + '_name'] = tender.name;
|
|
|
+
|
|
|
+ gatherData[preFix + 'tp'] = helper.add(gatherData[preFix + 'tp'], sourceData.tp);
|
|
|
+ gatherData['s_' + 'tp'] = helper.add(gatherData['s_' + 'tp'], sourceData.tp);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async _gatherFinalStagePay(sTender, index, hasPre) {
|
|
|
+ const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
|
|
|
+ const stages = await this.db.select(this.ctx.service.stage.tableName, {
|
|
|
+ where: { tid: tender.id },
|
|
|
+ orders: [['order', 'desc']],
|
|
|
+ });
|
|
|
+ if (stages.length !== 0) {
|
|
|
+ const lastStage = stages[0];
|
|
|
+ if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
|
|
|
+ stages.splice(0, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ await this._gatherStagePay(index, tender, stages[0], hasPre);
|
|
|
+ }
|
|
|
+
|
|
|
+ async _gatherCheckedFinalStagePay(sTender, index, hasPre) {
|
|
|
+ const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
|
|
|
+ const stages = await this.db.select(this.ctx.service.stage.tableName, {
|
|
|
+ where: { tid: tender.id },
|
|
|
+ orders: [['order', 'desc']],
|
|
|
+ });
|
|
|
+ if (stages.length !== 0) {
|
|
|
+ const lastStage = stages[0];
|
|
|
+ if (lastStage.status !== auditConst.stage.status.checked) {
|
|
|
+ stages.splice(0, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ await this._gatherStagePay(index, tender, stages[0], hasPre);
|
|
|
+ }
|
|
|
+
|
|
|
+ async getGatherStagePay(memFieldKeys, gsDefine, gsCustom) {
|
|
|
+ if (!gsDefine || !gsDefine.enable) return [];
|
|
|
+ if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
|
|
|
+
|
|
|
+ this.resultTenderInfo = [];
|
|
|
+ const gsSetting = JSON.parse(gsDefine.setting);
|
|
|
+ let commonIndex = 0;
|
|
|
+ for (const tender of gsCustom.tenders) {
|
|
|
+ const specialKey = this._checkSpecialTender(tender, gsSetting.special);
|
|
|
+ if (specialKey === '') {
|
|
|
+ switch (gsSetting.type) {
|
|
|
+ case 'month':
|
|
|
+ await this._gatherMonthStagePay(tender, commonIndex, gsCustom.month, gsSetting.hasPre);
|
|
|
+ break;
|
|
|
+ case 'zone':
|
|
|
+ await this._gatherZoneStagePay(tender, commonIndex, gsCustom.zone);
|
|
|
+ break;
|
|
|
+ case 'final':
|
|
|
+ await this._gatherFinalStagePay(tender, commonIndex, gsSetting.hasPre);
|
|
|
+ break;
|
|
|
+ case 'checked-final':
|
|
|
+ await this._gatherCheckedFinalStagePay(tender, commonIndex, gsSetting.hasPre);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ commonIndex++;
|
|
|
+ }
|
|
|
+ // 合同支付,只有在每一期中有数据,故特殊标段直接不汇总合同支付
|
|
|
+ }
|
|
|
+ return this.resultDealpPay;
|
|
|
}
|
|
|
}
|
|
|
|