This commit is contained in:
@@ -123,11 +123,25 @@ class PurchaseOrderController extends Controller
|
|||||||
$taxAmount = round($totalAmount * 0.05, 2);
|
$taxAmount = round($totalAmount * 0.05, 2);
|
||||||
$grandTotal = $totalAmount + $taxAmount;
|
$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([
|
$order = PurchaseOrder::create([
|
||||||
'code' => $code,
|
'code' => $code,
|
||||||
'vendor_id' => $validated['vendor_id'],
|
'vendor_id' => $validated['vendor_id'],
|
||||||
'warehouse_id' => $validated['warehouse_id'],
|
'warehouse_id' => $validated['warehouse_id'],
|
||||||
'user_id' => auth()->id() ?? 1, // Fallback for dev if not using auth
|
'user_id' => $userId,
|
||||||
'status' => 'draft',
|
'status' => 'draft',
|
||||||
'expected_delivery_date' => $validated['expected_delivery_date'],
|
'expected_delivery_date' => $validated['expected_delivery_date'],
|
||||||
'total_amount' => $totalAmount,
|
'total_amount' => $totalAmount,
|
||||||
|
|||||||
@@ -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 { Button } from "@/Components/ui/button";
|
||||||
import { Link, useForm } from "@inertiajs/react";
|
import { Link, useForm } from "@inertiajs/react";
|
||||||
import type { PurchaseOrder } from "@/types/purchase-order";
|
import type { PurchaseOrder } from "@/types/purchase-order";
|
||||||
@@ -38,13 +38,13 @@ export function PurchaseOrderActions({
|
|||||||
className="button-outlined-primary h-8 w-8 p-0"
|
className="button-outlined-primary h-8 w-8 p-0"
|
||||||
title="編輯採購單"
|
title="編輯採購單"
|
||||||
>
|
>
|
||||||
<Edit className="h-4 w-4" />
|
<Pencil className="h-4 w-4" />
|
||||||
</Button>
|
</Button>
|
||||||
</Link>
|
</Link>
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
size="sm"
|
size="sm"
|
||||||
className="hover:bg-red-50 hover:text-red-600 hover:border-red-200 text-gray-500 h-8 w-8 p-0 transition-colors"
|
className="button-outlined-error h-8 w-8 p-0"
|
||||||
title="刪除採購單"
|
title="刪除採購單"
|
||||||
onClick={handleDelete}
|
onClick={handleDelete}
|
||||||
disabled={processing}
|
disabled={processing}
|
||||||
|
|||||||
@@ -275,7 +275,7 @@ export default function TransferOrderDialog({
|
|||||||
key={`${product.productId}|||${product.batchNumber}`}
|
key={`${product.productId}|||${product.batchNumber}`}
|
||||||
value={`${product.productId}|||${product.batchNumber}`}
|
value={`${product.productId}|||${product.batchNumber}`}
|
||||||
>
|
>
|
||||||
{product.productName} - 批號: {product.batchNumber} (庫存:{" "}
|
{product.productName} (庫存:{" "}
|
||||||
{product.availableQty})
|
{product.availableQty})
|
||||||
</SelectItem>
|
</SelectItem>
|
||||||
))
|
))
|
||||||
@@ -300,12 +300,14 @@ export default function TransferOrderDialog({
|
|||||||
setFormData({ ...formData, quantity: Number(e.target.value) })
|
setFormData({ ...formData, quantity: Number(e.target.value) })
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
<div className="h-5">
|
||||||
{selectedProduct && (
|
{selectedProduct && (
|
||||||
<p className="text-sm text-gray-500">
|
<p className="text-sm text-gray-500">
|
||||||
可用庫存: {selectedProduct.availableQty}
|
可用庫存: {selectedProduct.availableQty}
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label htmlFor="transferDate">
|
<Label htmlFor="transferDate">
|
||||||
|
|||||||
@@ -186,7 +186,7 @@ export default function AuthenticatedLayout({ children }: { children: React.Reac
|
|||||||
|
|
||||||
<main className="flex-1 ml-64 overflow-auto min-h-screen bg-background">
|
<main className="flex-1 ml-64 overflow-auto min-h-screen bg-background">
|
||||||
{children}
|
{children}
|
||||||
<Toaster richColors closeButton position="top-right" />
|
<Toaster richColors closeButton position="top-center" />
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -64,14 +64,29 @@ export default function CreatePurchaseOrder({
|
|||||||
const isValid = validatePurchaseOrder(String(supplierId), expectedDate, items);
|
const isValid = validatePurchaseOrder(String(supplierId), expectedDate, items);
|
||||||
|
|
||||||
const handleSave = () => {
|
const handleSave = () => {
|
||||||
if (!isValid || !warehouseId) {
|
if (!warehouseId) {
|
||||||
toast.error("請填寫完整的表單資訊");
|
toast.error("請選擇入庫倉庫");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!supplierId) {
|
||||||
|
toast.error("請選擇供應商");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!expectedDate) {
|
||||||
|
toast.error("請選擇預計到貨日期");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (items.length === 0) {
|
||||||
|
toast.error("請至少新增一項採購商品");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const validItems = filterValidItems(items);
|
const validItems = filterValidItems(items);
|
||||||
if (validItems.length === 0) {
|
if (validItems.length === 0) {
|
||||||
toast.error("請至少新增一項採購商品");
|
toast.error("請填寫有效的採購數量(必須大於 0)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,13 +104,24 @@ export default function CreatePurchaseOrder({
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (order) {
|
if (order) {
|
||||||
// Edit not implemented yet but structure is ready
|
|
||||||
router.put(`/purchase-orders/${order.id}`, data, {
|
router.put(`/purchase-orders/${order.id}`, data, {
|
||||||
onSuccess: () => toast.success("採購單已更新")
|
onSuccess: () => toast.success("採購單已更新"),
|
||||||
|
onError: (errors) => {
|
||||||
|
toast.error("更新失敗,請檢查輸入內容");
|
||||||
|
console.error(errors);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
router.post("/purchase-orders", data, {
|
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"
|
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]"
|
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}
|
onClick={handleSave}
|
||||||
disabled={!canSave}
|
|
||||||
>
|
>
|
||||||
{order ? "更新採購單" : "確認發布採購單"}
|
{order ? "更新採購單" : "確認發布採購單"}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Reference in New Issue
Block a user