410 lines
20 KiB
TypeScript
410 lines
20 KiB
TypeScript
|
|
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
|
|||
|
|
import { Head, useForm, router } from '@inertiajs/react';
|
|||
|
|
import {
|
|||
|
|
Table,
|
|||
|
|
TableBody,
|
|||
|
|
TableCell,
|
|||
|
|
TableHead,
|
|||
|
|
TableHeader,
|
|||
|
|
TableRow,
|
|||
|
|
} from "@/Components/ui/table";
|
|||
|
|
import { Button } from "@/Components/ui/button";
|
|||
|
|
import { Input } from "@/Components/ui/input";
|
|||
|
|
import { Badge } from "@/Components/ui/badge";
|
|||
|
|
import {
|
|||
|
|
Dialog,
|
|||
|
|
DialogContent,
|
|||
|
|
DialogDescription,
|
|||
|
|
DialogFooter,
|
|||
|
|
DialogHeader,
|
|||
|
|
DialogTitle,
|
|||
|
|
} from "@/Components/ui/dialog";
|
|||
|
|
import { Label } from "@/Components/ui/label";
|
|||
|
|
import { Plus, Search, X, Eye, Pencil, ClipboardCheck } from "lucide-react";
|
|||
|
|
import { useState, useCallback, useEffect } from 'react';
|
|||
|
|
import Pagination from '@/Components/shared/Pagination';
|
|||
|
|
import { SearchableSelect } from '@/Components/ui/searchable-select';
|
|||
|
|
import { Can } from '@/Components/Permission/Can';
|
|||
|
|
import debounce from 'lodash/debounce';
|
|||
|
|
import axios from 'axios';
|
|||
|
|
|
|||
|
|
interface Doc {
|
|||
|
|
id: string;
|
|||
|
|
doc_no: string;
|
|||
|
|
warehouse_name: string;
|
|||
|
|
reason: string;
|
|||
|
|
status: string;
|
|||
|
|
created_by: string;
|
|||
|
|
created_at: string;
|
|||
|
|
posted_at: string;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
interface Warehouse {
|
|||
|
|
id: string;
|
|||
|
|
name: string;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
interface Filters {
|
|||
|
|
search?: string;
|
|||
|
|
warehouse_id?: string;
|
|||
|
|
per_page?: string;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
interface DocsPagination {
|
|||
|
|
data: Doc[];
|
|||
|
|
current_page: number;
|
|||
|
|
per_page: number;
|
|||
|
|
total: number;
|
|||
|
|
links: any[]; // Adjust type as needed for pagination links
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export default function Index({ docs, warehouses, filters }: { docs: DocsPagination, warehouses: Warehouse[], filters: Filters }) {
|
|||
|
|
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
|||
|
|
const [searchQuery, setSearchQuery] = useState(filters.search || '');
|
|||
|
|
const [warehouseId, setWarehouseId] = useState(filters.warehouse_id || '');
|
|||
|
|
const [perPage, setPerPage] = useState(filters.per_page || '15');
|
|||
|
|
|
|||
|
|
// For Count Doc Selection
|
|||
|
|
const [pendingCounts, setPendingCounts] = useState<any[]>([]);
|
|||
|
|
const [loadingPending, setLoadingPending] = useState(false);
|
|||
|
|
const [scanSearch, setScanSearch] = useState('');
|
|||
|
|
|
|||
|
|
const fetchPendingCounts = useCallback(
|
|||
|
|
debounce((search = '') => {
|
|||
|
|
setLoadingPending(true);
|
|||
|
|
axios.get(route('inventory.adjust.pending-counts'), { params: { search } })
|
|||
|
|
.then(res => setPendingCounts(res.data))
|
|||
|
|
.finally(() => setLoadingPending(false));
|
|||
|
|
}, 300),
|
|||
|
|
[]
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
useEffect(() => {
|
|||
|
|
if (isDialogOpen) {
|
|||
|
|
fetchPendingCounts();
|
|||
|
|
}
|
|||
|
|
}, [isDialogOpen, fetchPendingCounts]);
|
|||
|
|
|
|||
|
|
const debouncedFilter = useCallback(
|
|||
|
|
debounce((params: Filters) => {
|
|||
|
|
router.get(route('inventory.adjust.index'), params as any, {
|
|||
|
|
preserveState: true,
|
|||
|
|
replace: true,
|
|||
|
|
});
|
|||
|
|
}, 300),
|
|||
|
|
[]
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
const handleSearchChange = (val: string) => {
|
|||
|
|
setSearchQuery(val);
|
|||
|
|
debouncedFilter({ search: val, warehouse_id: warehouseId, per_page: perPage });
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
const handleWarehouseChange = (val: string) => {
|
|||
|
|
setWarehouseId(val);
|
|||
|
|
debouncedFilter({ search: searchQuery, warehouse_id: val, per_page: perPage });
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
const handlePerPageChange = (val: string) => {
|
|||
|
|
setPerPage(val);
|
|||
|
|
debouncedFilter({ search: searchQuery, warehouse_id: warehouseId, per_page: val });
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
const { data, setData, post, processing, reset } = useForm({
|
|||
|
|
count_doc_id: null as string | null,
|
|||
|
|
warehouse_id: '',
|
|||
|
|
reason: '',
|
|||
|
|
remarks: '',
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
const handleCreate = (countDocId?: string) => {
|
|||
|
|
if (countDocId) {
|
|||
|
|
setData('count_doc_id', countDocId);
|
|||
|
|
router.post(route('inventory.adjust.store'), { count_doc_id: countDocId }, {
|
|||
|
|
onSuccess: () => setIsDialogOpen(false),
|
|||
|
|
});
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
post(route('inventory.adjust.store'), {
|
|||
|
|
onSuccess: () => {
|
|||
|
|
setIsDialogOpen(false);
|
|||
|
|
reset();
|
|||
|
|
},
|
|||
|
|
});
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
const getStatusBadge = (status: string) => {
|
|||
|
|
switch (status) {
|
|||
|
|
case 'draft':
|
|||
|
|
return <Badge variant="secondary" className="bg-gray-100 text-gray-600 border-none">草稿</Badge>;
|
|||
|
|
case 'posted':
|
|||
|
|
return <Badge className="bg-green-100 text-green-700 border-none">已過帳</Badge>;
|
|||
|
|
case 'voided':
|
|||
|
|
return <Badge variant="destructive" className="bg-red-100 text-red-700 border-none">已作廢</Badge>;
|
|||
|
|
default:
|
|||
|
|
return <Badge variant="outline">{status}</Badge>;
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
return (
|
|||
|
|
<AuthenticatedLayout
|
|||
|
|
breadcrumbs={[
|
|||
|
|
{ label: '商品與庫存管理', href: '#' },
|
|||
|
|
{ label: '庫存盤調', href: route('inventory.adjust.index'), isPage: true },
|
|||
|
|
]}
|
|||
|
|
>
|
|||
|
|
<Head title="庫存盤調" />
|
|||
|
|
|
|||
|
|
<div className="container mx-auto p-6 max-w-7xl">
|
|||
|
|
<div className="flex items-center justify-between mb-6">
|
|||
|
|
<div>
|
|||
|
|
<h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
|
|||
|
|
<ClipboardCheck className="h-6 w-6 text-primary-main" />
|
|||
|
|
庫存盤調單
|
|||
|
|
</h1>
|
|||
|
|
<p className="text-sm text-gray-500 mt-1">針對盤點差異進行庫存調整與過帳 (盤盈、盤虧、報廢等)</p>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
{/* Toolbar Context */}
|
|||
|
|
<div className="bg-white rounded-lg shadow-sm border p-4 mb-6">
|
|||
|
|
<div className="flex flex-col md:flex-row gap-4">
|
|||
|
|
{/* Search */}
|
|||
|
|
<div className="flex-1 relative">
|
|||
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-gray-400" />
|
|||
|
|
<Input
|
|||
|
|
placeholder="搜尋單號、原因或備註..."
|
|||
|
|
value={searchQuery}
|
|||
|
|
onChange={(e) => handleSearchChange(e.target.value)}
|
|||
|
|
className="pl-10 pr-10 h-9"
|
|||
|
|
/>
|
|||
|
|
{searchQuery && (
|
|||
|
|
<button
|
|||
|
|
onClick={() => handleSearchChange('')}
|
|||
|
|
className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600"
|
|||
|
|
>
|
|||
|
|
<X className="h-4 w-4" />
|
|||
|
|
</button>
|
|||
|
|
)}
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
{/* Warehouse Filter */}
|
|||
|
|
<SearchableSelect
|
|||
|
|
options={[
|
|||
|
|
{ value: '', label: '所有倉庫' },
|
|||
|
|
...warehouses.map(w => ({ value: w.id, label: w.name }))
|
|||
|
|
]}
|
|||
|
|
value={warehouseId}
|
|||
|
|
onValueChange={handleWarehouseChange}
|
|||
|
|
placeholder="選擇倉庫"
|
|||
|
|
className="w-full md:w-[200px] h-9"
|
|||
|
|
/>
|
|||
|
|
|
|||
|
|
{/* Action Buttons */}
|
|||
|
|
<div className="flex gap-2 w-full md:w-auto">
|
|||
|
|
<Can permission="inventory.adjust">
|
|||
|
|
<Button
|
|||
|
|
className="flex-1 md:flex-none button-filled-primary h-9"
|
|||
|
|
onClick={() => setIsDialogOpen(true)}
|
|||
|
|
>
|
|||
|
|
<Plus className="mr-2 h-4 w-4" />
|
|||
|
|
新增盤調單
|
|||
|
|
</Button>
|
|||
|
|
</Can>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
{/* Table Container */}
|
|||
|
|
<div className="bg-white rounded-xl border border-gray-200 shadow-sm overflow-hidden">
|
|||
|
|
<Table>
|
|||
|
|
<TableHeader className="bg-gray-50">
|
|||
|
|
<TableRow>
|
|||
|
|
<TableHead className="w-[60px] text-center font-medium text-grey-600">#</TableHead>
|
|||
|
|
<TableHead className="w-[180px] font-medium text-grey-600">單號</TableHead>
|
|||
|
|
<TableHead className="font-medium text-grey-600">倉庫</TableHead>
|
|||
|
|
<TableHead className="font-medium text-grey-600">調整原因</TableHead>
|
|||
|
|
<TableHead className="font-medium text-grey-600 text-center">狀態</TableHead>
|
|||
|
|
<TableHead className="font-medium text-grey-600">建立者</TableHead>
|
|||
|
|
<TableHead className="font-medium text-grey-600">建立時間</TableHead>
|
|||
|
|
<TableHead className="font-medium text-grey-600">過帳時間</TableHead>
|
|||
|
|
<TableHead className="text-right font-medium text-grey-600">操作</TableHead>
|
|||
|
|
</TableRow>
|
|||
|
|
</TableHeader>
|
|||
|
|
<TableBody>
|
|||
|
|
{docs.data.length === 0 ? (
|
|||
|
|
<TableRow>
|
|||
|
|
<TableCell colSpan={9} className="h-32 text-center text-grey-400">
|
|||
|
|
尚無任何盤調單據
|
|||
|
|
</TableCell>
|
|||
|
|
</TableRow>
|
|||
|
|
) : (
|
|||
|
|
docs.data.map((doc: Doc, index: number) => (
|
|||
|
|
<TableRow
|
|||
|
|
key={doc.id}
|
|||
|
|
className="hover:bg-gray-50/50 transition-colors cursor-pointer group"
|
|||
|
|
onClick={() => router.visit(route('inventory.adjust.show', [doc.id]))}
|
|||
|
|
>
|
|||
|
|
<TableCell className="text-center text-grey-400 font-medium">
|
|||
|
|
{(docs.current_page - 1) * docs.per_page + index + 1}
|
|||
|
|
</TableCell>
|
|||
|
|
<TableCell className="font-semibold text-primary-main">
|
|||
|
|
{doc.doc_no}
|
|||
|
|
</TableCell>
|
|||
|
|
<TableCell className="text-grey-700">{doc.warehouse_name}</TableCell>
|
|||
|
|
<TableCell className="text-grey-600 max-w-[200px] truncate">{doc.reason}</TableCell>
|
|||
|
|
<TableCell className="text-center">{getStatusBadge(doc.status)}</TableCell>
|
|||
|
|
<TableCell className="text-grey-600 text-sm">{doc.created_by}</TableCell>
|
|||
|
|
<TableCell className="text-grey-400 text-xs">{doc.created_at}</TableCell>
|
|||
|
|
<TableCell className="text-grey-400 text-xs">{doc.posted_at}</TableCell>
|
|||
|
|
<TableCell className="text-right">
|
|||
|
|
<Button
|
|||
|
|
variant="ghost"
|
|||
|
|
size="sm"
|
|||
|
|
className="text-primary-main hover:bg-primary-50 px-2"
|
|||
|
|
>
|
|||
|
|
{doc.status === 'posted' ? (
|
|||
|
|
<><Eye className="h-4 w-4 mr-1" /> 查閱</>
|
|||
|
|
) : (
|
|||
|
|
<><Pencil className="h-4 w-4 mr-1" /> 編輯</>
|
|||
|
|
)}
|
|||
|
|
</Button>
|
|||
|
|
</TableCell>
|
|||
|
|
</TableRow>
|
|||
|
|
))
|
|||
|
|
)}
|
|||
|
|
</TableBody>
|
|||
|
|
</Table>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div className="mt-4 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: "15", value: "15" },
|
|||
|
|
{ label: "30", value: "30" },
|
|||
|
|
{ label: "50", value: "50" },
|
|||
|
|
{ label: "100", value: "100" }
|
|||
|
|
]}
|
|||
|
|
className="w-[100px] h-8"
|
|||
|
|
showSearch={false}
|
|||
|
|
/>
|
|||
|
|
<span>筆</span>
|
|||
|
|
</div>
|
|||
|
|
<span className="text-sm text-gray-500">共 {docs?.total || 0} 筆紀錄</span>
|
|||
|
|
</div>
|
|||
|
|
<Pagination links={docs.links} />
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
{/* Create Dialog */}
|
|||
|
|
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
|
|||
|
|
<DialogContent className="max-w-2xl">
|
|||
|
|
<DialogHeader>
|
|||
|
|
<DialogTitle className="text-xl font-bold flex items-center gap-2">
|
|||
|
|
<Plus className="h-5 w-5 text-primary-main" />
|
|||
|
|
新增盤調單
|
|||
|
|
</DialogTitle>
|
|||
|
|
<DialogDescription>
|
|||
|
|
請選擇一個已完成的盤點單來生成盤調項目,或手動建立。
|
|||
|
|
</DialogDescription>
|
|||
|
|
</DialogHeader>
|
|||
|
|
|
|||
|
|
<div className="py-4 space-y-6">
|
|||
|
|
{/* Option 1: Scan/Select from Count Docs */}
|
|||
|
|
<div className="space-y-4">
|
|||
|
|
<Label className="text-sm font-semibold text-grey-700">方法一:關聯盤點單 (推薦)</Label>
|
|||
|
|
<div className="relative">
|
|||
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-grey-400" />
|
|||
|
|
<Input
|
|||
|
|
placeholder="掃描盤點單號或搜尋..."
|
|||
|
|
className="pl-9 h-11 border-primary-100 focus:ring-primary-main"
|
|||
|
|
value={scanSearch}
|
|||
|
|
onChange={(e) => {
|
|||
|
|
setScanSearch(e.target.value);
|
|||
|
|
fetchPendingCounts(e.target.value);
|
|||
|
|
}}
|
|||
|
|
/>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div className="max-height-[300px] overflow-y-auto rounded-md border border-grey-100 bg-grey-50">
|
|||
|
|
{loadingPending ? (
|
|||
|
|
<div className="p-8 text-center text-sm text-grey-400">載入中...</div>
|
|||
|
|
) : pendingCounts.length === 0 ? (
|
|||
|
|
<div className="p-8 text-center text-sm text-grey-400">
|
|||
|
|
查無可供盤調的盤點單 (需為已完成狀態)
|
|||
|
|
</div>
|
|||
|
|
) : (
|
|||
|
|
<div className="divide-y divide-grey-100">
|
|||
|
|
{pendingCounts.map((c: any) => (
|
|||
|
|
<div
|
|||
|
|
key={c.id}
|
|||
|
|
className="p-3 hover:bg-white flex items-center justify-between cursor-pointer group transition-colors"
|
|||
|
|
onClick={() => handleCreate(c.id)}
|
|||
|
|
>
|
|||
|
|
<div>
|
|||
|
|
<p className="font-bold text-grey-900 group-hover:text-primary-main">{c.doc_no}</p>
|
|||
|
|
<p className="text-xs text-grey-500">{c.warehouse_name} | 完成於: {c.completed_at}</p>
|
|||
|
|
</div>
|
|||
|
|
<Button size="sm" variant="outline" className="button-outlined-primary">
|
|||
|
|
選擇並載入
|
|||
|
|
</Button>
|
|||
|
|
</div>
|
|||
|
|
))}
|
|||
|
|
</div>
|
|||
|
|
)}
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div className="relative">
|
|||
|
|
<div className="absolute inset-0 flex items-center"><span className="w-full border-t border-grey-200" /></div>
|
|||
|
|
<div className="relative flex justify-center text-xs uppercase"><span className="bg-white px-2 text-grey-400 font-medium">或</span></div>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
{/* Option 2: Manual (Optional, though less common in this flow) */}
|
|||
|
|
<div className="space-y-4">
|
|||
|
|
<Label className="text-sm font-semibold text-grey-700">方法二:手動建立調整</Label>
|
|||
|
|
<div className="grid grid-cols-2 gap-4">
|
|||
|
|
<div className="space-y-2">
|
|||
|
|
<Label className="text-xs">選擇倉庫</Label>
|
|||
|
|
<SearchableSelect
|
|||
|
|
options={warehouses.map(w => ({ value: w.id, label: w.name }))}
|
|||
|
|
value={data.warehouse_id}
|
|||
|
|
onValueChange={(val) => setData('warehouse_id', val)}
|
|||
|
|
placeholder="選擇倉庫"
|
|||
|
|
/>
|
|||
|
|
</div>
|
|||
|
|
<div className="space-y-2">
|
|||
|
|
<Label className="text-xs">調整原因</Label>
|
|||
|
|
<Input
|
|||
|
|
placeholder="例如: 報廢, 破損..."
|
|||
|
|
value={data.reason}
|
|||
|
|
onChange={(e) => setData('reason', e.target.value)}
|
|||
|
|
className="h-10"
|
|||
|
|
/>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<DialogFooter className="bg-gray-50 -mx-6 -mb-6 p-4 rounded-b-lg">
|
|||
|
|
<Button variant="ghost" onClick={() => setIsDialogOpen(false)}>取消</Button>
|
|||
|
|
<Button
|
|||
|
|
className="button-filled-primary"
|
|||
|
|
disabled={processing || !data.warehouse_id || !data.reason}
|
|||
|
|
onClick={() => handleCreate()}
|
|||
|
|
>
|
|||
|
|
建立手動盤調
|
|||
|
|
</Button>
|
|||
|
|
</DialogFooter>
|
|||
|
|
</DialogContent>
|
|||
|
|
</Dialog>
|
|||
|
|
</AuthenticatedLayout>
|
|||
|
|
);
|
|||
|
|
}
|