Skip to content

Commit

Permalink
Feature - Dashboard refactor (#1444)
Browse files Browse the repository at this point in the history
This PR adds more stats to the dashboard to make it more complete for
the MVP.

---------

Co-authored-by: Glenn Jacobs <[email protected]>
  • Loading branch information
alecritson and glennjacobs authored Jan 4, 2024
1 parent 3d7faf2 commit 1af1064
Show file tree
Hide file tree
Showing 12 changed files with 882 additions and 288 deletions.
95 changes: 76 additions & 19 deletions packages/admin/resources/lang/en/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,47 @@

return [
'dashboard' => [
'stats_overview' => [
'stat_one' => [
'label' => 'New Products',
],
'stat_two' => [
'label' => 'Returning Customers',
],
'stat_three' => [
'label' => 'Turnover',
],
'stat_four' => [
'label' => 'No. Orders',
'orders' => [
'order_stats_overview' => [
'stat_one' => [
'label' => 'Orders today',
'increase' => ':percentage% increase from :count yesterday',
'decrease' => ':percentage% decrease from :count yesterday',
'neutral' => 'No change compared to yesterday',
],
'stat_two' => [
'label' => 'Orders this week',
'increase' => ':percentage% increase from :count last week',
'decrease' => ':percentage% decrease from :count last week',
'neutral' => 'No change compared to last week',
],
'stat_three' => [
'label' => 'Orders this month',
'increase' => ':percentage% increase from :count last month',
'decrease' => ':percentage% decrease from :count last month',
'neutral' => 'No change compared to last month',
],
'stat_four' => [
'label' => 'Sales today',
'increase' => ':percentage% increase from :total yesterday',
'decrease' => ':percentage% decrease from :total yesterday',
'neutral' => 'No change compared to yesterday',
],
'stat_five' => [
'label' => 'Sales this week',
'increase' => ':percentage% increase from :total last week',
'decrease' => ':percentage% decrease from :total last week',
'neutral' => 'No change compared to last week',
],
'stat_six' => [
'label' => 'Sales this month',
'increase' => ':percentage% increase from :total last month',
'decrease' => ':percentage% decrease from :total last month',
'neutral' => 'No change compared to last month',
],
],
],
'sales_performance' => [
'heading' => 'Sales Performance',
'chart' => [
'order_totals_chart' => [
'heading' => 'Order totals for the past year',
'series_one' => [
'label' => 'This Period',
],
Expand All @@ -29,9 +53,42 @@
'label' => 'Turnover :currency',
],
],
],
'latest_orders' => [
'heading' => 'Latest Orders',
'order_sales_chart' => [
'heading' => 'Orders / Sales Report',
'series_one' => [
'label' => 'Orders',
],
'series_two' => [
'label' => 'Revenue',
],
'yaxis' => [
'series_one' => [
'label' => '# Orders',
],
'series_two' => [
'label' => 'Total Value',
],
],
],
'average_order_value' => [
'heading' => 'Average Order Value',
],
'new_returning_customers' => [
'heading' => 'New vs Returning Customers',
'series_one' => [
'label' => 'New Customers',
],
'series_two' => [
'label' => 'Returning Customers',
],
],
'popular_products' => [
'heading' => 'This months best sellers',
'description' => 'These figures are based on the number of times a product appears on an order, not the quantity ordered.',
],
'latest_orders' => [
'heading' => 'Latest orders',
],
],
],
];
40 changes: 14 additions & 26 deletions packages/admin/src/Filament/Pages/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,30 @@

namespace Lunar\Admin\Filament\Pages;

use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Section;
use Filament\Forms\Form;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
use Filament\Support\Facades\FilamentIcon;
use Lunar\Admin\Filament\Widgets\Dashboard\LatestOrders;
use Lunar\Admin\Filament\Widgets\Dashboard\SalesPerformance;
use Lunar\Admin\Filament\Widgets\Dashboard\StatsOverview;
use Lunar\Admin\Filament\Widgets\Dashboard\Orders\AverageOrderValueChart;
use Lunar\Admin\Filament\Widgets\Dashboard\Orders\LatestOrdersTable;
use Lunar\Admin\Filament\Widgets\Dashboard\Orders\NewVsReturningCustomersChart;
use Lunar\Admin\Filament\Widgets\Dashboard\Orders\OrdersSalesChart;
use Lunar\Admin\Filament\Widgets\Dashboard\Orders\OrderStatsOverview;
use Lunar\Admin\Filament\Widgets\Dashboard\Orders\OrderTotalsChart;
use Lunar\Admin\Filament\Widgets\Dashboard\Orders\PopularProductsTable;
use Lunar\Admin\Support\Pages\BaseDashboard;

class Dashboard extends BaseDashboard
{
use HasFiltersForm;

protected static ?int $navigationSort = 1;

public function filtersForm(Form $form): Form
{
return $form
->schema([
Section::make()
->schema([
DatePicker::make('startDate'),
DatePicker::make('endDate'),
// ...
])
->columns(2),
]);
}

public function getWidgets(): array
{
return [
StatsOverview::class,
SalesPerformance::class,
LatestOrders::class,
OrderStatsOverview::class,
OrderTotalsChart::class,
OrdersSalesChart::class,
AverageOrderValueChart::class,
NewVsReturningCustomersChart::class,
PopularProductsTable::class,
LatestOrdersTable::class,
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php

namespace Lunar\Admin\Filament\Widgets\Dashboard\Orders;

use Carbon\CarbonPeriod;
use Leandrocfe\FilamentApexCharts\Widgets\ApexChartWidget;
use Lunar\Facades\DB;
use Lunar\Models\Currency;
use Lunar\Models\CustomerGroup;
use Lunar\Models\Order;

class AverageOrderValueChart extends ApexChartWidget
{
/**
* Chart Id
*/
protected static string $chartId = 'averageOrderValue';

protected function getHeading(): ?string
{
return __('lunarpanel::widgets.dashboard.orders.average_order_value.heading');
}

protected function getOrderQuery(\DateTime $from = null, \DateTime $to = null)
{
return Order::whereNotNull('placed_at')
->whereBetween('placed_at', [
$from,
$to,
]);
}

protected function getOptions(): array
{

$customerGroups = CustomerGroup::get();

$date = now()->settings([
'monthOverflow' => false,
]);

$from = $date->clone()->subYear();

$period = CarbonPeriod::create($from, '1 month', $date);

$series = $customerGroups->mapWithKeys(function ($group) use ($date, $from, $period) {
$query = $this->getOrderQuery($from, $date);

/**
* $format = '%Y-%m';
* $displayFormat = '%M %Y';
*/
$guestOrders = collect();

if ($group->default) {
$guestOrders = $query->clone()->whereNull('user_id')->whereNull('customer_id')
->select(
DB::RAW('ROUND(AVG(total), 0) as total'),
DB::RAW('ROUND(AVG(shipping_total), 0) as shipping_total'),
DB::RAW('ROUND(AVG(discount_total), 0) as discount_total'),
DB::RAW('ROUND(AVG(sub_total), 0) as sub_total'),
DB::RAW('ROUND(AVG(tax_total), 0) as tax_total'),
DB::RAW(db_date('placed_at', '%Y-%m', 'date'))
)->groupBy(
DB::RAW('date')
)->orderBy(DB::RAW('date'), 'desc')->get();
}

$result = $query->whereHas(
'customer',
fn ($relation) => $relation->whereHas(
'customerGroups',
fn ($subRelation) => $subRelation->where("{$group->getTable()}.id", '=', $group->id)
)
)->select(
DB::RAW('ROUND(AVG(total), 0) as total'),
DB::RAW('ROUND(AVG(shipping_total), 0) as shipping_total'),
DB::RAW('ROUND(AVG(discount_total), 0) as discount_total'),
DB::RAW('ROUND(AVG(sub_total), 0) as sub_total'),
DB::RAW('ROUND(AVG(tax_total), 0) as tax_total'),
DB::RAW(db_date('placed_at', '%Y-%m', 'date'))
)->groupBy(
DB::RAW('date')
)->orderBy(DB::RAW('date'), 'desc')->get();

$merged = collect([
...$result,
...$guestOrders,
]);

$data = collect();

foreach ($period as $date) {
$result = $merged->first(function ($month) use ($date) {
return $month->date == $date->format('Y-m');
});
$data->push($result?->sub_total->decimal ?: 0);
}

return [
$group->handle => [
'name' => $group->name,
'data' => $data,
],
];
});

$labels = [];

foreach ($period as $date) {
$labels[] = $date->format('F Y');
}

$currency = Currency::getDefault();

return [
'chart' => [
'type' => 'bar',
'toolbar' => [
'show' => false,
],
],
'dataLabels' => [
'enabled' => false,
],
'series' => $series->values()->toArray(),
'xaxis' => [
'categories' => $labels,
],
'yaxis' => [
'title' => [
'text' => __('lunarpanel::widgets.dashboard.orders.order_totals_chart.yaxis.label', [
'currency' => $currency->code,
]),
],
],
'tooltip' => [
'x' => [
'format' => 'dd MMM yyyy',
],
],
];
}

protected function getTotalsForPeriod($from, $to)
{
$currentPeriod = collect();
$period = CarbonPeriod::create($from, '1 month', $to);

$results = $this->getOrderQuery($from, $to)
->select(
DB::RAW('SUM(total) as total'),
DB::RAW('SUM(shipping_total) as shipping_total'),
DB::RAW('SUM(discount_total) as discount_total'),
DB::RAW('SUM(sub_total) as sub_total'),
DB::RAW('SUM(tax_total) as tax_total'),
DB::RAW(db_date('placed_at', '%M', 'month')),
DB::RAW(db_date('placed_at', '%Y', 'year')),
DB::RAW(db_date('placed_at', '%Y%m', 'monthstamp'))
)->groupBy(
DB::RAW('month'),
DB::RAW('year'),
DB::RAW('monthstamp'),
DB::RAW(db_date('placed_at', '%Y-%m')),
)->orderBy(DB::RAW("DATE_FORMAT(placed_at, '%Y-%m')"), 'desc')->get();

foreach ($period as $date) {
// Find our records for this period.
$report = $results->first(function ($month) use ($date) {
return $month->monthstamp == $date->format('Ym');
});
$currentPeriod->push((object) [
'order_total' => $report?->total->decimal ?: 0,
'shipping_total' => $report?->shipping_total->decimal ?: 0,
'discount_total' => $report?->discount_total->decimal ?: 0,
'sub_total' => $report?->sub_total->decimal ?: 0,
'month' => $date->format('F'),
'year' => $date->format('Y'),
'tax_total' => $report?->tax_total->decimal ?: 0,
]);
}

return $currentPeriod;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php

namespace Lunar\Admin\Filament\Widgets\Dashboard;
namespace Lunar\Admin\Filament\Widgets\Dashboard\Orders;

use Filament\Tables\Table;
use Filament\Widgets\TableWidget;
use Lunar\Admin\Filament\Resources\OrderResource;
use Lunar\Models\Order;

class LatestOrders extends TableWidget
class LatestOrdersTable extends TableWidget
{
protected int|string|array $columnSpan = 'full';

public static function getHeading(): ?string
{
return __('lunarpanel::widgets.dashboard.latest_orders.heading');
return __('lunarpanel::widgets.dashboard.orders.latest_orders.heading');
}

public function table(Table $table): Table
Expand Down
Loading

0 comments on commit 1af1064

Please sign in to comment.