8889841cid; $tasks = Tasks::where('user_id', $userId)->get(); if ($tasks->isEmpty()) { return response()->json(['error' => 'No tasks found for this user'], 404); } foreach ($tasks as $task) { if ($task->post_approval === 'yes') { continue; // Move to the next task } $dueDate = Carbon::parse($task->end_date); $completionDate = now(); // Determine KPI points if ($completionDate->lt($dueDate)) { $kpiPoints = 1; // Completed before due date } elseif ($completionDate->eq($dueDate)) { $kpiPoints = 0; // Completed on due date } else { $kpiPoints = -1; // Completed after due date } // Update or create KPI record KPIPerfomance::updateOrCreate( ['users_id' => $userId, 'tasks_id' => $task->id], ['kpi_points' => $kpiPoints, 'updated_at' => now()] ); } return response()->json([ 'message' => 'KPI updated successfully for all eligible tasks', ]); } }