2026-01-28 18:04:45 +08:00
|
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
|
|
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
|
2026-01-29 14:37:21 +08:00
|
|
|
|
import { Head, router, Link } from "@inertiajs/react";
|
2026-01-28 18:04:45 +08:00
|
|
|
|
import { Button } from "@/Components/ui/button";
|
|
|
|
|
|
import { Input } from "@/Components/ui/input";
|
|
|
|
|
|
import { Label } from "@/Components/ui/label";
|
|
|
|
|
|
import {
|
|
|
|
|
|
Table,
|
|
|
|
|
|
TableBody,
|
|
|
|
|
|
TableCell,
|
|
|
|
|
|
TableHead,
|
|
|
|
|
|
TableHeader,
|
|
|
|
|
|
TableRow,
|
|
|
|
|
|
} from "@/Components/ui/table";
|
|
|
|
|
|
import { Badge } from "@/Components/ui/badge";
|
2026-01-29 14:37:21 +08:00
|
|
|
|
import { Checkbox } from "@/Components/ui/checkbox";
|
2026-01-28 18:04:45 +08:00
|
|
|
|
import {
|
|
|
|
|
|
Dialog,
|
|
|
|
|
|
DialogContent,
|
|
|
|
|
|
DialogHeader,
|
|
|
|
|
|
DialogTitle,
|
|
|
|
|
|
DialogTrigger,
|
|
|
|
|
|
} from "@/Components/ui/dialog";
|
|
|
|
|
|
import {
|
2026-01-29 14:37:21 +08:00
|
|
|
|
AlertDialog,
|
|
|
|
|
|
AlertDialogAction,
|
|
|
|
|
|
AlertDialogCancel,
|
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
|
AlertDialogDescription,
|
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
|
AlertDialogTitle,
|
|
|
|
|
|
AlertDialogTrigger,
|
|
|
|
|
|
} from "@/Components/ui/alert-dialog";
|
|
|
|
|
|
import { Plus, Save, Trash2, ArrowLeft, CheckCircle, Package, ArrowLeftRight, Printer, Search } from "lucide-react";
|
2026-01-28 18:04:45 +08:00
|
|
|
|
import { toast } from "sonner";
|
|
|
|
|
|
import axios from "axios";
|
2026-01-29 14:37:21 +08:00
|
|
|
|
import { Can } from '@/Components/Permission/Can';
|
2026-02-03 17:29:32 +08:00
|
|
|
|
import { usePermission } from '@/hooks/usePermission';
|
2026-02-09 16:52:35 +08:00
|
|
|
|
import TransferImportDialog from '@/Components/Transfer/TransferImportDialog';
|
2026-01-28 18:04:45 +08:00
|
|
|
|
|
2026-01-29 14:37:21 +08:00
|
|
|
|
export default function Show({ order }: any) {
|
2026-02-03 17:29:32 +08:00
|
|
|
|
const { can } = usePermission();
|
2026-01-28 18:04:45 +08:00
|
|
|
|
const [items, setItems] = useState(order.items || []);
|
|
|
|
|
|
const [remarks, setRemarks] = useState(order.remarks || "");
|
|
|
|
|
|
const [isSaving, setIsSaving] = useState(false);
|
2026-01-29 14:37:21 +08:00
|
|
|
|
const [deleteId, setDeleteId] = useState<string | null>(null);
|
|
|
|
|
|
const [isPostDialogOpen, setIsPostDialogOpen] = useState(false);
|
2026-02-09 16:52:35 +08:00
|
|
|
|
const [isImportDialogOpen, setIsImportDialogOpen] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
// 當 order prop 變動時 (例如匯入後 router.reload),同步更新內部狀態
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (order) {
|
|
|
|
|
|
setItems(order.items || []);
|
|
|
|
|
|
setRemarks(order.remarks || "");
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [order]);
|
2026-01-28 18:04:45 +08:00
|
|
|
|
|
|
|
|
|
|
// Product Selection
|
|
|
|
|
|
const [isProductDialogOpen, setIsProductDialogOpen] = useState(false);
|
2026-01-29 14:37:21 +08:00
|
|
|
|
const [availableInventory, setAvailableInventory] = useState<any[]>([]);
|
2026-01-28 18:04:45 +08:00
|
|
|
|
const [loadingInventory, setLoadingInventory] = useState(false);
|
2026-01-29 14:37:21 +08:00
|
|
|
|
const [searchQuery, setSearchQuery] = useState('');
|
|
|
|
|
|
const [selectedInventory, setSelectedInventory] = useState<string[]>([]); // product_id-batch
|
2026-01-28 18:04:45 +08:00
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (isProductDialogOpen) {
|
|
|
|
|
|
loadInventory();
|
2026-01-29 14:37:21 +08:00
|
|
|
|
setSelectedInventory([]);
|
|
|
|
|
|
setSearchQuery('');
|
2026-01-28 18:04:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
}, [isProductDialogOpen]);
|
|
|
|
|
|
|
|
|
|
|
|
const loadInventory = async () => {
|
|
|
|
|
|
setLoadingInventory(true);
|
|
|
|
|
|
try {
|
|
|
|
|
|
// Fetch inventory from SOURCE warehouse
|
|
|
|
|
|
const response = await axios.get(route('api.warehouses.inventories', order.from_warehouse_id));
|
|
|
|
|
|
setAvailableInventory(response.data);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error("Failed to load inventory", error);
|
|
|
|
|
|
toast.error("無法載入庫存資料");
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setLoadingInventory(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-29 14:37:21 +08:00
|
|
|
|
const toggleSelect = (key: string) => {
|
|
|
|
|
|
setSelectedInventory(prev =>
|
|
|
|
|
|
prev.includes(key) ? prev.filter(k => k !== key) : [...prev, key]
|
2026-01-28 18:04:45 +08:00
|
|
|
|
);
|
2026-01-29 14:37:21 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const toggleSelectAll = () => {
|
|
|
|
|
|
const filtered = availableInventory.filter(inv =>
|
|
|
|
|
|
inv.product_name.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
2026-02-06 16:36:14 +08:00
|
|
|
|
inv.product_code.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
|
|
|
|
|
(inv.product_barcode && inv.product_barcode.toLowerCase().includes(searchQuery.toLowerCase()))
|
2026-01-29 14:37:21 +08:00
|
|
|
|
);
|
|
|
|
|
|
const filteredKeys = filtered.map(inv => `${inv.product_id}-${inv.batch_number}`);
|
2026-01-28 18:04:45 +08:00
|
|
|
|
|
2026-01-29 14:37:21 +08:00
|
|
|
|
if (filteredKeys.length > 0 && filteredKeys.every(k => selectedInventory.includes(k))) {
|
|
|
|
|
|
setSelectedInventory(prev => prev.filter(k => !filteredKeys.includes(k)));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
setSelectedInventory(prev => Array.from(new Set([...prev, ...filteredKeys])));
|
2026-01-28 18:04:45 +08:00
|
|
|
|
}
|
2026-01-29 14:37:21 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleAddSelected = () => {
|
|
|
|
|
|
if (selectedInventory.length === 0) return;
|
|
|
|
|
|
|
|
|
|
|
|
const newItems = [...items];
|
|
|
|
|
|
let addedCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
availableInventory.forEach(inv => {
|
|
|
|
|
|
const key = `${inv.product_id}-${inv.batch_number}`;
|
|
|
|
|
|
if (selectedInventory.includes(key)) {
|
2026-02-09 16:52:35 +08:00
|
|
|
|
newItems.push({
|
|
|
|
|
|
product_id: inv.product_id,
|
|
|
|
|
|
product_name: inv.product_name,
|
|
|
|
|
|
product_code: inv.product_code,
|
|
|
|
|
|
batch_number: inv.batch_number,
|
|
|
|
|
|
expiry_date: inv.expiry_date,
|
|
|
|
|
|
unit: inv.unit_name,
|
|
|
|
|
|
quantity: 1, // Default 1
|
|
|
|
|
|
max_quantity: inv.quantity, // Max available
|
|
|
|
|
|
notes: "",
|
|
|
|
|
|
});
|
|
|
|
|
|
addedCount++;
|
2026-01-29 14:37:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
setItems(newItems);
|
2026-01-28 18:04:45 +08:00
|
|
|
|
setIsProductDialogOpen(false);
|
2026-01-29 14:37:21 +08:00
|
|
|
|
|
|
|
|
|
|
if (addedCount > 0) {
|
|
|
|
|
|
toast.success(`已成功加入 ${addedCount} 個項目`);
|
|
|
|
|
|
}
|
2026-01-28 18:04:45 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-29 14:37:21 +08:00
|
|
|
|
const handleUpdateItem = (index: number, field: string, value: any) => {
|
2026-01-28 18:04:45 +08:00
|
|
|
|
const newItems = [...items];
|
|
|
|
|
|
newItems[index][field] = value;
|
|
|
|
|
|
setItems(newItems);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-29 14:37:21 +08:00
|
|
|
|
const handleRemoveItem = (index: number) => {
|
|
|
|
|
|
const newItems = items.filter((_: any, i: number) => i !== index);
|
2026-01-28 18:04:45 +08:00
|
|
|
|
setItems(newItems);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleSave = async () => {
|
|
|
|
|
|
setIsSaving(true);
|
|
|
|
|
|
try {
|
|
|
|
|
|
await router.put(route('inventory.transfer.update', [order.id]), {
|
|
|
|
|
|
items: items,
|
|
|
|
|
|
remarks: remarks,
|
|
|
|
|
|
}, {
|
2026-02-05 09:33:36 +08:00
|
|
|
|
onSuccess: () => { },
|
2026-01-28 18:04:45 +08:00
|
|
|
|
onError: () => toast.error("儲存失敗,請檢查輸入"),
|
|
|
|
|
|
});
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setIsSaving(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handlePost = () => {
|
|
|
|
|
|
router.put(route('inventory.transfer.update', [order.id]), {
|
|
|
|
|
|
action: 'post'
|
2026-01-29 14:37:21 +08:00
|
|
|
|
}, {
|
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
|
setIsPostDialogOpen(false);
|
2026-02-09 16:52:35 +08:00
|
|
|
|
},
|
|
|
|
|
|
onError: (errors) => {
|
|
|
|
|
|
const message = Object.values(errors).join('\n') || "過帳失敗,請檢查輸入或庫存狀態";
|
|
|
|
|
|
toast.error(message);
|
|
|
|
|
|
setIsPostDialogOpen(false);
|
2026-01-29 14:37:21 +08:00
|
|
|
|
}
|
2026-01-28 18:04:45 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleDelete = () => {
|
2026-01-29 14:37:21 +08:00
|
|
|
|
router.delete(route('inventory.transfer.destroy', [order.id]), {
|
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
|
setDeleteId(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2026-01-28 18:04:45 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-03 17:29:32 +08:00
|
|
|
|
const canEdit = can('inventory_transfer.edit');
|
|
|
|
|
|
const isReadOnly = order.status !== 'draft' || !canEdit;
|
2026-02-09 16:52:35 +08:00
|
|
|
|
const isVending = order.to_warehouse_type === 'vending';
|
2026-01-28 18:04:45 +08:00
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<AuthenticatedLayout
|
|
|
|
|
|
breadcrumbs={[
|
2026-01-29 14:37:21 +08:00
|
|
|
|
{ label: '商品與庫存管理', href: '#' },
|
2026-01-28 18:04:45 +08:00
|
|
|
|
{ label: '庫存調撥', href: route('inventory.transfer.index') },
|
2026-01-29 14:37:21 +08:00
|
|
|
|
{ label: `調撥單: ${order.doc_no}`, href: route('inventory.transfer.show', [order.id]), isPage: true },
|
2026-01-28 18:04:45 +08:00
|
|
|
|
]}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Head title={`調撥單 ${order.doc_no}`} />
|
|
|
|
|
|
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<div className="container mx-auto p-6 max-w-7xl animate-in fade-in duration-500 space-y-6">
|
|
|
|
|
|
<div>
|
2026-01-29 13:04:54 +08:00
|
|
|
|
<Link href={route('inventory.transfer.index')}>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
className="gap-2 button-outlined-primary mb-6"
|
|
|
|
|
|
>
|
|
|
|
|
|
<ArrowLeft className="h-4 w-4" />
|
|
|
|
|
|
返回調撥單列表
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Link>
|
|
|
|
|
|
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<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">
|
|
|
|
|
|
<ArrowLeftRight className="h-6 w-6 text-primary-main" />
|
|
|
|
|
|
調撥單: {order.doc_no}
|
|
|
|
|
|
</h1>
|
|
|
|
|
|
{order.status === 'completed' && <Badge className="bg-green-500 hover:bg-green-600">已完成</Badge>}
|
|
|
|
|
|
{order.status === 'draft' && <Badge className="bg-blue-500 hover:bg-blue-600">草稿</Badge>}
|
|
|
|
|
|
{order.status === 'voided' && <Badge variant="destructive">已作廢</Badge>}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<p className="text-sm text-gray-500 mt-1 font-medium">
|
|
|
|
|
|
來源: {order.from_warehouse_name} <ArrowLeftRight className="inline-block h-3 w-3 mx-1" /> 目的: {order.to_warehouse_name} <span className="mx-2">|</span> 建立人: {order.created_by}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
className="button-outlined-primary"
|
|
|
|
|
|
onClick={() => window.print()}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Printer className="w-4 h-4 mr-2" />
|
|
|
|
|
|
列印
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
2026-01-29 13:04:54 +08:00
|
|
|
|
{!isReadOnly && (
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<div className="flex items-center gap-2">
|
2026-02-03 17:29:32 +08:00
|
|
|
|
<Can permission="inventory_transfer.delete">
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<AlertDialog open={!!deleteId} onOpenChange={(open) => !open && setDeleteId(null)}>
|
|
|
|
|
|
<AlertDialogTrigger asChild>
|
|
|
|
|
|
<Button variant="outline" size="sm" className="button-outlined-error" onClick={() => setDeleteId(order.id)}>
|
|
|
|
|
|
<Trash2 className="w-4 h-4 mr-2" />
|
|
|
|
|
|
刪除
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</AlertDialogTrigger>
|
|
|
|
|
|
<AlertDialogContent>
|
|
|
|
|
|
<AlertDialogHeader>
|
|
|
|
|
|
<AlertDialogTitle>確定要刪除此調撥單嗎?</AlertDialogTitle>
|
|
|
|
|
|
<AlertDialogDescription>
|
|
|
|
|
|
此動作無法復原。如果單據已存在重要資料,請謹慎操作。
|
|
|
|
|
|
</AlertDialogDescription>
|
|
|
|
|
|
</AlertDialogHeader>
|
|
|
|
|
|
<AlertDialogFooter>
|
|
|
|
|
|
<AlertDialogCancel>取消</AlertDialogCancel>
|
2026-02-02 09:34:24 +08:00
|
|
|
|
<AlertDialogAction onClick={handleDelete} className="button-filled-error">確認刪除</AlertDialogAction>
|
2026-01-29 14:37:21 +08:00
|
|
|
|
</AlertDialogFooter>
|
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
|
</AlertDialog>
|
2026-02-03 17:29:32 +08:00
|
|
|
|
</Can>
|
2026-01-29 14:37:21 +08:00
|
|
|
|
|
2026-02-03 17:29:32 +08:00
|
|
|
|
<Can permission="inventory_transfer.edit">
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
className="button-outlined-primary"
|
|
|
|
|
|
onClick={handleSave}
|
|
|
|
|
|
disabled={isSaving}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Save className="w-4 h-4 mr-2" />
|
|
|
|
|
|
儲存草稿
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
|
|
<AlertDialog open={isPostDialogOpen} onOpenChange={setIsPostDialogOpen}>
|
|
|
|
|
|
<AlertDialogTrigger asChild>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
className="button-filled-primary"
|
|
|
|
|
|
disabled={items.length === 0 || isSaving}
|
|
|
|
|
|
>
|
|
|
|
|
|
<CheckCircle className="w-4 h-4 mr-2" />
|
|
|
|
|
|
確認過帳
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</AlertDialogTrigger>
|
|
|
|
|
|
<AlertDialogContent>
|
|
|
|
|
|
<AlertDialogHeader>
|
|
|
|
|
|
<AlertDialogTitle>確定要過帳嗎?</AlertDialogTitle>
|
|
|
|
|
|
<AlertDialogDescription>
|
|
|
|
|
|
過帳後庫存將立即從「{order.from_warehouse_name}」轉移至「{order.to_warehouse_name}」,且無法再進行修改。
|
|
|
|
|
|
</AlertDialogDescription>
|
|
|
|
|
|
</AlertDialogHeader>
|
|
|
|
|
|
<AlertDialogFooter>
|
|
|
|
|
|
<AlertDialogCancel>取消</AlertDialogCancel>
|
2026-02-02 09:34:24 +08:00
|
|
|
|
<AlertDialogAction onClick={handlePost} className="button-filled-primary">確認過帳</AlertDialogAction>
|
2026-01-29 14:37:21 +08:00
|
|
|
|
</AlertDialogFooter>
|
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
|
</AlertDialog>
|
|
|
|
|
|
</Can>
|
|
|
|
|
|
</div>
|
2026-01-29 13:04:54 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<div className="bg-white rounded-lg shadow-sm border p-6 space-y-4">
|
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
|
<Label className="text-gray-500 font-semibold">備註資訊</Label>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{isReadOnly ? (
|
|
|
|
|
|
<div className="text-gray-700 p-2 bg-gray-50 rounded border text-sm min-h-[40px]">
|
|
|
|
|
|
{order.remarks || '無備註'}
|
2026-01-28 18:04:45 +08:00
|
|
|
|
</div>
|
2026-01-29 14:37:21 +08:00
|
|
|
|
) : (
|
|
|
|
|
|
<Input
|
2026-02-09 16:52:35 +08:00
|
|
|
|
value={remarks || ""}
|
2026-01-29 14:37:21 +08:00
|
|
|
|
onChange={(e) => setRemarks(e.target.value)}
|
|
|
|
|
|
className="h-9 focus:ring-primary-main"
|
|
|
|
|
|
placeholder="填寫調撥單備註..."
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div className="bg-white rounded-lg shadow-sm border p-6 space-y-4">
|
|
|
|
|
|
<div className="flex items-center justify-between">
|
2026-01-28 18:04:45 +08:00
|
|
|
|
<div>
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<h3 className="font-semibold text-lg text-grey-900">調撥明細</h3>
|
|
|
|
|
|
<p className="text-sm text-grey-500">
|
|
|
|
|
|
請選擇要調撥的商品並輸入數量。所有商品將從「{order.from_warehouse_name}」轉出。
|
|
|
|
|
|
</p>
|
2026-01-28 18:04:45 +08:00
|
|
|
|
</div>
|
2026-01-29 14:37:21 +08:00
|
|
|
|
{!isReadOnly && (
|
2026-02-09 16:52:35 +08:00
|
|
|
|
<div className="flex gap-2">
|
|
|
|
|
|
<Button variant="outline" className="button-outlined-primary" onClick={() => setIsImportDialogOpen(true)}>
|
|
|
|
|
|
<Package className="h-4 w-4 mr-2" />
|
|
|
|
|
|
匯入 Excel
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<TransferImportDialog
|
|
|
|
|
|
open={isImportDialogOpen}
|
|
|
|
|
|
onOpenChange={setIsImportDialogOpen}
|
|
|
|
|
|
orderId={order.id}
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
<Dialog open={isProductDialogOpen} onOpenChange={setIsProductDialogOpen}>
|
|
|
|
|
|
<DialogTrigger asChild>
|
|
|
|
|
|
<Button variant="outline" className="button-outlined-primary">
|
|
|
|
|
|
<Plus className="h-4 w-4 mr-2" />
|
|
|
|
|
|
加入商品
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</DialogTrigger>
|
|
|
|
|
|
<DialogContent className="max-w-4xl max-h-[85vh] flex flex-col p-6">
|
|
|
|
|
|
<DialogHeader className="flex flex-row items-center justify-between space-y-0 pb-4">
|
|
|
|
|
|
<DialogTitle className="text-xl">選擇來源庫存 ({order.from_warehouse_name})</DialogTitle>
|
|
|
|
|
|
<div className="relative w-72">
|
|
|
|
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-grey-3" />
|
|
|
|
|
|
<Input
|
|
|
|
|
|
placeholder="搜尋品名、代號或條碼..."
|
|
|
|
|
|
className="pl-9 h-9 border-2 border-grey-3 focus:ring-primary-main"
|
|
|
|
|
|
value={searchQuery}
|
|
|
|
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
|
|
|
|
|
/>
|
2026-01-29 14:37:21 +08:00
|
|
|
|
</div>
|
2026-02-09 16:52:35 +08:00
|
|
|
|
</DialogHeader>
|
|
|
|
|
|
<div className="flex-1 overflow-auto pr-1">
|
|
|
|
|
|
{loadingInventory ? (
|
|
|
|
|
|
<div className="text-center py-12">
|
|
|
|
|
|
<Package className="h-10 w-10 animate-bounce mx-auto text-gray-300 mb-2" />
|
|
|
|
|
|
<p className="text-grey-2 text-sm">庫存資料載入中...</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<div className="border rounded-lg overflow-hidden">
|
|
|
|
|
|
<Table>
|
|
|
|
|
|
<TableHeader className="bg-gray-50/80 sticky top-0 z-10 shadow-sm">
|
|
|
|
|
|
<TableRow>
|
|
|
|
|
|
<TableHead className="w-[50px] text-center">
|
|
|
|
|
|
<Checkbox
|
|
|
|
|
|
checked={availableInventory.length > 0 && (() => {
|
|
|
|
|
|
const filtered = availableInventory.filter(inv =>
|
|
|
|
|
|
inv.product_name.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
|
|
|
|
|
inv.product_code.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
|
|
|
|
|
(inv.product_barcode && inv.product_barcode.toLowerCase().includes(searchQuery.toLowerCase()))
|
|
|
|
|
|
);
|
|
|
|
|
|
const filteredKeys = filtered.map(inv => `${inv.product_id}-${inv.batch_number}`);
|
|
|
|
|
|
return filteredKeys.length > 0 && filteredKeys.every(k => selectedInventory.includes(k));
|
|
|
|
|
|
})()}
|
|
|
|
|
|
onCheckedChange={() => toggleSelectAll()}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</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 pr-6">現有庫存</TableHead>
|
|
|
|
|
|
</TableRow>
|
|
|
|
|
|
</TableHeader>
|
|
|
|
|
|
<TableBody>
|
|
|
|
|
|
{(() => {
|
|
|
|
|
|
const filtered = availableInventory.filter(inv =>
|
|
|
|
|
|
inv.product_name.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
|
|
|
|
|
inv.product_code.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
|
|
|
|
|
(inv.product_barcode && inv.product_barcode.toLowerCase().includes(searchQuery.toLowerCase()))
|
2026-01-29 14:37:21 +08:00
|
|
|
|
);
|
2026-02-09 16:52:35 +08:00
|
|
|
|
|
|
|
|
|
|
if (filtered.length === 0) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<TableRow>
|
|
|
|
|
|
<TableCell colSpan={5} className="text-center py-12 text-grey-3 italic font-medium">
|
|
|
|
|
|
{searchQuery ? `找不到與 "${searchQuery}" 相關的商品` : '尚無庫存資料'}
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
</TableRow>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return filtered.map((inv) => {
|
|
|
|
|
|
const key = `${inv.product_id}-${inv.batch_number}`;
|
|
|
|
|
|
const isSelected = selectedInventory.includes(key);
|
|
|
|
|
|
return (
|
|
|
|
|
|
<TableRow
|
|
|
|
|
|
key={key}
|
|
|
|
|
|
className={`hover:bg-primary-lightest/20 cursor-pointer transition-colors ${isSelected ? 'bg-primary-lightest/40' : ''}`}
|
|
|
|
|
|
onClick={() => toggleSelect(key)}
|
|
|
|
|
|
>
|
|
|
|
|
|
<TableCell className="text-center" onClick={(e) => e.stopPropagation()}>
|
|
|
|
|
|
<Checkbox
|
|
|
|
|
|
checked={isSelected}
|
|
|
|
|
|
onCheckedChange={() => toggleSelect(key)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
|
|
|
|
|
|
<TableCell className="py-3">
|
|
|
|
|
|
<div className="flex flex-col">
|
|
|
|
|
|
<span className="font-semibold text-grey-0">{inv.product_name}</span>
|
|
|
|
|
|
<span className="text-xs text-grey-2 font-mono">{inv.product_code}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
<TableCell className="text-sm font-mono text-grey-2">{inv.batch_number || '-'}</TableCell>
|
|
|
|
|
|
<TableCell className="text-sm font-mono text-grey-2">{inv.expiry_date || '-'}</TableCell>
|
|
|
|
|
|
<TableCell className="text-right font-bold text-primary-main pr-6">{inv.quantity} {inv.unit_name}</TableCell>
|
|
|
|
|
|
</TableRow>
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
})()}
|
|
|
|
|
|
</TableBody>
|
|
|
|
|
|
</Table>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="mt-6 flex items-center justify-between border-t pt-4">
|
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
|
<div className="px-3 py-1 bg-primary-lightest/50 border border-primary-light/20 rounded-full text-sm font-medium text-primary-main animate-in zoom-in duration-200">
|
|
|
|
|
|
已選取 {selectedInventory.length} 項商品
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{selectedInventory.length > 0 && (
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="ghost"
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
className="text-grey-3 hover:text-red-500 hover:bg-red-50 text-xs px-2 h-7"
|
|
|
|
|
|
onClick={() => setSelectedInventory([])}
|
|
|
|
|
|
>
|
|
|
|
|
|
清除全部
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
)}
|
2026-01-29 14:37:21 +08:00
|
|
|
|
</div>
|
2026-02-09 16:52:35 +08:00
|
|
|
|
<div className="flex gap-2">
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<Button
|
2026-02-09 16:52:35 +08:00
|
|
|
|
variant="outline"
|
|
|
|
|
|
className="button-outlined-primary w-24"
|
|
|
|
|
|
onClick={() => setIsProductDialogOpen(false)}
|
2026-01-29 14:37:21 +08:00
|
|
|
|
>
|
2026-02-09 16:52:35 +08:00
|
|
|
|
取消
|
2026-01-29 14:37:21 +08:00
|
|
|
|
</Button>
|
2026-02-09 16:52:35 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
className="button-filled-primary min-w-32"
|
|
|
|
|
|
disabled={selectedInventory.length === 0}
|
|
|
|
|
|
onClick={handleAddSelected}
|
|
|
|
|
|
>
|
|
|
|
|
|
確認加入選取項
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
2026-01-29 14:37:21 +08:00
|
|
|
|
</div>
|
2026-02-09 16:52:35 +08:00
|
|
|
|
</DialogContent>
|
|
|
|
|
|
</Dialog>
|
|
|
|
|
|
</div>
|
2026-01-29 14:37:21 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</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>
|
2026-02-05 09:33:36 +08:00
|
|
|
|
<TableHead className="text-right w-32 font-medium text-grey-600">
|
|
|
|
|
|
{order.status === 'completed' ? '過帳時庫存' : '可用庫存'}
|
|
|
|
|
|
</TableHead>
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<TableHead className="text-right w-40 font-medium text-grey-600">調撥數量</TableHead>
|
|
|
|
|
|
<TableHead className="font-medium text-grey-600">單位</TableHead>
|
2026-02-09 16:52:35 +08:00
|
|
|
|
{isVending && <TableHead className="font-medium text-grey-600">貨道</TableHead>}
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<TableHead className="font-medium text-grey-600">備註</TableHead>
|
|
|
|
|
|
{!isReadOnly && <TableHead className="w-[50px]"></TableHead>}
|
|
|
|
|
|
</TableRow>
|
|
|
|
|
|
</TableHeader>
|
|
|
|
|
|
<TableBody>
|
|
|
|
|
|
{items.length === 0 ? (
|
|
|
|
|
|
<TableRow>
|
2026-02-09 16:52:35 +08:00
|
|
|
|
<TableCell colSpan={isVending ? 9 : 8} className="text-center h-24 text-gray-500">
|
2026-01-29 14:37:21 +08:00
|
|
|
|
尚未加入商品
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
</TableRow>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
items.map((item: any, index: number) => (
|
|
|
|
|
|
<TableRow key={index}>
|
|
|
|
|
|
<TableCell className="text-center text-gray-500 font-medium">{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>
|
2026-02-05 11:45:08 +08:00
|
|
|
|
<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>
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<TableCell className="text-right font-semibold text-primary-main">
|
|
|
|
|
|
{item.max_quantity} {item.unit || item.unit_name}
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
<TableCell className="px-1 py-3">
|
|
|
|
|
|
{isReadOnly ? (
|
|
|
|
|
|
<div className="text-right font-semibold mr-2">{item.quantity}</div>
|
2026-01-28 18:04:45 +08:00
|
|
|
|
) : (
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<div className="flex flex-col gap-1 items-end pr-2">
|
|
|
|
|
|
<Input
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
min="0.01"
|
2026-02-05 11:45:08 +08:00
|
|
|
|
step="any"
|
2026-02-09 16:52:35 +08:00
|
|
|
|
value={item.quantity ?? ""}
|
2026-01-29 14:37:21 +08:00
|
|
|
|
onChange={(e) => handleUpdateItem(index, 'quantity', e.target.value)}
|
2026-02-05 11:45:08 +08:00
|
|
|
|
className="h-9 w-32 font-medium focus:ring-primary-main text-right"
|
2026-01-29 14:37:21 +08:00
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
2026-01-28 18:04:45 +08:00
|
|
|
|
)}
|
2026-01-29 14:37:21 +08:00
|
|
|
|
</TableCell>
|
|
|
|
|
|
<TableCell className="text-sm text-gray-500">{item.unit || item.unit_name}</TableCell>
|
2026-02-09 16:52:35 +08:00
|
|
|
|
{isVending && (
|
|
|
|
|
|
<TableCell className="px-1">
|
|
|
|
|
|
{isReadOnly ? (
|
|
|
|
|
|
<span className="text-sm font-medium">{item.position}</span>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<Input
|
|
|
|
|
|
value={item.position || ""}
|
|
|
|
|
|
onChange={(e) => handleUpdateItem(index, 'position', e.target.value)}
|
|
|
|
|
|
placeholder="貨道..."
|
|
|
|
|
|
className="h-9 w-24 text-sm font-medium"
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
)}
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<TableCell className="px-1">
|
|
|
|
|
|
{isReadOnly ? (
|
|
|
|
|
|
<span className="text-sm text-gray-600">{item.notes}</span>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<Input
|
2026-02-09 16:52:35 +08:00
|
|
|
|
value={item.notes || ""}
|
2026-01-29 14:37:21 +08:00
|
|
|
|
onChange={(e) => handleUpdateItem(index, 'notes', e.target.value)}
|
|
|
|
|
|
placeholder="備註..."
|
|
|
|
|
|
className="h-9 text-sm"
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
{!isReadOnly && (
|
|
|
|
|
|
<TableCell className="text-center">
|
2026-02-05 11:45:08 +08:00
|
|
|
|
<Button variant="outline" size="icon" className="button-outlined-error h-8 w-8" onClick={() => handleRemoveItem(index)}>
|
2026-01-29 14:37:21 +08:00
|
|
|
|
<Trash2 className="h-4 w-4" />
|
|
|
|
|
|
</Button>
|
2026-01-28 18:04:45 +08:00
|
|
|
|
</TableCell>
|
2026-01-29 14:37:21 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</TableRow>
|
|
|
|
|
|
))
|
|
|
|
|
|
)}
|
|
|
|
|
|
</TableBody>
|
|
|
|
|
|
</Table>
|
2026-01-28 18:04:45 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</AuthenticatedLayout>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|