diff --git a/app/Modules/Inventory/Controllers/ProductController.php b/app/Modules/Inventory/Controllers/ProductController.php index 2bdca4e..7b8ee91 100644 --- a/app/Modules/Inventory/Controllers/ProductController.php +++ b/app/Modules/Inventory/Controllers/ProductController.php @@ -152,6 +152,7 @@ class ProductController extends Controller 'price' => (float) $product->price, 'member_price' => (float) $product->member_price, 'wholesale_price' => (float) $product->wholesale_price, + 'is_active' => (bool) $product->is_active, ] ]); } @@ -188,14 +189,17 @@ class ProductController extends Controller 'price' => 'nullable|numeric|min:0', 'member_price' => 'nullable|numeric|min:0', 'wholesale_price' => 'nullable|numeric|min:0', + 'is_active' => 'boolean', ]); - $validated['is_active'] = true; - if (empty($validated['code'])) { $validated['code'] = $this->generateRandomCode(); } + if (!isset($validated['is_active'])) { + $validated['is_active'] = true; + } + $product = Product::create($validated); return redirect()->route('products.index')->with('success', '商品已建立'); @@ -224,6 +228,7 @@ class ProductController extends Controller 'price' => (float) $product->price, 'member_price' => (float) $product->member_price, 'wholesale_price' => (float) $product->wholesale_price, + 'is_active' => (bool) $product->is_active, ], 'categories' => Category::where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]), 'units' => Unit::all()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]), @@ -251,14 +256,17 @@ class ProductController extends Controller 'price' => 'nullable|numeric|min:0', 'member_price' => 'nullable|numeric|min:0', 'wholesale_price' => 'nullable|numeric|min:0', + 'is_active' => 'boolean', ]); - $validated['is_active'] = true; - if (empty($validated['code'])) { $validated['code'] = $this->generateRandomCode(); } + if (!isset($validated['is_active'])) { + $validated['is_active'] = true; + } + $product->update($validated); if ($request->input('from') === 'show') { diff --git a/app/Modules/Inventory/Models/Product.php b/app/Modules/Inventory/Models/Product.php index b8a018e..e243aba 100644 --- a/app/Modules/Inventory/Models/Product.php +++ b/app/Modules/Inventory/Models/Product.php @@ -31,10 +31,12 @@ class Product extends Model 'price', 'member_price', 'wholesale_price', + 'is_active', ]; protected $casts = [ 'conversion_rate' => 'decimal:4', + 'is_active' => 'boolean', ]; /** diff --git a/resources/js/Components/Product/ProductForm.tsx b/resources/js/Components/Product/ProductForm.tsx index a30cef4..816198a 100644 --- a/resources/js/Components/Product/ProductForm.tsx +++ b/resources/js/Components/Product/ProductForm.tsx @@ -4,6 +4,7 @@ import { Input } from "@/Components/ui/input"; import { Label } from "@/Components/ui/label"; import { Textarea } from "@/Components/ui/textarea"; import { SearchableSelect } from "@/Components/ui/searchable-select"; +import { Switch } from "@/Components/ui/switch"; import { useForm } from "@inertiajs/react"; import { toast } from "sonner"; import type { Category, Product } from "@/Pages/Product/Index"; @@ -41,6 +42,7 @@ export default function ProductForm({ price: initialData?.price?.toString() || "", member_price: initialData?.member_price?.toString() || "", wholesale_price: initialData?.wholesale_price?.toString() || "", + is_active: initialData?.is_active ?? true, }); const handleSubmit = (e: React.FormEvent) => { @@ -79,6 +81,16 @@ export default function ProductForm({