<?php defined('BASEPATH') or exit('No direct Access is Allowed'); ?>
<?php
class Coupon_categorymodel extends CI_Model{

	public function __construct() {
        parent::__construct();
    }
	public function coupon_category_list($limit, $offset){
		
		$offset = ($offset-1)*$limit;
		$query=$this->db->select(['*'])->from('coupon_category')
		//->order_by("coupon_category", "asc")
    	->limit($limit, $offset)
		->get();
		//echo $this->db->last_query();die;
		return $query->result();
	}
	
	public function num_rows(){
	
		$user_id = $this->session->userdata('user_id');
		$query=$this->db->select(['*'])->from('coupon_category')->get();
		return $query->num_rows();
	}
	public function insert_coupon_category($data_coupon_category){
	
		$this->db->insert('coupon_category', $data_coupon_category);
		$data_coupon_category['id'] = $this->db->insert_id();
    	return $data_coupon_category['id'];	
	}
	
	public function update_coupon_category($data_coupon_category,$list_id){
	
		$this->db->where('id',$list_id);
		$this->db->update('coupon_category', $data_coupon_category);
		return true;
	}
	
	public function view_coupon_category($list_id){
	
		$query=$this->db->select(['*'])->from('coupon_category')->where('id',$list_id)->get();
		return $query->row();
	}
	public function count_coupon_category($coupon_category){
	
		$query=$this->db->select(['*'])->from('coupon_category')->where('coupon_category',$coupon_category)->get();
		return $query->num_rows();
	}
	
	public function delete_coupon_category($list_id){
	
		$this->db->where('id', $list_id);
		$this->db->delete('coupon_category');
	}
	function getclass(){
	    $response = array();
	    // Select record
	    $this->db->select('*');
	    $q = $this->db->get('class');
	    $response = $q->result_array();
	    
	    return $response;
	}
	
	// Get City departments
	function get_class_subject($postData){
	    $response = array();
	    
	    // Select record
	    $this->db->select('id,subject');
	    $this->db->where('class_id', $postData['classes']);
	    $q = $this->db->get('subject');
	    $response = $q->result_array();
	    
	    return $response;
	}
	
	// Get Department user
	function get_class_subject_faculty($postData){
	    // echo "<pre>";print_r($postData['subject']);die;
	    $response     = array();
	    $this->db->select('*');
	    $this->db->where('subject_id', $postData['subject']);
	    $q            = $this->db->get('v2_faculty');
	    $response     = $q->result_array();
	    return $response;
	}
	
	
	public function search($data_taskpo){
	    
	    $condition	=	$data_taskpo['seach_term'];
	    $order		=	$data_taskpo['order_by'];
	    $query=$this->db->select(['coupon_category.*'])
	        ->from('coupon_category');
	        
	        if($condition=='or' && !empty($order)){
	            
	            if(!empty($data_taskpo['coupon_category'])){
	                $this->db->or_like('coupon_category.id',$data_taskpo['coupon_category'])->order_by('coupon_category.coupon_category',$order);
	            }
	            if(!empty($data_taskpo['coupon_category_code'])){
	                $this->db->or_like('coupon_category.coupon_category_code'	,$data_taskpo['coupon_category_code'])
	                ->order_by('coupon_category.coupon_category_code',$order);
	            }
	        }
	        
	        if($condition=='or' && empty($order)){
	            
	            if(!empty($data_taskpo['coupon_category'])){
	                $this->db->or_like('coupon_category.id'	,$data_taskpo['coupon_category'])		;
	            }
	            if(!empty($data_taskpo['coupon_category_code'])){
	                $this->db->or_like('coupon_category.coupon_category_code'	,$data_taskpo['coupon_category_code'])		;
	            }
	        }
	        
	        if($condition=='and' && !empty($order)){
	            
	            if(!empty($data_taskpo['coupon_category']))	{
	                $this->db->like('coupon_category.id',$data_taskpo['coupon_category'])->order_by('coupon_category.coupon_category'	,$order);
	            }
	            if(!empty($data_taskpo['coupon_category_code'])){
	                $this->db->like('coupon_category.coupon_category_code',$data_taskpo['coupon_category_code'])	->order_by('coupon_category.coupon_category_code'		,$order);
	            }
	        }
	        
	        if($condition=='and' && empty($order)){
	            
	            if(!empty($data_taskpo['coupon_category']))	{
	                $this->db->like('coupon_category.id',$data_taskpo['coupon_category']);
	            }
	            if(!empty($data_taskpo['coupon_category_code']))		{
	                $this->db->like('coupon_category.coupon_category_code',$data_taskpo['coupon_category_code'])	;
	            }            
	        }
	        
	        $query=$this->db->get();
	        //echo $this->db->last_query();die;
	        if($query->num_rows()>0){
	            return $query->result();
	        }
	        else{
	            return $query->result();
	        }
	}
}
?>
