<?
class clientModel extends Model{

        function clientModel()
        {
                parent::Model();
        }


/**
 ***********************************************************************
 * @Start Global Section of Reseller ***********************************
 ***********************************************************************
*/

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return unique  Client Login Name
 * @copyright 2011
 */

function checkClientLogin($login)

{
    $query = $this->db->query("select *from clientsip where login = '$login'");
    if($query->num_rows()>0)
    {
       return 1;
    }
    else
    {
    	$query = $this->db->query("select *from clientspin where login = '$login'");
    	if($query->num_rows()>0)
    	{
			return 1;
		}
		else
		{
			$query = $this->db->query("select *from clientssip where login = '$login'");
			if($query->num_rows()>0)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
		
    }
}






/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add Reseller Payment
 * @copyright 2011
 */


 function addClientPayment($userID)
 {


		$idClient = $this->input->post('id_client');
		$id_reseller = $this->input->post('id_reseller');
		$postBalance = $this->input->post('amount');
		$paymentType = $this->input->post('type');
		$clienttype = $this->input->post('clienttype');
		$lastbalance = $this->input->post('lastbalance');
		if(empty($postBalance) OR !is_numeric($postBalance) OR $postBalance <= 0)
		$postBalance = 0;

		$dataPayment['id_client'] = $idClient;
		$dataPayment['clienttype'] = $clienttype;
		$dataPayment['amount'] = $postBalance;
		$dataPayment['type'] = $paymentType;
		$dataPayment['description'] = $this->input->post('description');
		$dataPayment['user_id'] = $userID;
		$dataPayment['lastbalance'] = $lastbalance;
		$dataPayment['usertype'] = ADMIN_TYPE;
		$dataPayment['ipaddress'] = $this->getRealIpAddr();

		$sql = $this->db->insert_string('clientpayment', $dataPayment);
		$query = $this->db->query($sql);
		$idInsert = $this->db->insert_id();
		if($idInsert)
		{

		   if($paymentType==1)
		   {
		     
			 if($clienttype == SIP_USER)
		     {
		   		$sqlUpdate = "UPDATE clientssip set balance  = balance  + '$postBalance' where id='$idClient'";
		   		$queryUpdate = $this->db->query($sqlUpdate);
		     }
		     if($clienttype == ANI_USER)
		     {
				$sqlUpdate = "UPDATE clientspin set balance  = balance  + $postBalance where id='$idClient'";
				$queryUpdate = $this->db->query($sqlUpdate);
				
				$sqlUpdateResellerLimit = "UPDATE resellers1 set limit_balance  = limit_balance  + $postBalance where id='$id_reseller'";
				$queryUpdateResellerLimit = $this->db->query($sqlUpdateResellerLimit);
			 }
		     if($clienttype == WHOLESALE_USER_TYPE)
		     {
				$sqlUpdate = "UPDATE clientsip set balance  = balance  + $postBalance where id='$idClient'";
				$queryUpdate = $this->db->query($sqlUpdate);
				$sqlUpdateResellerLimit = "UPDATE resellers1 set limit_balance  = limit_balance  + $postBalance where id='$id_reseller'";
				$queryUpdateResellerLimit = $this->db->query($sqlUpdateResellerLimit);

			 }
			 
		   }
		   else
		   {
		   	 
			 if($clienttype==SIP_USER)
		     {
		   		$sqlUpdate = "UPDATE clientssip set balance  = balance  - '$postBalance' where id='$idClient'";
		   		$queryUpdate = $this->db->query($sqlUpdate);
		     }
		     if($clienttype==ANI_USER)
		     {
				$sqlUpdate = "UPDATE clientspin set balance  = balance  - $postBalance where id='$idClient'";
				$queryUpdate = $this->db->query($sqlUpdate);

				$sqlUpdateResellerLimit = "UPDATE resellers1 set limit_balance  = limit_balance  - $postBalance where id='$id_reseller'";
				$queryUpdateResellerLimit = $this->db->query($sqlUpdateResellerLimit);

			 }

		     if($clienttype == WHOLESALE_USER_TYPE)
		     {
				$sqlUpdate = "UPDATE clientsip set balance  = balance  - $postBalance where id='$idClient'";
				$queryUpdate = $this->db->query($sqlUpdate);
				$sqlUpdateResellerLimit = "UPDATE resellers1 set limit_balance  = limit_balance  - $postBalance where id='$id_reseller'";
				$queryUpdateResellerLimit = $this->db->query($sqlUpdateResellerLimit);

			 }
			 			 
		   }

		}

 }




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function check ANI Number Information
 * @copyright 2011
 */


function checkANINumberInfo($idClient,$clienttype,$aninumber)
{
    $query = $this->db->query("select *from aninumber where id_client = '$idClient' AND clienttype = '$clienttype' AND aninumber = '$aninumber'");
    echo $this->db->last_query();
    if($query->num_rows()>0)
	{
		return 1;
	}
    else
	return 0;
}



/**
 ***********************************************************************
 * @Start WholeSale Client Section *************************************
 ***********************************************************************
*/




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return total number of WholeSale Client
 * @copyright 2011
 */
 
 	function getTotalnumClientsIP($login,$password,$full_name,$phone,$id_rate,$id_reseller)
 	{

		if($full_name=="NULL")
		$fullNameStr = "";
		else
		$fullNameStr = "AND a.full_name like '$full_name%'";

		if($login=="NULL")
		$loginStr = "";
		else
		$loginStr = "AND a.login like '$login%'";

		if($password=="NULL")
		$passwordStr = "";
		else
		$passwordStr = "AND a.password like '$password%'";


		if($phone=="NULL")
		$phoneStr = "";
		else
		$phoneStr = "AND a.phone like '$phone%'";

		if($id_rate=="NULL")
		$idRateStr = "";
		else
		$idRateStr = "AND a.id_rate ='$id_rate'";

		if($id_reseller=="NULL")
		$idResellerStr = "";
		else
		$idResellerStr = "AND a.id_reseller ='$id_reseller'";
 	   	
		$sql="SELECT
	   		count(a.id) as totalrows	   	
			FROM clientsip a
			Where a.id != 0 $fullNameStr $loginStr $passwordStr $phoneStr $idRateStr $idResellerStr";			
		
		$row = 0;
		$query = $this->db->query($sql);
		foreach($query->result() as $resultrow)
		{
			$row = $resultrow->totalrows;
		}
		return $row;

	}




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return WholeSale Clients List
 * @copyright 2011
 */
	function clientsIPList($startoffset,$limit,$login,$password,$full_name,$phone,$id_rate,$id_reseller)
	{

		
		$clienttype = WHOLESALE_USER;
		$new_startoffset = $startoffset - 1;
		if($new_startoffset <0)
		$new_startoffset = 0;
		$startoffset1 = $limit * $new_startoffset;
	    $limit_string = "LIMIT $startoffset1,$limit";
			
 		if($full_name=="NULL")
		$fullNameStr = "";
		else
		$fullNameStr = "AND a.full_name like '$full_name%'";

		if($login=="NULL")
		$loginStr = "";
		else
		$loginStr = "AND a.login like '$login%'";

		if($password=="NULL")
		$passwordStr = "";
		else
		$passwordStr = "AND a.password like '$password%'";


		if($phone=="NULL")
		$phoneStr = "";
		else
		$phoneStr = "AND a.phone like '$phone%'";

		if($id_rate=="NULL")
		$idRateStr = "";
		else
		$idRateStr = "AND a.id_rate ='$id_rate'";

		if($id_reseller=="NULL")
		$idResellerStr = "";
		else
		$idResellerStr = "AND a.id_reseller ='$id_reseller'";

        
        
		$sql = "SELECT
				a.id,
				a.login,
				a.password,
				a.calllimit,
				a.inuse,
				a.numuse,
				a.id_rate,
				a.balance,
				a.id_reseller,
				a.tech_prefix,
				a.billing_prefix,
				a.full_name,
				a.calllimit,
				a.status
		        FROM clientsip a

				where a.id != 0 $fullNameStr $loginStr $passwordStr $phoneStr $idRateStr $idResellerStr order by a.login ASC $limit_string";

			   //echo $sql;

	    $query = $this->db->query($sql);
	    if($query->num_rows()>0)
		  return $query->result();
	    else
		 return 0;
	}




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add Client
 * @copyright 2011
 */

 function addClientsIP($userID)
 {
	$postBalance = $this->input->post('balance');
	if(empty($postBalance) OR !is_numeric($postBalance) OR $postBalance <= 0)
	$postBalance = 0;
	
	$idReseller = $this->input->post('id_reseller');
	if(empty($idReseller) OR !is_numeric($idReseller) OR $idReseller == 0)
	$idReseller = -1;
  
  	$calllimit = $this->input->post('calllimit');
	if(empty($calllimit) OR !is_numeric($calllimit) OR $calllimit <= 0)
	$calllimit = 0;


	$dataWholeSale['login']		= $this->input->post('login');
	$dataWholeSale['password']		= $this->input->post('password');
	$dataWholeSale['calllimit']		= $calllimit;
	$dataWholeSale['id_rate']		= $this->input->post('id_rate');
	$dataWholeSale['balance']		= $postBalance;
	$dataWholeSale['id_reseller']		= $idReseller;
	$dataWholeSale['tech_prefix']		= $this->input->post('tech_prefix');
	$dataWholeSale['billing_prefix']		= $this->input->post('billing_prefix');
	$dataWholeSale['full_name']		= $this->input->post('full_name');
	$dataWholeSale['phone']		= $this->input->post('phone');	
	$dataWholeSale['cell']		= $this->input->post('cell');
	$dataWholeSale['email']		= $this->input->post('email');
	$dataWholeSale['country']		= $this->input->post('country');
	$dataWholeSale['address']		= $this->input->post('address');
	$dataWholeSale['status']		= $this->input->post('status');

	$sql = $this->db->insert_string('clientsip', $dataWholeSale);
	$query = $this->db->query($sql);
	$idClient = $this->db->insert_id();
			
	if($idClient)
	{
				$dataPayment['id_client']		= $idClient;
				$dataPayment['clienttype']		= WHOLESALE_USER;
				$dataPayment['amount']		= $postBalance;
				$dataPayment['type']		= PREPAID;
				$dataPayment['description']		= "Initial Payment";
				$dataPayment['user_id']		= $userID;
				$dataPayment['usertype']		= ADMIN_TYPE;
				$dataPayment['ipaddress']		= $this->getRealIpAddr();
				$sqlPayment = $this->db->insert_string('clientpayment', $dataPayment);
		        $queryPayment = $this->db->query($sqlPayment);
				if($idReseller>0)
		   		{
			   		$sqlUpdate = "UPDATE resellers1 set limit_balance = limit_balance - '$balance' where id='$idReseller'";
			   		$queryUpdate = $this->db->query($sqlUpdate);
		   		}	
	}
 }	

 


 
 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return Edit Information of WholeSale Client
 * @copyright 2011
 */
	function clientsIPEditInfo($id)
	{

        $sql = "SELECT * FROM clientsip where id= '$id'";
        
	    $query = $this->db->query($sql);
	    if($query->num_rows() == 1)
		  return $query->result();
	    else
		 return 0;
	}
	



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add Client
 * @copyright 2011
 */

 function editClientsIP($userID)
 {
   	$key		= $this->input->post('id');
   	$idReseller = $this->input->post('id_reseller');
	if(empty($idReseller) OR !is_numeric($idReseller) OR $idReseller == 0)
	$idReseller = -1;

  	$calllimit = $this->input->post('calllimit');
	if(empty($calllimit) OR !is_numeric($calllimit) OR $calllimit <= 0)
	$calllimit = 0;


	$dataWholeSale['password']		= $this->input->post('password');
	$dataWholeSale['calllimit']		= $calllimit;
	$dataWholeSale['id_rate']		= $this->input->post('id_rate');
	$dataWholeSale['id_reseller']		= $idReseller;
	$dataWholeSale['tech_prefix']		= $this->input->post('tech_prefix');
	$dataWholeSale['billing_prefix']		= $this->input->post('billing_prefix');
	$dataWholeSale['full_name']		= $this->input->post('full_name');
	$dataWholeSale['phone']		= $this->input->post('phone');	
	$dataWholeSale['cell']		= $this->input->post('cell');
	$dataWholeSale['email']		= $this->input->post('email');
	$dataWholeSale['country']		= $this->input->post('country');
	$dataWholeSale['address']		= $this->input->post('address');
	$dataWholeSale['status']		= $this->input->post('status');
	
    $where = "id = $key";
	$sql = $this->db->update_string('clientsip', $dataWholeSale, $where);
	$query = $this->db->query($sql);
 }	

 




 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Delete information of ANI Number
 * @copyright 2011
 */


function deleteClientsIP($id)
{
    $type = WHOLESALE_USER_TYPE;
	$query = $this->db->query("DELETE FROM sipusers where id_client = '$id' AND clienttype = '$type'");
	$query = $this->db->query("UPDATE clientsip SET status = 0 where id = '$id'");
}



 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return IP Address Information of WholeSale Client
 * @copyright 2011
 */
	function getIPAddressClinetsIP($id,$type)
	{

        $sql = "SELECT id,context,ipaddr,port,id_client FROM sipusers where id_client= '$id' AND clienttype = '$type'";

			   //echo $sql;

	    $query = $this->db->query($sql);
	    if($query->num_rows() > 0)
		  return $query->result();
	    else
		 return 0;
	}
	



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return unique  IP Address
 * @copyright 2011
 */

function checkClientsIPAddress($ipAddress)

{
    $query = $this->db->query("select *from sipusers where ipaddr = '$ipAddress'");
    if($query->num_rows()>0)
    {
       return 1;
    }
    else
    {
    	return 0;
    }
}




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add Client
 * @copyright 2011
 */

 function addClientsIPAddress($userID)
 {

	//$dataProvider['id_provider']		= $this->input->post('id_provider');
	$id = $this->input->post('id');
	$ipType = $this->input->post('ipType');
	$postIpAddress = $this->input->post('ipAddress');
	$port = $this->input->post('port');
	if(empty($port) OR !is_numeric($port) OR $port <= 0)
	$port = "5060";
	
	$name = str_replace (".", "_", $postIpAddress);
	
	
	$dataGW['name']		= $postIpAddress;
	$dataGW['host']		= $postIpAddress;
	$dataGW['nat']		= WHOLESALE_NAT;
	$dataGW['type']		= WHOLESALE_TYPE;
	$dataGW['accountcode']		= $postIpAddress;
	$dataGW['cancallforward']		= WHOLESALE_CANCALLFORWARD;
	$dataGW['canreinvite']		= WHOLESALE_CANREINVITE;
	$dataGW['context']		= $ipType;
	$dataGW['defaultip']		= $postIpAddress;
	$dataGW['dtmfmode']		= WHOLESALE_DTMFMODE;
	$dataGW['insecure']		= WHOLESALE_INSECURE;
	$dataGW['qualify']		= WHOLESALE_QUALIFY;
	$dataGW['rtptimeout']		= WHOLESALE_RTP_TIME_OUT;
	$dataGW['rtpholdtimeout']		= WHOLESALE_RTP_TIME_HOLD;
	//$dataGW['disallow']		= WHOLESALE_DISALLOW;
	//$dataGW['allow']		= WHOLESALE_ALLOW;
	$dataGW['ipaddr']		= $postIpAddress;
	$dataGW['port']		= $port;
	$dataGW['encryption']		= WHOLESALE_ENC;
	$dataGW['id_client']		= $id;
	$dataGW['clienttype']		= WHOLESALE_USER_TYPE;
	
	$sql = $this->db->insert_string('sipusers', $dataGW);
	$query = $this->db->query($sql);
	$idGW = $this->db->insert_id();
 }	



 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Delete information Wholesale user IP Address
 * @copyright 2011
 */


function deleteClientsIPAddress($idClient,$idIP,$type)
{
    $query = $this->db->query("DELETE FROM  sipusers WHERE id = '$idIP' AND id_client ='$idClient' AND clienttype='$type'");
}




/**
 ***********************************************************************
 * @Start Client Section ***********************************************
 ***********************************************************************
*/



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return total number of Clinets SIP
 * @copyright 2011
 */
 
 	function getTotalnumClientsSIP($login,$password,$lot_id,$id_rate,$idReseller)
 	{
		if($login=="NULL")
		$loginStr = "";
		else
		$loginStr = "AND login like '$login%'";

		if($password=="NULL")
		$passwordStr = "";
		else
		$passwordStr = "AND password like '$password%'";

		if($idReseller=="NULL")
		$idResellerStr = "";
		else
		$idResellerStr = "AND id_reseller = '$idReseller'";

		if($lot_id=="NULL")
		$LotStr = "";
		else
		$LotStr = "AND lot_id = '$lot_id'";

		if($id_rate=="NULL")
		$idRateStr = "";
		else
		$idRateStr = "AND id_rate ='$id_rate'";


	   	$sql="SELECT
	   	count(id) as totalrows
	   	FROM clientssip 

	    Where id != 0 $loginStr $passwordStr $LotStr $idRateStr $idResellerStr";
	   	//echo $sql;
		$row = 0;
		$query = $this->db->query($sql);
		foreach($query->result() as $resultrow)
		{
			$row = $resultrow->totalrows;
		}
		return $row;

	}
	

/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return Client List
 * @copyright 2011
 */
	function SIPclientList($startoffset,$limit,$login,$password,$lot_id,$id_rate,$idReseller)
	{

		$new_startoffset = $startoffset - 1;
		if($new_startoffset <0)
		$new_startoffset = 0;
		$startoffset1 = $limit * $new_startoffset;
	    $limit_string = "LIMIT $startoffset1,$limit";

		if($login=="NULL")
		$loginStr = "";
		else
		$loginStr = "AND login like '$login%'";

		if($password=="NULL")
		$passwordStr = "";
		else
		$passwordStr = "AND password like '$password%'";

		if($idReseller=="NULL")
		$idResellerStr = "";
		else
		$idResellerStr = "AND id_reseller = '$idReseller'";

		if($lot_id=="NULL")
		$LotStr = "";
		else
		$LotStr = "AND lot_id = '$lot_id'";

		if($id_rate=="NULL")
		$idRateStr = "";
		else
		$idRateStr = "AND id_rate ='$id_rate'";

        $sql = "SELECT
				a.id,
				a.login,
				a.password,
				a.calllimit,
				a.inuse,
				a.numuse,
				a.id_rate,
				a.balance,
				a.id_reseller,
				a.tech_prefix,
				a.firstuse,				
				a.lastuse,
				a.full_name,
				a.email,
				a.cell,
				a.phone,
				a.city,
				a.country,
				a.state,
				a.address,
				a.status
				
				FROM clientssip a

				where a.id != 0 $loginStr $passwordStr $LotStr $idRateStr  $idResellerStr order by a.login ASC $limit_string";

			   //echo $sql;

	    $query = $this->db->query($sql);
	    if($query->num_rows()>0)
		  return $query->result();
	    else
		 return 0;
	}





/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add Client
 * @copyright 2011
 */

 function addSipClient($userID)
 {

	$login = $this->input->post('login');
	$password = $this->input->post('password');
	
	$postBalance = $this->input->post('balance');
	if(empty($postBalance) OR !is_numeric($postBalance) OR $postBalance <= 0)
	$postBalance = 0;
	
	$idReseller = $this->input->post('id_reseller');
	if(empty($idReseller) OR !is_numeric($idReseller) OR $idReseller == 0)
	$idReseller = -1;
  
  	$calllimit = $this->input->post('calllimit');
	if(empty($calllimit) OR !is_numeric($calllimit) OR $calllimit <= 0)
	$calllimit = 1;
	
	$status		= $this->input->post('status');


	$dataSIPClient['login']		= $login;
	$dataSIPClient['password']		= $password;
	$dataSIPClient['calllimit']		= $calllimit;
	$dataSIPClient['id_rate']		= $this->input->post('id_rate');
	$dataSIPClient['balance']		= $postBalance;
	$dataSIPClient['id_reseller']		= $idReseller;
	$dataSIPClient['tech_prefix']		= $this->input->post('tech_prefix');
	$dataSIPClient['billing_prefix']		= $this->input->post('billing_prefix');
	$dataSIPClient['ivr_allow']		= $this->input->post('ivr_allow');
	$dataSIPClient['full_name']		= $this->input->post('full_name');
	$dataSIPClient['phone']		= $this->input->post('phone');	
	$dataSIPClient['cell']		= $this->input->post('cell');
	$dataSIPClient['email']		= $this->input->post('email');
	$dataSIPClient['country']		= $this->input->post('country');
	$dataSIPClient['address']		= $this->input->post('address');
	$dataSIPClient['status']		= $status;

	$sql = $this->db->insert_string('clientssip', $dataSIPClient);
	$query = $this->db->query($sql);
	$idClient = $this->db->insert_id();
	
	if($idClient)
	{
		$dataPayment['id_client']		= $idClient;
		$dataPayment['clienttype']		= SIP_USER;
		$dataPayment['amount']		= $postBalance;
		$dataPayment['type']		= PREPAID;
		$dataPayment['description']		= "Initial Payment";
		$dataPayment['user_id']		= $userID;
		$dataPayment['usertype']		= ADMIN_TYPE;
		$dataPayment['ipaddress']		= $this->getRealIpAddr();
		$sqlPayment = $this->db->insert_string('clientpayment', $dataPayment);
		$queryPayment = $this->db->query($sqlPayment);
		if($idReseller>0)
		{
			$sqlUpdate = "UPDATE resellers1 set limit_balance = limit_balance - '$balance' where id='$idReseller'";
			$queryUpdate = $this->db->query($sqlUpdate);
		}
		
		// ************** Add SIP Clients ************** //
		if($status == 1)
		{
			$dataSIPUsers['name']		= $login;
			$dataSIPUsers['host']		= HOST;
			$dataSIPUsers['nat']		= NAT;
			$dataSIPUsers['type']		= TYPE_SIP;
			$dataSIPUsers['accountcode']		= $login;
			$dataSIPUsers['amaflags']		= AMAFLAGS;
			$dataSIPUsers['cancallforward']		= CANCALLFORWORD;
			$dataSIPUsers['canreinvite']		= CANREINVITE;
			$dataSIPUsers['context']		= SIP_CONTEXT;
			$dataSIPUsers['dtmfmode']		= DTMFMODE;
			$dataSIPUsers['secret']		= $password;
			//$dataSIPUsers['disallow']		= DISALLOW;
			//$dataSIPUsers['allow']		= ALLOW_SIP;
			$dataSIPUsers['qualify']		= QUALIFY;
			$dataSIPUsers['rtptimeout']		= SIP_RTP_TIME_OUT;
			$dataSIPUsers['rtpholdtimeout']		= SIP_RTP_TIME_HOLD;
			$dataSIPUsers['ipaddr']		= IP_ADDRESS;
			$dataSIPUsers['encryption']		= SIP_ENC;
			$dataSIPUsers['id_client']		= $idClient;
			$dataSIPUsers['clienttype']		= SIP_USER;	
			$sqlSIPUser = $this->db->insert_string('sipusers', $dataSIPUsers);
		    $querySIPUsers = $this->db->query($sqlSIPUser);
		}			   	
	}			
}


 
 
 
 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Return Edit Infomation of client
 * @copyright 2011
 */

	function sipClientEditInfo($id)
	{
    	$sql = "SELECT * FROM clientssip where id ='$id' LIMIT 1";
		$query = $this->db->query($sql);
    	if($query->num_rows()>0)
		{
			return $query->result();
		}
    	else
		return 0;
	}




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Client Payment Infomation
 * @copyright 2011
 */


function clientPaymentHistory($idClient,$clienttype )
{
    $query = $this->db->query("select *from clientpayment where id_client = '$idClient' AND clienttype = '$clienttype' order by paymentdate DESC LIMIT 100");
    //echo $this->db->last_query();
    if($query->num_rows()>0)
	{
		return $query->result();
	}
    else
	return 0;
}





 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Edit SIP Users
 * @copyright 2011
 */

 function sipEditClient($userID)
 {

	$keyvalue		= $this->input->post('id');

	$password = $this->input->post('password');
	$login = $this->input->post('login');
	
	
	$idReseller = $this->input->post('id_reseller');
	if(empty($idReseller) OR !is_numeric($idReseller) OR $idReseller == 0)
	$idReseller = -1;
  
  	$calllimit = $this->input->post('calllimit');
	if(empty($calllimit) OR !is_numeric($calllimit) OR $calllimit <= 0)
	$calllimit = 1;
	
	$status		= $this->input->post('status');
	$clientType = SIP_USER;


	$dataSIPClient['password']		= $password;
	$dataSIPClient['calllimit']		= $calllimit;
	$dataSIPClient['id_rate']		= $this->input->post('id_rate');
	$dataSIPClient['id_reseller']		= $idReseller;
	$dataSIPClient['tech_prefix']		= $this->input->post('tech_prefix');
	$dataSIPClient['billing_prefix']		= $this->input->post('billing_prefix');
	$dataSIPClient['ivr_allow']		= $this->input->post('ivr_allow');
	$dataSIPClient['full_name']		= $this->input->post('full_name');
	$dataSIPClient['phone']		= $this->input->post('phone');	
	$dataSIPClient['cell']		= $this->input->post('cell');
	$dataSIPClient['email']		= $this->input->post('email');
	$dataSIPClient['country']		= $this->input->post('country');
	$dataSIPClient['address']		= $this->input->post('address');
	$dataSIPClient['status']		= $status;

	$where = "id = $keyvalue";
	$sql = $this->db->update_string('clientssip', $dataSIPClient, $where);
	$query = $this->db->query($sql);
	if($status == 0)
	{
		 $sqlDeleteSIPuser = "DELETE FROM sipusers WHERE id_client = '$keyvalue' AND clienttype = '$clientType'";
		 $queryDelete = $this->db->query($sqlDeleteSIPuser); 
	}
	else
	{
		 $checkSIPUser = $this->checkSIPUsersInfo($keyvalue,$clientType);
		 if($checkSIPUser == 0)
		 {
			$dataSIPUsers['name']		= $login;
			$dataSIPUsers['host']		= HOST;
			$dataSIPUsers['nat']		= NAT;
			$dataSIPUsers['type']		= TYPE_SIP;
			$dataSIPUsers['accountcode']		= $login;
			$dataSIPUsers['amaflags']		= AMAFLAGS;
			$dataSIPUsers['cancallforward']		= CANCALLFORWORD;
			$dataSIPUsers['canreinvite']		= CANREINVITE;
			$dataSIPUsers['context']		= SIP_CONTEXT;
			$dataSIPUsers['dtmfmode']		= DTMFMODE;
			$dataSIPUsers['secret']		= $password;
			$dataSIPUsers['rtptimeout']		= SIP_RTP_TIME_OUT;
			$dataSIPUsers['rtpholdtimeout']		= SIP_RTP_TIME_HOLD;

			//$dataSIPUsers['disallow']		= DISALLOW;
			//$dataSIPUsers['allow']		= ALLOW_SIP;
			$dataSIPUsers['ipaddr']		= IP_ADDRESS;
			$dataSIPUsers['encryption']		= SIP_ENC;
			$dataSIPUsers['qualify']		= QUALIFY;
			$dataSIPUsers['id_client']		= $keyvalue;
			$dataSIPUsers['clienttype']		= SIP_USER;	
			$sqlSIPUser = $this->db->insert_string('sipusers', $dataSIPUsers);
		    $querySIPUsers = $this->db->query($sqlSIPUser);
		 }
		 else
		 {
			$sqlUpdatePassword = "UPDATE sipusers SET secret = '$password' WHERE id_client = '$keyvalue' AND clienttype = '$clientType'";
			$queryUPDATE = $this->db->query($sqlUpdatePassword);
		 }
	}

  }
 
  
  /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Check SIP Users 
 * @copyright 2011
 */

	function checkSIPUsersInfo($id,$type)
	{
    	$sql = "SELECT * FROM sipusers where id_client ='$id' AND clienttype = '$type'";
		$query = $this->db->query($sql);
    	if($query->num_rows()>0)
		{
			return 1;
		}
    	else
		return 0;
	}

	
/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Delete information of Reseller I
 * @copyright 2011
 */


function deleteSipClient($idClient)
{
    $clientType = SIP_USER;
	$querySIPUser = $this->db->query("delete from sipusers where id_client = '$idClient' AND clienttype = '$clientType'");
    $queryInactiveSIPClient = $this->db->query("UPDATE clientssip set status = 0 where id = '$idClient'");
}


/**
 ***********************************************************************
 * @Start Calling Card / ANI Section ***********************************
 ***********************************************************************
*/


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return total number of Clinet
 * @copyright 2011
 */
 
 	function getTotalnumAniClient($login,$password,$fullName,$id_rate,$idReseller,$lot_id,$phone)
 	{
		if($login=="NULL")
		$loginStr = "";
		else
		$loginStr = "AND a.login like '$login%'";

		if($password=="NULL")
		$passwordStr = "";
		else
		$passwordStr = "AND a.password like '$password%'";

		if($idReseller=="NULL")
		$idResellerStr = "";
		else
		$idResellerStr = "AND a.id_reseller = '$idReseller'";

		if($fullName=="NULL")
		$fullNameStr = "";
		else
		$fullNameStr = "AND a.full_name like '$fullName%'";

		if($id_rate=="NULL")
		$idRateStr = "";
		else
		$idRateStr = "AND a.id_rate ='$id_rate'";

		if($lot_id=="NULL")
		$lotsStr = "";
		else
		$lotsStr = "AND a.lot_id ='$lot_id'";


		if($phone=="NULL")
		$pinnumberStr = "";
		else
		$pinnumberStr = "AND a.pinnumber  like '$phone%'";


	   	$sql="SELECT
	   	count(a.id) as totalrows
	   	FROM clientspin a
		   		   
	    Where a.id !=0  $loginStr $passwordStr $fullNameStr $idRateStr $idResellerStr $lotsStr  $pinnumberStr";
	   	//echo $sql;
		$row = 0;
		$query = $this->db->query($sql);
		foreach($query->result() as $resultrow)
		{
			$row = $resultrow->totalrows;
		}
		return $row;

	}
	


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return Client List
 * @copyright 2011
 */
	function aniClienttList($startoffset,$limit,$login,$password,$fullName,$id_rate,$idReseller,$lot_id,$phone)
	{

		//echo $pinnumber;
		$new_startoffset = $startoffset - 1;
		if($new_startoffset <0)
		$new_startoffset = 0;
		$startoffset1 = $limit * $new_startoffset;
	    $limit_string = "LIMIT $startoffset1,$limit";

		if($login=="NULL")
		$loginStr = "";
		else
		$loginStr = "AND a.login like '$login%'";

		if($password=="NULL")
		$passwordStr = "";
		else
		$passwordStr = "AND a.password like '$password%'";

		if($idReseller=="NULL")
		$idResellerStr = "";
		else
		$idResellerStr = "AND a.id_reseller = '$idReseller'";

		if($fullName=="NULL")
		$fullNameStr = "";
		else
		$fullNameStr = "AND a.full_name like '$fullName%'";

		if($id_rate=="NULL")
		$idRateStr = "";
		else
		$idRateStr = "AND a.id_rate ='$id_rate'";

		if($lot_id=="NULL")
		$lotsStr = "";
		else
		$lotsStr = "AND a.lot_id ='$lot_id'";

		if($phone=="NULL")
		$pinnumberStr = "";
		else
		$pinnumberStr = "AND a.pinnumber  like '$phone%'";

		
    	        $sql = "SELECT
				a.id,
				a.lot_id,
				a.plan_id,
				a.login,
				a.password,
				a.pinnumber,
				a.id_reseller,
				a.balance,
				a.id_rate,
				a.calllimit,
				a.numuse,
				a.id_rate,
				a.balance,
				a.creationdate,
				a.firstuse,
				a.lastuse,
				a.full_name,
				a.email,
				a.phone,
				a.status
				FROM clientspin a
				where a.id != 0 $loginStr $passwordStr $fullNameStr $idRateStr $idResellerStr $lotsStr $pinnumberStr order by a.full_name ASC $limit_string";			
			   //echo $sql;

	    $query = $this->db->query($sql);
	    if($query->num_rows()>0)
		  return $query->result();
	    else
		 return 0;
	}





/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return Client List as ANI Number
 * @copyright 2011
 */
	function aniNumberClienttList($startoffset,$limit,$login,$password,$fullName,$id_rate,$idReseller,$lot_id,$phone)
	{

		//echo $pinnumber;
		$clientType = ANI_USER;
		$new_startoffset = $startoffset - 1;
		if($new_startoffset <0)
		$new_startoffset = 0;
		$startoffset1 = $limit * $new_startoffset;
	    $limit_string = "LIMIT $startoffset1,$limit";

		if($login=="NULL")
		$loginStr = "";
		else
		$loginStr = "AND a.login like '$login%'";

		if($password=="NULL")
		$passwordStr = "";
		else
		$passwordStr = "AND a.password like '$password%'";

		if($idReseller=="NULL")
		$idResellerStr = "";
		else
		$idResellerStr = "AND a.id_reseller = '$idReseller'";

		if($fullName=="NULL")
		$fullNameStr = "";
		else
		$fullNameStr = "AND a.full_name like '$fullName%'";

		if($id_rate=="NULL")
		$idRateStr = "";
		else
		$idRateStr = "AND a.id_rate ='$id_rate'";

		if($lot_id=="NULL")
		$lotsStr = "";
		else
		$lotsStr = "AND a.lot_id ='$lot_id'";

		if($phone=="NULL")
		$pinnumberStr = "";
		else
		$pinnumberStr = "AND b.aninumber  like '$phone%'";

		
    	        $sql = "SELECT
				a.id,
				a.lot_id,
				a.plan_id,
				a.login,
				a.password,
				a.pinnumber,
				a.id_reseller,
				a.balance,
				a.id_rate,
				a.calllimit,
				a.numuse,
				a.id_rate,
				a.balance,
				a.creationdate,
				a.firstuse,
				a.lastuse,
				a.full_name,
				a.email,
				a.phone,
				a.status
				FROM clientspin a
				
				INNER JOIN aninumber b
		        ON a.id = b.id_client	

				where a.id != 0 AND b.clienttype = '$clientType' $loginStr $passwordStr $fullNameStr $idRateStr $idResellerStr $lotsStr $pinnumberStr order by a.full_name ASC $limit_string";			
			   //echo $sql;

	    $query = $this->db->query($sql);
	    if($query->num_rows()>0)
		  return $query->result();
	    else
		 return 0;
	}




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return unique  Client Login Name
 * @copyright 2011
 */

function checkAniClientLogin($login)
{
    $query = $this->db->query("select *from clientspin where login = '$login'");
    if($query->num_rows()>0)
    {
       return 1;
    }
    else
    {
    	return 0;
    }
}


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return unique  Client Login Name
 * @copyright 2011
 */

function checkAniClientPIN($pinnumber)
{
    $clientType = ANI_USER;
	$query = $this->db->query("select *from clientspin where pinnumber = '$pinnumber'");
    if($query->num_rows()>0)
    {
       return 1;
    }
    else
    {
    	$query = $this->db->query("select *from aninumber where aninumber = '$pinnumber' AND clienttype = '$clientType'");
    	if($query->num_rows()>0)
    	{
			return 1;
		}
		else
		{
			return 0;
		}
		
    }
}



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return unique  Client Login Name
 * @copyright 2011
 */

function checkAniClientPassword($password)
{
    $query = $this->db->query("select *from aniclient where password = '$password'");
    if($query->num_rows()>0)
    {
       return 1;
    }
    else
    {
    	return 0;
    }
}



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add Client
 * @copyright 2011
 */

 function addANIClient($userID)
 {
		//$data['id']		= $this->input->post('id');
		$data['login']		= $this->input->post('login');
		$data['password']		= $this->input->post('password');
		$data['pinnumber']		= $this->input->post('pinnumber');
		$data['lot_id']		= $this->input->post('lot_id');
		$data['plan_id']		= $this->input->post('plan_id');

		$calllimit		= $this->input->post('calllimit');
		if(empty($calllimit) OR !is_numeric($calllimit) OR $calllimit <= 0)
		$calllimit = 1;
		$data['calllimit'] =  $calllimit;

		$data['inuse']		= 0;
		$data['numuse']		= 0;
		$data['id_rate']		= $this->input->post('id_rate');
		$balance		= $this->input->post('balance');

		if(empty($balance) OR !is_numeric($balance) OR $balance <= 0)
		$balance = 0;
        $data['balance']		= $balance;		

		$idReseller		= $this->input->post('id_reseller');
		if($idReseller=="")
		$idReseller = -1;
		$data['id_reseller'] = $idReseller;


		$data['tech_prefix']		= $this->input->post('tech_prefix');
		$data['billing_prefix']		= $this->input->post('billing_prefix');
		$data['autoani']		= $this->input->post('autoani');
		$data['creationdate']		= date('Y-m-d H:i:s');
		//$data['firstuse']		= $this->input->post('firstuse');
		//$data['lastuse']		= $this->input->post('lastuse');
		$data['full_name']		= $this->input->post('full_name');
		$data['email']		= $this->input->post('email');
		$data['cell']		= $this->input->post('cell');
		$data['phone']		= $this->input->post('phone');
		$data['city']		= $this->input->post('city');
		$data['country']		= $this->input->post('country');
		$data['state']		= $this->input->post('state');
		$data['address']		= $this->input->post('address');
		$data['status']		= $this->input->post('status');
		
		$sql = $this->db->insert_string('clientspin', $data);
		$query = $this->db->query($sql);
		$idCardIDInsert = $this->db->insert_id();
		if($idCardIDInsert)
		{		    				
				$dataPayment['id_client']		= $idCardIDInsert;
				$dataPayment['clienttype']		= ANI_USER;
				$dataPayment['amount']		= $balance;
				$dataPayment['type']		= PREPAID;
				$dataPayment['description']		= "Initial Payment";
				$dataPayment['user_id']		= $userID;
				$dataPayment['usertype']		= ADMIN_TYPE;
				$dataPayment['ipaddress']		= $this->getRealIpAddr();
				$sqlPayment = $this->db->insert_string('clientpayment', $dataPayment);
		        $queryPayment = $this->db->query($sqlPayment);
		        
		        if($idReseller>0)
				{
					$sqlUpdate = "UPDATE resellers1 set limit_balance = limit_balance - '$balance' where id='$idReseller'";
					$queryUpdate = $this->db->query($sqlUpdate);
				}
		        
		}
 }
 
 
 

/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Return Edit Infomation of client
 * @copyright 2011
 */

	function aniClientEditInfo($id)
	{
        $sql = "SELECT *FROM clientspin where id ='$id' LIMIT 1";
	    $query = $this->db->query($sql);
    	if($query->num_rows()>0)
		{
			return $query->result();
		}
    	else
		return 0;
	}




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add Client
 * @copyright 2011
 */

 function editANIClient($userID)
 {

		$id		= $this->input->post('id');
		//$data['id']		= $this->input->post('id');
		//$data['login']		= $this->input->post('login');
		$data['password']		= $this->input->post('password');
		//$data['pinnumber']		= $this->input->post('pinnumber');
		$data['lot_id']		= $this->input->post('lot_id');
		$data['plan_id']		= $this->input->post('plan_id');

		$calllimit		= $this->input->post('calllimit');
		if(empty($calllimit) OR !is_numeric($calllimit) OR $calllimit <= 0)
		$calllimit = 1;
		$data['calllimit'] =  $calllimit;

		$data['inuse']		= 0;

		$data['id_rate']		= $this->input->post('id_rate');
		$balance		= $this->input->post('balance');


		$id_reseller		= $this->input->post('id_reseller');
		if($id_reseller=="")
		$id_reseller = -1;
		$data['id_reseller'] = $id_reseller;


		$data['tech_prefix']		= $this->input->post('tech_prefix');
		$data['billing_prefix']		= $this->input->post('billing_prefix');
		$data['autoani']		= $this->input->post('autoani');
		$data['full_name']		= $this->input->post('full_name');
		$data['email']		= $this->input->post('email');
		$data['cell']		= $this->input->post('cell');
		$data['phone']		= $this->input->post('phone');
		$data['city']		= $this->input->post('city');
		$data['country']		= $this->input->post('country');
		$data['state']		= $this->input->post('state');
		$data['address']		= $this->input->post('address');
		$data['status']		= $this->input->post('status');

		$whereANI = "id = $id";
		$sqlANI = $this->db->update_string('clientspin', $data, $whereANI);
		$query = $this->db->query($sqlANI);
				
 }
 
 
 
 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Delete information of ANI Client
 * @copyright 2011
 */


function deleteANIClient($idClient)
{
    $clientType = ANI_USER;
	$query = $this->db->query("DELETE FROM aninumber WHERE id_client =  '$idClient' AND clienttype = '$clientType'");
	$query = $this->db->query("UPDATE clientspin SET autoani = 0,status = 0 WHERE id =  '$idClient'");
}



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Return ANI Number History
 * @copyright 2011
 */


function clientAniNumberHistory($idClient,$clienttype)
{
    $query = $this->db->query("select *from aninumber where id_client = '$idClient' AND clienttype = '$clienttype' order by creationdate DESC LIMIT 100");
    //echo $this->db->last_query();
    if($query->num_rows()>0)
	{
		return $query->result();
	}
    else
	return 0;
}





/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return Speed Dial Information
 * @copyright 2011
 */


function clientSpeedDialHistory($idClient,$clienttype)
{
    $query = $this->db->query("select *from speeddial where id_client = '$idClient' AND clienttype = '$clienttype' order by name DESC");
    //echo $this->db->last_query();
    if($query->num_rows()>0)
	{
		return $query->result();
	}
    else
	return 0;
}





/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add ANI Number
 * @copyright 2011
 */


 function addClientANINumber($userID)
 {


		$idClient = $this->input->post('id_client');
		$aninumber = $this->input->post('aninumber');
		$clienttype = $this->input->post('clienttype');

		$dataANINumber['id_client'] = $idClient;
		$dataANINumber['clienttype'] = $clienttype;
		$dataANINumber['aninumber'] = $aninumber;
		$dataANINumber['user_id'] = $userID;
		$dataANINumber['usertype'] = ADMIN_TYPE;
		$dataANINumber['ipaddress'] = $this->getRealIpAddr();
		$dataANINumber['status'] = 1;

		$sql = $this->db->insert_string('aninumber', $dataANINumber);
		$query = $this->db->query($sql);
		$idInsert = $this->db->insert_id();
}


 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Delete information of ANI Number
 * @copyright 2011
 */


function deleteANINumber($id)
{
    //$query = $this->db->query("delete from cc_sip_buddies where id_cc_card = '$idClient'");
    $query = $this->db->query("DELETE FROM aninumber where id = '$id'");
}




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return Speed Dial Information Exits
 * @copyright 2011
 */


function cehckSpeedDialExits($idClient,$clienttype,$phone,$speeddial)
{
    $query = $this->db->query("select *from speeddial where id_client = '$idClient' AND clienttype = '$clienttype' AND phone = '$phone' AND speeddial = '$speeddial'");
    //echo $this->db->last_query();
    if($query->num_rows()>0)
	{
		return 1;
	}
    else
	return 0;
}




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add Speed Dial
 * @copyright 2011
 */


 function addClientSpeedDial($userID)
 {


		$idClient = $this->input->post('id_client');
		$phone = $this->input->post('phone');
		$clienttype = $this->input->post('clienttype');

		$data['id_client'] = $idClient;
		$data['clienttype'] = $clienttype;
		$data['phone'] = $phone;
		$data['name'] = $this->input->post('name');
		$data['speeddial'] = $this->input->post('speeddial');
		$data['creationdate'] = date('Y-m-d H:i:s');
		$data['user_id'] = $userID;
		$data['usertype'] = ADMIN_TYPE;
		$data['ipaddress'] = $this->getRealIpAddr();

		$sql = $this->db->insert_string('speeddial', $data);
		$query = $this->db->query($sql);
		$idInsert = $this->db->insert_id();
}




 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Delete information of ANI Number
 * @copyright 2011
 */


function deleteSpeedDial($id)
{
    //$query = $this->db->query("delete from cc_sip_buddies where id_cc_card = '$idClient'");
    $query = $this->db->query("DELETE FROM speeddial where id = '$id'");
}



/**
 ***********************************************************************
 * @Start DID Numbers Section ******************************************
 ***********************************************************************
*/


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return total number of DID 
 * @copyright 2011
 */
 
 	function getTotalnumDIDNumber($didNumber,$IDprovider,$didType,$location)
 	{
		if($didNumber=="NULL")
		$didNumberStr = "";
		else
		$didNumberStr = "AND didnumber like '$didNumber%'";

		if($IDprovider=="NULL")
		$IDproviderStr = "";
		else
		$IDproviderStr = "AND id_client = '$IDprovider'";


		if($didType=="NULL")
		$didTypeStr = "";
		else
		$didTypeStr = "AND didtype = '$didType'";

		if($location=="NULL")
		$locationStr = "";
		else
		$locationStr = "AND location LIKE '$location%'";


	   	$sql="SELECT
	   	count(id_did) as totalrows FROM didnumber
		Where clienttype = 4 $didNumberStr $IDproviderStr $didTypeStr $locationStr";
		$row = 0;
		$query = $this->db->query($sql);
		foreach($query->result() as $resultrow)
		{
			$row = $resultrow->totalrows;
		}
		return $row;

	}



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return DID Number List
 * @copyright 2011
 */
	function didNumberList($startoffset,$limit,$didNumber,$IDprovider,$didType,$location)
	{

		$new_startoffset = $startoffset - 1;
		if($new_startoffset <0)
		$new_startoffset = 0;
		$startoffset1 = $limit * $new_startoffset;
	    $limit_string = "LIMIT $startoffset1,$limit";

		if($didNumber=="NULL")
		$didNumberStr = "";
		else
		$didNumberStr = "AND a.didnumber like '$didNumber%'";

		if($IDprovider=="NULL")
		$IDproviderStr = "";
		else
		$IDproviderStr = "AND a.id_client = '$IDprovider'";


		if($didType=="NULL")
		$didTypeStr = "";
		else
		$didTypeStr = "AND a.didtype = '$didType'";
	
		if($location=="NULL")
		$locationStr = "";
		else
		$locationStr = "AND location LIKE '$location%'";


        $sql = "SELECT
				a.id_did,
				a.id_client,
				a.didnumber,
				a.didtype,
				a.location,
				a.creationdate,
				a.firstuse,
				a.lastuse,
				a.numuse,
				a.status,
				b.login,
				b.full_name

		        FROM didnumber a
		        INNER JOIN clientsip b
		        ON a.id_client = b.id
		        
				where a.clienttype = 4 $didNumberStr $IDproviderStr $didTypeStr $locationStr order by b.full_name ASC $limit_string";

			   //echo $sql;

	    $query = $this->db->query($sql);
	    if($query->num_rows()>0)
		  return $query->result();
	    else
		 return 0;
	}




 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Check DID Number Exits
 * @copyright 2011
 */
	function didNumberExits($didNumber)
	{

        $sql = "SELECT *FROM didnumber
				where didnumber= '$didNumber'";

			   //echo $sql;

	    $query = $this->db->query($sql);
	    if($query->num_rows()>0)
		  return 1;
	    else
		 return 0;
	}
	
	


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add DID Number
 * @copyright 2011
 */

 function addDIDNumber()
 {

	
	$data['id_client']		= $this->input->post('IDprovider');
	$data['clienttype']		= 4;
	$data['didnumber']		= $this->input->post('didnumber');
	$data['location']		= $this->input->post('location');
	$data['didtype']		= $this->input->post('didType');
	$data['creationdate']		= date('Y-m-d H:i:s');
	$data['firstuse']		= '000-00-00 00:00:00';
	$data['lastuse']		= '000-00-00 00:00:00';
	$data['numuse']		= 0;
	$data['status']		= $this->input->post('status');

	$sql = $this->db->insert_string('didnumber', $data);
	$query = $this->db->query($sql);
	$idProvider = $this->db->insert_id();
}




 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return Edit Information
 * @copyright 2011
 */
	function didNumberEditInfo($id)
	{

        $sql = "SELECT *FROM didnumber
				where id_did= '$id'";

			   //echo $sql;

	    $query = $this->db->query($sql);
	    if($query->num_rows()>0)
		  return $query->result();
	    else
		 return 0;
	}
	


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add DID Number
 * @copyright 2011
 */

 function editDIDNumber()
 {

	
	$id		= $this->input->post('id_did');
	$data['id_client']		= $this->input->post('IDprovider');
	$data['clienttype']		= 4;
	$data['didnumber']		= $this->input->post('didnumber');
	$data['location']		= $this->input->post('location');
	$data['didtype']		= $this->input->post('didType');
	$data['status']		= $this->input->post('status');

    $where = "id_did = $id";
	$sql = $this->db->update_string('didnumber', $data, $where);
	$query = $this->db->query($sql);

}



 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Delete information of DID Number
 * @copyright 2011
 */


function deleteDIDNumber($id)
{
    $query = $this->db->query("DELETE FROM didnumber where id_did = '$id'");
}


/**
 ***********************************************************************
 * @Start Account Generation Section ***********************************
 ***********************************************************************
*/


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return total number of Lots
 * @copyright 2011
 */
 
 	function getTotalnumLotsName($idReseller,$description,$clientType)
 	{
		if($idReseller=="NULL")
		$idResellerStr = "";
		else
		$idResellerStr = "AND id_reseller =  '$idReseller%'";

		if($description=="NULL")
		$descriptionStr = "";
		else
		$descriptionStr = "AND description LIKE '$description%'";


		if($clientType=="NULL")
		$clientTypeStr = "";
		else
		$clientTypeStr = "AND clienttype = '$clientType'";



	   	$sql="SELECT
	   	count(lot_id) as totalrows FROM lotsname
		Where lot_id > 0  $idResellerStr $descriptionStr $clientTypeStr";
		$row = 0;
		$query = $this->db->query($sql);
		foreach($query->result() as $resultrow)
		{
			$row = $resultrow->totalrows;
		}
		return $row;

	}




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return Lots Name List
 * @copyright 2011
 */
	function lotsNameList($startoffset,$limit,$idReseller,$description,$clientType)
	{

		$new_startoffset = $startoffset - 1;
		if($new_startoffset <0)
		$new_startoffset = 0;
		$startoffset1 = $limit * $new_startoffset;
	    $limit_string = "LIMIT $startoffset1,$limit";

		if($idReseller=="NULL")
		$idResellerStr = "";
		else
		$idResellerStr = "AND id_reseller =  '$idReseller%'";

		if($description=="NULL")
		$descriptionStr = "";
		else
		$descriptionStr = "AND description LIKE '$description%'";


		if($clientType=="NULL")
		$clientTypeStr = "";
		else
		$clientTypeStr = "AND clienttype = '$clientType'";


        $sql = "SELECT * FROM lotsname
				where lot_id > 0 $idResellerStr $descriptionStr $clientTypeStr  order by description ASC $limit_string";

			   //echo $sql;

	    $query = $this->db->query($sql);
	    if($query->num_rows()>0)
		  return $query->result();
	    else
		 return 0;
	}





/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add Lot Name With Account
 * @copyright 2011
 */


 function addClientLotsName($userID)
 {

		$description = $this->input->post('description');
		$clientType = $this->input->post('clientType');
		$id_reseller = $this->input->post('id_reseller');
		$balance = $this->input->post('balance');
		$id_rate = $this->input->post('id_rate');
		$calllimit = $this->input->post('calllimit');
		$numClients = $this->input->post('numClients');
		$loginLength = $this->input->post('loginLength');
		$loginPrefix = $this->input->post('loginPrefix');
		$passwordLength = $this->input->post('passwordLength');
		$passwordPrefix = $this->input->post('passwordPrefix');
		$status = $this->input->post('status');
		
		if($id_reseller=="")
		$id_reseller = -1;

		
		if(empty($balance) OR !is_numeric($balance) OR $balance <= 0)
		$balance = 0;
		
		if(empty($calllimit) OR !is_numeric($calllimit) OR $calllimit <= 0)
		$calllimit = 1;
		
		if(empty($numClients) OR !is_numeric($numClients) OR $numClients <= 0)
		$numClients = 0;
		
		$loginPrefixLength = strlen($loginPrefix);
		$loginLengthGen = $loginLength - $loginPrefixLength;
		
		if($loginPrefixLength == 0)
		$addLoginPrefix = "";
		else
		$addLoginPrefix = $loginPrefix;

		
		
		$passwordPrefixLength = strlen($passwordPrefix);
		$passwordLengthGen = $passwordLength - $passwordPrefixLength;
		
		if($passwordPrefixLength == 0)
		$addPasswordPrefix = "";
		else
		$addPasswordPrefix = $passwordPrefix;

		$chars = '1234567890';
		
		$dataLotsName['clienttype']  = $clientType;
		$dataLotsName['id_reseller']  = $id_reseller;
		$dataLotsName['description']  = $description;
		$dataLotsName['numpin']  = $numClients;
		$dataLotsName['user_id']  = $userID;
		$dataLotsName['usertype']  = ADMIN_TYPE;
		$dataLotsName['ipaddress']  = $this->getRealIpAddr();
		
		$sql = $this->db->insert_string('lotsname', $dataLotsName);
		$query = $this->db->query($sql);
		$lotsID = $this->db->insert_id();
		if($lotsID)
		{
			for($i = 1; $i <= $numClients; $i++)
			{			
				$flag = 1;
				while($flag != 0)
				{				
					$loginRandValue = $this->rand_str($loginLengthGen, $chars);
					$loginName = $addLoginPrefix.$loginRandValue;
					//echo "<br>";
					
					$passwordRandValue = $this->rand_str($passwordLengthGen, $chars);
					$password = $addPasswordPrefix.$passwordRandValue;
					
					// Check Login Name
					$checkLogin = $this->checkClientLogin($loginName);
					if($checkLogin == 0)
					{
						if($clientType == SIP_USER)
						$this->addSipClientRandom($lotsID,$userID,$loginName,$password,$balance,$calllimit,$id_rate,$id_reseller,$status);
						if($clientType == ANI_USER)
						$this->addANIClientRandom($lotsID,$userID,$loginName,$password,$balance,$calllimit,$id_rate,$id_reseller,$status);
						// Update Flag
						$flag = 0;
					}
				
				} // End While
			
			} // End For			
		}
  }
 



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add SIP Client
 * @copyright 2011
 */

 function addSipClientRandom($lotID,$userID,$login,$password,$postBalance,$calllimit,$rateID,$idReseller,$status)
 {


	$dataSIPClient['login']		= $login;
	$dataSIPClient['password']		= $password;
	$dataSIPClient['calllimit']		= $calllimit;
	$dataSIPClient['id_rate']		= $rateID;
	$dataSIPClient['lot_id']		= $lotID;
	$dataSIPClient['balance']		= $postBalance;
	$dataSIPClient['id_reseller']		= $idReseller;
	$dataSIPClient['status']		= $status;

	$sql = $this->db->insert_string('clientssip', $dataSIPClient);
	$query = $this->db->query($sql);
	$idClient = $this->db->insert_id();
	
	if($idClient)
	{
		$dataPayment['id_client']		= $idClient;
		$dataPayment['clienttype']		= SIP_USER;
		$dataPayment['amount']		= $postBalance;
		$dataPayment['type']		= PREPAID;
		$dataPayment['description']		= "Initial Payment during lot generation";
		$dataPayment['user_id']		= $userID;
		$dataPayment['usertype']		= ADMIN_TYPE;
		$dataPayment['ipaddress']		= $this->getRealIpAddr();
		$sqlPayment = $this->db->insert_string('clientpayment', $dataPayment);
		$queryPayment = $this->db->query($sqlPayment);
		if($idReseller>0)
		{
			$sqlUpdate = "UPDATE resellers1 set limit_balance = limit_balance - '$postBalance' where id='$idReseller'";
			$queryUpdate = $this->db->query($sqlUpdate);
		}
		
		// ************** Add SIP Clients ************** //
		if($status == 1)
		{
			$dataSIPUsers['name']		= $login;
			$dataSIPUsers['host']		= HOST;
			$dataSIPUsers['nat']		= NAT;
			$dataSIPUsers['type']		= TYPE_SIP;
			$dataSIPUsers['accountcode']		= $login;
			$dataSIPUsers['amaflags']		= AMAFLAGS;
			$dataSIPUsers['cancallforward']		= CANCALLFORWORD;
			$dataSIPUsers['canreinvite']		= CANREINVITE;
			$dataSIPUsers['context']		= SIP_CONTEXT;
			$dataSIPUsers['dtmfmode']		= DTMFMODE;
			$dataSIPUsers['secret']		= $password;
			$dataSIPUsers['rtptimeout']		= SIP_RTP_TIME_OUT;
			$dataSIPUsers['rtpholdtimeout']		= SIP_RTP_TIME_HOLD;
			//$dataSIPUsers['disallow']		= DISALLOW;
			//$dataSIPUsers['allow']		= ALLOW_SIP;
			$dataSIPUsers['ipaddr']		= IP_ADDRESS;
			$dataSIPUsers['encryption']		= SIP_ENC;
			$dataSIPUsers['qualify']		= QUALIFY;
			$dataSIPUsers['id_client']		= $idClient;
			$dataSIPUsers['clienttype']		= SIP_USER;	
			$sqlSIPUser = $this->db->insert_string('sipusers', $dataSIPUsers);
		    $querySIPUsers = $this->db->query($sqlSIPUser);
		}			   	
	}			
 }





/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add Client
 * @copyright 2011
 */

 function addANIClientRandom($lotID,$userID,$login,$password,$postBalance,$calllimit,$rateID,$idReseller,$status)
 {
		
		$data['login']		= $login;
		$data['password']		= $password;
		$data['pinnumber']		= $login;
		$data['lot_id']		= $lotID;
		//$data['plan_id']		= $this->input->post('plan_id');
		$data['calllimit'] =  $calllimit;

		$data['inuse']		= 0;
		$data['numuse']		= 0;
		$data['id_rate']		= $rateID;
        $data['balance']		= $postBalance;		

		$data['id_reseller'] = $idReseller;

		$data['autoani']		= 1;
		$data['creationdate']		= date('Y-m-d H:i:s');
		$data['status']		= $this->input->post('status');
		
		$sql = $this->db->insert_string('clientspin', $data);
		$query = $this->db->query($sql);
		$idCardIDInsert = $this->db->insert_id();
		if($idCardIDInsert)
		{		    				
				$dataPayment['id_client']		= $idCardIDInsert;
				$dataPayment['clienttype']		= ANI_USER;
				$dataPayment['amount']		= $postBalance;
				$dataPayment['type']		= PREPAID;
				$dataPayment['description']		= "Initial Payment during lot generation";
				$dataPayment['user_id']		= $userID;
				$dataPayment['usertype']		= ADMIN_TYPE;
				$dataPayment['ipaddress']		= $this->getRealIpAddr();
				$sqlPayment = $this->db->insert_string('clientpayment', $dataPayment);
		        $queryPayment = $this->db->query($sqlPayment);
		        
		        if($idReseller>0)
				{
					$sqlUpdate = "UPDATE resellers1 set limit_balance = limit_balance - '$postBalance' where id='$idReseller'";
					$queryUpdate = $this->db->query($sqlUpdate);
				}
		        
		}
  }
 
 
 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Return Random value
 * @copyright 2011
 */
  
	function rand_str($length = 32, $chars = '1234567890')
	{
    	// Length of character list
    	$chars_length = (strlen($chars) - 1);

    	// Start our string
    	$string = $chars{rand(0, $chars_length)};
   
    	// Generate random string
    	for ($i = 1; $i < $length; $i = strlen($string))
    	{
        	// Grab a random character from our list
        	$r = $chars{rand(0, $chars_length)};
       
        	// Make sure the same two characters don't appear next to each other
        	if ($r != $string{$i - 1}) $string .=  $r;
    	}
   
    	// Return the string
    	return $string;
	}





/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Inactive Client SIP Lot
 * @copyright 2011
 */
	function inactiveClientsSipLot($idLot,$clientType)
	{

		$sql = "SELECT id FROM clientssip WHERE lot_id = '$idLot'";
		$query = $this->db->query($sql);
		if($query->num_rows()>0)
		{
		    if(is_array($query->result()))
			{
				foreach($query->result() as $row)
				{
					$idClient = $row->id;
					$sqlUpdateStatus = "UPDATE clientssip SET status = 0 WHERE id = '$idClient' AND lot_id = '$idLot'";
					$queryUpdate = $this->db->query($sqlUpdateStatus);
					// Delete SIP User	    
					$sqlDeleteSipUser = "DELETE FROM sipusers  WHERE id_client = '$idClient' AND clienttype = '$clientType'";
					$queryDELETE = $this->db->query($sqlDeleteSipUser);						
				}
				
			} // End If	
		} // End If
		

	}





/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Inactive Client PIN Lot
 * @copyright 2011
 */
	function inactiveClientsPinLot($idLot)
	{

		$sqlUpdateStatus = "UPDATE clientspin SET status = 0 WHERE lot_id = '$idLot'";
		$query = $this->db->query($sqlUpdateStatus);
	}
	

/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Active Client SIP Lot
 * @copyright 2011
 */
	function activeClientsSipLot($idLot,$clientType)
	{
		$sql = "SELECT id,login,password FROM clientssip WHERE lot_id = '$idLot'";
		$query = $this->db->query($sql);
		if($query->num_rows()>0)
		{
		    if(is_array($query->result()))
			{
				foreach($query->result() as $row)
				{
					$idClient = $row->id;
					$login = $row->login;
					$password = $row->password;
					$sqlUpdateStatus = "UPDATE clientssip SET status = 1 WHERE id = '$idClient' AND lot_id = '$idLot'";
					$queryUpdate = $this->db->query($sqlUpdateStatus);
					
					// Delete SIP User	    
					$sqlDeleteSipUser = "DELETE FROM sipusers  WHERE id_client = '$idClient' AND clienttype = '$clientType'";
					$queryDELETE = $this->db->query($sqlDeleteSipUser);						
					
					// Insert SIP User
					$dataSIPUsers['name']		= $login;
					$dataSIPUsers['host']		= HOST;
					$dataSIPUsers['nat']		= NAT;
					$dataSIPUsers['type']		= TYPE_SIP;
					$dataSIPUsers['accountcode']		= $login;
					$dataSIPUsers['amaflags']		= AMAFLAGS;
					$dataSIPUsers['cancallforward']		= CANCALLFORWORD;
					$dataSIPUsers['canreinvite']		= CANREINVITE;
					$dataSIPUsers['context']		= SIP_CONTEXT;
					$dataSIPUsers['dtmfmode']		= DTMFMODE;
					$dataSIPUsers['secret']		= $password;
					$dataSIPUsers['rtptimeout']		= SIP_RTP_TIME_OUT;
					$dataSIPUsers['rtpholdtimeout']		= SIP_RTP_TIME_HOLD;

					//$dataSIPUsers['disallow']		= DISALLOW;
					//$dataSIPUsers['allow']		= ALLOW_SIP;
					$dataSIPUsers['ipaddr']		= IP_ADDRESS;
					$dataSIPUsers['encryption']		= SIP_ENC;
					$dataSIPUsers['qualify']		= QUALIFY;
					$dataSIPUsers['id_client']		= $idClient;
					$dataSIPUsers['clienttype']		= SIP_USER;	
					$sqlSIPUser = $this->db->insert_string('sipusers', $dataSIPUsers);
		    		$querySIPUsers = $this->db->query($sqlSIPUser);					
				}
				
			} // End If	
		} // End If
	}


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Active Client PIN Lot
 * @copyright 2011
 */
	function activeClientsPinLot($idLot)
	{

		$sqlUpdateStatus = "UPDATE clientspin SET status = 1 WHERE lot_id = '$idLot'";
		$query = $this->db->query($sqlUpdateStatus);
	}
	


 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Export SIP Client Lot
 * @copyright 2011
 */
 
	function exportClientsLotsInfo($idLot,$clientType = 2)
	{ 
        if($clientType == SIP_USER)
		$sql = "SELECT *FROM clientssip where lot_id= '$idLot'";
		if($clientType == ANI_USER)
		$sql = "SELECT *FROM clientspin where lot_id= '$idLot'";
		
	    $query = $this->db->query($sql);
	    if($query->num_rows()>0)
		  return $query->result();
	    else
		 return 0;
	}
	



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Delete Client SIP Lot
 * @copyright 2011
 */
	function deleteClientsSipLot($idLot,$clientType)
	{
		$sql = "SELECT id FROM clientssip WHERE lot_id = '$idLot'";
		$query = $this->db->query($sql);
		if($query->num_rows()>0)
		{
		    if(is_array($query->result()))
			{
				foreach($query->result() as $row)
				{
					$idClient = $row->id;
					// Delete SIP Client
					$sqlDeleteSipClient = "DELETE FROM clientssip WHERE id = '$idClient' AND lot_id = '$idLot'";
					$querySipClient = $this->db->query($sqlDeleteSipClient);					
					// Delete SIP User	    
					$sqlDeleteSipUser = "DELETE FROM sipusers  WHERE id_client = '$idClient' AND clienttype = '$clientType'";
					$querySipUser = $this->db->query($sqlDeleteSipUser);
					// Delete Payment History
					$sqlDeletePayment = "DELETE FROM clientpayment  WHERE id_client = '$idClient' AND clienttype = '$clientType'";
					$queryPayment = $this->db->query($sqlDeletePayment);
					// Delete Call History
					$sqlDeleteCalls = "DELETE FROM calls  WHERE id_client = '$idClient' AND clienttype = '$clientType'";
					$queryCalls = $this->db->query($sqlDeleteCalls);
				}
				
			} // End If	
		} // End If
		
		// Delete Lot Name
		$sqlDeleteLotsName = "DELETE FROM lotsname WHERE clienttype = '$clientType' AND lot_id = '$idLot'";
		$queryLotsName = $this->db->query($sqlDeleteLotsName);					

		
	}


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Delete Client PIN Lot
 * @copyright 2011
 */
	function deleteClientsPinLot($idLot,$clientType)
	{
		$sql = "SELECT id FROM clientspin WHERE lot_id = '$idLot'";
		$query = $this->db->query($sql);
		if($query->num_rows()>0)
		{
		    if(is_array($query->result()))
			{
				foreach($query->result() as $row)
				{
					$idClient = $row->id;
					// Delete Client
					$sqlDeleteClient = "DELETE FROM clientspin WHERE id = '$idClient' AND lot_id = '$idLot'";
					$querySipClient = $this->db->query($sqlDeleteClient);					

					// Delete Payment History
					$sqlDeletePayment = "DELETE FROM clientpayment  WHERE id_client = '$idClient' AND clienttype = '$clientType'";
					$queryPayment = $this->db->query($sqlDeletePayment);
					// Delete Call History
					$sqlDeleteCalls = "DELETE FROM calls  WHERE id_client = '$idClient' AND clienttype = '$clientType'";
					$queryCalls = $this->db->query($sqlDeleteCalls);
					
				}
				
			} // End If	
		} // End If
		
		// Delete Lot Name
		$sqlDeleteLotsName = "DELETE FROM lotsname WHERE clienttype = '$clientType' AND lot_id = '$idLot'";
		$queryLotsName = $this->db->query($sqlDeleteLotsName);					
	}






/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add Client
 * @copyright 2011
 */

 function addSipClientBackdsdsd()
 {

	$queryBack = $this->db->query("SELECT *FROM sipusers_back");
	if($queryBack->num_rows()>0)
	{
	   $resultBack =  $queryBack->result();
	   foreach($resultBack as $row)
	   {
	   	$id =  $row->id;
	   	$login = $row->name;
	   	$password = $row->secret;
	   	$id_rate = $row->id_rate;
	   	$id_reseller = $row->id_reseller;
	   	$balance = $row->balance;
	   	$status = $row->status;

	$dataSIPClient['id']		= $id;
	$dataSIPClient['login']		= $login;
	$dataSIPClient['password']		= $password;
	$dataSIPClient['calllimit']		= 1;
	$dataSIPClient['id_rate']		= $id_rate;
	$dataSIPClient['balance']		= $balance;
	$dataSIPClient['id_reseller']		= $id_reseller;
	//$dataSIPClient['tech_prefix']		= $this->input->post('tech_prefix');
	//$dataSIPClient['billing_prefix']		= $this->input->post('billing_prefix');
	//$dataSIPClient['full_name']		= $this->input->post('full_name');
	//$dataSIPClient['phone']		= $this->input->post('phone');	
	//$dataSIPClient['cell']		= $this->input->post('cell');
	//$dataSIPClient['email']		= $this->input->post('email');
	//$dataSIPClient['country']		= $this->input->post('country');
	//$dataSIPClient['address']		= $this->input->post('address');
	$dataSIPClient['status']		= $status;

	$sql = $this->db->insert_string('clientssip', $dataSIPClient);
	$query = $this->db->query($sql);
	$idClient = $id;
	
	if($idClient)
	{
		
		// ************** Add SIP Clients ************** //
		if($status == 1)
		{
			$dataSIPUsers['name']		= $login;
			$dataSIPUsers['host']		= HOST;
			$dataSIPUsers['nat']		= NAT;
			$dataSIPUsers['type']		= TYPE_SIP;
			$dataSIPUsers['accountcode']		= $login;
			$dataSIPUsers['amaflags']		= AMAFLAGS;
			$dataSIPUsers['cancallforward']		= CANCALLFORWORD;
			$dataSIPUsers['canreinvite']		= CANREINVITE;
			$dataSIPUsers['context']		= SIP_CONTEXT;
			$dataSIPUsers['dtmfmode']		= DTMFMODE;
			$dataSIPUsers['secret']		= $password;
			$dataSIPUsers['rtptimeout']		= SIP_RTP_TIME_OUT;
			$dataSIPUsers['rtpholdtimeout']		= SIP_RTP_TIME_HOLD;

			//$dataSIPUsers['disallow']		= DISALLOW;
			//$dataSIPUsers['allow']		= ALLOW_SIP;
			$dataSIPUsers['ipaddr']		= IP_ADDRESS;
			$dataSIPUsers['encryption']		= SIP_ENC;
			$dataSIPUsers['qualify']		= QUALIFY;
			$dataSIPUsers['id_client']		= $idClient;
			$dataSIPUsers['clienttype']		= SIP_USER;	
			$sqlSIPUser = $this->db->insert_string('sipusers', $dataSIPUsers);
		    $querySIPUsers = $this->db->query($sqlSIPUser);
		}			   	
	}
	}
 }				
}






}

?>