Skip to content

Commit

Permalink
added better dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Flatroy committed Jun 18, 2024
1 parent 8fc853b commit 3189eb3
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/Filament/Resources/LocationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
];
}

Expand Down
41 changes: 41 additions & 0 deletions app/Filament/Widgets/Locations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Filament\Widgets;

use App\Filament\Resources\ItemResource;
use App\Filament\Resources\LocationResource;
use App\Models\Item;
use App\Models\Location;
use Filament\Tables;
use Filament\Tables\Columns\Layout\Stack;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;

class Locations extends BaseWidget
{
protected int|string|array $columnSpan = 'full';

protected static ?int $sort = 3;

public function table(Table $table): Table
{
return $table
->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])),
]);
}
}
43 changes: 43 additions & 0 deletions app/Filament/Widgets/RecentlyAdded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Filament\Widgets;

use App\Filament\Resources\ItemResource;
use App\Models\Item;
use Filament\Tables;
use Filament\Tables\Columns\CheckboxColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;

class RecentlyAdded extends BaseWidget
{
protected int|string|array $columnSpan = 'full';

protected static ?int $sort = 2;

public function table(Table $table): Table
{
return $table
->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])),
]);
}
}
76 changes: 76 additions & 0 deletions app/Filament/Widgets/StatsOverviewWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Filament\Widgets;

use App\Models\Item;
use App\Models\Location;
use Carbon\Carbon;
use Filament\Widgets\Concerns\InteractsWithPageFilters;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Support\Number;

class StatsOverviewWidget extends BaseWidget
{
use InteractsWithPageFilters;

protected static ?int $sort = 0;

protected function getStats(): array
{

$startDate = ! is_null($this->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'),
];
}
}
2 changes: 1 addition & 1 deletion app/Providers/Filament/AppPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3189eb3

Please sign in to comment.