|
@@ -250,23 +250,19 @@ module.exports = app => {
|
|
|
return changeBills;
|
|
|
}
|
|
|
|
|
|
- async _reCalcStageBills(tenderId, newDecimal, oldDecimal) {
|
|
|
- const updateStageBills = [], insertStageBills = [];
|
|
|
- const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
|
|
|
- if (stages.length === 0 || newDecimal.tp === oldDecimal.tp) return [updateStageBills, insertStageBills];
|
|
|
-
|
|
|
+ async _reCalcStageBillsUp(stages, updateStageBills, insertStageBills, decimal) {
|
|
|
for (const stage of stages) {
|
|
|
const stageBills = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
|
|
|
const bills = await this.ctx.service.ledger.getAllDataByCondition({
|
|
|
columns: ['id', 'unit_price'],
|
|
|
- where: { tender_id: tenderId, is_leaf: true },
|
|
|
+ where: { tender_id: stage.tid, is_leaf: true },
|
|
|
});
|
|
|
for (const sb of stageBills) {
|
|
|
const b = bills.find(x => {return x.id === sb.lid});
|
|
|
if (!b) continue;
|
|
|
- const contract_tp = this.ctx.helper.mul(b.unit_price, sb.contract_qty, newDecimal.tp);
|
|
|
- const qc_tp = this.ctx.helper.mul(b.unit_price, sb.qc_qty, newDecimal.tp);
|
|
|
- const ex_stage_tp1 = this.ctx.helper.mul(b.unit_price, sb.ex_stage_qty1, newDecimal.tp);
|
|
|
+ const contract_tp = this.ctx.helper.mul(b.unit_price, sb.contract_qty, decimal.tp);
|
|
|
+ const qc_tp = this.ctx.helper.mul(b.unit_price, sb.qc_qty, decimal.tp);
|
|
|
+ const ex_stage_tp1 = this.ctx.helper.mul(b.unit_price, sb.ex_stage_qty1, decimal.tp);
|
|
|
if (contract_tp == sb.contract_tp && qc_tp === sb.qc_tp && ex_stage_tp1 === sb.ex_stage_tp1) continue;
|
|
|
|
|
|
if (sb.times === stage.times && sb.order === 0) {
|
|
@@ -285,6 +281,67 @@ module.exports = app => {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ async _reCalcStageBillsTp(stages, updateStageBills, insertStageBills, decimal) {
|
|
|
+ stages.sort((x, y) => { return x.order - y.order; });
|
|
|
+ const preStage = stages[0].order > 1 ? await this.ctx.service.stage.getDataByCondition({ tid: stages[0].tid, order: stages[0].order - 1 }) : null;
|
|
|
+ const preStageBills = preStage ? await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { tid: preStage.tid, sid: preStage.id } }) : [];
|
|
|
+ for (const stage of stages) {
|
|
|
+ const stageBills = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
|
|
|
+ const bills = await this.ctx.service.ledger.getAllDataByCondition({
|
|
|
+ columns: ['id', 'unit_price', 'quantity', 'total_price'],
|
|
|
+ where: { tender_id: stage.tid, is_leaf: true },
|
|
|
+ });
|
|
|
+ for (const sb of stageBills) {
|
|
|
+ const preSb = preStageBills.find(x => { return x.lid === sb.lid; });
|
|
|
+ const b = bills.find(x => {return x.id === sb.lid});
|
|
|
+ if (!b) continue;
|
|
|
+ let activeQty = this.ctx.helper.add(b.quantity, sb.qc_minus_qty);
|
|
|
+ let end_contract_qty = sb.contract_qty;
|
|
|
+ if (preSb) {
|
|
|
+ activeQty = this.ctx.helper.add(activeQty, preSb.qc_minus_qty);
|
|
|
+ end_contract_qty = this.ctx.helper.add(end_contract_qty, preSb.contract_qty);
|
|
|
+ }
|
|
|
+ const end_contract_tp = this.ctx.helper.mul(this.ctx.helper.div(end_contract_qty, activeQty), b.total_price, decimal.tp);
|
|
|
+ const contract_tp = preSb ? this.ctx.helper.sub(end_contract_tp, preSb.contract_tp) : end_contract_tp;
|
|
|
+ const qc_tp = this.ctx.helper.mul(b.unit_price, sb.qc_qty, decimal.tp);
|
|
|
+ const ex_stage_tp1 = this.ctx.helper.mul(b.unit_price, sb.ex_stage_qty1, decimal.tp);
|
|
|
+ if (contract_tp == sb.contract_tp && qc_tp === sb.qc_tp && ex_stage_tp1 === sb.ex_stage_tp1) continue;
|
|
|
+
|
|
|
+ if (sb.times === stage.times && sb.order === 0) {
|
|
|
+ updateStageBills.push({
|
|
|
+ id: sb.id, contract_tp, qc_tp, ex_stage_tp1,
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ insertStageBills.push({
|
|
|
+ tid: stage.tid, lid: sb.lid, sid: stage.id, said: this.ctx.session.sessionUser.accountId,
|
|
|
+ times: stage.times, order: 0,
|
|
|
+ contract_qty: sb.contract_qty, contract_expr: sb.contract_expr, contract_tp,
|
|
|
+ qc_qty: sb.qc_qty, qc_tp,
|
|
|
+ ex_stage_qty1: sb.ex_stage_qty1, ex_stage_tp1,
|
|
|
+ postil: sb.postil,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async _reCalcStageBills(tenderId, newDecimal, oldDecimal) {
|
|
|
+ const updateStageBills = [], insertStageBills = [];
|
|
|
+ const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
|
|
|
+ if (stages.length === 0 || newDecimal.tp === oldDecimal.tp) return [updateStageBills, insertStageBills];
|
|
|
+ switch (this.ctx.tender.info.calc_type) {
|
|
|
+ case 'up':
|
|
|
+ await this._reCalcStageBillsUp(stages, updateStageBills, insertStageBills, newDecimal);
|
|
|
+ break;
|
|
|
+ case 'tp':
|
|
|
+ await this._reCalcStageBillsTp(stages, updateStageBills, insertStageBills, newDecimal);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ await this._reCalcStageBillsUp(stages, updateStageBills, insertStageBills, newDecimal);
|
|
|
+ break;
|
|
|
+ }
|
|
|
return [updateStageBills, insertStageBills];
|
|
|
}
|
|
|
|
|
@@ -386,6 +443,41 @@ module.exports = app => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ async _reCalcStageBills2(tenderId, calcType) {
|
|
|
+ const updateStageBills = [], insertStageBills = [];
|
|
|
+ const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
|
|
|
+ if (stages.length === 0) return [updateStageBills, insertStageBills];
|
|
|
+ switch (calcType) {
|
|
|
+ case 'up':
|
|
|
+ await this._reCalcStageBillsUp(stages, updateStageBills, insertStageBills, this.ctx.tender.info.decimal);
|
|
|
+ break;
|
|
|
+ case 'tp':
|
|
|
+ await this._reCalcStageBillsTp(stages, updateStageBills, insertStageBills, this.ctx.tender.info.decimal);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ await this._reCalcStageBillsUp(stages, updateStageBills, insertStageBills, this.ctx.tender.info.decimal);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return [updateStageBills, insertStageBills];
|
|
|
+ }
|
|
|
+
|
|
|
+ async saveCalcType(tenderId, calcType) {
|
|
|
+ if (!this.ctx.tender.info.calc_type && calcType === 'up') return;
|
|
|
+ if (this.ctx.tender.info.calc_type && calcType === this.ctx.tender.info.calc_type) return;
|
|
|
+
|
|
|
+ const [updateStageBills, insertStageBills] = await this._reCalcStageBills2(tenderId, calcType);
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ await transaction.update(this.tableName, { calc_type: calcType }, { where: { tid: tenderId } });
|
|
|
+ if (updateStageBills.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, updateStageBills);
|
|
|
+ if (insertStageBills.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, insertStageBills);
|
|
|
+ await transaction.commit();
|
|
|
+ } catch(err) {
|
|
|
+ await transaction.rollback();
|
|
|
+ throw '保存计价类型失败';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取标段审批相关信息 (审批设置用)
|
|
|
* @param tenderId
|