From 3189eb309996b95d0d5c4949bf2ff6d7a0d853de Mon Sep 17 00:00:00 2001 From: George Daneke Date: Wed, 19 Jun 2024 00:59:14 +0300 Subject: [PATCH] added better dashboard --- app/Filament/Resources/LocationResource.php | 4 +- app/Filament/Widgets/Locations.php | 41 +++++++++++ app/Filament/Widgets/RecentlyAdded.php | 43 +++++++++++ app/Filament/Widgets/StatsOverviewWidget.php | 76 ++++++++++++++++++++ app/Providers/Filament/AppPanelProvider.php | 2 +- 5 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 app/Filament/Widgets/Locations.php create mode 100644 app/Filament/Widgets/RecentlyAdded.php create mode 100644 app/Filament/Widgets/StatsOverviewWidget.php diff --git a/app/Filament/Resources/LocationResource.php b/app/Filament/Resources/LocationResource.php index e4add2e..a599e9c 100644 --- a/app/Filament/Resources/LocationResource.php +++ b/app/Filament/Resources/LocationResource.php @@ -120,8 +120,8 @@ public static function getPages(): array return [ 'index' => Pages\ListLocations::route('/'), // to make separate pages you can uncomment this: - /*'create' => Pages\CreateLocation::route('/create'), - 'edit' => Pages\EditLocation::route('/{record}/edit'),*/ + /*'create' => Pages\CreateLocation::route('/create'),*/ + 'edit' => Pages\EditLocation::route('/{record}/edit'), ]; } diff --git a/app/Filament/Widgets/Locations.php b/app/Filament/Widgets/Locations.php new file mode 100644 index 0000000..e46b126 --- /dev/null +++ b/app/Filament/Widgets/Locations.php @@ -0,0 +1,41 @@ +query(LocationResource::getEloquentQuery()->where('parent_id', null)) + ->defaultPaginationPageOption(15) + ->defaultSort('created_at', 'desc') + ->columns([ + Stack::make([ + Tables\Columns\TextColumn::make('name') + ->searchable()->sortable(), +]) + ]) + ->contentGrid([ + 'md' => 2, + 'xl' => 3, + ]) + ->actions([ + Tables\Actions\Action::make('open') + ->url(fn (Location $record): string => LocationResource::getUrl('edit', ['record' => $record])), + ]); + } +} diff --git a/app/Filament/Widgets/RecentlyAdded.php b/app/Filament/Widgets/RecentlyAdded.php new file mode 100644 index 0000000..953b954 --- /dev/null +++ b/app/Filament/Widgets/RecentlyAdded.php @@ -0,0 +1,43 @@ +query(ItemResource::getEloquentQuery()) + ->defaultPaginationPageOption(5) + ->defaultSort('created_at', 'desc') + ->columns([ + Tables\Columns\TextColumn::make('name') + ->searchable()->sortable(), + Tables\Columns\TextColumn::make('quantity') + ->searchable() + ->sortable(), + Tables\Columns\TextColumn::make('location.name') + ->searchable() + ->sortable(), + Tables\Columns\TextColumn::make('purchase_price') + ->searchable() + ->sortable(), + Tables\Columns\CheckboxColumn::make('insured'), + ]) + ->actions([ + Tables\Actions\Action::make('open') + ->url(fn (Item $record): string => ItemResource::getUrl('edit', ['record' => $record])), + ]); + } +} diff --git a/app/Filament/Widgets/StatsOverviewWidget.php b/app/Filament/Widgets/StatsOverviewWidget.php new file mode 100644 index 0000000..69e076c --- /dev/null +++ b/app/Filament/Widgets/StatsOverviewWidget.php @@ -0,0 +1,76 @@ +filters['startDate'] ?? null) ? + Carbon::parse($this->filters['startDate']) : + null; + + $endDate = ! is_null($this->filters['endDate'] ?? null) ? + Carbon::parse($this->filters['endDate']) : + now(); + + $isBusinessCustomersOnly = $this->filters['businessCustomersOnly'] ?? null; + $businessCustomerMultiplier = match (true) { + boolval($isBusinessCustomersOnly) => 2 / 3, + blank($isBusinessCustomersOnly) => 1, + default => 1 / 3, + }; + + $diffInDays = $startDate ? $startDate->diffInDays($endDate) : 0; + + $revenue = (int) (($startDate ? ($diffInDays * 137) : 192100) * $businessCustomerMultiplier); + $newCustomers = (int) (($startDate ? ($diffInDays * 7) : 1340) * $businessCustomerMultiplier); + $newOrders = (int) (($startDate ? ($diffInDays * 13) : 3543) * $businessCustomerMultiplier); + + $formatNumber = function (int $number): string { + if ($number < 1000) { + return (string) Number::format($number, 0); + } + + if ($number < 1000000) { + return Number::format($number / 1000, 2).'k'; + } + + return Number::format($number / 1000000, 2).'m'; + }; + + $totalValue = Item::sum('purchase_price'); + + return [ + Stat::make('Total Value', '$'.$formatNumber($totalValue)), + Stat::make('Total Items', Item::count()) + ->description('10% increase') + ->descriptionIcon('heroicon-m-arrow-trending-up') + ->chart([7, 2, 10, 3, 15, 4, 17]) + ->color('success'), + Stat::make('Total Locations', Location::count()) + ->description('3% decrease') + ->descriptionIcon('heroicon-m-arrow-trending-down') + ->chart([17, 16, 14, 15, 14, 13, 12]) + ->color('danger'), + Stat::make('Total Labels', 5) + ->description('7% increase') + ->descriptionIcon('heroicon-m-arrow-trending-up') + ->chart([15, 4, 10, 2, 12, 4, 12]) + ->color('success'), + ]; + } +} diff --git a/app/Providers/Filament/AppPanelProvider.php b/app/Providers/Filament/AppPanelProvider.php index 413287c..e1b48f6 100644 --- a/app/Providers/Filament/AppPanelProvider.php +++ b/app/Providers/Filament/AppPanelProvider.php @@ -62,7 +62,7 @@ public function panel(Panel $panel): Panel ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets') ->widgets([ Widgets\AccountWidget::class, - Widgets\FilamentInfoWidget::class, + // Widgets\FilamentInfoWidget::class, ]) ->userMenuItems([ MenuItem::make()