/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import { formatUserReference } from '../../helpers/safeDisplay';
import { Trans } from 'react-i18next';
import {
ConsoleFilterField,
ConsoleFilterGrid,
ConsoleListTable,
} from '../../components/console/ConsolePrimitives';
import ConsoleCollection from '../../components/console/ConsoleCollection';
import { TableRowActions } from '../../components/common/table/TableToolbar';
import { useIsMobile } from '../../hooks/common/useIsMobile';
import { createCardProPagination } from '../../helpers/utils';
import React, { useEffect, useState, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import {
Button,
Input,
Tag,
Select,
Modal,
TextArea,
Spin,
Typography,
} from '@/components/ui';
import { Search } from 'lucide-react';
import { API, showError, showSuccess } from '../../helpers';
import {
INVOICE_STATUS,
INVOICE_STATUS_TEXT,
INVOICE_STATUS_COLOR,
} from '../../constants';
const { Text } = Typography;
const paymentMethodMap = {
wechat: '微信',
alipay: '支付宝',
stripe: 'Stripe',
creem: 'Creem',
epay: '易支付',
};
const AdminInvoice = () => {
const { t, i18n } = useTranslation();
const isMobile = useIsMobile();
const [invoices, setInvoices] = useState([]);
const [total, setTotal] = useState(0);
const [loading, setLoading] = useState(false);
const [page, setPage] = useState(1);
const [pageSize, setPageSize] = useState(20);
const [keyword, setKeyword] = useState('');
const [searchKeyword, setSearchKeyword] = useState('');
const [statusFilter, setStatusFilter] = useState('');
// 详情弹窗
const [detailVisible, setDetailVisible] = useState(false);
const [detailData, setDetailData] = useState(null);
const [detailLoading, setDetailLoading] = useState(false);
// 拒绝弹窗
const [rejectVisible, setRejectVisible] = useState(false);
const [rejectId, setRejectId] = useState(null);
const [rejectReason, setRejectReason] = useState('');
const [rejectLoading, setRejectLoading] = useState(false);
// 审核通过弹窗
const [approveVisible, setApproveVisible] = useState(false);
const [approveId, setApproveId] = useState(null);
const [adminRemark, setAdminRemark] = useState('');
const [approveLoading, setApproveLoading] = useState(false);
const fetchInvoices = useCallback(async () => {
setLoading(true);
try {
const params = { p: page, page_size: pageSize };
if (searchKeyword) params.keyword = searchKeyword;
if (statusFilter) params.status = statusFilter;
const res = await API.get('/api/invoice/list', { params });
if (res.data.success) {
setInvoices(res.data.data?.items || []);
setTotal(res.data.data?.total || 0);
} else {
showError(res.data.message || t('查询失败'));
}
} catch (err) {
showError(err.message);
} finally {
setLoading(false);
}
}, [page, pageSize, searchKeyword, statusFilter, t]);
useEffect(() => {
fetchInvoices();
}, [fetchInvoices]);
const handleSearch = () => {
setPage(1);
setSearchKeyword(keyword);
};
const handleReset = () => {
setPage(1);
setKeyword('');
setSearchKeyword('');
setStatusFilter('');
};
const handleStatusFilterChange = (value) => {
setPage(1);
setStatusFilter(value || '');
};
// 查看详情
const handleViewDetail = async (id) => {
setDetailData(null);
setDetailVisible(true);
setDetailLoading(true);
try {
const res = await API.get(`/api/invoice/detail/${id}`);
if (res.data.success) {
setDetailData(res.data.data);
} else {
showError(res.data.message);
setDetailVisible(false);
}
} catch (err) {
showError(err.message);
setDetailVisible(false);
} finally {
setDetailLoading(false);
}
};
// 审核通过
const handleApprove = async () => {
setApproveLoading(true);
try {
const res = await API.post('/api/invoice/approve', {
id: approveId,
admin_remark: adminRemark,
});
if (res.data.success) {
showSuccess(t('审核通过'));
setApproveVisible(false);
setApproveId(null);
setAdminRemark('');
fetchInvoices();
} else {
showError(res.data.message);
}
} catch (err) {
showError(err.message);
} finally {
setApproveLoading(false);
}
};
// 拒绝
const handleReject = async () => {
if (!rejectReason.trim()) {
showError(t('请填写拒绝理由'));
return;
}
setRejectLoading(true);
try {
const res = await API.post('/api/invoice/reject', {
id: rejectId,
reject_reason: rejectReason.trim(),
});
if (res.data.success) {
showSuccess(t('已拒绝'));
setRejectVisible(false);
setRejectId(null);
setRejectReason('');
fetchInvoices();
} else {
showError(res.data.message);
}
} catch (err) {
showError(err.message);
} finally {
setRejectLoading(false);
}
};
// 重试开票
const handleRetry = async (id) => {
try {
const res = await API.post('/api/invoice/retry', { id });
if (res.data.success) {
showSuccess(t('已重新发起开票'));
fetchInvoices();
} else {
showError(res.data.message);
}
} catch (err) {
showError(err.message);
}
};
// 取消失败订单
const handleFailCancel = async (id) => {
Modal.confirm({
title: t('确认取消'),
content: t('取消后关联的充值记录将被释放,用户可重新申请开票。'),
onOk: async () => {
try {
const res = await API.post('/api/invoice/fail-cancel', { id });
if (res.data.success) {
showSuccess(t('已取消'));
fetchInvoices();
} else {
showError(res.data.message);
}
} catch (err) {
showError(err.message);
}
},
});
};
const formatTime = (timestamp) => {
if (!timestamp) return '-';
return new Date(timestamp * 1000).toLocaleString(i18n.language);
};
const columns = [
{ title: 'ID', dataIndex: 'id', width: 60 },
{
title: t('用户'),
width: 120,
ellipsis: true,
render: (_, record) => {
const displayName = record.display_name || '';
const username = record.username || '';
if (displayName && username) {
return (
{displayName}{' '}
({username})
);
}
return (
displayName ||
username || (
{t('用户 #{{id}}', { id: record.user_id })}
)
);
},
},
{
title: t('发票抬头'),
dataIndex: 'invoice_title',
width: 160,
ellipsis: true,
},
{
title: t('税号'),
dataIndex: 'tax_id',
width: 140,
ellipsis: true,
render: (text) => {text},
},
{
title: t('金额'),
dataIndex: 'amount',
width: 90,
render: (v) => `¥${Number(v).toFixed(2)}`,
},
{
title: t('状态'),
dataIndex: 'status',
width: 90,
render: (status) => (
{t(INVOICE_STATUS_TEXT[status] || '未知')}
),
},
{
title: t('申请时间'),
dataIndex: 'create_time',
width: 150,
render: (v) => formatTime(v),
},
{
title: t('操作'),
key: 'actions',
width: 140,
fixed: 'right',
render: (_, record) => (
{
setApproveId(record.id);
setApproveVisible(true);
},
},
record.status === INVOICE_STATUS.PENDING && {
key: 'reject',
label: t('拒绝'),
danger: true,
onClick: () => {
setRejectId(record.id);
setRejectVisible(true);
},
},
record.status === INVOICE_STATUS.FAILED && {
key: 'retry',
label: t('重试'),
onClick: () => handleRetry(record.id),
},
[
INVOICE_STATUS.APPROVED,
INVOICE_STATUS.INVOICING,
INVOICE_STATUS.FAILED,
].includes(record.status) && {
key: 'cancel',
label: t('取消'),
danger: true,
onClick: () => handleFailCancel(record.id),
},
]}
>
),
},
];
return (
}
placeholder={t('搜索抬头/税号/订单号/用户')}
value={keyword}
onChange={setKeyword}
onEnterPress={handleSearch}
/>
}
paginationArea={createCardProPagination({
currentPage: page,
pageSize,
total,
onPageChange: setPage,
onPageSizeChange: (size) => {
setPage(1);
setPageSize(size);
},
isMobile,
t,
})}
>
{/* 审核通过弹窗 */}
{
setApproveVisible(false);
setApproveId(null);
setAdminRemark('');
}}
onOk={handleApprove}
okText={t('确认通过')}
confirmLoading={approveLoading}
>
{t('确认审核通过此发票申请?')}
{/* 拒绝弹窗 */}
{
setRejectVisible(false);
setRejectId(null);
setRejectReason('');
}}
onOk={handleReject}
okText={t('确认拒绝')}
okButtonProps={{ type: 'danger' }}
confirmLoading={rejectLoading}
>
{/* 详情弹窗 */}
setDetailVisible(false)}
footer={null}
width={600}
>
{detailLoading ? (
) : detailData ? (
{t('状态')}
{t(INVOICE_STATUS_TEXT[detailData.status] || '未知')}
{t('申请人')}
{detailData.display_name ||
detailData.username ||
formatUserReference(
detailData.display_name || detailData.username,
detailData.user_id,
)}
{t('发票抬头')}
{detailData.invoice_title}
{t('税号')}
{detailData.tax_id}
{t('开票金额')}
¥{Number(detailData.amount).toFixed(2)}
{t('收件邮箱')}
{detailData.email}
{t('订单号')}
{detailData.order_no}
{t('申请时间')}
{formatTime(detailData.create_time)}
{detailData.invoice_number && (
{t('发票号码')}: {detailData.invoice_number}
)}
{detailData.reject_reason && (
{t('拒绝原因')}: {detailData.reject_reason}
)}
{detailData.fail_reason && (
{t('失败原因')}: {detailData.fail_reason}
)}
{detailData.admin_remark && (
{t('管理员备注')}: {detailData.admin_remark}
)}
{/* 关联充值记录 */}
{detailData.topup_list && detailData.topup_list.length > 0 && (
s + (item.money || 0), 0)
.toFixed(2),
}}
/>
formatTime(v),
},
{
title: t('方式'),
dataIndex: 'payment_method',
width: 80,
render: (v) => t(paymentMethodMap[v] || v),
},
{
title: t('金额'),
dataIndex: 'money',
width: 90,
render: (v) => `¥${Number(v).toFixed(2)}`,
},
]}
/>
)}
) : null}
);
};
export default AdminInvoice;