Commit 60ff1e86 authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix dashboard crash: trainer_compensations table name + graceful fallback

- Add explicit $table = 'trainer_compensations' to TrainerCompensation
  model (Laravel resolved singular 'trainer_compensation' by default)
- Wrap TrainerDuesWidget query in try/catch so dashboard loads even if
  the compensation migration hasn't been run yet
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 19e94971
...@@ -15,6 +15,8 @@ class TrainerCompensation extends Model ...@@ -15,6 +15,8 @@ class TrainerCompensation extends Model
{ {
use BelongsToAcademy, HasUuid, SoftDeletes; use BelongsToAcademy, HasUuid, SoftDeletes;
protected $table = 'trainer_compensations';
protected $fillable = [ protected $fillable = [
'academy_id', 'academy_id',
'trainer_id', 'trainer_id',
......
...@@ -12,44 +12,52 @@ class TrainerDuesWidget extends Component ...@@ -12,44 +12,52 @@ class TrainerDuesWidget extends Component
{ {
public function render() public function render()
{ {
$startOfMonth = now()->startOfMonth()->toDateString(); $totalDues = 0;
$endOfMonth = now()->endOfMonth()->toDateString(); $trainerTotals = [];
$topTrainers = collect();
$compensations = TrainerCompensation::whereIn('status', ['pending', 'approved']) try {
->forPeriod($startOfMonth, $endOfMonth) $startOfMonth = now()->startOfMonth()->toDateString();
->select('trainer_id', DB::raw('SUM(amount) as total_amount'), DB::raw('COUNT(*) as records_count')) $endOfMonth = now()->endOfMonth()->toDateString();
->groupBy('trainer_id')
->get()
->keyBy('trainer_id');
$salaryTrainers = Trainer::whereIn('compensation_model', ['salary', 'hybrid']) $compensations = TrainerCompensation::whereIn('status', ['pending', 'approved'])
->where('status', 'active') ->forPeriod($startOfMonth, $endOfMonth)
->with('employee:id,salary_amount') ->select('trainer_id', DB::raw('SUM(amount) as total_amount'), DB::raw('COUNT(*) as records_count'))
->get(); ->groupBy('trainer_id')
->get()
->keyBy('trainer_id');
$trainerTotals = []; $salaryTrainers = Trainer::whereIn('compensation_model', ['salary', 'hybrid'])
->where('status', 'active')
->with('employee:id,salary_amount')
->get();
foreach ($compensations as $trainerId => $comp) { foreach ($compensations as $trainerId => $comp) {
$trainerTotals[$trainerId] = ($trainerTotals[$trainerId] ?? 0) + $comp->total_amount; $trainerTotals[$trainerId] = ($trainerTotals[$trainerId] ?? 0) + $comp->total_amount;
} }
foreach ($salaryTrainers as $trainer) { foreach ($salaryTrainers as $trainer) {
$salary = $trainer->employee?->salary_amount ?? 0; $salary = $trainer->employee?->salary_amount ?? 0;
$trainerTotals[$trainer->id] = ($trainerTotals[$trainer->id] ?? 0) + $salary; $trainerTotals[$trainer->id] = ($trainerTotals[$trainer->id] ?? 0) + $salary;
} }
$totalDues = array_sum($trainerTotals);
$totalDues = array_sum($trainerTotals); if ($trainerTotals) {
$topTrainers = Trainer::whereIn('id', array_keys($trainerTotals))
$topTrainers = Trainer::whereIn('id', array_keys($trainerTotals)) ->with(['employee.person:id,name_ar,name', 'person:id,name_ar,name'])
->with('employee.person:id,name_ar,name') ->get()
->get() ->map(fn ($t) => [
->map(fn ($t) => [ 'name' => $t->employee?->person?->name_ar ?? $t->person?->name_ar ?? $t->trainer_number,
'name' => $t->employee?->person?->name_ar ?? $t->trainer_number, 'amount' => $trainerTotals[$t->id],
'amount' => $trainerTotals[$t->id], ])
]) ->sortByDesc('amount')
->sortByDesc('amount') ->take(5)
->take(5) ->values();
->values(); }
} catch (\Illuminate\Database\QueryException $e) {
// Table may not exist yet if migrations haven't run
}
return view('livewire.dashboard.trainer-dues-widget', [ return view('livewire.dashboard.trainer-dues-widget', [
'totalDues' => $totalDues, 'totalDues' => $totalDues,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment