diff --git a/app/Http/Controllers/PurchaseOrderController.php b/app/Http/Controllers/PurchaseOrderController.php index 8e2f750..8755c5a 100644 --- a/app/Http/Controllers/PurchaseOrderController.php +++ b/app/Http/Controllers/PurchaseOrderController.php @@ -123,11 +123,25 @@ class PurchaseOrderController extends Controller $taxAmount = round($totalAmount * 0.05, 2); $grandTotal = $totalAmount + $taxAmount; + // 確保有一個有效的使用者 ID + $userId = auth()->id(); + if (!$userId) { + $user = \App\Models\User::first(); + if (!$user) { + $user = \App\Models\User::create([ + 'name' => '系統管理員', + 'email' => 'admin@example.com', + 'password' => bcrypt('password'), + ]); + } + $userId = $user->id; + } + $order = PurchaseOrder::create([ 'code' => $code, 'vendor_id' => $validated['vendor_id'], 'warehouse_id' => $validated['warehouse_id'], - 'user_id' => auth()->id() ?? 1, // Fallback for dev if not using auth + 'user_id' => $userId, 'status' => 'draft', 'expected_delivery_date' => $validated['expected_delivery_date'], 'total_amount' => $totalAmount, diff --git a/resources/js/Components/PurchaseOrder/PurchaseOrderActions.tsx b/resources/js/Components/PurchaseOrder/PurchaseOrderActions.tsx index 8f16d2f..16ec08a 100644 --- a/resources/js/Components/PurchaseOrder/PurchaseOrderActions.tsx +++ b/resources/js/Components/PurchaseOrder/PurchaseOrderActions.tsx @@ -1,4 +1,4 @@ -import { Edit, Eye, Trash2 } from "lucide-react"; +import { Pencil, Eye, Trash2 } from "lucide-react"; import { Button } from "@/Components/ui/button"; import { Link, useForm } from "@inertiajs/react"; import type { PurchaseOrder } from "@/types/purchase-order"; @@ -38,13 +38,13 @@ export function PurchaseOrderActions({ className="button-outlined-primary h-8 w-8 p-0" title="編輯採購單" > - + ( - {product.productName} - 批號: {product.batchNumber} (庫存:{" "} + {product.productName} (庫存:{" "} {product.availableQty}) )) @@ -300,11 +300,13 @@ export default function TransferOrderDialog({ setFormData({ ...formData, quantity: Number(e.target.value) }) } /> - {selectedProduct && ( - - 可用庫存: {selectedProduct.availableQty} - - )} + + {selectedProduct && ( + + 可用庫存: {selectedProduct.availableQty} + + )} + diff --git a/resources/js/Layouts/AuthenticatedLayout.tsx b/resources/js/Layouts/AuthenticatedLayout.tsx index d2e0ea0..67035c0 100644 --- a/resources/js/Layouts/AuthenticatedLayout.tsx +++ b/resources/js/Layouts/AuthenticatedLayout.tsx @@ -186,7 +186,7 @@ export default function AuthenticatedLayout({ children }: { children: React.Reac {children} - + ); diff --git a/resources/js/Pages/PurchaseOrder/Create.tsx b/resources/js/Pages/PurchaseOrder/Create.tsx index 5a6fd3f..68d5ca6 100644 --- a/resources/js/Pages/PurchaseOrder/Create.tsx +++ b/resources/js/Pages/PurchaseOrder/Create.tsx @@ -64,14 +64,29 @@ export default function CreatePurchaseOrder({ const isValid = validatePurchaseOrder(String(supplierId), expectedDate, items); const handleSave = () => { - if (!isValid || !warehouseId) { - toast.error("請填寫完整的表單資訊"); + if (!warehouseId) { + toast.error("請選擇入庫倉庫"); + return; + } + + if (!supplierId) { + toast.error("請選擇供應商"); + return; + } + + if (!expectedDate) { + toast.error("請選擇預計到貨日期"); + return; + } + + if (items.length === 0) { + toast.error("請至少新增一項採購商品"); return; } const validItems = filterValidItems(items); if (validItems.length === 0) { - toast.error("請至少新增一項採購商品"); + toast.error("請填寫有效的採購數量(必須大於 0)"); return; } @@ -89,13 +104,24 @@ export default function CreatePurchaseOrder({ }; if (order) { - // Edit not implemented yet but structure is ready router.put(`/purchase-orders/${order.id}`, data, { - onSuccess: () => toast.success("採購單已更新") + onSuccess: () => toast.success("採購單已更新"), + onError: (errors) => { + toast.error("更新失敗,請檢查輸入內容"); + console.error(errors); + } }); } else { router.post("/purchase-orders", data, { - onSuccess: () => toast.success("採購單已成功建立") + onSuccess: () => toast.success("採購單已成功建立"), + onError: (errors) => { + if (errors.error) { + toast.error(errors.error); + } else { + toast.error("建立失敗,請檢查輸入內容"); + } + console.error(errors); + } }); } }; @@ -284,7 +310,6 @@ export default function CreatePurchaseOrder({ size="lg" className="bg-primary hover:bg-primary/90 text-white px-12 h-14 rounded-xl shadow-lg shadow-primary/20 text-lg font-bold transition-all hover:scale-[1.02] active:scale-[0.98]" onClick={handleSave} - disabled={!canSave} > {order ? "更新採購單" : "確認發布採購單"}
- 可用庫存: {selectedProduct.availableQty} -
+ 可用庫存: {selectedProduct.availableQty} +