<?php
function terbilang($angka=0,$curr='IDR')
{
	$hasil = '';
	if ($curr=='IDR')
		$hasil = terbilang_idr($angka);
		//
	if ($curr<>'IDR')
		$hasil = convert_number_to_words($angka);
	
	
	return $hasil;
	
}

function convert_number_to_words($number=0)
{
    
    $hyphen      = '-';
    $conjunction = ' AND ';
    $separator   = ' ';
    $negative    = 'NEGATIVE ';
    $decimal     = ' ';
    $dictionary  = array(
        0                   => '',
        1                   => 'ONE',
        2                   => 'TWO',
        3                   => 'THREE',
        4                   => 'FOUR',
        5                   => 'FIVE',
        6                   => 'SIX',
        7                   => 'SEVEN',
        8                   => 'EIGHT',
        9                   => 'NINE',
        10                  => 'TEN',
        11                  => 'ELEVEN',
        12                  => 'TWELVE',
        13                  => 'THIRTEEN',
        14                  => 'FOURTEEN',
        15                  => 'FIFTEEN',
        16                  => 'SIXTEEN',
        17                  => 'SEVENTEEN',
        18                  => 'EIGHTEEN',
        19                  => 'NINETEEN',
        20                  => 'TWENTY',
        30                  => 'THIRTY',
        40                  => 'FOURTY',
        50                  => 'FIFTY',
        60                  => 'SIXTY',
        70                  => 'SEVENTY',
        80                  => 'EIGHTY',
        90                  => 'NINETY',
        100                 => 'HUNDRED',
        1000                => 'THOUSAND',
        1000000             => 'MILLION',
        1000000000          => 'BILLION',
        1000000000000       => 'TRILLION',
        1000000000000000    => 'QUADRILLION',
        1000000000000000000 => 'QUINTILLION'
    );
    
    if (!is_numeric($number)) {
        return false;
    }
    
    if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
        // overflow
        trigger_error(
            'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
            E_USER_WARNING
        );
        return false;
    }

    if ($number < 0) {
        return $negative . convert_number_to_words(abs($number));
    }
    
    $string = $fraction = null;
    
    if (strpos($number, '.') !== false) {
        list($number, $fraction) = explode('.', $number);
    }
    
    switch (true) {
        case $number < 21:
            $string = $dictionary[$number];
            break;
        case $number < 100:
            $tens   = ((int) ($number / 10)) * 10;
            $units  = $number % 10;
            $string = $dictionary[$tens];
            if ($units) {
                $string .= $hyphen . $dictionary[$units];
            }
            break;
        case $number < 1000:
            $hundreds  = $number / 100;
            $remainder = $number % 100;
            $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
            if ($remainder) {
                $string .= $conjunction . convert_number_to_words($remainder);
            }
            break;
        default:
            $baseUnit = pow(1000, floor(log($number, 1000)));
            $numBaseUnits = (int) ($number / $baseUnit);
            $remainder = $number % $baseUnit;
            $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
            if ($remainder) {
                $string .= $remainder < 100 ? $conjunction : $separator;
                $string .= convert_number_to_words($remainder);
            }
            break;
    }
    
    if (null !== $fraction && is_numeric($fraction)) {
        $string .= $decimal;
        $words = array();
        foreach (str_split((string) $fraction) as $number) {
            $words[] = $dictionary[$number];
        }
        $string .= implode(' ', $words);
    }
    
    return $string;
}
function terbilang_idr($angka=0)
{
	// pastikan kita hanya berususan dengan tipe data numeric
    $angka = (float)$angka;
     
    // array bilangan 
    // sepuluh dan sebelas merupakan special karena awalan 'se'
    $bilangan = array(
            '',
            'SATU',
            'DUA',
            'TIGA',
            'EMPAT',
            'LIMA',
            'ENAM',
            'TUJUH',
            'DELAPAN',
            'SEMBILAN',
            'SEPULUH',
            'SEBELAS'
    );
     
    // pencocokan dimulai dari satuan angka terkecil
    if ($angka < 12) {
        // mapping angka ke index array $bilangan
        return $bilangan[$angka];
    } else if ($angka < 20) {
        // bilangan 'belasan'
        // misal 18 maka 18 - 10 = 8
        return $bilangan[$angka - 10] . ' BELAS';
    } else if ($angka < 100) {
        // bilangan 'puluhan'
        // misal 27 maka 27 / 10 = 2.7 (integer => 2) 'dua'
        // untuk mendapatkan sisa bagi gunakan modulus
        // 27 mod 10 = 7 'tujuh'
        $hasil_bagi = (int)($angka / 10);
        $hasil_mod = $angka % 10;
        return trim(sprintf('%s PULUH %s', $bilangan[$hasil_bagi], $bilangan[$hasil_mod]));
    } else if ($angka < 200) {
        // bilangan 'seratusan' (itulah indonesia knp tidak satu ratus saja? :))
        // misal 151 maka 151 = 100 = 51 (hasil berupa 'puluhan')
        // daripada menulis ulang rutin kode puluhan maka gunakan
        // saja fungsi rekursif dengan memanggil fungsi terbilang(51)
        return sprintf('SERATUS %s', terbilang($angka - 100));
    } else if ($angka < 1000) {
        // bilangan 'ratusan'
        // misal 467 maka 467 / 100 = 4,67 (integer => 4) 'empat'
        // sisanya 467 mod 100 = 67 (berupa puluhan jadi gunakan rekursif terbilang(67))
        $hasil_bagi = (int)($angka / 100);
        $hasil_mod = $angka % 100;
        return trim(sprintf('%s RATUS %s', $bilangan[$hasil_bagi], terbilang($hasil_mod)));
    } else if ($angka < 2000) {
        // bilangan 'seribuan'
        // misal 1250 maka 1250 - 1000 = 250 (ratusan)
        // gunakan rekursif terbilang(250)
        return trim(sprintf('SERIBU %s', terbilang($angka - 1000)));
    } else if ($angka < 1000000) {
        // bilangan 'ribuan' (sampai ratusan ribu
        $hasil_bagi = (int)($angka / 1000); // karena hasilnya bisa ratusan jadi langsung digunakan rekursif
        $hasil_mod = $angka % 1000;
        return sprintf('%s RIBU %s', terbilang($hasil_bagi), terbilang($hasil_mod));
    } else if ($angka < 1000000000) {
        // bilangan 'jutaan' (sampai ratusan juta)
        // 'satu puluh' => SALAH
        // 'satu ratus' => SALAH
        // 'satu juta' => BENAR 
        // @#$%^ WT*
         
        // hasil bagi bisa satuan, belasan, ratusan jadi langsung kita gunakan rekursif
        $hasil_bagi = (int)($angka / 1000000);
        $hasil_mod = $angka % 1000000;
        return trim(sprintf('%s JUTA %s', terbilang($hasil_bagi), terbilang($hasil_mod)));
    } else if ($angka < 1000000000000) {
        // bilangan 'milyaran'
        $hasil_bagi = (int)($angka / 1000000000);
        // karena batas maksimum integer untuk 32bit sistem adalah 2147483647
        // maka kita gunakan fmod agar dapat menghandle angka yang lebih besar
        $hasil_mod = fmod($angka, 1000000000);
        return trim(sprintf('%s MILYAR %s', terbilang($hasil_bagi), terbilang($hasil_mod)));
    } else if ($angka < 1000000000000000) {
        // bilangan 'triliun'
        $hasil_bagi = $angka / 1000000000000;
        $hasil_mod = fmod($angka, 1000000000000);
        return trim(sprintf('%s TRILIUN %s', terbilang($hasil_bagi), terbilang($hasil_mod)));
    } else {
        return 'Wow...';
    }
}

