<?php 

class Master_model extends CI_Model {
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
	
	function insert_record($table_name,$data_array)             //insert data
	{
		$this->db->insert($table_name, $data_array);
		$insert_id = $this->db->insert_id();
        return  $insert_id;		
	}
	function delete_record($table_name,$condition)             //delete data
	{
		$this->db->delete($table_name,$condition); 
	}
	function update_record($table_name,$data_array,$condition)  //update data
	{
		$this->db->update($table_name, $data_array, $condition);
		
	}
	//for get any table data from table-ashish
	
	function get_table_data($parameters){
	
		if(isset($parameters['order']) && isset($parameters['order_by_field'])) 
		{
			$order=$parameters['order'];
			$order_by_field=$parameters['order_by_field'];
		}
		
		$table_name=$parameters['table_name'];
		$table_field=$parameters['table_fields'];
		$this->db->select($table_field);
		
		if(isset($order_by_field) && $order_by_field!='' && isset($order) &&  $order!='')
		{
			$this->db->order_by($order_by_field,$order);
		}
		
		$this->db->from($table_name);
		// $users['users']= $this->db->get()->result_array();
		// return $users['users'];
		return $this->db->get()->result_array();
		
	}
	
	
	
	function unique($parameters)
	{
		$table_name=$parameters['table_name'];
		$table_field=$parameters['table_fields'];
		$condition=$parameters['condition'];
		$limit=$parameters['limit'];
		$this->db->select($table_field);
		$this->db->from($table_name);
		if(!empty($condition))
		{
		
			foreach($condition as $cond_k => $cond_v)
			{
				if(isset($cond_v[3]))
				$this->db->$cond_v[0]($cond_v[1], $cond_v[2],$cond_v[3]);
				else
				$this->db->$cond_v[0]($cond_v[1], $cond_v[2]);
			}
		}
		
		return $this->db->get()->num_rows();
	}
	 function select_data($parameters)						   //listing
	{
		
	//echo '<pre>';print_r($parameters);echo "</pre>";
		$table_name=$parameters['table_name'];
		$table_field=$parameters['table_fields'];
		$condition=$parameters['condition'];
		$limit=$parameters['limit'];
		$offset=$parameters['offset'];
		/////added by pooja//////
		if(isset($parameters['order']) && isset($parameters['order_by_field'])) 
		{
			$order=$parameters['order'];
			$order_by_field=$parameters['order_by_field'];
		}	
		//////
		/////added by hemant//////
		if(isset($parameters['group_by_field']) && $parameters['group_by_field']!='') 
		{
			$group_by_field=$parameters['group_by_field'];
		}	
		//////
		$this->db->select($table_field);
		$this->db->from($table_name);
		// if($condition)
		// 	$this->db->where($condition);
		if(!empty($condition))
		{
			foreach($condition as $cond_k => $cond_v)
			{
				if(isset($cond_v[3]))
				$this->db->$cond_v[0]($cond_v[1], $cond_v[2],$cond_v[3]);
				else
				$this->db->$cond_v[0]($cond_v[1], $cond_v[2]);
			}
		}
		
		/////added by pooja//////
		if(isset($order_by_field) && $order_by_field!='' && isset($order) &&  $order!='')
		{
			$this->db->order_by($order_by_field,$order);
		}	
		//////
		/////added by hemant//////
		if(isset($group_by_field) && $group_by_field!='')
		{
			$this->db->group_by($group_by_field);
		}	
		
		//////
		/////added by hemant//////
		if(isset($limit) && $limit!='')
		{
			$this->db->limit($limit,$offset);
		}
		//dump($this->db->last_query());
		//////
		$query = $this->db->get();
		
		$result = $query->result_array();
		
		return $result;
	}
	function search($search_array) {
		
		$limit=$search_array['limit'];
		$offset=$search_array['offset'];
		$sort_by=$search_array['sort_by'];
		$sort_order=$search_array['sort_order'];
		$sort_columns=$search_array['sort_columns'];
		$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
		//echo $sort_by;
		//echo "xxxxxx".$sort_columns;
		$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : $sort_columns[0];
		if(isset($search_array['date_columns']) && $search_array['date_columns']!='')
		$select_date=$search_array['date_columns'];
		else
		$select_date='';
		// results query
		$q = $this->db->select($search_array['table_fields']);
		$this->db->select($select_date,false);
		$this->db->from($search_array['table_name']);
		//$this->db->limit($limit, $offset);
		////////added by madhuri
		if(isset($limit) && $limit!='')
		{
			$this->db->limit($limit, $offset);
		}	
		if(isset($search_array['sort_by']) && $search_array['sort_by']!='' && isset($search_array['sort_order']) && $search_array['sort_order']!='' )
		{
			$this->db->order_by($sort_by, $sort_order);
		}	
		////////////
		/////added by hemant//////
		if(isset($search_array['group_by_field']) && $search_array['group_by_field']!='')
		{
			$this->db->group_by($search_array['group_by_field']);
		}	
		//////
		
		if(!empty($search_array['cond']))
		{
			foreach($search_array['cond'] as $cond_k => $cond_v)
			{	//echo '<pre>';print_r($search_array['cond']);echo '</pre>';
				if(isset($cond_v[3]))
				$q->$cond_v[0]($cond_v[1], $cond_v[2], $cond_v[3]);
				else
				$q->$cond_v[0]($cond_v[1], $cond_v[2]);
			}
		}
		/*if (strlen($query_array['profit_center_code'])) {
			
		}*/
		
		
		$ret['rows'] = $q->get()->result();
		
		if(isset($search_array['group_by_field']) && $search_array['group_by_field']!='')
		{
			$this->db->group_by($search_array['group_by_field']);
		}	
		/*if(isset($search_array['sort_by']) && $search_array['sort_by']!='')
		{
			$this->db->order_by($sort_by);
		}*/
		$q = $this->db->select('COUNT(*) as count', FALSE)
			->from($search_array['table_name']);
		
		if(!empty($search_array['cond']))
		{
			
			foreach($search_array['cond'] as $cond_k => $cond_v)
			{
				if(isset($cond_v[3]))
				$q->$cond_v[0]($cond_v[1], $cond_v[2], $cond_v[3]);
				else
				$q->$cond_v[0]($cond_v[1], $cond_v[2]);
			}
			
		}
		/*if (strlen($query_array['profit_center_code'])) {
			$q->like('profit_center_code', $query_array['profit_center_code']);
		}*/
		//echo $this->db->_compile_select();
		$tmp = $q->get()->result();
		
		if(isset($tmp) && !empty($tmp))
		$ret['num_rows'] = $tmp[0]->count;
		else
		$ret['num_rows']=0;
		
		return $ret;
	}
	
