<?php
/**
 * AUTO MAP REKENING DARI DATABASE
 * Dipakai oleh rekap_bku_proses.php
 * TANPA session
 * TANPA HTML
 */

require_once __DIR__ . '/config/config.php';

$map = [];

/* ===============================
   AMBIL DATA REKENING MAP
=============================== */
$q = $conn->query("
    SELECT kode_rekening, nama_rekening, kelompok
    FROM rekening_map
    ORDER BY kode_rekening
");

if (!$q) {
    return $map;
}

while ($r = $q->fetch_assoc()) {

    $kode = trim($r['kode_rekening']);
    if ($kode === '') continue;

    // NORMALISASI KE NAMA KOLOM DATABASE
    $kelompok = strtolower(trim($r['kelompok']));
    $nama     = strtolower(trim($r['nama_rekening']));

    // ganti karakter aneh → underscore
    $kelompok = preg_replace('/[^a-z0-9]/', '_', $kelompok);
    $nama     = preg_replace('/[^a-z0-9]/', '_', $nama);

    // rapikan underscore ganda
    $kelompok = preg_replace('/_+/', '_', $kelompok);
    $nama     = preg_replace('/_+/', '_', $nama);

    $kolom = $kelompok . '_' . $nama;

    $map[$kode] = $kolom;
}

return $map;
