Commit 50db0586 authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix session completion, trainer workload, and wallet balance reports

1. Session completion: column is 'session_date' not 'date'
2. Trainer workload: TrainingGroup has no assignments() — use TrainingSession.trainer_id via employee→user link, count groups via head_trainer_id
3. Wallet balances: Wallet uses polymorphic owner() not participant() — use owner with owner_type filter
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 00e571f7
...@@ -454,24 +454,34 @@ public function retentionReport(string $from, string $to, ?int $branchId = null) ...@@ -454,24 +454,34 @@ public function retentionReport(string $from, string $to, ?int $branchId = null)
public function trainerWorkload(string $from, string $to, ?int $branchId = null): Collection public function trainerWorkload(string $from, string $to, ?int $branchId = null): Collection
{ {
return Trainer::with(['person']) return Trainer::with(['person', 'employee'])
->where('status', 'active') ->where('status', 'active')
->get() ->get()
->map(function ($trainer) use ($from, $to, $branchId) { ->map(function ($trainer) use ($from, $to, $branchId) {
$sessions = TrainingSession::whereHas('group.assignments', fn ($q) => $q->where('trainer_id', $trainer->id)) $userId = $trainer->employee?->user_id;
->whereBetween('date', [$from, $to]) if (!$userId) {
return null;
}
$sessions = TrainingSession::where('trainer_id', $userId)
->whereBetween('session_date', [$from, $to])
->when($branchId, fn ($q) => $q->whereHas('group', fn ($g) => $g->where('branch_id', $branchId))); ->when($branchId, fn ($q) => $q->whereHas('group', fn ($g) => $g->where('branch_id', $branchId)));
$groupsCount = TrainingGroup::where('head_trainer_id', $userId)
->where('status', 'active')
->when($branchId, fn ($q) => $q->where('branch_id', $branchId))
->count();
return [ return [
'name' => $trainer->person?->name_ar ?? '', 'name' => $trainer->person?->name_ar ?? '',
'phone' => $trainer->person?->phone ?? '', 'phone' => $trainer->person?->phone ?? '',
'total_sessions' => (clone $sessions)->count(), 'total_sessions' => (clone $sessions)->count(),
'completed' => (clone $sessions)->where('status', 'completed')->count(), 'completed' => (clone $sessions)->where('status', 'completed')->count(),
'cancelled' => (clone $sessions)->where('status', 'cancelled')->count(), 'cancelled' => (clone $sessions)->where('status', 'cancelled')->count(),
'groups_count' => $trainer->assignments()->where('status', 'active')->distinct('training_group_id')->count('training_group_id'), 'groups_count' => $groupsCount,
]; ];
}) })
->filter(fn ($t) => $t['total_sessions'] > 0) ->filter(fn ($t) => $t && $t['total_sessions'] > 0)
->sortByDesc('total_sessions') ->sortByDesc('total_sessions')
->values(); ->values();
} }
...@@ -583,8 +593,7 @@ public function posDailySummary(string $from, string $to, ?int $branchId = null) ...@@ -583,8 +593,7 @@ public function posDailySummary(string $from, string $to, ?int $branchId = null)
public function sessionCompletionReport(string $from, string $to, ?int $branchId = null): Collection public function sessionCompletionReport(string $from, string $to, ?int $branchId = null): Collection
{ {
return TrainingSession::with(['group.program', 'group.branch']) return TrainingSession::whereBetween('session_date', [$from, $to])
->whereBetween('date', [$from, $to])
->when($branchId, fn ($q) => $q->whereHas('group', fn ($g) => $g->where('branch_id', $branchId))) ->when($branchId, fn ($q) => $q->whereHas('group', fn ($g) => $g->where('branch_id', $branchId)))
->select('status', DB::raw('COUNT(*) as count')) ->select('status', DB::raw('COUNT(*) as count'))
->groupBy('status') ->groupBy('status')
...@@ -626,15 +635,16 @@ public function branchComparison(string $from, string $to): Collection ...@@ -626,15 +635,16 @@ public function branchComparison(string $from, string $to): Collection
public function walletBalances(?int $branchId = null): Collection public function walletBalances(?int $branchId = null): Collection
{ {
return Wallet::with(['participant.person']) return Wallet::with(['owner.person'])
->where('owner_type', 'App\\Domain\\Participant\\Models\\Participant')
->where('status', 'active') ->where('status', 'active')
->where('balance', '>', 0) ->where('balance', '>', 0)
->when($branchId, fn ($q) => $q->whereHas('participant', fn ($p) => $p->where('branch_id', $branchId))) ->when($branchId, fn ($q) => $q->whereHasMorph('owner', [Participant::class], fn ($p) => $p->where('branch_id', $branchId)))
->orderByDesc('balance') ->orderByDesc('balance')
->get() ->get()
->map(fn ($w) => [ ->map(fn ($w) => [
'participant' => $w->participant?->person?->name_ar ?? '', 'participant' => $w->owner?->person?->name_ar ?? '',
'phone' => $w->participant?->person?->phone ?? '', 'phone' => $w->owner?->person?->phone ?? '',
'balance' => $w->balance, 'balance' => $w->balance,
'frozen' => $w->frozen_amount ?? 0, 'frozen' => $w->frozen_amount ?? 0,
'available' => $w->balance - ($w->frozen_amount ?? 0), 'available' => $w->balance - ($w->frozen_amount ?? 0),
......
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