fix orderable datatable on mutations and products index
This commit is contained in:
@@ -12,6 +12,7 @@ use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
@@ -26,45 +27,89 @@ class ProductsController extends Controller
|
||||
{
|
||||
$menu = Menu::where('link','products.index')->first();
|
||||
if($request->ajax()){
|
||||
$data = Product::with(['category'])
|
||||
->select('products.*')
|
||||
->leftJoin('product_categories', 'products.product_category_id', '=', 'product_categories.id');
|
||||
Log::info('Products DataTables request received');
|
||||
Log::info('Request parameters:', $request->all());
|
||||
|
||||
return DataTables::of($data)
|
||||
->addIndexColumn()
|
||||
->addColumn('category_name', function ($row) {
|
||||
return $row->category ? $row->category->name : '-';
|
||||
})
|
||||
->addColumn('total_stock', function ($row){
|
||||
return number_format($row->current_total_stock, 2);
|
||||
})
|
||||
->addColumn('action', function ($row) use ($menu) {
|
||||
$btn = '<div class="d-flex">';
|
||||
try {
|
||||
// Check if products exist
|
||||
$productCount = Product::count();
|
||||
Log::info('Total products in database: ' . $productCount);
|
||||
|
||||
if (Gate::allows('update', $menu)) {
|
||||
$btn .= '<a href="' . route('products.edit', $row->id) . '" class="btn btn-warning btn-sm" style="margin-right: 8px;">Edit</a>';
|
||||
$btn .= '<button class="btn btn-sm btn-toggle-active '
|
||||
. ($row->active ? 'btn-danger' : 'btn-success') . '"
|
||||
data-url="' . route('products.toggleActive', $row->id) . '" data-active="'.$row->active.'" style="margin-right: 8px;">'
|
||||
. ($row->active ? 'Nonaktifkan' : 'Aktifkan') . '</button>';
|
||||
}
|
||||
$btn .= '<button class="btn btn-sm btn-secondary btn-product-stock-dealers"
|
||||
data-id="'.$row->id.'"
|
||||
data-url="'.route('products.dealers_stock').'"
|
||||
data-name="'.$row->name.'">Dealer</button>';
|
||||
$data = Product::with(['category', 'stocks'])
|
||||
->select(['id', 'code', 'name', 'product_category_id', 'unit', 'active']);
|
||||
|
||||
Log::info('Query built, executing DataTables...');
|
||||
|
||||
return DataTables::of($data)
|
||||
->addIndexColumn()
|
||||
->addColumn('code', function ($row) {
|
||||
return $row->code;
|
||||
})
|
||||
->addColumn('name', function ($row) {
|
||||
return $row->name;
|
||||
})
|
||||
->addColumn('category_name', function ($row) {
|
||||
return $row->category ? $row->category->name : '-';
|
||||
})
|
||||
->addColumn('unit', function ($row) {
|
||||
return $row->unit ?? '-';
|
||||
})
|
||||
->addColumn('total_stock', function ($row){
|
||||
try {
|
||||
$totalStock = $row->stocks()->sum('quantity');
|
||||
return number_format($totalStock, 2);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Error calculating total stock for product ' . $row->id . ': ' . $e->getMessage());
|
||||
return '0.00';
|
||||
}
|
||||
})
|
||||
->addColumn('action', function ($row) use ($menu) {
|
||||
$btn = '<div class="d-flex">';
|
||||
|
||||
if (Gate::allows('update', $menu)) {
|
||||
$btn .= '<a href="' . route('products.edit', $row->id) . '" class="btn btn-warning btn-sm" style="margin-right: 8px;">Edit</a>';
|
||||
$btn .= '<button class="btn btn-sm btn-toggle-active '
|
||||
. ($row->active ? 'btn-danger' : 'btn-success') . '"
|
||||
data-url="' . route('products.toggleActive', $row->id) . '" data-active="'.$row->active.'" style="margin-right: 8px;">'
|
||||
. ($row->active ? 'Nonaktifkan' : 'Aktifkan') . '</button>';
|
||||
}
|
||||
$btn .= '<button class="btn btn-sm btn-secondary btn-product-stock-dealers"
|
||||
data-id="'.$row->id.'"
|
||||
data-url="'.route('products.dealers_stock').'"
|
||||
data-name="'.$row->name.'">Dealer</button>';
|
||||
|
||||
$btn .= '</div>';
|
||||
|
||||
return $btn;
|
||||
})
|
||||
->filterColumn('category_name', function($query, $keyword) {
|
||||
$query->where('product_categories.name', 'like', "%{$keyword}%");
|
||||
})
|
||||
->orderColumn('category_name', function ($query, $order) {
|
||||
$query->orderBy('product_categories.name', $order);
|
||||
})
|
||||
->rawColumns(['action'])
|
||||
->make(true);
|
||||
$btn .= '</div>';
|
||||
|
||||
return $btn;
|
||||
})
|
||||
->filterColumn('category_name', function($query, $keyword) {
|
||||
$query->whereHas('category', function($q) use ($keyword) {
|
||||
$q->where('name', 'like', "%{$keyword}%");
|
||||
});
|
||||
})
|
||||
->orderColumn('code', function ($query, $order) {
|
||||
$query->orderBy('products.code', $order);
|
||||
})
|
||||
->orderColumn('name', function ($query, $order) {
|
||||
$query->orderBy('products.name', $order);
|
||||
})
|
||||
->orderColumn('category_name', function ($query, $order) {
|
||||
$query->orderBy(
|
||||
DB::raw('(SELECT name FROM product_categories WHERE product_categories.id = products.product_category_id)'),
|
||||
$order
|
||||
);
|
||||
})
|
||||
->orderColumn('unit', function ($query, $order) {
|
||||
$query->orderBy('products.unit', $order);
|
||||
})
|
||||
->rawColumns(['action'])
|
||||
->make(true);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Products DataTables error: ' . $e->getMessage());
|
||||
Log::error('Stack trace: ' . $e->getTraceAsString());
|
||||
return response()->json(['error' => 'Failed to load data'], 500);
|
||||
}
|
||||
}
|
||||
return view('warehouse_management.products.index');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user