function getAssets(){
    $_this = & get_Instance();
	$conf = base_url('assets').'/';
	if($conf <> ''){
		return $conf;
	}else{
		return false;
	}
}

function getBootstrap(){
    $_this = & get_Instance();
	$conf = base_url('assets/bootstrap-3.3.4-dist').'/';
	if($conf <> ''){
		return $conf;
	}else{
		return false;
	}
}

function getJquery(){
    $_this = & get_Instance();
	$conf = base_url('assets/jquery').'/';
	if($conf <> ''){
		return $conf;
	}else{
		return false;
	}
}

function getJeasyui(){
    $_this = & get_Instance();
	$conf = base_url('assets/jquery/jquery-easyui-1.4.2').'/';
	if($conf <> ''){
		return $conf;
	}else{
		return false;
	}
}

function getCss(){
    $_this = & get_Instance();
	$conf = base_url('assets/kikinsoft/css').'/';
	if($conf <> ''){
		return $conf;
	}else{
		return false;
	}
}

function getJS(){
    $_this = & get_Instance();
	$conf = base_url('assets/kikinsoft/js').'/';
	if($conf <> ''){
		return $conf;
	}else{
		return false;
	}
}

function getImg(){
    $_this = & get_Instance();
	$conf = base_url('assets/kikinsoft/img').'/';
	if($conf <> ''){
		return $conf;
	}else{
		return false;
	}
}

