fix orderable datatable on mutations and products index
This commit is contained in:
@@ -10,6 +10,7 @@ use App\Enums\MutationStatus;
|
||||
use App\Models\Menu;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Yajra\DataTables\DataTables;
|
||||
|
||||
class MutationsController extends Controller
|
||||
@@ -19,13 +20,18 @@ class MutationsController extends Controller
|
||||
$menu = Menu::where('link','mutations.index')->first();
|
||||
|
||||
if ($request->ajax()) {
|
||||
Log::info('Mutations DataTables request', [
|
||||
'order' => $request->get('order'),
|
||||
'columns' => $request->get('columns'),
|
||||
'user_id' => auth()->id()
|
||||
]);
|
||||
|
||||
// Use a more specific query to avoid join conflicts
|
||||
$data = Mutation::query()
|
||||
->with(['fromDealer', 'toDealer', 'requestedBy.role', 'approvedBy.role', 'receivedBy.role'])
|
||||
->select([
|
||||
'mutations.*'
|
||||
])
|
||||
->orderBy('mutations.id', 'desc'); // Default order by ID desc
|
||||
]); // Remove default ordering to let DataTables handle it
|
||||
|
||||
// Filter berdasarkan dealer jika user bukan admin
|
||||
if (auth()->user()->dealer_id) {
|
||||
@@ -60,7 +66,7 @@ class MutationsController extends Controller
|
||||
return number_format($row->total_items, 0);
|
||||
})
|
||||
->addColumn('created_at', function($row) {
|
||||
return $row->created_at->format('d/m/Y H:i');
|
||||
return $row->created_at->format('d M Y, H:i');
|
||||
})
|
||||
->addColumn('action', function($row) {
|
||||
return view('warehouse_management.mutations._action', compact('row'))->render();
|
||||
@@ -426,7 +432,7 @@ class MutationsController extends Controller
|
||||
return number_format($row->total_items, 0);
|
||||
})
|
||||
->addColumn('created_at', function($row) {
|
||||
return $row->created_at->format('d/m/Y H:i');
|
||||
return $row->created_at->format('d M Y, H:i');
|
||||
})
|
||||
->addColumn('action', function($row) use ($dealerId) {
|
||||
$buttons = '';
|
||||
@@ -478,7 +484,7 @@ class MutationsController extends Controller
|
||||
]);
|
||||
|
||||
// Format created_at
|
||||
$mutation->created_at_formatted = $mutation->created_at->format('d/m/Y H:i');
|
||||
$mutation->created_at_formatted = $mutation->created_at->format('d M Y, H:i');
|
||||
|
||||
// Add status color and label
|
||||
$mutation->status_color = $mutation->status_color;
|
||||
|
||||
@@ -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