From 534533425b15b183cea60fa162d9884e5d3c6243 Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期二, 13 十月 2026 17:55:50 +0800
Subject: [PATCH] Merge branch 'dev_海西州_北京同仁堂' into dev_海西州_惠牧

---
 src/views/erp/finance/voucher/modules/entry-table.vue |  361 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 361 insertions(+), 0 deletions(-)

diff --git a/src/views/erp/finance/voucher/modules/entry-table.vue b/src/views/erp/finance/voucher/modules/entry-table.vue
new file mode 100644
index 0000000..6ec1c95
--- /dev/null
+++ b/src/views/erp/finance/voucher/modules/entry-table.vue
@@ -0,0 +1,361 @@
+<script lang="ts" setup>
+import type { FinVoucherApi } from '#/api/erp/finance/voucher';
+
+import { computed, nextTick, ref, watch } from 'vue';
+
+import { DICT_TYPE } from '@vben/constants';
+import { getDictOptions } from '@vben/hooks';
+import { erpPriceInputFormatter } from '@vben/utils';
+
+import { Input, InputNumber, Select } from 'ant-design-vue';
+
+import { TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
+
+import { useEntryColumns } from '../data';
+import AccountSelect from './account-select.vue';
+
+interface EntryRow extends FinVoucherApi.VoucherEntry {
+  seq: number; // 鍓嶇琛屾爣璇嗭紝鐢ㄤ簬琛ㄦ牸瀹氫綅
+}
+
+interface Props {
+  entries?: FinVoucherApi.VoucherEntry[];
+  disabled?: boolean;
+}
+
+const props = withDefaults(defineProps<Props>(), {
+  entries: () => [],
+  disabled: false,
+});
+
+const emit = defineEmits(['update:entries']);
+
+/** 鍒嗗綍琛岋紝seq 鐢ㄤ簬琛ㄦ牸琛屽畾浣嶏紙鏂板鍑瘉鏃跺垎褰曡繕娌℃湁 id锛� */
+const tableData = ref<EntryRow[]>([]);
+let seq = 0;
+
+/** 鎶婂悗绔垎褰曡浆涓哄甫 seq 鐨勮〃鏍艰 */
+function toRows(entries: FinVoucherApi.VoucherEntry[]): EntryRow[] {
+  return entries.map((entry) => ({ ...entry, seq: ++seq }));
+}
+
+/** 鍚堣锛氬�熸柟銆佽捶鏂广�佸樊棰� */
+const summaries = computed(() => {
+  const debit = tableData.value.reduce(
+    (sum, item) => sum + (item.debit || 0),
+    0,
+  );
+  const credit = tableData.value.reduce(
+    (sum, item) => sum + (item.credit || 0),
+    0,
+  );
+  return { debit, credit, diff: debit - credit };
+});
+
+/** 鏄惁鍊熻捶骞宠  */
+const isBalanced = computed(
+  () => summaries.value.debit > 0 && summaries.value.diff === 0,
+);
+
+/** 琛ㄦ牸閰嶇疆 */
+const [Grid, gridApi] = useVbenVxeGrid({
+  gridOptions: {
+    columns: useEntryColumns(props.disabled),
+    data: tableData.value,
+    minHeight: 300,
+    autoResize: true,
+    border: true,
+    rowConfig: {
+      keyField: 'seq',
+      isHover: true,
+    },
+    pagerConfig: {
+      enabled: false,
+    },
+    toolbarConfig: {
+      enabled: false,
+    },
+  },
+});
+
+/** 鐩戝惉澶栭儴浼犲叆鐨勫垎褰� */
+watch(
+  () => props.entries,
+  async (entries) => {
+    if (!entries) {
+      return;
+    }
+    tableData.value = toRows(entries);
+    await nextTick(); // 鐗规畩锛氫繚璇� gridApi 宸茬粡鍒濆鍖�
+    await gridApi.grid.reloadData(tableData.value);
+  },
+  { immediate: true },
+);
+
+/** 閫氱煡鐖剁粍浠跺垎褰曞凡鍙樻洿 */
+function emitChange() {
+  const entries = tableData.value.map(({ seq: _seq, ...entry }) => entry);
+  emit('update:entries', entries);
+}
+
+/** 淇敼鏌愯鏁版嵁 */
+function handleRowChange(row: EntryRow) {
+  const index = tableData.value.findIndex((item) => item.seq === row.seq);
+  if (index === -1) {
+    tableData.value.push(row);
+  } else {
+    tableData.value[index] = row;
+  }
+  emitChange();
+}
+
+/** 褰曞叆鍊熸柟閲戦鏃舵竻绌鸿捶鏂归噾棰濓紝鍙嶄箣浜︾劧锛堝悓涓�琛屽彧鑳芥湁涓�鏂规湁閲戦锛� */
+function handleDebitChange(row: EntryRow) {
+  if (row.debit && row.debit > 0) {
+    row.credit = 0;
+  }
+  handleRowChange(row);
+}
+
+function handleCreditChange(row: EntryRow) {
+  if (row.credit && row.credit > 0) {
+    row.debit = 0;
+  }
+  handleRowChange(row);
+}
+
+/** 閫夋嫨浼氳绉戠洰 */
+const accountSelectRef = ref();
+const editingRow = ref<EntryRow>();
+
+function handleOpenAccount(row: EntryRow) {
+  if (props.disabled) {
+    return;
+  }
+  editingRow.value = row;
+  accountSelectRef.value?.open();
+}
+
+function handleSelectAccount(subject: {
+  subjectCode: string;
+  subjectName: string;
+}) {
+  const row = editingRow.value;
+  if (!row) {
+    return;
+  }
+  row.subjectCode = subject.subjectCode;
+  row.subjectName = subject.subjectName;
+  handleRowChange(row);
+}
+
+/** 娣诲姞涓�琛岀┖鐧藉垎褰� */
+function handleAdd() {
+  const row: EntryRow = {
+    seq: ++seq,
+    subjectCode: undefined,
+    subjectName: undefined,
+    summary: '',
+    debit: 0,
+    credit: 0,
+    auxiliaryType: undefined,
+    auxiliaryId: '',
+    auxiliaryName: '',
+  };
+  tableData.value.push(row);
+  gridApi.grid.reloadData(tableData.value);
+  emitChange();
+}
+
+/** 鍒犻櫎涓�琛屽垎褰� */
+function handleDelete(row: EntryRow) {
+  const index = tableData.value.findIndex((item) => item.seq === row.seq);
+  if (index !== -1) {
+    tableData.value.splice(index, 1);
+  }
+  gridApi.grid.reloadData(tableData.value);
+  emitChange();
+}
+
+/** 琛ㄥ崟鏍¢獙锛氫笌鍚庣 FinVoucherServiceImpl 鐨勬牎楠屽彛寰勪繚鎸佷竴鑷� */
+function validate() {
+  const rows = tableData.value.filter(
+    (item) => item.subjectCode || item.debit || item.credit,
+  );
+  if (rows.length === 0) {
+    throw new Error('璇疯嚦灏戝綍鍏ヤ竴鏉″嚟璇佸垎褰�');
+  }
+  for (let i = 0; i < rows.length; i++) {
+    const item = rows[i]!;
+    if (!item.subjectCode) {
+      throw new Error(`绗� ${i + 1} 琛岋細宸插綍鍏ラ噾棰濅絾鏈�夋嫨浼氳绉戠洰`);
+    }
+    const debit = item.debit || 0;
+    const credit = item.credit || 0;
+    if (debit <= 0 && credit <= 0) {
+      throw new Error(`绗� ${i + 1} 琛岋細宸查�夋嫨浼氳绉戠洰浣嗘湭褰曞叆閲戦`);
+    }
+    if (debit > 0 && credit > 0) {
+      throw new Error(`绗� ${i + 1} 琛岋細鍊熸柟涓庤捶鏂归噾棰濅笉鑳藉悓鏃跺ぇ浜� 0`);
+    }
+  }
+  if (summaries.value.debit <= 0) {
+    throw new Error('鍑瘉鍊熻捶鍚堣蹇呴』澶т簬 0');
+  }
+  if (summaries.value.diff !== 0) {
+    throw new Error(
+      `鍑瘉鍊熻捶涓嶅钩琛★細鍊熸柟鍚堣 ${erpPriceInputFormatter(summaries.value.debit)}锛宍 +
+        `璐锋柟鍚堣 ${erpPriceInputFormatter(summaries.value.credit)}锛宍 +
+        `宸 ${erpPriceInputFormatter(summaries.value.diff)}`,
+    );
+  }
+}
+
+defineExpose({ validate });
+</script>
+
+<template>
+  <div>
+    <Grid class="w-full">
+      <template #summary="{ row }">
+        <Input
+          v-model:value="row.summary"
+          :disabled="disabled"
+          placeholder="璇疯緭鍏ユ憳瑕�"
+          @change="handleRowChange(row)"
+        />
+      </template>
+
+      <template #subject="{ row }">
+        <div
+          class="flex cursor-pointer items-center"
+          :class="{ 'text-primary': !disabled }"
+          @click="handleOpenAccount(row)"
+        >
+          <span v-if="row.subjectCode">
+            {{ row.subjectCode }} {{ row.subjectName }}
+          </span>
+          <span v-else class="text-muted-foreground">璇烽�夋嫨浼氳绉戠洰</span>
+        </div>
+      </template>
+
+      <template #debit="{ row }">
+        <InputNumber
+          v-model:value="row.debit"
+          :precision="2"
+          :min="0"
+          :disabled="disabled"
+          class="w-full"
+          placeholder="鍊熸柟閲戦"
+          @change="handleDebitChange(row)"
+        />
+      </template>
+
+      <template #credit="{ row }">
+        <InputNumber
+          v-model:value="row.credit"
+          :precision="2"
+          :min="0"
+          :disabled="disabled"
+          class="w-full"
+          placeholder="璐锋柟閲戦"
+          @change="handleCreditChange(row)"
+        />
+      </template>
+
+      <template #auxiliaryType="{ row }">
+        <Select
+          v-model:value="row.auxiliaryType"
+          :disabled="disabled"
+          allow-clear
+          class="w-full"
+          placeholder="鏃�"
+          @change="handleRowChange(row)"
+        >
+          <Select.Option
+            v-for="dict in getDictOptions(
+              DICT_TYPE.FINANCE_AUXILIARY_TYPE,
+              'number',
+            )"
+            :key="dict.value"
+            :value="dict.value"
+          >
+            {{ dict.label }}
+          </Select.Option>
+        </Select>
+      </template>
+
+      <template #auxiliaryName="{ row }">
+        <Input
+          v-model:value="row.auxiliaryName"
+          :disabled="disabled"
+          placeholder="璇疯緭鍏ヨ緟鍔╂牳绠楀璞�"
+          @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">
+          <div class="flex justify-between text-sm">
+            <span class="font-medium text-foreground">鍚堣锛�</span>
+            <div class="flex space-x-4">
+              <span>
+                鍊熸柟鍚堣锛歿{ erpPriceInputFormatter(summaries.debit) }}
+              </span>
+              <span>
+                璐锋柟鍚堣锛歿{ erpPriceInputFormatter(summaries.credit) }}
+              </span>
+              <span :class="isBalanced ? 'text-green-600' : 'text-red-500'">
+                宸锛歿{ erpPriceInputFormatter(summaries.diff) }}
+                {{ isBalanced ? '锛堝�熻捶骞宠 锛�' : '锛堝�熻捶涓嶅钩琛★級' }}
+              </span>
+            </div>
+          </div>
+        </div>
+        <TableAction
+          v-if="!disabled"
+          class="mt-2 flex justify-center"
+          :actions="[
+            {
+              label: '娣诲姞鍒嗗綍',
+              type: 'default',
+              onClick: handleAdd,
+            },
+          ]"
+        />
+      </template>
+    </Grid>
+
+    <!-- 浼氳绉戠洰閫夋嫨 -->
+    <AccountSelect ref="accountSelectRef" @selected="handleSelectAccount" />
+  </div>
+</template>
+
+<style scoped>
+/*
+ * 鍊熸柟/璐锋柟閲戦鍒楀眳涓��
+ * vxe 鍒楃殑 align: 'center' 鍙綔鐢ㄤ簬鍗曞厓鏍硷紝鑰� ant 鐨� InputNumber 鍐呴儴 <input>
+ * 锛�.ant-input-number-input锛夎嚜甯� text-align: start锛屼細鎶婃暟瀛楀帇鍥炲乏杈癸紝瀵艰嚧琛ㄥご灞呬腑銆�
+ * 鏁板瓧闈犲乏鐨勯敊浣嶏紝鎵�浠ヨ繖閲岃繛杈撳叆妗嗗唴鐨勬枃瀛椾竴璧疯鐩栥��
+ * 鏈粍浠跺彧鏈夎繖涓や釜 InputNumber锛堝�熸柟銆佽捶鏂癸級锛屼笉鍙楀奖鍝嶅叾瀹冨垪銆�
+ */
+:deep(.ant-input-number .ant-input-number-input) {
+  text-align: center;
+}
+</style>

--
Gitblit v1.9.3