<?
class cpanelModel extends Model{

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


/**
 ***********************************************************************
 * @Start Cpanel Section ***********************************************
 ***********************************************************************
*/


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return Call Summary List
 * @copyright 2011
 */
	function totalCallSummary($callStart,$callEnd)
	{

       $callStart = $callStart."00:00:00";
       $callEnd = $callEnd." 23:59:59";

        $sql_back = "SELECT 
		        SUBSTRING(callstart, 1, 10) AS callDate,
				COUNT(id_call) AS totalCall, 
				SUM(duration) AS totalDuration 
				FROM calls WHERE call_status = 'ANSWER' AND callstart BETWEEN '$callStart' AND '$callEnd' 
				GROUP BY CallDate ORDER BY callDate DESC";

        $sql = "SELECT 
		        SUBSTRING(callend, 1, 10) AS callDate,
				COUNT(id_call) AS totalCall, 
				SUM(duration) AS totalDuration 
				FROM calls WHERE call_status = 'ANSWER' GROUP BY CallDate ORDER BY callDate DESC LIMIT 7";

			   //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 ASR ACD List
 * @copyright 2011
 */
	function asrCallSummary($callStart)
	{

        $sql = "SELECT 
		        SUBSTRING(callend, 1, 13) AS callDate,
				COUNT(id_call) AS totalSucessCall, 
				SUM(duration) AS totalDuration 
				FROM calls WHERE call_status = 'ANSWER' AND callstart LIKE '$callStart%'
				GROUP BY CallDate ORDER BY callDate DESC";
			   //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 Password Match or Not
 * @copyright 2011
 */
	function checkPassword($userID,$password)
	{

		$sql = "SELECT *FROM admin WHERE id = '$userID' AND password ='$password'";

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






/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return total number of Sucess Call
 * @copyright 2011
 */
 
 	function totalActiveCall()
 	{
        $sql = "SELECT
                count(id) as totalrows								               
				FROM activecall";
	   	
		   //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 Active Call List
 * @copyright 2011
 */
	function activeCallList()
	{		
      $sql = "SELECT
                dialnumber,
                login,
                call_start,
		  		TIMESTAMPDIFF(SECOND,call_start,NOW()) AS duration								               
				FROM activecall
				order by call_start DESC LIMIT 50";

			   //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 total number of Sucess Call
 * @copyright 2011
 */
 
 	function totalRegisterUser()
 	{
		$sql = "SELECT
                count(id) as totalrows								               
				FROM sipusers WHERE clienttype = 1 AND ipaddr !=''";
		   //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 Active Call List
 * @copyright 2011
 */
	function registerUserList()
	{
				
      $sql = "SELECT
                accountcode,
                ipaddr,
                regseconds								               
				FROM sipusers WHERE clienttype = 1 AND ipaddr !='' ORDER BY regseconds ASC LIMIT 50";
			   //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 Active Call List
 * @copyright 2011
 */
	function internalError($callDate)
	{

//id, login, clienttype, call_start, called_number, description
				
      $sql = "SELECT
                login,
                called_number,
                description								               
				FROM failedcalls WHERE call_start LIKE '$callDate%' ORDER BY call_start ASC LIMIT 50";
			   //echo $sql;

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


/**
 ***********************************************************************
 * @End Cpanel Section *************************************************
 ***********************************************************************
*/


/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return Protal Setting Information
 * @copyright 2011
 */
	function profileSettingInfo()
	{

		$sql = "SELECT * FROM billing_setting 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 return Delete Profile Setting
 * @copyright 2011
 */
	function deleteLastProfileInfo()
	{

		$sql = "DELETE FROM billing_setting";
	    $query = $this->db->query($sql);
   }
	

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


	 function insertProfileInfo($fileName,$userID)
 	 {
        
		$this->deleteLastProfileInfo();
		$data['com_name']		= mysql_escape_string($this->input->post('com_name'));
		$data['com_email']		= mysql_escape_string($this->input->post('com_email'));
		$data['com_phone']		= mysql_escape_string($this->input->post('com_phone'));
		$data['com_fax']		= mysql_escape_string($this->input->post('com_fax'));
		$data['com_vat_reg']		= mysql_escape_string($this->input->post('com_vat_reg'));
		$data['com_address']		= mysql_escape_string($this->input->post('com_address'));
		$data['header_title']		= mysql_escape_string($this->input->post('header_title'));
		$data['logo_images']		= $fileName;
		$data['ftp_ip']		= mysql_escape_string($this->input->post('ftp_ip'));
		$data['ftp_user']		= mysql_escape_string($this->input->post('ftp_user'));
		$data['ftp_pass']		= mysql_escape_string($this->input->post('ftp_pass'));
		$data['user_id']		= $userID;
		$data['update_date']		= date('Y-m-d H:i:s');
		$this->db->insert('billing_setting', $data);
			
 	}
 	



/**
 ***********************************************************************
 * @Start Activity Log  Section ****************************************
 ***********************************************************************
*/



/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: Show Total num of User Activity Logs
 * @copyright 20114
 */
 
 
 	function getTotalnumLogs($description,$moduleName,$date1, $date2)
 	{

		if($date1 == 'NULL')
		$date1Str = date('Y-m-d')." 00:00:00";
		else
		$date1Str = $date1." 00:00:00";

		if($date2 == 'NULL')
		$date2Str = date('Y-m-d')." 00:00:00";
		else
		$date2Str = $date2." 00:00:00";
		
		if($description=="NULL")
		$descriptionStr = "";
		else
		$descriptionStr = "AND description like 'description%'";

		if($moduleName=="NULL")
		$moduleNameStr = "";
		else
		$moduleNameStr = "AND  narration like '$moduleName%'";


	   	$sql="SELECT
	   	count(id) as totalrows
	   	FROM userlog 		
		Where date_time BETWEEN '$date1Str' AND '$date2Str' $descriptionStr $moduleNameStr";

		$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 Log List
 * @copyright 2014
 */
	function userLogList($startoffset,$limit,$description,$moduleName,$date1, $date2)
	{
		$new_startoffset = $startoffset - 1;
		if($new_startoffset <0)
		$new_startoffset = 0;
		$startoffset1 = $limit * $new_startoffset;
	    $limit_string = "LIMIT $startoffset1,$limit";

		if($date1 == 'NULL')
		$date1Str = date('Y-m-d')." 00:00:00";
		else
		$date1Str = $date1." 00:00:00";

		if($date2 == 'NULL')
		$date2Str = date('Y-m-d')." 00:00:00";
		else
		$date2Str = $date2." 00:00:00";
		
		if($description=="NULL")
		$descriptionStr = "";
		else
		$descriptionStr = "AND description like 'description%'";

		if($moduleName=="NULL")
		$moduleNameStr = "";
		else
		$moduleNameStr = "AND  narration like '$moduleName%'";

        $sql = "SELECT 
				*FROM userlog
				Where date_time BETWEEN '$date1Str' AND '$date2Str' $descriptionStr $moduleNameStr order by date_time DESC $limit_string";
				
	    $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 DB Back Task Setting Information
 * @copyright 2014
 */
	function dbBackupSettingInfo()
	{

		$sql = "SELECT * FROM task_scheduler 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 Profile Information
 * @copyright 2011
 */


	 function insertDBtask()
 	 {
 		$this->deleteDBtask();
		$data['task_type']		= mysql_escape_string($this->input->post('task_type'));
		$data['task_time']		= mysql_escape_string($this->input->post('task_time'));
		$data['name_prefix']		= mysql_escape_string($this->input->post('name_prefix'));
		$data['calls']		= mysql_escape_string($this->input->post('calls'));
		$this->db->insert('task_scheduler', $data);
			
 	}
 	
 
 
 
/**
 * @author: Md. Golam Rabbany
 * @email: mgrnahid@gmail.com
 * @description: This function return Delete Profile Setting
 * @copyright 2011
 */
	function deleteDBtask()
	{

		$sql = "DELETE FROM task_scheduler";
	    $query = $this->db->query($sql);
   }
	
	
		

	
		 	


}

?>