Files
Academic-Affairs-System/web/src/views/TuitionView.vue
T

162 lines
10 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.
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { ElMessage, ElMessageBox } from 'element-plus'
import http, { apiErrorMessage } from '../api/http'
import { downloadApiFile } from '../api/excel'
import { useAuthStore } from '../stores/auth'
interface Term { id: string; name: string; status: string; creditPrice: number; isCurrent: boolean }
interface Bill {
id: string; studentNumber?: string; studentName?: string; termName?: string
baseTuition: number; basePaid: number; creditFee: number; creditPaid: number
accommodationFee: number; accommodationPaid: number
reconciliationAdjustment: number; adjustmentPaid: number; isPayable: boolean
}
const auth = useAuthStore()
const { locale, t } = useI18n()
const isStudent = computed(() => auth.user?.roles.includes('Student') ?? false)
const terms = ref<Term[]>([])
const rows = ref<Bill[]>([])
const termId = ref('')
const creditPrice = ref<number | undefined>()
const loading = ref(false)
const fileInput = ref<HTMLInputElement | null>(null)
const keyword = ref('')
const page = ref(1)
const total = ref(0)
const pageSize = 20
const money = (value: number) => new Intl.NumberFormat(locale.value, { style: 'currency', currency: 'CNY' }).format(Number(value || 0))
const bill = (value: unknown) => value as Bill
const remaining = (total: number, paid: number) => Math.max(0, Number(total || 0) - Number(paid || 0))
const statusLabel = (value: string) => t(`tuition.statuses.${value}`)
async function load() {
loading.value = true
try {
if (isStudent.value) {
rows.value = (await http.get('/tuition/mine')).data
return
}
const { data } = await http.get('/tuition/terms')
terms.value = data.map((term: any) => ({
...term,
status: term.setting?.status ?? 'Draft',
creditPrice: term.setting?.creditUnitPrice ?? 0,
}))
if (!termId.value) termId.value = terms.value.find(x => x.isCurrent)?.id || terms.value[0]?.id || ''
const term = terms.value.find(x => x.id === termId.value)
creditPrice.value = term?.creditPrice
if (!termId.value) { rows.value = []; total.value = 0; return }
const { data: result } = await http.get('/tuition/bills', { params: { termId: termId.value, keyword: keyword.value || undefined, page: page.value, pageSize } })
rows.value = result.items; total.value = result.total
} catch (error) {
ElMessage.error(apiErrorMessage(error))
} finally {
loading.value = false
}
}
async function saveCreditPrice() {
if (!termId.value || creditPrice.value == null || creditPrice.value < 0) return ElMessage.warning(t('tuition.messages.invalidPrice'))
try {
await http.put(`/tuition/terms/${termId.value}/credit-price`, { creditUnitPrice: creditPrice.value })
ElMessage.success(t('tuition.messages.priceSaved'))
await load()
} catch (error) { ElMessage.error(apiErrorMessage(error)) }
}
async function downloadTemplate() {
try { await downloadApiFile(`/tuition/terms/${termId.value}/import-template.xlsx`, t('tuition.templateFilename')) }
catch (error) { ElMessage.error(apiErrorMessage(error)) }
}
async function importBills(event: Event) {
const file = (event.target as HTMLInputElement).files?.[0]
if (!file || !termId.value) return
const form = new FormData(); form.append('file', file)
try {
const { data } = await http.post(`/tuition/terms/${termId.value}/import`, form)
ElMessage.success(t('tuition.messages.imported', { count: data.imported }))
await load()
} catch (error) { ElMessage.error(apiErrorMessage(error)) }
finally { if (fileInput.value) fileInput.value.value = '' }
}
async function lockCredits() {
try { await http.post(`/tuition/terms/${termId.value}/lock-credits`); ElMessage.success(t('tuition.messages.creditsLocked')); await load() }
catch (error) { ElMessage.error(apiErrorMessage(error)) }
}
async function openPayment() {
try { await http.post(`/tuition/terms/${termId.value}/open-payment`); ElMessage.success(t('tuition.messages.paymentOpened')); await load() }
catch (error) { ElMessage.error(apiErrorMessage(error)) }
}
async function reconcile(row: Bill) {
try {
const { value } = await ElMessageBox.prompt(t('tuition.reconcilePrompt'), t('tuition.reconcileTitle'), { inputPattern: /^-?\d+(\.\d{1,2})?$/, inputErrorMessage: t('tuition.amountError') })
await http.post(`/tuition/bills/${row.id}/reconcile`, { amount: Number(value), reason: t('tuition.reconcileReason') })
ElMessage.success(t('tuition.messages.reconciled')); await load()
} catch (error) { if (error !== 'cancel' && error !== 'close') ElMessage.error(apiErrorMessage(error)) }
}
async function pay(row: Bill, item: string) {
try { await http.post(`/tuition/bills/${row.id}/items/${item}/pay-balance`); ElMessage.success(t('tuition.messages.paid')); await load() }
catch (error) { ElMessage.error(apiErrorMessage(error)) }
}
onMounted(load)
</script>
<template>
<section v-loading="loading" class="tuition">
<header>
<p>{{ isStudent ? 'MY TUITION' : 'TUITION OPERATIONS' }}</p>
<h2>{{ t(isStudent ? 'tuition.myTitle' : 'tuition.managementTitle') }}</h2>
<span>{{ t('tuition.description') }}</span>
</header>
<el-card v-if="!isStudent" class="workbench">
<div class="toolbar">
<el-select v-model="termId" :placeholder="t('tuition.selectTerm')" @change="page = 1; load()">
<el-option v-for="term in terms" :key="term.id" :label="`${term.name}(${statusLabel(term.status)})`" :value="term.id" />
</el-select>
<el-input-number v-model="creditPrice" :min="0" :precision="2" :placeholder="t('tuition.creditPrice')" />
<el-button @click="saveCreditPrice">{{ t('tuition.savePrice') }}</el-button>
<el-button :disabled="!termId" @click="downloadTemplate">{{ t('tuition.downloadTemplate') }}</el-button>
<el-button :disabled="!termId" @click="fileInput?.click()">{{ t('tuition.importFees') }}</el-button>
<input ref="fileInput" type="file" accept=".xlsx" hidden @change="importBills" />
<el-button :disabled="!termId" @click="lockCredits">{{ t('tuition.lockCredits') }}</el-button>
<el-button type="primary" :disabled="!termId" @click="openPayment">{{ t('tuition.openPayment') }}</el-button>
</div>
<el-alert type="info" :closable="false" :title="t('tuition.workflow')" />
</el-card>
<el-card>
<div v-if="!isStudent" class="bill-filter"><el-input v-model="keyword" clearable :placeholder="t('tuition.searchPlaceholder')" @keyup.enter="page = 1; load()" /><el-button @click="page = 1; load()">{{ t('tuition.search') }}</el-button></div>
<el-table :data="rows" :empty-text="t('tuition.empty')">
<el-table-column v-if="!isStudent" prop="studentNumber" :label="t('tuition.studentNumber')" width="140" />
<el-table-column v-if="!isStudent" prop="studentName" :label="t('tuition.student')" width="120" />
<el-table-column v-if="isStudent" prop="termName" :label="t('tuition.term')" min-width="160" />
<el-table-column :label="t('tuition.baseTuition')" min-width="155"><template #default="{ row }">{{ money(bill(row).baseTuition) }}<small>{{ t('tuition.remaining', { amount: money(remaining(bill(row).baseTuition, bill(row).basePaid)) }) }}</small></template></el-table-column>
<el-table-column :label="t('tuition.creditFee')" min-width="155"><template #default="{ row }">{{ money(bill(row).creditFee) }}<small>{{ t('tuition.remaining', { amount: money(remaining(bill(row).creditFee, bill(row).creditPaid)) }) }}</small></template></el-table-column>
<el-table-column :label="t('tuition.accommodationFee')" min-width="155"><template #default="{ row }">{{ money(bill(row).accommodationFee) }}<small>{{ t('tuition.remaining', { amount: money(remaining(bill(row).accommodationFee, bill(row).accommodationPaid)) }) }}</small></template></el-table-column>
<el-table-column :label="t('tuition.adjustment')" min-width="155"><template #default="{ row }">{{ money(bill(row).reconciliationAdjustment) }}<small>{{ t('tuition.remaining', { amount: money(remaining(bill(row).reconciliationAdjustment, bill(row).adjustmentPaid)) }) }}</small></template></el-table-column>
<el-table-column v-if="isStudent" :label="t('tuition.balancePayment')" width="260" fixed="right">
<template #default="{ row }">
<el-button v-if="bill(row).isPayable && remaining(bill(row).baseTuition, bill(row).basePaid) > 0" link type="primary" @click="pay(bill(row), 'BaseTuition')">{{ t('tuition.payBase') }}</el-button>
<el-button v-if="bill(row).isPayable && remaining(bill(row).creditFee, bill(row).creditPaid) > 0" link type="primary" @click="pay(bill(row), 'CreditFee')">{{ t('tuition.payCredits') }}</el-button>
<el-button v-if="bill(row).isPayable && remaining(bill(row).accommodationFee, bill(row).accommodationPaid) > 0" link type="primary" @click="pay(bill(row), 'AccommodationFee')">{{ t('tuition.payAccommodation') }}</el-button>
<el-button v-if="bill(row).isPayable && remaining(bill(row).reconciliationAdjustment, bill(row).adjustmentPaid) > 0" link type="primary" @click="pay(bill(row), 'ReconciliationAdjustment')">{{ t('tuition.payAdjustment') }}</el-button>
</template>
</el-table-column>
<el-table-column v-else :label="t('tuition.reconciliation')" width="110" fixed="right"><template #default="{ row }"><el-button link type="warning" @click="reconcile(bill(row))">{{ t('tuition.reconcile') }}</el-button></template></el-table-column>
</el-table>
<el-pagination v-if="!isStudent && total > pageSize" v-model:current-page="page" :page-size="pageSize" :total="total" layout="total, prev, pager, next" @current-change="load" />
</el-card>
</section>
</template>
<style scoped>
.tuition{display:grid;gap:18px}.tuition header p{margin:0;color:#17867c;font-size:12px;font-weight:800;letter-spacing:.16em}.tuition h2{margin:6px 0;color:#183b56}.tuition header span,small{color:#738596}.toolbar,.bill-filter{display:flex;gap:10px;flex-wrap:wrap;margin-bottom:14px}.toolbar .el-select,.bill-filter .el-input{width:260px}small{display:block;margin-top:4px}.el-pagination{justify-content:flex-end;margin-top:16px}@media(max-width:720px){.toolbar>*,.bill-filter>*{width:100%!important}}
</style>