function getThemes(){
    $_this = & get_Instance();
	$conf = base_url('assets/kikinsoft/themes').'/';
	if($conf <> ''){
		return $conf;
	}else{
		return false;
	}
}

function get_myconf($var){
    $_this = & get_Instance();
	$conf = $_this->config->item($var);
	if($conf <> ''){
		return $conf;
	}else{
		return false;
	}
}

if ( ! function_exists('match_key'))
{
	function match_key($data,$key)
	{
		$data_upper = strtoupper($data);
		$key_upper = strtoupper($key);
		$pos_start = strrpos($data_upper,$key_upper);
		$result = $data;
		if ($pos_start OR $key_upper == substr($data_upper,0,strlen($key_upper)) )
		{
			$pos_end = $pos_start + strlen($key_upper);
			$result = substr_replace($data,'<b>',$pos_start,0);
			$result = substr_replace($result,'</b>',$pos_end+3,0);
		}		
		return $result;
	}
}

if ( ! function_exists('check_priv'))
{
	function check_priv($module_code=null, $aksi=null)
	{
		$_this = & get_Instance();
		//$_this->load->model('select * from ms_user_module');
		//$where['module_code'] = $module_code;
		//$where['id_mst_user'] = $_this->session->userdata('id_mst_user');
		$query = "SELECT * 
				FROM ms_user_module a
				LEFT JOIN ms_module b ON b.id_ms_module = a.fid_ms_module
				WHERE fid_mst_user =".$_this->session->userdata('level').
					" AND fid_ms_module = '$module_code' AND $aksi = 1 ";
		//$result = $ms_user_module->count();
		$result = $_this->db->query($query);
		return $result->num_rows();
	}
}

function auditrial($user_id, $table, $row_id, $action)
{
	$_this = & get_Instance();
	
	$data = array (
		'date' 		=> $datenow,
		'user_id'	=> $user_id,
		'table'		=> $table,
		'row_id'	=> $row_id,
		'action'	=> $action,
		
	);
	
	$insert = $_this->db->insert('audit_trial', $data); 
	return $insert;
}

function dd($str){
    echo "<pre>";
    print_r($str);
    echo "</pre>";
    die;
}

function dmys2ymd($date){
	if ($date!=""){
		list($day, $month, $year) = explode('/', $date);		
		return $year."-".str_pad($month, 2, "0", STR_PAD_LEFT)."-".str_pad($day, 2, "0", STR_PAD_LEFT);
	} else {
		return "0000-00-00";
	}
}

function dmy_st2dmy_sl($date){
	return str_replace('-','/',$date);
}

function dmy_sl2dmy_st($date){
	return str_replace('-','/',$date);
}

function ymd2dmy($date){
	list($year, $month, $day) = explode('-', $date);	
	return $day . "/" . $month . "/" . $year;
}

function ymd2dmy_st($date){
	list($year, $month, $day) = explode('-', $date);	
	return $day . "-" . $month . "-" . $year;
}

function ymd2dmy3($date){
	list($year, $month, $day) = explode('-', $date);	
	return $day . " " . GetNameMonth($month) . " " . $year;
}