/* 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, { useCallback, useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Button, DatePicker, Select, Spin, Tag, Typography } from 'antd'; import dayjs from 'dayjs'; import { Table } from '@/components/ui'; import { RefreshCw } from '@/components/common/icons/ActionIcons'; import OrgSideSheet from '../components/OrgSideSheet'; import OrgStateBlock from '../components/OrgStateBlock'; import { renderQuota } from '../../../helpers/render'; import { timestamp2string } from '../../../helpers/utils'; import { FUND_ENTRY_TYPES, FUND_LEDGER_PENDING, fundEntryLabel, } from './fundFormat'; const PAGE_SIZE = 20; const defaultRange = () => { const end = new Date(); return [new Date(end.getTime() - 30 * 24 * 3600 * 1000), end]; }; /** * 账户流水(F-29):所选账户、时间范围(默认近 30 日)、类型;时间倒序、每页 20 条。 * 金额:消费类保留 6 位(与消费明细一致),资金操作类 2 位。 * * @param {(accountId, params)=>Promise} fetchLedger 企业侧或部门侧的流水接口 */ const FundLedgerSheet = ({ visible, account, fetchLedger, onClose }) => { const { t } = useTranslation(); const [range, setRange] = useState(defaultRange); const [types, setTypes] = useState([]); const [page, setPage] = useState(1); const [items, setItems] = useState([]); const [total, setTotal] = useState(0); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [rangeError, setRangeError] = useState(''); // 筛选状态随组件挂载初始化:父组件按账户 key 重新挂载本抽屉,不会沿用上一个账户的筛选/页码。 // 请求序号:快速改筛选或翻页时只采纳最后一次请求的结果。 const seqRef = useRef(0); const load = useCallback(async () => { if (!visible || !account?.id) return; const [start, end] = range || []; if (start && end && start > end) { setRangeError(t('开始时间不能晚于结束时间')); return; } setRangeError(''); const seq = ++seqRef.current; setLoading(true); setError(''); try { const res = await fetchLedger(account.id, { start_timestamp: start ? Math.floor(start.getTime() / 1000) : undefined, end_timestamp: end ? Math.floor(end.getTime() / 1000) : undefined, type: types.length ? types.join(',') : undefined, p: page, page_size: PAGE_SIZE, }); if (seq !== seqRef.current) return; if (res?.data?.success) { setItems(res.data.data?.items || []); setTotal(res.data.data?.total || 0); } else { setError(res?.data?.message || t('加载失败,请重试')); } } catch { // 保留旧数据并标识刷新失败,不把旧数据冒充为最新 if (seq === seqRef.current) setError(t('数据刷新失败,请重试')); } finally { if (seq === seqRef.current) setLoading(false); } }, [visible, account?.id, range, types, page, fetchLedger, t]); useEffect(() => { load(); }, [load]); const isConsume = (r) => r.entry_type === 'consume' || r.entry_type === 'consume_adjust'; const columns = [ { title: t('时间'), dataIndex: 'created_at', width: 170, render: (v) => (v ? timestamp2string(v) : '—'), }, { title: t('类型'), dataIndex: 'entry_type', width: 120, render: (v, r) => ( {fundEntryLabel(t, v)} {r.status === FUND_LEDGER_PENDING && ( {t('结算中')} )} ), }, { title: t('金额'), dataIndex: 'amount', width: 140, align: 'right', render: (v, r) => ( {v > 0 ? '+' : v < 0 ? '-' : ''} {renderQuota(Math.abs(v), isConsume(r) ? 6 : 2)} ), }, { title: t('对方账户'), dataIndex: 'counterpart_name', render: (v, r) => ( {isConsume(r) ? t('系统消费') : v || '—'} ), }, { title: t('操作人'), dataIndex: 'operator_name', width: 120, render: (v, r) => { // 消费、结算调账等系统产生的行没有操作人 const name = r.operator_kind === 'platform' ? t('平台管理员') : isConsume(r) || !r.operator_user_id ? t('系统') : v || `#${r.operator_user_id}`; return ( {name} ); }, }, { title: t('操作后余额'), dataIndex: 'balance_after', width: 130, align: 'right', render: (v) => ( {renderQuota(v || 0, 2)} ), }, { title: t('备注'), render: (_, r) => { const badDebt = isConsume(r) ? r.amount_due + r.amount : 0; if (r.entry_type === 'consume' && badDebt > 0) { return ( {t('应扣 {{due}},未收回 {{bad}}', { due: renderQuota(r.amount_due, 6), bad: renderQuota(badDebt, 6), })} ); } return ( {r.ref_type === 'transfer' || isConsume(r) ? r.ref_id : ''} ); }, }, ]; const filtered = types.length > 0; return (
{/* 状态仍为 Date[](请求换算不变),仅在控件边界与 dayjs 互转 */} dayjs(d)) : null} onChange={(v) => { setRange(v ? v.map((d) => d.toDate()) : []); setPage(1); }} style={{ maxWidth: 380 }} />