<?
class resellerModel extends Model{

        function resellerModel()
        {
                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  Reseller Login Name
 * @copyright 2011
 */

function checkResellerLogin($login)

{
    $query = $this->db->query("select *from resellers4 where login = '$login'");
    if($query->num_rows()>0)
    {
       return 1;
    }
    else
    {
        $query = $this->db->query("select *from resellers3 where login = '$login'");
        if($query->num_rows()>0)
        {
          return 1;
        }
        else
        {
          $query = $this->db->query("select *from resellers2 where login = '$login'");
          if($query->num_rows()>0)
          {
           	return 1;
          }
          else
          {
           $query = $this->db->query("select *from resellers1 where login = '$login'");
           if($query->num_rows()>0)
           {
			 return 1;
		   }
		   else
		   {
			 return 0;
		   }
          }
        }
    }
}



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


function resellerPaymentHistory($id_reseller,$level)
{
    $query = $this->db->query("select *from resellerpayment where id_reseller = '$id_reseller' AND level = '$level' 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 Add Reseller Payment
 * @copyright 2011
 */


 function addResellerPayment($userID,$level)
 {

		$idReseller = $this->input->post('id_reseller');
		$postBalance = $this->input->post('amount');
		$resellerLevel = $level;
		$paymentType = $this->input->post('type');
		$lastBalance = $this->input->post('lastBalance');

		if(!is_numeric($postBalance) OR $postBalance < 0 )
		$postBalance = 0;

		$dataPayment['id_reseller'] = $idReseller;
		$dataPayment['level'] = $resellerLevel;
		$dataPayment['amount'] = $postBalance;
		$dataPayment['lastbalance'] = $lastBalance;
		$dataPayment['type'] = $paymentType;
		$dataPayment['description'] = $this->input->post('description');
		$dataPayment['user_id'] = $userID;
		$dataPayment['usertype'] = ADMIN_TYPE;
		$dataPayment['ipaddress']		= $this->getRealIpAddr();

		$tableName = "resellers".$resellerLevel;

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

		   if($paymentType==1)
		   {
		      $sqlUpdate = "UPDATE $tableName set balance = balance + '$postBalance',limit_balance = limit_balance + '$postBalance' where id='$idReseller'";
		      $queryUpdate = $this->db->query($sqlUpdate);
		   }
		   else
		   {
		      $sqlUpdate = "UPDATE $tableName set balance = balance - '$postBalance',limit_balance = limit_balance - '$postBalance' where id='$idReseller'";
		      $queryUpdate = $this->db->query($sqlUpdate);
		   }
		   

		}

 }


/**
 ***********************************************************************
 * @Start Reseller IV Section ******************************************
 ***********************************************************************
*/


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return total number of reseller
 * @copyright 2011
 */
 
 	function getTotalnumResellerLeve4($login,$password,$full_name,$id_rate,$callPhone,$type,$typeValue)
 	{
		if($login=="NULL")
		$loginStr = "";
		else
		$loginStr = "AND login like '$login%'";

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

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

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

		if($callPhone=="NULL")
		$callPhoneStr = "";
		else
		$callPhoneStr = "AND phone like '$callPhone%'";
		
		if($type == -1)
		$typeStr = "";
		elseif($type == 0)
		$typeStr = "AND balance = '$typeValue'";
		elseif($type == 1)
		$typeStr = "AND balance > '$typeValue'";
		else
		$typeStr = "AND balance < '$typeValue'";
	
		

	   	$sql="SELECT
	   	count(id) as totalrows
	   	FROM resellers4 Where id != '-1' $loginStr $passwordStr $fullNameStr $idRateStr $callPhoneStr $typeStr";
		$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 Reseller List
 * @copyright 2011
 */
	function resellerLevel4List($startoffset,$limit,$login,$password,$full_name,$id_rate,$callPhone,$type,$typeValue)
	{

		$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($full_name=="NULL")
		$fullNameStr = "";
		else
		$fullNameStr = "AND a.full_name like '$full_name%'";

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

		if($callPhone=="NULL")
		$callPhoneStr = "";
		else
		$callPhoneStr = "AND a.phone like '$callPhone%'";
		
		if($type == -1)
		$typeStr = "";
		elseif($type == 0)
		$typeStr = "AND a.balance = '$typeValue'";
		elseif($type == 1)
		$typeStr = "AND a.balance > '$typeValue'";
		else
		$typeStr = "AND a.balance < '$typeValue'";



        $sql = "SELECT
				a.id,
				a.login,
				a.password,
				a.balance,
				a.id_rate,
				a.limit_user,
				a.limit_rate,
				a.limit_balance,
				a.sipuser,
				a.aniuser,
				a.limit_fnf,
				a.full_name,
				a.cell,
				a.phone,
				a.status

				FROM resellers4 a

				where a.id !=-1 $loginStr $passwordStr $fullNameStr $idRateStr $callPhoneStr $typeStr 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 Reseller III
 * @copyright 2011
 */

 function addReseller4($userID)
 {

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


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

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

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

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

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


		
		$data['balance']		= $balance;
		$data['id_rate']		= $this->input->post('id_rate');
		$data['limit_user']		= $limit_user;
		$data['limit_rate']		= $limit_rate;
		$data['limit_balance']		= $limit_balance;
		$data['limit_fnf']		= $this->input->post('limit_fnf');
		$data['commission']		= $commission;
		$data['type']		= $this->input->post('type');
		$data['sipuser']		= $this->input->post('sipuser');
		$data['aniuser']		= $this->input->post('aniuser');
		$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');
		$data['user_id']		= $userID;
		$data['usertype']		= ADMIN_TYPE;
		$data['ipaddress']		= $this->getRealIpAddr();
		$data['creationdate']		= date('Y-m-d H:i:s');

		$sql = $this->db->insert_string('resellers4', $data);
		$query = $this->db->query($sql);
		$idReseller = $this->db->insert_id();
		if($idReseller)
		{
		   $dataPayment['id_reseller'] = $idReseller;
		   $dataPayment['level'] = 4;
		   $dataPayment['amount'] = $balance;
		   $dataPayment['type'] = PREPAID;
		   $dataPayment['description'] = 'Initial Balance';
		   $dataPayment['user_id'] = $userID;
		   $dataPayment['usertype'] = ADMIN_TYPE;

		   $sqlPayment = $this->db->insert_string('resellerpayment', $dataPayment);
		   $queryPayment = $this->db->query($sqlPayment);


		}
 }




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


	function reseller4EditInfo($id)
	{
    	$query = $this->db->query("select *from resellers4 where id = '$id' LIMIT 1");
    	if($query->num_rows()>0)
		{
			return $query->result();
		}
    	else
		return 0;
	}
	



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

 function editReseller4($userID)
 {

		$keyvalue		= $this->input->post('id');
		
		$commission		= $this->input->post('commission');
		if(!is_numeric($commission) OR $commission<0)
		$commission = 0;

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

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

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

	
		//$data['login']		= $this->input->post('login');
		$data['password']		= $this->input->post('password');
		//$data['balance']		= $this->input->post('balance');
		$data['id_rate']		= $this->input->post('id_rate');
		$data['limit_user']		= $limit_user;
		$data['limit_rate']		= $limit_rate;
		$data['limit_balance']		= $limit_balance;
		$data['limit_fnf']		= $this->input->post('limit_fnf');
		$data['commission']		= $commission;
		$data['type']		= $this->input->post('type');
		$data['sipuser']		= $this->input->post('sipuser');
		$data['aniuser']		= $this->input->post('aniuser');
		$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');
		$data['user_id']		= $userID;
		$data['usertype']		= ADMIN_TYPE;
		$data['ipaddress']		= $this->getRealIpAddr();
		$data['creationdate']		= date('Y-m-d H:i:s');

        $where = "id = $keyvalue";
		$sql = $this->db->update_string('resellers4', $data, $where);
		$query = $this->db->query($sql);
 }




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Find check Child Reseller of Reseller IV
 * @copyright 2011
 */


function checkChildReseller4($idReseller)
{
    $query = $this->db->query("select *from resellers3 where id_reseller = '$idReseller'");

    if($query->num_rows()>0)
	{
		return 1;
	}
    else
	return 0;
}





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


function deleteReseller4($idReseller)
{
    $sql = "select id_rate from ratename where id_reseller = '$idReseller' AND level = 4";
	$queryRateName = $this->db->query($sql);
	if($queryRateName->num_rows()>0)
	 {
		$result =  $queryRateName->result();
		if(is_array($result))
		{
			foreach($result as $row)
			{
				$idRate = $row->id_rate;
				$sqlDeleteRateChart = "delete from ratechart where id_rate = '$idRate'";
				$queryDeleteRate = $this->db->query($sqlDeleteRateChart);
			}
		}	   
	$query = $this->db->query("delete from ratename where id_reseller = '$idReseller' AND level = 4");	  
   }

    $query = $this->db->query("delete from resellerpayment where id_reseller = '$idReseller' AND level = 4");
    $query = $this->db->query("delete from resellers4 where id = '$idReseller'");   
}



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Check Reseller 4 Balance Limit
 * @copyright 2011
 */
 
	function getReseller4BalanceLimit($idReseller)
	{
 		$query = $this->obj1->db->query("SELECT limit_balance FROM bb_resellers4 WHERE id = '$idReseller'");
		if ($query->num_rows() > 0)
	 	{
	 		$row = $query->row();
	  		return $row->limit_balance;
	 	}
	 	else
	 	return 0;
	}
	
		


/**
 ***********************************************************************
 * @end Reseller IV Section ********************************************
 ***********************************************************************
*/



/**
 ***********************************************************************
 * @Start Reseller III Section *****************************************
 ***********************************************************************
*/

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

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

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

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

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


		if($type == -1)
		$typeStr = "";
		elseif($type == 0)
		$typeStr = "AND balance = '$typeValue'";
		elseif($type == 1)
		$typeStr = "AND balance > '$typeValue'";
		else
		$typeStr = "AND balance < '$typeValue'";


	   	$sql="SELECT
	   	count(id) as totalrows
	   	FROM resellers3 Where id != '-1' $loginStr $passwordStr $fullNameStr $idRateStr $idResellerStr $typeStr";
		$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 Reseller List
 * @copyright 2011
 */
	function resellerLevel3List($startoffset,$limit,$login,$password,$full_name,$id_rate,$idReseller,$type,$typeValue)
	{

		$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($full_name=="NULL")
		$fullNameStr = "";
		else
		$fullNameStr = "AND a.full_name like '$full_name%'";

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

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

		if($type == -1)
		$typeStr = "";
		elseif($type == 0)
		$typeStr = "AND a.balance = '$typeValue'";
		elseif($type == 1)
		$typeStr = "AND a.balance > '$typeValue'";
		else
		$typeStr = "AND a.balance < '$typeValue'";



        $sql = "SELECT
				a.id,
				a.id_reseller,
				a.login,
				a.password,
				a.balance,
				a.id_rate,
				a.limit_user,
				a.limit_rate,
				a.limit_balance,
				a.sipuser,
				a.aniuser,
				a.limit_fnf,
				a.full_name,
				a.status

				FROM resellers3 a

				where a.id !=-1 $loginStr $passwordStr $fullNameStr $idRateStr $idResellerStr $typeStr  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 Reseller III
 * @copyright 2011
 */

 function addReseller3($userID)
 {

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

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

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

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

		$limit_rate		= $this->input->post('limit_rate');
		if(!is_numeric($limit_rate) OR $limit_rate<0)
		$limit_rate = 0;
		
		$limit_balance		= $this->input->post('limit_balance');
        if(!is_numeric($limit_balance) OR $limit_balance <= 0)
		$limit_balance = $balance;

		
		$data['id_reseller']		= $id_reseller;
		$data['login']		= $this->input->post('login');
		$data['password']		= $this->input->post('password');
		$data['balance']		= $balance;
		$data['id_rate']		= $this->input->post('id_rate');
		$data['limit_user']		= $limit_user;
		$data['limit_rate']		= $limit_rate;
		$data['limit_balance']		= $limit_balance;
		$data['limit_fnf']		= $this->input->post('limit_fnf');
		$data['commission']		= $commission;
		$data['type']		= $this->input->post('type');
		$data['sipuser']		= $this->input->post('sipuser');
		$data['aniuser']		= $this->input->post('aniuser');
		$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');
		$data['user_id']		= $userID;
		$data['usertype']		= ADMIN_TYPE;
		$data['ipaddress']		= $this->getRealIpAddr();
		$data['creationdate']		= date('Y-m-d H:i:s');

		$sql = $this->db->insert_string('resellers3', $data);
		$query = $this->db->query($sql);
		$idReseller = $this->db->insert_id();
		if($idReseller)
		{
		   $dataPayment['id_reseller'] = $idReseller;
		   $dataPayment['level'] = 3;
		   $dataPayment['amount'] = $balance;
		   $dataPayment['type'] = PREPAID;
		   $dataPayment['description'] = 'Initial Balance';
		   $dataPayment['user_id'] = $userID;
		   $dataPayment['usertype'] = ADMIN_TYPE;

		   $sqlPayment = $this->db->insert_string('resellerpayment', $dataPayment);
		   $queryPayment = $this->db->query($sqlPayment);
		   
		   if($id_reseller>0)
		   {
			   $sqlUpdate = "UPDATE resellers4 set limit_balance = limit_balance - '$balance' where id='$id_reseller'";
			   $queryUpdate = $this->db->query($sqlUpdate);
		   }


		}
 }


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


	function reseller3EditInfo($id)
	{
    	$query = $this->db->query("select *from resellers3 where id = '$id' LIMIT 1");
    	if($query->num_rows()>0)
		{
			return $query->result();
		}
    	else
		return 0;
	}

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

 function editReseller3($userID)
 {

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

		$id_reseller		= $this->input->post('id_reseller');
		if($id_reseller=='')
		$id_reseller = -1;
	
		$commission		= $this->input->post('commission');
		if(!is_numeric($commission) OR $commission <= 0)
		$commission = 0;

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

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

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

	
		//$data['login']		= $this->input->post('login');
		$data['id_reseller']		= $id_reseller;
		$data['password']		= $this->input->post('password');
		//$data['balance']		= $this->input->post('balance');
		$data['id_rate']		= $this->input->post('id_rate');
		$data['limit_user']		= $limit_user;
		$data['limit_rate']		= $limit_rate;
		$data['limit_balance']		= $limit_balance;
		$data['limit_fnf']		= $this->input->post('limit_fnf');
		$data['commission']		= $commission;
		$data['type']		= $this->input->post('type');
		$data['sipuser']		= $this->input->post('sipuser');
		$data['aniuser']		= $this->input->post('aniuser');
		$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');
		$data['user_id']		= $userID;
		$data['usertype']		= ADMIN_TYPE;
		$data['ipaddress']		= $this->getRealIpAddr();
		$data['creationdate']		= date('Y-m-d H:i:s');

        $where = "id = $keyvalue";
		$sql = $this->db->update_string('resellers3', $data, $where);
		$query = $this->db->query($sql);
 }



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Find check Child Reseller of Reseller III
 * @copyright 2011
 */


function checkChildReseller3($idReseller)
{
    $query = $this->db->query("select *from resellers2 where id_reseller = '$idReseller'");

    if($query->num_rows()>0)
	{
		return 1;
	}
    else
	return 0;
}


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

function deleteReseller3($idReseller)
{
    $sql = "select id_rate from ratename where id_reseller = '$idReseller' AND level = 3";
	$queryRateName = $this->db->query($sql);
	if($queryRateName->num_rows()>0)
	 {
		$result =  $queryRateName->result();
		if(is_array($result))
		{
			foreach($result as $row)
			{
				$idRate = $row->id_rate;
				$sqlDeleteRateChart = "delete from ratechart where id_rate = '$idRate'";
				$queryDeleteRate = $this->db->query($sqlDeleteRateChart);
			}
		}	   
	$query = $this->db->query("delete from ratename where id_reseller = '$idReseller' AND level = 3");	  
   }

    $query = $this->db->query("delete from resellerpayment where id_reseller = '$idReseller' AND level = 3");
    $query = $this->db->query("delete from resellers3 where id = '$idReseller'");

    
}




/**
 ***********************************************************************
 * @Start Reseller II Section ******************************************
 ***********************************************************************
*/


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return total number of reseller
 * @copyright 2011
 */
 
 	function getTotalnumResellerLeve2($login,$password,$full_name,$id_rate,$idReseller,$type, $typeValue)
 	{
		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($full_name=="NULL")
		$fullNameStr = "";
		else
		$fullNameStr = "AND full_name like '$full_name%'";

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

		if($type == -1)
		$typeStr = "";
		elseif($type == 0)
		$typeStr = "AND balance = '$typeValue'";
		elseif($type == 1)
		$typeStr = "AND balance > '$typeValue'";
		else
		$typeStr = "AND balance < '$typeValue'";


	   	$sql="SELECT
	   	count(id) as totalrows
	   	FROM resellers2 Where id != '-1' $loginStr $passwordStr $fullNameStr $idRateStr $idResellerStr $typeStr";
	   	//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 Reseller List
 * @copyright 2011
 */
	function resellerLevel2List($startoffset,$limit,$login,$password,$full_name,$id_rate,$idReseller, $type, $typeValue)
	{

		$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($idReseller=="NULL")
		$idResellerStr = "";
		else
		$idResellerStr = "AND a.id_reseller = '$idReseller'";

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

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

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

		if($type == -1)
		$typeStr = "";
		elseif($type == 0)
		$typeStr = "AND a.balance = '$typeValue'";
		elseif($type == 1)
		$typeStr = "AND a.balance > '$typeValue'";
		else
		$typeStr = "AND a.balance < '$typeValue'";


        $sql = "SELECT
				a.id,
				a.login,
				a.password,
				a.id_reseller,
				a.balance,
				a.id_rate,
				a.limit_user,
				a.limit_rate,
				a.limit_balance,
				a.limit_fnf,
				a.aniuser,
				a.sipuser,
				a.full_name,
				a.status

				FROM resellers2 a

				where a.id !=-1 $loginStr $passwordStr $fullNameStr $idRateStr  $idResellerStr $typeStr 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 Reseller II
 * @copyright 2011
 */

 function addReseller2($userID)
 {

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

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

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

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

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

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

		
		$data['id_reseller']		= $idReseller;
		$data['login']		= $this->input->post('login');
		$data['password']		= $this->input->post('password');
		$data['balance']		= $balance;
		$data['id_rate']		= $this->input->post('id_rate');
		$data['limit_user']		= $limit_user;
		$data['limit_rate']		= $limit_rate;
		$data['limit_balance']		= $limit_balance;
		$data['limit_fnf']		= $this->input->post('limit_fnf');
		$data['commission']		= $commission;
		$data['type']		= $this->input->post('type');
		$data['sipuser']		= $this->input->post('sipuser');
		$data['aniuser']		= $this->input->post('aniuser');
		$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');
		$data['user_id']		= $userID;
		$data['usertype']		= ADMIN_TYPE;
		$data['ipaddress']		= $this->getRealIpAddr();

		$sql = $this->db->insert_string('resellers2', $data);
		$query = $this->db->query($sql);
		$idInsert = $this->db->insert_id();
		if($idInsert)
		{
		   $dataPayment['id_reseller'] = $idInsert;
		   $dataPayment['level'] = 2;
		   $dataPayment['amount'] = $data['balance'];
		   $dataPayment['type'] = PREPAID;
		   $dataPayment['description'] = 'Initial Balance';
		   $dataPayment['user_id'] = $userID;
		   $dataPayment['usertype'] = ADMIN_TYPE;
		   $dataPayment['ipaddress'] = $this->getRealIpAddr();;

		   $sqlPayment = $this->db->insert_string('resellerpayment', $dataPayment);
		   $queryPayment = $this->db->query($sqlPayment);
		   
		   if($idReseller>0)
		   {
			   $sqlUpdate = "UPDATE resellers3 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 Reseller III
 * @copyright 2011
 */


	function reseller2EditInfo($id)
	{
    	$query = $this->db->query("select *from resellers2 where id = '$id' LIMIT 1");
    	if($query->num_rows()>0)
		{
			return $query->result();
		}
    	else
		return 0;
	}
	

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

 function editReseller2($userID)
 {

		$keyvalue		= $this->input->post('id');
		//$data['login']		= $this->input->post('login');

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

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

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


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


		$data['password']		= $this->input->post('password');
		//$data['balance']		= $this->input->post('balance');
              $data['id_reseller'] = $idReseller;
		$data['id_rate']		= $this->input->post('id_rate');
		$data['limit_user']		= $limit_user;
		$data['limit_rate']		= $limit_rate;
		$data['limit_balance']		= $limit_balance;
		$data['limit_fnf']		= $this->input->post('limit_fnf');
		$data['type']		= $this->input->post('type');
		$data['sipuser']		= $this->input->post('sipuser');
		$data['aniuser']		= $this->input->post('aniuser');
		$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');
		$data['user_id']		= $userID;
		$data['usertype']		= ADMIN_TYPE;
		$data['ipaddress']		= $this->getRealIpAddr();

        $where = "id = $keyvalue";
		$sql = $this->db->update_string('resellers2', $data, $where);
		$query = $this->db->query($sql);
 }


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Find check Child Reseller of Reseller II
 * @copyright 2011
 */


function checkChildReseller2($idReseller)
{
    $query = $this->db->query("select *from resellers1 where id_reseller = '$idReseller'");

    if($query->num_rows()>0)
	{
		return 1;
	}
    else
	return 0;
}




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

function deleteReseller2($idReseller)
{
    $sql = "select id_rate from ratename where id_reseller = '$idReseller' AND level = 2";
	$queryRateName = $this->db->query($sql);
	if($queryRateName->num_rows()>0)
	 {
		$result =  $queryRateName->result();
		if(is_array($result))
		{
			foreach($result as $row)
			{
				$idRate = $row->id_rate;
				$sqlDeleteRateChart = "delete from ratechart where id_rate = '$idRate'";
				$queryDeleteRate = $this->db->query($sqlDeleteRateChart);
			}
		}	   
	$query = $this->db->query("delete from ratename where id_reseller = '$idReseller' AND level = 2");	  
   }

    $query = $this->db->query("delete from resellerpayment where id_reseller = '$idReseller' AND level =2");
    $query = $this->db->query("delete from resellers2 where id = '$idReseller'");

    
}



/**
 ***********************************************************************
 * @Start Reseller I Section *******************************************
 ***********************************************************************
*/



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return total number of reseller
 * @copyright 2011
 */
 
 	function getTotalnumResellerLeve1($login,$password,$full_name,$id_rate,$idReseller,$type,$typeValue)
 	{
		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($full_name=="NULL")
		$fullNameStr = "";
		else
		$fullNameStr = "AND full_name like '$full_name%'";

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


		if($type == -1)
		$typeStr = "";
		elseif($type == 0)
		$typeStr = "AND balance = '$typeValue'";
		elseif($type == 1)
		$typeStr = "AND balance > '$typeValue'";
		else
		$typeStr = "AND balance < '$typeValue'";


	   	$sql="SELECT
	   	count(id) as totalrows
	   	FROM resellers1 Where id != '-1' $loginStr $passwordStr $fullNameStr $idRateStr $idResellerStr $typeStr";
	   	//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 Reseller List
 * @copyright 2011
 */
	function resellerLevel1List($startoffset,$limit,$login,$password,$full_name,$id_rate,$idReseller,$type,$typeValue)
	{

		$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($idReseller=="NULL")
		$idResellerStr = "";
		else
		$idResellerStr = "AND a.id_reseller = '$idReseller'";

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

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

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

		if($type == -1)
		$typeStr = "";
		elseif($type == 0)
		$typeStr = "AND a.balance = '$typeValue'";
		elseif($type == 1)
		$typeStr = "AND a.balance > '$typeValue'";
		else
		$typeStr = "AND a.balance < '$typeValue'";


        $sql = "SELECT
				a.id,
				a.login,
				a.password,
				a.id_reseller,
				a.balance,
				a.id_rate,
				a.limit_user,
				a.limit_rate,
				a.limit_balance,
				a.limit_fnf,
				a.commission,
				a.aniuser,
				a.sipuser,
				a.full_name,
				a.status

				FROM resellers1 a
				
				where a.id !=-1 $loginStr $passwordStr $fullNameStr $idRateStr  $idResellerStr $typeStr 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 Reseller I
 * @copyright 2011
 */

 function addReseller1($userID)
 {

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

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

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

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

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

		
		$limit_balance		= $this->input->post('limit_balance');
        if(!is_numeric($limit_balance) OR $limit_balance <= 0)
		$limit_balance = $balance;
		
				
		$data['id_reseller']		= $idReseller;
		$data['login']		= $this->input->post('login');
		$data['password']		= $this->input->post('password');
		$data['balance']		= $balance;
		$data['id_rate']		= $this->input->post('id_rate');
		$data['limit_user']		= $limit_user;
		$data['limit_rate']		= $limit_rate;
		$data['limit_balance']		= $limit_balance;
		$data['limit_fnf']		= $this->input->post('limit_fnf');
		$data['commission']		= $this->input->post('commission');
		$data['type']		= $this->input->post('type');
		$data['sipuser']		= $this->input->post('sipuser');
		$data['aniuser']		= $this->input->post('aniuser');
		$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');
		$data['user_id']		= $userID;
		$data['usertype']		= ADMIN_TYPE;
		$data['ipaddress']		= $this->getRealIpAddr();

		$sql = $this->db->insert_string('resellers1', $data);
		$query = $this->db->query($sql);
		$idInsert = $this->db->insert_id();
		if($idInsert)
		{
		   $dataPayment['id_reseller'] = $idInsert;
		   $dataPayment['level'] = 1;
		   $dataPayment['amount'] = $data['balance'];
		   $dataPayment['type'] = PREPAID;
		   $dataPayment['description'] = 'Initial Balance';
		   $dataPayment['user_id'] = $userID;
		   $dataPayment['usertype'] = ADMIN_TYPE;
		   $dataPayment['ipaddress']		= $this->getRealIpAddr();

		   $sqlPayment = $this->db->insert_string('resellerpayment', $dataPayment);
		   $queryPayment = $this->db->query($sqlPayment);
		   
		   if($idReseller>0)
		   {
			   $sqlUpdate = "UPDATE resellers2 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 Reseller III
 * @copyright 2011
 */

	function reseller1EditInfo($id)
	{
    	$query = $this->db->query("select *from resellers1 where id = '$id' LIMIT 1");
    	if($query->num_rows()>0)
		{
			return $query->result();
		}
    	else
		return 0;
	}
	
 
 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Add Reseller III
 * @copyright 2011
 */

 function editReseller1($userID)
 {

		$keyvalue		= $this->input->post('id');
		//$data['login']		= $this->input->post('login');

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

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

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

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

		
		$limit_balance		= $this->input->post('limit_balance');
        if(!is_numeric($limit_balance) OR $limit_balance <= 0)
		$limit_balance = 0;
		
		$data['id_reseller']		= $idReseller;
		$data['password']		= $this->input->post('password');
		//$data['balance']		= $this->input->post('balance');
		$data['id_rate']		= $this->input->post('id_rate');
		$data['limit_user']		= $limit_user;
		$data['limit_rate']		= $limit_rate;
		$data['limit_balance']		= $limit_balance;
		$data['limit_fnf']		= $this->input->post('limit_fnf');
		$data['commission']		= $commission;
		$data['type']		= $this->input->post('type');
		$data['sipuser']		= $this->input->post('sipuser');
		$data['aniuser']		= $this->input->post('aniuser');
		$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');
		$data['user_id']		= $userID;
		$data['usertype']		= ADMIN_TYPE;
		$data['ipaddress']		= $this->getRealIpAddr();

        $where = "id = $keyvalue";
		$sql = $this->db->update_string('resellers1', $data, $where);
		$query = $this->db->query($sql);
 }




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Find check Child Reseller of Reseller I
 * @copyright 2011
 */


function checkChildReseller1SIP($idReseller)
{
    $query = $this->db->query("select *from clientssip where id_reseller = '$idReseller'");

    if($query->num_rows()>0)
	{
		return 1;
	}
    else
	return 0;
}


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Find check Child Reseller of Reseller I
 * @copyright 2011
 */


function checkChildReseller1PIN($idReseller)
{
    $query = $this->db->query("select *from clientspin where id_reseller = '$idReseller'");

    if($query->num_rows()>0)
	{
		return 1;
	}
    else
	return 0;
}


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Find check Child Reseller of Reseller I
 * @copyright 2011
 */


function checkChildReseller1IP($idReseller)
{
    $query = $this->db->query("select *from clientsip where id_reseller = '$idReseller'");

    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 deleteReseller1($idReseller)
{
    $sql = "select id_rate from ratename where id_reseller = '$idReseller' AND level = 1";
	$queryRateName = $this->db->query($sql);
	if($queryRateName->num_rows()>0)
	 {
		$result =  $queryRateName->result();
		if(is_array($result))
		{
			foreach($result as $row)
			{
				$idRate = $row->id_rate;
				$sqlDeleteRateChart = "delete from ratechart where id_rate = '$idRate'";
				$queryDeleteRate = $this->db->query($sqlDeleteRateChart);
			}
		}	   
	$query = $this->db->query("delete from ratename where id_reseller = '$idReseller' AND level = 1");	  
   }

    $query = $this->db->query("delete from resellerpayment where id_reseller = '$idReseller' AND level =1");
    $query = $this->db->query("delete from resellers1 where id = '$idReseller'");

    
}



}

?>