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/clients-crm.php
<?php
session_start();
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/functions.php';
$role = $_SESSION['user_role'] ?? 'guest';
$role_norm = strtolower(str_replace([' ', '-'], '_', (string)$role));
$allowed = ['super_admin','admin','sales_manager','agent','marketer','customer_rep','contact_rep','chairman_ceo'];
if (!isset($_SESSION['user_id']) || !in_array($role_norm, $allowed)) {
    include __DIR__ . '/includes/header.php';
    echo '<div class="container p-4"><div class="alert alert-danger">Access denied.</div></div>';
    include __DIR__ . '/includes/footer.php';
    exit;
}
$notice = null; $type = 'info';
$companyId = getCurrentCompanyId();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['crm_action'] ?? '';
    if ($action === 'record_form_fee') {
        $clientId = isset($_POST['client_id']) && ctype_digit($_POST['client_id']) ? (int)$_POST['client_id'] : 0;
        $amount = isset($_POST['amount']) ? (float)$_POST['amount'] : 0;
        $pdate = $_POST['pay_date'] ?? date('Y-m-d');
        $proof = null;
        try {
            if (isset($_FILES['proof']) && $_FILES['proof']['error'] === UPLOAD_ERR_OK) {
                $dir = __DIR__ . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'form_fees';
                if (!is_dir($dir)) { @mkdir($dir, 0777, true); }
                $n = 'proof_' . time() . '_' . preg_replace('/[^a-zA-Z0-9_\.-]/', '_', basename($_FILES['proof']['name']));
                $target = $dir . DIRECTORY_SEPARATOR . $n;
                if (move_uploaded_file($_FILES['proof']['tmp_name'], $target)) {
                    $proof = 'uploads/form_fees/' . $n;
                }
            }
        } catch (Throwable $e) {}
        if ($clientId > 0 && $amount > 0) {
            try {
                $cols = ['user_id','amount','status'];
                $vals = [$clientId, $amount, 'pending_confirmation'];
                $ph = ['?','?','?'];
                if ($companyId && function_exists('tableHasColumn') && tableHasColumn('payments','company_id')) { $cols[]='company_id'; $vals[]=$companyId; $ph[]='?'; }
                $methodCol = function_exists('tableHasColumn') && tableHasColumn('payments','method') ? 'method' : (tableHasColumn('payments','payment_method') ? 'payment_method' : null);
                if ($methodCol) { $cols[] = $methodCol; $vals[] = 'bank_transfer'; $ph[]='?'; }
                if ($proof && function_exists('tableHasColumn') && tableHasColumn('payments','proof_file')) { $cols[]='proof_file'; $vals[]=$proof; $ph[]='?'; }
                if (function_exists('tableHasColumn') && tableHasColumn('payments','payment_date')) { $cols[]='payment_date'; $vals[]=$pdate; $ph[]='?'; }
                elseif (function_exists('tableHasColumn') && tableHasColumn('payments','date')) { $cols[]='date'; $vals[]=$pdate; $ph[]='?'; }
                $sql = "INSERT INTO payments (" . implode(',', $cols) . ") VALUES (" . implode(',', $ph) . ")";
                $st = $pdo->prepare($sql);
                $st->execute($vals);
                logActivity($_SESSION['user_id'], 'FORM_FEE_SUBMITTED', json_encode(['client_id'=>$clientId,'amount'=>$amount,'date'=>$pdate]));
                $notice = 'Form fee recorded. Awaiting Finance verification.';
                $type = 'success';
            } catch (Exception $e) {
                $notice = 'Failed to record fee.';
                $type = 'danger';
            }
        } else {
            $notice = 'Please select client and enter amount.';
            $type = 'danger';
        }
    }
    if ($action === 'assign_sales_actors') {
        $clientId = isset($_POST['client_id_assign']) && ctype_digit($_POST['client_id_assign']) ? (int)$_POST['client_id_assign'] : 0;
        $marketer = $_POST['marketer_id'] ?? '';
        $agent = $_POST['agent_id'] ?? '';
        $care = $_POST['care_id'] ?? '';
        if ($clientId > 0) {
            try {
                logActivity($_SESSION['user_id'], 'ASSIGN_SALES_ACTORS', json_encode([
                    'client_id'=>$clientId,'marketer'=>$marketer,'agent'=>$agent,'care'=>$care
                ]));
                $notice = 'Selections captured (foundation). Persistence wiring will be added next.';
                $type = 'success';
            } catch (Exception $e) {
                $notice = 'Failed to capture selections.';
                $type = 'danger';
            }
        } else {
            $notice = 'Select a client first.';
            $type = 'danger';
        }
    }
}

