Files
star-erp/resources/js/Components/SafetyStock/SafetyStockList.tsx

152 lines
4.8 KiB
TypeScript
Raw Normal View History

2025-12-30 15:03:19 +08:00
/**
*
*/
import { Trash2, Pencil } from "lucide-react";
2025-12-30 15:03:19 +08:00
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/Components/ui/table";
import { Button } from "@/Components/ui/button";
import { SafetyStockSetting, WarehouseInventory, SafetyStockStatus } from "@/types/warehouse";
import { StatusBadge } from "@/Components/shared/StatusBadge";
2025-12-30 15:03:19 +08:00
interface SafetyStockListProps {
settings: SafetyStockSetting[];
inventories: WarehouseInventory[];
onEdit: (setting: SafetyStockSetting) => void;
onDelete: (id: string) => void;
}
// 計算安全庫存狀態
function getSafetyStockStatus(
currentStock: number,
safetyStock: number
): SafetyStockStatus {
const ratio = currentStock / safetyStock;
if (ratio >= 1.2) return "正常";
if (ratio >= 1.0) return "接近";
return "低於";
}
// 獲取狀態徽章
function getStatusBadge(status: SafetyStockStatus) {
if (status === '正常') {
return (
<StatusBadge variant="success">
</StatusBadge>
);
}
if (status === '接近') {
return (
<StatusBadge variant="warning">
</StatusBadge>
);
}
if (status === '低於') {
return (
<StatusBadge variant="destructive">
</StatusBadge>
);
2025-12-30 15:03:19 +08:00
}
return null; // Should not happen if SafetyStockStatus is exhaustive
2025-12-30 15:03:19 +08:00
}
export default function SafetyStockList({
settings,
inventories,
onEdit,
onDelete,
}: SafetyStockListProps) {
if (settings.length === 0) {
return (
<div className="bg-white rounded-lg shadow-sm border p-12 text-center text-gray-400">
<p></p>
<p className="text-sm mt-1"></p>
</div>
);
}
// 計算每個商品的目前總庫存
const getCurrentStock = (productId: string): number => {
return inventories
.filter((inv) => inv.productId === productId)
.reduce((sum, inv) => sum + inv.quantity, 0);
};
return (
<div className="bg-white rounded-lg shadow-sm border overflow-hidden">
<div className="overflow-x-auto">
<Table>
<TableHeader>
<TableRow>
<TableHead className="w-[5%]">#</TableHead>
<TableHead className="w-[25%]"></TableHead>
<TableHead className="w-[12%]"></TableHead>
<TableHead className="w-[12%]"></TableHead>
<TableHead className="w-[12%]"></TableHead>
<TableHead className="w-[15%]"></TableHead>
<TableHead className="w-[12%] text-right"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{settings.map((setting, index) => {
const currentStock = getCurrentStock(setting.productId);
const status = getSafetyStockStatus(currentStock, setting.safetyStock);
const isLowStock = status === "低於";
return (
<TableRow key={setting.id} className={isLowStock ? "bg-red-50" : ""}>
<TableCell className="text-grey-2">{index + 1}</TableCell>
<TableCell className="font-medium">{setting.productName}</TableCell>
<TableCell>
<StatusBadge variant="neutral">{setting.productType}</StatusBadge>
2025-12-30 15:03:19 +08:00
</TableCell>
<TableCell>
<span className={isLowStock ? "text-red-600 font-medium" : ""}>
{currentStock}
</span>
</TableCell>
<TableCell>{setting.safetyStock}</TableCell>
<TableCell>{getStatusBadge(status)}</TableCell>
<TableCell className="text-right">
<div className="flex items-center justify-end gap-2">
<Button
type="button"
variant="ghost"
size="sm"
onClick={() => onEdit(setting)}
className="hover:bg-primary/10 hover:text-primary"
>
<Pencil className="h-4 w-4 mr-1" />
2025-12-30 15:03:19 +08:00
</Button>
<Button
type="button"
variant="ghost"
size="sm"
onClick={() => onDelete(setting.id)}
className="hover:bg-red-50 hover:text-red-600"
>
<Trash2 className="h-4 w-4 mr-1" />
</Button>
</div>
</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</div>
</div>
);
}