|
@@ -93,6 +93,153 @@ module.exports = app => {
|
|
|
return { pays: await this.getPays(options, cid), node: { update: node } };
|
|
return { pays: await this.getPays(options, cid), node: { update: node } };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ async addTenderPays(options, cid, stageIds) {
|
|
|
|
|
+ const node = await this.ctx.service.contract.getDataById(cid);
|
|
|
|
|
+ if (!node) {
|
|
|
|
|
+ throw '合同不存在';
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!node.rela_tender) {
|
|
|
|
|
+ throw '该合同没有关联标段';
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!Array.isArray(stageIds) || stageIds.length === 0) {
|
|
|
|
|
+ throw '请选择至少一个付款阶段';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const ids = this.ctx.helper._.uniq(stageIds.map(id => String(id).trim()).filter(id => id));
|
|
|
|
|
+ if (ids.length !== stageIds.length) {
|
|
|
|
|
+ throw '付款阶段参数有误';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const stages = this.ctx.subProject.page_show.phasePay
|
|
|
|
|
+ ? await this.ctx.service.phasePay.getDatasForContract(node.rela_tender)
|
|
|
|
|
+ : await this.ctx.service.stagePay.getDatasForContract(node.rela_tender);
|
|
|
|
|
+ const selectedStages = ids.map(id => stages.find(stage => stage.id + '' === id));
|
|
|
|
|
+ if (selectedStages.some(stage => !stage)) {
|
|
|
|
|
+ throw '付款阶段不存在或未通过审批';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const existedPays = await this.getAllDataByCondition({ where: { cid, rela_stage: ids } });
|
|
|
|
|
+ if (existedPays.length > 0) {
|
|
|
|
|
+ throw '选择的付款阶段已关联该合同';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const isPhasePay = !!this.ctx.subProject.page_show.phasePay;
|
|
|
|
|
+ const sourcePays = isPhasePay
|
|
|
|
|
+ ? await this.ctx.service.phasePay.getAllDataByCondition({ where: { id: ids } })
|
|
|
|
|
+ : await this.ctx.service.stage.getAllDataByCondition({ where: { id: ids } });
|
|
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
|
|
+ try {
|
|
|
|
|
+ for (const stage of selectedStages) {
|
|
|
|
|
+ const sourcePay = sourcePays.find(item => item.id + '' === stage.id + '');
|
|
|
|
|
+ if (!sourcePay) {
|
|
|
|
|
+ throw '付款阶段源数据不存在';
|
|
|
|
|
+ }
|
|
|
|
|
+ const insertData = {
|
|
|
|
|
+ spid: options.spid || null,
|
|
|
|
|
+ tid: options.tid || null,
|
|
|
|
|
+ contract_type: options.contract_type,
|
|
|
|
|
+ cid,
|
|
|
|
|
+ uid: isPhasePay ? sourcePay.create_user_id : sourcePay.user_id,
|
|
|
|
|
+ pay_time: stage.pay_time,
|
|
|
|
|
+ used: stage.used || '合同',
|
|
|
|
|
+ pay_price: stage.pay_price,
|
|
|
|
|
+ debit_price: stage.debit_price,
|
|
|
|
|
+ yf_price: stage.yf_price,
|
|
|
|
|
+ sf_price: stage.sf_price,
|
|
|
|
|
+ pay_type: stage.pay_type,
|
|
|
|
|
+ remark: '',
|
|
|
|
|
+ rela_stage: stage.id,
|
|
|
|
|
+ create_time: new Date(),
|
|
|
|
|
+ status: auditConst.status.checked,
|
|
|
|
|
+ times: 1,
|
|
|
|
|
+ final_auditor_str: sourcePay.final_auditor_str || '',
|
|
|
|
|
+ need_shenpi: options.spid
|
|
|
|
|
+ ? this.ctx.subProject.page_show.openContractPaySubProjectShenpi
|
|
|
|
|
+ : this.ctx.subProject.page_show.openContractPayTenderShenpi,
|
|
|
|
|
+ };
|
|
|
|
|
+ const result = await transaction.insert(this.tableName, insertData);
|
|
|
|
|
+ insertData.id = result.insertId;
|
|
|
|
|
+ await this.copyTenderPayAudits(transaction, insertData, sourcePay, isPhasePay);
|
|
|
|
|
+ }
|
|
|
|
|
+ await this.calcContract(transaction, node);
|
|
|
|
|
+ await transaction.commit();
|
|
|
|
|
+ } catch (err) {
|
|
|
|
|
+ await transaction.rollback();
|
|
|
|
|
+ throw err;
|
|
|
|
|
+ }
|
|
|
|
|
+ return { pays: await this.getPays(options, cid), node: { update: node } };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async copyTenderPayAudits(transaction, contractPay, sourcePay, isPhasePay) {
|
|
|
|
|
+ const sourceAudits = isPhasePay
|
|
|
|
|
+ ? await this.ctx.service.phasePayAudit.getAuditors(sourcePay.id, sourcePay.audit_times)
|
|
|
|
|
+ : await this.ctx.service.stageAudit.getAuditors(sourcePay.id, sourcePay.times);
|
|
|
|
|
+ const audits = [];
|
|
|
|
|
+ if (isPhasePay) {
|
|
|
|
|
+ let preAuditTime = sourcePay.create_time;
|
|
|
|
|
+ for (const audit of sourceAudits) {
|
|
|
|
|
+ audits.push({
|
|
|
|
|
+ spid: contractPay.spid,
|
|
|
|
|
+ tid: contractPay.tid,
|
|
|
|
|
+ cid: contractPay.cid,
|
|
|
|
|
+ cpid: contractPay.id,
|
|
|
|
|
+ csid: null,
|
|
|
|
|
+ aid: audit.audit_id,
|
|
|
|
|
+ order: audit.active_order,
|
|
|
|
|
+ times: 1,
|
|
|
|
|
+ status: auditConst.status.checked,
|
|
|
|
|
+ begin_time: preAuditTime,
|
|
|
|
|
+ end_time: audit.audit_time,
|
|
|
|
|
+ opinion: audit.opinion || '',
|
|
|
|
|
+ audit_type: audit.audit_type,
|
|
|
|
|
+ audit_order: audit.audit_order,
|
|
|
|
|
+ });
|
|
|
|
|
+ if (audit.audit_time) {
|
|
|
|
|
+ preAuditTime = audit.audit_time;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ const firstAudit = sourceAudits.length > 0 ? sourceAudits[0] : null;
|
|
|
|
|
+ audits.push({
|
|
|
|
|
+ spid: contractPay.spid,
|
|
|
|
|
+ tid: contractPay.tid,
|
|
|
|
|
+ cid: contractPay.cid,
|
|
|
|
|
+ cpid: contractPay.id,
|
|
|
|
|
+ csid: null,
|
|
|
|
|
+ aid: sourcePay.user_id,
|
|
|
|
|
+ order: 0,
|
|
|
|
|
+ times: 1,
|
|
|
|
|
+ status: auditConst.status.checked,
|
|
|
|
|
+ begin_time: sourcePay.create_time,
|
|
|
|
|
+ end_time: firstAudit && firstAudit.begin_time ? firstAudit.begin_time : sourcePay.create_time,
|
|
|
|
|
+ opinion: '',
|
|
|
|
|
+ audit_type: auditType.key.common,
|
|
|
|
|
+ audit_order: 0,
|
|
|
|
|
+ });
|
|
|
|
|
+ for (const audit of sourceAudits) {
|
|
|
|
|
+ audits.push({
|
|
|
|
|
+ spid: contractPay.spid,
|
|
|
|
|
+ tid: contractPay.tid,
|
|
|
|
|
+ cid: contractPay.cid,
|
|
|
|
|
+ cpid: contractPay.id,
|
|
|
|
|
+ csid: null,
|
|
|
|
|
+ aid: audit.aid,
|
|
|
|
|
+ order: audit.order,
|
|
|
|
|
+ times: 1,
|
|
|
|
|
+ status: auditConst.status.checked,
|
|
|
|
|
+ begin_time: audit.begin_time,
|
|
|
|
|
+ end_time: audit.end_time,
|
|
|
|
|
+ opinion: audit.opinion || '',
|
|
|
|
|
+ audit_type: audit.audit_type,
|
|
|
|
|
+ audit_order: audit.audit_order,
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (audits.length > 0) {
|
|
|
|
|
+ await transaction.insert(this.ctx.service.contractSpAudit.tableName, audits);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
async save(options, cid, data) {
|
|
async save(options, cid, data) {
|
|
|
if (!data.id) {
|
|
if (!data.id) {
|
|
|
throw '参数有误';
|
|
throw '参数有误';
|