$clients = [];
try {
    $q = "SELECT id, name, email FROM users WHERE role = 'client'";
    if ($companyId && function_exists('tableHasColumn') && tableHasColumn('users','company_id')) { $q .= " AND company_id = " . (int)$companyId; }
    $q .= " ORDER BY name";
    $clients = $pdo->query($q)->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {}
$agents = [];
try {
    $q = "SELECT id, name FROM users WHERE role = 'agent' ORDER BY name";
    if ($companyId && function_exists('tableHasColumn') && tableHasColumn('users','company_id')) { $q = "SELECT id, name FROM users WHERE role = 'agent' AND company_id = " . (int)$companyId . " ORDER BY name"; }
    $agents = $pdo->query($q)->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {}
$marketers = [];
try {
    $q = "SELECT id, name FROM users WHERE role IN ('marketer','sales','sales_agent') ORDER BY name";
    if ($companyId && function_exists('tableHasColumn') && tableHasColumn('users','company_id')) { $q = "SELECT id, name FROM users WHERE role IN ('marketer','sales','sales_agent') AND company_id = " . (int)$companyId . " ORDER BY name"; }
    $marketers = $pdo->query($q)->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {}
$careReps = [];
try {
    $q = "SELECT id, name FROM users WHERE role IN ('customer_rep','contact_rep') ORDER BY name";
    if ($companyId && function_exists('tableHasColumn') && tableHasColumn('users','company_id')) { $q = "SELECT id, name FROM users WHERE role IN ('customer_rep','contact_rep') AND company_id = " . (int)$companyId . " ORDER BY name"; }
    $careReps = $pdo->query($q)->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {}

include __DIR__ . '/includes/header.php';
?>
<div class="container-fluid px-4">
    <div class="d-flex justify-content-between align-items-center mt-4 mb-3">
        <div>
            <h2 class="fw-bold text-navy">Clients & CRM</h2>
            <div class="text-muted">Simplified onboarding and fee capture (foundation)</div>
        </div>
        <div class="d-flex gap-2">
            <a href="clients.php" class="btn btn-outline-secondary btn-sm"><i class="fa-solid fa-users me-2"></i>Manage Clients</a>
            <a href="client-dashboard.php" class="btn btn-outline-secondary btn-sm"><i class="fa-solid fa-user me-2"></i>Client Dashboard Viewer</a>
        </div>
    </div>
    <?php if ($notice): ?>
        <div class="alert alert-<?= htmlspecialchars($type) ?>"><?= htmlspecialchars($notice) ?></div>
    <?php endif; ?>

    <div class="row g-3">
        <div class="col-lg-6">
            <div class="card shadow-sm">
                <div class="card-header bg-white"><strong>Onboarding Form</strong></div>
                <div class="card-body">
                    <div class="text-muted mb-2">Use the existing client onboarding form. Share the link with clients.</div>
                    <a href="client-onboarding.php" class="btn btn-primary btn-sm"><i class="fa-solid fa-id-card me-2"></i>Open Onboarding Form</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 (Admin)</a>
                </div>
            </div>
            <div class="card shadow-sm mt-3">
                <div class="card-header bg-white"><strong>Assign Sales Actors</strong></div>
                <div class="card-body">
                    <form method="POST" class="row g-2">
                        <input type="hidden" name="crm_action" value="assign_sales_actors">
                        <div class="col-12">
                            <label class="form-label">Client</label>
                            <select name="client_id_assign" class="form-select" required>
                                <option value="">Select Client</option>
                                <?php foreach ($clients as $c): ?>
                                    <option value="<?= (int)$c['id'] ?>"><?= htmlspecialchars($c['name'] . ' — ' . ($c['email'] ?? '')) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Marketer</label>
                            <select name="marketer_id" class="form-select">
                                <option value="">None</option>
                                <?php foreach ($marketers as $m): ?>
                                    <option value="<?= (int)$m['id'] ?>"><?= htmlspecialchars($m['name']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Agent</label>
                            <select name="agent_id" class="form-select">
                                <option value="">None</option>
                                <?php foreach ($agents as $a): ?>
                                    <option value="<?= (int)$a['id'] ?>"><?= htmlspecialchars($a['name']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Customer Care</label>
                            <select name="care_id" class="form-select">
                                <option value="">None</option>
                                <?php foreach ($careReps as $r): ?>
                                    <option value="<?= (int)$r['id'] ?>"><?= htmlspecialchars($r['name']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-12 text-end">
                            <button class="btn btn-outline-primary btn-sm"><i class="fa-solid fa-save me-2"></i>Save Selection</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-lg-6">
            <div class="card shadow-sm">
                <div class="card-header bg-white"><strong>Form Fee Payment (Bank Transfer)</strong></div>
                <div class="card-body">
                    <form method="POST" enctype="multipart/form-data" class="row g-2">
                        <input type="hidden" name="crm_action" value="record_form_fee">
                        <div class="col-12">
                            <label class="form-label">Client</label>
                            <select name="client_id" class="form-select" required>
                                <option value="">Select Client</option>
                                <?php foreach ($clients as $c): ?>
                                    <option value="<?= (int)$c['id'] ?>"><?= htmlspecialchars($c['name'] . ' — ' . ($c['email'] ?? '')) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Amount</label>
                            <input type="number" step="0.01" min="0" name="amount" class="form-control" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Date</label>
                            <input type="date" name="pay_date" class="form-control" value="<?= date('Y-m-d') ?>" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Receipt</label>
                            <input type="file" name="proof" accept=".jpg,.jpeg,.png,.pdf" class="form-control">
                        </div>
                        <div class="col-12 text-end">
                            <button class="btn btn-success btn-sm"><i class="fa-solid fa-paper-plane me-2"></i>Submit for Verification</button>
                        </div>
                    </form>
                </div>
            </div>
            <div class="text-muted small mt-2">Allocation letters appear only after Chairman approval and allocation finalization.</div>
        </div>
    </div>
</div>
<?php include __DIR__ . '/includes/footer.php'; ?>

Hry