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/includes/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/u390967363/domains/aibenproperties.com/public_html/app/includes/charges.php
<?php

function normalizeChargeType(string $s): string {
    $s = strtolower(trim($s));
    $s = preg_replace('/[^a-z0-9]+/', '_', $s);
    $s = trim($s, '_');
    return $s;
}

function copyPropertyChargesToAllocation(PDO $pdo, int $allocationId, int $propertyId, int $companyId = 0): int {
    if ($allocationId <= 0 || $propertyId <= 0) return 0;
    try {
        $hasPc = $pdo->query("SHOW TABLES LIKE 'property_charges'")->rowCount() > 0;
        $hasCc = $pdo->query("SHOW TABLES LIKE 'client_charges'")->rowCount() > 0;
        if (!$hasPc || !$hasCc) return 0;

        $sql = "SELECT id, charge_name, amount FROM property_charges WHERE property_id = ? AND is_active = 1";
        $params = [$propertyId];
        if ($companyId && function_exists('tableHasColumn') && tableHasColumn('property_charges','company_id')) { $sql .= " AND (company_id = ? OR company_id IS NULL)"; $params[] = $companyId; }
        $sql .= " ORDER BY id ASC";
        $st = $pdo->prepare($sql);
        $st->execute($params);
        $charges = $st->fetchAll(PDO::FETCH_ASSOC) ?: [];
        if (empty($charges)) return 0;

        $created = 0;
        foreach ($charges as $c) {
            $cid = (int)($c['id'] ?? 0);
            if ($cid <= 0) continue;
            $name = (string)($c['charge_name'] ?? '');
            $amt = (float)($c['amount'] ?? 0);
            if (!is_finite($amt) || $amt < 0) $amt = 0.0;

            $exists = 0;
            try {
                $chk = $pdo->prepare("SELECT COUNT(*) FROM client_charges WHERE allocation_id = ? AND charge_id = ? LIMIT 1");
                $chk->execute([$allocationId, $cid]);
                $exists = (int)($chk->fetchColumn() ?: 0);
            } catch (Throwable $e) { $exists = 0; }
            if ($exists > 0) continue;

            $cols = ['allocation_id','charge_id','amount','amount_paid','status','created_at'];
            $vals = [$allocationId, $cid, $amt, 0.0, ($amt <= 0.0 ? 'paid' : 'unpaid'), date('Y-m-d H:i:s')];
            if (function_exists('tableHasColumn') && tableHasColumn('client_charges','charge_name')) { $cols[] = 'charge_name'; $vals[] = $name !== '' ? $name : null; }
            if ($companyId && function_exists('tableHasColumn') && tableHasColumn('client_charges','company_id')) { $cols[] = 'company_id'; $vals[] = $companyId; }
            $sqlIns = "INSERT INTO client_charges (" . implode(',', $cols) . ") VALUES (" . implode(',', array_fill(0, count($cols), '?')) . ")";
            $pdo->prepare($sqlIns)->execute($vals);
            $created++;
        }
        return $created;
    } catch (Throwable $e) {
        return 0;
    }
}

