<?php
function e($s){return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8');}
function redirect($u){header("Location: $u"); exit;}
function money($n){return CURRENCY.' '.number_format((float)$n, 2);}
function flash_set($k,$m){$_SESSION['flash'][$k]=$m;}
function flash_get($k){$m=$_SESSION['flash'][$k]??null; unset($_SESSION['flash'][$k]); return $m;}
function setting($pdo,$key,$default=''){
    static $c=null;
    if($c===null){$c=[]; foreach($pdo->query("SELECT `key`,`value` FROM settings") as $r)$c[$r['key']]=$r['value'];}
    return $c[$key] ?? $default;
}
function log_txn($pdo,$user_id,$type,$amount,$ref='',$note=''){
    // ref_id এখন VARCHAR — খালি হলে NULL হিসেবে সেভ হবে
    $ref = ($ref === '' || $ref === null) ? null : (string)$ref;
    $pdo->prepare("INSERT INTO transactions(user_id,type,amount,ref_id,note,created_at) VALUES(?,?,?,?,?,NOW())")
        ->execute([(int)$user_id,$type,$amount,$ref,$note]);
}
