feat(inventory): 優化盤點顯示與權限設定

This commit is contained in:
2026-01-29 09:36:07 +08:00
parent e5edad4fd0
commit 1833ca192d
5 changed files with 273 additions and 317 deletions

View File

@@ -137,7 +137,7 @@ class CountDocController extends Controller
if ($request->input('action') === 'complete') { if ($request->input('action') === 'complete') {
$this->countService->complete($doc, auth()->id()); $this->countService->complete($doc, auth()->id());
return redirect()->route('inventory.count.index') return redirect()->route('inventory.count.index')
->with('success', '盤點已完成並過帳'); ->with('success', '盤點已完成');
} }
return redirect()->back()->with('success', '暫存成功'); return redirect()->back()->with('success', '暫存成功');

View File

@@ -75,55 +75,8 @@ class CountService
public function complete(InventoryCountDoc $doc, int $userId): void public function complete(InventoryCountDoc $doc, int $userId): void
{ {
DB::transaction(function () use ($doc, $userId) { DB::transaction(function () use ($doc, $userId) {
foreach ($doc->items as $item) { // 僅更新單據狀態為「已完成」,不執行庫存入庫/調整
// 如果沒有輸入實盤數量,預設跳過或是視為 0? // 盤點單僅作為記錄,後續調整由盤調單 (AdjustDoc) 執行
// 安全起見:如果 counted_qty 是 null表示沒盤到跳過不處理 (或者依業務邏輯視為0)
// 這裡假設前端會確保有送出資料,若 null 則不做異動
if (is_null($item->counted_qty)) {
continue;
}
$diff = $item->counted_qty - $item->system_qty;
// 如果無差異,更新 item 狀態即可 (diff_qty 已經是 computed field 或在儲存時計算)
// 這裡 update 一下 diff_qty 以防萬一
$item->update(['diff_qty' => $diff]);
if (abs($diff) > 0.0001) {
// 找回原本的 Inventory
$inventory = Inventory::where('warehouse_id', $doc->warehouse_id)
->where('product_id', $item->product_id)
->where('batch_number', $item->batch_number)
->first();
if (!$inventory) {
// 如果原本沒庫存紀錄 (例如是新增的盤點項目),需要新建 Inventory
// 但目前 snapshot 邏輯只抓現有。若允許 "盤盈" (發現不在帳上的),需要額外邏輯
// 暫時略過 "新增 Inventory" 的複雜邏輯,假設只能針對 existing batch 調整
continue;
}
$oldQty = $inventory->quantity;
$newQty = $oldQty + $diff;
$inventory->quantity = $newQty;
$inventory->total_value = $inventory->unit_cost * $newQty;
$inventory->save();
// 寫入 Transaction
$inventory->transactions()->create([
'type' => '盤點調整',
'quantity' => $diff,
'unit_cost' => $inventory->unit_cost,
'balance_before' => $oldQty,
'balance_after' => $newQty,
'reason' => "盤點單 {$doc->doc_no} 過帳",
'actual_time' => now(),
'user_id' => $userId,
]);
}
}
$doc->update([ $doc->update([
'status' => 'completed', 'status' => 'completed',
'completed_at' => now(), 'completed_at' => now(),

View File

@@ -36,7 +36,8 @@ class PermissionSeeder extends Seeder
'inventory.view', 'inventory.view',
'inventory.view_cost', // 查看成本與價值 'inventory.view_cost', // 查看成本與價值
'inventory.adjust', 'inventory.adjust',
'inventory.transfer', 'inventory.count', // 庫存盤點
'inventory.transfer', // 庫存調撥
'inventory.delete', 'inventory.delete',
// 進貨單管理 // 進貨單管理
@@ -132,7 +133,7 @@ class PermissionSeeder extends Seeder
// warehouse-manager 管理庫存與倉庫 // warehouse-manager 管理庫存與倉庫
$warehouseManager->givePermissionTo([ $warehouseManager->givePermissionTo([
'products.view', 'products.view',
'inventory.view', 'inventory.adjust', 'inventory.transfer', 'inventory.delete', 'inventory.view', 'inventory.adjust', 'inventory.count', 'inventory.transfer', 'inventory.delete',
'goods_receipts.view', 'goods_receipts.create', 'goods_receipts.edit', 'goods_receipts.delete', 'goods_receipts.view', 'goods_receipts.create', 'goods_receipts.edit', 'goods_receipts.delete',
'production_orders.view', 'production_orders.create', 'production_orders.edit', 'production_orders.view', 'production_orders.create', 'production_orders.edit',
'warehouses.view', 'warehouses.create', 'warehouses.edit', 'warehouses.view', 'warehouses.create', 'warehouses.edit',

View File

@@ -79,7 +79,6 @@ export default function Show({ doc }: { auth: any, doc: AdjDoc }) {
action: 'save', action: 'save',
}); });
const [newItemOpen, setNewItemOpen] = useState(false);
// Helper to add new item // Helper to add new item
const addItem = (product: any, batchNumber: string | null) => { const addItem = (product: any, batchNumber: string | null) => {
@@ -107,7 +106,6 @@ export default function Show({ doc }: { auth: any, doc: AdjDoc }) {
notes: '', notes: '',
} }
]); ]);
setNewItemOpen(false);
}; };
const removeItem = (index: number) => { const removeItem = (index: number) => {
@@ -294,16 +292,17 @@ export default function Show({ doc }: { auth: any, doc: AdjDoc }) {
</Card> </Card>
</div> </div>
<Card className="border border-gray-200 shadow-sm overflow-hidden gap-0"> <div className="bg-white rounded-lg shadow-sm border p-6 space-y-4">
<CardHeader className="bg-white border-b flex flex-row items-center justify-between py-4 px-6"> <div className="flex flex-row items-center justify-between">
<CardTitle className="text-lg font-medium text-grey-900">調</CardTitle> <h3 className="text-lg font-medium text-grey-900">調</h3>
{isDraft && ( {isDraft && (
<ProductSearchDialog <ProductSearchDialog
warehouseId={doc.warehouse_id} warehouseId={doc.warehouse_id}
onSelect={(product, batch) => addItem(product, batch)} onSelect={(product, batch) => addItem(product, batch)}
/> />
)} )}
</CardHeader> </div>
<div className="border rounded-lg overflow-hidden">
<Table> <Table>
<TableHeader className="bg-gray-50"> <TableHeader className="bg-gray-50">
<TableRow> <TableRow>
@@ -383,7 +382,9 @@ export default function Show({ doc }: { auth: any, doc: AdjDoc }) {
)} )}
</TableBody> </TableBody>
</Table> </Table>
</Card> </div>
</div>
<div className="bg-gray-50/80 border border-dashed border-grey-200 rounded-lg p-4 flex items-start gap-3"> <div className="bg-gray-50/80 border border-dashed border-grey-200 rounded-lg p-4 flex items-start gap-3">
<CheckCircle className="h-5 w-5 text-primary-main mt-0.5 shrink-0" /> <CheckCircle className="h-5 w-5 text-primary-main mt-0.5 shrink-0" />

View File

@@ -10,9 +10,8 @@ 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, CardHeader, CardTitle, CardDescription } from '@/Components/ui/card';
import { Badge } from '@/Components/ui/badge'; import { Badge } from '@/Components/ui/badge';
import { Save, CheckCircle, Printer, Trash2, ClipboardCheck, Eye, Pencil } from 'lucide-react'; import { Save, CheckCircle, Printer, Trash2, ClipboardCheck } from 'lucide-react';
import { import {
AlertDialog, AlertDialog,
AlertDialogAction, AlertDialogAction,
@@ -25,11 +24,10 @@ import {
AlertDialogTrigger, AlertDialogTrigger,
} from "@/Components/ui/alert-dialog" } from "@/Components/ui/alert-dialog"
import { Can } from '@/Components/Permission/Can'; import { Can } from '@/Components/Permission/Can';
import { Link } from '@inertiajs/react';
export default function Show({ doc }: any) { export default function Show({ doc }: any) {
// Transform items to form data structure // Transform items to form data structure
const { data, setData, put, delete: destroy, processing } = useForm({ const { data, setData, put, delete: destroy, processing, transform } = useForm({
items: doc.items.map((item: any) => ({ items: doc.items.map((item: any) => ({
id: item.id, id: item.id,
counted_qty: item.counted_qty, counted_qty: item.counted_qty,
@@ -46,12 +44,11 @@ export default function Show({ doc }: any) {
}; };
const handleSubmit = (action: string) => { const handleSubmit = (action: string) => {
setData('action', action); transform((data) => ({
put(route('inventory.count.update', [doc.id]), { ...data,
onSuccess: () => { action: action,
// Handle success if needed }));
} put(route('inventory.count.update', [doc.id]));
});
}; };
const handleDelete = () => { const handleDelete = () => {
@@ -75,8 +72,9 @@ export default function Show({ doc }: any) {
> >
<Head title={`盤點單 ${doc.doc_no}`} /> <Head title={`盤點單 ${doc.doc_no}`} />
<div className="container mx-auto p-6 max-w-7xl"> <div className="container mx-auto p-6 max-w-7xl animate-in fade-in duration-500">
<div className="flex items-center justify-between mb-6"> <div className="space-y-6">
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4">
<div className="flex items-center gap-4"> <div className="flex items-center gap-4">
<div> <div>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
@@ -90,8 +88,8 @@ export default function Show({ doc }: any) {
<Badge className="bg-blue-500 hover:bg-blue-600"></Badge> <Badge className="bg-blue-500 hover:bg-blue-600"></Badge>
)} )}
</div> </div>
<p className="text-sm text-gray-500 mt-1"> <p className="text-sm text-gray-500 mt-1 font-medium">
: {doc.warehouse_name} | : {doc.created_by} | : {doc.snapshot_date} : {doc.warehouse_name} <span className="mx-2">|</span> : {doc.created_by} <span className="mx-2">|</span> : {doc.snapshot_date}
</p> </p>
</div> </div>
</div> </div>
@@ -137,7 +135,7 @@ export default function Show({ doc }: any) {
disabled={processing} disabled={processing}
> >
<CheckCircle className="w-4 h-4 mr-2" /> <CheckCircle className="w-4 h-4 mr-2" />
</Button> </Button>
</Can> </Can>
</div> </div>
@@ -151,39 +149,40 @@ export default function Show({ doc }: any) {
</div> </div>
</div> </div>
<div className="max-w-7xl mx-auto space-y-6">
{!isCompleted && ( {!isCompleted && (
<Card className="border-none shadow-sm overflow-hidden"> <div className="bg-white rounded-lg shadow-sm border p-6 space-y-4">
<CardContent className="py-4"> <div className="flex items-center justify-between text-sm">
<div className="flex items-center justify-between text-sm mb-2"> <span className="text-gray-500 font-medium">: {countedItems} / {totalItems} </span>
<span className="text-gray-500">: {countedItems} / {totalItems} </span> <span className="font-bold text-primary-main">{progress}%</span>
<span className="font-semibold text-primary-main">{progress}%</span>
</div> </div>
<div className="w-full bg-gray-200 rounded-full h-2"> <div className="w-full bg-gray-200 rounded-full h-2">
<div className="bg-primary-main h-2 rounded-full transition-all duration-300" style={{ width: `${progress}%` }}></div> <div className="bg-primary-main h-2 rounded-full transition-all duration-300" style={{ width: `${progress}%` }}></div>
</div> </div>
</CardContent> </div>
</Card>
)} )}
<Card className="border border-gray-200 shadow-sm overflow-hidden gap-0"> <div className="bg-white rounded-lg shadow-sm border p-6 space-y-4">
<CardHeader className="bg-white border-b px-6 py-4"> <div className="flex items-center justify-between">
<CardTitle className="text-lg font-medium"></CardTitle> <div>
<CardDescription> <h3 className="font-semibold text-lg text-grey-900"></h3>
<p className="text-sm text-grey-500">
</CardDescription> </p>
</CardHeader> </div>
</div>
<div className="border rounded-lg overflow-hidden">
<Table> <Table>
<TableHeader className="bg-gray-50"> <TableHeader className="bg-gray-50">
<TableRow> <TableRow>
<TableHead className="w-[50px] text-center">#</TableHead> <TableHead className="w-[50px] text-center font-medium text-grey-600">#</TableHead>
<TableHead> / </TableHead> <TableHead className="font-medium text-grey-600"> / </TableHead>
<TableHead></TableHead> <TableHead className="font-medium text-grey-600"></TableHead>
<TableHead className="text-right"></TableHead> <TableHead className="text-right font-medium text-grey-600"></TableHead>
<TableHead className="text-right w-32"></TableHead> <TableHead className="text-right w-32 font-medium text-grey-600"></TableHead>
<TableHead className="text-right"></TableHead> <TableHead className="text-right font-medium text-grey-600"></TableHead>
<TableHead></TableHead> <TableHead className="font-medium text-grey-600"></TableHead>
<TableHead></TableHead> <TableHead className="font-medium text-grey-600"></TableHead>
</TableRow> </TableRow>
</TableHeader> </TableHeader>
<TableBody> <TableBody>
@@ -199,17 +198,17 @@ export default function Show({ doc }: any) {
<TableCell className="text-gray-500 font-medium text-center"> <TableCell className="text-gray-500 font-medium text-center">
{index + 1} {index + 1}
</TableCell> </TableCell>
<TableCell> <TableCell className="py-3">
<div className="flex flex-col"> <div className="flex flex-col">
<span className="font-medium text-gray-900">{item.product_code}</span> <span className="font-semibold text-gray-900">{item.product_name}</span>
<span className="text-xs text-gray-500">{item.product_name}</span> <span className="text-xs text-gray-500 font-mono">{item.product_code}</span>
</div> </div>
</TableCell> </TableCell>
<TableCell className="text-sm font-mono">{item.batch_number || '-'}</TableCell> <TableCell className="text-sm font-mono">{item.batch_number || '-'}</TableCell>
<TableCell className="text-right font-medium">{item.system_qty.toFixed(2)}</TableCell> <TableCell className="text-right font-medium">{item.system_qty.toFixed(2)}</TableCell>
<TableCell className="text-right px-1"> <TableCell className="text-right px-1 py-3">
{isCompleted ? ( {isCompleted ? (
<span className="font-medium mr-2">{item.counted_qty}</span> <span className="font-semibold mr-2">{item.counted_qty}</span>
) : ( ) : (
<Input <Input
type="number" type="number"
@@ -254,7 +253,9 @@ export default function Show({ doc }: any) {
})} })}
</TableBody> </TableBody>
</Table> </Table>
</Card> </div>
</div>
<div className="bg-gray-50/80 border border-dashed border-grey-200 rounded-lg p-4 flex items-start gap-3"> <div className="bg-gray-50/80 border border-dashed border-grey-200 rounded-lg p-4 flex items-start gap-3">
<div className="bg-blue-100 p-2 rounded-lg"> <div className="bg-blue-100 p-2 rounded-lg">
@@ -264,7 +265,7 @@ export default function Show({ doc }: any) {
<h4 className="font-semibold text-gray-900 mb-1 text-sm"></h4> <h4 className="font-semibold text-gray-900 mb-1 text-sm"></h4>
<p className="text-xs text-gray-500 leading-relaxed"> <p className="text-xs text-gray-500 leading-relaxed">
調
</p> </p>
</div> </div>
</div> </div>