function applyPaymentToClientCharges(PDO $pdo, int $paymentId, int $companyId = 0): bool {
    if ($paymentId <= 0) return false;
    try {
        $hasCc = $pdo->query("SHOW TABLES LIKE 'client_charges'")->rowCount() > 0;
        $hasCcp = $pdo->query("SHOW TABLES LIKE 'client_charge_payments'")->rowCount() > 0;
        $hasPay = $pdo->query("SHOW TABLES LIKE 'payments'")->rowCount() > 0;
        if (!$hasCc || !$hasCcp || !$hasPay) return false;

        $st = $pdo->prepare("SELECT * FROM payments WHERE id = ? LIMIT 1");
        $st->execute([$paymentId]);
        $p = $st->fetch(PDO::FETCH_ASSOC);
        if (!$p) return false;

        $status = strtolower((string)($p['status'] ?? ''));
        $final = function_exists('kpiPaymentFinalizedStatuses') ? array_map('strtolower', kpiPaymentFinalizedStatuses()) : ['verified','approved','paid','completed','success'];
        if (!in_array($status, $final, true)) return false;

        $amount = (float)($p['amount'] ?? 0);
        if (!is_finite($amount) || $amount <= 0) return false;

        $type = '';
        if (function_exists('tableHasColumn') && tableHasColumn('payments','payment_type')) { $type = (string)($p['payment_type'] ?? ''); }
        if ($type === '' && !empty($p['meta_json'])) {
            try {
                $mj = json_decode((string)$p['meta_json'], true) ?: [];
                $type = (string)($mj['payment_type'] ?? '');
            } catch (Throwable $e) { $type = ''; }
        }
        $type = normalizeChargeType($type);
        if ($type === '' || $type === 'land') return false;

        $clientChargeId = 0;
        if (function_exists('tableHasColumn') && tableHasColumn('payments','client_charge_id')) { $clientChargeId = (int)($p['client_charge_id'] ?? 0); }
        if ($clientChargeId <= 0 && !empty($p['meta_json'])) {
            try {
                $mj = json_decode((string)$p['meta_json'], true) ?: [];
                $clientChargeId = (int)($mj['client_charge_id'] ?? 0);
            } catch (Throwable $e) { $clientChargeId = 0; }
        }
        if ($clientChargeId <= 0) {
            $allocId = (int)($p['allocation_id'] ?? 0);
            if ($allocId <= 0) return false;
            $sql = "SELECT cc.id, cc.amount, cc.amount_paid, cc.charge_name, pc.charge_name AS base_name
                    FROM client_charges cc
                    LEFT JOIN property_charges pc ON pc.id = cc.charge_id
                    WHERE cc.allocation_id = ?
                    ORDER BY cc.id ASC";
            $stc = $pdo->prepare($sql);
            $stc->execute([$allocId]);
            $cands = $stc->fetchAll(PDO::FETCH_ASSOC) ?: [];
            foreach ($cands as $c) {
                $name = normalizeChargeType((string)($c['charge_name'] ?? ($c['base_name'] ?? '')));
                if ($name === $type || strpos($name, $type) !== false) {
                    $clientChargeId = (int)($c['id'] ?? 0);
                    break;
                }
            }
        }
        if ($clientChargeId <= 0) return false;

        $chk = $pdo->prepare("SELECT COUNT(*) FROM client_charge_payments WHERE payment_id = ? LIMIT 1");
        $chk->execute([$paymentId]);
        if ((int)$chk->fetchColumn() > 0) return true;

        $pdo->prepare("INSERT INTO client_charge_payments (client_charge_id, payment_id, amount) VALUES (?,?,?)")->execute([$clientChargeId, $paymentId, $amount]);

        $sum = $pdo->prepare("SELECT COALESCE(SUM(amount),0) FROM client_charge_payments WHERE client_charge_id = ?");
        $sum->execute([$clientChargeId]);
        $paid = (float)($sum->fetchColumn() ?: 0);
        if (!is_finite($paid) || $paid < 0) $paid = 0.0;

        $stg = $pdo->prepare("SELECT amount FROM client_charges WHERE id = ? LIMIT 1");
        $stg->execute([$clientChargeId]);
        $due = (float)($stg->fetchColumn() ?: 0);
        if (!is_finite($due) || $due < 0) $due = 0.0;
        $newStatus = $paid + 0.01 >= $due && $due > 0 ? 'paid' : ($paid > 0 ? 'part_paid' : 'unpaid');
        if ($due <= 0) { $newStatus = 'paid'; }

        $u = $pdo->prepare("UPDATE client_charges SET amount_paid = ?, status = ?, updated_at = NOW() WHERE id = ?");
        $u->execute([min($paid, $due > 0 ? $due : $paid), $newStatus, $clientChargeId]);
        return true;
    } catch (Throwable $e) {
        return false;
    }
}


Hry