fix: make dashboard sales-by-hour query cross-database compatible and add dashboard feature tests
This commit is contained in:
parent
00586e168f
commit
b62dbf32d9
@ -36,23 +36,26 @@ public function __invoke()
|
||||
$stats['net_profit'] = $this->calculateDiff($stats['gross_profit'], $stats['total_expenses']);
|
||||
$stats['profit_margin'] = $this->calculateMargin($stats['net_profit'], $stats['total_revenue']);
|
||||
|
||||
$isSqlite = DB::getDriverName() === 'sqlite';
|
||||
$hourSelect = $isSqlite ? "CAST(strftime('%H', created_at) AS INTEGER)" : 'HOUR(created_at)';
|
||||
|
||||
// Get sales by hour
|
||||
$todayOrders = DB::table('orders')
|
||||
->whereDate('created_at', $today)
|
||||
->select(
|
||||
DB::raw('HOUR(created_at) as hour'),
|
||||
DB::raw("$hourSelect as hour"),
|
||||
DB::raw('COUNT(*) as total')
|
||||
)
|
||||
->groupBy(DB::raw('HOUR(created_at)'))
|
||||
->groupBy(DB::raw($hourSelect))
|
||||
->get();
|
||||
|
||||
$yesterdayOrders = DB::table('orders')
|
||||
->whereDate('created_at', $yesterday)
|
||||
->select(
|
||||
DB::raw('HOUR(created_at) as hour'),
|
||||
DB::raw("$hourSelect as hour"),
|
||||
DB::raw('COUNT(*) as total')
|
||||
)
|
||||
->groupBy(DB::raw('HOUR(created_at)'))
|
||||
->groupBy(DB::raw($hourSelect))
|
||||
->get();
|
||||
|
||||
$salesByHour = collect(range(0, 23))->map(function ($hour) use ($todayOrders, $yesterdayOrders) {
|
||||
|
||||
73
tests/Feature/Admin/DashboardTest.php
Normal file
73
tests/Feature/Admin/DashboardTest.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Expense;
|
||||
use App\Models\Order;
|
||||
use App\Models\Purchase;
|
||||
|
||||
use function Pest\Laravel\actingAs;
|
||||
use function Pest\Laravel\get;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Dashboard Module Tests
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
describe('Dashboard Module - Authorization', function () {
|
||||
it('redirects to login when accessing dashboard unauthenticated', function () {
|
||||
get(route('dashboard'))
|
||||
->assertRedirect(route('login'));
|
||||
});
|
||||
|
||||
it('returns 403 when user has no permission to view dashboard', function () {
|
||||
actingAs(createUnauthorizedUser())
|
||||
->get(route('dashboard'))
|
||||
->assertStatus(403);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Dashboard Module - Authorized Actions', function () {
|
||||
beforeEach(function () {
|
||||
$user = createAuthorizedUser([
|
||||
'View:Dashboard',
|
||||
]);
|
||||
actingAs($user);
|
||||
});
|
||||
|
||||
it('can access dashboard page and view statistics', function () {
|
||||
// Create some dummy data to ensure queries don't fail and aggregations work
|
||||
Order::factory()->count(3)->create([
|
||||
'created_at' => now(),
|
||||
'total' => 100000,
|
||||
'cogs' => 50000,
|
||||
]);
|
||||
|
||||
Expense::factory()->count(2)->create([
|
||||
'created_at' => now(),
|
||||
'amount' => 20000,
|
||||
]);
|
||||
|
||||
Purchase::factory()->create([
|
||||
'created_at' => now(),
|
||||
'total' => 150000,
|
||||
]);
|
||||
|
||||
get(route('dashboard'))
|
||||
->assertOk()
|
||||
->assertInertia(fn ($page) => $page
|
||||
->component('dashboard')
|
||||
->has('stats')
|
||||
->has('stats.total_sales')
|
||||
->has('stats.total_revenue')
|
||||
->has('stats.total_expenses')
|
||||
->has('stats.gross_profit')
|
||||
->has('stats.net_profit')
|
||||
->has('salesByHour')
|
||||
->has('paymentMethods')
|
||||
->has('orderStatuses')
|
||||
->has('orderChannels')
|
||||
->has('topProducts')
|
||||
->has('topCustomers')
|
||||
);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user