-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathSalesStats.php
More file actions
65 lines (50 loc) · 1.97 KB
/
SalesStats.php
File metadata and controls
65 lines (50 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace App\Filament\Resources\SalesResource\Widgets;
use App\Filament\Resources\SalesResource\Pages\ListSales;
use App\Models\Sale;
use Filament\Widgets\Concerns\InteractsWithPageTable;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
class SalesStats extends BaseWidget
{
use InteractsWithPageTable;
protected static ?string $pollingInterval = null;
protected function getTablePage(): string
{
return ListSales::class;
}
protected function getStats(): array
{
$filteredQuery = $this->getPageTableQuery();
$grandTotalRevenue = Sale::sum('price_paid');
$grandTotalSales = Sale::count();
$filteredRevenue = (clone $filteredQuery)->sum('price_paid');
$filteredSales = (clone $filteredQuery)->count();
$isFiltered = $filteredSales !== $grandTotalSales;
$stats = [
Stat::make('Total Revenue', $this->formatCurrency($grandTotalRevenue))
->description('All time')
->descriptionIcon('heroicon-m-banknotes')
->color('success'),
Stat::make('Total Sales', number_format($grandTotalSales))
->description('All time')
->descriptionIcon('heroicon-m-shopping-cart')
->color('primary'),
];
if ($isFiltered) {
$stats[] = Stat::make('Filtered Revenue', $this->formatCurrency($filteredRevenue))
->description('Current filter')
->descriptionIcon('heroicon-m-funnel')
->color('warning');
$stats[] = Stat::make('Filtered Sales', number_format($filteredSales))
->description('Current filter')
->descriptionIcon('heroicon-m-funnel')
->color('warning');
}
return $stats;
}
protected function formatCurrency(int $cents): string
{
return '$'.number_format($cents / 100, 2);
}
}