create crud product categories and partial update crud products with dealers stock

This commit is contained in:
2025-05-28 18:24:44 +07:00
parent 80375d8af3
commit 59e23ae535
28 changed files with 1336 additions and 28 deletions

View File

@@ -0,0 +1,124 @@
<?php
namespace App\Http\Controllers\WarehouseManagement;
use App\Http\Controllers\Controller;
use App\Models\Menu;
use App\Models\ProductCategory;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Yajra\DataTables\Facades\DataTables;
class ProductCategoriesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$menu = Menu::where('link','product_categories.index')->first();
if($request->ajax()){
$data = ProductCategory::query();
return DataTables::of($data)
->addIndexColumn()
->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>';
}
if (Auth::user()->can('update', $menu)) {
$btn .= '<button class="btn btn-warning btn-sm btn-edit-product-category" data-url="' . route('product_categories.edit', $row->id) . '" data-action="' . route('product_categories.update', $row->id) . '" data-id="' . $row->id . '">Edit</button>';
}
return $btn;
})
->rawColumns(['action'])
->make(true);
}
return view('warehouse_management.product_categories.index');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
]);
ProductCategory::create($validated);
return response()->json(['success' => true, 'message' => 'Kategori berhasil ditambahkan.']);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$category = ProductCategory::findOrFail($id);
return response()->json($category);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
]);
$category = ProductCategory::findOrFail($id);
$category->update($validated);
return response()->json(['success' => true, 'message' => 'Kategori berhasil diperbarui.']);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
ProductCategory::findOrFail($id)->delete();
return response()->json(['success' => true, 'message' => 'Kategorii berhasil dihapus.']);
}
}

View File

@@ -0,0 +1,120 @@
<?php
namespace App\Http\Controllers\WarehouseManagement;
use App\Http\Controllers\Controller;
use App\Models\Menu;
use App\Models\Product;
use App\Models\ProductCategory;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Yajra\DataTables\Facades\DataTables;
class ProductsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$menu = Menu::where('link','products.index')->first();
if($request->ajax()){
$data = Product::with(['category','dealers']);
return DataTables::of($data)
->addIndexColumn()
->addColumn('category_name', function ($row) {
return $row->category ? $row->category->name : '-';
})
->addColumn('total_stock', function ($row){
return $row->dealers->sum(function($dealer){
return $dealer->pivot->quantity ?? 0;
});
})
->addColumn('action', function ($row) use ($menu) {
$btn = '';
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>';
}
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>';
}
return $btn;
})
->rawColumns(['action'])
->make(true);
}
return view('warehouse_management.products.index');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}