New Initial Commit
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Filament\Resources\OrderResource;
|
||||
use App\Models\Order;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Widgets\TableWidget as BaseWidget;
|
||||
|
||||
class LatestOrders extends BaseWidget
|
||||
{
|
||||
protected static ?int $sort = 2;
|
||||
|
||||
protected int | string | array $columnSpan = 'full';
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->query(OrderResource::getEloquentQuery()->latest()->limit(10))
|
||||
->defaultPaginationPageOption(5)
|
||||
->defaultSort('created_at', 'desc')
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('order_number')
|
||||
->label('Order #')
|
||||
->searchable()
|
||||
->sortable()
|
||||
->url(fn (Order $record): string => OrderResource::getUrl('view', ['record' => $record]))
|
||||
->color('primary'),
|
||||
Tables\Columns\TextColumn::make('customer_name')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('total')
|
||||
->money('ZAR')
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('status')
|
||||
->badge()
|
||||
->color(fn (string $state): string => match ($state) {
|
||||
'pending' => 'warning',
|
||||
'processing' => 'info',
|
||||
'completed' => 'success',
|
||||
'cancelled' => 'danger',
|
||||
}),
|
||||
Tables\Columns\TextColumn::make('payment_status')
|
||||
->badge()
|
||||
->color(fn (string $state): string => match ($state) {
|
||||
'pending' => 'warning',
|
||||
'paid' => 'success',
|
||||
'failed' => 'danger',
|
||||
}),
|
||||
Tables\Columns\TextColumn::make('created_at')
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->since(),
|
||||
]);
|
||||
}
|
||||
|
||||
protected function getTableHeading(): string
|
||||
{
|
||||
return 'Latest Orders';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\Order;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RevenueChart extends ChartWidget
|
||||
{
|
||||
protected ?string $heading = 'Revenue (Last 7 Days)';
|
||||
|
||||
protected static ?int $sort = 3;
|
||||
|
||||
protected function getData(): array
|
||||
{
|
||||
$data = Order::where('payment_status', 'paid')
|
||||
->where('created_at', '>=', now()->subDays(7))
|
||||
->select(
|
||||
DB::raw('DATE(created_at) as date'),
|
||||
DB::raw('SUM(total) as revenue')
|
||||
)
|
||||
->groupBy('date')
|
||||
->orderBy('date')
|
||||
->get();
|
||||
|
||||
$labels = [];
|
||||
$revenues = [];
|
||||
|
||||
// Fill in last 7 days
|
||||
for ($i = 6; $i >= 0; $i--) {
|
||||
$date = now()->subDays($i)->format('Y-m-d');
|
||||
$dayName = now()->subDays($i)->format('D');
|
||||
$labels[] = $dayName;
|
||||
|
||||
$dayData = $data->firstWhere('date', $date);
|
||||
$revenues[] = $dayData ? $dayData->revenue : 0;
|
||||
}
|
||||
|
||||
return [
|
||||
'datasets' => [
|
||||
[
|
||||
'label' => 'Revenue (R)',
|
||||
'data' => $revenues,
|
||||
'backgroundColor' => 'rgba(59, 130, 246, 0.1)',
|
||||
'borderColor' => 'rgb(59, 130, 246)',
|
||||
'fill' => true,
|
||||
],
|
||||
],
|
||||
'labels' => $labels,
|
||||
];
|
||||
}
|
||||
|
||||
protected function getType(): string
|
||||
{
|
||||
return 'line';
|
||||
}
|
||||
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'scales' => [
|
||||
'y' => [
|
||||
'beginAtZero' => true,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\Order;
|
||||
use App\Models\Product;
|
||||
use Filament\Widgets\StatsOverviewWidget;
|
||||
use Filament\Widgets\StatsOverviewWidget\Stat;
|
||||
|
||||
class StatsOverview extends StatsOverviewWidget
|
||||
{
|
||||
protected static ?int $sort = 1;
|
||||
|
||||
protected function getStats(): array
|
||||
{
|
||||
$totalOrders = Order::count();
|
||||
$pendingOrders = Order::where('status', 'pending')->count();
|
||||
$processingOrders = Order::where('status', 'processing')->count();
|
||||
$lowStockProducts = Product::where('stock', '<', 10)->count();
|
||||
|
||||
$totalRevenue = Order::where('payment_status', 'paid')->sum('total');
|
||||
$monthlyRevenue = Order::where('payment_status', 'paid')
|
||||
->whereMonth('created_at', now()->month)
|
||||
->whereYear('created_at', now()->year)
|
||||
->sum('total');
|
||||
|
||||
$todayOrders = Order::whereDate('created_at', today())->count();
|
||||
$todayRevenue = Order::where('payment_status', 'paid')
|
||||
->whereDate('created_at', today())
|
||||
->sum('total');
|
||||
|
||||
return [
|
||||
Stat::make('Pending Orders', $pendingOrders)
|
||||
->description('Awaiting processing')
|
||||
->descriptionIcon('heroicon-m-clock')
|
||||
->color('warning')
|
||||
->chart([7, 5, 10, 5, $pendingOrders]),
|
||||
|
||||
Stat::make('Processing Orders', $processingOrders)
|
||||
->description('Currently being processed')
|
||||
->descriptionIcon('heroicon-m-arrow-path')
|
||||
->color('info'),
|
||||
|
||||
Stat::make('Low Stock Alert', $lowStockProducts)
|
||||
->description('Products with less than 10 units')
|
||||
->descriptionIcon('heroicon-m-exclamation-triangle')
|
||||
->color($lowStockProducts > 0 ? 'danger' : 'success'),
|
||||
|
||||
Stat::make('Today\'s Orders', $todayOrders)
|
||||
->description('Orders placed today')
|
||||
->descriptionIcon('heroicon-m-shopping-bag')
|
||||
->color('success'),
|
||||
|
||||
Stat::make('Today\'s Revenue', 'R' . number_format($todayRevenue, 2))
|
||||
->description(now()->format('l, F j'))
|
||||
->descriptionIcon('heroicon-m-banknotes')
|
||||
->color('success'),
|
||||
|
||||
Stat::make('Monthly Revenue', 'R' . number_format($monthlyRevenue, 2))
|
||||
->description(now()->format('F Y'))
|
||||
->descriptionIcon('heroicon-m-chart-bar')
|
||||
->color('info'),
|
||||
|
||||
Stat::make('Total Revenue', 'R' . number_format($totalRevenue, 2))
|
||||
->description('All time revenue')
|
||||
->descriptionIcon('heroicon-m-currency-dollar')
|
||||
->color('success'),
|
||||
|
||||
Stat::make('Total Orders', $totalOrders)
|
||||
->description('All time orders')
|
||||
->descriptionIcon('heroicon-m-shopping-cart')
|
||||
->color('primary'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user