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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user