Files
financial_system_frontend/src/components/ReimbursementForm.vue
T
2026-09-11 13:56:36 +08:00

51 lines
13 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div>
<div class="page-head"><div><span class="eyebrow">{{ id ? 'EDIT DRAFT' : 'NEW REIMBURSEMENT' }}</span><h1>{{ id && existing?.approvalStatus === '退回' ? '补充退回报销' : id ? '编辑报销草稿' : '发起员工报销' }}</h1><p>{{ id ? '沿用原报销单号,补充材料后重新提交。' : '填写个人垫付费用,提交前检查发票、附件与金额。' }}</p></div><router-link class="btn" to="/applications">取消</router-link></div>
<div v-if="loadError" class="inline-alert danger" role="alert"><b>!</b><span>{{ loadError }}</span></div>
<div class="step-rail"><div class="step done"><span>1</span><strong>填写报销</strong></div><div class="step active"><span>2</span><strong>材料校验</strong></div><div class="step"><span>3</span><strong>飞书审批</strong></div><div class="step"><span>4</span><strong>财务付款</strong></div></div>
<form v-if="!loading" class="form-page" @submit.prevent="save(true)">
<section class="section-card"><div class="section-title"><h2>1. 报销信息</h2><span>员工报销 · * 为必填项</span></div><div class="form-grid">
<div class="field"><label for="title">报销事项 <em>*</em></label><input id="title" v-model.trim="form.title" required maxlength="100" placeholder="例如:上海客户现场技术交流差旅"></div>
<div class="field"><label for="category">费用类型 <em>*</em></label><select id="category" v-model="form.category" required><option v-for="type in expenseTypes" :key="type.id" :value="type.label">{{ type.label }}</option></select></div>
<div v-if="showField('application:amount')" class="field"><label for="amount">报销金额(元) <em v-if="editableField('application:amount')">*</em></label><input id="amount" v-model.number="form.totalAmount" type="number" min="0.01" step="0.01" :readonly="readonlyField('application:amount')" :required="editableField('application:amount')" :aria-describedby="readonlyField('application:amount') ? 'amount-permission-help' : undefined"><small v-if="readonlyField('application:amount')" id="amount-permission-help" class="form-help">当前字段只读</small></div>
<div class="field"><label>报销人 / 部门</label><input readonly :value="`${userName} · ${department}`"></div>
<div v-if="showField('application:payeeName')" class="field"><label for="payee">收款人 <em v-if="editableField('application:payeeName')">*</em></label><input id="payee" v-model.trim="form.payeeName" :readonly="readonlyField('application:payeeName')" maxlength="100" :required="editableField('application:payeeName')"><small v-if="readonlyField('application:payeeName')" class="form-help">当前字段只读</small></div>
<div v-if="showField('application:bankAccount')" class="field"><label for="bank">收款账户 ID <em v-if="editableField('application:bankAccount')">*</em></label><input id="bank" v-model.number="form.payeeBankId" type="number" min="1" step="1" :readonly="readonlyField('application:bankAccount')" :required="editableField('application:bankAccount')"><small class="form-help">{{ readonlyField('application:bankAccount') ? '当前字段只读;账户信息不会明文展示。' : '后端当前按账户 ID 关联,详情页只显示脱敏账号。' }}</small></div>
<div class="field full"><label for="description">费用说明 <em>*</em></label><textarea id="description" v-model.trim="form.description" required maxlength="500" rows="3"></textarea></div>
</div></section>
<section class="section-card section-gap"><div class="section-title"><h2>2. 费用明细</h2><button class="btn small" type="button" @click="addItem">+ 添加明细</button></div><div class="expense-items"><div v-for="(item, index) in form.items" :key="item.key" class="expense-item"><div class="field"><label :for="`expense-date-${item.key}`">日期 <em>*</em></label><input :id="`expense-date-${item.key}`" v-model="item.expenseDate" type="date" required></div><div class="field"><label :for="`expense-category-${item.key}`">类别 <em>*</em></label><input :id="`expense-category-${item.key}`" v-model.trim="item.category" required maxlength="50"></div><div class="field"><label :for="`expense-amount-${item.key}`">金额 <em>*</em></label><input :id="`expense-amount-${item.key}`" v-model.number="item.amount" type="number" min="0.01" step="0.01" required></div><div class="field"><label :for="`expense-description-${item.key}`">说明 <em>*</em></label><input :id="`expense-description-${item.key}`" v-model.trim="item.description" required maxlength="200"></div><button v-if="form.items.length > 1" class="remove-item" type="button" :aria-label="`删除第 ${index + 1} 条费用明细`" @click="removeItem(index)">删除</button></div></div><div class="amount-summary" :class="{ 'amount-mismatch': form.totalAmount > 0 && !amountMatches }"><span>明细合计</span><strong>{{ money(itemsTotal) }}</strong><span>申请金额 {{ money(form.totalAmount) }}</span></div></section>
<section class="section-card section-gap"><div class="section-title"><h2>3. 发票与附件</h2><span>至少一份材料后可提交</span></div><div class="form-grid"><div class="field"><label for="invoice-no">发票号码</label><input id="invoice-no" v-model.trim="invoiceNumber" maxlength="80"></div><div class="field"><label for="invoice-amount">发票金额</label><input id="invoice-amount" v-model.number="invoiceAmount" type="number" min="0" step="0.01"></div><div class="field"><label for="invoice-file">发票影像</label><input id="invoice-file" type="file" accept="image/*,.pdf" @change="selectFile($event, 'invoice')"><small class="form-help">{{ invoiceFile || '尚未选择文件' }}</small></div><div class="field"><label for="attachment-file">其他附件</label><input id="attachment-file" type="file" multiple @change="selectFile($event, 'attachment')"><small class="form-help">{{ attachmentFiles || '尚未选择文件' }}</small></div></div></section>
<div class="form-actions"><button class="btn" type="button" :disabled="saving" @click="save(false)">保存草稿</button><button class="btn primary" type="submit" :disabled="saving" :aria-busy="saving">{{ saving ? '保存中…' : '提交报销' }}</button></div>
</form><div v-else class="card load-wrap" role="status"><span class="loader" aria-hidden="true"></span><span>正在读取报销信息…</span></div>
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import type { CreateApplicationRequest, ExpenseItem } from '@/api/reimbursement'
import { useUserStore } from '@/store/user'
import { useWorkbenchStore } from '@/store/workbench'
type FieldLevel = 'HIDDEN' | 'READ' | 'EDIT'
interface FormItem extends ExpenseItem { key: number; description: string }
const route = useRoute(); const router = useRouter(); const userStore = useUserStore(); const store = useWorkbenchStore(); const id = computed(() => route.params.id ? String(route.params.id) : ''); const existing = computed(() => id.value ? store.getById(id.value) : null); const loading = ref(true); const saving = ref(false); const loadError = ref(''); const invoiceNumber = ref(''); const invoiceAmount = ref(0); const invoiceFile = ref(''); const attachmentFiles = ref(''); let nextKey = 1
const userName = computed(() => userStore.userInfo?.realName || store.user?.realName || '用户'); const department = computed(() => userStore.userInfo?.departmentName || store.user?.departmentName || '销售中心'); const expenseTypes = [{ id: 1, label: '差旅交通' }, { id: 2, label: '住宿费' }, { id: 3, label: '业务招待' }, { id: 4, label: '差旅补贴' }, { id: 5, label: '办公费用' }]; const newItem = (value?: Partial<ExpenseItem>): FormItem => ({ key: nextKey++, expenseDate: value?.expenseDate || new Date().toISOString().slice(0, 10), category: value?.category || '', description: value?.description || '', amount: Number(value?.amount || 0), seqNo: value?.seqNo }); const form = reactive({ title: '', category: '差旅交通', totalAmount: 0, description: '', payeeName: '', payeeBankId: undefined as number | undefined, items: [newItem()] })
// A missing field entry remains editable for compatibility with older roles.
// Only a successfully loaded field permission can hide or lock a control.
const fieldLevel = (code: string): FieldLevel => { if (!userStore.permissionsLoaded || userStore.permissionsError) return 'EDIT'; return userStore.fieldPermissions[code] || 'EDIT' }; const showField = (code: string) => fieldLevel(code) !== 'HIDDEN'; const readonlyField = (code: string) => fieldLevel(code) === 'READ'; const editableField = (code: string) => fieldLevel(code) === 'EDIT'
const itemsTotal = computed(() => form.items.reduce((sum, item) => sum + (Number(item.amount) || 0), 0)); const amountMatches = computed(() => form.items.length > 0 && form.totalAmount > 0 && Math.abs(itemsTotal.value - form.totalAmount) < 0.005); const hasMaterials = computed(() => Boolean(invoiceNumber.value || invoiceAmount.value > 0 || invoiceFile.value || attachmentFiles.value || existing.value?.invoices.length || existing.value?.attachments.length)); const money = (value: number) => `¥ ${Number(value || 0).toLocaleString('zh-CN', { minimumFractionDigits: 2 })}`
const hydrate = () => { const item = existing.value; if (!item) return; form.title = item.title; form.category = item.category || '差旅交通'; form.totalAmount = item.totalAmount; form.description = item.description; form.payeeName = item.payeeName || userName.value; form.payeeBankId = item.payeeBankId; form.items = item.expenseItems.length ? item.expenseItems.map(newItem) : [newItem({ amount: item.totalAmount, category: item.category, description: item.title })]; invoiceNumber.value = item.invoices[0]?.invoiceNo || ''; invoiceAmount.value = item.invoices[0]?.amount || 0 }
const addItem = () => form.items.push(newItem()); const removeItem = (index: number) => form.items.splice(index, 1); const selectFile = (event: Event, type: 'invoice' | 'attachment') => { const input = event.target as HTMLInputElement; const names = [...(input.files || [])].map(file => file.name).join('、'); if (type === 'invoice') invoiceFile.value = names; else attachmentFiles.value = names }
const permissionBlock = (code: string, label: string, value: unknown) => { const level = fieldLevel(code); const missing = value === undefined || value === null || value === '' || Number(value) === 0; return level !== 'EDIT' && missing ? `当前字段“${label}”为${level === 'HIDDEN' ? '隐藏' : '只读'},但业务接口仍要求该字段,请联系管理员开放字段权限后再提交。` : '' }
const save = async (submit: boolean) => { loadError.value = ''; const hiddenRequired = [permissionBlock('application:amount', '报销金额', form.totalAmount), permissionBlock('application:payeeName', '收款人', form.payeeName), permissionBlock('application:bankAccount', '收款账户', form.payeeBankId)].find(Boolean); if (hiddenRequired) { loadError.value = hiddenRequired; return } if (!form.title || !form.description || !(form.totalAmount > 0) || !form.payeeName || !(form.payeeBankId && form.items.length && amountMatches.value) || form.items.some(item => !item.expenseDate || !item.category || !item.description || !(item.amount > 0))) { loadError.value = '请完善报销信息,并确保费用明细合计等于报销金额'; return } if (submit && !hasMaterials.value) { loadError.value = '提交前请至少添加一份发票或附件材料'; return } saving.value = true; const payload: CreateApplicationRequest = { title: form.title, expenseTypeId: expenseTypes.find(type => type.label === form.category)?.id || 1, category: form.category, totalAmount: Number(form.totalAmount.toFixed(2)), description: form.description, payeeName: form.payeeName, payeeBankId: form.payeeBankId, items: form.items.map((item, index) => ({ seqNo: index + 1, expenseDate: item.expenseDate, category: item.category, description: item.description, amount: Number(item.amount.toFixed(2)) })) }; try { const saved = await store.save(payload, id.value || undefined); if (store.source === 'fallback') { if (invoiceNumber.value || invoiceAmount.value || invoiceFile.value) saved.invoices = [{ id: `${saved.id}-invoice`, invoiceNo: invoiceNumber.value, invoiceType: '发票影像', issueDate: new Date().toISOString().slice(0, 10), amount: invoiceAmount.value, sellerName: '', verifyStatus: '待校验' }]; if (attachmentFiles.value) saved.attachments = attachmentFiles.value.split('、').map((file, index) => ({ id: `${saved.id}-attachment-${index}`, fileName: file, fileType: 'FILE', fileUrl: '' })) } if (submit) await store.submit(saved.id); ElMessage.success(submit ? '报销已提交' : '草稿已保存'); router.replace(`/applications/${saved.id}`) } catch (error) { loadError.value = error instanceof Error ? error.message : '保存失败,请稍后重试'; ElMessage.error(loadError.value) } finally { saving.value = false } }
onMounted(async () => { if (id.value) await store.loadDetail(id.value); else if (!store.reimbursements.length) await store.load(); hydrate(); if (id.value && !existing.value) loadError.value = '未找到该报销单,无法编辑'; loading.value = false })
</script>
<style scoped>
.form-actions { display: flex; justify-content: flex-end; gap: 9px; padding: 16px 0; }
@media (max-width: 560px) { .form-actions { flex-direction: column-reverse; } .form-actions .btn { width: 100%; } }
</style>