Heray-Was-Here
Server : LiteSpeed
System : Linux uk-fast-web1372.main-hosting.eu 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64
User : u390967363 ( 390967363)
PHP Version : 8.2.30
Disable Function : system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Directory :  /home/u390967363/domains/aibenproperties.com/public_html/app/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/u390967363/domains/aibenproperties.com/public_html/app/management-dashboard.php
<?php
include 'includes/header.php';
require_once 'includes/db.php';
require_once 'includes/functions.php';

$companyId = getCurrentCompanyId();

$kpis = [
    'properties' => 0,
    'clients' => 0,
    'revenue_month' => 0,
    'expenses_month' => 0,
    'net_profit_month' => 0,
    'overdue' => 0,
];

try {
    $q = $companyId && tableHasColumn('properties', 'company_id')
        ? $pdo->prepare("SELECT COUNT(*) FROM properties WHERE company_id = ?")
        : $pdo->query("SELECT COUNT(*) FROM properties");
    if ($companyId && $q instanceof PDOStatement) { $q->execute([$companyId]); $kpis['properties'] = (int)$q->fetchColumn(); }
    else { $kpis['properties'] = (int)$q->fetchColumn(); }
} catch (Exception $e) {}

try {
    $q = $companyId && tableHasColumn('users', 'company_id')
        ? $pdo->prepare("SELECT COUNT(*) FROM users WHERE role = 'client' AND company_id = ?")
        : $pdo->prepare("SELECT COUNT(*) FROM users WHERE role = 'client'");
    $q->execute($companyId && tableHasColumn('users', 'company_id') ? [$companyId] : []);
    $kpis['clients'] = (int)$q->fetchColumn();
} catch (Exception $e) {}

try {
    $monthStart = date('Y-m-01 00:00:00');
    $monthEnd = date('Y-m-t 23:59:59');
    $dateCol = function_exists('kpiPaymentDateColumn') ? kpiPaymentDateColumn('payments') : null;
    if ($dateCol) {
        $statusApproved = function_exists('kpiSqlList') ? kpiSqlList(kpiPaymentFinalizedStatuses()) : "('verified','approved','paid','completed','success')";
        [$companyClause, $companyParams] = function_exists('kpiCompanyFilter') ? kpiCompanyFilter('payments', $companyId, true) : ['', []];
        $st = $pdo->prepare("SELECT COALESCE(SUM(amount),0) FROM payments WHERE status IN $statusApproved AND $dateCol BETWEEN ? AND ?" . $companyClause);
        $st->execute(array_merge([$monthStart, $monthEnd], $companyParams));
        $kpis['revenue_month'] = (float)($st->fetchColumn() ?: 0);
    }
} catch (Exception $e) {}

try {
    if (function_exists('buildExpensesUnionSql')) {
        $p = [];
        $union = buildExpensesUnionSql(['start_date' => substr((string)$monthStart, 0, 10), 'end_date' => substr((string)$monthEnd, 0, 10), 'include_pending_manual' => false, 'company_id' => $companyId], $p, false);
        $st = $pdo->prepare("SELECT COALESCE(SUM(amount),0) FROM $union e");
        $st->execute($p);
        $kpis['expenses_month'] = (float)($st->fetchColumn() ?: 0);
    } elseif (function_exists('get_expense_totals')) {
        $et = get_expense_totals(['start_date' => substr((string)$monthStart, 0, 10), 'end_date' => substr((string)$monthEnd, 0, 10), 'company_id' => $companyId]);
        $kpis['expenses_month'] = (float)($et['total_all'] ?? 0);
    }
} catch (Exception $e) { $kpis['expenses_month'] = 0.0; }
$kpis['net_profit_month'] = (float)$kpis['revenue_month'] - (float)$kpis['expenses_month'];

