2026-08-10 76c9a49c3ded285857f0c252def2552469b85825
src/views/erp/finance/payment/modules/item-form.vue
@@ -1,26 +1,30 @@
<script lang="ts" setup>
import type { ErpFinancePaymentApi } from '#/api/erp/finance/payment';
import type { ErpPurchaseInApi } from '#/api/erp/purchase/in';
import type { ErpPurchaseReturnApi } from '#/api/erp/purchase/return';
import { computed, nextTick, ref, watch } from 'vue';
import { ErpBizType } from '@vben/constants';
import { erpPriceInputFormatter } from '@vben/utils';
import { Input, InputNumber, message } from 'ant-design-vue';
import { Input, InputNumber } from 'ant-design-vue';
import { TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { useFormItemColumns } from '../data';
import PurchaseInSelect from './purchase-in-select.vue';
import SaleReturnSelect from './sale-return-select.vue';
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>(), {
@@ -28,6 +32,7 @@
  supplierId: undefined,
  disabled: false,
  discountPrice: 0,
  invoice: undefined,
});
const emit = defineEmits([
@@ -37,6 +42,9 @@
]);
const tableData = ref<ErpFinancePaymentApi.FinancePaymentItem[]>([]); // 表格数据
/** 已根据该来票生成的明细,避免重复生成 */
const generatedInvoiceId = ref<number | undefined>();
/** 获取表格合计数据 */
const summaries = computed(() => {
@@ -59,7 +67,7 @@
/** 表格配置 */
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: useFormItemColumns(props.disabled),
    columns: useFormItemColumns(),
    data: tableData.value,
    minHeight: 250,
    autoResize: true,
@@ -85,11 +93,44 @@
      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]);
  },
);
@@ -116,74 +157,6 @@
  { deep: true },
);
/** 添加采购入库单 */
const purchaseInSelectRef = ref();
const handleOpenPurchaseIn = () => {
  if (!props.supplierId) {
    message.error('请选择供应商');
    return;
  }
  purchaseInSelectRef.value?.open(props.supplierId);
};
const handleAddPurchaseIn = (rows: ErpPurchaseInApi.PurchaseIn[]) => {
  rows.forEach((row) => {
    const totalPrice = row.totalPrice ?? 0;
    const paidPrice = row.paymentPrice ?? 0;
    const newItem: ErpFinancePaymentApi.FinancePaymentItem = {
      bizId: row.id ?? 0,
      bizType: ErpBizType.PURCHASE_IN,
      bizNo: row.no ?? '',
      totalPrice,
      paidPrice,
      paymentPrice: totalPrice - paidPrice,
      remark: undefined,
    };
    tableData.value.push(newItem);
  });
  emit('update:items', [...tableData.value]);
};
/** 添加采购退货单 */
const saleReturnSelectRef = ref();
const handleOpenSaleReturn = () => {
  if (!props.supplierId) {
    message.error('请选择供应商');
    return;
  }
  saleReturnSelectRef.value?.open(props.supplierId);
};
const handleAddSaleReturn = (rows: ErpPurchaseReturnApi.PurchaseReturn[]) => {
  rows.forEach((row) => {
    const totalPrice = row.totalPrice ?? 0;
    const refundPrice = row.refundPrice ?? 0;
    const newItem: ErpFinancePaymentApi.FinancePaymentItem = {
      bizId: row.id ?? 0,
      bizType: ErpBizType.PURCHASE_RETURN,
      bizNo: row.no ?? '',
      totalPrice: -totalPrice,
      paidPrice: -refundPrice,
      paymentPrice: -totalPrice + refundPrice,
      remark: undefined,
    };
    tableData.value.push(newItem);
  });
  emit('update:items', [...tableData.value]);
};
/** 删除行 */
const handleDelete = async (row: any) => {
  const index = tableData.value.findIndex(
    (item) => item.bizId === row.bizId && item.bizType === row.bizType,
  );
  if (index !== -1) {
    tableData.value.splice(index, 1);
  }
  // 通知父组件更新
  emit('update:items', [...tableData.value]);
};
/** 处理行数据变更 */
const handleRowChange = (row: any) => {
  const index = tableData.value.findIndex(
@@ -201,7 +174,7 @@
const validate = () => {
  // 检查是否有明细
  if (tableData.value.length === 0) {
    throw new Error('请添加付款明细');
    throw new Error('请先选择关联来票');
  }
  // 检查每行的付款金额
  for (let i = 0; i < tableData.value.length; i++) {
@@ -236,21 +209,6 @@
          @change="handleRowChange(row)"
        />
      </template>
      <template #actions="{ row }">
        <TableAction
          :actions="[
            {
              label: '删除',
              type: 'link',
              danger: true,
              popConfirm: {
                title: '确认删除该付款明细吗?',
                confirm: handleDelete.bind(null, row),
              },
            },
          ]"
        />
      </template>
      <template #bottom>
        <div class="mt-2 rounded border border-border bg-muted p-2">
@@ -258,7 +216,7 @@
            <span class="font-medium text-foreground">合计:</span>
            <div class="flex space-x-4">
              <span>
                合计付款:{{ erpPriceInputFormatter(summaries.totalPrice) }}
                应付金额:{{ erpPriceInputFormatter(summaries.totalPrice) }}
              </span>
              <span>
                已付金额:{{ erpPriceInputFormatter(summaries.paidPrice) }}
@@ -270,34 +228,13 @@
            </div>
          </div>
        </div>
        <TableAction
          v-if="!disabled"
          class="mt-2 flex justify-center"
          :actions="[
            {
              label: '添加采购入库单',
              type: 'default',
              onClick: handleOpenPurchaseIn,
            },
            {
              label: '添加采购退货单',
              type: 'default',
              onClick: handleOpenSaleReturn,
            },
          ]"
        />
        <div
          v-if="!invoice && !disabled"
          class="mt-2 flex justify-center text-sm text-muted-foreground"
        >
          请先在上方表单选择关联来票,系统将自动生成付款明细
        </div>
      </template>
    </Grid>
    <!-- 采购入库单选择组件 -->
    <PurchaseInSelect
      ref="purchaseInSelectRef"
      @success="handleAddPurchaseIn"
    />
    <!-- 采购退货单选择组件 -->
    <SaleReturnSelect
      ref="saleReturnSelectRef"
      @success="handleAddSaleReturn"
    />
  </div>
</template>