2025-12-30 15:03:19 +08:00
|
|
|
import { useState } from "react";
|
2026-02-02 10:07:36 +08:00
|
|
|
import { SearchableSelect } from "@/Components/ui/searchable-select";
|
|
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
DialogFooter,
|
|
|
|
|
DialogDescription,
|
|
|
|
|
} from "@/Components/ui/dialog";
|
|
|
|
|
import { Label } from "@/Components/ui/label";
|
|
|
|
|
import { Loader2, Plus, Warehouse as WarehouseIcon } from 'lucide-react';
|
|
|
|
|
import { Card, CardContent } from "@/Components/ui/card";
|
2025-12-30 15:03:19 +08:00
|
|
|
import { Button } from "@/Components/ui/button";
|
|
|
|
|
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
|
|
|
|
|
import { Head, router } from "@inertiajs/react";
|
|
|
|
|
import WarehouseDialog from "@/Components/Warehouse/WarehouseDialog";
|
|
|
|
|
import SearchToolbar from "@/Components/shared/SearchToolbar";
|
|
|
|
|
import WarehouseCard from "@/Components/Warehouse/WarehouseCard";
|
|
|
|
|
import WarehouseEmptyState from "@/Components/Warehouse/WarehouseEmptyState";
|
|
|
|
|
import { Warehouse } from "@/types/warehouse";
|
|
|
|
|
import Pagination from "@/Components/shared/Pagination";
|
|
|
|
|
import { toast } from "sonner";
|
2026-01-07 13:06:49 +08:00
|
|
|
import { getBreadcrumbs } from "@/utils/breadcrumb";
|
2026-01-13 17:00:58 +08:00
|
|
|
import { Can } from "@/Components/Permission/Can";
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
interface PageProps {
|
|
|
|
|
warehouses: {
|
|
|
|
|
data: Warehouse[];
|
|
|
|
|
links: any[];
|
|
|
|
|
current_page: number;
|
|
|
|
|
last_page: number;
|
|
|
|
|
total: number;
|
|
|
|
|
};
|
2026-01-26 14:59:24 +08:00
|
|
|
totals: {
|
|
|
|
|
available_stock: number;
|
2026-02-05 13:18:22 +08:00
|
|
|
available_amount: number;
|
2026-01-26 14:59:24 +08:00
|
|
|
book_stock: number;
|
2026-02-05 13:18:22 +08:00
|
|
|
book_amount: number;
|
2026-02-05 15:50:14 +08:00
|
|
|
abnormal_amount: number;
|
2026-01-26 14:59:24 +08:00
|
|
|
};
|
2026-02-13 13:16:05 +08:00
|
|
|
transitWarehouses: Array<{
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
license_plate?: string;
|
|
|
|
|
driver_name?: string;
|
|
|
|
|
}>;
|
2025-12-30 15:03:19 +08:00
|
|
|
filters: {
|
|
|
|
|
search?: string;
|
2026-02-03 17:24:34 +08:00
|
|
|
per_page?: string;
|
2025-12-30 15:03:19 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 13:16:05 +08:00
|
|
|
export default function WarehouseIndex({ warehouses, totals, transitWarehouses, filters }: PageProps) {
|
2025-12-30 15:03:19 +08:00
|
|
|
// 篩選狀態
|
|
|
|
|
const [searchTerm, setSearchTerm] = useState(filters.search || "");
|
2026-02-03 17:24:34 +08:00
|
|
|
const [perPage, setPerPage] = useState(filters.per_page || '10');
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
// 對話框狀態
|
|
|
|
|
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
|
|
|
|
const [editingWarehouse, setEditingWarehouse] = useState<Warehouse | null>(null);
|
2026-02-02 10:07:36 +08:00
|
|
|
|
|
|
|
|
// 調撥單單建立狀態
|
|
|
|
|
const [isTransferCreateOpen, setIsTransferCreateOpen] = useState(false);
|
|
|
|
|
const [sourceWarehouseId, setSourceWarehouseId] = useState("");
|
|
|
|
|
const [targetWarehouseId, setTargetWarehouseId] = useState("");
|
|
|
|
|
const [creating, setCreating] = useState(false);
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
// 搜尋處理
|
|
|
|
|
const handleSearch = (term: string) => {
|
|
|
|
|
setSearchTerm(term);
|
2026-02-03 17:24:34 +08:00
|
|
|
router.get(route('warehouses.index'), { search: term, per_page: perPage }, {
|
2025-12-30 15:03:19 +08:00
|
|
|
preserveState: true,
|
|
|
|
|
preserveScroll: true,
|
|
|
|
|
replace: true,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-03 17:24:34 +08:00
|
|
|
const handlePerPageChange = (value: string) => {
|
|
|
|
|
setPerPage(value);
|
|
|
|
|
router.get(route('warehouses.index'),
|
|
|
|
|
{ ...filters, per_page: value, page: 1 },
|
|
|
|
|
{ preserveState: false, replace: true, preserveScroll: true }
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-30 15:03:19 +08:00
|
|
|
// 導航處理
|
2026-01-13 13:30:51 +08:00
|
|
|
const handleViewInventory = (warehouseId: string | number) => {
|
2025-12-30 15:03:19 +08:00
|
|
|
router.get(`/warehouses/${warehouseId}/inventory`);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 倉庫操作處理函式
|
|
|
|
|
const handleAddWarehouse = () => {
|
|
|
|
|
setEditingWarehouse(null);
|
|
|
|
|
setIsDialogOpen(true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleEditWarehouse = (warehouse: Warehouse) => {
|
|
|
|
|
setEditingWarehouse(warehouse);
|
|
|
|
|
setIsDialogOpen(true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 接收 Dialog 回傳的資料並呼叫後端
|
|
|
|
|
const handleSaveWarehouse = (data: Partial<Warehouse>) => {
|
|
|
|
|
if (editingWarehouse) {
|
|
|
|
|
router.put(route('warehouses.update', editingWarehouse.id), data, {
|
|
|
|
|
onSuccess: () => setIsDialogOpen(false),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
router.post(route('warehouses.store'), data, {
|
|
|
|
|
onSuccess: () => setIsDialogOpen(false),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-13 13:30:51 +08:00
|
|
|
const handleDeleteWarehouse = (id: string | number) => {
|
2026-01-08 16:32:10 +08:00
|
|
|
router.delete(route('warehouses.destroy', id), {
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
toast.success('倉庫已刪除');
|
|
|
|
|
setEditingWarehouse(null);
|
|
|
|
|
},
|
|
|
|
|
onError: (errors: any) => {
|
2026-01-13 13:30:51 +08:00
|
|
|
console.error(errors);
|
2026-01-08 16:32:10 +08:00
|
|
|
}
|
|
|
|
|
});
|
2025-12-30 15:03:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleAddTransferOrder = () => {
|
2026-02-02 10:07:36 +08:00
|
|
|
setIsTransferCreateOpen(true);
|
2025-12-30 15:03:19 +08:00
|
|
|
};
|
|
|
|
|
|
2026-02-02 10:07:36 +08:00
|
|
|
const handleCreateTransferOrder = () => {
|
|
|
|
|
if (!sourceWarehouseId) {
|
|
|
|
|
toast.error("請選擇來源倉庫");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!targetWarehouseId) {
|
|
|
|
|
toast.error("請選擇目的倉庫");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (sourceWarehouseId === targetWarehouseId) {
|
|
|
|
|
toast.error("來源與目的倉庫不能相同");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setCreating(true);
|
|
|
|
|
router.post(route('inventory.transfer.store'), {
|
|
|
|
|
from_warehouse_id: sourceWarehouseId,
|
|
|
|
|
to_warehouse_id: targetWarehouseId
|
|
|
|
|
}, {
|
|
|
|
|
onFinish: () => setCreating(false),
|
2025-12-30 15:03:19 +08:00
|
|
|
onSuccess: () => {
|
2026-02-02 10:07:36 +08:00
|
|
|
setIsTransferCreateOpen(false);
|
|
|
|
|
setSourceWarehouseId("");
|
|
|
|
|
setTargetWarehouseId("");
|
|
|
|
|
toast.success('調撥單已建立');
|
2025-12-30 15:03:19 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2026-01-07 13:06:49 +08:00
|
|
|
<AuthenticatedLayout breadcrumbs={getBreadcrumbs("warehouses")}>
|
2025-12-30 15:03:19 +08:00
|
|
|
<Head title="倉庫管理" />
|
|
|
|
|
<div className="container mx-auto p-6 max-w-7xl">
|
|
|
|
|
{/* 頁面標題 */}
|
|
|
|
|
<div className="mb-6">
|
2026-01-13 13:30:51 +08:00
|
|
|
<h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
|
2026-01-16 14:36:24 +08:00
|
|
|
<WarehouseIcon className="h-6 w-6 text-primary-main" />
|
2026-01-13 13:30:51 +08:00
|
|
|
倉庫管理
|
|
|
|
|
</h1>
|
|
|
|
|
<p className="text-gray-500 mt-1">
|
|
|
|
|
管理倉庫地點、負責人與庫存監控
|
|
|
|
|
</p>
|
2025-12-30 15:03:19 +08:00
|
|
|
</div>
|
|
|
|
|
|
2026-01-26 14:59:24 +08:00
|
|
|
{/* 統計區塊 */}
|
2026-02-05 15:50:14 +08:00
|
|
|
<div className="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
|
2026-01-26 14:59:24 +08:00
|
|
|
<Card className="shadow-sm">
|
|
|
|
|
<CardContent className="p-6">
|
|
|
|
|
<div className="flex flex-col">
|
|
|
|
|
<span className="text-sm font-medium text-gray-500 mb-1">可用庫存總計</span>
|
2026-02-05 13:18:22 +08:00
|
|
|
<div className="flex items-baseline gap-2">
|
|
|
|
|
<Can permission="inventory.view_cost">
|
2026-02-05 15:50:14 +08:00
|
|
|
<span className="text-3xl font-bold text-primary-main">
|
|
|
|
|
${Number(totals.available_amount || 0).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
|
2026-02-05 13:18:22 +08:00
|
|
|
</span>
|
|
|
|
|
</Can>
|
|
|
|
|
</div>
|
2026-01-26 14:59:24 +08:00
|
|
|
</div>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
|
|
<Card className="shadow-sm">
|
|
|
|
|
<CardContent className="p-6">
|
|
|
|
|
<div className="flex flex-col">
|
|
|
|
|
<span className="text-sm font-medium text-gray-500 mb-1">帳面庫存總計</span>
|
2026-02-05 13:18:22 +08:00
|
|
|
<div className="flex items-baseline gap-2">
|
|
|
|
|
<Can permission="inventory.view_cost">
|
2026-02-05 15:50:14 +08:00
|
|
|
<span className="text-3xl font-bold text-gray-700">
|
|
|
|
|
${Number(totals.book_amount || 0).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
|
|
|
|
|
</span>
|
|
|
|
|
</Can>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
|
|
<Card className="shadow-sm border-red-100 bg-red-50/10">
|
|
|
|
|
<CardContent className="p-6">
|
|
|
|
|
<div className="flex flex-col">
|
2026-02-05 15:54:24 +08:00
|
|
|
<span className="text-sm font-medium text-red-500 mb-1">過期與瑕疵總計</span>
|
2026-02-05 15:50:14 +08:00
|
|
|
<div className="flex items-baseline gap-2">
|
|
|
|
|
<Can permission="inventory.view_cost">
|
|
|
|
|
<span className="text-3xl font-bold text-red-600">
|
|
|
|
|
${Number(totals.abnormal_amount || 0).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
|
2026-02-05 13:18:22 +08:00
|
|
|
</span>
|
|
|
|
|
</Can>
|
|
|
|
|
</div>
|
2026-01-26 14:59:24 +08:00
|
|
|
</div>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-12-30 15:03:19 +08:00
|
|
|
{/* 工具列 */}
|
|
|
|
|
<div className="bg-white rounded-lg shadow-sm border p-4 mb-6">
|
|
|
|
|
<div className="flex flex-col md:flex-row gap-4 items-start md:items-center justify-between">
|
|
|
|
|
<div className="flex flex-col sm:flex-row gap-4 flex-1 w-full">
|
|
|
|
|
{/* 搜尋框 */}
|
|
|
|
|
<SearchToolbar
|
|
|
|
|
value={searchTerm}
|
|
|
|
|
onChange={handleSearch}
|
|
|
|
|
placeholder="搜尋倉庫名稱..."
|
|
|
|
|
className="flex-1 w-full md:max-w-md"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* 操作按鈕 */}
|
|
|
|
|
<div className="flex gap-2 w-full md:w-auto">
|
2026-02-03 17:24:34 +08:00
|
|
|
<Can permission="inventory_count.create">
|
2026-01-13 17:00:58 +08:00
|
|
|
<Button
|
|
|
|
|
onClick={handleAddTransferOrder}
|
|
|
|
|
className="flex-1 md:flex-initial button-outlined-primary"
|
|
|
|
|
>
|
|
|
|
|
<Plus className="mr-2 h-4 w-4" />
|
2026-02-02 10:07:36 +08:00
|
|
|
新增調撥單
|
2026-01-13 17:00:58 +08:00
|
|
|
</Button>
|
|
|
|
|
</Can>
|
|
|
|
|
<Can permission="warehouses.create">
|
|
|
|
|
<Button
|
|
|
|
|
onClick={handleAddWarehouse}
|
|
|
|
|
className="flex-1 md:flex-initial button-filled-primary"
|
|
|
|
|
>
|
|
|
|
|
<Plus className="mr-2 h-4 w-4" />
|
|
|
|
|
新增倉庫
|
|
|
|
|
</Button>
|
|
|
|
|
</Can>
|
2025-12-30 15:03:19 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* 倉庫卡片列表 */}
|
|
|
|
|
{warehouses.data.length === 0 ? (
|
|
|
|
|
<WarehouseEmptyState />
|
|
|
|
|
) : (
|
|
|
|
|
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
|
|
|
|
{warehouses.data.map((warehouse) => (
|
|
|
|
|
<WarehouseCard
|
|
|
|
|
key={warehouse.id}
|
|
|
|
|
warehouse={warehouse}
|
|
|
|
|
stats={{
|
2026-02-02 11:03:09 +08:00
|
|
|
totalQuantity: warehouse.book_stock || 0,
|
2026-02-05 15:50:14 +08:00
|
|
|
totalValue: warehouse.book_amount || 0,
|
|
|
|
|
abnormalValue: warehouse.abnormal_amount || 0,
|
2025-12-30 15:03:19 +08:00
|
|
|
lowStockCount: warehouse.low_stock_count || 0,
|
|
|
|
|
replenishmentNeeded: warehouse.low_stock_count || 0
|
|
|
|
|
}}
|
|
|
|
|
hasWarning={(warehouse.low_stock_count || 0) > 0}
|
|
|
|
|
onViewInventory={() => handleViewInventory(warehouse.id)}
|
|
|
|
|
onEdit={handleEditWarehouse}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* 分頁 */}
|
2026-02-03 17:24:34 +08:00
|
|
|
<div className="mt-6 flex flex-col sm:flex-row items-center justify-between gap-4">
|
|
|
|
|
<div className="flex items-center gap-4">
|
|
|
|
|
<div className="flex items-center gap-2 text-sm text-gray-500">
|
|
|
|
|
<span>每頁顯示</span>
|
|
|
|
|
<SearchableSelect
|
|
|
|
|
value={perPage}
|
|
|
|
|
onValueChange={handlePerPageChange}
|
|
|
|
|
options={[
|
|
|
|
|
{ label: "10", value: "10" },
|
|
|
|
|
{ label: "20", value: "20" },
|
|
|
|
|
{ label: "50", value: "50" },
|
|
|
|
|
{ label: "100", value: "100" }
|
|
|
|
|
]}
|
|
|
|
|
className="w-[90px] h-8"
|
|
|
|
|
showSearch={false}
|
|
|
|
|
/>
|
|
|
|
|
<span>筆</span>
|
|
|
|
|
</div>
|
|
|
|
|
<span className="text-sm text-gray-500">共 {warehouses.total} 筆紀錄</span>
|
|
|
|
|
</div>
|
2025-12-30 15:03:19 +08:00
|
|
|
<Pagination links={warehouses.links} />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* 倉庫對話框 */}
|
|
|
|
|
<WarehouseDialog
|
|
|
|
|
open={isDialogOpen}
|
|
|
|
|
onOpenChange={setIsDialogOpen}
|
|
|
|
|
warehouse={editingWarehouse}
|
|
|
|
|
onSave={handleSaveWarehouse}
|
|
|
|
|
onDelete={handleDeleteWarehouse}
|
2026-02-13 13:16:05 +08:00
|
|
|
transitWarehouses={transitWarehouses}
|
2025-12-30 15:03:19 +08:00
|
|
|
/>
|
|
|
|
|
|
2026-02-02 10:07:36 +08:00
|
|
|
{/* 調撥單建立對話框 */}
|
|
|
|
|
<Dialog open={isTransferCreateOpen} onOpenChange={setIsTransferCreateOpen}>
|
|
|
|
|
<DialogContent className="sm:max-w-[425px]">
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>建立新調撥單</DialogTitle>
|
|
|
|
|
<DialogDescription>
|
|
|
|
|
請選擇來源倉庫與目的倉庫。
|
|
|
|
|
</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
<div className="grid gap-4 py-4">
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label>來源倉庫</Label>
|
|
|
|
|
<SearchableSelect
|
|
|
|
|
value={sourceWarehouseId}
|
|
|
|
|
onValueChange={setSourceWarehouseId}
|
|
|
|
|
options={warehouses.data.map((w: any) => ({ label: w.name, value: w.id.toString() }))}
|
|
|
|
|
placeholder="請選擇來源倉庫"
|
|
|
|
|
className="h-9"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label>目的倉庫</Label>
|
|
|
|
|
<SearchableSelect
|
|
|
|
|
value={targetWarehouseId}
|
|
|
|
|
onValueChange={setTargetWarehouseId}
|
|
|
|
|
options={warehouses.data
|
|
|
|
|
.filter((w: any) => w.id.toString() !== sourceWarehouseId)
|
|
|
|
|
.map((w: any) => ({ label: w.name, value: w.id.toString() }))
|
|
|
|
|
}
|
|
|
|
|
placeholder="請選擇目的倉庫"
|
|
|
|
|
className="h-9"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<Button type="button" variant="outline" className="button-outlined-primary" onClick={() => setIsTransferCreateOpen(false)}>
|
|
|
|
|
取消
|
|
|
|
|
</Button>
|
|
|
|
|
<Button onClick={handleCreateTransferOrder} className="button-filled-primary" disabled={creating || !sourceWarehouseId || !targetWarehouseId}>
|
|
|
|
|
{creating && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
|
|
|
|
新增
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
2025-12-30 15:03:19 +08:00
|
|
|
</div>
|
|
|
|
|
</AuthenticatedLayout>
|
|
|
|
|
);
|
|
|
|
|
}
|