Update: 2026-05-04 00:37:13
This commit is contained in:
@@ -18,25 +18,31 @@ try {
|
|||||||
LEFT JOIN tenants t ON c.tenant_id = t.id
|
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();
|
$stmt->execute();
|
||||||
|
$companies = $stmt->fetchAll();
|
||||||
}
|
}
|
||||||
// 2. Admin sees all companies in their tenant
|
// 2. Admin sees all companies in their tenant
|
||||||
else if ($decoded['role'] === 'admin') {
|
else if ($decoded['role'] === 'admin') {
|
||||||
$stmt = $db->prepare("SELECT * FROM companies WHERE tenant_id = ? AND deleted_at IS NULL");
|
$stmt = $db->prepare("SELECT * FROM companies WHERE tenant_id = ? AND deleted_at IS NULL");
|
||||||
$stmt->execute([$decoded['tenant_id']]);
|
$stmt->execute([$decoded['tenant_id']]);
|
||||||
|
$companies = $stmt->fetchAll();
|
||||||
}
|
}
|
||||||
// 3. Others (accountant, etc) see only their assigned company
|
// 3. Others (accountant, etc) see only their assigned companies
|
||||||
else {
|
else {
|
||||||
// Need to get their assigned company_id from users table first
|
// Get assigned company IDs from the pivot table
|
||||||
$stmtUser = $db->prepare("SELECT company_id FROM users WHERE id = ?");
|
$stmtUser = $db->prepare("SELECT company_id FROM user_company_assignments WHERE user_id = ? AND is_active = 1");
|
||||||
$stmtUser->execute([$decoded['user_id']]);
|
$stmtUser->execute([$decoded['user_id']]);
|
||||||
$assignedCompanyId = $stmtUser->fetchColumn();
|
$assignedCompanyIds = $stmtUser->fetchAll(PDO::FETCH_COLUMN);
|
||||||
|
|
||||||
$stmt = $db->prepare("SELECT * FROM companies WHERE id = ? AND deleted_at IS NULL");
|
if (empty($assignedCompanyIds)) {
|
||||||
$stmt->execute([$assignedCompanyId]);
|
$companies = [];
|
||||||
|
} else {
|
||||||
|
$placeholders = implode(',', array_fill(0, count($assignedCompanyIds), '?'));
|
||||||
|
$stmt = $db->prepare("SELECT * FROM companies WHERE id IN ($placeholders) AND deleted_at IS NULL");
|
||||||
|
$stmt->execute($assignedCompanyIds);
|
||||||
|
$companies = $stmt->fetchAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$companies = $stmt->fetchAll();
|
|
||||||
|
|
||||||
// 3. Decrypt fields
|
// 3. Decrypt fields
|
||||||
foreach ($companies as &$company) {
|
foreach ($companies as &$company) {
|
||||||
// Decrypt Name
|
// Decrypt Name
|
||||||
|
|||||||
@@ -26,27 +26,39 @@ try {
|
|||||||
$where .= " AND tenant_id = :tenant_id";
|
$where .= " AND tenant_id = :tenant_id";
|
||||||
$params[':tenant_id'] = $tenantId;
|
$params[':tenant_id'] = $tenantId;
|
||||||
} else {
|
} else {
|
||||||
// Accountant/Viewer: Filter by specific company
|
// Accountant/Viewer: Filter by assigned companies
|
||||||
$where .= " AND tenant_id = :tenant_id";
|
$where .= " AND tenant_id = :tenant_id";
|
||||||
$params[':tenant_id'] = $tenantId;
|
$params[':tenant_id'] = $tenantId;
|
||||||
|
|
||||||
if ($companyId) {
|
// Get assigned company IDs
|
||||||
$where .= " AND company_id = :company_id";
|
$stmtUser = $db->prepare("SELECT company_id FROM user_company_assignments WHERE user_id = ? AND is_active = 1");
|
||||||
$params[':company_id'] = $companyId;
|
$stmtUser->execute([$decoded['user_id']]);
|
||||||
|
$assignedCompanyIds = $stmtUser->fetchAll(PDO::FETCH_COLUMN);
|
||||||
|
|
||||||
|
if (empty($assignedCompanyIds)) {
|
||||||
|
// No companies assigned, see nothing
|
||||||
|
$where .= " AND 1=0";
|
||||||
|
} else {
|
||||||
|
$placeholders = implode(',', array_fill(0, count($assignedCompanyIds), '?'));
|
||||||
|
$where .= " AND company_id IN ($placeholders)";
|
||||||
|
// We need to merge params carefully since we are using both named and positional
|
||||||
|
// Actually, let's switch to pure positional for simplicity here
|
||||||
|
$where = str_replace(':tenant_id', '?', $where);
|
||||||
|
$params = array_merge([$tenantId], $assignedCompanyIds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Fetch Stats
|
// 3. Fetch Stats
|
||||||
$stmt = $db->prepare("SELECT COUNT(*) FROM invoices $where");
|
$stmt = $db->prepare("SELECT COUNT(*) FROM invoices $where");
|
||||||
$stmt->execute($params);
|
$stmt->execute(array_values($params));
|
||||||
$total = $stmt->fetchColumn();
|
$total = $stmt->fetchColumn();
|
||||||
|
|
||||||
$stmt = $db->prepare("SELECT COUNT(*) FROM invoices $where AND status = 'pending'");
|
$stmt = $db->prepare("SELECT COUNT(*) FROM invoices $where AND status = 'pending'");
|
||||||
$stmt->execute($params);
|
$stmt->execute(array_values($params));
|
||||||
$pending = $stmt->fetchColumn();
|
$pending = $stmt->fetchColumn();
|
||||||
|
|
||||||
$stmt = $db->prepare("SELECT COUNT(*) FROM invoices $where AND status = 'approved'");
|
$stmt = $db->prepare("SELECT COUNT(*) FROM invoices $where AND status = 'approved'");
|
||||||
$stmt->execute($params);
|
$stmt->execute(array_values($params));
|
||||||
$approved = $stmt->fetchColumn();
|
$approved = $stmt->fetchColumn();
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user