566 lines
14 KiB
Vue
566 lines
14 KiB
Vue
<script setup lang="ts">
|
||
import { computed, onMounted, ref } from 'vue'
|
||
import { storeToRefs } from 'pinia'
|
||
import { RouterLink, useRoute, useRouter } from 'vue-router'
|
||
import { ElMessage, ElMessageBox, ElButton, ElTable, ElTableColumn } from 'element-plus'
|
||
import { PaginationBar, StatusTag } from '@/components'
|
||
import { useFinanceStore } from '@/stores/modules/finance'
|
||
import { useProfileStore } from '@/stores/modules/profile'
|
||
import type { Invoice, InvoiceApplyPayload, InvoiceStatus } from '@/types/business/finance'
|
||
import { formatMoney } from '@/utils/format'
|
||
import { INVOICE_TYPE_LABELS } from '@/utils/invoiceVo'
|
||
import InvoiceApplyDialog from './cpns/InvoiceApplyDialog.vue'
|
||
|
||
const router = useRouter()
|
||
const route = useRoute()
|
||
|
||
const financeStore = useFinanceStore()
|
||
const { titles, invoices, invoiceQuery, invoiceTotal, availableBills, invoiceStatusOptions } = storeToRefs(financeStore)
|
||
|
||
const profileStore = useProfileStore()
|
||
const { profile } = storeToRefs(profileStore)
|
||
|
||
const applyVisible = ref(false)
|
||
const applying = ref(false)
|
||
const applyBillId = ref<string | undefined>(undefined)
|
||
|
||
const defaultTitle = computed(() => titles.value.find((item) => item.isDefault) ?? titles.value[0] ?? null)
|
||
|
||
type TagVariant = 'primary' | 'success' | 'warning' | 'danger' | 'info'
|
||
|
||
/** 发票状态选项卡:字典 + 首项「全部」(value 为空,表示不传 status)。 */
|
||
const STATUS_TABS = computed<Array<{ label: string; value: InvoiceStatus | '' }>>(() => [
|
||
{ label: '全部', value: '' },
|
||
...invoiceStatusOptions.value.map((item) => ({
|
||
label: item.dictLabel,
|
||
value: item.dictValue as InvoiceStatus,
|
||
})),
|
||
])
|
||
|
||
/** 状态文案优先用后端 statusName,缺失时回退字典文案。 */
|
||
function statusLabel(invoice: Invoice): string {
|
||
return invoice.statusName
|
||
|| invoiceStatusOptions.value.find((item) => item.dictValue === invoice.status)?.dictLabel
|
||
|| '—'
|
||
}
|
||
|
||
/** 标签颜色取字典 listClass(info/warning/success)。 */
|
||
function statusVariant(status: InvoiceStatus): TagVariant {
|
||
const listClass = invoiceStatusOptions.value.find((item) => item.dictValue === status)?.listClass
|
||
if (listClass === 'primary' || listClass === 'success' || listClass === 'warning' || listClass === 'danger') {
|
||
return listClass
|
||
}
|
||
return 'info'
|
||
}
|
||
|
||
function onTab(value: InvoiceStatus | ''): void {
|
||
if (invoiceQuery.value.status === value) return
|
||
financeStore.setInvoiceQuery({ ...invoiceQuery.value, status: value })
|
||
void financeStore.searchInvoices()
|
||
}
|
||
|
||
function onPageChange(page: number): void {
|
||
void financeStore.changeInvoicePage(page)
|
||
}
|
||
|
||
async function onOpenApply(billId?: string): Promise<void> {
|
||
applyBillId.value = billId || undefined
|
||
try {
|
||
await financeStore.fetchAvailableBills()
|
||
applyVisible.value = true
|
||
}
|
||
catch {
|
||
// 请求异常已由 request.ts 统一提示
|
||
}
|
||
}
|
||
|
||
async function onSubmitApply(payload: InvoiceApplyPayload): Promise<void> {
|
||
applying.value = true
|
||
try {
|
||
await financeStore.applyInvoice(payload)
|
||
applyVisible.value = false
|
||
ElMessage.success('开票申请已提交')
|
||
}
|
||
catch {
|
||
// 请求异常已由 request.ts 统一提示
|
||
}
|
||
finally {
|
||
applying.value = false
|
||
}
|
||
}
|
||
|
||
/** 已驳回的发票可重新发起开票申请。 */
|
||
function onApplyForRow(invoice: Invoice): void {
|
||
void onOpenApply(invoice.billId)
|
||
}
|
||
|
||
async function onPreview(invoice: Invoice): Promise<void> {
|
||
try {
|
||
const url = await financeStore.fetchPreviewUrl(invoice.id)
|
||
if (!url) {
|
||
ElMessage.warning('暂无可预览的发票文件')
|
||
return
|
||
}
|
||
window.open(url, '_blank', 'noopener')
|
||
}
|
||
catch {
|
||
// 请求异常已由 request.ts 统一提示
|
||
}
|
||
}
|
||
|
||
async function onDownload(invoice: Invoice): Promise<void> {
|
||
try {
|
||
const { blob, fileName } = await financeStore.fetchDownloadFile(invoice.id)
|
||
if (!blob || blob.size === 0) {
|
||
ElMessage.warning('暂无可下载的发票文件')
|
||
return
|
||
}
|
||
const url = URL.createObjectURL(blob)
|
||
const link = document.createElement('a')
|
||
link.href = url
|
||
link.download = fileName || `${invoice.invoiceNo || 'invoice'}.pdf`
|
||
document.body.appendChild(link)
|
||
link.click()
|
||
link.remove()
|
||
URL.revokeObjectURL(url)
|
||
}
|
||
catch {
|
||
// 请求异常已由 request.ts 统一提示
|
||
}
|
||
}
|
||
|
||
async function onSendEmail(invoice: Invoice): Promise<void> {
|
||
if (!profile.value?.email) {
|
||
try {
|
||
await ElMessageBox.confirm('您还未绑定邮箱,请先绑定邮箱后再发送发票', '提示', {
|
||
confirmButtonText: '去绑定',
|
||
cancelButtonText: '取消',
|
||
type: 'warning',
|
||
})
|
||
void router.push({ name: 'ProfileBindEmail', query: { redirect: route.fullPath } })
|
||
}
|
||
catch {
|
||
// 用户取消
|
||
}
|
||
return
|
||
}
|
||
|
||
try {
|
||
await financeStore.sendInvoiceEmail(invoice.id)
|
||
ElMessage.success('发票已发送至绑定邮箱')
|
||
}
|
||
catch {
|
||
// 请求异常已由 request.ts 统一提示
|
||
}
|
||
}
|
||
|
||
onMounted(() => {
|
||
void Promise.all([
|
||
financeStore.fetchInvoiceStatusOptions(),
|
||
financeStore.fetchTitles(),
|
||
financeStore.fetchInvoices(),
|
||
profileStore.fetchProfile().catch(() => null),
|
||
])
|
||
})
|
||
</script>
|
||
|
||
<template>
|
||
<div class="invoices">
|
||
<nav class="invoices__breadcrumb text-secondary">
|
||
<RouterLink to="/finance" class="link">
|
||
财务中心
|
||
</RouterLink>
|
||
<span> > </span>
|
||
<span>发票申请</span>
|
||
</nav>
|
||
|
||
<section v-if="defaultTitle" class="invoices__title-card card">
|
||
<div class="invoices__title-main">
|
||
<div class="invoices__title-head">
|
||
<span class="invoices__title-label text-secondary">默认发票抬头</span>
|
||
<StatusTag :label="defaultTitle.type" variant="primary" />
|
||
<StatusTag label="默认" variant="success" />
|
||
</div>
|
||
<p class="invoices__title-name">
|
||
{{ defaultTitle.title }}
|
||
</p>
|
||
<p class="invoices__title-tax text-secondary">
|
||
税号:{{ defaultTitle.taxNo }}
|
||
</p>
|
||
</div>
|
||
<RouterLink to="/finance/invoice-titles" class="invoices__title-manage">
|
||
管理抬头 >
|
||
</RouterLink>
|
||
</section>
|
||
|
||
<div class="invoices__toolbar">
|
||
<div class="invoices__tabs">
|
||
<el-button
|
||
v-for="tab in STATUS_TABS"
|
||
:key="tab.value"
|
||
class="invoices__tab"
|
||
:class="{ 'is-active': invoiceQuery.status === tab.value }"
|
||
@click="onTab(tab.value)"
|
||
>
|
||
{{ tab.label }}
|
||
</el-button>
|
||
</div>
|
||
|
||
<el-button class="invoices__apply" @click="onOpenApply()">
|
||
+ 申请开票
|
||
</el-button>
|
||
</div>
|
||
|
||
<div class="invoices__table card">
|
||
<el-table
|
||
class="invoices__table-el"
|
||
:data="invoices"
|
||
row-key="id"
|
||
>
|
||
<el-table-column
|
||
type="index"
|
||
label="序号"
|
||
width="60"
|
||
/>
|
||
<el-table-column
|
||
label="发票号码"
|
||
min-width="150"
|
||
show-overflow-tooltip
|
||
>
|
||
<template #default="{ row }">
|
||
{{ (row as Invoice).invoiceNo || '—' }}
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column
|
||
prop="projectName"
|
||
label="项目名称"
|
||
min-width="160"
|
||
show-overflow-tooltip
|
||
/>
|
||
<el-table-column
|
||
prop="titleName"
|
||
label="发票抬头"
|
||
min-width="180"
|
||
show-overflow-tooltip
|
||
/>
|
||
<el-table-column
|
||
label="发票类型"
|
||
width="100"
|
||
>
|
||
<template #default="{ row }">
|
||
{{ INVOICE_TYPE_LABELS[(row as Invoice).invoiceType ?? ''] ?? '—' }}
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column
|
||
label="开票金额"
|
||
width="110"
|
||
>
|
||
<template #default="{ row }">
|
||
<span class="invoices__amount">¥{{ formatMoney((row as Invoice).amount) }}</span>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column
|
||
prop="transactionNo"
|
||
label="交易流水号"
|
||
min-width="180"
|
||
show-overflow-tooltip
|
||
/>
|
||
<el-table-column
|
||
label="状态"
|
||
width="90"
|
||
>
|
||
<template #default="{ row }">
|
||
<StatusTag
|
||
:label="statusLabel(row as Invoice)"
|
||
:variant="statusVariant((row as Invoice).status)"
|
||
/>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column
|
||
prop="applyTime"
|
||
label="申请时间"
|
||
width="140"
|
||
show-overflow-tooltip
|
||
/>
|
||
<el-table-column
|
||
label="操作"
|
||
width="180"
|
||
fixed="right"
|
||
>
|
||
<template #default="{ row }">
|
||
<span class="invoices__ops">
|
||
<template v-if="(row as Invoice).status === '2'">
|
||
<el-button
|
||
link
|
||
type="primary"
|
||
class="invoices__link"
|
||
@click="onPreview(row as Invoice)"
|
||
>
|
||
预览
|
||
</el-button>
|
||
<el-button
|
||
link
|
||
type="primary"
|
||
class="invoices__link"
|
||
@click="onDownload(row as Invoice)"
|
||
>
|
||
下载
|
||
</el-button>
|
||
<el-button
|
||
link
|
||
type="primary"
|
||
class="invoices__link"
|
||
@click="onSendEmail(row as Invoice)"
|
||
>
|
||
发送邮箱
|
||
</el-button>
|
||
</template>
|
||
<el-button
|
||
v-else-if="(row as Invoice).status === '0'"
|
||
link
|
||
type="primary"
|
||
class="invoices__link"
|
||
@click="onApplyForRow(row as Invoice)"
|
||
>
|
||
申请开票
|
||
</el-button>
|
||
<span v-else class="invoices__dash">—</span>
|
||
</span>
|
||
</template>
|
||
</el-table-column>
|
||
|
||
<template #empty>
|
||
<span class="invoices__empty text-secondary">暂无发票记录</span>
|
||
</template>
|
||
</el-table>
|
||
</div>
|
||
|
||
<PaginationBar
|
||
:page="invoiceQuery.page"
|
||
:page-size="invoiceQuery.pageSize"
|
||
:total="invoiceTotal"
|
||
@page-change="onPageChange"
|
||
/>
|
||
|
||
<p class="invoices__tip text-secondary">
|
||
仅已支付且未开票的账单可申请开票,开票进度可在本页查看。抬头管理请前往
|
||
<RouterLink to="/finance/invoice-titles" class="link">管理抬头</RouterLink>。
|
||
</p>
|
||
|
||
<InvoiceApplyDialog
|
||
v-model:visible="applyVisible"
|
||
:bills="availableBills"
|
||
:titles="titles"
|
||
:preselect-bill-id="applyBillId"
|
||
:submitting="applying"
|
||
@submit="onSubmitApply"
|
||
/>
|
||
</div>
|
||
</template>
|
||
|
||
<style lang="less" scoped>
|
||
.invoices {
|
||
.flex(column, flex-start);
|
||
align-items: stretch;
|
||
gap: @spacing-base;
|
||
width: 100%;
|
||
max-width: @container-width;
|
||
min-height: 100%;
|
||
margin: 0 auto;
|
||
padding: @spacing-lg @spacing-base @spacing-page-bottom;
|
||
background: @bg-color;
|
||
|
||
&__breadcrumb {
|
||
font-size: 11px;
|
||
color: @text-color-disabled;
|
||
|
||
> span:last-child {
|
||
color: @text-color;
|
||
}
|
||
}
|
||
|
||
&__title-card {
|
||
.flex(row, space-between, center);
|
||
padding: @spacing-base @spacing-lg;
|
||
border-radius: @radius-lg;
|
||
background: @white;
|
||
box-shadow: @shadow-base;
|
||
}
|
||
|
||
&__title-main {
|
||
.flex(column, flex-start, stretch);
|
||
gap: @spacing-xs;
|
||
}
|
||
|
||
&__title-head {
|
||
.flex(row, flex-start, center);
|
||
gap: @spacing-sm;
|
||
}
|
||
|
||
&__title-label {
|
||
font-size: @font-size-sm;
|
||
}
|
||
|
||
&__title-name {
|
||
font-size: @font-size-lg;
|
||
font-weight: 600;
|
||
color: @heading-color;
|
||
}
|
||
|
||
&__title-tax {
|
||
font-size: @font-size-base;
|
||
}
|
||
|
||
&__title-manage {
|
||
color: @primary-color;
|
||
font-size: @font-size-base;
|
||
white-space: nowrap;
|
||
|
||
&:hover {
|
||
color: @primary-hover;
|
||
}
|
||
}
|
||
|
||
&__toolbar {
|
||
.flex(row, space-between, center);
|
||
gap: @spacing-base;
|
||
}
|
||
|
||
&__tabs {
|
||
display: flex;
|
||
flex-direction: row;
|
||
align-items: flex-start;
|
||
gap: @spacing-sm;
|
||
}
|
||
|
||
&__apply {
|
||
height: 34px;
|
||
padding: 0 @spacing-base;
|
||
border: none;
|
||
border-radius: @radius-base;
|
||
background: @primary-strong;
|
||
color: @white;
|
||
font-size: @font-size-base;
|
||
font-weight: 500;
|
||
transition: background 0.2s;
|
||
|
||
&:hover {
|
||
background: @primary-color;
|
||
color: @white;
|
||
}
|
||
}
|
||
|
||
&__tab {
|
||
height: 34px;
|
||
margin: 0;
|
||
padding: 0 @spacing-base;
|
||
border: 1px solid @border-color;
|
||
border-radius: @radius-base;
|
||
background: @white;
|
||
color: @text-color;
|
||
font-size: @font-size-base;
|
||
font-weight: 400;
|
||
transition: all 0.2s;
|
||
|
||
&:hover {
|
||
border-color: @primary-color;
|
||
background: @white;
|
||
color: @primary-color;
|
||
}
|
||
|
||
&.is-active {
|
||
border-color: @primary-color;
|
||
background: @filter-selected-bg;
|
||
color: @primary-color;
|
||
font-weight: 500;
|
||
|
||
&:hover {
|
||
border-color: @primary-color;
|
||
background: @filter-selected-bg;
|
||
color: @primary-color;
|
||
}
|
||
}
|
||
}
|
||
|
||
&__table {
|
||
padding: @spacing-base;
|
||
border-radius: @radius-lg;
|
||
background: @white;
|
||
box-shadow: @shadow-base;
|
||
overflow-x: auto;
|
||
}
|
||
|
||
&__table-el {
|
||
width: 100%;
|
||
font-size: @font-size-base;
|
||
color: @text-color;
|
||
|
||
:deep(th.el-table__cell) {
|
||
background: @bg-color-light;
|
||
border-bottom: 1px solid @border-color;
|
||
color: @text-color-secondary;
|
||
font-size: @font-size-base;
|
||
font-weight: 500;
|
||
text-align: left;
|
||
}
|
||
|
||
:deep(td.el-table__cell) {
|
||
padding: @spacing-sm 0;
|
||
border-bottom: 1px solid @border-color;
|
||
font-size: @font-size-base;
|
||
}
|
||
|
||
:deep(.el-table__body tr:hover > td.el-table__cell) {
|
||
background: @bg-color-light;
|
||
}
|
||
|
||
:deep(.el-table .cell) {
|
||
padding-left: @spacing-sm;
|
||
padding-right: @spacing-sm;
|
||
white-space: nowrap;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
}
|
||
}
|
||
|
||
&__amount {
|
||
color: @heading-color;
|
||
font-weight: 600;
|
||
}
|
||
|
||
&__ops {
|
||
display: inline-flex;
|
||
flex-direction: row;
|
||
align-items: center;
|
||
gap: @spacing-sm;
|
||
}
|
||
|
||
&__link {
|
||
padding: 0;
|
||
border: none;
|
||
background: transparent;
|
||
color: @primary-color;
|
||
font-size: @font-size-base;
|
||
transition: color 0.2s;
|
||
|
||
&:hover {
|
||
color: @primary-hover;
|
||
background: transparent;
|
||
}
|
||
}
|
||
|
||
&__dash {
|
||
color: @text-color-secondary;
|
||
}
|
||
|
||
&__empty {
|
||
text-align: center;
|
||
padding: @spacing-xl 0;
|
||
}
|
||
|
||
&__tip {
|
||
font-size: @font-size-sm;
|
||
}
|
||
}
|
||
</style>
|