try {
    $ov = function_exists('kpiOverdueInstallmentsSummary') ? kpiOverdueInstallmentsSummary($pdo, $companyId, true) : ['count' => 0, 'amount' => 0.0];
    $kpis['overdue'] = (int)($ov['count'] ?? 0);
} catch (Exception $e) {}
$kpis['queue_waiting'] = 0;
try {
    $kpis['queue_waiting'] = function_exists('kpiCountAllocations')
        ? kpiCountAllocations($pdo, kpiAllocationStatuses('awaiting_allocation'), $companyId)
        : 0;
} catch (Exception $e) {}
$kpis['transfers_pending'] = 0;
try {
    $hasTransfers = $pdo->query("SHOW TABLES LIKE 'ownership_transfers'")->rowCount() > 0;
    if ($hasTransfers) {
        $sql = "SELECT COUNT(*) FROM ownership_transfers WHERE status = 'pending'";
        if ($companyId && function_exists('tableHasColumn') && tableHasColumn('ownership_transfers','company_id')) {
            $sql .= " AND (company_id = " . (int)$companyId . " OR company_id IS NULL)";
        }
        $kpis['transfers_pending'] = (int)$pdo->query($sql)->fetchColumn();
    }
} catch (Exception $e) { $kpis['transfers_pending'] = 0; }
$labels = [];
$revData = [];
$allocData = [];
try {
    $now = new DateTime('first day of this month');
    $dateCol = function_exists('kpiPaymentDateColumn') ? kpiPaymentDateColumn('payments') : null;
    $statusApproved = function_exists('kpiSqlList') ? kpiSqlList(kpiPaymentFinalizedStatuses()) : "('verified','approved','paid','completed','success')";
    for ($i = 5; $i >= 0; $i--) {
        $d = clone $now;
        $d->modify("-$i month");
        $start = $d->format('Y-m-01 00:00:00');
        $end = (clone $d)->modify('first day of next month')->format('Y-m-01 00:00:00');
        $labels[] = $d->format('M Y');
        $sum = 0;
        if ($dateCol) {
            [$companyClause, $companyParams] = function_exists('kpiCompanyFilter') ? kpiCompanyFilter('payments', $companyId, true) : ['', []];
            $st = $pdo->prepare("SELECT COALESCE(SUM(amount),0) FROM payments WHERE status IN $statusApproved AND $dateCol >= ? AND $dateCol < ?" . $companyClause);
            $st->execute(array_merge([$start, $end], $companyParams));
            $sum = (float)($st->fetchColumn() ?: 0);
        }
        $revData[] = $sum;
        $cnt = 0;
        if (tableHasColumn('allocations', 'created_at')) {
            if ($companyId && tableHasColumn('allocations', 'company_id')) {
                $st2 = $pdo->prepare("SELECT COUNT(*) FROM allocations WHERE created_at >= ? AND created_at < ? AND company_id = ?");
                $st2->execute([$start, $end, $companyId]);
            } else {
                $st2 = $pdo->prepare("SELECT COUNT(*) FROM allocations WHERE created_at >= ? AND created_at < ?");
                $st2->execute([$start, $end]);
            }
            $cnt = (int)$st2->fetchColumn();
        }
        $allocData[] = $cnt;
    }
} catch (Exception $e) {}

