/* 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 React, { useEffect, useState, useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import { Table, Button, Input, Tag, Select, Modal, TextArea, Spin, Typography, } from '@/components/ui'; import { Receipt, Search, Eye, Check, X, RotateCw, Ban } 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 } = useTranslation(); const [invoices, setInvoices] = useState([]); const [total, setTotal] = useState(0); const [loading, setLoading] = useState(false); const [page, setPage] = useState(1); 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: 20 }; 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); } } catch (err) { showError(err.message); } finally { setLoading(false); } }, [page, searchKeyword, statusFilter]); useEffect(() => { fetchInvoices(); }, [fetchInvoices]); const handleSearch = () => { setPage(1); setSearchKeyword(keyword); }; 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(); }; 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 || ( User#{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('操作'), width: 200, fixed: 'right', render: (_, record) => (
)} {(record.status === INVOICE_STATUS.APPROVED || record.status === INVOICE_STATUS.INVOICING) && ( )} {record.status === INVOICE_STATUS.FAILED && ( <> )}
), }, ]; return (
{/* 筛选区:标题与筛选项同行 */}
{t('发票管理')}
} placeholder={t('搜索抬头/税号/订单号/用户')} value={keyword} onChange={setKeyword} onEnterPress={handleSearch} style={{ width: 240 }} />