SettlementRecordPages.tsx 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /**
  2. * 册 6《计费与对账出账》四页:FN-05 对账清单(14.1)、批次详情(14.2)、FN-08 账单明细与明细详情(14.3)。
  3. *
  4. * 接线状态(2026-11-11 核实):bos.recon/* 与 bos.bill/* 未在 openapi.yaml 声明 ⇒ 网关 11001 未声明即拒绝。
  5. * 撞 11001 ⇒ 契约占位分支(结构按 14 章落位、横幅标明待接线、数据区「后端未下发」);
  6. * ⛔ 用 settlement-demo/settlement-retries 本地台账冒充真接口(=绕过,本版起不再作为本页数据源)。
  7. * 文案唯一真源 ../lib/copy-billing(逐字复制自册 6 契约 14 章/05 章 §5.1,⛔ 手打转述);
  8. * 「分成方式与取值」列走 ../lib/settle-text 单源(册 4 settle_text,⛔ 本册自造分成取值文案)。
  9. * 禁项:⛔「按金额分成本期不计算」「未对账」「已打回」字样;⛔ 出账状态与批次状态混列;⛔ history.back()。
  10. */
  11. import { useEffect, useState, type FormEvent } from "react";
  12. import { PageIntro } from "../components/PageIntro";
  13. import { ArrowLeft, ArrowRight, Download, FileSpreadsheet, Search, X } from "lucide-react";
  14. import { Link, useLocation, useNavigate, useParams, useSearchParams } from "react-router";
  15. import { Button } from "../components/ui/button";
  16. import { SearchInput } from "../components/ui/search-input";
  17. import { FilterSelect } from "../components/ui/filter-select";
  18. import { MonthFilter } from "../components/ui/month-filter";
  19. import { ServerListPager } from "../components/ListPager";
  20. import { OpsNotice, OpsStatus, OpsTableColumns, OpsTableEmpty, OpsTag, type OpsTone } from "../components/OperationsKit";
  21. import { useDemoIdentity } from "../lib/demo-session";
  22. import { entTeamGetList, operatorGetList, partnerGetList, reconExportDetail, reconExportList } from "../lib/api/bos";
  23. import type { HolderType, ReconBatchStatus, ReconcileStatus, ReconDetailResult } from "../lib/api/types";
  24. import type { StatusTone } from "../domain/statuses";
  25. import { WORD_FETCH_FAILED } from "../lib/api/lockedCopy";
  26. import { settleTextFromSnapshot } from "../lib/settle-text";
  27. import {
  28. lastClosedMonth, submitReconRerun,
  29. useBillDetail, useBillList, useReconDetail, useReconList, useReconPeriods,
  30. } from "../lib/recon";
  31. import {
  32. ACTION_FLAG_PENDING_NOTE,
  33. ALL_PERIODS_LABEL,
  34. BACK_TO_BILL_DETAIL,
  35. BACK_TO_BILL_LIST,
  36. BACK_TO_CORRECTIONS,
  37. BACK_TO_RECON_DETAIL,
  38. BACK_TO_RECON_LIST,
  39. BACKEND_NOT_WIRED,
  40. BASE_FLOOR_NOTE,
  41. BATCH_STATUS_TEXT,
  42. BILL_WIRING_NOTE,
  43. COST_UNCONFIGURED_NOTE,
  44. EXECUTOR_SYSTEM,
  45. EXPORT_EMPTY_NOTE,
  46. exportResultNote,
  47. HOLDER_TYPE_TEXT,
  48. MONTH_NO_BATCH_NOTE,
  49. PERIODS_UNAVAILABLE,
  50. periodProgressLine,
  51. PROGRESS_COLLAPSE,
  52. PROGRESS_EXPAND,
  53. RECON_WIRING_NOTE,
  54. RECONCILE_STATUS_TEXT,
  55. RERUN_DIALOG_TITLE,
  56. RERUN_NO_EMPTY_BATCH_NOTE,
  57. RERUN_SUBMIT,
  58. RERUN_VOID_NOTE,
  59. rerunMonthBoundNote,
  60. rerunScopeLine,
  61. S404_EMPTY_NOTE,
  62. saveReconExportCsv,
  63. STAT_CARDS,
  64. TICKET_GAP_LABEL,
  65. ticketGapText,
  66. TRIGGER_TYPE_TEXT,
  67. VOIDED_BY_PREFIX,
  68. ZERO_NOTE_TEXT,
  69. } from "../lib/copy-billing";
  70. import "../styles/operations.css";
  71. import "../styles/settlement-records.css";
  72. /* ── 本地小工具 ─────────────────────────────────────────────────── */
  73. /** 金额列以 `_cents`(分)下发;界面统一两位小数元。 */
  74. const moneyFromCents = (cents: number): string =>
  75. `¥${(cents / 100).toLocaleString("zh-CN", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
  76. /** 三态格:null ⇒ 「取数失败」(KLT-S406 单格语义),⛔ 回落 0。 */
  77. const moneyCell = (cents: number | null): string => (cents == null ? WORD_FETCH_FAILED : moneyFromCents(cents));
  78. const reconcileTone: Record<ReconcileStatus, StatusTone> = { settled: "success", abnormal: "danger", unsettled: "neutral" };
  79. const batchTone: Record<ReconBatchStatus, StatusTone> = { pending: "neutral", executed: "success", failed: "danger", voided: "neutral" };
  80. const holderTone: Record<HolderType, OpsTone> = { operator: "brand", partner: "info", ops_team: "warning" };
  81. /**
  82. * 执行时间本地化:后端 `CAST(executed_at AS TEXT)` 下发 UTC 裸串(`2026-11-13 03:49:40.05+00`),
  83. * 直接入列既难读又与手册「执行时间」口径不符 ⇒ 按浏览器时区显示,原文留 `title` 可核。
  84. */
  85. function localTime(iso: string): string {
  86. const d = new Date(iso.includes("T") ? iso : iso.replace(" ", "T"));
  87. if (Number.isNaN(d.getTime())) return iso;
  88. const p = (n: number) => String(n).padStart(2, "0");
  89. return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`;
  90. }
  91. /**
  92. * 执行人可读化:优先绑定表用户名(`operator_username`);NULL ⇒「系统」(对账§14.2);
  93. * 只有裸 seoh 用户 ID 时标明是 ID,⛔ 把裸 ID 当人名直接渲染。
  94. */
  95. function executorText(e: { operator_user_id: string | null; operator_username?: string | null }): string {
  96. const name = e.operator_username?.trim();
  97. if (name) return name;
  98. return e.operator_user_id ? `用户 ${e.operator_user_id}` : EXECUTOR_SYSTEM;
  99. }
  100. const batchPath = (holderId: string, batchNo: string) =>
  101. `/settlement-batches/${encodeURIComponent(holderId)}/${encodeURIComponent(batchNo)}`;
  102. /** URL 搜索参数筛选(返回列表时保留条件;⛔ 路由级收敛另行处理)。 */
  103. function useListFilters() {
  104. const [params, setParams] = useSearchParams();
  105. const has = (key: string) => params.has(key);
  106. const get = (key: string) => params.get(key) ?? "";
  107. const set = (key: string, value: string) =>
  108. setParams((current) => {
  109. const next = new URLSearchParams(current);
  110. if (value) next.set(key, value);
  111. else next.delete(key);
  112. return next;
  113. }, { replace: true });
  114. const reset = () => setParams({}, { replace: true });
  115. return { has, get, set, reset };
  116. }
  117. /** 失败原因行末红点(fail_reason 原文悬浮;⚠️ 不是 KLT-* 码,对账§7.1 末格)。 */
  118. function FailDot({ reason }: { reason: string | null }) {
  119. if (!reason) return null;
  120. return <span style={{ color: "var(--destructive)", cursor: "help" }} title={reason}> ●</span>;
  121. }
  122. /* ── 人工补跑对话框(14.1 动作矩阵:仅 admin;入参只三件) ────────── */
  123. function RerunDialog({ onClose }: { onClose: () => void }) {
  124. const navigate = useNavigate();
  125. const location = useLocation();
  126. const [holderType, setHolderType] = useState<HolderType>("operator");
  127. const [holders, setHolders] = useState<{ id: string; name: string }[] | null>(null);
  128. const [holdersError, setHoldersError] = useState("");
  129. const [holderId, setHolderId] = useState("");
  130. const [period, setPeriod] = useState(lastClosedMonth());
  131. const [error, setError] = useState("");
  132. const [busy, setBusy] = useState(false);
  133. // 分成方候选=第 1 册三类档案真源列表口(⛔ 本地台账);切换类型时重拉。
  134. useEffect(() => {
  135. let alive = true;
  136. setHolders(null);
  137. setHoldersError("");
  138. setHolderId("");
  139. // ⚠️ bos 列表接口 `page` 以 0 为首页(同 `organization-demo.ts::pagedAll`)⇒ 传 1 会跳过首页,
  140. // dev 数据不足一页时候选恒空(补跑弹窗打不开)。
  141. const load =
  142. holderType === "operator"
  143. ? operatorGetList({ page: 0, page_size: 100 }).then((r) => (r.items ?? []).map((o) => ({ id: o.operator_id, name: o.name })))
  144. : holderType === "partner"
  145. ? partnerGetList({ page: 0, page_size: 100 }).then((r) => (r.items ?? []).map((p) => ({ id: p.partner_id, name: p.name })))
  146. : entTeamGetList({ page: 0, page_size: 100 }).then((r) => (r.items ?? []).map((t) => ({ id: t.team_id, name: t.team_name })));
  147. load
  148. .then((rows) => {
  149. if (!alive) return;
  150. setHolders(rows);
  151. setHolderId(rows[0]?.id ?? "");
  152. })
  153. .catch((e: unknown) => {
  154. if (alive) setHoldersError(e instanceof Error ? e.message : WORD_FETCH_FAILED);
  155. });
  156. return () => {
  157. alive = false;
  158. };
  159. }, [holderType]);
  160. function submit(event: FormEvent) {
  161. event.preventDefault();
  162. if (!holderId || !period) {
  163. setError(!holderId ? "请选择分成方。" : "请选择自然月。");
  164. return;
  165. }
  166. setBusy(true);
  167. setError("");
  168. submitReconRerun({ holder_type: holderType, holder_id: holderId, period })
  169. .then((res) => {
  170. onClose();
  171. navigate(batchPath(holderId, res.batch_no), { state: location.state });
  172. })
  173. .catch((e: unknown) => {
  174. // 四道闸(60502→60503→60504→60506)/KLT-S407:文案直读后端 message(出路句由服务端带出)。
  175. setError(e instanceof Error && e.message ? e.message : "补跑失败,请重试。");
  176. setBusy(false);
  177. });
  178. }
  179. // 弹窗须写明补跑对象(分成方名称+月份),⛔ 只回显 ID。
  180. const holderName = holders?.find((h) => h.id === holderId)?.name ?? holderId;
  181. return (
  182. <div className="ops-confirm-backdrop" role="presentation" onMouseDown={onClose}>
  183. <div className="ops-confirm" role="dialog" aria-modal="true" aria-labelledby="rerun-dialog-title" onMouseDown={(event) => event.stopPropagation()}>
  184. <h2 id="rerun-dialog-title">{RERUN_DIALOG_TITLE}</h2>
  185. <form onSubmit={submit}>
  186. <p style={{ margin: "12px 0" }}>
  187. <FilterSelect icon="type" aria-label="分成方类型" value={holderType} onChange={(event) => { setHolderType(event.target.value as HolderType); }}>
  188. <option value="operator">{HOLDER_TYPE_TEXT.operator}</option>
  189. <option value="partner">{HOLDER_TYPE_TEXT.partner}</option>
  190. <option value="ops_team">{HOLDER_TYPE_TEXT.ops_team}</option>
  191. </FilterSelect>{" "}
  192. {holders ? (
  193. <FilterSelect icon="operator" aria-label="分成方" value={holderId} onChange={(event) => setHolderId(event.target.value)}>
  194. {holders.map((h) => <option key={h.id} value={h.id}>{h.name}</option>)}
  195. {!holders.length && <option value="">(无可选分成方)</option>}
  196. </FilterSelect>
  197. ) : (
  198. <span>{holdersError || "加载中…"}</span>
  199. )}{" "}
  200. {/* 月份上界=上一个已结束自然月(对账§8.3 只可对已结束月份补跑);期次必填 ⇒ ⛔「全部自然月」。 */}
  201. <MonthFilter value={period} onChange={setPeriod} maxMonth={lastClosedMonth()} allowAll={false} />
  202. </p>
  203. <p>{rerunScopeLine(holderName, period)}</p>
  204. <p>{rerunMonthBoundNote(lastClosedMonth())}</p>
  205. <p>{RERUN_NO_EMPTY_BATCH_NOTE}</p>
  206. <p>{RERUN_VOID_NOTE}</p>
  207. {error && <p className="ops-form-error" role="alert">{error}</p>}
  208. <div>
  209. <Button variant="outline" type="button" onClick={onClose} disabled={busy}>取消</Button>
  210. <Button type="submit" disabled={busy || !holders?.length}>{busy ? "提交中…" : RERUN_SUBMIT}</Button>
  211. </div>
  212. </form>
  213. </div>
  214. </div>
  215. );
  216. }
  217. /* ── FN-05 对账清单(14.1) ─────────────────────────────────────── */
  218. export function SettlementStatementListPage() {
  219. const identity = useDemoIdentity();
  220. const location = useLocation();
  221. const filters = useListFilters();
  222. // 导出当前结果(B):admin/opsMain/opsSub(demo 角色无 opsSub);⛔ teamLead/partner/operator。
  223. const canExportAll = !identity || identity.role === "admin" || identity.role === "regAdmin";
  224. const isAdmin = !identity || identity.role === "admin";
  225. const defaultMonth = lastClosedMonth();
  226. // 自然月三态:参数缺席 ⇒ 默认预选 lastClosedMonth();显式 "all" ⇒ 全部自然月(空值项,⛔ 不得删除)。
  227. const monthAll = filters.get("month") === "all";
  228. const month = monthAll ? "" : filters.get("month") || defaultMonth;
  229. const type = filters.get("type");
  230. const party = filters.get("party");
  231. const reconcile = filters.get("reconcile");
  232. const status = filters.get("status");
  233. const trigger = filters.get("trigger");
  234. const zero = filters.get("zero") === "1";
  235. const q = filters.get("q");
  236. const page = Number(filters.get("page") || "1") || 1;
  237. const setMonth = (value: string) => {
  238. filters.set("page", "");
  239. filters.set("month", value === "" ? "all" : value === defaultMonth ? "" : value);
  240. };
  241. const hasFilter = Boolean(monthAll || filters.get("month") || type || party || reconcile || status || trigger || zero || q);
  242. const periodsFetch = useReconPeriods();
  243. // D-19:下拉选项=接口期次 ∪ 上一个已结束自然月(与人工补跑弹窗 `lastClosedMonth()` 同基准)。
  244. // 仅有历史批、尚无「上月」批时,仍须能选上月(可能空表),⛔ 静默退到更早月造成「弹窗说 10 月、清单只到 9 月」。
  245. const periods = (() => {
  246. const raw = periodsFetch.data?.periods ?? null;
  247. if (!raw) return null;
  248. if (raw.includes(defaultMonth)) return raw;
  249. return [defaultMonth, ...raw];
  250. })();
  251. // 仅当**用户显式选了**不在选项内的历史月(例如书签)才纠偏;默认预选月即使暂无批次也保留。
  252. const [monthFallback, setMonthFallback] = useState(false);
  253. useEffect(() => {
  254. if (!periods?.length) return;
  255. if (monthAll) {
  256. setMonthFallback(false);
  257. return;
  258. }
  259. if (!filters.has("month")) {
  260. setMonthFallback(false);
  261. return;
  262. }
  263. if (periods.includes(month)) {
  264. setMonthFallback(false);
  265. return;
  266. }
  267. filters.set("month", periods[0]);
  268. setMonthFallback(true);
  269. // eslint-disable-next-line react-hooks/exhaustive-deps
  270. }, [periods, month, monthAll]);
  271. const list = useReconList({
  272. period: month,
  273. holderType: type,
  274. holderKw: party,
  275. reconcileStatus: reconcile,
  276. batchStatus: status,
  277. trigger,
  278. includeZero: zero,
  279. keyword: q,
  280. page,
  281. });
  282. const data = list.data;
  283. const wiring = list.fail?.kind === "wiring";
  284. const s404 = list.fail?.kind === "s404";
  285. const rows = data?.items ?? [];
  286. const total = data?.total ?? 0;
  287. const pageSize = 20;
  288. const [notice, setNotice] = useState("");
  289. const [runOpen, setRunOpen] = useState(false);
  290. const [progressOpen, setProgressOpen] = useState(false);
  291. function exportAll() {
  292. // 筛选无结果 ⇒ 只提示、不导出、无错误码(14.1 动作矩阵)。
  293. if (!rows.length) {
  294. setNotice(EXPORT_EMPTY_NOTE);
  295. return;
  296. }
  297. reconExportList({ period: month || undefined, holder_type: type || undefined, status: status || undefined })
  298. .then((res) => setNotice(saveReconExportCsv(res) ? exportResultNote(res) : EXPORT_EMPTY_NOTE))
  299. .catch((e: unknown) => setNotice(e instanceof Error && e.message ? e.message : WORD_FETCH_FAILED));
  300. }
  301. return (
  302. <section className="ops-page reconcile-index page-fill page-fill-window">
  303. <header className="reconcile-page-heading">
  304. <div>
  305. <p className="ops-eyebrow"><span /> RECONCILIATION / 分成与对账</p>
  306. <h1>对账清单</h1>
  307. <PageIntro>每个账期出账后,按分成方核对批次状态和结算结果。</PageIntro>
  308. </div>
  309. <div className="statement-heading-actions">
  310. {isAdmin && <Button onClick={() => setRunOpen(true)}>人工补跑</Button>}
  311. </div>
  312. </header>
  313. {runOpen && <RerunDialog onClose={() => setRunOpen(false)} />}
  314. {/* 接线占位横幅(klt-bos recon/* 未交付;交付后本分支自然失效) */}
  315. {wiring && <p className="ops-import-note" role="status">{RECON_WIRING_NOTE}</p>}
  316. {periodsFetch.fail && !wiring && <p className="ops-import-note" role="status">{PERIODS_UNAVAILABLE}</p>}
  317. {monthFallback && !rows.length && <p className="ops-import-note" role="status">{MONTH_NO_BATCH_NOTE}</p>}
  318. {/* ② 三统计卡:与列表同一响应同一行集;S404 ⇒ 整体不渲染(不是显示 0);单格 null ⇒「取数失败」。 */}
  319. {!s404 && !wiring && data && (
  320. <div className="reconcile-totals" aria-label="当前筛选结果统计">
  321. {STAT_CARDS.map((card) => {
  322. const value =
  323. card.key === "base" ? data.stats.total_base_cents : card.key === "cost" ? data.stats.total_cost_cents : data.stats.total_share_cents;
  324. return (
  325. <div key={card.key}>
  326. <span>{card.title}</span>
  327. <strong>{moneyCell(value)}</strong>
  328. <small>{card.subtitle}</small>
  329. </div>
  330. );
  331. })}
  332. </div>
  333. )}
  334. {/* 契约占位分支:卡结构落位、值格显式标「后端未下发」(总则§16.9,⛔ 演示例数冒充)。 */}
  335. {wiring && (
  336. <div className="reconcile-totals" aria-label="当前筛选结果统计">
  337. {STAT_CARDS.map((card) => (
  338. <div key={card.key}>
  339. <span>{card.title}</span>
  340. <strong>{BACKEND_NOT_WIRED}</strong>
  341. <small>{card.subtitle}</small>
  342. </div>
  343. ))}
  344. </div>
  345. )}
  346. {/* ③-1 期次出账进度(常驻;只随自然月变化,⛔ 随其余筛选变;后端未下发 ⇒ 不渲)。 */}
  347. {data?.progress && (
  348. <>
  349. <div className="ops-detail-callout" role="status">
  350. <strong>{periodProgressLine(data.progress)}</strong>
  351. <p>
  352. <button type="button" className="ops-table-action" onClick={() => setProgressOpen((v) => !v)}>
  353. {progressOpen ? PROGRESS_COLLAPSE : PROGRESS_EXPAND} <ArrowRight size={13} />
  354. </button>
  355. </p>
  356. </div>
  357. {progressOpen && (
  358. <div className="ops-table-wrap">
  359. <table className="ops-table">
  360. <thead><tr><th>分成方</th><th>建档区域</th><th>对账日</th><th>本期应出账日期</th><th>状态</th><th>对账单号</th></tr></thead>
  361. <tbody>
  362. {data.progress.items.map((item) => (
  363. <tr key={`${item.holder_type}-${item.holder_id}`}>
  364. <td><OpsTag tone={holderTone[item.holder_type]}>{HOLDER_TYPE_TEXT[item.holder_type]}</OpsTag> {item.holder_name}</td>
  365. <td>{item.region_label ?? "—"}</td>
  366. <td>{item.recon_day} 日</td>
  367. <td>{item.expected_date}</td>
  368. <td>{item.status}</td>
  369. <td className="ops-mono">{item.batch_no ?? "—"}</td>
  370. </tr>
  371. ))}
  372. </tbody>
  373. </table>
  374. </div>
  375. )}
  376. </>
  377. )}
  378. {/* ④ 批次清单 */}
  379. <div className="reconcile-ledger list-shell-fill">
  380. {/* ③ 筛选条:自然月(periods 口)/分成方类型/分成方(可搜)/出账状态/批次状态/对账单号关键字/零明细开关/重置筛选。 */}
  381. <div className="reconcile-filters">
  382. <label className="ops-search">
  383. <Search size={17} />
  384. <SearchInput aria-label="搜索对账单号" placeholder="搜索对账单号" value={q}
  385. onCommit={(value) => { filters.set("page", ""); filters.set("q", value); }} />
  386. {q && <button type="button" aria-label="清空搜索" onClick={() => filters.set("q", "")}><X size={15} /></button>}
  387. </label>
  388. <FilterSelect icon="month" aria-label="自然月" value={monthAll ? "" : month} onChange={(event) => setMonth(event.target.value)}>
  389. <option value="">{ALL_PERIODS_LABEL}</option>
  390. {(periods ?? (month ? [month] : [])).map((p) => <option key={p} value={p}>{p}</option>)}
  391. </FilterSelect>
  392. <FilterSelect icon="type" aria-label="分成方类型" value={type} onChange={(event) => { filters.set("page", ""); filters.set("type", event.target.value); }}>
  393. <option value="">全部类型</option>
  394. <option value="operator">{HOLDER_TYPE_TEXT.operator}</option>
  395. <option value="partner">{HOLDER_TYPE_TEXT.partner}</option>
  396. <option value="ops_team">{HOLDER_TYPE_TEXT.ops_team}</option>
  397. </FilterSelect>
  398. <label className="ops-search">
  399. <Search size={17} />
  400. <SearchInput aria-label="搜索分成方" placeholder="搜索分成方" value={party}
  401. onCommit={(value) => { filters.set("page", ""); filters.set("party", value); }} />
  402. </label>
  403. {/* 出账状态与批次状态是两个独立下拉(⚠️ 合并=缺陷);出账状态⛔ 出现「待执行」「已作废」。 */}
  404. <FilterSelect icon="status" aria-label="出账状态" value={reconcile} onChange={(event) => { filters.set("page", ""); filters.set("reconcile", event.target.value); }}>
  405. <option value="">全部出账状态</option>
  406. <option value="settled">{RECONCILE_STATUS_TEXT.settled}</option>
  407. <option value="abnormal">{RECONCILE_STATUS_TEXT.abnormal}</option>
  408. <option value="unsettled">{RECONCILE_STATUS_TEXT.unsettled}</option>
  409. </FilterSelect>
  410. <FilterSelect icon="action" aria-label="批次状态" value={status} onChange={(event) => { filters.set("page", ""); filters.set("status", event.target.value); }}>
  411. <option value="">全部批次状态</option>
  412. <option value="pending">{BATCH_STATUS_TEXT.pending}</option>
  413. <option value="executed">{BATCH_STATUS_TEXT.executed}</option>
  414. <option value="failed">{BATCH_STATUS_TEXT.failed}</option>
  415. <option value="voided">{BATCH_STATUS_TEXT.voided}</option>
  416. </FilterSelect>
  417. {/* 触发方式(手册步序:批次状态之后、关键字之前):只两值,⛔ 第三值。 */}
  418. <FilterSelect icon="action" aria-label="触发方式" value={trigger} onChange={(event) => { filters.set("page", ""); filters.set("trigger", event.target.value); }}>
  419. <option value="">全部触发方式</option>
  420. {(["manual", "auto"] as const).map((key) => <option key={key} value={key}>{TRIGGER_TYPE_TEXT[key]}</option>)}
  421. </FilterSelect>
  422. <Button variant={zero ? "default" : "outline"} size="sm" aria-pressed={zero}
  423. onClick={() => { filters.set("page", ""); filters.set("zero", zero ? "" : "1"); }}>零明细批次</Button>
  424. {hasFilter && <Button variant="ghost" size="sm" onClick={() => { filters.reset(); setMonthFallback(false); }}>重置筛选</Button>}
  425. {canExportAll && <Button variant="outline" size="sm" onClick={exportAll}><Download size={16} />导出当前结果</Button>}
  426. </div>
  427. {/* S404 触顶 ⇒ 不渲染三卡与列表(⚠️ 禁止静默截断)。 */}
  428. {s404 ? (
  429. <p className="ops-import-note" role="alert">{S404_EMPTY_NOTE}</p>
  430. ) : (
  431. <div className="ops-table-wrap">
  432. <table className="ops-table reconcile-table">
  433. <OpsTableColumns widths={[150, 190, 90, 100, 90, 80, 110, 110, 96, 96, 90, 76]} />
  434. <thead>
  435. <tr><th>对账单号</th><th>分成方</th><th>自然月</th><th>应出账日期</th><th>触发方式</th><th>明细条数</th><th>总金额</th><th>分成金额</th><th>出账状态</th><th>批次状态</th><th>执行人</th><th>操作</th></tr>
  436. </thead>
  437. <tbody>
  438. {rows.map((row) => (
  439. <tr key={row.batch_no}>
  440. <td>
  441. <Link className="reconcile-row-link" title={row.batch_no} to={batchPath(row.holder_id, row.batch_no)}
  442. state={{ from: "/settlement-batches", listSearch: location.search }}>
  443. {row.batch_no}<ArrowRight size={14} />
  444. </Link>
  445. </td>
  446. <td><OpsTag tone={holderTone[row.holder_type]}>{HOLDER_TYPE_TEXT[row.holder_type]}</OpsTag> <strong title={row.holder_name}>{row.holder_name}</strong></td>
  447. <td><span className="ops-mono">{row.period}</span></td>
  448. <td>{row.recon_date}</td>
  449. <td><OpsTag tone={row.trigger_type === "manual" ? "warning" : "neutral"}>{TRIGGER_TYPE_TEXT[row.trigger_type]}</OpsTag></td>
  450. <td className="table-number">{row.detail_count} 条</td>
  451. <td><span className="ops-money">{moneyFromCents(row.base_cents)}</span></td>
  452. <td><strong className="reconcile-money">{moneyFromCents(row.share_cents)}</strong></td>
  453. <td><OpsStatus tone={reconcileTone[row.reconcile_status]}>{RECONCILE_STATUS_TEXT[row.reconcile_status]}</OpsStatus></td>
  454. <td>
  455. <OpsStatus tone={batchTone[row.status]}>{BATCH_STATUS_TEXT[row.status]}</OpsStatus>
  456. {row.status === "failed" && <FailDot reason={row.fail_reason} />}
  457. </td>
  458. <td title={row.operator_user_id ?? undefined}>{executorText(row)}</td>
  459. <td>
  460. <Link className="ops-table-action" to={batchPath(row.holder_id, row.batch_no)}
  461. state={{ from: "/settlement-batches", listSearch: location.search }}>详情</Link>
  462. </td>
  463. </tr>
  464. ))}
  465. {!rows.length && (
  466. <OpsTableEmpty colSpan={12}>
  467. {list.loading ? "加载中…" : wiring ? "对账接口待后端接线,批次数据暂不可读。" : "没有符合筛选条件的对账清单。"}
  468. </OpsTableEmpty>
  469. )}
  470. </tbody>
  471. </table>
  472. </div>
  473. )}
  474. {!s404 && (
  475. <ServerListPager
  476. total={total}
  477. page={page}
  478. pageSize={pageSize}
  479. onPageChange={(next) => filters.set("page", String(next))}
  480. />
  481. )}
  482. </div>
  483. {list.fail && !wiring && !s404 && (
  484. <p className="ops-form-error" role="alert">{list.fail.message} <Button variant="ghost" size="sm" onClick={list.reload}>重试</Button></p>
  485. )}
  486. {notice && <OpsNotice message={notice} onClose={() => setNotice("")} />}
  487. </section>
  488. );
  489. }
  490. /* ── 批次详情(14.2) ───────────────────────────────────────────── */
  491. export function SettlementStatementRecordPage() {
  492. const identity = useDemoIdentity();
  493. const navigate = useNavigate();
  494. const location = useLocation();
  495. const { no } = useParams();
  496. const state = (location.state ?? {}) as { from?: string; listSearch?: string };
  497. // 返回恒回进来那一页(总则§16.11;⛔ history.back());账单明细来路带 listSearch 保留筛选。
  498. const back = state.from === "/correction-notes"
  499. ? { to: "/correction-notes", label: BACK_TO_CORRECTIONS }
  500. : state.from?.startsWith("/settlement-bills/")
  501. ? { to: state.from, label: BACK_TO_BILL_DETAIL }
  502. : state.from === "/settlement-bills"
  503. ? { to: `/settlement-bills${state.listSearch ?? ""}`, label: BACK_TO_BILL_LIST }
  504. : { to: `/settlement-batches${state.listSearch ?? ""}`, label: BACK_TO_RECON_LIST };
  505. const detail = useReconDetail(no ?? null);
  506. const d = detail.data;
  507. const wiring = detail.fail?.kind === "wiring";
  508. const isAdmin = !identity || identity.role === "admin";
  509. const [confirmRerun, setConfirmRerun] = useState(false);
  510. const [rerunError, setRerunError] = useState("");
  511. const [busy, setBusy] = useState(false);
  512. const [notice, setNotice] = useState("");
  513. function runRerun() {
  514. if (!d) return;
  515. setBusy(true);
  516. setRerunError("");
  517. submitReconRerun({ holder_type: d.holder_type, holder_id: d.holder_id, period: d.period })
  518. .then((res) => {
  519. setConfirmRerun(false);
  520. // 补跑成功 ⇒ 原批次作废、生成新批;直接落到新批详情(来路保留)。
  521. navigate(batchPath(d.holder_id, res.batch_no), { state: location.state, replace: true });
  522. })
  523. .catch((e: unknown) => {
  524. // 四道闸(60502→60503→60504→60506)/KLT-S407:文案直读后端 message。
  525. setRerunError(e instanceof Error && e.message ? e.message : "补跑失败,请重试。");
  526. setBusy(false);
  527. });
  528. }
  529. function exportDetail() {
  530. if (!d) return;
  531. reconExportDetail({ batch_id: d.batch_no })
  532. .then((res) => setNotice(saveReconExportCsv(res) ? exportResultNote(res) : EXPORT_EMPTY_NOTE))
  533. .catch((e: unknown) => setNotice(e instanceof Error && e.message ? e.message : WORD_FETCH_FAILED));
  534. }
  535. return (
  536. <article className="ops-page reconcile-record">
  537. <Link className="ops-back-link" to={back.to}><ArrowLeft size={16} />{back.label}</Link>
  538. {wiring && <p className="ops-import-note" role="status">{RECON_WIRING_NOTE}</p>}
  539. {detail.fail && !wiring && (
  540. <p className="ops-form-error" role="alert">{detail.fail.message} <Button variant="ghost" size="sm" onClick={detail.reload}>重试</Button></p>
  541. )}
  542. {/* 契约占位分支:八字段头结构落位、值格「后端未下发」。 */}
  543. {(wiring || (!d && detail.loading)) && (
  544. <header className="reconcile-record-hero">
  545. <div className="reconcile-record-symbol"><FileSpreadsheet size={27} /></div>
  546. <div>
  547. <span className="reconcile-record-eyebrow">RECONCILIATION / {no}</span>
  548. <h1>批次详情</h1>
  549. <p>{detail.loading && !wiring ? "加载中…" : BACKEND_NOT_WIRED}</p>
  550. </div>
  551. </header>
  552. )}
  553. {d && <BatchDetailBody
  554. d={d}
  555. isAdmin={isAdmin}
  556. onRerun={() => { setRerunError(""); setConfirmRerun(true); }}
  557. onExport={exportDetail}
  558. onCorrect={() => navigate("/correction-notes", { state: { ref: d.batch_no } })}
  559. lineState={{ from: location.pathname }}
  560. />}
  561. {confirmRerun && d && (
  562. <div className="ops-confirm-backdrop" role="presentation" onMouseDown={() => !busy && setConfirmRerun(false)}>
  563. <div className="ops-confirm" role="alertdialog" aria-modal="true" aria-labelledby="rerun-confirm-title" onMouseDown={(event) => event.stopPropagation()}>
  564. <h2 id="rerun-confirm-title">{RERUN_DIALOG_TITLE}</h2>
  565. <p>{rerunScopeLine(d.holder_name, d.period)}</p>
  566. {/* failed 批补跑区追加句(§8.6 界面唯一落点之一)。 */}
  567. {d.status === "failed" && <p>{RERUN_VOID_NOTE}</p>}
  568. {rerunError && <p className="ops-form-error" role="alert">{rerunError}</p>}
  569. <div>
  570. <Button variant="outline" onClick={() => setConfirmRerun(false)} disabled={busy}>取消</Button>
  571. <Button onClick={runRerun} disabled={busy}>{busy ? "提交中…" : RERUN_SUBMIT}</Button>
  572. </div>
  573. </div>
  574. </div>
  575. )}
  576. {notice && <OpsNotice message={notice} onClose={() => setNotice("")} />}
  577. </article>
  578. );
  579. }
  580. function BatchDetailBody({ d, isAdmin, onRerun, onExport, onCorrect, lineState }: {
  581. d: ReconDetailResult;
  582. isAdmin: boolean;
  583. onRerun: () => void;
  584. onExport: () => void;
  585. onCorrect: () => void;
  586. lineState: { from: string };
  587. }) {
  588. // 动作可用性=服务端布尔位(与对账§5.5.4 同源常量;⚠️ 前端按角色自猜=第二套判据)。
  589. // null ⇒ 后端未下发 ⇒ 禁用+「可操作性位后端未下发」说明。
  590. const rerunEnabled = isAdmin && d.can_rerun === true;
  591. const exportEnabled = d.can_export === true;
  592. const correctEnabled = isAdmin && d.can_correct === true;
  593. const flagPending = d.can_rerun == null || d.can_export == null || d.can_correct == null;
  594. return (
  595. <>
  596. <header className="reconcile-record-hero">
  597. <div className="reconcile-record-symbol"><FileSpreadsheet size={27} /></div>
  598. <div>
  599. <span className="reconcile-record-eyebrow">RECONCILIATION / {d.batch_no}</span>
  600. <h1>批次详情</h1>
  601. <p>
  602. <strong>{d.holder_name}</strong>
  603. <span>{HOLDER_TYPE_TEXT[d.holder_type]}</span>
  604. <span>自然月 {d.period}</span>
  605. <span>{TRIGGER_TYPE_TEXT[d.trigger_type]}触发 · {executorText(d)}执行</span>
  606. </p>
  607. <div className="reconcile-hero-pills">
  608. <OpsStatus tone={batchTone[d.status]}>{BATCH_STATUS_TEXT[d.status]}</OpsStatus>
  609. <OpsTag tone={d.trigger_type === "manual" ? "warning" : "neutral"}>{TRIGGER_TYPE_TEXT[d.trigger_type]}触发</OpsTag>
  610. </div>
  611. </div>
  612. </header>
  613. <div className="reconcile-record-stats">
  614. <div><span>明细条数</span><strong>{d.detail_count}</strong><small>本批有效行现算,作废行不计</small></div>
  615. <div><span>总金额</span><strong>{moneyFromCents(d.base_cents)}</strong><small>本批有效行应收基数合计</small></div>
  616. <div><span>分成金额</span><strong>{moneyFromCents(d.share_cents)}</strong><small>本批有效行分成金额合计</small></div>
  617. <div>
  618. <span>{TICKET_GAP_LABEL}</span>
  619. <strong>{d.ticket_gap_failed ? WORD_FETCH_FAILED : d.ticket_gap_count == null ? BACKEND_NOT_WIRED : ticketGapText(d.ticket_gap_count)}</strong>
  620. <small>{TICKET_GAP_LABEL}按工单台账现算(0 个缺口与取数失败是两回事)</small>
  621. </div>
  622. </div>
  623. <section className="reconcile-record-section">
  624. <div className="reconcile-record-section-head">
  625. <div><h2>执行结果</h2><p>批次动作与执行说明。</p></div>
  626. <div style={{ display: "flex", gap: 8 }}>
  627. {isAdmin && (
  628. <Button variant="outline" onClick={onRerun} disabled={!rerunEnabled} title={d.can_rerun == null ? ACTION_FLAG_PENDING_NOTE : undefined}>人工补跑</Button>
  629. )}
  630. <Button variant="outline" onClick={onExport} disabled={!exportEnabled} title={d.can_export == null ? ACTION_FLAG_PENDING_NOTE : undefined}>
  631. <Download size={15} />导出本批账单明细
  632. </Button>
  633. {isAdmin && (
  634. <Button variant="outline" onClick={onCorrect} disabled={!correctEnabled} title={d.can_correct == null ? ACTION_FLAG_PENDING_NOTE : undefined}>登记更正</Button>
  635. )}
  636. </div>
  637. </div>
  638. {flagPending && <p className="reconcile-footnote">{ACTION_FLAG_PENDING_NOTE},动作暂不可用。</p>}
  639. {/* 失败原因只在 failed/voided 时渲染;voided 必带「由哪一次补跑作废」链接。 */}
  640. {(d.status === "failed" || d.status === "voided") && d.fail_reason && (
  641. <p className="ops-form-error" role="alert">{d.fail_reason}</p>
  642. )}
  643. {d.status === "voided" && d.voided_by_batch_no && (
  644. <p className="reconcile-footnote">
  645. {VOIDED_BY_PREFIX}
  646. <Link className="ops-table-action" to={batchPath(d.holder_id, d.voided_by_batch_no)}>{d.voided_by_batch_no} <ArrowRight size={14} /></Link>
  647. 作废。
  648. </p>
  649. )}
  650. {/* 成本未配置提示一行(不发通知、不建提醒表;不说"异常")。 */}
  651. {d.cost_unconfigured.length > 0 && (
  652. <p className="reconcile-footnote">{COST_UNCONFIGURED_NOTE}:{d.cost_unconfigured.join("、")}</p>
  653. )}
  654. </section>
  655. <section className="reconcile-record-section">
  656. <div className="reconcile-record-section-head">
  657. <span>01</span>
  658. <div><h2>本批账单明细</h2><p>{d.holder_name} · {d.period} · {d.detail_count} 条</p></div>
  659. </div>
  660. <div className="ops-table-wrap">
  661. <table className="ops-table" style={{ minWidth: 980 }}>
  662. <thead><tr><th>客户</th><th>订阅标识</th><th>应收金额</th><th>分成基数</th><th>分成方式与取值</th><th>本方分成金额</th><th>恒 0 说明</th><th>操作</th></tr></thead>
  663. <tbody>
  664. {d.items.map((line) => (
  665. <tr key={line.bill_detail_id}>
  666. <td><strong>{line.customer_name}</strong></td>
  667. <td>{line.subscription_label}</td>
  668. <td><span className="ops-money">{moneyFromCents(line.receivable_cents)}</span></td>
  669. <td><span className="ops-money">{moneyFromCents(line.share_base_cents)}</span></td>
  670. <td className="table-number">{settleTextFromSnapshot(line.share_mode, line.share_value)}</td>
  671. <td><strong className="reconcile-money">{moneyFromCents(line.share_amount_cents)}</strong></td>
  672. {/* zero_note 如实展示并解释为什么是 0(⛔ 隐藏 0 金额行)。 */}
  673. <td>{line.zero_note ? ZERO_NOTE_TEXT[line.zero_note] : "—"}</td>
  674. <td>
  675. <Link className="ops-table-action" to={`/settlement-bills/${encodeURIComponent(line.bill_detail_id)}`} state={lineState}>
  676. 详情
  677. </Link>
  678. </td>
  679. </tr>
  680. ))}
  681. {!d.items.length && <OpsTableEmpty colSpan={8}>本批无明细行。</OpsTableEmpty>}
  682. </tbody>
  683. </table>
  684. </div>
  685. </section>
  686. <section className="reconcile-record-section">
  687. <div className="reconcile-record-section-head"><span>02</span><div><h2>执行与口径</h2><p>批次八字段头与状态说明。</p></div></div>
  688. <dl className="reconcile-record-facts">
  689. <div><dt>对账单号</dt><dd>{d.batch_no}</dd></div>
  690. <div><dt>分成方</dt><dd>{d.holder_name} · {HOLDER_TYPE_TEXT[d.holder_type]}</dd></div>
  691. <div><dt>自然月</dt><dd>{d.period}</dd></div>
  692. <div><dt>应出账日期</dt><dd title="该分成方该自然月的应出账日期=自然月次月的对账日;「每月 N 日」的对账日口径见「出账配置」">{d.recon_date ?? "—"}</dd></div>
  693. <div><dt>批次状态</dt><dd>{BATCH_STATUS_TEXT[d.status]}</dd></div>
  694. <div><dt>执行人</dt><dd title={d.operator_user_id ?? undefined}>{executorText(d)}</dd></div>
  695. <div><dt>执行时间</dt><dd title={d.executed_at ?? undefined}>{d.executed_at ? localTime(d.executed_at) : "—"}</dd></div>
  696. <div><dt>触发方式</dt><dd>{TRIGGER_TYPE_TEXT[d.trigger_type]}</dd></div>
  697. </dl>
  698. <p className="reconcile-footnote">状态以批次当前记录为准。执行失败的批次由总运营补跑;账单差错通过出账更正登记。</p>
  699. </section>
  700. </>
  701. );
  702. }
  703. /* ── FN-08 账单明细(14.3 列表) ─────────────────────────────────── */
  704. export function SettlementBillListPage() {
  705. const location = useLocation();
  706. const filters = useListFilters();
  707. const q = filters.get("q");
  708. const type = filters.get("type");
  709. const month = filters.get("month");
  710. const page = Number(filters.get("page") || "1") || 1;
  711. const hasFilter = Boolean(q || type || month);
  712. // 详情/批次返回时拼回本串,保留筛选(D-2;对齐对账清单 listSearch)。
  713. const listNavState = { from: "/settlement-bills" as const, listSearch: location.search };
  714. const periodsFetch = useReconPeriods();
  715. const periods = periodsFetch.data?.periods ?? null;
  716. const list = useBillList({ period: month, holderType: type, keyword: q, page });
  717. const wiring = list.fail?.kind === "wiring";
  718. const rows = list.data?.items ?? [];
  719. const total = list.data?.total ?? 0;
  720. const pageSize = 20;
  721. return (
  722. <section className="ops-page bill-index page-fill page-fill-window">
  723. <header className="reconcile-page-heading">
  724. <div>
  725. <p className="ops-eyebrow"><span /> BILLING LINES / 分成与对账</p>
  726. <h1>账单明细</h1>
  727. <PageIntro>对账有差异时,按客户和账期追到具体订阅,逐项核对应收、成本和分成。</PageIntro>
  728. </div>
  729. </header>
  730. {wiring && <p className="ops-import-note" role="status">{BILL_WIRING_NOTE}</p>}
  731. <div className="reconcile-ledger list-shell-fill">
  732. <div className="reconcile-filters">
  733. <label className="ops-search">
  734. <Search size={17} />
  735. <SearchInput aria-label="搜索账单明细" placeholder="搜索分成方、客户、订阅或单号" value={q}
  736. onCommit={(value) => { filters.set("page", ""); filters.set("q", value); }} />
  737. {q && <button type="button" aria-label="清空搜索" onClick={() => filters.set("q", "")}><X size={15} /></button>}
  738. </label>
  739. <FilterSelect icon="type" aria-label="分成方类型" value={type} onChange={(event) => { filters.set("page", ""); filters.set("type", event.target.value); }}>
  740. <option value="">全部类型</option>
  741. <option value="operator">{HOLDER_TYPE_TEXT.operator}</option>
  742. <option value="partner">{HOLDER_TYPE_TEXT.partner}</option>
  743. <option value="ops_team">{HOLDER_TYPE_TEXT.ops_team}</option>
  744. </FilterSelect>
  745. <FilterSelect icon="month" aria-label="自然月" value={month} onChange={(event) => { filters.set("page", ""); filters.set("month", event.target.value); }}>
  746. <option value="">{ALL_PERIODS_LABEL}</option>
  747. {(periods ?? []).map((p) => <option key={p} value={p}>{p}</option>)}
  748. </FilterSelect>
  749. {hasFilter && <Button variant="ghost" size="sm" onClick={filters.reset}>重置筛选</Button>}
  750. </div>
  751. <div className="ops-table-wrap">
  752. <table className="ops-table">
  753. <OpsTableColumns widths={[180, 90, 170, 200, 90, 120, 130, 160, 76]} />
  754. <thead><tr><th>分成方</th><th>自然月</th><th>客户</th><th>订阅</th><th>计费天数</th><th>分成基数</th><th>本方分成金额</th><th>对账单号</th><th>操作</th></tr></thead>
  755. <tbody>
  756. {rows.map((row) => (
  757. <tr key={row.bill_detail_id}>
  758. <td><OpsTag tone={holderTone[row.holder_type]}>{HOLDER_TYPE_TEXT[row.holder_type]}</OpsTag> <strong className="table-cell-title">{row.holder_name}</strong></td>
  759. <td><span className="ops-mono">{row.period}</span></td>
  760. <td>{row.customer_name}</td>
  761. <td>{row.subscription_label}</td>
  762. <td className="table-number">{row.billing_days} 天</td>
  763. <td><span className="ops-money">{moneyFromCents(row.share_base_cents)}</span></td>
  764. <td><strong className="reconcile-money">{moneyFromCents(row.share_amount_cents)}</strong></td>
  765. <td>
  766. <Link className="reconcile-row-link" title={row.batch_no} to={batchPath(row.holder_id, row.batch_no)} state={listNavState}>
  767. {row.batch_no}<ArrowRight size={14} />
  768. </Link>
  769. </td>
  770. <td>
  771. <Link className="ops-table-action" to={`/settlement-bills/${encodeURIComponent(row.bill_detail_id)}`} state={listNavState}>
  772. 详情
  773. </Link>
  774. </td>
  775. </tr>
  776. ))}
  777. {!rows.length && (
  778. <OpsTableEmpty colSpan={9}>
  779. {list.loading ? "加载中…" : wiring ? "账单明细接口待后端接线,数据暂不可读。" : "没有符合筛选条件的账单明细。"}
  780. </OpsTableEmpty>
  781. )}
  782. </tbody>
  783. </table>
  784. </div>
  785. <ServerListPager
  786. total={total}
  787. page={page}
  788. pageSize={pageSize}
  789. onPageChange={(next) => filters.set("page", String(next))}
  790. />
  791. </div>
  792. {list.fail && !wiring && (
  793. <p className="ops-form-error" role="alert">{list.fail.message} <Button variant="ghost" size="sm" onClick={list.reload}>重试</Button></p>
  794. )}
  795. </section>
  796. );
  797. }
  798. /* ── FN-08 明细详情(14.3 七段;字段按「钱是怎么来的」排序) ────────── */
  799. export function SettlementBillRecordPage() {
  800. const { index } = useParams();
  801. const location = useLocation();
  802. const state = (location.state ?? {}) as { from?: string; listSearch?: string };
  803. // 来自列表时拼回 listSearch,保留筛选(D-2);其它来路仍回进来那一页。
  804. const back = state.from === "/correction-notes"
  805. ? { to: "/correction-notes", label: BACK_TO_CORRECTIONS }
  806. : state.from?.startsWith("/settlement-batches/")
  807. ? { to: state.from, label: BACK_TO_RECON_DETAIL }
  808. : { to: `/settlement-bills${state.from === "/settlement-bills" ? (state.listSearch ?? "") : ""}`, label: BACK_TO_BILL_LIST };
  809. const detail = useBillDetail(index ?? null);
  810. const d = detail.data;
  811. const wiring = detail.fail?.kind === "wiring";
  812. return (
  813. <article className="ops-page reconcile-record">
  814. <Link className="ops-back-link" to={back.to}><ArrowLeft size={16} />{back.label}</Link>
  815. {wiring && <p className="ops-import-note" role="status">{BILL_WIRING_NOTE}</p>}
  816. {detail.fail && !wiring && (
  817. <p className="ops-form-error" role="alert">{detail.fail.message} <Button variant="ghost" size="sm" onClick={detail.reload}>重试</Button></p>
  818. )}
  819. {(wiring || (!d && detail.loading)) && (
  820. <header className="reconcile-record-hero">
  821. <div className="reconcile-record-symbol"><FileSpreadsheet size={27} /></div>
  822. <div>
  823. <span className="reconcile-record-eyebrow">BILLING LINE / {index}</span>
  824. <h1>明细详情</h1>
  825. <p>{detail.loading && !wiring ? "加载中…" : BACKEND_NOT_WIRED}</p>
  826. </div>
  827. </header>
  828. )}
  829. {d && (
  830. <>
  831. <header className="reconcile-record-hero">
  832. <div className="reconcile-record-symbol"><FileSpreadsheet size={27} /></div>
  833. <div>
  834. <span className="reconcile-record-eyebrow">BILLING LINE / {d.bill_detail_id}</span>
  835. <h1>明细详情</h1>
  836. <p><strong>{d.holder_name}</strong><span>{HOLDER_TYPE_TEXT[d.holder_type]}</span><span>自然月 {d.period}</span><span>{d.customer_name} · {d.subscription_label}</span></p>
  837. </div>
  838. </header>
  839. {/* ① 本期应收口径:pre_align_cents 与 receivable_cents 同屏(缺一=页面缺字段)。 */}
  840. <section className="reconcile-record-section">
  841. <div className="reconcile-record-section-head"><span>01</span><div><h2>本期应收口径</h2><p>尾月对齐前两值必须同屏。</p></div></div>
  842. <dl className="reconcile-record-facts">
  843. <div><dt>计费标准单价</dt><dd>{d.settle_std_cents == null ? "—" : moneyFromCents(d.settle_std_cents)}</dd></div>
  844. <div><dt>订阅份数</dt><dd>{d.quantity == null ? "—" : d.quantity}</dd></div>
  845. <div><dt>尾月对齐前应收</dt><dd>{moneyFromCents(d.pre_align_cents)}</dd></div>
  846. <div><dt>本期应收(对齐后)</dt><dd>{moneyFromCents(d.receivable_cents)}</dd></div>
  847. </dl>
  848. {d.receivable_calc && <p className="reconcile-footnote">{d.receivable_calc}</p>}
  849. </section>
  850. {/* ② 计费区间与天数、是否不满月。 */}
  851. <section className="reconcile-record-section">
  852. <div className="reconcile-record-section-head"><span>02</span><div><h2>计费依据</h2><p>本期计费区间与天数。</p></div></div>
  853. <dl className="reconcile-record-facts">
  854. <div><dt>计费区间</dt><dd>{(d.bill_from || d.bill_to) ? `${d.bill_from || "—"} ~ ${d.bill_to || "—"}` : "—"}</dd></div>
  855. <div><dt>计费天数</dt><dd>{d.billing_days} 天</dd></div>
  856. <div><dt>当月天数</dt><dd>{d.month_days == null ? "—" : `${d.month_days} 天`}</dd></div>
  857. <div><dt>是否不满月</dt><dd>{d.partial_month ? "是" : "否"}</dd></div>
  858. </dl>
  859. </section>
  860. {/* ③ 成本:合计取实扣;明细列为应扣;封顶时提示。 */}
  861. <section className="reconcile-record-section">
  862. <div className="reconcile-record-section-head"><span>03</span><div><h2>成本快照</h2><p>合计为实扣;明细行为应扣(封顶前)。</p></div></div>
  863. <dl className="reconcile-record-facts">
  864. <div><dt>成本实扣</dt><dd>{moneyFromCents(d.cost_deduct_cents ?? (d.receivable_cents - d.share_base_cents))}</dd></div>
  865. {d.cost_should_deduct_cents != null && (
  866. <div><dt>成本应扣</dt><dd>{moneyFromCents(d.cost_should_deduct_cents)}</dd></div>
  867. )}
  868. </dl>
  869. {(d.cost_should_deduct_cents != null
  870. && d.cost_deduct_cents != null
  871. && d.cost_should_deduct_cents > d.cost_deduct_cents)
  872. || d.base_floor_protected
  873. ? <p className="reconcile-footnote">成本已按应收封顶(应扣大于实扣时,分成基数按 0 保护)。</p>
  874. : null}
  875. {d.costs.length ? (
  876. <div className="ops-table-wrap">
  877. <table className="ops-table">
  878. <thead><tr><th>成本项</th><th>单价</th><th>倍率</th><th>应扣</th></tr></thead>
  879. <tbody>
  880. {d.costs.map((cost, i) => {
  881. const price = cost.price_cents ?? cost.amount_cents;
  882. const should = cost.cost_cents ?? cost.fold ?? null;
  883. return (
  884. <tr key={`${cost.item_name}-${i}`}>
  885. <td>{cost.item_name}{!cost.configured && <OpsTag tone="warning">未配置</OpsTag>}</td>
  886. <td><span className="ops-money table-number">{price == null ? "—" : moneyFromCents(price)}</span></td>
  887. <td>{cost.multiplier == null ? "—" : <span className="table-number">{cost.multiplier}</span>}</td>
  888. <td><span className="ops-money table-number">{should == null ? "—" : moneyFromCents(should)}</span></td>
  889. </tr>
  890. );
  891. })}
  892. </tbody>
  893. </table>
  894. </div>
  895. ) : (
  896. <p className="reconcile-empty-line">本行无成本快照。</p>
  897. )}
  898. </section>
  899. {/* ④ 分成基数;floor 到 0 ⇒ 显式标下限保护(floor 是静默的,不写=藏起一次配置错误)。 */}
  900. <section className="reconcile-record-section">
  901. <div className="reconcile-record-section-head"><span>04</span><div><h2>实际出账口径</h2><p>分成基数与下限保护。</p></div></div>
  902. <dl className="reconcile-record-facts">
  903. <div><dt>分成基数</dt><dd>{moneyFromCents(d.share_base_cents)}</dd></div>
  904. </dl>
  905. {d.base_floor_protected && <p className="reconcile-footnote">{BASE_FLOOR_NOTE}</p>}
  906. </section>
  907. {/* ⑤ 分成方式与取值=当时冻结在明细行上的值(⛔ 读档案现值,I22);文案走册 4 settle_text 单源。 */}
  908. <section className="reconcile-record-section">
  909. <div className="reconcile-record-section-head"><span>05</span><div><h2>分成方式与取值</h2><p>当时冻结在明细行上的值。</p></div></div>
  910. <dl className="reconcile-record-facts">
  911. <div><dt>分成方式与取值</dt><dd>{settleTextFromSnapshot(d.share_mode, d.share_value)}</dd></div>
  912. </dl>
  913. </section>
  914. {/* ⑥ 本方分成金额+恒 0 标注+所属对账单号。 */}
  915. <section className="reconcile-record-section">
  916. <div className="reconcile-record-section-head"><span>06</span><div><h2>对账归属</h2><p>本方分成金额与所属批次。</p></div></div>
  917. <dl className="reconcile-record-facts">
  918. <div><dt>本方分成金额</dt><dd>{moneyFromCents(d.share_amount_cents)}{d.zero_note ? ` · ${ZERO_NOTE_TEXT[d.zero_note]}` : ""}</dd></div>
  919. <div>
  920. <dt>所属对账单号</dt>
  921. <dd>
  922. <Link className="ops-table-action" to={batchPath(d.holder_id, d.batch_no)} state={{ from: location.pathname }}>
  923. {d.batch_no} <ArrowRight size={14} />
  924. </Link>
  925. </dd>
  926. </div>
  927. </dl>
  928. {/* 其他分成方面板:三类只读身份响应体不出现该字段(⛔ 值为空)。 */}
  929. {d.other_parties != null && d.other_parties.length > 0 && (
  930. <div className="ops-table-wrap" style={{ marginTop: 12 }}>
  931. <table className="ops-table">
  932. <thead><tr><th>其他分成方</th><th>分成金额</th><th>对账单号</th></tr></thead>
  933. <tbody>
  934. {d.other_parties.map((p, i) => (
  935. <tr key={`${p.holder_type}-${p.holder_name}-${i}`}>
  936. <td><OpsTag tone={holderTone[p.holder_type]}>{HOLDER_TYPE_TEXT[p.holder_type]}</OpsTag> {p.holder_name}</td>
  937. <td><span className="ops-money">{moneyFromCents(p.share_amount_cents)}</span></td>
  938. <td>
  939. {p.batch_no && p.holder_id ? (
  940. <Link className="ops-table-action" to={batchPath(p.holder_id, p.batch_no)} state={{ from: location.pathname }}>
  941. {p.batch_no} <ArrowRight size={14} />
  942. </Link>
  943. ) : (
  944. <span className="ops-mono">{p.batch_no ?? "—"}</span>
  945. )}
  946. </td>
  947. </tr>
  948. ))}
  949. </tbody>
  950. </table>
  951. </div>
  952. )}
  953. </section>
  954. {/* ⑦ 逐账号在期天数;KLT-S406 ⇒ 该块「取数失败」(⛔ 回落「无开停记录」);未下发 ⇒「后端未下发」。 */}
  955. <section className="reconcile-record-section">
  956. <div className="reconcile-record-section-head"><span>07</span><div><h2>逐账号在期天数</h2><p>按订阅逐分账号统计。</p></div></div>
  957. {d.account_days_failed ? (
  958. <p className="ops-form-error" role="alert">{WORD_FETCH_FAILED}</p>
  959. ) : d.account_days == null ? (
  960. <p className="reconcile-footnote">{BACKEND_NOT_WIRED}</p>
  961. ) : d.account_days.length ? (
  962. <div className="ops-table-wrap">
  963. <table className="ops-table">
  964. <thead><tr><th>分账号</th><th>在期天数</th></tr></thead>
  965. <tbody>
  966. {d.account_days.map((row, i) => (
  967. <tr key={`${row.sub_account_label}-${i}`}><td>{row.sub_account_label}</td><td className="table-number">{row.in_period_days} 天</td></tr>
  968. ))}
  969. </tbody>
  970. </table>
  971. </div>
  972. ) : (
  973. <p className="reconcile-empty-line">本期无在期分账号。</p>
  974. )}
  975. </section>
  976. </>
  977. )}
  978. </article>
  979. );
  980. }