feat: 優化採購單操作紀錄與統一刪除確認 UI
- 優化採購單更新與刪除的活動紀錄邏輯 (PurchaseOrderController) - 整合更新異動為單一紀錄,包含品項差異 - 刪除時記錄當下品項快照 - 統一採購單刪除確認介面,使用 AlertDialog 取代原生 confirm (PurchaseOrderActions) - Refactor: 將 ActivityDetailDialog 移至 Components/ActivityLog 並優化樣式與大數據顯示 - 調整 UI 文字:將「總金額」統一為「小計」 - 其他模型與 Controller 的活動紀錄支援更新
This commit is contained in:
@@ -1,239 +0,0 @@
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/Components/ui/dialog";
|
||||
import { Badge } from "@/Components/ui/badge";
|
||||
import { ScrollArea } from "@/Components/ui/scroll-area";
|
||||
import { User, Clock, Package, Activity as ActivityIcon } from "lucide-react";
|
||||
|
||||
interface Activity {
|
||||
id: number;
|
||||
description: string;
|
||||
subject_type: string;
|
||||
event: string;
|
||||
causer: string;
|
||||
created_at: string;
|
||||
properties: {
|
||||
attributes?: Record<string, any>;
|
||||
old?: Record<string, any>;
|
||||
};
|
||||
}
|
||||
|
||||
interface Props {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
activity: Activity | null;
|
||||
}
|
||||
|
||||
// Field translation map
|
||||
const fieldLabels: Record<string, string> = {
|
||||
name: '名稱',
|
||||
code: '代碼',
|
||||
description: '描述',
|
||||
price: '價格',
|
||||
cost: '成本',
|
||||
stock: '庫存',
|
||||
category_id: '分類',
|
||||
unit_id: '單位',
|
||||
is_active: '啟用狀態',
|
||||
conversion_rate: '換算率',
|
||||
specification: '規格',
|
||||
brand: '品牌',
|
||||
base_unit_id: '基本單位',
|
||||
large_unit_id: '大單位',
|
||||
purchase_unit_id: '採購單位',
|
||||
email: 'Email',
|
||||
password: '密碼',
|
||||
phone: '電話',
|
||||
address: '地址',
|
||||
role_id: '角色',
|
||||
// Snapshot fields
|
||||
category_name: '分類名稱',
|
||||
base_unit_name: '基本單位名稱',
|
||||
large_unit_name: '大單位名稱',
|
||||
purchase_unit_name: '採購單位名稱',
|
||||
// Warehouse & Inventory fields
|
||||
warehouse_name: '倉庫名稱',
|
||||
product_name: '商品名稱',
|
||||
warehouse_id: '倉庫',
|
||||
product_id: '商品',
|
||||
quantity: '數量',
|
||||
safety_stock: '安全庫存',
|
||||
location: '儲位',
|
||||
};
|
||||
|
||||
export default function ActivityDetailDialog({ open, onOpenChange, activity }: Props) {
|
||||
if (!activity) return null;
|
||||
|
||||
const attributes = activity.properties?.attributes || {};
|
||||
const old = activity.properties?.old || {};
|
||||
|
||||
// Get all keys from both attributes and old to ensure we show all changes
|
||||
const allKeys = Array.from(new Set([...Object.keys(attributes), ...Object.keys(old)]));
|
||||
|
||||
// Filter out internal keys often logged but not useful for users
|
||||
const filteredKeys = allKeys.filter(key =>
|
||||
!['created_at', 'updated_at', 'deleted_at', 'id'].includes(key)
|
||||
);
|
||||
|
||||
const getEventBadgeClass = (event: string) => {
|
||||
switch (event) {
|
||||
case 'created': return 'bg-green-100 text-green-700 hover:bg-green-200 border-green-200';
|
||||
case 'updated': return 'bg-blue-100 text-blue-700 hover:bg-blue-200 border-blue-200';
|
||||
case 'deleted': return 'bg-red-100 text-red-700 hover:bg-red-200 border-red-200';
|
||||
default: return 'bg-gray-100 text-gray-700 hover:bg-gray-200 border-gray-200';
|
||||
}
|
||||
};
|
||||
|
||||
const getEventLabel = (event: string) => {
|
||||
switch (event) {
|
||||
case 'created': return '新增';
|
||||
case 'updated': return '更新';
|
||||
case 'deleted': return '刪除';
|
||||
default: return event;
|
||||
}
|
||||
};
|
||||
|
||||
const formatValue = (key: string, value: any) => {
|
||||
if (value === null || value === undefined) return <span className="text-gray-400">-</span>;
|
||||
|
||||
// Special handling for boolean values based on key
|
||||
if (typeof value === 'boolean') {
|
||||
if (key === 'is_active') return value ? '啟用' : '停用';
|
||||
return value ? '是' : '否';
|
||||
}
|
||||
|
||||
if (typeof value === 'object') return JSON.stringify(value);
|
||||
return String(value);
|
||||
};
|
||||
|
||||
const getFieldName = (key: string) => {
|
||||
return fieldLabels[key] || key.charAt(0).toUpperCase() + key.slice(1).replace(/_/g, ' ');
|
||||
};
|
||||
|
||||
// Helper to check if a key is a snapshot name field
|
||||
const isSnapshotField = (key: string) => {
|
||||
return ['category_name', 'base_unit_name', 'large_unit_name', 'purchase_unit_name', 'warehouse_name', 'product_name'].includes(key);
|
||||
};
|
||||
|
||||
// Helper to get formatted value (merging ID and Name if available)
|
||||
const getFormattedValue = (key: string, value: any, allData: Record<string, any>) => {
|
||||
// If it's an ID field, check if we have a corresponding name snapshot
|
||||
if (key.endsWith('_id')) {
|
||||
const nameKey = key.replace('_id', '_name');
|
||||
const nameValue = allData[nameKey];
|
||||
if (nameValue) {
|
||||
return `${nameValue}`;
|
||||
}
|
||||
}
|
||||
return formatValue(key, value);
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-2xl">
|
||||
<DialogHeader className="border-b pb-4">
|
||||
<DialogTitle className="flex items-center gap-3">
|
||||
<span className="text-xl font-bold text-gray-900">操作詳情</span>
|
||||
<Badge variant="outline" className={`text-sm px-3 py-1 ${getEventBadgeClass(activity.event)}`}>
|
||||
{getEventLabel(activity.event)}
|
||||
</Badge>
|
||||
</DialogTitle>
|
||||
|
||||
{/* Modern Metadata Strip */}
|
||||
<div className="flex flex-wrap items-center gap-6 pt-4 text-sm text-gray-500">
|
||||
<div className="flex items-center gap-2">
|
||||
<User className="w-4 h-4 text-gray-400" />
|
||||
<span className="font-medium text-gray-700">{activity.causer}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Clock className="w-4 h-4 text-gray-400" />
|
||||
<span>{activity.created_at}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Package className="w-4 h-4 text-gray-400" />
|
||||
<span>{activity.subject_type}</span>
|
||||
</div>
|
||||
{/* Only show 'description' if it differs from event name (unlikely but safe) */}
|
||||
{activity.description !== getEventLabel(activity.event) &&
|
||||
activity.description !== 'created' && activity.description !== 'updated' && (
|
||||
<div className="flex items-center gap-2">
|
||||
<ActivityIcon className="w-4 h-4 text-gray-400" />
|
||||
<span>{activity.description}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="py-4">
|
||||
{activity.event === 'created' ? (
|
||||
<div className="border rounded-md overflow-hidden bg-white">
|
||||
<div className="grid grid-cols-2 bg-gray-50/80 px-4 py-2 text-xs font-semibold text-gray-500 uppercase tracking-wider border-b">
|
||||
<div>欄位</div>
|
||||
<div>初始內容</div>
|
||||
</div>
|
||||
<ScrollArea className="h-[300px]">
|
||||
<div className="divide-y divide-gray-100">
|
||||
{filteredKeys
|
||||
.filter(key => attributes[key] !== null && attributes[key] !== '' && !isSnapshotField(key))
|
||||
.map((key) => (
|
||||
<div key={key} className="grid grid-cols-2 px-4 py-3 text-sm hover:bg-gray-50/50 transition-colors">
|
||||
<div className="font-medium text-gray-700">{getFieldName(key)}</div>
|
||||
<div className="text-gray-900 font-medium">
|
||||
{getFormattedValue(key, attributes[key], attributes)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{filteredKeys.filter(key => attributes[key] !== null && attributes[key] !== '' && !isSnapshotField(key)).length === 0 && (
|
||||
<div className="p-8 text-center text-gray-500 text-sm">
|
||||
無初始資料
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</ScrollArea>
|
||||
</div>
|
||||
) : (
|
||||
<div className="border rounded-md overflow-hidden bg-white">
|
||||
<div className="grid grid-cols-3 bg-gray-50/80 px-4 py-2 text-xs font-semibold text-gray-500 uppercase tracking-wider border-b">
|
||||
<div>欄位</div>
|
||||
<div>異動前</div>
|
||||
<div>異動後</div>
|
||||
</div>
|
||||
<ScrollArea className="h-[300px]">
|
||||
{filteredKeys.some(key => !isSnapshotField(key)) ? (
|
||||
<div className="divide-y divide-gray-100">
|
||||
{filteredKeys
|
||||
.filter(key => !isSnapshotField(key))
|
||||
.map((key) => {
|
||||
const oldValue = old[key];
|
||||
const newValue = attributes[key];
|
||||
const isChanged = JSON.stringify(oldValue) !== JSON.stringify(newValue);
|
||||
|
||||
return (
|
||||
<div key={key} className={`grid grid-cols-3 px-4 py-3 text-sm transition-colors ${isChanged ? 'bg-amber-50/50' : 'hover:bg-gray-50/50'}`}>
|
||||
<div className="font-medium text-gray-700 flex items-center">{getFieldName(key)}</div>
|
||||
<div className="text-gray-500 break-words pr-4">
|
||||
{getFormattedValue(key, oldValue, old)}
|
||||
</div>
|
||||
<div className="text-gray-900 font-medium break-words">
|
||||
{activity.event === 'deleted' ? '-' : getFormattedValue(key, newValue, attributes)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
) : (
|
||||
<div className="p-8 text-center text-gray-500 text-sm">
|
||||
無詳細異動內容
|
||||
</div>
|
||||
)}
|
||||
</ScrollArea>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -2,30 +2,11 @@ import { useState } from 'react';
|
||||
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
|
||||
import { Head, router } from '@inertiajs/react';
|
||||
import { PageProps } from '@/types/global';
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@/Components/ui/table";
|
||||
import { Badge } from "@/Components/ui/badge";
|
||||
import Pagination from '@/Components/shared/Pagination';
|
||||
import { SearchableSelect } from "@/Components/ui/searchable-select";
|
||||
import { FileText, Eye, ArrowUpDown, ArrowUp, ArrowDown } from 'lucide-react';
|
||||
import { Button } from '@/Components/ui/button';
|
||||
import ActivityDetailDialog from './ActivityDetailDialog';
|
||||
|
||||
interface Activity {
|
||||
id: number;
|
||||
description: string;
|
||||
subject_type: string;
|
||||
event: string;
|
||||
causer: string;
|
||||
created_at: string;
|
||||
properties: any;
|
||||
}
|
||||
import { FileText } from 'lucide-react';
|
||||
import LogTable, { Activity } from '@/Components/ActivityLog/LogTable';
|
||||
import ActivityDetailDialog from '@/Components/ActivityLog/ActivityDetailDialog';
|
||||
|
||||
interface PaginationLinks {
|
||||
url: string | null;
|
||||
@@ -54,82 +35,6 @@ export default function ActivityLogIndex({ activities, filters }: Props) {
|
||||
const [selectedActivity, setSelectedActivity] = useState<Activity | null>(null);
|
||||
const [detailOpen, setDetailOpen] = useState(false);
|
||||
|
||||
const getEventBadgeClass = (event: string) => {
|
||||
switch (event) {
|
||||
case 'created': return 'bg-green-50 text-green-700 border-green-200 hover:bg-green-100';
|
||||
case 'updated': return 'bg-blue-50 text-blue-700 border-blue-200 hover:bg-blue-100';
|
||||
case 'deleted': return 'bg-red-50 text-red-700 border-red-200 hover:bg-red-100';
|
||||
default: return 'bg-gray-50 text-gray-700 border-gray-200 hover:bg-gray-100';
|
||||
}
|
||||
};
|
||||
|
||||
const getEventLabel = (event: string) => {
|
||||
switch (event) {
|
||||
case 'created': return '新增';
|
||||
case 'updated': return '更新';
|
||||
case 'deleted': return '刪除';
|
||||
default: return event;
|
||||
}
|
||||
};
|
||||
|
||||
const getDescription = (activity: Activity) => {
|
||||
const props = activity.properties || {};
|
||||
const attrs = props.attributes || {};
|
||||
const old = props.old || {};
|
||||
|
||||
// Try to find a name in attributes or old values
|
||||
// Priority: specific name fields > generic name > code > ID
|
||||
const nameParams = ['product_name', 'warehouse_name', 'category_name', 'base_unit_name', 'name', 'code', 'title'];
|
||||
let subjectName = '';
|
||||
|
||||
// Special handling for Inventory: show "Warehouse - Product"
|
||||
if (attrs.warehouse_name && attrs.product_name) {
|
||||
subjectName = `${attrs.warehouse_name} - ${attrs.product_name}`;
|
||||
} else if (old.warehouse_name && old.product_name) {
|
||||
subjectName = `${old.warehouse_name} - ${old.product_name}`;
|
||||
} else {
|
||||
// Default fallback
|
||||
for (const param of nameParams) {
|
||||
if (attrs[param]) {
|
||||
subjectName = attrs[param];
|
||||
break;
|
||||
}
|
||||
if (old[param]) {
|
||||
subjectName = old[param];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If no name found, try ID but format it nicely if possible, or just don't show it if it's redundant with subject_type
|
||||
if (!subjectName && (attrs.id || old.id)) {
|
||||
subjectName = `#${attrs.id || old.id}`;
|
||||
}
|
||||
|
||||
// Combine parts: [Causer] [Action] [Name] [Subject]
|
||||
// Example: Admin 新增 可樂 商品
|
||||
// Example: Admin 更新 台北倉 - 可樂 庫存
|
||||
return (
|
||||
<span className="flex items-center gap-1.5 flex-wrap">
|
||||
<span className="font-medium text-gray-900">{activity.causer}</span>
|
||||
<span className="text-gray-500">{getEventLabel(activity.event)}</span>
|
||||
{subjectName && (
|
||||
<span className="font-medium text-primary-600 bg-primary-50 px-1.5 py-0.5 rounded text-xs">
|
||||
{subjectName}
|
||||
</span>
|
||||
)}
|
||||
<span className="text-gray-700">{activity.subject_type}</span>
|
||||
|
||||
{/* Display reason/source if available (e.g., from Replenishment) */}
|
||||
{(attrs._reason || old._reason) && (
|
||||
<span className="text-gray-500 text-xs">
|
||||
(來自 {attrs._reason || old._reason})
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
const handleViewDetail = (activity: Activity) => {
|
||||
setSelectedActivity(activity);
|
||||
setDetailOpen(true);
|
||||
@@ -164,16 +69,6 @@ export default function ActivityLogIndex({ activities, filters }: Props) {
|
||||
);
|
||||
};
|
||||
|
||||
const SortIcon = ({ field }: { field: string }) => {
|
||||
if (filters.sort_by !== field) {
|
||||
return <ArrowUpDown className="h-4 w-4 text-muted-foreground ml-1" />;
|
||||
}
|
||||
if (filters.sort_order === "asc") {
|
||||
return <ArrowUp className="h-4 w-4 text-primary-main ml-1" />;
|
||||
}
|
||||
return <ArrowDown className="h-4 w-4 text-primary-main ml-1" />;
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthenticatedLayout
|
||||
breadcrumbs={[
|
||||
@@ -196,75 +91,14 @@ export default function ActivityLogIndex({ activities, filters }: Props) {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="bg-white rounded-xl border border-gray-200 shadow-sm overflow-hidden">
|
||||
<Table>
|
||||
<TableHeader className="bg-gray-50">
|
||||
<TableRow>
|
||||
<TableHead className="w-[50px] text-center">#</TableHead>
|
||||
<TableHead className="w-[180px]">
|
||||
<button
|
||||
onClick={() => handleSort('created_at')}
|
||||
className="flex items-center gap-1 hover:text-gray-900 transition-colors"
|
||||
>
|
||||
時間 <SortIcon field="created_at" />
|
||||
</button>
|
||||
</TableHead>
|
||||
<TableHead className="w-[150px]">操作人員</TableHead>
|
||||
<TableHead>描述</TableHead>
|
||||
<TableHead className="w-[100px] text-center">動作</TableHead>
|
||||
<TableHead>對象</TableHead>
|
||||
<TableHead className="w-[100px] text-center">操作</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{activities.data.length > 0 ? (
|
||||
activities.data.map((activity, index) => (
|
||||
<TableRow key={activity.id}>
|
||||
<TableCell className="text-gray-500 font-medium text-center">
|
||||
{activities.from + index}
|
||||
</TableCell>
|
||||
<TableCell className="text-gray-500 font-medium whitespace-nowrap">
|
||||
{activity.created_at}
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<span className="font-medium text-gray-900">{activity.causer}</span>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
{getDescription(activity)}
|
||||
</TableCell>
|
||||
<TableCell className="text-center">
|
||||
<Badge variant="outline" className={getEventBadgeClass(activity.event)}>
|
||||
{getEventLabel(activity.event)}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant="outline" className="bg-slate-50 text-slate-600 border-slate-200">
|
||||
{activity.subject_type}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell className="text-center">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => handleViewDetail(activity)}
|
||||
className="button-outlined-primary"
|
||||
title="檢視詳情"
|
||||
>
|
||||
<Eye className="h-4 w-4" />
|
||||
</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))
|
||||
) : (
|
||||
<TableRow>
|
||||
<TableCell colSpan={6} className="text-center py-8 text-gray-500">
|
||||
尚無操作紀錄
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
<LogTable
|
||||
activities={activities.data}
|
||||
sortField={filters.sort_by}
|
||||
sortOrder={filters.sort_order}
|
||||
onSort={handleSort}
|
||||
onViewDetail={handleViewDetail}
|
||||
from={activities.from}
|
||||
/>
|
||||
|
||||
<div className="mt-4 flex flex-col sm:flex-row items-center justify-between gap-4">
|
||||
<div className="flex items-center gap-2 text-sm text-gray-500">
|
||||
|
||||
Reference in New Issue
Block a user