<?php

class Konversi{
    private $bil;
    private $data_titik;
    private $jChar;

	function cAngka($angka){
			switch ($angka)
				{
					case 0:
					$this->bil = "";				
					break;
					case 1:
					$this->bil = "Satu";				
					break;
					case 2:
					$this->bil = "Dua ";
					break;
					case 3:
					$this->bil = "Tiga ";
					break;
					case 4:
					$this->bil = "Empat ";
					break;
					case 5:
					$this->bil = "Lima ";
					break;
					case 6:
					$this->bil = "Enam ";
					break;
					case 7:
					$this->bil = "Tujuh ";
					break;
					case 8:
					$this->bil = "Delapan ";
					break;
					case 9:
					$this->bil = "Sembilan ";
					break;
				}
				return $this->bil;	
		}
		function blokAngka($blok){
			if(strlen($blok) == 3)
			{	
						$a1 = substr($blok, 0, 1);
						$a2 = substr($blok, -2, 1);
						$a3 = substr($blok, -1);
						//untuk format 500
						if ($a1 >= 1 && $a2 == 0 && $a3 == 0)
						{
							$a1 = $this->cAngka($a1);	
							$a2 = $this->cAngka($a2);
							if ($a1 == "Satu")
							{
								$a1 = "Se";
							}
							if($a2 == "Satu")
							{
									$a2 = "se";
							}
							$nmBlok1 = $a1. "ratus ". $a2;
						}
						//untuk format 510
						elseif ( $a1 >= 1 && $a2 >= 1 && $a3 == 0)
						{
							$a1 = $this->cAngka($a1);
							$a2 = $this->cAngka($a2);
							if ($a1 == "Satu")
							{
								$a1 = "Se";
							}
							if ($a2 == "Satu")
							{
								$a2 = "Se";
							}
							$nmBlok1 = $a1. "ratus ".$a2. "puluh ";
						}
						//untuk format 501
						elseif ($a1 >=1 && $a2 == 0 && $a3 >= 1 )
						{
							$a1 = $this->cAngka($a1);
							$a3 = $this->cAngka($a3);
							if ($a1 == "Satu")
							{
								$a1 = "Se";
							}
							$nmBlok1 = $a1. "ratus ". $a3;
						}
						//untuk format 050
						elseif ($a1 == 0 && $a2 >= 1 && $a3 == 0 )
						{
							$a1 = $this->cAngka($a1);
							$a2 = $this->cAngka($a2);
							$a3 = $this->cAngka($a3);
							$nmBlok1 = $a2. " puluh ";
						}
						// untuk format 051 ke atas
						elseif ($a1 == 0 && $a2 >= 1 && $a3 >= 1 )
						{
							$a1 = $this->cAngka($a1);
							$a2 = $this->cAngka($a2);
							$a3 = $this->cAngka($a3);
							$nmBlok1 = $a2. " puluh ". $a3;
						}
						//untuk format 005
						elseif ($a1 == 0 && $a2 == 0 && $a3 >= 1 )
						{
							$a3 = $this->cAngka($a3);
							$nmBlok1 = $a3;
						}
						//untu format 512 
						elseif ($a2 == 1)
						{
							$a1 = $this->cAngka($a1);	
							$a2 = $this->cAngka($a3);
							if ($a1 == "Satu")
							{
								$a1 = "Se";
							}
							if ($a2 == "Satu")
							{
								$a2 = "Se";
							}
							$nmBlok1 = $a1. "ratus ". $a2 ."belas ";
						}
						//untuk format 000
						elseif ($blok == 000)
						{
							$nmBlok1 = "";
						}
						else
						{
							$a1 = $this->cAngka($a1);	
							$a2 = $this->cAngka($a2);
							$a3 = $this->cAngka($a3);
							if ($a1 == "Satu")
							{
								$a1 = "Se";
							}
							if ($a2 == "Satu")
							{
								$a2 = "Se";
							}
									if ($a1 == "Se")
									{
										$nmBlok1 = $a1. "ratus ". $a2 ." puluh ".$a3;
									}
									else
									{
									$nmBlok1 = $a1. " ratus ". $a2 ." puluh ".$a3;
									}
						}
			}
			elseif (strlen($blok) == 2)
			{	
					$a1 = substr($blok, 0, 1);
					$a2 = substr($blok, -1, 1);
					if($a1 == 1 && $a2 != 0)
					{
						$a2 = $this->cAngka($a2);
						if ($a2 == "Satu")
						{
							$a2 = "Se";
						}
							if ($a2 == "Se")
							{
								$nmBlok1 = $a2."belas ";
							}
							else
							{
								$nmBlok1 = $a2." belas ";
							}
					}
					else
					{
						$a1 = $this->cAngka($a1);
						if ($a1 == "Satu")
						{
						$a1 = "Se";
						}
						$a2 = $this->cAngka($a2);
						$nmBlok1 = $a1."puluh " .$a2;
					}
			}
			else
			{
				$a1 = substr($blok, 0, 1);
				$a1 = $this->cAngka($a1);
				$nmBlok1 = $a1;
				
			}
			return $nmBlok1;
		}
		function nominalAngka($data){
		$jAngka = strlen($data);
				if ($jAngka == 1)
				{
					$terbilang = "";
				}
				elseif ($jAngka == 2)
				{
					$terbilang = "";
				}
				elseif ($jAngka == 3)
				{
					$terbilang = "";
				}
				elseif ($jAngka == 4)
				{
					$terbilang = "ribu";
				}
				elseif ($jAngka == 5)
				{	
					$terbilang = "ribu";
				}
				elseif ($jAngka == 6)
				{	
					$terbilang = "ribu";
				}
				elseif ($jAngka == 7)
				{	
					$terbilang = "juta";
				}
				elseif ($jAngka == 8)
				{	
					$terbilang = "juta";
				}
				elseif ($jAngka == 9)
				{	
					$terbilang = "juta";
				}
				elseif ($jAngka >= 10)
				{	
					$terbilang = "milyar";
				}
				return $terbilang;	
			}
			function conversiAngka2($data){
				$titik = number_format($data, 0,'','.');
				$exTitik = explode(".",$titik);
				$jTitik = count($exTitik);

				$blok1 = '';
				$blok2 = '';
				$blok3 = '';
				$blok4 = '';

                if(!empty($exTitik[0])){
                    $blok1 = $exTitik[0];
                }
                if(!empty($exTitik[1])){
                    $blok2 = $exTitik[1];
                }
                if(!empty($exTitik[2])){
                    $blok3 = $exTitik[2];
                }
                if(!empty($exTitik[3])){
                    $blok4 = $exTitik[3];
                }

                    $nmBlok1 = $this->blokAngka($blok1);
					$nmBlok2 = $this->blokAngka($blok2);
					$nmBlok3 = $this->blokAngka($blok3);
					$nmBlok4 = $this->blokAngka($blok4);
					$nmNominal1 = $this->nominalAngka($data);
					$rp = "Rupiah";
					if ($blok2 == 000 && $blok3 != 000)
					{
						$nm1 = "";
					}
					elseif ($blok2 == 000 && $blok3 == 000)
					{
						$nm1 = "";
					}
					elseif($nmNominal1 == "juta")
					{
						$nm1 = "ribu";
					}
					elseif($nmNominal1 == "milyard")
					{
						$nm1 = "juta";
						$nm2 = "ribu";
					}
					else
					{
						$nm1 = "";
						$nm2 = "";
						$nm3 = "";
					}
						if ($nmBlok1 == "Se")
						{
                            $hasil = '';
                            if(!empty($nmBlok1)){
                                $hasil.= $nmBlok1;
                            }
                            if(!empty($nmNominal1)){
                                $hasil.= $nmNominal1.' ';
                            }
                            if(!empty($nmBlok2)){
                                $hasil.= $nmBlok2.' ';
                            }
                            if(!empty($nm1)){
                                $hasil.= $nm1.' ';
                            }
                            if(!empty($nmBlok3)){
                                $hasil.= $nmBlok3.' ';
                            }
                            if(!empty($nmBlok4)){
                                $hasil.= $nmBlok4.' ';
                            }
                            if(!empty($nm2)){
                                $hasil.= $nm2.' ';
                            }
                            if(!empty($rp)){
                                $hasil.= $rp;
                            }
						}
						else
						{
                            $hasil = '';
                            if(!empty($nmBlok1)){
                                $hasil.= $nmBlok1.' ';
                            }
                            if(!empty($nmNominal1)){
                                $hasil.= $nmNominal1.' ';
                            }
                            if(!empty($nmBlok2)){
                                $hasil.= $nmBlok2.' ';
                            }
                            if(!empty($nm1)){
                                $hasil.= $nm1.' ';
                            }
                            if(!empty($nmBlok3)){
                                $hasil.= $nmBlok3.' ';
                            }
                            if(!empty($nmBlok4)){
                                $hasil.= $nmBlok4.' ';
                            }
                            if(!empty($nm2)){
                                $hasil.= $nm2.' ';
                            }
                            if(!empty($rp)){
                                $hasil.= $rp;
                            }
						}
					if ($data == 1000)
					{
						$hasil ="Seribu $rp";
					}
					elseif ($data == 1000000)
					{
						$hasil ="Satu Juta $rp";
					}
					elseif ($data == 10000000)
					{
						$hasil ="Sepuluh Juta $rp";
					}
					return strtoupper($hasil);
			}
			
			
			
			
			
			
	//-------------------------Beda Jenis------------------------		
			
			
			
			
	function konversi($x = ''){

		$x = abs($x);
		$angka = array ("","satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
		$temp = "";

		if($x < 12){
		$temp = " ".$angka[$x];
		}else if($x<20){
		$temp = $this->konversi($x - 10)." belas";
		}else if ($x<100){
		$temp = $this->konversi($x/10)." puluh". $this->konversi($x%10);
		}else if($x<200){
		$temp = " seratus".$this->konversi($x-100);
		}else if($x<1000){
		$temp = $this->konversi($x/100)." ratus".$this->konversi($x%100);
		}else if($x<2000){
		$temp = " seribu".$this->konversi($x-1000);
		}else if($x<1000000){
		$temp = $this->konversi($x/1000)." ribu".$this->konversi($x%1000);
		}else if($x<1000000000){
		$temp = $this->konversi($x/1000000)." juta".$this->konversi($x%1000000);
		}else if($x<1000000000000){
		$temp = $this->konversi($x/1000000000)." milyar".$this->konversi($x%1000000000);
		}

		return $temp;
	}


	function tkoma($x){
		$str = stristr($x,",");
		$ex = explode(',',$x);

		if(($ex[1]/10) >= 1){
		$a = abs($ex[1]);
		}
		$string = array("nol", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan",   "sembilan","sepuluh", "sebelas");
		$temp = "";

		$a2 = $ex[1]/10;
		$pjg = strlen($str);
		$i =1;


		if($a>=1 && $a< 12){
		$temp .= " ".$string[$a];
		}else if($a>12 && $a<20){
		$temp .= kata($a - 10)." belas";
		}else if ($a>20 && $a<100){
		$temp .= kata($a/10)." puluh". kata($a%10);
		}else{
		if($a2<1){

		while ($i<$pjg){
		$char = substr($str,$i,1);
		$i++;
		$temp .= " ".$string[$char];
		}
		}
		}

		return $temp;

	}

	function conversiAngka($x){
		if($x<0){
			$hasil = "minus ".trim(konversi(x));
		}else{
			$poin = trim($this->tkoma($x));
			$hasil = trim($this->konversi($x));
		}


		if($poin){
			$hasil = $hasil." koma ".$poin;
		}else{
			$hasil = $hasil;
		}
		
		$ex = explode(' ', $hasil);
		$res = '';
		foreach($ex as $row){
			if(!empty($res)){
				$res.= ' '.strtolower($row);
			}else{
				$res.= ucfirst(strtolower($row));
			}
		}
		
		return trim($res).' Rupiah';
	} 		

	public function conversi3($angka) 
	{
		$angka = (float)$angka;
		 
		$bilangan = array(
				'',
				'satu',
				'dua',
				'tiga',
				'empat',
				'lima',
				'enam',
				'tujuh',
				'delapan',
				'sembilan',
				'sepuluh',
				'sebelas'
		);
		 
		if ($angka < 12) {
			return $bilangan[$angka];
		} else if ($angka < 20) {
			return $bilangan[$angka - 10] . ' belas';
		} else if ($angka < 100) {
			$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) {
			return sprintf('seratus %s', $this->conversi3($angka - 100));
		} else if ($angka < 1000) {
			$hasil_bagi = (int)($angka / 100);
			$hasil_mod = $angka % 100;
			return trim(sprintf('%s ratus %s', $bilangan[$hasil_bagi], $this->conversi3($hasil_mod)));
		} else if ($angka < 2000) {
			return trim(sprintf('seribu %s', $this->conversi3($angka - 1000)));
		} else if ($angka < 1000000) {
			$hasil_bagi = (int)($angka / 1000); // karena hasilnya bisa ratusan jadi langsung digunakan rekursif
			$hasil_mod = $angka % 1000;
			return sprintf('%s ribu %s', $this->conversi3($hasil_bagi), $this->conversi3($hasil_mod));
		} else if ($angka < 1000000000) {
			$hasil_bagi = (int)($angka / 1000000);
			$hasil_mod = $angka % 1000000;
			return trim(sprintf('%s juta %s', $this->conversi3($hasil_bagi), $this->conversi3($hasil_mod)));
		} else if ($angka < 1000000000000) {
			$hasil_bagi = (int)($angka / 1000000000);
			$hasil_mod = fmod($angka, 1000000000);
			return trim(sprintf('%s milyar %s', $this->conversi3($hasil_bagi), $this->conversi3($hasil_mod)));
		} else if ($angka < 1000000000000000) {
			$hasil_bagi = $angka / 1000000000000;
			$hasil_mod = fmod($angka, 1000000000000);
			return trim(sprintf('%s triliun %s', $this->conversi3($hasil_bagi), $this->conversi3($hasil_mod)));
		} else {
			return 'Wow...';
		}
	}
}
?>