<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('get_country_list'))
{

 function get_country_list($old_data='') {
 if($old_data==""){
 echo file_get_contents('./application/country_list/country.txt');
 }else{
 $pattern='<option value="'.$old_data.'">';
 $replace='<option value="'.$old_data.'" selected="selected">';
 $country_list=file_get_contents('./application/country_list/country.txt');
 $country_list=str_replace($pattern,$replace,$country_list);
 echo $country_list;

   }

  }	
}


if ( ! function_exists('decimalPlace'))
{

 function decimalPlace($number){
  return number_format((float)$number, 2);
 }

}


if ( ! function_exists('get_fee'))
{

 function get_fee($cost_name) {
  $CI =& get_instance();
  $CI->db->select('fee');
  $CI->db->from('cost');
  $CI->db->where('cost_name',$cost_name);
  $query=$CI->db->get();
  if ($query->num_rows() > 0) {
   return $query->row()->fee;
  }else{
   return 0;
  }
 }
}

if ( ! function_exists('calculate_fee'))
{

 function calculate_fee($amount){
 $CI =& get_instance();
 return $CI->db->query("SELECT fee FROM cost WHERE 
 from_amount<='".$amount."' AND to_amount>='".$amount."'")->row()->fee;
 }

}

if ( ! function_exists('user_type'))
{

 function user_type(){
 $CI =& get_instance();
 return $CI->session->userdata("user_type");
 }

}

if ( ! function_exists('varify_payment'))
{

 function varify_payment($code,$type){
 $CI =& get_instance();
 return $CI->db->query("SELECT * FROM sending_money WHERE 
 sending_code='".$code."' AND status='sending' AND sending_type='".$type."'")->row();
 }

}

if ( ! function_exists('get_agent_count'))
{

 function get_agent_count($status){
 $CI =& get_instance();
 return $CI->db->query("SELECT COUNT(id) as c FROM users 
 WHERE status='".$status."' AND user_type='agent'")->row()->c;
 }

}

if ( ! function_exists('get_agent_list'))
{

 function get_agent_list(){
 $CI =& get_instance();
 return $CI->db->query("SELECT id,username FROM users 
 WHERE user_type='agent'")->result();
 }

}

if ( ! function_exists('get_transfer_count'))
{

 function get_transfer_count(){
 $CI =& get_instance();
 if(user_type()=="admin"){
   return $CI->db->query("SELECT COUNT(id) as c FROM sending_money 
   WHERE status='sending'")->row()->c;
 }else{
   return $CI->db->query("SELECT COUNT(id) as c FROM sending_money 
   WHERE status='sending' AND agent_id='".$CI->session->userdata('id')."'")->row()->c;
 }
 }

}

if ( ! function_exists('get_paid_count'))
{

 function get_paid_count(){
 $CI =& get_instance();
 if(user_type()=="admin"){
   return $CI->db->query("SELECT COUNT(id) as c FROM sending_money 
   WHERE status='paid'")->row()->c;
   }else{
   return $CI->db->query("SELECT COUNT(id) as c FROM sending_money 
   WHERE status='paid' AND agent_id='".$CI->session->userdata('id')."'")->row()->c;
   }
 }

}

if ( ! function_exists('get_cancle_count'))
{
 function get_cancle_count(){
 $CI =& get_instance();
 if(user_type()=="admin"){
   return $CI->db->query("SELECT COUNT(id) as c FROM sending_money 
   WHERE status='cancle'")->row()->c;
   }else{
   return $CI->db->query("SELECT COUNT(id) as c FROM sending_money 
   WHERE status='cancle' AND agent_id='".$CI->session->userdata('id')."'")->row()->c;
   }
 }

}

if ( ! function_exists('get_current_setting'))
{

 function get_current_setting($setting) {
   $CI =&	get_instance();
   $CI->load->database();
   $CI->db->select('value');
   $CI->db->from('settings');
   $CI->db->where('settings',$setting);
   $query=$CI->db->get();
   $result=$query->row();
   return $result->value;
}

}



if ( ! function_exists('convert_number_to_words'))
{

function convert_number_to_words($number) {
    
    $hyphen      = '-';
    $conjunction = ' and ';
    $separator   = ', ';
    $negative    = 'negative ';
    $decimal     = ' point ';
    $dictionary  = array(
        0                   => 'zero',
        1                   => 'un',
        2                   => 'deux',
        3                   => 'trois',
        4                   => 'quatre',
        5                   => 'cinq',
        6                   => 'six',
        7                   => 'sept',
        8                   => 'huit',
        9                   => 'neuf',
        10                  => 'dix',
        11                  => 'onze',
        12                  => 'douze',
        13                  => 'treize',
        14                  => 'quatorze',
        15                  => 'quinze',
        16                  => 'seize',
        17                  => 'dix-sept',
        18                  => 'dix-huit',
        19                  => 'dix-neuf',
        20                  => 'vingt',
        30                  => 'trente',
        40                  => 'quarante',
        50                  => 'cinquante',
        60                  => 'soixante',
        70                  => 'soixante-dix',
        80                  => 'quatre-vingts',
        90                  => 'quatre-vingt-dix',
        100                 => 'cent',
        1000                => 'mille',
        1000000             => 'million',
        1000000000          => 'milliard',
        1000000000000       => 'billion',
        1000000000000000    => 'mille billion',
        1000000000000000000 => 'trillion'
    );
    
    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;
}

}



