<script lang="ts" setup>
|
import type { ErpFinancePaymentApi } from '#/api/erp/finance/payment';
|
|
import { computed, nextTick, ref, watch } from 'vue';
|
|
import { ErpBizType } from '@vben/constants';
|
import { erpPriceInputFormatter } from '@vben/utils';
|
|
import { Input, InputNumber } from 'ant-design-vue';
|
|
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
|
import { useFormItemColumns } from '../data';
|
|
interface InvoiceForItem {
|
id?: number;
|
no?: string;
|
price?: number;
|
remainingInvoicePrice?: number;
|
}
|
|
interface Props {
|
items?: ErpFinancePaymentApi.FinancePaymentItem[];
|
supplierId?: number;
|
disabled?: boolean;
|
discountPrice?: number;
|
invoice?: InvoiceForItem;
|
}
|
|
const props = withDefaults(defineProps<Props>(), {
|
items: () => [],
|
supplierId: undefined,
|
disabled: false,
|
discountPrice: 0,
|
invoice: undefined,
|
});
|
|
const emit = defineEmits([
|
'update:items',
|
'update:totalPrice',
|
'update:paymentPrice',
|
]);
|
|
/** 输入时不补零,失焦后仍按金额格式保留两位小数 */
|
const paymentPriceFormatter = (
|
value: number | string | undefined,
|
info: { input: string; userTyping: boolean },
|
) => {
|
return info.userTyping ? info.input : erpPriceInputFormatter(value);
|
};
|
|
const tableData = ref<ErpFinancePaymentApi.FinancePaymentItem[]>([]); // 表格数据
|
|
/** 已根据该来票生成的明细,避免重复生成 */
|
const generatedInvoiceId = ref<number | undefined>();
|
|
/** 获取表格合计数据 */
|
const summaries = computed(() => {
|
return {
|
totalPrice: tableData.value.reduce(
|
(sum, item) => sum + (item.totalPrice || 0),
|
0,
|
),
|
paidPrice: tableData.value.reduce(
|
(sum, item) => sum + (item.paidPrice || 0),
|
0,
|
),
|
paymentPrice: tableData.value.reduce(
|
(sum, item) => sum + (item.paymentPrice || 0),
|
0,
|
),
|
};
|
});
|
|
/** 表格配置 */
|
const [Grid, gridApi] = useVbenVxeGrid({
|
gridOptions: {
|
columns: useFormItemColumns(),
|
data: tableData.value,
|
minHeight: 250,
|
autoResize: true,
|
border: true,
|
rowConfig: {
|
keyField: 'seq',
|
isHover: true,
|
},
|
pagerConfig: {
|
enabled: false,
|
},
|
toolbarConfig: {
|
enabled: false,
|
},
|
},
|
});
|
|
/** 监听外部传入的列数据 */
|
watch(
|
() => props.items,
|
async (items) => {
|
if (!items) {
|
return;
|
}
|
tableData.value = [...items];
|
// 编辑回显时,标记已根据当前来票生成过,避免重复生成
|
generatedInvoiceId.value = props.invoice?.id;
|
await nextTick(); // 特殊:保证 gridApi 已经初始化
|
await gridApi.grid.reloadData(tableData.value);
|
},
|
{
|
immediate: true,
|
},
|
);
|
|
/** 关联来票变化时,自动生成唯一的付款明细(付款基于来票) */
|
watch(
|
() => props.invoice?.id,
|
async (newId) => {
|
if (!newId || !props.invoice) {
|
return;
|
}
|
if (generatedInvoiceId.value === newId) {
|
return;
|
}
|
generatedInvoiceId.value = newId;
|
const invoice = props.invoice;
|
const price = invoice.price ?? 0;
|
const remaining = invoice.remainingInvoicePrice ?? price;
|
tableData.value = [
|
{
|
bizId: invoice.id ?? 0,
|
bizType: ErpBizType.PURCHASE_INVOICE,
|
bizNo: invoice.no ?? '',
|
totalPrice: price,
|
paidPrice: price - remaining,
|
paymentPrice: remaining,
|
remark: '',
|
},
|
];
|
await nextTick();
|
await gridApi.grid.reloadData(tableData.value);
|
emit('update:items', [...tableData.value]);
|
},
|
);
|
|
/** 计算 totalPrice、paymentPrice 价格 */
|
watch(
|
() => [tableData.value, props.discountPrice],
|
() => {
|
if (!tableData.value || tableData.value.length === 0) {
|
return;
|
}
|
const totalPrice = tableData.value.reduce(
|
(prev, curr) => prev + (curr.totalPrice || 0),
|
0,
|
);
|
const paymentPrice = tableData.value.reduce(
|
(prev, curr) => prev + (curr.paymentPrice || 0),
|
0,
|
);
|
const finalPaymentPrice = paymentPrice - (props.discountPrice || 0);
|
// 通知父组件更新
|
emit('update:totalPrice', totalPrice);
|
emit('update:paymentPrice', finalPaymentPrice);
|
},
|
{ deep: true },
|
);
|
|
/** 处理行数据变更 */
|
const handleRowChange = (row: any) => {
|
const index = tableData.value.findIndex(
|
(item) => item.bizId === row.bizId && item.bizType === row.bizType,
|
);
|
if (index === -1) {
|
tableData.value.push(row);
|
} else {
|
tableData.value[index] = row;
|
}
|
emit('update:items', [...tableData.value]);
|
};
|
|
/** 表单校验 */
|
const validate = () => {
|
// 检查是否有明细
|
if (tableData.value.length === 0) {
|
throw new Error('请先选择关联来票');
|
}
|
// 检查每行的付款金额
|
for (let i = 0; i < tableData.value.length; i++) {
|
const item = tableData.value[i];
|
if (!item?.paymentPrice || item.paymentPrice <= 0) {
|
throw new Error(`第 ${i + 1} 行:本次付款必须大于0`);
|
}
|
}
|
};
|
|
defineExpose({ validate });
|
</script>
|
|
<template>
|
<div>
|
<Grid class="w-full">
|
<template #paymentPrice="{ row }">
|
<InputNumber
|
v-model:value="row.paymentPrice"
|
:precision="2"
|
:disabled="disabled"
|
:formatter="paymentPriceFormatter"
|
placeholder="请输入本次付款"
|
@change="handleRowChange(row)"
|
/>
|
</template>
|
<template #remark="{ row }">
|
<Input
|
v-model:value="row.remark"
|
:disabled="disabled"
|
placeholder="请输入备注"
|
@change="handleRowChange(row)"
|
/>
|
</template>
|
|
<template #bottom>
|
<div class="mt-2 rounded border border-border bg-muted p-2">
|
<div class="flex justify-between text-sm text-muted-foreground">
|
<span class="font-medium text-foreground">合计:</span>
|
<div class="flex space-x-4">
|
<span>
|
应付金额:{{ erpPriceInputFormatter(summaries.totalPrice) }}
|
</span>
|
<span>
|
已付金额:{{ erpPriceInputFormatter(summaries.paidPrice) }}
|
</span>
|
<span>
|
本次付款:
|
{{ erpPriceInputFormatter(summaries.paymentPrice) }}
|
</span>
|
</div>
|
</div>
|
</div>
|
<div
|
v-if="!invoice && !disabled"
|
class="mt-2 flex justify-center text-sm text-muted-foreground"
|
>
|
请先在上方表单选择关联来票,系统将自动生成付款明细
|
</div>
|
</template>
|
</Grid>
|
</div>
|
</template>
|