orderBy('created_at', 'desc') ->paginate(15); // Get mechanics using role_id 3 (mechanic) with dealer relationship $mechanics = User::with('dealer') ->where('role_id', 3) ->orderBy('name', 'asc') ->limit(50) ->get(); // If no mechanics found, get all users as fallback if ($mechanics->isEmpty()) { $mechanics = User::with('dealer') ->orderBy('name', 'asc') ->limit(50) ->get(); } return view('kpi.targets.index', compact('targets', 'mechanics')); } /** * Show the form for creating a new KPI target */ public function create() { // Get mechanics using role_id 3 (mechanic) with dealer relationship $mechanics = User::with('dealer') ->where('role_id', 3) ->orderBy('name', 'asc') ->limit(50) ->get(); // Debug: Log the mechanics found Log::info('Mechanics found for KPI target creation:', [ 'count' => $mechanics->count(), 'mechanics' => $mechanics->pluck('name', 'id')->toArray() ]); // If no mechanics found, get all users as fallback if ($mechanics->isEmpty()) { $mechanics = User::with('dealer') ->orderBy('name', 'asc') ->limit(50) ->get(); Log::warning('No mechanics found, using all users as fallback', [ 'count' => $mechanics->count() ]); } return view('kpi.targets.create', compact('mechanics')); } /** * Store a newly created KPI target */ public function store(StoreKpiTargetRequest $request) { try { // Log the validated data Log::info('Creating KPI target with data:', $request->validated()); // Check if user already has an active target and deactivate it $existingTarget = KpiTarget::where('user_id', $request->user_id) ->where('is_active', true) ->first(); if ($existingTarget) { Log::info('Deactivating existing active KPI target', [ 'user_id' => $request->user_id, 'existing_target_id' => $existingTarget->id ]); // Deactivate the existing target $existingTarget->update(['is_active' => false]); } $target = KpiTarget::create($request->validated()); Log::info('KPI target created successfully', [ 'target_id' => $target->id, 'user_id' => $target->user_id ]); return redirect()->route('kpi.targets.index') ->with('success', 'Target KPI berhasil ditambahkan'); } catch (\Exception $e) { Log::error('Failed to create KPI target', [ 'error' => $e->getMessage(), 'data' => $request->validated() ]); return redirect()->back() ->withInput() ->with('error', 'Gagal menambahkan target KPI: ' . $e->getMessage()); } } /** * Display the specified KPI target */ public function show(KpiTarget $target) { $target->load(['user.dealer', 'achievements']); return view('kpi.targets.show', compact('target')); } /** * Show the form for editing the specified KPI target */ public function edit(KpiTarget $target) { // Debug: Check if target is loaded correctly if (!$target) { abort(404, 'Target KPI tidak ditemukan'); } // Load target with user relationship $target->load('user'); // Get mechanics using role_id 3 (mechanic) with dealer relationship $mechanics = User::with('dealer') ->where('role_id', 3) ->orderBy('name', 'asc') ->limit(50) ->get(); // If no mechanics found, get all users as fallback if ($mechanics->isEmpty()) { $mechanics = User::with('dealer') ->orderBy('name', 'asc') ->limit(50) ->get(); } // Ensure data types are correct for comparison $target->user_id = (int)$target->user_id; return view('kpi.targets.edit', compact('target', 'mechanics')); } /** * Update the specified KPI target */ public function update(UpdateKpiTargetRequest $request, KpiTarget $target) { try { $target->update($request->validated()); return redirect()->route('kpi.targets.index') ->with('success', 'Target KPI berhasil diperbarui'); } catch (\Exception $e) { return redirect()->back() ->withInput() ->with('error', 'Gagal memperbarui target KPI: ' . $e->getMessage()); } } /** * Remove the specified KPI target */ public function destroy(KpiTarget $target) { try { $target->delete(); return redirect()->route('kpi.targets.index') ->with('success', 'Target KPI berhasil dihapus'); } catch (\Exception $e) { return redirect()->back() ->with('error', 'Gagal menghapus target KPI: ' . $e->getMessage()); } } /** * Toggle active status of KPI target */ public function toggleStatus(KpiTarget $target) { try { $target->update(['is_active' => !$target->is_active]); $status = $target->is_active ? 'diaktifkan' : 'dinonaktifkan'; return response()->json([ 'success' => true, 'message' => "Target KPI berhasil {$status}", 'is_active' => $target->is_active ]); } catch (\Exception $e) { return response()->json([ 'success' => false, 'message' => 'Gagal mengubah status target KPI' ], 500); } } /** * Get KPI targets for specific user */ public function getUserTargets(User $user) { $targets = $user->kpiTargets() ->with('achievements') ->orderBy('year', 'desc') ->orderBy('month', 'desc') ->get(); return response()->json([ 'success' => true, 'data' => $targets ]); } }