<?php
session_start();

function encrypt($str)
{
    $kunci = '979a218e0632df2935317f98d47956c7';
    for ($i = 0; $i < strlen($str); $i++) {
        $karakter = substr($str, $i, 1);
        $kuncikarakter = substr($kunci, ($i % strlen($kunci)) - 1, 1);
        $karakter = chr(ord($karakter) + ord($kuncikarakter));
        $hasil .= $karakter;
    }
    return urlencode(base64_encode($hasil));
}

function decrypt($str)
{
    $str = base64_decode(urldecode($str));
    $hasil = '';
    $kunci = '979a218e0632df2935317f98d47956c7';
    for ($i = 0; $i < strlen($str); $i++) {
        $karakter = substr($str, $i, 1);
        $kuncikarakter = substr($kunci, ($i % strlen($kunci)) - 1, 1);
        $karakter = chr(ord($karakter) - ord($kuncikarakter));
        $hasil .= $karakter;
    }
    return $hasil;
}

function login_validate()
{
    $timeout = 30;
    $_SESSION['expires_by'] = time() + $timeout;
}


// mencari jumlah dari total anggaran tiap bulan nya
function total_jml_in_per_id_ang($xidanggaran){
    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT 
                SUM(pda.jml) AS total_jml
            FROM 
                perencanaan_detail_anggaran pda
            JOIN 
                jenis_transaksi jt ON pda.id_jenis = jt.id_jenis
            WHERE 
                pda.id_anggaran = '$xidanggaran'
                AND jt.jenis = 0 ");
    $isi = mysqli_fetch_array($query);

    return $isi['total_jml'];
}

// mencari jumlah dari total anggaran tiap bulan nya
function total_jml_out_per_id_ang($xidanggaran){
    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT 
                SUM(pda.jml) AS total_jml
            FROM 
                perencanaan_detail_anggaran pda
            JOIN 
                jenis_transaksi jt ON pda.id_jenis = jt.id_jenis
            WHERE 
                pda.id_anggaran = '$xidanggaran'
                AND jt.jenis = 2 ");
    $isi = mysqli_fetch_array($query);

    return $isi['total_jml'];
}

function login_check()
{
    $exp_time = $_SESSION['expires_by'];
    if (time() < $exp_time) {
        login_validate();
        return true;
    } else {
        unset($_SESSION['expires_by']);
        return false;
    }
}
function namajenis($id)
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT nama_jenis from jenis_transaksi WHERE id_jenis='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['nama_jenis'];
}

function totalbawauang()
{

    global $conn; // panggil koneksi
    $query_saldo_x = mysqli_query($conn, "SELECT SUM(jumlah_bawa) AS total_transaksi FROM transaksi_bawauang WHERE valid != '1'");
    $row_saldo_x = mysqli_fetch_array($query_saldo_x);

    return $row_saldo_x['total_transaksi'];
}

function namakelas($id)
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT jenjang_kelas from kelas_umum WHERE id_kelas='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['jenjang_kelas'];
}

function namapekerja($id)
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT nama_pekerja from pekerja WHERE id_pekerja='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['nama_pekerja'];
}

function statusaktif($id)
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT nama from status_aktif WHERE id='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['nama'];
}

function namataghut($id)
//nama tagihan/hutang
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT * from tagihan_hutang WHERE id_tag_hut='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['nama_tag_hut'];
}

function namakategori($id)
//nama tagihan/hutang
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT * from kategori_kas WHERE id_kategori='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['nama_kategori'];
}

function namajurusan($id)
//nama tagihan/hutang
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT * from prog_kelas WHERE id='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['nama_prog_kelas'];
}

function jeniskelamin($id)
//nama jenis kelamin
{

   if ($id=="l"){
    $isi="Laki-Laki";
   }elseif($id=="p"){
    $isi="Perempuan";
   }else{
    $isi="error";
   }

    return $isi;
}

function baruataupindahan($id)
//nama baruataupindahan
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT * from baruataupindahan WHERE id='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['baruataupindahan'];
}

function statuus($id)
//nama statuus
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT * from jenis_status WHERE id_status='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['nama_status'];
}