	function get_query_result($query){
	  if($query != ""){
	   $query = $this->db->query($query);
	   return $query->result_array();
	  }
	 }

	 	function select_datas($parameters)						   //listing
		{
		$table_name=$parameters['table_name']; 
		$table_field=$parameters['table_fields'];
		$condition=$parameters['condition'];
		$limit=$parameters['limit'];
		$offset=$parameters['offset'];
		
		if(isset($parameters['order']) && isset($parameters['order_by_field'])) 
		{
			$order=$parameters['order'];
			$order_by_field=$parameters['order_by_field'];
		}	
		if(isset($parameters['group_by_field']) && $parameters['group_by_field']!='') 
		{
			$group_by_field=$parameters['group_by_field'];
		}	
		$this->db->select($table_field);
		$this->db->from($table_name);
		
		
		if(!empty($condition))
		{
			foreach($condition as $cond_k => $cond_v)
			{
				/* Chnaged by sunita on 20190213 for mysqli and php version 7 update changes*/ 
				$cond_v[0]=strtolower($cond_v[0]);
				if(isset($cond_v[3])){
					if($cond_v[0] == 'join'){
						$this->db->join($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'where'){
						$this->db->where($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'where_in'){
						$this->db->where_in($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'or_where'){
						$this->db->or_where($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'or_where_in'){
						$this->db->or_where_in($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'where_not_in'){
						$this->db->where_not_in($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'like'){
						$this->db->like($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'or_like'){
						$this->db->or_like($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'not_like'){
						$this->db->not_like($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'or_not_like'){
						$this->db->or_not_like($cond_v[1], $cond_v[2],$cond_v[3]);
					}
				}else{
					if($cond_v[0] == 'join'){
						$this->db->join($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'where'){
						$this->db->where($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'where_in'){
						$this->db->where_in($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'or_where'){
						$this->db->or_where($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'or_where_in'){
						$this->db->or_where_in($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'where_not_in'){
						$this->db->where_not_in($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'like'){
						$this->db->like($cond_v[1], $cond_v[2],'both',false);
					}else if($cond_v[0] == 'or_like'){
						$this->db->or_like($cond_v[1], $cond_v[2],'both',false);
					}else if($cond_v[0] == 'not_like'){
						$this->db->not_like($cond_v[1], $cond_v[2],'both',false);
					}else if($cond_v[0] == 'or_not_like'){
						$this->db->or_not_like($cond_v[1], $cond_v[2],'both',false);
					}
				}	
				/* Chnaged by sunita on 20190213 for mysqli and php version 7 update changes*/ 
				// if(isset($cond_v[3]))
				// 	$this->db->$cond_v[0]($cond_v[1], $cond_v[2],$cond_v[3]);
				// else
				// 	$this->db->$cond_v[0]($cond_v[1], $cond_v[2]);
			}
		}
		if(isset($order_by_field) && $order_by_field!='' && isset($order) &&  $order!='')
		{
			$this->db->order_by($order_by_field,$order);
		}	
		if(isset($group_by_field) && $group_by_field!='')
		{
			$this->db->group_by($group_by_field);
		}	
		if(isset($limit) && $limit!='')
		{
			$this->db->limit($limit,$offset);
		}	
		$query = $this->db->get();	
		/* dump($this->db->last_query()); */
		$result = $query->result_array();
		return $result;
	}
 
   


   

}
