fix structure product categories table and crud product
This commit is contained in:
@@ -24,11 +24,14 @@ class ProductCategoriesController extends Controller
|
||||
$data = ProductCategory::query();
|
||||
return DataTables::of($data)
|
||||
->addIndexColumn()
|
||||
->addColumn('parent', function ($row) {
|
||||
return $row->parent ? $row->parent->name : '-';
|
||||
})
|
||||
->addColumn('action', function ($row) use ($menu) {
|
||||
$btn = '';
|
||||
|
||||
if (Auth::user()->can('delete', $menu)) {
|
||||
$btn .= '<button class="btn btn-danger btn-sm btn-destroy-product-category" data-action="' . route('product_categories.destroy', $row->id) . '" data-id="' . $row->id . '">Hapus</button>';
|
||||
$btn .= '<button style="margin-right: 8px;" class="btn btn-danger btn-sm btn-destroy-product-category" data-action="' . route('product_categories.destroy', $row->id) . '" data-id="' . $row->id . '">Hapus</button>';
|
||||
}
|
||||
|
||||
if (Auth::user()->can('update', $menu)) {
|
||||
@@ -63,6 +66,7 @@ class ProductCategoriesController extends Controller
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'parent_id' => 'nullable|exists:product_categories,id',
|
||||
]);
|
||||
ProductCategory::create($validated);
|
||||
return response()->json(['success' => true, 'message' => 'Kategori berhasil ditambahkan.']);
|
||||
@@ -121,4 +125,10 @@ class ProductCategoriesController extends Controller
|
||||
ProductCategory::findOrFail($id)->delete();
|
||||
return response()->json(['success' => true, 'message' => 'Kategorii berhasil dihapus.']);
|
||||
}
|
||||
|
||||
public function getParents(Request $request)
|
||||
{
|
||||
$parents = ProductCategory::whereNull('parent_id')->get(['id', 'name']);
|
||||
return response()->json($parents);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\WarehouseManagement;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Dealer;
|
||||
use App\Models\Menu;
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductCategory;
|
||||
@@ -34,16 +35,18 @@ class ProductsController extends Controller
|
||||
});
|
||||
})
|
||||
->addColumn('action', function ($row) use ($menu) {
|
||||
$btn = '';
|
||||
$btn = '<div class="d-flex">';
|
||||
|
||||
if (Auth::user()->can('delete', $menu)) {
|
||||
$btn .= '<button class="btn btn-danger btn-sm btn-destroy-product" data-action="' . route('products.destroy', $row->id) . '" data-id="' . $row->id . '">Hapus</button>';
|
||||
$btn .= '<button style="margin-right: 8px;" class="btn btn-danger btn-sm btn-destroy-product" data-action="' . route('products.destroy', $row->id) . '" data-id="' . $row->id . '">Hapus</button>';
|
||||
}
|
||||
|
||||
|
||||
if (Auth::user()->can('update', $menu)) {
|
||||
$btn .= '<button class="btn btn-warning btn-sm btn-edit-product" data-url="' . route('products.edit', $row->id) . '" data-action="' . route('products.update', $row->id) . '" data-id="' . $row->id . '">Edit</button>';
|
||||
$btn .= '<a href="' . route('products.edit', $row->id) . '" class="btn btn-warning btn-sm">Edit</a>';
|
||||
}
|
||||
|
||||
|
||||
$btn .= '</div>';
|
||||
|
||||
return $btn;
|
||||
})
|
||||
->rawColumns(['action'])
|
||||
@@ -59,8 +62,10 @@ class ProductsController extends Controller
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
$categories = ProductCategory::with('children')->whereNull('parent_id')->get();
|
||||
$dealers = Dealer::all();
|
||||
return view('warehouse_management.products.create', compact('categories', 'dealers'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
@@ -70,8 +75,39 @@ class ProductsController extends Controller
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
}
|
||||
$request->validate([
|
||||
'code' => 'required|string|unique:products,code',
|
||||
'name' => 'required|string',
|
||||
'description' => 'nullable|string',
|
||||
'product_category_id' => 'required|exists:product_categories,id',
|
||||
'dealer_stock' => 'nullable|array',
|
||||
'dealer_stock.*.dealer_id' => 'required|exists:dealers,id',
|
||||
'dealer_stock.*.quantity' => 'required|integer|min:0',
|
||||
]);
|
||||
|
||||
// Create product
|
||||
$product = Product::create([
|
||||
'code' => $request->code,
|
||||
'name' => $request->name,
|
||||
'description' => $request->description,
|
||||
'product_category_id' => $request->product_category_id,
|
||||
]);
|
||||
|
||||
// Prepare dealer stock for pivot
|
||||
$pivotData = [];
|
||||
if ($request->has('dealer_stock')) {
|
||||
foreach ($request->dealer_stock as $stockData) {
|
||||
if (empty($stockData['dealer_id']) || !isset($stockData['quantity'])) continue;
|
||||
|
||||
$pivotData[$stockData['dealer_id']] = ['quantity' => $stockData['quantity']];
|
||||
}
|
||||
|
||||
// Attach dealer stock using pivot table
|
||||
$product->dealers()->attach($pivotData);
|
||||
}
|
||||
|
||||
return redirect()->route('products.index')->with('success', 'Produk berhasil ditambahkan.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
@@ -92,7 +128,12 @@ class ProductsController extends Controller
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
//
|
||||
$product = Product::findOrFail($id);
|
||||
return view('warehouse_management.products.edit', [
|
||||
'product' => $product->load('dealers'),
|
||||
'dealers' => Dealer::all(),
|
||||
'categories' => ProductCategory::with('children')->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,10 +143,33 @@ class ProductsController extends Controller
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
public function update(Request $request, Product $product)
|
||||
{
|
||||
//
|
||||
}
|
||||
$request->validate([
|
||||
'code' => 'required|string|unique:products,code,' . $product->id,
|
||||
'name' => 'required|string',
|
||||
'description' => 'nullable|string',
|
||||
'product_category_id' => 'required|exists:product_categories,id',
|
||||
'dealer_stock' => 'nullable|array',
|
||||
'dealer_stock.*.dealer_id' => 'required|exists:dealers,id',
|
||||
'dealer_stock.*.quantity' => 'required|integer|min:0',
|
||||
]);
|
||||
|
||||
$product->update($request->only(['code', 'name', 'description', 'product_category_id']));
|
||||
|
||||
// Prepare pivot sync data
|
||||
$syncData = [];
|
||||
if ($request->has('dealer_stock')) {
|
||||
foreach ($request->dealer_stock as $item) {
|
||||
$syncData[$item['dealer_id']] = ['quantity' => $item['quantity']];
|
||||
}
|
||||
}
|
||||
|
||||
// Sync with pivot table
|
||||
$product->dealers()->sync($syncData);
|
||||
|
||||
return redirect()->route('products.index')->with('success', 'Produk berhasil diperbarui.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
@@ -113,8 +177,14 @@ class ProductsController extends Controller
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
public function destroy(Product $product)
|
||||
{
|
||||
//
|
||||
// Detach all dealer relationships (optional if using cascade on delete)
|
||||
$product->dealers()->detach();
|
||||
|
||||
// Delete the product
|
||||
$product->delete();
|
||||
|
||||
return response()->json(['success' => true, 'message' => 'Produk berhasil dihapus.']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user