feat(Inventory): 實作批號溯源完整功能與 UI 呈現,包含文字敘述卡片與更完整的關聯屬性
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
import { Info } from "lucide-react";
|
||||
import { TraceabilityNode } from "./TreeView";
|
||||
|
||||
interface TraceabilitySummaryProps {
|
||||
data: TraceabilityNode;
|
||||
direction: 'forward' | 'backward';
|
||||
}
|
||||
|
||||
export function TraceabilitySummary({ data, direction }: TraceabilitySummaryProps) {
|
||||
if (!data) return null;
|
||||
|
||||
// --- Helper to extract unqiue names/codes from children ---
|
||||
const getUniqueLabels = (nodes: TraceabilityNode[], prefixToStrip: string = '') => {
|
||||
const labels = nodes
|
||||
.map(n => n.label.replace(prefixToStrip, '').trim())
|
||||
.filter(Boolean);
|
||||
return Array.from(new Set(labels));
|
||||
};
|
||||
|
||||
// --- Logic for Backward Tracing ---
|
||||
if (direction === 'backward') {
|
||||
const poNodes = data.children?.filter(c => c.type === 'production_order') || [];
|
||||
const poNames = getUniqueLabels(poNodes, '生產工單:');
|
||||
|
||||
let materialNodes: TraceabilityNode[] = [];
|
||||
let grNodes: TraceabilityNode[] = [];
|
||||
|
||||
poNodes.forEach(po => {
|
||||
const materials = po.children?.filter(c => c.type === 'material_batch') || [];
|
||||
materialNodes = [...materialNodes, ...materials];
|
||||
|
||||
materials.forEach(mat => {
|
||||
const grs = mat.children?.filter(c => c.type === 'goods_receipt') || [];
|
||||
grNodes = [...grNodes, ...grs];
|
||||
});
|
||||
});
|
||||
|
||||
const materialNames = getUniqueLabels(materialNodes, '原料批號:');
|
||||
const grNames = getUniqueLabels(grNodes, '進貨單:');
|
||||
|
||||
// Handle case where batch is directly from Goods Receipt (no PO)
|
||||
const directGrNodes = data.children?.filter(c => c.type === 'goods_receipt') || [];
|
||||
if (directGrNodes.length > 0) {
|
||||
grNodes = [...grNodes, ...directGrNodes];
|
||||
const directGrNames = getUniqueLabels(directGrNodes, '進貨單:');
|
||||
|
||||
return (
|
||||
<div className="bg-blue-50/50 border border-blue-100 rounded-xl p-4 mb-6 flex gap-3 text-gray-700 leading-relaxed shadow-sm">
|
||||
<Info className="w-5 h-5 text-blue-500 shrink-0 mt-0.5" />
|
||||
<div>
|
||||
<p>
|
||||
查詢的批號 <strong className="text-gray-900">{data.batch_number}</strong>
|
||||
{data.product_name && <span> ({data.product_name})</span>}
|
||||
主要是由進貨單 {directGrNames.map(n => <strong key={n} className="text-gray-900 mx-1">{n}</strong>).reduce((prev, curr) => [prev, '、', curr] as any)} 採購進貨。
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="bg-blue-50/50 border border-blue-100 rounded-xl p-4 mb-6 flex gap-3 text-gray-700 leading-relaxed shadow-sm">
|
||||
<Info className="w-5 h-5 text-blue-500 shrink-0 mt-0.5" />
|
||||
<div className="space-y-1.5">
|
||||
<p>
|
||||
查詢的成品批號 <strong className="text-gray-900">{data.batch_number}</strong>
|
||||
{data.product_name && <span> ({data.product_name})</span>} 是由
|
||||
{poNames.length > 0 ? (
|
||||
<>
|
||||
生產工單 {poNames.slice(0, 3).map(n => <strong key={n} className="text-gray-900 mx-1">{n}</strong>).reduce((prev, curr) => [prev, '、', curr] as any)}
|
||||
{poNames.length > 3 && ` 等 ${poNames.length} 張工單`} 產出。
|
||||
</>
|
||||
) : (
|
||||
'未知的生產工單產出。'
|
||||
)}
|
||||
</p>
|
||||
{materialNodes.length > 0 && (
|
||||
<p>
|
||||
這些工單總共使用了 <strong className="text-gray-900">{materialNodes.length}</strong> 批原料
|
||||
(包含原料批號 {materialNames.slice(0, 3).map(n => <strong key={n} className="text-gray-900 mx-1">{n}</strong>).reduce((prev, curr) => [prev, '、', curr] as any)}
|
||||
{materialNames.length > 3 && ' 等'})。
|
||||
</p>
|
||||
)}
|
||||
{grNodes.length > 0 && (
|
||||
<p>
|
||||
而這些原料主要來自於進貨單 {grNames.slice(0, 3).map(n => <strong key={n} className="text-gray-900 mx-1">{n}</strong>).reduce((prev, curr) => [prev, '、', curr] as any)}
|
||||
{grNames.length > 3 && ` 等 ${grNames.length} 張單據`}。
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// --- Logic for Forward Tracing ---
|
||||
if (direction === 'forward') {
|
||||
const poNodes = data.children?.filter(c => c.type === 'production_order') || [];
|
||||
const poNames = getUniqueLabels(poNodes, '投入工單:');
|
||||
|
||||
let targetNodes: TraceabilityNode[] = [];
|
||||
let outNodes: TraceabilityNode[] = [];
|
||||
|
||||
poNodes.forEach(po => {
|
||||
const targets = po.children?.filter(c => c.type === 'target_batch') || [];
|
||||
targetNodes = [...targetNodes, ...targets];
|
||||
|
||||
targets.forEach(tgt => {
|
||||
const outs = tgt.children?.filter(c => c.type === 'outbound_transaction') || [];
|
||||
outNodes = [...outNodes, ...outs];
|
||||
});
|
||||
});
|
||||
|
||||
const targetNames = getUniqueLabels(targetNodes, '產出成品:');
|
||||
const outNames = getUniqueLabels(outNodes, '出庫單據:');
|
||||
|
||||
return (
|
||||
<div className="bg-blue-50/50 border border-blue-100 rounded-xl p-4 mb-6 flex gap-3 text-gray-700 leading-relaxed shadow-sm">
|
||||
<Info className="w-5 h-5 text-blue-500 shrink-0 mt-0.5" />
|
||||
<div className="space-y-1.5">
|
||||
<p>
|
||||
查詢的原料批號 <strong className="text-gray-900">{data.batch_number}</strong>
|
||||
{data.product_name && <span> ({data.product_name})</span>} 被投入了
|
||||
{poNames.length > 0 ? (
|
||||
<>
|
||||
生產工單 {poNames.slice(0, 3).map(n => <strong key={n} className="text-gray-900 mx-1">{n}</strong>).reduce((prev, curr) => [prev, '、', curr] as any)}
|
||||
{poNames.length > 3 && ` 等 ${poNames.length} 張工單`}。
|
||||
</>
|
||||
) : (
|
||||
' 未知的生產工單。'
|
||||
)}
|
||||
</p>
|
||||
{targetNodes.length > 0 && (
|
||||
<p>
|
||||
這些工單隨後產出了成品批號 {targetNames.slice(0, 3).map(n => <strong key={n} className="text-gray-900 mx-1">{n}</strong>).reduce((prev, curr) => [prev, '、', curr] as any)}
|
||||
{targetNames.length > 3 && ` 等 ${targetNodes.length} 批成品`}。
|
||||
</p>
|
||||
)}
|
||||
{outNodes.length > 0 && (
|
||||
<p>
|
||||
最終,這些成品透過 {outNames.slice(0, 3).map(n => <strong key={n} className="text-gray-900 mx-1">{n}</strong>).reduce((prev, curr) => [prev, '、', curr] as any)}
|
||||
{outNames.length > 3 && ` 等 ${outNodes.length} 張單據`} 離開了倉庫。
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
import { cn } from '@/lib/utils';
|
||||
import { Package, Truck, Factory, CornerDownRight, Box, ShoppingCart } from 'lucide-react';
|
||||
import { formatDate } from '@/lib/date';
|
||||
|
||||
export interface TraceabilityNode {
|
||||
id: string;
|
||||
type: 'target_batch' | 'source_batch' | 'production_order' | 'material_batch' | 'goods_receipt' | 'outbound_transaction';
|
||||
label: string;
|
||||
batch_number?: string;
|
||||
date?: string;
|
||||
vendor_id?: number | string;
|
||||
product_name?: string;
|
||||
spec?: string;
|
||||
quantity?: number | string;
|
||||
unit?: string;
|
||||
warehouse_name?: string;
|
||||
children?: TraceabilityNode[];
|
||||
}
|
||||
|
||||
interface TreeViewProps {
|
||||
data: TraceabilityNode;
|
||||
}
|
||||
|
||||
const getNodeIcon = (type: TraceabilityNode['type']) => {
|
||||
switch (type) {
|
||||
case 'target_batch':
|
||||
case 'source_batch':
|
||||
return <Package className="h-5 w-5 text-primary-main" />;
|
||||
case 'production_order':
|
||||
return <Factory className="h-5 w-5 text-other-warning" />;
|
||||
case 'material_batch':
|
||||
return <Box className="h-5 w-5 text-primary-main" />;
|
||||
case 'goods_receipt':
|
||||
return <Truck className="h-5 w-5 text-other-info" />;
|
||||
case 'outbound_transaction':
|
||||
return <ShoppingCart className="h-5 w-5 text-other-success" />;
|
||||
default:
|
||||
return <Box className="h-5 w-5 text-gray-400" />;
|
||||
}
|
||||
};
|
||||
|
||||
const getNodeColor = (type: TraceabilityNode['type']) => {
|
||||
switch (type) {
|
||||
case 'target_batch':
|
||||
case 'source_batch':
|
||||
return 'border-primary-light bg-primary-lightest';
|
||||
case 'production_order':
|
||||
return 'border-orange-200 bg-orange-50';
|
||||
case 'material_batch':
|
||||
return 'border-primary-light bg-primary-lightest';
|
||||
case 'goods_receipt':
|
||||
return 'border-blue-200 bg-blue-50';
|
||||
case 'outbound_transaction':
|
||||
return 'border-emerald-200 bg-emerald-50';
|
||||
default:
|
||||
return 'border-gray-200 bg-gray-50';
|
||||
}
|
||||
};
|
||||
|
||||
const TreeNode = ({ node, isLast, level = 0 }: { node: TraceabilityNode; isLast: boolean; level?: number }) => {
|
||||
const hasChildren = node.children && node.children.length > 0;
|
||||
|
||||
return (
|
||||
<div className="relative">
|
||||
{/* 連接線 (上層到此節點) */}
|
||||
{level > 0 && (
|
||||
<div
|
||||
className={cn(
|
||||
"absolute border-l-2 border-gray-200",
|
||||
isLast ? "h-6 top-0" : "h-full top-0"
|
||||
)}
|
||||
style={{ left: '-1.5rem' }}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 橫向連接線 */}
|
||||
{level > 0 && (
|
||||
<div
|
||||
className="absolute border-t-2 border-gray-200 w-6 top-6"
|
||||
style={{ left: '-1.5rem' }}
|
||||
/>
|
||||
)}
|
||||
|
||||
<div className="flex items-start mb-6">
|
||||
{level > 0 && (
|
||||
<div className="w-6 h-12 shrink-0 flex items-center justify-end pr-2 text-gray-400">
|
||||
<CornerDownRight className="h-4 w-4" />
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className={cn(
|
||||
"relative flex flex-col p-4 rounded-xl border shadow-sm min-w-[320px] max-w-md",
|
||||
getNodeColor(node.type)
|
||||
)}>
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="p-2 bg-white rounded-lg shadow-sm shrink-0">
|
||||
{getNodeIcon(node.type)}
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<h4 className="font-semibold text-gray-900 truncate">{node.label}</h4>
|
||||
{node.date && (
|
||||
<div className="mt-0.5">
|
||||
<span className="text-xs text-gray-500">{formatDate(node.date)}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{(node.product_name || node.warehouse_name || (node.quantity !== undefined && node.quantity !== null)) && (
|
||||
<div className="mt-3 pt-3 border-t border-gray-200/60 flex flex-wrap gap-4">
|
||||
{node.product_name && (
|
||||
<div className="flex-1 min-w-[120px]">
|
||||
<span className="text-[11px] text-gray-500 font-medium block mb-0.5">品名 / 規格</span>
|
||||
<span className="text-sm font-medium text-gray-800 leading-tight block truncate" title={`${node.product_name} ${node.spec ? `(${node.spec})` : ''}`}>
|
||||
{node.product_name}
|
||||
{node.spec && <span className="text-gray-400 text-xs ml-1">({node.spec})</span>}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{node.warehouse_name && (
|
||||
<div className="shrink-0 min-w-[80px]">
|
||||
<span className="text-[11px] text-gray-500 font-medium block mb-0.5">倉庫</span>
|
||||
<span className="text-sm font-medium text-gray-800 leading-tight block truncate" title={node.warehouse_name}>
|
||||
{node.warehouse_name}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{node.quantity !== undefined && node.quantity !== null && (
|
||||
<div className="shrink-0 text-right min-w-[60px]">
|
||||
<span className="text-[11px] text-gray-500 font-medium block mb-0.5">數量</span>
|
||||
<span className="text-sm font-bold text-gray-900 block truncate">
|
||||
{Number(node.quantity).toLocaleString()}
|
||||
<span className="text-[10px] ml-0.5 text-gray-500 font-normal">{node.unit || 'PCS'}</span>
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{hasChildren && (
|
||||
<div className="pl-12 relative">
|
||||
{node.children!.map((child, index) => (
|
||||
<TreeNode
|
||||
key={`${child.id} -${index} `}
|
||||
node={child}
|
||||
isLast={index === node.children!.length - 1}
|
||||
level={level + 1}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default function TreeView({ data }: TreeViewProps) {
|
||||
if (!data) return null;
|
||||
|
||||
return (
|
||||
<div className="p-4 md:p-8 bg-gray-50/50 rounded-2xl border border-gray-100 overflow-x-auto">
|
||||
<TreeNode node={data} isLast={true} level={0} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user