Files
ai-platform/src/modules/jieya-ibp/components/supply/SupplierList.tsx
T
2026-10-18 15:27:02 +08:00

141 lines
7.0 KiB
TypeScript

import { useMemo } from 'react';
import { Link2, Users, X } from 'lucide-react';
import { useTheme, type ThemeTokens } from '../../context/ThemeContext';
import { purchaseOrders, suppliers, type Supplier } from '../../data/ibp';
import { ScrollList } from './PurchaseOrderList';
import { money } from '../../lib/money';
/**
* 供应商资料页签:Excel「供应商资料」的 8 个字段全量展示(采购行数 / 采购金额沿用表内汇总口径)。
* 左栏选中销售订单时,只列出该订单实际下单过的供应商——关联关系由采购订单行反推,不另外造数据。
*/
/** 按采购金额从大到小:钱多的供应商先看 */
export function sortSuppliers(rows: Supplier[]): Supplier[] {
return [...rows].sort((a, b) => b.采购金额 - a.采购金额);
}
/** filterSO 非空时用采购订单行反查该订单涉及的供应商编码 */
export function buildSuppliers(filterSO: string | null): Supplier[] {
if (!filterSO) return sortSuppliers(suppliers);
const codes = new Set(purchaseOrders.filter(p => p.来源销售订单号 === filterSO).map(p => p.供应商编码));
return sortSuppliers(suppliers.filter(s => codes.has(s.供应商编码)));
}
/** 编码 / 简称 / 所属类别 / 分管部门 四向模糊匹配 */
export function filterSuppliers(rows: Supplier[], query: string): Supplier[] {
const q = query.trim().toLowerCase();
if (!q) return rows;
return rows.filter(s =>
s.供应商编码.toLowerCase().includes(q)
|| s.供应商简称.toLowerCase().includes(q)
|| s.所属类别.toLowerCase().includes(q)
|| (s.分管部门 ?? '').toLowerCase().includes(q));
}
/** 客商一体(性质=客户/供应商)是治理关注项,单独给色 */
const natureColor = (nature: string, tk: ThemeTokens) =>
nature === '客户/供应商' ? { c: tk.warning, bg: tk.warningBg } : { c: tk.info, bg: tk.infoBg };
interface Props {
query: string;
filterSO: string | null;
onClearFilter: () => void;
/** 点击供应商:筛出它的采购单行,回到采购单页签看 */
onPickSupplier: (code: string) => void;
}
export default function SupplierList({ query, filterSO, onClearFilter, onPickSupplier }: Props) {
const { tokens } = useTheme();
const list = useMemo(() => filterSuppliers(buildSuppliers(filterSO), query), [filterSO, query]);
const total = useMemo(() => list.reduce((a, s) => a + s.采购金额, 0), [list]);
const totalLines = useMemo(() => list.reduce((a, s) => a + s.采购行数, 0), [list]);
return (
<>
{filterSO && (
<div style={{
display: 'flex', alignItems: 'center', gap: 6, flex: '0 0 auto', marginBottom: 8,
padding: '5px 9px', borderRadius: 8, fontSize: 11.5, fontWeight: 600,
color: tokens.accent, background: tokens.accentBg, border: `1px solid ${tokens.borderAccent}`,
}}>
<Link2 size={12} style={{ flexShrink: 0 }} />
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>关联订单 {filterSO} · 仅该单涉及的供应商</span>
<span style={{ flex: 1 }} />
<button onClick={onClearFilter} title="显示全部供应商" style={{ background: 'none', border: 'none', color: tokens.accent, cursor: 'pointer', display: 'flex', padding: 2 }}>
<X size={13} />
</button>
</div>
)}
{/* 汇总条 */}
<div style={{
display: 'flex', justifyContent: 'space-between', alignItems: 'center', flex: '0 0 auto',
padding: '6px 10px', marginBottom: 10, borderRadius: 8, fontSize: 12,
background: tokens.kpiCardBg, border: `1px solid ${tokens.border}`, color: tokens.textSub,
}}>
<span>{list.length} 家供应商 · {totalLines} 行采购</span>
<span style={{ color: tokens.accent, fontWeight: 700, fontVariantNumeric: 'tabular-nums' }}>{money(total)}</span>
</div>
<ScrollList>
{list.length === 0 && (
<div style={{ padding: '20px 0', textAlign: 'center', fontSize: 13, color: tokens.textMuted }}>
{filterSO ? '该销售订单暂无涉及供应商' : '无匹配供应商'}
</div>
)}
{list.map(s => {
const nc = natureColor(s.性质, tokens);
return (
<div
key={s.供应商编码}
onClick={() => onPickSupplier(s.供应商编码)}
title="点击只看这家供应商的采购单"
style={{
marginBottom: 8, padding: '9px 11px', borderRadius: 9, cursor: 'pointer',
background: tokens.panelBgAlt, border: `1px solid ${tokens.border}`,
transition: 'background .15s, border-color .15s',
}}
>
{/* 行1:简称 + 编码 + 性质 */}
<div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
<Users size={12} style={{ color: tokens.textMuted, flexShrink: 0 }} />
<span style={{ fontSize: 13, fontWeight: 700, color: tokens.text, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
{s.供应商简称}
</span>
<span style={{ fontSize: 10.5, color: tokens.textMuted, fontVariantNumeric: 'tabular-nums' }}>{s.供应商编码}</span>
<span style={{ flex: 1 }} />
<span style={{
fontSize: 10.5, fontWeight: 600, padding: '1px 7px', borderRadius: 99,
color: nc.c, background: nc.bg, border: `1px solid ${nc.c}44`, whiteSpace: 'nowrap',
}}>{s.性质}</span>
</div>
{/* 行2:类别 + 部门 */}
<div style={{ display: 'flex', alignItems: 'center', gap: 6, marginTop: 5 }}>
<span style={{ fontSize: 10.5, color: tokens.textSub, padding: '0 6px', borderRadius: 99, border: `1px solid ${tokens.border}`, whiteSpace: 'nowrap' }}>{s.所属类别}</span>
<span style={{ fontSize: 11, color: tokens.textMuted, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
{s.分管部门 || '分管部门未填'}
</span>
</div>
{/* 行3:金额 + 行数 */}
<div style={{ display: 'flex', alignItems: 'baseline', gap: 6, marginTop: 5 }}>
<span style={{ fontSize: 14, fontWeight: 800, color: tokens.accent, fontVariantNumeric: 'tabular-nums' }}>{money(s.采购金额)}</span>
<span style={{ flex: 1 }} />
<span style={{ fontSize: 11, color: tokens.textMuted, fontVariantNumeric: 'tabular-nums' }}>{s.采购行数} 行采购</span>
</div>
</div>
);
})}
</ScrollList>
<div style={{
display: 'flex', alignItems: 'center', gap: 5, flex: '0 0 auto', paddingTop: 6,
fontSize: 10.5, color: tokens.textMuted, borderTop: `1px solid ${tokens.border}`,
}}>
<span style={{ flex: 1 }}>行数与金额为该供应商在本批采购订单中的合计,与采购单页签同源可核对</span>
</div>
</>
);
}