Files
star-erp/resources/js/Pages/Inventory/Count/Show.tsx

346 lines
19 KiB
TypeScript
Raw Normal View History

import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import { Head, useForm, Link, router } from '@inertiajs/react';
import { usePermission } from '@/hooks/usePermission'; // Added Link
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/Components/ui/table';
import { Button } from '@/Components/ui/button';
import { Input } from '@/Components/ui/input';
import { StatusBadge } from "@/Components/shared/StatusBadge";
import { Save, Printer, Trash2, ClipboardCheck, ArrowLeft, RotateCcw } from 'lucide-react'; // Added ArrowLeft
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/Components/ui/alert-dialog"
import { Can } from '@/Components/Permission/Can';
export default function Show({ doc }: any) {
// Get query parameters for dynamic back button
const urlParams = new URLSearchParams(window.location.search);
const fromSource = urlParams.get('from');
const adjustId = urlParams.get('adjust_id');
const backUrl = fromSource === 'adjust' && adjustId
? route('inventory.adjust.show', [adjustId])
: route('inventory.count.index');
const backLabel = fromSource === 'adjust' ? '返回盤調單' : '返回盤點單列表';
// Transform items to form data structure
const { data, setData, put, delete: destroy, processing, transform } = useForm({
items: doc.items.map((item: any) => ({
id: item.id,
counted_qty: item.counted_qty,
notes: item.notes || '',
})),
action: 'save', // 'save' or 'complete'
});
// Helper to update local form data
const updateItem = (index: number, field: string, value: any) => {
const newItems = [...data.items];
newItems[index][field] = value;
setData('items', newItems);
};
const handleSubmit = (action: string) => {
transform((data) => ({
...data,
action: action,
}));
put(route('inventory.count.update', [doc.id]));
};
const handleDelete = () => {
destroy(route('inventory.count.destroy', [doc.id]));
};
const handleReopen = () => {
router.visit(route('inventory.count.reopen', [doc.id]), {
method: 'put',
});
}
const { can } = usePermission();
const isCompleted = ['completed', 'no_adjust', 'adjusted'].includes(doc.status);
const canEdit = can('inventory_count.edit');
const isReadOnly = isCompleted || !canEdit;
// Calculate progress
const totalItems = doc.items.length;
const countedItems = data.items.filter((i: any) => i.counted_qty !== '' && i.counted_qty !== null).length;
const progress = Math.round((countedItems / totalItems) * 100) || 0;
return (
<AuthenticatedLayout
breadcrumbs={[
{ label: '商品與庫存管理', href: '#' },
{
label: fromSource === 'adjust' ? '庫存盤調' : '庫存盤點',
href: fromSource === 'adjust' ? route('inventory.adjust.index') : route('inventory.count.index')
},
fromSource === 'adjust' && adjustId ? {
label: `盤調單詳情`,
href: route('inventory.adjust.show', [adjustId])
} : null,
{ label: `盤點單: ${doc.doc_no}`, href: route('inventory.count.show', [doc.id]), isPage: true },
].filter(Boolean) as any}
>
<Head title={`盤點單 ${doc.doc_no}`} />
<div className="container mx-auto p-6 max-w-7xl animate-in fade-in duration-500 space-y-6">
<div>
<Link href={backUrl}>
<Button
variant="outline"
className="gap-2 button-outlined-primary mb-6"
>
<ArrowLeft className="h-4 w-4" />
{backLabel}
</Button>
</Link>
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4">
<div>
<div className="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" />
: {doc.doc_no}
</h1>
{doc.status === 'completed' && (
<StatusBadge variant="success"></StatusBadge>
)}
{doc.status === 'no_adjust' && (
<StatusBadge variant="success"> (調)</StatusBadge>
)}
{doc.status === 'adjusted' && (
<StatusBadge variant="warning">調</StatusBadge>
)}
{doc.status === 'draft' && (
<StatusBadge variant="info"></StatusBadge>
)}
</div>
<p className="text-sm text-gray-500 mt-1 font-medium">
: {doc.warehouse_name} <span className="mx-2">|</span> : {doc.created_by} <span className="mx-2">|</span> : {doc.snapshot_date}
</p>
</div>
<div className="flex items-center gap-2">
<Button
variant="outline"
size="sm"
className="button-outlined-primary"
onClick={() => window.open(route('inventory.count.print', [doc.id]), '_blank')}
>
<Printer className="w-4 h-4 mr-2" />
</Button>
{['completed', 'no_adjust'].includes(doc.status) && (
<Can permission="inventory.adjust">
<AlertDialog>
<AlertDialogTrigger asChild>
<Button
variant="outline"
size="sm"
className="button-outlined-error"
disabled={processing}
>
<RotateCcw className="w-4 h-4 mr-2" />
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle></AlertDialogTitle>
<AlertDialogDescription>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel></AlertDialogCancel>
<AlertDialogAction onClick={handleReopen} className="bg-red-600 hover:bg-red-700"></AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</Can>
)}
{!isCompleted && (
<div className="flex items-center gap-2">
<Can permission="inventory_count.delete">
<AlertDialog>
<AlertDialogTrigger asChild>
<Button variant="outline" size="sm" disabled={processing} className="button-outlined-error">
<Trash2 className="w-4 h-4 mr-2" />
</Button>
</AlertDialogTrigger>
<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>
</Can>
<Can permission="inventory_count.edit">
<Button
size="sm"
className="button-filled-primary"
onClick={() => handleSubmit('save')}
disabled={processing}
>
<Save className="w-4 h-4 mr-2" />
</Button>
</Can>
</div>
)}
</div>
</div>
</div>
{!isCompleted && (
<div className="bg-white rounded-lg shadow-sm border p-6 space-y-4">
<div className="flex items-center justify-between text-sm">
<span className="text-gray-500 font-medium">: {countedItems} / {totalItems} </span>
<span className="font-bold text-primary-main">{progress}%</span>
</div>
<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>
</div>
)}
<div className="bg-white rounded-lg shadow-sm border p-6 space-y-4">
<div className="flex items-center justify-between">
<div>
<h3 className="font-semibold text-lg text-grey-900"></h3>
<p className="text-sm text-grey-500">
</p>
</div>
</div>
<div className="border rounded-lg overflow-hidden">
<Table>
<TableHeader className="bg-gray-50">
<TableRow>
<TableHead className="w-[50px] text-center 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-right w-32 font-medium text-grey-600"></TableHead>
<TableHead className="text-right font-medium text-grey-600"></TableHead>
<TableHead className="font-medium text-grey-600"></TableHead>
<TableHead className="font-medium text-grey-600"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{doc.items.map((item: any, index: number) => {
const formItem = data.items[index];
const diff = formItem.counted_qty !== '' && formItem.counted_qty !== null
? (parseFloat(formItem.counted_qty) - item.system_qty)
: 0;
const hasDiff = Math.abs(diff) > 0.0001;
return (
<TableRow key={item.id} className={hasDiff && formItem.counted_qty !== '' ? "bg-red-50/30" : ""}>
<TableCell className="text-gray-500 font-medium text-center">
{index + 1}
</TableCell>
<TableCell className="py-3">
<div className="flex flex-col">
<span className="font-semibold text-gray-900">{item.product_name}</span>
<span className="text-xs text-gray-500 font-mono">{item.product_code}</span>
</div>
</TableCell>
<TableCell className="text-sm font-mono">
<div>{item.batch_number || '-'}</div>
{item.expiry_date && (
<div className="text-xs text-gray-400 mt-1">
: {item.expiry_date}
</div>
)}
</TableCell>
<TableCell className="text-right font-medium">{Number(item.system_qty)}</TableCell>
<TableCell className="text-right px-1 py-3">
{isReadOnly ? (
<span className="font-semibold mr-2">{item.counted_qty}</span>
) : (
<Input
type="number"
step="any"
value={formItem.counted_qty ?? ''}
onChange={(e) => updateItem(index, 'counted_qty', e.target.value)}
onWheel={(e: any) => e.target.blur()}
disabled={processing}
className="h-9 font-medium focus:ring-primary-main text-right"
placeholder="盤點..."
/>
)}
</TableCell>
<TableCell className="text-right">
<span className={`font-bold ${!hasDiff
? 'text-gray-400'
: diff > 0
? 'text-green-600'
: 'text-red-600'
}`}>
{formItem.counted_qty !== '' && formItem.counted_qty !== null
? Number(diff.toFixed(2))
: '-'}
</span>
</TableCell>
<TableCell className="text-sm text-gray-500">{item.unit || item.unit_name}</TableCell>
<TableCell className="px-1">
{isReadOnly ? (
<span className="text-sm text-gray-600">{item.notes}</span>
) : (
<Input
value={formItem.notes}
onChange={(e) => updateItem(index, 'notes', e.target.value)}
disabled={processing}
className="h-9 text-sm"
placeholder="備註..."
/>
)}
</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</div>
</div>
</div>
</AuthenticatedLayout >
);
}