<?
class rateModel extends Model{

        function rateModel()
        {
                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;
}



/**
 ***********************************************************************
 * @Start Rate Name Section ********************************************
 ***********************************************************************
*/



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return total number of Rate
 * @copyright 2011
 */
 
 	function getTotalnumRate($description,$level)
 	{
		if($description=="NULL")
		$descriptionStr = "";
		else
		$descriptionStr = "AND description like '$description%'";

		if($level=="NULL")
		$levelStr = "";
		else
		$levelStr = "AND level = '$level'";

	   	$sql="SELECT
	   	count(id_rate) as totalrows
	   	FROM ratename Where id_rate != '-1' $descriptionStr $levelStr";
		$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 Rate Name List
 * @copyright 2011
 */
	function rateList($startoffset,$limit,$description,$level)
	{

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

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

		if($level=="NULL")
		$levelStr = "";
		else
		$levelStr = "AND level = '$level'";


        $sql = "SELECT
				id_rate,
				description,
				id_reseller,
				creationdate,
				level
				
				FROM ratename

				where id_rate !=-1 $descriptionStr $levelStr 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 Rate Name
 * @copyright 2011
 */

 function addRate($userID)
 {

		$dataRate['description']		= $this->input->post('description');
		$dataRate['id_reseller']		= -1;
		$dataRate['level']		= 0;

		$sql = $this->db->insert_string('ratename', $dataRate);
		$query = $this->db->query($sql);
		$idInsert = $this->db->insert_id();
 }
 
 
 
 
 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Return Edit Infomation of Rate
 * @copyright 2011
 */

	function rateEditInfo($id)
	{
    	$query = $this->db->query("select *from ratename where id_rate = '$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 Edit Rate Chart Name
 * @copyright 2011
 */

 function editRate()
 {

		$keyvalue		= $this->input->post('id_rate');
		$dataRate['description']		= $this->input->post('description');	
        $where = "id_rate = $keyvalue";
		$sql = $this->db->update_string('ratename', $dataRate, $where);
		$query = $this->db->query($sql);
		
 }


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


function deleteRate($idRate)
{
    $query = $this->db->query("delete from ratechart where id_rate = '$idRate'");
    $query = $this->db->query("delete from ratename where id_rate = '$idRate'");

}



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

	function rateChartInfo($id)
	{
    	$query = $this->db->query("select *from ratechart where id_rate = '$id'");
    	if($query->num_rows()>0)
		{
			return $query->result();
		}
    	else
		return 0;
	}
	

/**
 ***********************************************************************
 * @Start Rate Chart Section *******************************************
 ***********************************************************************
*/


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return total number of Rate Chart
 * @copyright 2011
 */
 
 	function getTotalnumRateChart($idRate,$description,$prefix,$grace_period,$resolution)
 	{
		if($description=="NULL")
		$descriptionStr = "";
		else
		$descriptionStr = "AND description like '$description%'";

		if($prefix=="NULL")
		$perfixStr = "";
		else
		$perfixStr = "AND prefix LIKE '$prefix%'";

		if($grace_period=="NULL")
		$greacePeriodStr = "";
		else
		$greacePeriodStr = "AND grace_period = '$grace_period'";

		if($resolution=="NULL")
		$resolutionStr = "";
		else
		$resolutionStr = "AND resolution = '$resolution'";

	   	$sql="SELECT
	   	count(id) as totalrows
	   	FROM ratechart Where id_rate = '$idRate' $descriptionStr $perfixStr $greacePeriodStr $resolutionStr";
		$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 Rate Chart List
 * @copyright 2011
 */
	function rateChartList($startoffset,$limit,$idRate,$description,$prefix,$grace_period,$resolution)
	{

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

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

		if($prefix=="NULL")
		$perfixStr = "";
		else
		$perfixStr = "AND prefix LIKE '$prefix%'";

		if($grace_period=="NULL")
		$greacePeriodStr = "";
		else
		$greacePeriodStr = "AND grace_period = '$grace_period'";

		if($resolution=="NULL")
		$resolutionStr = "";
		else
		$resolutionStr = "AND resolution = '$resolution'";

        $sql = "SELECT *
				FROM ratechart

				 Where id_rate = '$idRate' $descriptionStr $perfixStr $greacePeriodStr $resolutionStr 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 Rate Chart
 * @copyright 2011
 */

	 function addRateChart($idRate,$userID)
 	 {

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

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

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

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

		//$data['id']		= $this->input->post('id');
		$data['id_rate']		= $idRate;
		$data['prefix']		= $this->input->post('prefix');
		$data['description']		= $this->input->post('description');
		$data['voice_rate']		= $voice_rate;
		$data['rate_mult']		= $rate_mult;
		$data['grace_period']		= $grace_period;
		$data['minimal_time']		= $minimal_time;
		$data['resolution']		= $resolution;
		$data['is_disable']		= $this->input->post('is_disable');
		$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('ratechart', $data);
		$query = $this->db->query($sql);
		$idInsert = $this->db->insert_id();
 	}
 
 
 
  /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function Return Edit Infomation of Rate Chart
 * @copyright 2011
 */

	function rateChartEditInfo($id,$idRate)
	{
    	$query = $this->db->query("select *from ratechart where id = '$id' AND id_rate ='$idRate' LIMIT 1");
    	if($query->num_rows()>0)
		{
			return $query->result();
		}
    	else
		return 0;
	}



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

 function editRateChart($userID)
 {

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

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

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

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

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


		//$data['id_rate']		= $idRate;
		$data['prefix']		= $this->input->post('prefix');
		$data['description']		= $this->input->post('description');
		$data['voice_rate']		= $voice_rate;
		$data['rate_mult']		= $rate_mult;
		$data['grace_period']		= $grace_period;
		$data['minimal_time']		= $minimal_time;
		$data['resolution']		= $resolution;
		$data['is_disable']		= $this->input->post('is_disable');
		$data['user_id']		= $userID;
		$data['usertype']		= ADMIN_TYPE;
		$data['ipaddress']		= $this->getRealIpAddr();
		
        $where = "id = $keyvalue";
		$sql = $this->db->update_string('ratechart', $data, $where);
		$query = $this->db->query($sql);
		
 }
 
 	
 /**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Delete information of Rate Name
 * @copyright 2011
 */

	function deleteRateChart($idRate,$id)
	{
    	$query = $this->db->query("delete from ratechart where id_rate = '$idRate' and id='$id'");
	}


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return Rate Chart List
 * @copyright 2011
 */
	function exportSearchRateChartList($idRate,$description,$prefix,$grace_period,$resolution)
	{

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

		if($prefix=="NULL")
		$perfixStr = "";
		else
		$perfixStr = "AND prefix LIKE '$prefix%'";

		if($grace_period=="NULL")
		$greacePeriodStr = "";
		else
		$greacePeriodStr = "AND grace_period = '$grace_period'";

		if($resolution=="NULL")
		$resolutionStr = "";
		else
		$resolutionStr = "AND resolution = '$resolution'";

        $sql = "SELECT *
				FROM ratechart

				 Where id_rate = '$idRate' $descriptionStr $perfixStr $greacePeriodStr $resolutionStr order by prefix ASC";

			   //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 Rate Chart Prefix
 * @copyright 2011
 */

	function checkRatePrefix($idRate,$prefix)
	{
    	$query = $this->db->query("select *from ratechart where id_rate = '$idRate' AND prefix = '$prefix'");
    	if($query->num_rows()>0)
		{
		    $query = $this->db->query("delete from ratechart where id_rate = '$idRate' and prefix='$prefix'");
		}
	}
		



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return total number of Rate Plan
 * @copyright 02 Jun 2014
 */
 

 	function getNumRatePlan($idRate,$prefix,$codeType,$startTime,$endTime)
 	{

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

		if($prefix=="NULL")
		$perfixStr = "";
		else
		$perfixStr = "AND prefix LIKE '$prefix%'";

		if($startTime=="NULL")
		$startTimeStr = date('Y-m-d H:i:s');
		else
		$startTimeStr = urldecode($startTime).":00"; 

		if($endTime=="NULL")
		$endTimeStr = date('Y-m-d H:i:s');
		else
		$endTimeStr = urldecode($endTime).":00"; 
	   	
		$sql="SELECT
	   	COUNT(id) as totalRow
	   	FROM feature_tariffplan WHERE code_type = '$codeType' 
		AND effective_date BETWEEN '$startTimeStr' AND '$endTimeStr' AND status = 0
		$idRateStr $perfixStr";
		$row = 0;
		$query = $this->db->query($sql);
		foreach($query->result() as $resultrow)
		{
			$row = $resultrow->totalRow;
		}
		return $row;

	}




/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return List of Plan
 * @copyright 02 Jun 2014
 */
 
	function ratePlanList($startoffset,$limit,$idRate,$prefix,$codeType,$startTime,$endTime)
	{

		
		$new_startoffset = $startoffset - 1;
		if($new_startoffset <0)
		$new_startoffset = 0;
		$startoffset1 = $limit * $new_startoffset;
   		$limit_string = "LIMIT $startoffset1,$limit";
    
		if($idRate=="NULL")
		$idRateStr = "";
		else
		$idRateStr = "AND id_rate = '$idRate%'";

		if($prefix=="NULL")
		$perfixStr = "";
		else
		$perfixStr = "AND prefix LIKE '$prefix%'";

		if($startTime=="NULL")
		$startTimeStr = date('Y-m-d H:i:s');
		else
		$startTimeStr = urldecode($startTime).":00"; 

		if($endTime=="NULL")
		$endTimeStr = date('Y-m-d H:i:s');
		else
		$endTimeStr = urldecode($endTime).":00"; 
		

		$sql = "SELECT * FROM feature_tariffplan WHERE code_type = '$codeType' AND status = 0 
		AND effective_date BETWEEN '$startTimeStr' AND '$endTimeStr'  $idRateStr $perfixStr 
		ORDER BY effective_date ASC, status ASC $limit_string";
			
			  //echo $sql;
	    $query = $this->db->query($sql);
	    if($query->num_rows()>0)
		  return $query->result();
	    else
		 return 0;
	}
	
	



}

?>