<?php
if (!function_exists('cash_tx_linked_source_label')) {
  function cash_tx_linked_source_label(PDO $pdo, int $companyId, int $cashTxId): ?string {
    $checks = [
      ['sql' => "SELECT id FROM employee_advances WHERE company_id=? AND cash_tx_id=? LIMIT 1", 'label' => 'عهدة موظف'],
      ['sql' => "SELECT id FROM employee_advance_settlements WHERE company_id=? AND cash_tx_id=? LIMIT 1", 'label' => 'تسوية عهدة'],
    ];
    foreach ($checks as $check) {
      try {
        $st = $pdo->prepare($check['sql']);
        $st->execute([$companyId, $cashTxId]);
        if ($st->fetchColumn()) {
          return $check['label'];
        }
      } catch (Throwable $e) {
        // ignore missing tables or restricted environments
      }
    }
    return null;
  }

  function cash_tx_is_locked_from_cash_page(PDO $pdo, int $companyId, int $cashTxId): bool {
    return cash_tx_linked_source_label($pdo, $companyId, $cashTxId) !== null;
  }
}
