Update: 2026-05-04 21:54:02
This commit is contained in:
@@ -13,16 +13,26 @@ $db = Database::getInstance();
|
||||
try {
|
||||
// 1. Super Admin sees ALL companies
|
||||
if ($decoded['role'] === 'super_admin') {
|
||||
$stmt = $db->prepare("SELECT c.*, t.name as tenant_name
|
||||
$stmt = $db->prepare("
|
||||
SELECT c.*, t.name as tenant_name,
|
||||
(SELECT COUNT(*) FROM invoices WHERE company_id = c.id AND deleted_at IS NULL) as invoices_count,
|
||||
(SELECT SUM(grand_total) FROM invoices WHERE company_id = c.id AND deleted_at IS NULL) as total_amount
|
||||
FROM companies c
|
||||
LEFT JOIN tenants t ON c.tenant_id = t.id
|
||||
WHERE c.deleted_at IS NULL ORDER BY c.created_at DESC");
|
||||
WHERE c.deleted_at IS NULL ORDER BY c.created_at DESC
|
||||
");
|
||||
$stmt->execute();
|
||||
$companies = $stmt->fetchAll();
|
||||
}
|
||||
// 2. Tenant Users (Admin, Accountant, Employee) see all companies in their tenant
|
||||
else {
|
||||
$stmt = $db->prepare("SELECT * FROM companies WHERE tenant_id = ? AND deleted_at IS NULL ORDER BY created_at DESC");
|
||||
$stmt = $db->prepare("
|
||||
SELECT *,
|
||||
(SELECT COUNT(*) FROM invoices WHERE company_id = companies.id AND deleted_at IS NULL) as invoices_count,
|
||||
(SELECT SUM(grand_total) FROM invoices WHERE company_id = companies.id AND deleted_at IS NULL) as total_amount
|
||||
FROM companies
|
||||
WHERE tenant_id = ? AND deleted_at IS NULL ORDER BY created_at DESC
|
||||
");
|
||||
$stmt->execute([$decoded['tenant_id']]);
|
||||
$companies = $stmt->fetchAll();
|
||||
}
|
||||
|
||||
@@ -15,7 +15,13 @@ if ($decoded['role'] !== 'super_admin') {
|
||||
$db = Database::getInstance();
|
||||
|
||||
try {
|
||||
$stmt = $db->query("SELECT id, name, email, phone, status, created_at FROM tenants ORDER BY created_at DESC");
|
||||
$stmt = $db->query("
|
||||
SELECT t.id, t.name, t.email, t.phone, t.status, t.created_at,
|
||||
(SELECT COUNT(*) FROM companies WHERE tenant_id = t.id) as companies_count,
|
||||
(SELECT COUNT(*) FROM invoices WHERE tenant_id = t.id) as invoices_count
|
||||
FROM tenants t
|
||||
ORDER BY t.created_at DESC
|
||||
");
|
||||
$tenants = $stmt->fetchAll();
|
||||
|
||||
foreach ($tenants as &$t) {
|
||||
|
||||
60
app/modules_app/tenants/stats.php
Normal file
60
app/modules_app/tenants/stats.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* Tenant Aggregated Stats Endpoint (Super Admin Only)
|
||||
*/
|
||||
|
||||
use App\Core\Database;
|
||||
use App\Middleware\AuthMiddleware;
|
||||
|
||||
$decoded = AuthMiddleware::check();
|
||||
|
||||
if ($decoded['role'] !== 'super_admin') {
|
||||
json_error('Unauthorized', 403);
|
||||
}
|
||||
|
||||
$tenantId = $_GET['tenant_id'] ?? null;
|
||||
if (!$tenantId) {
|
||||
json_error('Missing tenant_id', 400);
|
||||
}
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
try {
|
||||
// 1. Overall Stats
|
||||
$stmt = $db->prepare("
|
||||
SELECT
|
||||
COUNT(DISTINCT c.id) as total_companies,
|
||||
COUNT(i.id) as total_invoices,
|
||||
SUM(i.grand_total) as total_amount,
|
||||
SUM(i.tax_amount) as total_tax
|
||||
FROM companies c
|
||||
LEFT JOIN invoices i ON c.id = i.company_id AND i.deleted_at IS NULL
|
||||
WHERE c.tenant_id = ? AND c.deleted_at IS NULL
|
||||
");
|
||||
$stmt->execute([$tenantId]);
|
||||
$summary = $stmt->fetch();
|
||||
|
||||
// 2. Monthly breakdown
|
||||
$stmt = $db->prepare("
|
||||
SELECT
|
||||
DATE_FORMAT(i.invoice_date, '%Y-%m') as month,
|
||||
COUNT(*) as total_invoices,
|
||||
SUM(i.tax_amount) as total_tax,
|
||||
SUM(i.grand_total) as total_amount
|
||||
FROM invoices i
|
||||
WHERE i.tenant_id = ? AND i.deleted_at IS NULL
|
||||
GROUP BY month
|
||||
ORDER BY month DESC
|
||||
LIMIT 12
|
||||
");
|
||||
$stmt->execute([$tenantId]);
|
||||
$monthly = $stmt->fetchAll();
|
||||
|
||||
json_success([
|
||||
'summary' => $summary,
|
||||
'monthly' => $monthly
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
json_error('Stats Error: ' . $e->getMessage(), 500);
|
||||
}
|
||||
Reference in New Issue
Block a user