77use App \Modules \Inventory \Models \ProductBundleItem ;
88use Illuminate \Http \RedirectResponse ;
99use Illuminate \Http \Request ;
10- use Illuminate \Support \Facades \DB ;
1110use Illuminate \Validation \Rule ;
1211use Inertia \Inertia ;
1312use Inertia \Response ;
@@ -17,112 +16,72 @@ class ProductBundleController extends Controller
1716 public function index (): Response
1817 {
1918 $ this ->authorize ('viewAny ' , Product::class);
20-
2119 $ bundles = Product::with ('bundleItems.componentProduct ' )
20+ ->where ('tenant_id ' , app ('tenant ' )->id )
2221 ->where ('is_bundle ' , true )
23- ->paginate (15 );
24-
25- return Inertia::render ('Inventory/ProductBundles/Index ' , [
26- 'bundles ' => $ bundles ,
27- ]);
28- }
29-
30- public function create (): Response
31- {
32- $ this ->authorize ('create ' , Product::class);
33-
34- $ products = Product::where ('is_bundle ' , false )
35- ->orderBy ('name ' )
36- ->get (['id ' , 'name ' , 'sku ' ]);
37-
38- return Inertia::render ('Inventory/ProductBundles/Create ' , [
39- 'products ' => $ products ,
40- ]);
22+ ->paginate (20 );
23+ return Inertia::render ('Inventory/ProductBundles/Index ' , compact ('bundles ' ));
4124 }
4225
4326 public function store (Request $ request ): RedirectResponse
4427 {
4528 $ this ->authorize ('create ' , Product::class);
46-
4729 $ validated = $ request ->validate ([
48- 'name ' => [ 'required ' , ' string ' , ' max:255 '] ,
49- 'sku ' => [ 'nullable ' , ' string ' , ' max:100 '] ,
50- 'description ' => [ 'nullable ' , ' string '] ,
51- 'selling_price ' => [ 'nullable ' , ' numeric ' , ' min:0 '] ,
52- 'items ' => [ 'required ' , ' array ' , ' min:1 '] ,
30+ 'name ' => 'required| string| max:255 ' ,
31+ 'sku ' => 'nullable| string| max:100 ' ,
32+ 'description ' => 'nullable| string ' ,
33+ 'selling_price ' => 'nullable| numeric| min:0 ' ,
34+ 'items ' => 'required| array| min:1 ' ,
5335 'items.*.component_product_id ' => ['required ' , Rule::exists ('products ' , 'id ' )],
54- 'items.*.quantity ' => [ 'required ' , ' numeric ' , ' min:0.0001 '] ,
36+ 'items.*.quantity ' => 'required| numeric| min:0.0001 ' ,
5537 ]);
5638
57- $ bundleId = null ;
39+ $ bundle = Product::create ([
40+ 'tenant_id ' => app ('tenant ' )->id ,
41+ 'name ' => $ validated ['name ' ],
42+ 'sku ' => $ validated ['sku ' ] ?? null ,
43+ 'description ' => $ validated ['description ' ] ?? null ,
44+ 'sale_price ' => $ validated ['selling_price ' ] ?? 0 ,
45+ 'cost_price ' => 0 ,
46+ 'is_bundle ' => true ,
47+ 'is_active ' => true ,
48+ ]);
5849
59- DB ::transaction (function () use ($ validated , &$ bundleId ) {
60- $ bundle = Product::create ([
61- 'name ' => $ validated ['name ' ],
62- 'sku ' => $ validated ['sku ' ] ?? null ,
63- 'description ' => $ validated ['description ' ] ?? null ,
64- 'sale_price ' => $ validated ['selling_price ' ] ?? 0 ,
65- 'is_bundle ' => true ,
66- 'is_active ' => true ,
67- 'cost_price ' => 0 ,
50+ foreach ($ validated ['items ' ] as $ item ) {
51+ ProductBundleItem::create ([
52+ 'tenant_id ' => app ('tenant ' )->id ,
53+ 'bundle_product_id ' => $ bundle ->id ,
54+ 'component_product_id ' => $ item ['component_product_id ' ],
55+ 'quantity ' => $ item ['quantity ' ],
6856 ]);
57+ }
6958
70- foreach ($ validated ['items ' ] as $ item ) {
71- ProductBundleItem::create ([
72- 'bundle_product_id ' => $ bundle ->id ,
73- 'component_product_id ' => $ item ['component_product_id ' ],
74- 'quantity ' => $ item ['quantity ' ],
75- ]);
76- }
77-
78- $ bundleId = $ bundle ->id ;
79- });
80-
81- return redirect ()->route ('inventory.product-bundles.show ' , $ bundleId )
82- ->with ('success ' , 'Bundle created successfully. ' );
59+ return redirect ()->route ('inventory.product-bundles.show ' , $ bundle )
60+ ->with ('success ' , 'Bundle created. ' );
8361 }
8462
8563 public function show (Product $ productBundle ): Response
8664 {
8765 $ this ->authorize ('view ' , $ productBundle );
88-
8966 $ productBundle ->load ('bundleItems.componentProduct ' );
90-
91- $ products = Product::where ('is_bundle ' , false )
92- ->orderBy ('name ' )
93- ->get (['id ' , 'name ' , 'sku ' ]);
94-
95- return Inertia::render ('Inventory/ProductBundles/Show ' , [
96- 'bundle ' => $ productBundle ,
97- 'products ' => $ products ,
98- ]);
99- }
100-
101- public function destroy (Product $ productBundle ): RedirectResponse
102- {
103- $ this ->authorize ('delete ' , $ productBundle );
104-
105- $ productBundle ->delete ();
106-
107- return redirect ()->route ('inventory.product-bundles.index ' )
108- ->with ('success ' , 'Bundle deleted. ' );
67+ return Inertia::render ('Inventory/ProductBundles/Show ' , ['bundle ' => $ productBundle ]);
10968 }
11069
11170 public function addItem (Request $ request , Product $ productBundle ): RedirectResponse
11271 {
113- $ this ->authorize ('create ' , Product::class);
114-
72+ $ this ->authorize ('update ' , $ productBundle );
11573 $ validated = $ request ->validate ([
11674 'component_product_id ' => [
11775 'required ' ,
11876 Rule::exists ('products ' , 'id ' ),
11977 Rule::unique ('product_bundle_items ' )
12078 ->where (fn ($ q ) => $ q ->where ('bundle_product_id ' , $ productBundle ->id )),
12179 ],
122- 'quantity ' => [ 'required ' , ' numeric ' , ' min:0.0001 '] ,
80+ 'quantity ' => 'required| numeric| min:0.0001 ' ,
12381 ]);
12482
12583 ProductBundleItem::create ([
84+ 'tenant_id ' => app ('tenant ' )->id ,
12685 'bundle_product_id ' => $ productBundle ->id ,
12786 'component_product_id ' => $ validated ['component_product_id ' ],
12887 'quantity ' => $ validated ['quantity ' ],
@@ -135,10 +94,16 @@ public function addItem(Request $request, Product $productBundle): RedirectRespo
13594 public function removeItem (Product $ productBundle , ProductBundleItem $ item ): RedirectResponse
13695 {
13796 $ this ->authorize ('delete ' , $ productBundle );
138-
13997 $ item ->delete ();
140-
14198 return redirect ()->route ('inventory.product-bundles.show ' , $ productBundle )
14299 ->with ('success ' , 'Component removed. ' );
143100 }
101+
102+ public function destroy (Product $ productBundle ): RedirectResponse
103+ {
104+ $ this ->authorize ('delete ' , $ productBundle );
105+ $ productBundle ->delete ();
106+ return redirect ()->route ('inventory.product-bundles.index ' )
107+ ->with ('success ' , 'Bundle deleted. ' );
108+ }
144109}
0 commit comments