|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SapB1\Toolkit\Filament\Resources\InvoiceResource\Widgets; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use Filament\Widgets\StatsOverviewWidget; |
| 9 | +use Filament\Widgets\StatsOverviewWidget\Stat; |
| 10 | +use Illuminate\Support\Facades\Cache; |
| 11 | +use SapB1\Toolkit\Models\Sales\Invoice; |
| 12 | + |
| 13 | +class InvoiceStatsWidget extends StatsOverviewWidget |
| 14 | +{ |
| 15 | + protected ?string $pollingInterval = null; |
| 16 | + |
| 17 | + protected int|string|array $columnSpan = 'full'; |
| 18 | + |
| 19 | + protected function getStats(): array |
| 20 | + { |
| 21 | + $stats = Cache::remember('sapb1_invoice_stats', 300, function (): array { |
| 22 | + try { |
| 23 | + $firstOfMonth = now()->startOfMonth()->format('Y-m-d'); |
| 24 | + $today = now()->format('Y-m-d'); |
| 25 | + |
| 26 | + $totalThisMonth = Invoice::query() |
| 27 | + ->where('DocDate', '>=', $firstOfMonth) |
| 28 | + ->count(); |
| 29 | + |
| 30 | + $invoicesThisMonth = Invoice::query() |
| 31 | + ->where('DocDate', '>=', $firstOfMonth) |
| 32 | + ->select(['DocTotal']) |
| 33 | + ->get(); |
| 34 | + |
| 35 | + $totalRevenue = (float) $invoicesThisMonth->sum('DocTotal'); |
| 36 | + |
| 37 | + $openInvoices = Invoice::query() |
| 38 | + ->where('DocumentStatus', 'bost_Open') |
| 39 | + ->select(['DocTotal', 'PaidToDate']) |
| 40 | + ->get(); |
| 41 | + |
| 42 | + $outstanding = 0.0; |
| 43 | + |
| 44 | + foreach ($openInvoices as $inv) { |
| 45 | + $outstanding += (float) ($inv->DocTotal ?? 0) - (float) ($inv->PaidToDate ?? 0); |
| 46 | + } |
| 47 | + |
| 48 | + $overdueCount = Invoice::query() |
| 49 | + ->where('DocDueDate', '<', $today) |
| 50 | + ->where('DocumentStatus', 'bost_Open') |
| 51 | + ->count(); |
| 52 | + |
| 53 | + return [ |
| 54 | + 'total' => $totalThisMonth, |
| 55 | + 'revenue' => $totalRevenue, |
| 56 | + 'outstanding' => $outstanding, |
| 57 | + 'overdue' => $overdueCount, |
| 58 | + ]; |
| 59 | + } catch (Exception) { |
| 60 | + return ['total' => 0, 'revenue' => 0, 'outstanding' => 0, 'overdue' => 0]; |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + return [ |
| 65 | + Stat::make( |
| 66 | + __('sapb1-filament::resources.invoice.widgets.total_this_month'), |
| 67 | + (string) $stats['total'], |
| 68 | + )->icon('heroicon-o-document-text'), |
| 69 | + |
| 70 | + Stat::make( |
| 71 | + __('sapb1-filament::resources.invoice.widgets.total_revenue'), |
| 72 | + number_format((float) $stats['revenue'], 2).' TRY', |
| 73 | + )->icon('heroicon-o-banknotes'), |
| 74 | + |
| 75 | + Stat::make( |
| 76 | + __('sapb1-filament::resources.invoice.widgets.outstanding'), |
| 77 | + number_format((float) $stats['outstanding'], 2).' TRY', |
| 78 | + ) |
| 79 | + ->icon('heroicon-o-exclamation-triangle') |
| 80 | + ->color($stats['outstanding'] > 0 ? 'danger' : 'success'), |
| 81 | + |
| 82 | + Stat::make( |
| 83 | + __('sapb1-filament::resources.invoice.widgets.overdue'), |
| 84 | + (string) $stats['overdue'], |
| 85 | + ) |
| 86 | + ->icon('heroicon-o-clock') |
| 87 | + ->color($stats['overdue'] > 0 ? 'danger' : 'success'), |
| 88 | + ]; |
| 89 | + } |
| 90 | +} |
0 commit comments