$alloc_pending_count = $alloc_approved_count = $alloc_active_count = $alloc_completed_count = $alloc_rejected_count = 0;
try {
    $allocCounts = function_exists('kpiAllocationStatusCounts')
        ? kpiAllocationStatusCounts($pdo, $companyId, false, ['pending', 'approved', 'active', 'completed', 'rejected'])
        : [];
    $alloc_pending_count = (int)($allocCounts['pending'] ?? 0);
    $alloc_approved_count = (int)($allocCounts['approved'] ?? 0);
    $alloc_active_count = (int)($allocCounts['active'] ?? 0);
    $alloc_completed_count = (int)($allocCounts['completed'] ?? 0);
    $alloc_rejected_count = (int)($allocCounts['rejected'] ?? 0);
} catch (Exception $e) {}
?>
<style>
.md-chart-wrap{position:relative;height:260px}
@media (max-width: 992px){.md-chart-wrap{height:220px}}
@media (max-width: 576px){.md-chart-wrap{height:200px}}
.md-card-head{gap:.25rem}
.dashboard-premium{--card-radius:16px;--card-pad:22px;--bg-soft:#f5f7fb;--text-muted:#6b7280;--shadow-soft:0 8px 24px rgba(0,0,0,0.06);font-family:"Inter","Poppins",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;background:var(--bg-soft)}
.dashboard-premium .card{border:0;border-radius:var(--card-radius);box-shadow:var(--shadow-soft)}
.dashboard-premium .metric-label{color:var(--text-muted);font-size:.85rem}
.dashboard-premium .metric-value{font-weight:800;font-size:2rem;line-height:1.1}
.dashboard-premium .stat-gradient{background:linear-gradient(135deg,#ff6b5f 0%,#ff8a5b 100%)!important;color:#fff}
.dashboard-premium .stat-gradient .metric-label{color:rgba(255,255,255,.85)}
.dashboard-premium .section-title{display:flex;align-items:center;justify-content:space-between;margin:8px 0 16px 0}
.dashboard-premium .chip{display:inline-flex;align-items:center;gap:6px;border-radius:9999px;padding:6px 10px;font-size:12px;border:1px solid #e5e7eb;background:#fff;color:#111827}
.dashboard-premium .chip i{color:#64748b}
.quick-actions .btn{border-radius:12px}
.hero-metrics{display:grid;grid-template-columns:2fr 1fr 1fr 1fr;gap:12px}
@media (max-width: 992px){.hero-metrics{grid-template-columns:1fr 1fr}}
@media (max-width: 576px){.hero-metrics{grid-template-columns:1fr}}
.hero-metrics .card{height:100%}
.revenue-card{background:linear-gradient(135deg,#0f172a,#1e293b)!important;color:#ffffff!important;box-shadow:0 10px 25px rgba(0,0,0,0.2)!important}
.revenue-card *{color:#ffffff!important}
.revenue-card .label{color:#cbd5f5!important;font-size:12px;font-weight:900;letter-spacing:.08em;text-transform:uppercase}
.revenue-card .amount{font-size:clamp(22px, 2.2vw, 28px);font-weight:700;letter-spacing:-.02em;overflow-wrap:anywhere;word-break:break-word;font-variant-numeric:tabular-nums;line-height:1.12}
.revenue-card .sub-text{font-size:12px;color:#94a3b8!important}
.sync-badge{font-size:11px;background:#22c55e;color:#ffffff!important;padding:2px 8px;border-radius:999px;font-weight:800}
.alert-strip{display:flex;gap:10px;background:#fff7ed;padding:10px;border-radius:10px;align-items:stretch;flex-wrap:wrap}
.alert-strip .alert-item{display:flex;gap:10px;align-items:center;padding:8px 10px;border-radius:10px;border:1px solid rgba(249,115,22,.18);background:rgba(255,255,255,.75);text-decoration:none;color:#111827}
.alert-strip .alert-ico{width:34px;height:34px;border-radius:10px;display:inline-flex;align-items:center;justify-content:center;background:rgba(249,115,22,.15);color:#9a3412}
.alert-strip .alert-meta{display:flex;flex-direction:column;line-height:1.1}
.alert-strip .alert-label{font-size:12px;font-weight:800;color:#9a3412;letter-spacing:.04em;text-transform:uppercase}
.alert-strip .alert-value{font-size:16px;font-weight:900}
.action-bar .group-title{font-size:12px;font-weight:900;color:#374151;letter-spacing:.08em;text-transform:uppercase;margin-bottom:8px}
.action-bar .btn{border-radius:12px}
.analytics-grid{display:grid;grid-template-columns:2fr 1fr;gap:15px}
@media (max-width: 992px){.analytics-grid{grid-template-columns:1fr}}
.finance-card{background:linear-gradient(135deg,#111827,#1f2937)!important;color:#ffffff!important}
.finance-card *{color:#ffffff!important}
.finance-card .btn{border-color:rgba(255,255,255,.25)}
.finance-card .btn:hover{background:rgba(255,255,255,.08)}
.health-grid{display:grid;grid-template-columns:repeat(3,1fr);gap:12px}
@media (max-width: 992px){.health-grid{grid-template-columns:1fr}}
.health-card{border:1px solid #e5e7eb;border-radius:16px;padding:14px 14px;text-decoration:none;color:#111827;background:#fff;display:flex;justify-content:space-between;gap:12px}
.health-card .health-title{font-size:12px;font-weight:900;letter-spacing:.08em;text-transform:uppercase;color:#6b7280}
.health-card .health-value{font-size:22px;font-weight:900;line-height:1.1}
.health-card .health-status{display:inline-flex;align-items:center;gap:6px;border-radius:9999px;padding:6px 10px;font-size:12px;font-weight:800;border:1px solid #e5e7eb;background:#f8fafc;color:#111827}
.health-card .health-ico{width:44px;height:44px;border-radius:14px;display:inline-flex;align-items:center;justify-content:center;background:#f1f5f9;color:#0f172a;flex:0 0 auto}
</style>

<div class="container-fluid px-4 dashboard-premium">
    <div class="d-flex justify-content-between align-items-center mt-4 mb-4">
        <div>
            <h1 class="h3 mb-0 text-navy">Operations Dashboard</h1>
            <p class="text-muted">Operations overview (management view)</p>
        </div>
        <div class="d-none d-md-flex gap-2">
            <a href="reports.php" class="btn btn-outline-secondary btn-sm"><i class="fa-solid fa-chart-pie me-2"></i>Reports</a>
            <a href="finance-dashboard.php" class="btn btn-outline-secondary btn-sm"><i class="fa-solid fa-gauge me-2"></i>Finance</a>
            <a href="clients.php" class="btn btn-outline-secondary btn-sm"><i class="fa-solid fa-users me-2"></i>Clients</a>
            <?php if (file_exists(__DIR__ . '/ownership-transfers.php')): ?>
                <a href="ownership-transfers.php" class="btn btn-outline-secondary btn-sm"><i class="fa-solid fa-right-left me-2"></i>Transfers<?= $kpis['transfers_pending'] > 0 ? (' <span class="badge bg-warning text-dark ms-1">'.(int)$kpis['transfers_pending'].'</span>') : '' ?></a>
            <?php endif; ?>
        </div>
    </div>

    <div class="hero-metrics mb-3">
        <div class="card hover-elevate gold-outline revenue-card hero-revenue">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start gap-3">
                    <div>
                        <div class="d-flex flex-wrap align-items-center gap-2 mb-2">
                            <div class="label">Verified Revenue (Finance Synced)</div>
                            <span class="sync-badge">Synced with Finance</span>
                        </div>
                        <div class="amount"><?= formatCurrency($kpis['revenue_month']) ?></div>
                        <div class="sub-text mt-2">Finalized payments (same as Finance)</div>
                    </div>
                    <div class="rounded d-inline-flex align-items-center justify-content-center" style="width:54px;height:54px;background:rgba(255,255,255,.10)">
                        <i class="fa-solid fa-sack-dollar"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="card hover-elevate gold-outline revenue-card hero-revenue">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start gap-3">
                    <div>
                        <div class="d-flex flex-wrap align-items-center gap-2 mb-2">
                            <div class="label">Net Profit (This Month)</div>
                            <span class="sync-badge">Synced with Finance</span>
                        </div>
                        <div class="amount"><?= formatCurrency($kpis['net_profit_month']) ?></div>
                        <div class="sub-text mt-2">Revenue <?= formatCurrency($kpis['revenue_month']) ?> ยท Expenses <?= formatCurrency($kpis['expenses_month']) ?></div>
                    </div>
                    <div class="rounded d-inline-flex align-items-center justify-content-center" style="width:54px;height:54px;background:rgba(255,255,255,.10)">
                        <i class="fa-solid fa-chart-line"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="card hover-elevate h-100 gold-outline">
            <div class="card-body">
                <div class="rounded bg-accent-soft d-inline-flex align-items-center justify-content-center p-3 mb-3" style="width:44px;height:44px">
                    <i class="fa-solid fa-users"></i>
                </div>
                <div class="metric-label">Clients</div>
                <div class="metric-value"><?= number_format($kpis['clients']) ?></div>
                <div class="metric-trend text-muted"><span class="chip">Active</span></div>
            </div>
        </div>
        <div class="card hover-elevate h-100 gold-outline">
            <div class="card-body">
                <div class="rounded bg-primary-soft d-inline-flex align-items-center justify-content-center p-3 mb-3" style="width:44px;height:44px">
                    <i class="fa-solid fa-house-chimney"></i>
                </div>
                <div class="metric-label">Properties</div>
                <div class="metric-value"><?= number_format($kpis['properties']) ?></div>
                <div class="metric-trend text-muted"><span class="chip">Total units</span></div>
            </div>
        </div>
        <div class="card hover-elevate h-100 gold-outline">
            <div class="card-body">
                <div class="rounded bg-danger-soft d-inline-flex align-items-center justify-content-center p-3 mb-3" style="width:44px;height:44px">
                    <i class="fa-regular fa-calendar-xmark"></i>
                </div>
                <div class="metric-label">Overdue Installments</div>
                <div class="metric-value"><?= number_format($kpis['overdue']) ?></div>
                <div class="metric-trend"><span class="text-danger fw-600">Action required</span></div>
            </div>
        </div>
    </div>

    <div class="alert-strip mb-4">
        <a class="alert-item" href="finance-installments.php">
            <span class="alert-ico"><i class="fa-regular fa-calendar-xmark"></i></span>
            <span class="alert-meta">
                <span class="alert-label">Overdue Installments</span>
                <span class="alert-value"><?= number_format($kpis['overdue']) ?></span>
            </span>
        </a>
        <a class="alert-item" href="allocation-queue.php">
            <span class="alert-ico"><i class="fa-solid fa-list-check"></i></span>
            <span class="alert-meta">
                <span class="alert-label">Allocation Queue</span>
                <span class="alert-value"><?= number_format($kpis['queue_waiting']) ?></span>
            </span>
        </a>
        <a class="alert-item" href="allocations.php?status=pending">
            <span class="alert-ico"><i class="fa-solid fa-user-shield"></i></span>
            <span class="alert-meta">
                <span class="alert-label">Pending Approvals</span>
                <span class="alert-value"><?= number_format((int)$alloc_pending_count) ?></span>
            </span>
        </a>
    </div>

    <div class="card action-bar mb-4">
        <div class="card-body d-flex flex-column flex-lg-row justify-content-between gap-4">
            <div>
                <div class="group-title">Primary</div>
                <div class="d-flex flex-wrap gap-2">
                    <a href="deal-submission.php" class="btn btn-primary btn-sm"><i class="fa-solid fa-file-invoice-dollar me-2"></i>Record Payment</a>
                    <a href="finance-payment-queue.php" class="btn btn-outline-primary btn-sm"><i class="fa-solid fa-clipboard-check me-2"></i>Verify Payments</a>
                </div>
            </div>
            <div>
                <div class="group-title">Secondary</div>
                <div class="d-flex flex-wrap gap-2">
                    <a href="clients.php" class="btn btn-outline-secondary btn-sm"><i class="fa-solid fa-users me-2"></i>Clients</a>
                    <a href="allocations.php" class="btn btn-outline-secondary btn-sm"><i class="fa-solid fa-diagram-project me-2"></i>Allocations</a>
                    <a href="reports.php" class="btn btn-outline-secondary btn-sm"><i class="fa-solid fa-chart-line me-2"></i>Reports</a>
                </div>
            </div>
        </div>
    </div>

    <div class="analytics-grid mb-4">
        <div class="card shadow-sm hover-elevate h-100">
            <div class="card-header bg-white d-flex flex-column flex-sm-row justify-content-between align-items-start align-items-sm-center md-card-head">
                <strong class="fw-semibold">Revenue Trend</strong>
                <span class="text-muted small">Last 6 months</span>
            </div>
            <div class="card-body">
                <div class="md-chart-wrap">
                    <canvas id="revTrend"></canvas>
                </div>
            </div>
        </div>
        <div class="d-flex flex-column gap-3">
            <div class="card shadow-sm hover-elevate">
                <div class="card-header bg-white d-flex flex-column flex-sm-row justify-content-between align-items-start align-items-sm-center md-card-head">
                    <strong class="fw-semibold">Allocations Trend</strong>
                    <span class="text-muted small">Last 6 months</span>
                </div>
                <div class="card-body">
                    <div class="md-chart-wrap" style="height:220px">
                        <canvas id="allocTrend"></canvas>
                    </div>
                </div>
            </div>
            <div class="card shadow-sm hover-elevate">
                <div class="card-header bg-white d-flex flex-column flex-sm-row justify-content-between align-items-start align-items-sm-center md-card-head">
                    <strong class="fw-semibold">Allocations Mix</strong>
                </div>
                <div class="card-body">
                    <div class="md-chart-wrap" style="height:220px">
                        <canvas id="allocDonutMgmt"></canvas>
                    </div>
                </div>
            </div>
            <div class="card shadow-sm hover-elevate finance-card">
                <div class="card-header d-flex justify-content-between align-items-center" style="background:transparent;border-bottom:1px solid rgba(255,255,255,.10)">
                    <strong>Finance Snapshot</strong>
                    <a href="reports-financial.php" class="btn btn-sm btn-outline-light">Open Reports</a>
                </div>
                <div class="card-body">
                    <div class="d-flex align-items-center justify-content-between mb-3">
                        <div class="small" style="color:rgba(255,255,255,.78)">Receivables</div>
                        <div class="fw-bold">
                            <?php
                            $recv = 0;
                            try {
                                $recv = function_exists('kpiSumInvoiceReceivables')
                                    ? kpiSumInvoiceReceivables($pdo, $companyId)
                                    : 0.0;
                            } catch (Exception $e) {}
                            echo formatCurrency($recv);
                            ?>
                        </div>
                    </div>
                    <div class="d-flex align-items-center justify-content-between">
                        <div class="small" style="color:rgba(255,255,255,.78)">Verified This Month</div>
                        <div class="fw-bold"><?= formatCurrency($kpis['revenue_month']) ?></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="card shadow-sm hover-elevate mb-4">
        <div class="card-header bg-white d-flex justify-content-between align-items-center">
            <strong>System Health</strong>
            <a href="maintenance.php" class="btn btn-sm btn-outline-primary">Open Operations</a>
        </div>
        <div class="card-body">
            <div class="health-grid">
                <a class="health-card" href="maintenance.php">
                    <div>
                        <div class="health-title">Maintenance</div>
                        <div class="health-value">
                            <?php
                            $m = 0;
                            try {
                                if ($companyId) {
                                    $s = $pdo->prepare("SELECT COUNT(*) FROM maintenance_requests WHERE status IN ('open','pending','in_progress') AND company_id = ?");
                                    $s->execute([$companyId]); $m = (int)$s->fetchColumn();
                                } else {
                                    $m = (int)$pdo->query("SELECT COUNT(*) FROM maintenance_requests WHERE status IN ('open','pending','in_progress')")->fetchColumn();
                                }
                            } catch (Exception $e) {}
                            echo number_format($m);
                            ?>
                        </div>
                        <div class="health-status">Open</div>
                    </div>
                    <div class="health-ico"><i class="fa-solid fa-screwdriver-wrench"></i></div>
                </a>
                <a class="health-card" href="support.php">
                    <div>
                        <div class="health-title">Support</div>
                        <div class="health-value">
                            <?php
                            $c = 0;
                            try {
                                if ($companyId) {
                                    $s = $pdo->prepare("SELECT COUNT(*) FROM support_tickets WHERE status IN ('open','pending') AND company_id = ?");
                                    $s->execute([$companyId]); $c = (int)$s->fetchColumn();
                                } else {
                                    $c = (int)$pdo->query("SELECT COUNT(*) FROM support_tickets WHERE status IN ('open','pending')")->fetchColumn();
                                }
                            } catch (Exception $e) {}
                            echo number_format($c);
                            ?>
                        </div>
                        <div class="health-status">Active</div>
                    </div>
                    <div class="health-ico"><i class="fa-solid fa-headset"></i></div>
                </a>
                <a class="health-card" href="tasks.php?assigned_to=me&overdue=1">
                    <div>
                        <div class="health-title">Tasks</div>
                        <div class="health-value">
                            <?php
                            $t = 0;
                            try {
                                if ($companyId) {
                                    $s = $pdo->prepare("SELECT COUNT(*) FROM tasks WHERE due_date < NOW() AND status != 'completed' AND company_id = ?");
                                    $s->execute([$companyId]); $t = (int)$s->fetchColumn();
                                } else {
                                    $t = (int)$pdo->query("SELECT COUNT(*) FROM tasks WHERE due_date < NOW() AND status != 'completed'")->fetchColumn();
                                }
                            } catch (Exception $e) {}
                            echo number_format($t);
                            ?>
                        </div>
                        <div class="health-status">Overdue</div>
                    </div>
                    <div class="health-ico"><i class="fa-solid fa-list-check"></i></div>
                </a>
            </div>
        </div>
    </div>

    <div class="card shadow-sm hover-elevate mb-4">
        <div class="card-header bg-white">
            <strong>Quick Commands</strong>
        </div>
        <div class="card-body d-flex flex-wrap gap-2">
            <a href="invoices.php" class="btn btn-dark btn-sm"><i class="fa-solid fa-file-invoice me-2"></i>Create Invoice</a>
            <a href="client-add.php" class="btn btn-outline-secondary btn-sm"><i class="fa-solid fa-user-plus me-2"></i>Add Client</a>
            <a href="allocation-add.php" class="btn btn-outline-secondary btn-sm"><i class="fa-solid fa-plus me-2"></i>Allocate Property</a>
        </div>
    </div>
</div>

<script>
(function(){
    var labels = <?= json_encode($labels) ?>;
    var revData = <?= json_encode($revData) ?>;
    var allocData = <?= json_encode($allocData) ?>;
    var ctx1 = document.getElementById('revTrend');
    if (ctx1 && window.Chart) {
        new Chart(ctx1.getContext('2d'), {
            type: 'line',
            data: {
                labels: labels,
                datasets: [{
                    label: 'Revenue',
                    data: revData,
                    borderColor: '#10b981',
                    backgroundColor: 'rgba(16,185,129,0.15)',
                    tension: 0.35,
                    fill: true,
                    borderWidth: 2,
                    pointRadius: 2
                }]
            },
            options: {
                scales: {
                    y: { beginAtZero: true, grid: { color: 'rgba(226,232,240,0.6)' } },
                    x: { grid: { display: false } }
                },
                plugins: { legend: { display: false } },
                responsive: true,
                maintainAspectRatio: false
            }
        });
    }
    var ctx2 = document.getElementById('allocTrend');
    if (ctx2 && window.Chart) {
        new Chart(ctx2.getContext('2d'), {
            type: 'bar',
            data: {
                labels: labels,
                datasets: [{
                    label: 'Allocations',
                    data: allocData,
                    backgroundColor: 'rgba(59,130,246,0.35)',
                    borderColor: '#3b82f6',
                    borderWidth: 1,
                    borderRadius: 6,
                    maxBarThickness: 18
                }]
            },
            options: {
                scales: {
                    y: { beginAtZero: true, grid: { color: 'rgba(226,232,240,0.6)' } },
                    x: { grid: { display: false } }
                },
                plugins: { legend: { display: false } },
                responsive: true,
                maintainAspectRatio: false
            }
        });
    }
})();
</script>
<script>
(function(){
    var d = document.getElementById('allocDonutMgmt');
    if (!d || !window.Chart) return;
    var centerText = {
        id: 'centerText',
        afterDraw: function(chart){
            try {
                var ctx = chart.ctx;
                var ca = chart.chartArea;
                if (!ca) return;
                var cx = (ca.left + ca.right) / 2;
                var cy = (ca.top + ca.bottom) / 2;
                var ds = (chart.data.datasets[0].data || []);
                var total = ds.reduce(function(a,b){ return a + Number(b||0); }, 0);
                ctx.save();
                ctx.textAlign = 'center';
                ctx.textBaseline = 'middle';
                ctx.fillStyle = '#111827';
                ctx.font = '700 18px Inter, Arial, sans-serif';
                ctx.fillText(String(total), cx, cy - 6);
                ctx.fillStyle = '#6b7280';
                ctx.font = '500 10px Inter, Arial, sans-serif';
                ctx.fillText('Total', cx, cy + 10);
                ctx.restore();
            } catch (e) {}
        }
    };
    if (Chart.register) Chart.register(centerText);
    new Chart(d.getContext('2d'), {
        type: 'doughnut',
        data: {
            labels: ['Pending','Approved','Active','Completed','Rejected'],
            datasets: [{
                data: [<?= (int)$alloc_pending_count ?>, <?= (int)$alloc_approved_count ?>, <?= (int)$alloc_active_count ?>, <?= (int)$alloc_completed_count ?>, <?= (int)$alloc_rejected_count ?>],
                backgroundColor: ['#f59e0b','#3b82f6','#0ea5e9','#10b981','#ef4444'],
                borderWidth: 0
            }]
        },
        options: { cutout: '58%', plugins: { legend: { display: false }, centerText: {} }, maintainAspectRatio: false }
    });
})();
</script>

<?php include 'includes/footer.php'; ?>

Hry