統一庫存盤點與盤調 UI 及邏輯:修正狀態顯示、操作權限與列表樣式
This commit is contained in:
@@ -129,7 +129,7 @@ class AdjustDocController extends Controller
|
||||
|
||||
public function show(InventoryAdjustDoc $doc)
|
||||
{
|
||||
$doc->load(['items.product.baseUnit', 'createdBy', 'postedBy', 'warehouse']);
|
||||
$doc->load(['items.product.baseUnit', 'createdBy', 'postedBy', 'warehouse', 'countDoc']);
|
||||
|
||||
$docData = [
|
||||
'id' => (string) $doc->id,
|
||||
@@ -141,6 +141,8 @@ class AdjustDocController extends Controller
|
||||
'remarks' => $doc->remarks,
|
||||
'created_at' => $doc->created_at->format('Y-m-d H:i'),
|
||||
'created_by' => $doc->createdBy?->name,
|
||||
'count_doc_id' => $doc->count_doc_id ? (string)$doc->count_doc_id : null,
|
||||
'count_doc_no' => $doc->countDoc?->doc_no,
|
||||
'items' => $doc->items->map(function ($item) {
|
||||
return [
|
||||
'id' => (string) $item->id,
|
||||
@@ -171,7 +173,7 @@ class AdjustDocController extends Controller
|
||||
if ($request->input('action') === 'post') {
|
||||
$this->adjustService->post($doc, auth()->id());
|
||||
return redirect()->route('inventory.adjust.index')
|
||||
->with('success', '調整單已過帳生效');
|
||||
->with('success', '盤調單已過帳生效');
|
||||
}
|
||||
|
||||
// 僅儲存資料
|
||||
@@ -203,6 +205,6 @@ class AdjustDocController extends Controller
|
||||
$doc->delete();
|
||||
|
||||
return redirect()->route('inventory.adjust.index')
|
||||
->with('success', '調整單已刪除');
|
||||
->with('success', '盤調單已刪除');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ class AdjustService
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新調整單內容 (Items)
|
||||
* 更新盤調單內容 (Items)
|
||||
* 此處採用 "全量更新" 方式處理 items (先刪後加),簡單可靠
|
||||
*/
|
||||
public function updateItems(InventoryAdjustDoc $doc, array $itemsData): void
|
||||
@@ -123,7 +123,7 @@ class AdjustService
|
||||
'unit_cost' => $inventory->unit_cost,
|
||||
'balance_before' => $oldQty,
|
||||
'balance_after' => $newQty,
|
||||
'reason' => "調整單 {$doc->doc_no}: " . ($doc->reason ?? '手動調整'),
|
||||
'reason' => "盤調單 {$doc->doc_no}: " . ($doc->reason ?? '手動調整'),
|
||||
'actual_time' => now(),
|
||||
'user_id' => $userId,
|
||||
]);
|
||||
@@ -134,6 +134,13 @@ class AdjustService
|
||||
'posted_at' => now(),
|
||||
'posted_by' => $userId,
|
||||
]);
|
||||
|
||||
// 4. 若關聯盤點單,連動更新盤點單狀態
|
||||
if ($doc->count_doc_id) {
|
||||
InventoryCountDoc::where('id', $doc->count_doc_id)->update([
|
||||
'status' => 'adjusted'
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
|
||||
import { Head, useForm, router } from '@inertiajs/react';
|
||||
import { Head, useForm, router, Link } from '@inertiajs/react';
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
@@ -20,8 +20,18 @@ import {
|
||||
DialogTitle,
|
||||
} from "@/Components/ui/dialog";
|
||||
import { Label } from "@/Components/ui/label";
|
||||
import { Plus, Search, X, Eye, Pencil, ClipboardCheck } from "lucide-react";
|
||||
import { Plus, Search, X, Eye, Pencil, ClipboardCheck, Trash2 } from "lucide-react";
|
||||
import { useState, useCallback, useEffect } from 'react';
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from "@/Components/ui/alert-dialog";
|
||||
import Pagination from '@/Components/shared/Pagination';
|
||||
import { SearchableSelect } from '@/Components/ui/searchable-select';
|
||||
import { Can } from '@/Components/Permission/Can';
|
||||
@@ -63,6 +73,7 @@ export default function Index({ docs, warehouses, filters }: { docs: DocsPaginat
|
||||
const [searchQuery, setSearchQuery] = useState(filters.search || '');
|
||||
const [warehouseId, setWarehouseId] = useState(filters.warehouse_id || '');
|
||||
const [perPage, setPerPage] = useState(filters.per_page || '15');
|
||||
const [deleteId, setDeleteId] = useState<string | null>(null);
|
||||
|
||||
// For Count Doc Selection
|
||||
const [pendingCounts, setPendingCounts] = useState<any[]>([]);
|
||||
@@ -110,6 +121,20 @@ export default function Index({ docs, warehouses, filters }: { docs: DocsPaginat
|
||||
debouncedFilter({ search: searchQuery, warehouse_id: warehouseId, per_page: val });
|
||||
};
|
||||
|
||||
const confirmDelete = (id: string, e: React.MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
setDeleteId(id);
|
||||
};
|
||||
|
||||
const handleDelete = () => {
|
||||
if (deleteId) {
|
||||
router.delete(route('inventory.adjust.destroy', [deleteId]), {
|
||||
onSuccess: () => setDeleteId(null),
|
||||
onError: () => setDeleteId(null),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const { data, setData, post, processing, reset } = useForm({
|
||||
count_doc_id: null as string | null,
|
||||
warehouse_id: '',
|
||||
@@ -161,9 +186,11 @@ export default function Index({ docs, warehouses, filters }: { docs: DocsPaginat
|
||||
<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-gray-500 mt-1">針對盤點差異進行庫存調整與過帳 (盤盈、盤虧、報廢等)</p>
|
||||
<p className="text-gray-500 mt-1">
|
||||
針對盤點差異進行庫存調整與過帳 (盤盈、盤虧、報廢等)。
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -229,7 +256,7 @@ export default function Index({ docs, warehouses, filters }: { docs: DocsPaginat
|
||||
<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>
|
||||
<TableHead className="text-center font-medium text-grey-600">操作</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
@@ -246,30 +273,46 @@ export default function Index({ docs, warehouses, filters }: { docs: DocsPaginat
|
||||
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">
|
||||
<TableCell className="text-center text-gray-500 font-medium">
|
||||
{(docs.current_page - 1) * docs.per_page + index + 1}
|
||||
</TableCell>
|
||||
<TableCell className="font-semibold text-primary-main">
|
||||
<TableCell className="font-medium 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>{doc.warehouse_name}</TableCell>
|
||||
<TableCell className="text-gray-500 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" /> 編輯</>
|
||||
<TableCell className="text-sm">{doc.created_by}</TableCell>
|
||||
<TableCell className="text-gray-500 text-sm">{doc.created_at}</TableCell>
|
||||
<TableCell className="text-gray-500 text-sm">{doc.posted_at || '-'}</TableCell>
|
||||
<TableCell className="text-center">
|
||||
<div className="flex items-center justify-center gap-2" onClick={(e) => e.stopPropagation()}>
|
||||
<Link href={route('inventory.adjust.show', [doc.id])}>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="button-outlined-primary"
|
||||
title={doc.status === 'posted' ? '查閱' : '編輯'}
|
||||
>
|
||||
{doc.status === 'posted' ? (
|
||||
<Eye className="w-4 h-4" />
|
||||
) : (
|
||||
<Pencil className="w-4 h-4" />
|
||||
)}
|
||||
</Button>
|
||||
</Link>
|
||||
{doc.status === 'draft' && (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="button-outlined-error"
|
||||
title="刪除"
|
||||
onClick={(e) => confirmDelete(doc.id, e)}
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</Button>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))
|
||||
@@ -300,6 +343,21 @@ export default function Index({ docs, warehouses, filters }: { docs: DocsPaginat
|
||||
</div>
|
||||
<Pagination links={docs.links} />
|
||||
</div>
|
||||
|
||||
<AlertDialog open={!!deleteId} onOpenChange={(open) => !open && setDeleteId(null)}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>確定要刪除此盤調單嗎?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
此動作無法復原。刪除後請重新建立盤調單。
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>取消</AlertDialogCancel>
|
||||
<AlertDialogAction onClick={handleDelete} className="bg-red-600 hover:bg-red-700">確認刪除</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</div>
|
||||
|
||||
{/* Create Dialog */}
|
||||
|
||||
@@ -23,8 +23,7 @@ import {
|
||||
AlertDialogTrigger,
|
||||
} from "@/Components/ui/alert-dialog";
|
||||
import { Label } from "@/Components/ui/label";
|
||||
import { Textarea } from "@/Components/ui/textarea";
|
||||
import { Save, CheckCircle, Trash2, ArrowLeft, Plus, X, Search, FileText, ClipboardCheck } from "lucide-react";
|
||||
import { Save, CheckCircle, Trash2, ArrowLeft, Plus, X, Search, ClipboardCheck } from "lucide-react";
|
||||
import { useState, useEffect } from 'react';
|
||||
import {
|
||||
Dialog,
|
||||
@@ -122,23 +121,15 @@ export default function Show({ doc }: { auth: any, doc: AdjDoc }) {
|
||||
};
|
||||
|
||||
const handlePost = () => {
|
||||
// Validate
|
||||
if (data.items.length === 0) {
|
||||
alert('請至少加入一個調整項目');
|
||||
return;
|
||||
}
|
||||
|
||||
const hasZero = data.items.some(i => Number(i.adjust_qty) === 0);
|
||||
if (hasZero && !confirm('部分項目的調整數量為 0,確定要繼續嗎?')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (confirm('確定要過帳嗎?過帳後將無法修改,並直接影響庫存。')) {
|
||||
router.visit(route('inventory.adjust.update', [doc.id]), {
|
||||
method: 'put',
|
||||
data: { ...data, action: 'post' } as any,
|
||||
});
|
||||
}
|
||||
router.visit(route('inventory.adjust.update', [doc.id]), {
|
||||
method: 'put',
|
||||
data: { ...data, action: 'post' } as any,
|
||||
});
|
||||
};
|
||||
|
||||
const handleDelete = () => {
|
||||
@@ -282,7 +273,7 @@ export default function Show({ doc }: { auth: any, doc: AdjDoc }) {
|
||||
|
||||
<div className="flex flex-row items-center justify-between mb-2">
|
||||
<h3 className="text-lg font-medium text-grey-900">調整項目</h3>
|
||||
{isDraft && (
|
||||
{isDraft && !doc.count_doc_id && (
|
||||
<ProductSearchDialog
|
||||
warehouseId={doc.warehouse_id}
|
||||
onSelect={(product, batch) => addItem(product, batch)}
|
||||
@@ -353,7 +344,7 @@ export default function Show({ doc }: { auth: any, doc: AdjDoc }) {
|
||||
<span className="text-grey-600 text-sm">{item.notes || '-'}</span>
|
||||
)}
|
||||
</TableCell>
|
||||
{isDraft && (
|
||||
{isDraft && !doc.count_doc_id && (
|
||||
<TableCell className="text-center">
|
||||
<Button
|
||||
variant="ghost"
|
||||
|
||||
@@ -13,7 +13,6 @@ import {
|
||||
} from '@/Components/ui/table';
|
||||
import { Button } from '@/Components/ui/button';
|
||||
import { Input } from '@/Components/ui/input';
|
||||
import { Card, CardContent } from '@/Components/ui/card';
|
||||
import { Badge } from '@/Components/ui/badge';
|
||||
import {
|
||||
Dialog,
|
||||
@@ -141,6 +140,8 @@ export default function Index({ auth, docs, warehouses, filters }: any) {
|
||||
return <Badge className="bg-blue-500 hover:bg-blue-600">盤點中</Badge>;
|
||||
case 'completed':
|
||||
return <Badge className="bg-green-500 hover:bg-green-600">已核准</Badge>;
|
||||
case 'adjusted':
|
||||
return <Badge className="bg-purple-500 hover:bg-purple-600">已盤調庫存</Badge>;
|
||||
case 'cancelled':
|
||||
return <Badge variant="destructive">已取消</Badge>;
|
||||
default:
|
||||
@@ -307,16 +308,16 @@ export default function Index({ auth, docs, warehouses, filters }: any) {
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="button-outlined-primary"
|
||||
title={doc.status === 'completed' ? '查閱' : '盤點'}
|
||||
title={['completed', 'adjusted'].includes(doc.status) ? '查閱' : '盤點'}
|
||||
>
|
||||
{doc.status === 'completed' ? (
|
||||
{['completed', 'adjusted'].includes(doc.status) ? (
|
||||
<Eye className="w-4 h-4 ml-0.5" />
|
||||
) : (
|
||||
<Pencil className="w-4 h-4 ml-0.5" />
|
||||
)}
|
||||
</Button>
|
||||
</Link>
|
||||
{doc.status !== 'completed' && (
|
||||
{!['completed', 'adjusted'].includes(doc.status) && (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
|
||||
@@ -61,7 +61,7 @@ export default function Show({ doc }: any) {
|
||||
});
|
||||
}
|
||||
|
||||
const isCompleted = doc.status === 'completed';
|
||||
const isCompleted = ['completed', 'adjusted'].includes(doc.status);
|
||||
|
||||
// Calculate progress
|
||||
const totalItems = doc.items.length;
|
||||
@@ -97,9 +97,13 @@ export default function Show({ doc }: any) {
|
||||
<ClipboardCheck className="h-6 w-6 text-primary-main" />
|
||||
盤點單: {doc.doc_no}
|
||||
</h1>
|
||||
{doc.status === 'completed' ? (
|
||||
{doc.status === 'completed' && (
|
||||
<Badge className="bg-green-500 hover:bg-green-600">已核准</Badge>
|
||||
) : (
|
||||
)}
|
||||
{doc.status === 'adjusted' && (
|
||||
<Badge className="bg-purple-500 hover:bg-purple-600">已盤調庫存</Badge>
|
||||
)}
|
||||
{doc.status === 'draft' && (
|
||||
<Badge className="bg-blue-500 hover:bg-blue-600">盤點中</Badge>
|
||||
)}
|
||||
</div>
|
||||
@@ -119,7 +123,7 @@ export default function Show({ doc }: any) {
|
||||
列印
|
||||
</Button>
|
||||
|
||||
{isCompleted && (
|
||||
{doc.status === 'completed' && (
|
||||
<Can permission="inventory.adjust">
|
||||
<AlertDialog>
|
||||
<AlertDialogTrigger asChild>
|
||||
|
||||
Reference in New Issue
Block a user