350 lines
14 KiB
Vue
350 lines
14 KiB
Vue
<!--
|
||
features/contract-payment/views/ContractPaymentManualReviewPage.vue
|
||
用途:待确认合同的人工复核弹窗。补齐阶段金额、比例和日期依据后进入合同台账。
|
||
依赖:contract-payment.api、统一消息提示、admin.contractPaymentReview 文案。
|
||
逻辑:由合同详情页的「人工复核」按钮打开。合同详情本身不改;已识别阶段预填。
|
||
勾选无此阶段则该阶段按 0 且不预警。底部四条是填写提示。确认按钮在规则满足前不可点,后端仍是最终校验。
|
||
-->
|
||
<script setup lang="ts">
|
||
import { computed, onMounted, ref, watch } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import { BusinessError } from '@/shared/api/client';
|
||
import { useMessage } from '@/shared/composables/useMessage';
|
||
import {
|
||
getContractPaymentContract,
|
||
submitContractPaymentManualReview,
|
||
type ContractPaymentContract,
|
||
} from '../api/contract-payment.api';
|
||
|
||
const props = defineProps<{
|
||
contractId: number;
|
||
}>();
|
||
|
||
const emit = defineEmits<{
|
||
close: [];
|
||
confirmed: [];
|
||
}>();
|
||
|
||
const STAGE_TYPES = ['prepayment', 'shipment', 'acceptance', 'warranty'] as const;
|
||
type StageType = typeof STAGE_TYPES[number];
|
||
|
||
interface StageForm {
|
||
nodeType: StageType;
|
||
skipped: boolean;
|
||
percent: string;
|
||
amount: string;
|
||
basisDate: string;
|
||
}
|
||
|
||
const { t } = useI18n();
|
||
const msg = useMessage();
|
||
const loading = ref(false);
|
||
const submitting = ref(false);
|
||
const contract = ref<ContractPaymentContract | null>(null);
|
||
const totalAmount = ref('');
|
||
const purchaseOrderNo = ref('');
|
||
const contractNo = ref('');
|
||
const archiveDate = ref('');
|
||
const stages = ref<StageForm[]>([]);
|
||
const contractId = computed(() => Number(props.contractId));
|
||
|
||
/**
|
||
* 按合同金额和比例计算阶段金额,四舍五入到分。
|
||
* 核心逻辑:与后端 Decimal 半分向上的结果对齐,差额在 0.02 以内才允许确认。
|
||
* @param percentRaw - 当前阶段比例文本。
|
||
* @returns 金额;合同金额或比例还不是数字时返回 null。
|
||
*/
|
||
function expectedAmount(percentRaw: string): number | null {
|
||
const total = Number(totalAmount.value);
|
||
const percent = Number(percentRaw);
|
||
if (!Number.isFinite(total) || !Number.isFinite(percent) || percentRaw.trim() === '') return null;
|
||
return Math.round(total * percent) / 100;
|
||
}
|
||
|
||
/**
|
||
* 把金额格式化成两位小数,供输入框展示。
|
||
* @param value - 已算出的金额。
|
||
* @returns 固定两位小数字符串。
|
||
*/
|
||
function formatAmount(value: number): string {
|
||
return value.toFixed(2);
|
||
}
|
||
|
||
/**
|
||
* 用合同详情预填复核表单。
|
||
* 核心逻辑:已有节点带出比例、金额和日期依据;没有节点的阶段留空,不能把空比例当成 0。
|
||
* 预付款没有日期时用合同归档日,因为预付款的日期依据就是归档日。
|
||
* @param row - 合同详情。
|
||
*/
|
||
function prefill(row: ContractPaymentContract): void {
|
||
totalAmount.value = row.total_amount || '';
|
||
purchaseOrderNo.value = row.purchase_order_no || '';
|
||
contractNo.value = row.contract_no || '';
|
||
archiveDate.value = (row.oa_archive_at || '').slice(0, 10);
|
||
stages.value = STAGE_TYPES.map((nodeType) => {
|
||
const node = (row.nodes || []).find((item) => item.node_type === nodeType);
|
||
const skipped = String(node?.status || '').trim().toLowerCase() === 'not_applicable';
|
||
const percentValue = Number(node?.percentage);
|
||
const percent = node?.percentage != null && node.percentage !== '' && Number.isFinite(percentValue) ? String(percentValue) : '';
|
||
const basisDate = node?.due_date || node?.anchor_date || (nodeType === 'prepayment' ? archiveDate.value : '');
|
||
const amount = percent === '' ? '' : (node?.amount || '');
|
||
return {
|
||
nodeType,
|
||
skipped,
|
||
percent: skipped ? '0' : percent,
|
||
amount: skipped ? '0' : amount,
|
||
basisDate: skipped ? '' : basisDate,
|
||
};
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 加载待复核合同。
|
||
* 核心逻辑:失败时保留空表单并提示,避免把上一份合同的数字提交到新主键。
|
||
*/
|
||
async function load(): Promise<void> {
|
||
loading.value = true;
|
||
contract.value = null;
|
||
try {
|
||
const row = await getContractPaymentContract(contractId.value);
|
||
contract.value = row;
|
||
prefill(row);
|
||
} catch (error) {
|
||
msg.error(error instanceof BusinessError ? error.message : t('admin.contractPaymentReview.formLoadFailed'));
|
||
} finally {
|
||
loading.value = false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 比例变化时按合同金额重算该阶段金额。
|
||
* @param stage - 正在编辑的阶段。
|
||
*/
|
||
function onPercentInput(stage: StageForm): void {
|
||
if (stage.skipped) return;
|
||
const expected = expectedAmount(stage.percent);
|
||
stage.amount = expected == null ? '' : formatAmount(expected);
|
||
}
|
||
|
||
/**
|
||
* 合同金额变化时重算所有未跳过且已填比例的阶段金额。
|
||
*/
|
||
function onTotalInput(): void {
|
||
stages.value.forEach((stage) => {
|
||
if (stage.skipped || stage.percent.trim() === '') return;
|
||
const expected = expectedAmount(stage.percent);
|
||
if (expected != null) stage.amount = formatAmount(expected);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 归档日仍空着预付款日期时,把归档日带入预付款。
|
||
* 核心逻辑:预付款日期依据就是合同归档日;用户已经手改过预付款日期时不覆盖。
|
||
*/
|
||
function onArchiveInput(): void {
|
||
const prepayment = stages.value.find((stage) => stage.nodeType === 'prepayment');
|
||
if (prepayment && !prepayment.skipped && !prepayment.basisDate) prepayment.basisDate = archiveDate.value;
|
||
}
|
||
|
||
/**
|
||
* 切换无此阶段。勾选后比例和金额记 0 并停用输入。
|
||
* @param stage - 被勾选的阶段。
|
||
*/
|
||
function onSkipChange(stage: StageForm): void {
|
||
if (!stage.skipped) return;
|
||
stage.percent = '0';
|
||
stage.amount = '0';
|
||
stage.basisDate = '';
|
||
}
|
||
|
||
const enabledStages = computed(() => stages.value.filter((stage) => !stage.skipped));
|
||
|
||
const rules = computed(() => {
|
||
const total = Number(totalAmount.value);
|
||
const poOk = purchaseOrderNo.value.trim().length > 0 && Number.isFinite(total) && total > 0;
|
||
const percentOk = enabledStages.value.length > 0 && enabledStages.value.every((stage) => {
|
||
const value = Number(stage.percent);
|
||
return stage.percent.trim() !== '' && Number.isFinite(value) && value >= 0;
|
||
}) && Math.abs(enabledStages.value.reduce((sum, stage) => sum + Number(stage.percent), 0) - 100) < 0.001;
|
||
const amountOk = percentOk && enabledStages.value.every((stage) => {
|
||
const expected = expectedAmount(stage.percent);
|
||
const input = Number(stage.amount);
|
||
return expected != null && Number.isFinite(input) && Math.abs(input - expected) < 0.02;
|
||
});
|
||
const dateOk = enabledStages.value
|
||
.filter((stage) => stage.nodeType === 'prepayment' || stage.nodeType === 'shipment')
|
||
.every((stage) => !!stage.basisDate);
|
||
return [
|
||
{ ok: poOk, text: t('admin.contractPaymentReview.ruleIdentity') },
|
||
{ ok: percentOk, text: t('admin.contractPaymentReview.rulePercent') },
|
||
{ ok: amountOk, text: t('admin.contractPaymentReview.ruleAmount') },
|
||
{ ok: dateOk, text: t('admin.contractPaymentReview.ruleDate') },
|
||
];
|
||
});
|
||
|
||
const canSubmit = computed(() => !loading.value && !submitting.value && rules.value.every((rule) => rule.ok));
|
||
|
||
const fillHints = computed(() => [
|
||
t('admin.contractPaymentReview.ruleIdentity'),
|
||
t('admin.contractPaymentReview.rulePercent'),
|
||
t('admin.contractPaymentReview.ruleAmount'),
|
||
t('admin.contractPaymentReview.ruleDate'),
|
||
]);
|
||
|
||
/**
|
||
* 返回阶段输入框下的说明。
|
||
* @param nodeType - 付款阶段。
|
||
* @returns 该阶段日期依据的含义。
|
||
*/
|
||
function stageHint(nodeType: StageType): string {
|
||
const key = {
|
||
prepayment: 'hintPrepayment',
|
||
shipment: 'hintShipment',
|
||
acceptance: 'hintAcceptance',
|
||
warranty: 'hintWarranty',
|
||
}[nodeType];
|
||
return t(`admin.contractPaymentReview.${key}`);
|
||
}
|
||
|
||
/**
|
||
* 提交复核。通过后通知父级切到合同台账。
|
||
* 核心逻辑:请求体带上四个阶段;无此阶段仍传 skipped,由后端写成不预警。
|
||
*/
|
||
async function submitReview(): Promise<void> {
|
||
if (!canSubmit.value || !contract.value) return;
|
||
submitting.value = true;
|
||
try {
|
||
await submitContractPaymentManualReview(contract.value.id, {
|
||
purchase_order_no: purchaseOrderNo.value.trim(),
|
||
contract_no: contractNo.value.trim(),
|
||
total_amount: totalAmount.value.trim(),
|
||
archive_date: archiveDate.value,
|
||
stages: stages.value.map((stage) => ({
|
||
node_type: stage.nodeType,
|
||
skipped: stage.skipped,
|
||
percentage: stage.skipped ? '0' : stage.percent.trim(),
|
||
amount: stage.skipped ? '0' : stage.amount.trim(),
|
||
basis_date: stage.skipped ? '' : stage.basisDate,
|
||
})),
|
||
});
|
||
msg.success(t('admin.contractPaymentReview.confirmSuccess'));
|
||
emit('confirmed');
|
||
} catch (error) {
|
||
msg.error(error instanceof BusinessError ? error.message : t('admin.contractPaymentReview.submitFailed'));
|
||
} finally {
|
||
submitting.value = false;
|
||
}
|
||
}
|
||
|
||
watch(contractId, () => void load());
|
||
onMounted(() => void load());
|
||
</script>
|
||
|
||
<template>
|
||
<Teleport to="body">
|
||
<div class="dialog-overlay z-[160] items-center" @click.self="emit('close')">
|
||
<div class="dialog-panel review-dialog" role="dialog" aria-modal="true" :aria-label="t('admin.contractPaymentReview.manualAction')">
|
||
<div class="dialog-header dialog-header--compact">
|
||
<div class="min-w-0">
|
||
<div class="text-base font-semibold text-slate-900">{{ t('admin.contractPaymentReview.manualAction') }}</div>
|
||
<p v-if="contract" class="mt-1 text-sm text-muted">{{ t('admin.contractPaymentReview.manualSubtitle', { supplier: contract.supplier_name || '—', purchaseOrder: contract.purchase_order_no || '—' }) }}</p>
|
||
</div>
|
||
<button type="button" class="btn-secondary shrink-0" @click="emit('close')">{{ t('common.close') }}</button>
|
||
</div>
|
||
<div class="dialog-body">
|
||
<p v-if="loading && !contract" class="review-empty">{{ t('admin.contractPayment.detailLoading') }}</p>
|
||
<template v-else-if="contract">
|
||
<div class="review-cards">
|
||
<label>
|
||
<span>{{ t('admin.contractPayment.amount') }}</span>
|
||
<input v-model="totalAmount" type="number" min="0" step="0.01" @input="onTotalInput" />
|
||
</label>
|
||
<label>
|
||
<span>{{ t('admin.contractPayment.purchaseOrder') }}</span>
|
||
<input v-model="purchaseOrderNo" type="text" />
|
||
</label>
|
||
<label>
|
||
<span>{{ t('admin.contractPaymentReview.contractNoOptional') }}</span>
|
||
<input v-model="contractNo" type="text" :placeholder="t('admin.contractPaymentReview.contractNoPlaceholder')" />
|
||
</label>
|
||
<label>
|
||
<span>{{ t('admin.contractPayment.archiveDate') }}</span>
|
||
<input v-model="archiveDate" type="date" @input="onArchiveInput" />
|
||
</label>
|
||
</div>
|
||
|
||
<div class="review-stage">
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>{{ t('admin.contractPayment.detailPaymentPlan') }}</th>
|
||
<th>{{ t('admin.contractPaymentReview.percentage') }}</th>
|
||
<th>{{ t('admin.contractPaymentReview.stageAmount') }}</th>
|
||
<th>{{ t('admin.contractPaymentReview.basisDate') }}</th>
|
||
<th>{{ t('admin.contractPaymentReview.noStage') }}</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr v-for="stage in stages" :key="stage.nodeType">
|
||
<td>
|
||
<strong>{{ t(`admin.contractPayment.nodeType.${stage.nodeType}`) }}</strong>
|
||
<span>{{ stageHint(stage.nodeType) }}</span>
|
||
</td>
|
||
<td><input v-model="stage.percent" type="number" min="0" max="100" step="0.01" :disabled="stage.skipped" @input="onPercentInput(stage)" /></td>
|
||
<td><input v-model="stage.amount" type="number" min="0" step="0.01" :disabled="stage.skipped" /></td>
|
||
<td><input v-model="stage.basisDate" type="date" :disabled="stage.skipped" /></td>
|
||
<td>
|
||
<label class="review-check">
|
||
<input v-model="stage.skipped" type="checkbox" @change="onSkipChange(stage)" />
|
||
{{ t('admin.contractPaymentReview.noStage') }}
|
||
</label>
|
||
</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<div class="review-hint">
|
||
<strong>{{ t('admin.contractPaymentReview.fillHintTitle') }}</strong>
|
||
<p v-for="hint in fillHints" :key="hint">{{ hint }}</p>
|
||
</div>
|
||
</template>
|
||
</div>
|
||
<div class="dialog-footer">
|
||
<button type="button" class="btn-secondary" @click="emit('close')">{{ t('common.cancel') }}</button>
|
||
<button type="button" class="btn-primary" :disabled="!canSubmit" @click="submitReview">
|
||
{{ t('admin.contractPaymentReview.confirmLedger') }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</Teleport>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.review-dialog { width: min(1080px, 100%); max-height: min(85vh, 900px); }
|
||
.review-empty { padding: 28px 0; color: #58749f; }
|
||
.review-cards { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin-bottom: 12px; }
|
||
.review-cards label, .review-stage { border: 1px solid #d7e1ef; border-radius: 12px; background: #fff; }
|
||
.review-cards label { display: block; padding: 12px; background: #f8fbff; }
|
||
.review-cards span, .review-stage th { color: #58749f; font-size: 12px; }
|
||
.review-cards input, .review-stage input[type="number"], .review-stage input[type="text"], .review-stage input[type="date"] {
|
||
width: 100%; height: 32px; margin-top: 6px; border: 1px solid #d7e1ef; border-radius: 8px; padding: 0 8px; background: #fff; color: #102d59; font-weight: 650;
|
||
}
|
||
.review-cards input { border: 0; background: transparent; padding: 0; font-size: 15px; }
|
||
.review-cards input:focus { background: #fff; }
|
||
.review-stage { overflow: auto; }
|
||
.review-stage table { width: 100%; border-collapse: collapse; }
|
||
.review-stage th, .review-stage td { padding: 12px 14px; text-align: left; border-bottom: 1px solid #e4ecf6; vertical-align: top; }
|
||
.review-stage th { background: #f7faff; }
|
||
.review-stage td span { display: block; margin-top: 4px; color: #58749f; font-size: 12px; }
|
||
.review-stage input:disabled { background: #f3f6fb; color: #8aa0bd; }
|
||
.review-check { display: flex; align-items: center; gap: 6px; font-size: 13px; white-space: nowrap; }
|
||
.review-hint { margin-top: 12px; padding: 12px 14px; border-radius: 10px; background: #f7faff; color: #58749f; }
|
||
.review-hint strong { display: block; margin-bottom: 4px; color: #426493; font-size: 13px; }
|
||
.review-hint p { margin: 0; font-size: 13px; line-height: 1.6; }
|
||
@media (max-width: 900px) {
|
||
.review-cards { grid-template-columns: 1fr 1fr; }
|
||
}
|
||
</style>
|