<?
class FailedcallModel extends Model{

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


/**
 ***********************************************************************
 * @Start Failed Call Section ******************************************
 ***********************************************************************
*/



/**
 * @author: Md. Sabbir Ali
 * @email: sabbircsecuet@gmail.com
 * @description: This function return total number of Failed Call
 * @copyright 2011
 */
 
 	function getTotalnumFailedCall($call_start,$call_end,$login,$called_number)
 	{
		if($call_start=="NULL")
		$call_startStr = date('Y-m-d')." 00:00:00";
		else
		$call_startStr = $call_start." 00:00:00";

		if($call_end=="NULL")
		$call_endStr = date('Y-m-d')." 23:59:59";
		else
		$call_endStr = $call_end." 00:00:00";

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

		if($called_number=="NULL")
		$called_numberStr = "";
		else
		$called_numberStr = "AND a.called_number LIKE '$called_number%'";

        $sql = "SELECT COUNT(a.id) as totalrows FROM failedcalls a WHERE a.id != '-1' AND a.call_start BETWEEN '$call_startStr' AND '$call_endStr'   $loginStr $called_numberStr";
				 
	   	//echo $sql;
		$row = 0;
		$query = $this->db->query($sql);
		foreach($query->result() as $resultrow)
		{
			$row = $resultrow->totalrows;
		}
		return $row;
	}
	

/**
 * @author: Md. Sabbir Ali
 * @email: sabbircsecuet@gmail.com
 * @description: This function return Failed Call List
 * @copyright 2011
 */
	function failedCallList($startoffset,$limit,$call_start,$call_end,$login,$called_number)
	{

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

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

		if($call_end=="NULL")
		$call_endStr = date('Y-m-d')." 23:59:59";
		else
		$call_endStr = $call_end." 00:00:00";
		


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

        if($called_number=="NULL")
        $called_numberStr = "";
        else
        $called_numberStr = "AND a.called_number LIKE '$called_number%'";



        $sql = "SELECT * FROM failedcalls a WHERE a.id != '-1'AND a.call_start BETWEEN '$call_startStr' AND '$call_endStr' $loginStr $called_numberStr ORDER BY a.call_start DESC $limit_string";

			   //echo $sql;

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