<?php
	
	if (!function_exists('assets'))
	{
		function assets($path = '')
		{
			return base_url()."public/assets/".$path;
		}
	}

	if (!function_exists('uploads'))
	{
		function uploads($path = '')
		{
			return base_url()."public/uploads/".$path;
		}
	}

	if (!function_exists('sessionExist'))
	{
		function sessionExist()
		{
			$CI =& get_instance();
			return ($CI->session->userdata('user_id')) ? true : false;
    	}
	}

	if (!function_exists('encrypt_decrypt')) 
	{
		function encrypt_decrypt($action, $data, $secret_key) 
		{
		    $output         = false;
		    $encrypt_method = "AES-256-CBC";
		    $secret_iv      = $secret_key;
		    $key            = hash('sha256', $secret_key);
		    $iv             = substr(hash('sha256', $secret_iv), 0, 16);

		    if ($action == 'encrypt') 
		    {
		        $output = openssl_encrypt($data, $encrypt_method, $key, 0, $iv);
		        $output = base64_encode($output);
		    } 
		    else if ($action == 'decrypt') 
		    {
		        $output = openssl_decrypt(base64_decode($data), $encrypt_method, $key, 0, $iv);
		    }

		    return $output;
		}
	}

// 	if (!function_exists('number_to_word'))
// 	{
// 		function number_to_word( $number = '' )
// 		{
// 			//$number = 190908100.25;
// 		   $no = round($number);
// 		   $point = round($number - $no, 2) * 100;
// 		   $hundred = null;
// 		   $digits_1 = strlen($no);
// 		   $i = 0;
// 		   $str = array();
// 		   $words = 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' => 'Forty', '50' => 'Fifty',
// 			'60' => 'Sixty', '70' => 'Seventy',
// 			'80' => 'Eighty', '90' => 'Ninety');
// 		   $digits = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');
// 		   while ($i < $digits_1) {
// 			 $divider = ($i == 2) ? 10 : 100;
// 			 $number = floor($no % $divider);
// 			 $no = floor($no / $divider);
// 			 $i += ($divider == 10) ? 1 : 2;
// 			 if ($number) {
// 				$plural = (($counter = count($str)) && $number > 9) ? 's' : null;
// 				$hundred = ($counter == 1 && $str[0]) ? 'And ' : null;
// 				$str [] = ($number < 21) ? $words[$number] .
// 					" " . $digits[$counter] . $plural . " " . $hundred
// 					:
// 					$words[floor($number / 10) * 10]
// 					. " " . $words[$number % 10] . " "
// 					. $digits[$counter] . $plural . " " . $hundred;
// 			 } else $str[] = null;
// 		  }
// 		  $str = array_reverse($str);
// 		  $result = implode('', $str);
// 		  $result = rtrim($result);
// 		  $points = ($point) ?
// 			"." . $words[$point / 10] . " " .
// 				  $words[$point = $point % 10] : '';
// 		  return $result . " Rupees" . $points . " "."Only";
// 		}
// 	}
	
	if( !function_exists('displaywords'))
	{
	    function displaywords($number = ''){
            $words = 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' => 'forty', '50' => 'fifty',
            '60' => 'sixty', '70' => 'seventy',
            '80' => 'eighty', '90' => 'ninety');
            $digits = array('', '', '', 'hundred', 'thousand', 'lakh', 'crore');
           
            $number = explode(".", $number);
            $result = array("","");
            $j =0;
            foreach($number as $val){
                // loop each part of number, right and left of dot
                for($i=0;$i<strlen($val);$i++){
                    // look at each part of the number separately  [1] [5] [4] [2]  and  [5] [8]
                    
                    $numberpart = str_pad($val[$i], strlen($val)-$i, "0", STR_PAD_RIGHT); // make 1 => 1000, 5 => 500, 4 => 40 etc.
                    if($numberpart <= 20){
                        
                        // print_r($val);
                        // exit;
                        $numberpart = 1*substr($val, $i, 2);
                        // print_r($numberpart);exit;
                        $i++;
                        $result[$j] .= $words[$numberpart] ." ";
                    }else{
                        // echo $numberpart . "<br>\n"; //debug
                        // exit;
                        
                        if($numberpart > 90){  // more than 90 and it needs a $digit.
                            // $result[$j] .= $words[$val[$i]] . " " . $digits[strlen($numberpart)-1] . " ";
                            
                            $result[$j] .= $words[$val[$i]] . " " . $digits[strlen($numberpart)-2] . " "; 
                        }
                        else if($numberpart != 0){ // don't print zero
                            $result[$j] .= $words[str_pad($val[$i], strlen($val)-$i, "0", STR_PAD_RIGHT)] ." ";
                        }
                    }
                }
                $j++;
            }
            if(trim($result[0]) != "") $a = $result[0] . "Rupees ";
            if($result[1] != "") $b = $result[1] . "Paise";
            $c = $a.' '.$b.' Only';
            return $c;
        }
	}
	
	function number_to_word( $number = '')
		{
			
			$decimal = round($number - ($no = floor($number)), 2) * 100;
		    $hundred = null;
		    $digits_length = strlen($no);
		    $i = 0;
		    $str = array();
		    $words = 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' => 'Forty', '50' => 'Fifty',
			'60' => 'Sixty', '70' => 'Seventy',
			'80' => 'Eighty', '90' => 'Ninety');
			$digits = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');
		    while( $i < $digits_length ) {
		        $divider = ($i == 2) ? 10 : 100;
		        $number = floor($no % $divider);
		        $no = floor($no / $divider);
		        $i += $divider == 10 ? 1 : 2;
		        if ($number) {
		            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
		            $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
		            $str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
		        } else $str[] = null;
		    }
		    $Rupees = implode('', array_reverse($str));
		    $paise = ($decimal) ? "." . ($words[$decimal / 10] . " " . $words[$decimal % 10]) . ' Paise' : '';
		    return 'Rupees '.($Rupees ? $Rupees  : 'Zero') . $paise;
			
		}

		function number_to_word_po( $number = '')
		{
			
			$decimal = round($number - ($no = floor($number)), 2) * 100;
		    $hundred = null;
		    $digits_length = strlen($no);
		    $i = 0;
		    $str = array();
		    $words = 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' => 'Forty', '50' => 'Fifty',
			'60' => 'Sixty', '70' => 'Seventy',
			'80' => 'Eighty', '90' => 'Ninety');
			$digits = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');
		    while( $i < $digits_length ) {
		        $divider = ($i == 2) ? 10 : 100;
		        $number = floor($no % $divider);
		        $no = floor($no / $divider);
		        $i += $divider == 10 ? 1 : 2;
		        if ($number) {
		            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
		            $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
		            $str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
		        } else $str[] = null;
		    }
		    $Rupees = implode('', array_reverse($str));
		    $paise = ($decimal) ? "." . ($words[$decimal / 10] . " " . $words[$decimal % 10]) . ' Paise' : '';
		    return ($Rupees ? $Rupees  : 'Zero') . $paise;
			
		}

		function number_to_word_wo_ruppe( $number = '')
		{
			
			$decimal = round($number - ($no = floor($number)), 2) * 100;
		    $hundred = null;
		    $digits_length = strlen($no);
		    $i = 0;
		    $str = array();
		    $words = 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' => 'Forty', '50' => 'Fifty',
			'60' => 'Sixty', '70' => 'Seventy',
			'80' => 'Eighty', '90' => 'Ninety');
			$digits = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');
		    while( $i < $digits_length ) {
		        $divider = ($i == 2) ? 10 : 100;
		        $number = floor($no % $divider);
		        $no = floor($no / $divider);
		        $i += $divider == 10 ? 1 : 2;
		        if ($number) {
		            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
		            $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
		            $str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
		        } else $str[] = null;
		    }
		    $Rupees = implode('', array_reverse($str));
		    $paise = ($decimal) ? "." . ($words[$decimal / 10] . " " . $words[$decimal % 10]) . '' : '';
		    return ($Rupees ? $Rupees . '' : '') . $paise;
			
		}

?>