52 lines
1.7 KiB
PHP
52 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Dashboard;
|
|
|
|
use App\Core\{Request, Response, Database};
|
|
|
|
final class DashboardController
|
|
{
|
|
public function getStats(Request $request): void
|
|
{
|
|
$tenantId = $request->tenantId;
|
|
$role = $request->user->role ?? 'viewer';
|
|
$assignedCompanyId = $request->user->assigned_company_id ?? null;
|
|
$db = Database::getInstance();
|
|
|
|
$where = "WHERE tenant_id = ?";
|
|
$params = [$tenantId];
|
|
|
|
if ($role !== 'super_admin') {
|
|
$where .= " AND company_id = ?";
|
|
$params[] = $assignedCompanyId;
|
|
}
|
|
|
|
// 1. Total Invoices this month
|
|
$stmt = $db->prepare("SELECT COUNT(*) as count FROM invoices {$where} AND MONTH(created_at) = MONTH(CURRENT_DATE)");
|
|
$stmt->execute($params);
|
|
$thisMonth = $stmt->fetch()['count'];
|
|
|
|
// 2. Approved vs Rejected
|
|
$stmt = $db->prepare("SELECT status, COUNT(*) as count FROM invoices {$where} GROUP BY status");
|
|
$stmt->execute($params);
|
|
$statusCounts = $stmt->fetchAll();
|
|
|
|
// 3. Recent Activity - Fixed ambiguity
|
|
$stmt = $db->prepare("SELECT i.*, c.name as company_name FROM invoices i JOIN companies c ON i.company_id = c.id WHERE i.tenant_id = ? " . ($role !== 'super_admin' ? " AND i.company_id = ?" : "") . " ORDER BY i.created_at DESC LIMIT 5");
|
|
$stmt->execute($params);
|
|
$recent = $stmt->fetchAll();
|
|
|
|
Response::json([
|
|
'success' => true,
|
|
'data' => [
|
|
'total_this_month' => $thisMonth,
|
|
'status_distribution' => $statusCounts,
|
|
'recent_invoices' => $recent,
|
|
'subscription_usage' => 45 // Placeholder
|
|
]
|
|
]);
|
|
}
|
|
}
|