🚀 مُصادَق: تحديث برمجي جديد 2026-05-03 15:11
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Dashboard;
|
||||
|
||||
use App\Core\{Request, Response, Database};
|
||||
@@ -15,45 +13,55 @@ final class DashboardController
|
||||
$assignedCompanyId = $request->user->assigned_company_id ?? null;
|
||||
$db = Database::getInstance();
|
||||
|
||||
$where = "WHERE tenant_id = ?";
|
||||
$companyScope = '';
|
||||
$params = [$tenantId];
|
||||
|
||||
// Fix: Only accountants should be restricted to a single company if assigned.
|
||||
// Admins and Super Admins should see all companies in their tenant.
|
||||
if ($role === 'accountant' && $assignedCompanyId) {
|
||||
$where .= " AND company_id = ?";
|
||||
$companyScope = ' AND i.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)");
|
||||
// Total this month
|
||||
$stmt = $db->prepare("SELECT COUNT(*) FROM invoices i
|
||||
WHERE i.tenant_id = ? {$companyScope} AND MONTH(i.created_at) = MONTH(CURDATE()) AND YEAR(i.created_at) = YEAR(CURDATE()) AND i.deleted_at IS NULL");
|
||||
$stmt->execute($params);
|
||||
$thisMonth = (int) $stmt->fetch()['count'];
|
||||
$thisMonth = (int)$stmt->fetchColumn();
|
||||
|
||||
// 2. Approved vs Rejected
|
||||
$stmt = $db->prepare("SELECT status, COUNT(*) as count FROM invoices {$where} GROUP BY status");
|
||||
// Status distribution
|
||||
$stmt = $db->prepare("SELECT status, COUNT(*) as count FROM invoices i
|
||||
WHERE i.tenant_id = ? {$companyScope} AND i.deleted_at IS NULL GROUP BY status");
|
||||
$stmt->execute($params);
|
||||
$statusCounts = $stmt->fetchAll();
|
||||
$statusDistribution = $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 === 'accountant' && $assignedCompanyId ? " AND i.company_id = ?" : "") . " ORDER BY i.created_at DESC LIMIT 5");
|
||||
// Subscription usage
|
||||
$stmt = $db->prepare("SELECT max_invoices_per_month, invoices_used_this_month FROM subscriptions WHERE tenant_id = ?");
|
||||
$stmt->execute([$tenantId]);
|
||||
$sub = $stmt->fetch();
|
||||
$usagePct = $sub && $sub['max_invoices_per_month'] > 0
|
||||
? round(($sub['invoices_used_this_month'] / $sub['max_invoices_per_month']) * 100)
|
||||
: 0;
|
||||
|
||||
// Recent invoices
|
||||
$stmt = $db->prepare("SELECT i.id, i.invoice_number, i.invoice_date, i.grand_total, i.status, i.created_at, c.name as company_name, i.ai_confidence_score
|
||||
FROM invoices i
|
||||
JOIN companies c ON i.company_id = c.id
|
||||
WHERE i.tenant_id = ? {$companyScope} AND i.deleted_at IS NULL
|
||||
ORDER BY i.created_at DESC LIMIT 10");
|
||||
$stmt->execute($params);
|
||||
$recent = $stmt->fetchAll();
|
||||
|
||||
// 4. Calculate Subscription Usage
|
||||
$stmt = $db->prepare("SELECT max_invoices_per_month FROM subscriptions WHERE tenant_id = ?");
|
||||
// Pending extraction (from queue)
|
||||
$stmt = $db->prepare("SELECT COUNT(*) FROM queue_jobs WHERE tenant_id = ? AND status = 'pending' AND job_type = 'ExtractInvoiceJob'");
|
||||
$stmt->execute([$tenantId]);
|
||||
$sub = $stmt->fetch();
|
||||
$maxInvoices = (int) ($sub['max_invoices_per_month'] ?? 100);
|
||||
$usage = $maxInvoices > 0 ? round(($thisMonth / $maxInvoices) * 100, 1) : 0;
|
||||
$pendingExtraction = (int)$stmt->fetchColumn();
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'total_this_month' => $thisMonth,
|
||||
'status_distribution' => $statusCounts,
|
||||
'subscription_usage' => $usagePct,
|
||||
'status_distribution' => $statusDistribution,
|
||||
'recent_invoices' => $recent,
|
||||
'subscription_usage' => $usage
|
||||
'pending_extraction' => $pendingExtraction
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user