prepare('SHOW TABLES LIKE ?'); $stmt->execute([$table]); return $cache[$table] = (bool)$stmt->fetchColumn(); } catch (Throwable $e) { return $cache[$table] = false; } } } if (!function_exists('fourps_column_exists')) { function fourps_column_exists(string $table, string $column): bool { static $cache = []; $key = $table . '.' . $column; if (array_key_exists($key, $cache)) { return $cache[$key]; } if (!fourps_table_exists($table)) { return $cache[$key] = false; } $db = fourps_db(); if (!$db) { return $cache[$key] = false; } try { $stmt = $db->prepare("SHOW COLUMNS FROM `{$table}` LIKE ?"); $stmt->execute([$column]); return $cache[$key] = (bool)$stmt->fetch(PDO::FETCH_ASSOC); } catch (Throwable $e) { return $cache[$key] = false; } } } if (!function_exists('fourps_pick_first_existing_column')) { function fourps_pick_first_existing_column(string $table, array $columns): ?string { foreach ($columns as $column) { if (fourps_column_exists($table, $column)) { return $column; } } return null; } } if (!function_exists('fourps_first_existing_table')) { function fourps_first_existing_table(array $tables): ?string { foreach ($tables as $table) { if (fourps_table_exists($table)) { return $table; } } return null; } } if (!function_exists('fourps_money')) { function fourps_money($amount): string { return '$' . number_format((float)$amount, 2); } } if (!function_exists('fourps_fmt_datetime')) { function fourps_fmt_datetime(?string $value): string { if (!$value) { return '—'; } $ts = strtotime($value); if ($ts === false) { return (string)$value; } return date('M j, Y g:i A', $ts); } } if (!function_exists('fourps_query_one')) { function fourps_query_one(string $sql, array $params = []): ?array { $db = fourps_db(); if (!$db) { return null; } try { $stmt = $db->prepare($sql); $stmt->execute($params); $row = $stmt->fetch(PDO::FETCH_ASSOC); return $row ?: null; } catch (Throwable $e) { return null; } } } if (!function_exists('fourps_query_all')) { function fourps_query_all(string $sql, array $params = []): array { $db = fourps_db(); if (!$db) { return []; } try { $stmt = $db->prepare($sql); $stmt->execute($params); return $stmt->fetchAll(PDO::FETCH_ASSOC) ?: []; } catch (Throwable $e) { return []; } } } $table = fourps_first_existing_table(['fourps_payments', 'payments']); $id = isset($_GET['id']) ? (int)$_GET['id'] : 0; $titleColumn = $table ? fourps_pick_first_existing_column($table, ['title', 'name', 'payment_name', 'reference', 'number', 'payment_reference', 'transaction_id']) : null; $statusColumn = $table ? fourps_pick_first_existing_column($table, ['status', 'payment_status', 'state']) : null; $amountColumn = $table ? fourps_pick_first_existing_column($table, ['amount', 'payment_amount', 'total']) : null; $methodColumn = $table ? fourps_pick_first_existing_column($table, ['payment_method', 'method', 'tender_type']) : null; $createdColumn = $table ? fourps_pick_first_existing_column($table, ['created_at', 'date_created', 'received_at', 'payment_date']) : null; $updatedColumn = $table ? fourps_pick_first_existing_column($table, ['updated_at', 'modified_at', 'last_updated_at', 'posted_at', 'cleared_at']) : null; $customerColumn = $table ? fourps_pick_first_existing_column($table, ['customer_name', 'client_name', 'member_name', 'owner_name', 'payer_name']) : null; $emailColumn = $table ? fourps_pick_first_existing_column($table, ['customer_email', 'client_email', 'member_email', 'owner_email', 'payer_email']) : null; $phoneColumn = $table ? fourps_pick_first_existing_column($table, ['customer_phone', 'client_phone', 'member_phone', 'owner_phone', 'payer_phone']) : null; $invoiceRefColumn = $table ? fourps_pick_first_existing_column($table, ['invoice_id', 'invoice_number', 'invoice_reference']) : null; $claimRefColumn = $table ? fourps_pick_first_existing_column($table, ['claim_id', 'claim_number', 'claim_reference']) : null; $noteColumn = $table ? fourps_pick_first_existing_column($table, ['notes', 'note', 'description', 'details', 'payment_notes']) : null; $processorColumn = $table ? fourps_pick_first_existing_column($table, ['processor', 'gateway', 'payment_processor']) : null; $record = null; if ($table && $id > 0 && fourps_column_exists($table, 'id')) { $select = ['`id`']; $fieldsToCheck = [ 'titleColumn' => $titleColumn, 'statusColumn' => $statusColumn, 'amountColumn' => $amountColumn, 'methodColumn' => $methodColumn, 'createdColumn' => $createdColumn, 'updatedColumn' => $updatedColumn, 'customerColumn' => $customerColumn, 'emailColumn' => $emailColumn, 'phoneColumn' => $phoneColumn, 'invoiceRefColumn' => $invoiceRefColumn, 'claimRefColumn' => $claimRefColumn, 'noteColumn' => $noteColumn, 'processorColumn' => $processorColumn, ]; foreach ($fieldsToCheck as $alias => $column) { if ($column) { $select[] = "`{$column}` AS {$alias}"; } else { $select[] = "NULL AS {$alias}"; } } $sql = "SELECT " . implode(', ', $select) . " FROM `{$table}` WHERE `id` = ? LIMIT 1"; $record = fourps_query_one($sql, [$id]); } $searchTitle = $record['titleColumn'] ?? null; if ($searchTitle && is_string($searchTitle)) { $searchTitle = trim($searchTitle); } function fourps_related_rows(array $tables, ?string $searchTitle, int $limit = 5): array { if (!$searchTitle) { return []; } $all = []; foreach ($tables as $tableInfo) { $table = fourps_first_existing_table($tableInfo['tables']); if (!$table) { continue; } $idColumn = fourps_pick_first_existing_column($table, ['id']); $titleColumn = fourps_pick_first_existing_column($table, ['title', 'name', 'reference', 'number', 'plan_number', 'claim_number', 'invoice_number', 'payment_reference', 'transaction_id']); $statusColumn = fourps_pick_first_existing_column($table, ['status', 'plan_status', 'claim_status', 'invoice_status', 'payment_status', 'state']); $dateColumn = fourps_pick_first_existing_column($table, ['updated_at', 'created_at', 'date_created', 'submitted_at', 'invoice_date', 'payment_date', 'received_at']); if (!$idColumn || !$titleColumn) { continue; } $select = [ "`{$idColumn}` AS row_id", "`{$titleColumn}` AS row_title", $statusColumn ? "`{$statusColumn}` AS row_status" : "'' AS row_status", $dateColumn ? "`{$dateColumn}` AS row_date" : "NULL AS row_date", ]; $sql = "SELECT " . implode(', ', $select) . " FROM `{$table}` WHERE `{$titleColumn}` LIKE ? ORDER BY " . ($dateColumn ? "`{$dateColumn}` DESC" : "`{$idColumn}` DESC") . " LIMIT " . (int)$limit; $rows = fourps_query_all($sql, ['%' . $searchTitle . '%']); foreach ($rows as &$row) { $row['module_label'] = $tableInfo['label']; $row['view_file'] = $tableInfo['view']; $row['table_name'] = $table; } unset($row); $all = array_merge($all, $rows); } return $all; } $relatedClaims = []; $relatedPlans = []; $relatedInvoices = []; if ($record && $searchTitle) { $relatedClaims = fourps_related_rows([ ['label' => 'Claims', 'tables' => ['fourps_claims', 'claims'], 'view' => 'claim-view.php'] ], $searchTitle); $relatedPlans = fourps_related_rows([ ['label' => 'Plans', 'tables' => ['fourps_plans', 'plans'], 'view' => 'plan-view.php'] ], $searchTitle); $relatedInvoices = fourps_related_rows([ ['label' => 'Invoices', 'tables' => ['fourps_invoices', 'invoices'], 'view' => 'invoice-view.php'] ], $searchTitle); } $displayTitle = $record['titleColumn'] ?? null; if (!$displayTitle && $id > 0) { $displayTitle = 'Payment #' . $id; } if (!$displayTitle) { $displayTitle = 'Payment Record'; } ?>
= fourps_h($pageSubtitle) ?>
Single-record detail view that checks for supported columns before reading them.
fourps_payments first and then payments.payment-view.php?id=123.Best-effort related matches based on the payment title or reference text.
| Record | Status | Date |
|---|---|---|
|
= fourps_h((string)($row['row_title'] ?: ('Record #' . (int)($row['row_id'] ?? 0)))) ?>
= fourps_h((string)$row['table_name']) ?>
|
= fourps_h((string)($row['row_status'] ?? '—')) ?> | = fourps_fmt_datetime(isset($row['row_date']) ? (string)$row['row_date'] : null) ?> |
Quick paths from this payment record into the utility pages.