統一庫存盤點與盤調 UI 及邏輯:修正狀態顯示、操作權限與列表樣式
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 50s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped

This commit is contained in:
2026-01-29 13:41:31 +08:00
parent a31c8d6052
commit 2efaded77b
6 changed files with 115 additions and 52 deletions

View File

@@ -129,7 +129,7 @@ class AdjustDocController extends Controller
public function show(InventoryAdjustDoc $doc) public function show(InventoryAdjustDoc $doc)
{ {
$doc->load(['items.product.baseUnit', 'createdBy', 'postedBy', 'warehouse']); $doc->load(['items.product.baseUnit', 'createdBy', 'postedBy', 'warehouse', 'countDoc']);
$docData = [ $docData = [
'id' => (string) $doc->id, 'id' => (string) $doc->id,
@@ -141,6 +141,8 @@ class AdjustDocController extends Controller
'remarks' => $doc->remarks, 'remarks' => $doc->remarks,
'created_at' => $doc->created_at->format('Y-m-d H:i'), 'created_at' => $doc->created_at->format('Y-m-d H:i'),
'created_by' => $doc->createdBy?->name, '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) { 'items' => $doc->items->map(function ($item) {
return [ return [
'id' => (string) $item->id, 'id' => (string) $item->id,
@@ -171,7 +173,7 @@ class AdjustDocController extends Controller
if ($request->input('action') === 'post') { if ($request->input('action') === 'post') {
$this->adjustService->post($doc, auth()->id()); $this->adjustService->post($doc, auth()->id());
return redirect()->route('inventory.adjust.index') return redirect()->route('inventory.adjust.index')
->with('success', '調單已過帳生效'); ->with('success', '調單已過帳生效');
} }
// 僅儲存資料 // 僅儲存資料
@@ -203,6 +205,6 @@ class AdjustDocController extends Controller
$doc->delete(); $doc->delete();
return redirect()->route('inventory.adjust.index') return redirect()->route('inventory.adjust.index')
->with('success', '調單已刪除'); ->with('success', '調單已刪除');
} }
} }

View File

@@ -54,7 +54,7 @@ class AdjustService
} }
/** /**
* 更新調單內容 (Items) * 更新調單內容 (Items)
* 此處採用 "全量更新" 方式處理 items (先刪後加),簡單可靠 * 此處採用 "全量更新" 方式處理 items (先刪後加),簡單可靠
*/ */
public function updateItems(InventoryAdjustDoc $doc, array $itemsData): void public function updateItems(InventoryAdjustDoc $doc, array $itemsData): void
@@ -123,7 +123,7 @@ class AdjustService
'unit_cost' => $inventory->unit_cost, 'unit_cost' => $inventory->unit_cost,
'balance_before' => $oldQty, 'balance_before' => $oldQty,
'balance_after' => $newQty, 'balance_after' => $newQty,
'reason' => "調{$doc->doc_no}: " . ($doc->reason ?? '手動調整'), 'reason' => "調單 {$doc->doc_no}: " . ($doc->reason ?? '手動調整'),
'actual_time' => now(), 'actual_time' => now(),
'user_id' => $userId, 'user_id' => $userId,
]); ]);
@@ -134,6 +134,13 @@ class AdjustService
'posted_at' => now(), 'posted_at' => now(),
'posted_by' => $userId, 'posted_by' => $userId,
]); ]);
// 4. 若關聯盤點單,連動更新盤點單狀態
if ($doc->count_doc_id) {
InventoryCountDoc::where('id', $doc->count_doc_id)->update([
'status' => 'adjusted'
]);
}
}); });
} }

View File

