Files
fotospiel-app/app/Filament/Widgets/RevenueTrendWidget.php
Codex Agent fa33e7cbcf Fix Event & EventType resource issues and apply formatting
- Fix EventType deletion error handling (constraint violations)
- Fix Event update error (package_id column missing)
- Fix Event Type dropdown options (JSON display issue)
- Fix EventPackagesRelationManager query error
- Add missing translations for deletion errors
- Apply Pint formatting
2026-01-21 10:34:06 +01:00

55 lines
1.6 KiB
PHP

<?php
namespace App\Filament\Widgets;
use App\Models\PurchaseHistory;
use Filament\Widgets\LineChartWidget;
class RevenueTrendWidget extends LineChartWidget
{
protected static ?int $sort = 1;
protected int|string|array $columnSpan = 'full';
public function getHeading(): ?string
{
return __('admin.widgets.revenue_trend.heading');
}
protected function getData(): array
{
$start = now()->startOfMonth()->subMonths(11);
$months = collect(range(0, 11))->map(fn (int $offset) => $start->copy()->addMonths($offset));
$records = PurchaseHistory::query()
->where('purchased_at', '>=', $start)
->get(['purchased_at', 'price']);
$grouped = $records->groupBy(fn (PurchaseHistory $history) => $history->purchased_at?->format('Y-m'));
$labels = [];
$values = [];
foreach ($months as $month) {
$key = $month->format('Y-m');
$labels[] = $month->translatedFormat('M Y');
$total = $grouped->get($key, collect())->sum(fn (PurchaseHistory $history) => (float) $history->price);
$values[] = round($total, 2);
}
return [
'datasets' => [
[
'label' => __('admin.widgets.revenue_trend.series'),
'data' => $values,
'borderColor' => '#ec4899',
'backgroundColor' => 'rgba(236, 72, 153, 0.2)',
'tension' => 0.4,
'fill' => 'origin',
],
],
'labels' => $labels,
];
}
}