@php $peakMonth = $monthly->sortByDesc('total_amount')->first(); $maxAmt = $monthly->max('total_amount'); // Build pivot matrix for category breakdown $months = $monthlyByItem->pluck('month')->unique()->sort()->values(); $itemNames = $monthlyByItem->pluck('expense_item_name')->unique()->values(); $matrix = []; foreach ($monthlyByItem as $row) { $matrix[$row->month][$row->expense_item_name] = $row->total_amount; } @endphp {{-- Stat cards --}}
Grand Total
{{ number_format($totals['grand_total'], 2) }}
{{ $totals['total_months'] }} months
Avg Monthly
{{ number_format($totals['avg_monthly'], 2) }}
per month
Peak Month
{{ $peakMonth?->month ?? '—' }}
{{ number_format($peakMonth?->total_amount ?? 0, 2) }}
Months Recorded
{{ $totals['total_months'] }}
in range
{{-- Monthly totals --}}
Monthly Totals {{ $monthly->count() }} months
@if($monthly->isEmpty())
No monthly data found.
@else @foreach($monthly as $row) @php $pct = $maxAmt > 0 ? round($row->total_amount / $maxAmt * 100) : 0; $diff = $row->total_amount - $totals['avg_monthly']; @endphp @endforeach
Month Entries Branches Total Amount vs Avg Volume
{{ $row->month }} {{ number_format($row->entry_count) }} {{ number_format($row->branch_count) }} {{ number_format($row->total_amount, 2) }} {{ $diff > 0 ? '+' : '' }}{{ number_format($diff, 2) }}
{{ $pct }}%
Total {{ number_format($totals['grand_total'], 2) }}
@endif
{{-- Category pivot --}} @if($monthlyByItem->isNotEmpty())
Category Breakdown by Month {{ $itemNames->count() }} categories
@foreach($itemNames as $name) @endforeach @foreach($months as $month) @php $rowTotal = array_sum($matrix[$month] ?? []); @endphp @foreach($itemNames as $name) @endforeach @endforeach @foreach($itemNames as $name) @endforeach
Month{{ $name }}Row Total
{{ $month }} @if(!empty($matrix[$month][$name])) {{ number_format($matrix[$month][$name], 2) }} @else @endif {{ number_format($rowTotal, 2) }}
Column Total {{ number_format($monthlyByItem->where('expense_item_name', $name)->sum('total_amount'), 2) }} {{ number_format($totals['grand_total'], 2) }}
@endif