@@ -1,5 +1,5 @@
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout'; import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import { Head, useForm, router } from '@inertiajs/react'; import { Head, useForm, router, Link } from '@inertiajs/react';
import { import {
Table, Table,
TableBody, TableBody,
@@ -20,8 +20,18 @@ import {
DialogTitle, DialogTitle,
} from "@/Components/ui/dialog"; } from "@/Components/ui/dialog";
import { Label } from "@/Components/ui/label"; 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 { 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 Pagination from '@/Components/shared/Pagination';
import { SearchableSelect } from '@/Components/ui/searchable-select'; import { SearchableSelect } from '@/Components/ui/searchable-select';
import { Can } from '@/Components/Permission/Can'; 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 [searchQuery, setSearchQuery] = useState(filters.search || '');
const [warehouseId, setWarehouseId] = useState(filters.warehouse_id || ''); const [warehouseId, setWarehouseId] = useState(filters.warehouse_id || '');
const [perPage, setPerPage] = useState(filters.per_page || '15'); const [perPage, setPerPage] = useState(filters.per_page || '15');
const [deleteId, setDeleteId] = useState<string | null>(null);
// For Count Doc Selection // For Count Doc Selection
const [pendingCounts, setPendingCounts] = useState<any[]>([]); 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 }); 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({ const { data, setData, post, processing, reset } = useForm({
count_doc_id: null as string | null, count_doc_id: null as string | null,
warehouse_id: '', warehouse_id: '',
@@ -161,9 +186,11 @@ export default function Index({ docs, warehouses, filters }: { docs: DocsPaginat
<div> <div>
<h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2"> <h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
<ClipboardCheck className="h-6 w-6 text-primary-main" /> <ClipboardCheck className="h-6 w-6 text-primary-main" />
調 調
</h1> </h1>
<p className="text-gray-500 mt-1">調 ()</p> <p className="text-gray-500 mt-1">
調 ()
</p>
</div> </div>
</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="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> </TableRow>
</TableHeader> </TableHeader>
<TableBody> <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" className="hover:bg-gray-50/50 transition-colors cursor-pointer group"
onClick={() => router.visit(route('inventory.adjust.show', [doc.id]))} 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} {(docs.current_page - 1) * docs.per_page + index + 1}
</TableCell> </TableCell>
<TableCell className="font-semibold text-primary-main"> <TableCell className="font-medium text-primary-main">
{doc.doc_no} {doc.doc_no}
</TableCell> </TableCell>
<TableCell className="text-grey-700">{doc.warehouse_name}</TableCell> <TableCell>{doc.warehouse_name}</TableCell>
<TableCell className="text-grey-600 max-w-[200px] truncate">{doc.reason}</TableCell> <TableCell className="text-gray-500 max-w-[200px] truncate">{doc.reason}</TableCell>
<TableCell className="text-center">{getStatusBadge(doc.status)}</TableCell> <TableCell className="text-center">{getStatusBadge(doc.status)}</TableCell>
<TableCell className="text-grey-600 text-sm">{doc.created_by}</TableCell> <TableCell className="text-sm">{doc.created_by}</TableCell>
<TableCell className="text-grey-400 text-xs">{doc.created_at}</TableCell> <TableCell className="text-gray-500 text-sm">{doc.created_at}</TableCell>
<TableCell className="text-grey-400 text-xs">{doc.posted_at}</TableCell> <TableCell className="text-gray-500 text-sm">{doc.posted_at || '-'}</TableCell>
<TableCell className="text-right"> <TableCell className="text-center">
<Button <div className="flex items-center justify-center gap-2" onClick={(e) => e.stopPropagation()}>
variant="ghost" <Link href={route('inventory.adjust.show', [doc.id])}>
size="sm" <Button
className="text-primary-main hover:bg-primary-50 px-2" variant="outline"
> size="sm"
{doc.status === 'posted' ? ( className="button-outlined-primary"
<><Eye className="h-4 w-4 mr-1" /> </> title={doc.status === 'posted' ? '查閱' : '編輯'}
) : ( >
<><Pencil className="h-4 w-4 mr-1" /> </> {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> </TableCell>
</TableRow> </TableRow>
)) ))
@@ -300,6 +343,21 @@ export default function Index({ docs, warehouses, filters }: { docs: DocsPaginat
</div> </div>
<Pagination links={docs.links} /> <Pagination links={docs.links} />
</div> </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> </div>
{/* Create Dialog */} {/* Create Dialog */}

View File

@@ -23,8 +23,7 @@ import {
AlertDialogTrigger, AlertDialogTrigger,
} from "@/Components/ui/alert-dialog"; } from "@/Components/ui/alert-dialog";
import { Label } from "@/Components/ui/label"; import { Label } from "@/Components/ui/label";
import { Textarea } from "@/Components/ui/textarea"; import { Save, CheckCircle, Trash2, ArrowLeft, Plus, X, Search, ClipboardCheck } from "lucide-react";
import { Save, CheckCircle, Trash2, ArrowLeft, Plus, X, Search, FileText, ClipboardCheck } from "lucide-react";
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { import {
Dialog, Dialog,
@@ -122,23 +121,15 @@ export default function Show({ doc }: { auth: any, doc: AdjDoc }) {
}; };
const handlePost = () => { const handlePost = () => {
// Validate
if (data.items.length === 0) { if (data.items.length === 0) {
alert('請至少加入一個調整項目'); alert('請至少加入一個調整項目');
return; return;
} }
const hasZero = data.items.some(i => Number(i.adjust_qty) === 0); router.visit(route('inventory.adjust.update', [doc.id]), {
if (hasZero && !confirm('部分項目的調整數量為 0確定要繼續嗎')) { method: 'put',
return; data: { ...data, action: 'post' } as any,
} });
if (confirm('確定要過帳嗎?過帳後將無法修改,並直接影響庫存。')) {
router.visit(route('inventory.adjust.update', [doc.id]), {
method: 'put',
data: { ...data, action: 'post' } as any,
});
}
}; };
const handleDelete = () => { 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"> <div className="flex flex-row items-center justify-between mb-2">
<h3 className="text-lg font-medium text-grey-900">調</h3> <h3 className="text-lg font-medium text-grey-900">調</h3>
{isDraft && ( {isDraft && !doc.count_doc_id && (
<ProductSearchDialog <ProductSearchDialog
warehouseId={doc.warehouse_id} warehouseId={doc.warehouse_id}
onSelect={(product, batch) => addItem(product, batch)} 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> <span className="text-grey-600 text-sm">{item.notes || '-'}</span>
)} )}
</TableCell> </TableCell>
{isDraft && ( {isDraft && !doc.count_doc_id && (
<TableCell className="text-center"> <TableCell className="text-center">
<Button <Button
variant="ghost" variant="ghost"

View File

@@ -13,7 +13,6 @@ import {
} from '@/Components/ui/table'; } from '@/Components/ui/table';
import { Button } from '@/Components/ui/button'; import { Button } from '@/Components/ui/button';
import { Input } from '@/Components/ui/input'; import { Input } from '@/Components/ui/input';
import { Card, CardContent } from '@/Components/ui/card';
import { Badge } from '@/Components/ui/badge'; import { Badge } from '@/Components/ui/badge';
import { import {
Dialog, 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>; return <Badge className="bg-blue-500 hover:bg-blue-600"></Badge>;
case 'completed': case 'completed':
return <Badge className="bg-green-500 hover:bg-green-600"></Badge>; 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': case 'cancelled':
return <Badge variant="destructive"></Badge>; return <Badge variant="destructive"></Badge>;
default: default:
@@ -307,16 +308,16 @@ export default function Index({ auth, docs, warehouses, filters }: any) {
variant="outline" variant="outline"
size="sm" size="sm"
className="button-outlined-primary" 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" /> <Eye className="w-4 h-4 ml-0.5" />
) : ( ) : (
<Pencil className="w-4 h-4 ml-0.5" /> <Pencil className="w-4 h-4 ml-0.5" />
)} )}
</Button> </Button>
</Link> </Link>
{doc.status !== 'completed' && ( {!['completed', 'adjusted'].includes(doc.status) && (
<Button <Button
variant="outline" variant="outline"
size="sm" size="sm"

View File

@@ -61,7 +61,7 @@ export default function Show({ doc }: any) {
}); });
} }
const isCompleted = doc.status === 'completed'; const isCompleted = ['completed', 'adjusted'].includes(doc.status);
// Calculate progress // Calculate progress
const totalItems = doc.items.length; const totalItems = doc.items.length;
@@ -97,9 +97,13 @@ export default function Show({ doc }: any) {
<ClipboardCheck className="h-6 w-6 text-primary-main" /> <ClipboardCheck className="h-6 w-6 text-primary-main" />
: {doc.doc_no} : {doc.doc_no}
</h1> </h1>
{doc.status === 'completed' ? ( {doc.status === 'completed' && (
<Badge className="bg-green-500 hover:bg-green-600"></Badge> <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> <Badge className="bg-blue-500 hover:bg-blue-600"></Badge>
)} )}
</div> </div>
@@ -119,7 +123,7 @@ export default function Show({ doc }: any) {
</Button> </Button>
{isCompleted && ( {doc.status === 'completed' && (
<Can permission="inventory.adjust"> <Can permission="inventory.adjust">
<AlertDialog> <AlertDialog>
<AlertDialogTrigger asChild> <AlertDialogTrigger asChild>