function statusortu($id)
//nama data_pendukung
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT * from data_pendukung WHERE id='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['nama_data'];
}


function jeniskas_in_out($id)
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT * from jenis_transaksi WHERE id_jenis='$id'");
    $row = mysqli_fetch_array($query);

    if($row['jenis']=="0"){ $isi=" (Kas Masuk)";} elseif($row['jenis']=="2"){ $isi=" (Kas Keluar)";}

    return $isi;
}

function namadivisi($id)
{

    global $conn; // panggil koneksi

    $query = mysqli_query($conn, "SELECT nama_divisi from jenis_divisi WHERE id_divisi='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['nama_divisi'];
}

function saldo($id)
{

    global $conn; // panggil koneksi

    $query_saldo = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi_ewallet WHERE rekening='$id'");
    $row_saldo = mysqli_fetch_array($query_saldo);
    $saldo_keseluruhan = $row_saldo['jumlah_debit'] - $row_saldo['jumlah_kredit'];
    return floatval($saldo_keseluruhan);
}

function totalpinjam($id)
{

    global $conn; // panggil koneksi

    $query_saldo = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi_pinjaman WHERE rekening='$id'");
    $row_saldo = mysqli_fetch_array($query_saldo);
    $saldo_keseluruhan = $row_saldo['jumlah_debit'] - $row_saldo['jumlah_kredit'];
    return floatval($saldo_keseluruhan);
}

function total_taghut_sdhdibayar($id)
//total sdh hutang tagihan yang sudah dibayar
{

    global $conn; // panggil koneksi

    $query_saldo = mysqli_query($conn, "SELECT SUM(kredit) as jumlah_kredit FROM transaksi WHERE id_tag_hut='$id'");
    $row_saldo = mysqli_fetch_array($query_saldo);
    $saldo_keseluruhan = $row_saldo['jumlah_kredit'];
    return floatval($saldo_keseluruhan);
}
function pinjaman($id)
{

    global $conn; // panggil koneksi

    $query_saldo = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi_pinjaman WHERE rekening='$id'");
    $row_saldo = mysqli_fetch_array($query_saldo);
    $saldo_keseluruhan = $row_saldo['jumlah_debit'] - $row_saldo['jumlah_kredit'];
    return floatval($saldo_keseluruhan);
}

function nama($id)
{

    global $conn; // panggil koneksi





    $cekjenis = mysqli_query($conn, "SELECT * FROM santri WHERE id_santri='$id'");
    if (mysqli_num_rows($cekjenis) == 0) {
        $query_saldo = mysqli_query($conn, "SELECT nama from nasabah_umum WHERE rekening='$id'");
        $row_saldo = mysqli_fetch_array($query_saldo);
    } else {
        $query_saldo = mysqli_query($conn, "SELECT nama from santri WHERE id_santri='$id'");
        $row_saldo = mysqli_fetch_array($query_saldo);
    }
    return $row_saldo['nama'];
}


function Tgl($str)
{
    $bln = explode(" ", "Januari Februari Maret April Mei Juni Juli Agustus September Oktober November Desember");
    $tgl = explode("-", $str);

    if ($tgl[1] < 10) $tgl[1] = substr($tgl[1], 1, 1);
    //else $tgl[1] = $tgl[1];
    return $tgl[2] . " " . $bln[($tgl[1] - 1)] . " " . $tgl[0];
    //return $tgl[1];
}

function nama_bulan($bulan)
{
    $bln = explode(" ", "Januari Februari Maret April Mei Juni Juli Agustus September Oktober November Desember");
    return $bln[intval($bulan) - 1];
}


function namajeniskas($id)
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT nama from jenis_kas WHERE id='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['nama'];
}

function namapegawai($id)
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT nama from pegawai WHERE id_pegawai='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['nama'];
}

function namajenisdonatur($id)
{

    global $conn; // panggil koneksi


    $query = mysqli_query($conn, "SELECT nama_jenis from jenis_donatur WHERE id='$id'");
    $isi = mysqli_fetch_array($query);

    return $isi['nama_jenis'];
}
