<?php 
/*
///////////////////////////////////////////////////////////////////////
/  Project Name : BLUORBIT
/  File Name : master_model.php
/  Added By : SUNITA 
/  Created Date : 13 Feb 2019 	
/  Last Changed By : SUNITA 
/  Last Changed Date : 4 Feb 2019 
/  Purpose: master model
/  ---------
/
////////////////////////////////////////////////////////////////////////
*/
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); 
	// added by snehal on 04-04-19
		$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);
	}
	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);
		return $this->db->get()->result();
	}
	
	
	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)
			{
				/* 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]); */
				/* dump($cond_v); */
				$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);
					}
				}
			}
		}
		//dump($this->db->get());
		return $this->db->get()->num_rows();
	}
	function select_data($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'];
		}
		if(isset($parameters['having_field']) && $parameters['having_field']!='') 
		{
			$having_field=$parameters['having_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($having_field) && $having_field!='') // added by saurabh on 20200412 for multiple vendor assignment
		{
			$this->db->having($having_field);
		}	
		if(isset($limit) && $limit!='')
		{
			$this->db->limit($limit,$offset);
		}	
		$query = $this->db->get();	
		 //dumpEx($this->db->last_query()); 
		$result = $query->result_array();
		return $result;
	}
	function select_all_data($parameters)						   //listing
	{
		//print_r($parameters);exit;
		$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(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();
		//dumpEx($this->db->last_query()); 

		$result = $query->result_array();
		
		//echo "kk";exit;
		//print_r($result);exit;
		return $result;
	}
	function getFieldValue($table,$sel_field,$field,$id)
	{
		$this -> db -> where($field,$id);
		$this -> db -> select($sel_field);
		$this -> db -> from($table);
		$query['arr_data'] = $this -> db -> get()->row_array();
		if(isset($query['arr_data']) && ($query['arr_data']!='') && !empty($query['arr_data']))
		{
			return $query['arr_data'][$sel_field];
		}
		else
		{
			return '';
		}
	}
	
	function search($search_array,$print_qry=FALSE) {
		
		$sort_multiple_columns='';
		$distinct='';
		$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';
		
		if(isset($search_array['sort_multiple_columns']) && $search_array['sort_multiple_columns']!='')
		{
			$sort_multiple_columns=$search_array['sort_multiple_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='';
		
		if(isset($search_array['distinct']) && $search_array['distinct']!='')
		{
			//$this->db->distinct();
		}
		$q = $this->db->select($search_array['table_fields']);
		$this->db->select($select_date,false);
		$this->db->from($search_array['table_name']);
		
		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);
			if(isset($search_array['sort_multiple_columns']) && $search_array['sort_multiple_columns']!='')
			{
				$this->db->order_by($sort_multiple_columns, $sort_order);
			}
		}	
		
		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['having_field']) && $search_array['having_field']!='') // added by saurabh on 20200412 for multiple vendor assignment
		{
			$this->db->having($search_array['having_field']);
		}	
		
		if(!empty($search_array['cond']))
		{
			foreach($search_array['cond'] as $cond_k => $cond_v)
			{
				/* Chnaged by sunita on 20190213 for mysqli and php version 7 update changes*/ 
				/* 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]); */
				//dump($cond_v);
				$cond_v[0]=strtolower($cond_v[0]);
				if(isset($cond_v[3])){
					if($cond_v[0] == 'join'){
						$q->join($cond_v[1], $cond_v[2],$cond_v[3]);
					}
					else if($cond_v[0] == 'left_join')
					{
						$q->join($cond_v[1], $cond_v[2],$cond_v[3],'left');
					}
					else if($cond_v[0] == 'where')
					{
						$q->where($cond_v[1], $cond_v[2],$cond_v[3]);
					}
					else if($cond_v[0] == 'where_in')
					{
						$q->where_in($cond_v[1], $cond_v[2],$cond_v[3]);
					}
					else if($cond_v[0] == 'or_where'){
						$q->or_where($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'or_where_in'){
						$q->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'){
						$q->like($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'or_like'){
						$q->or_like($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'not_like'){
						$q->not_like($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'or_not_like'){
						$q->or_not_like($cond_v[1], $cond_v[2],$cond_v[3]);
					}
				}else{
					if($cond_v[0] == 'join'){
						$q->join($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'left_join'){
						$q->join($cond_v[1], $cond_v[2],'left');
					}else if($cond_v[0] == 'where'){
						$q->where($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'where_in'){
						$q->where_in($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'or_where'){
						$q->or_where($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'or_where_in'){
						$q->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'){
						$q->like($cond_v[1], $cond_v[2],'both',false);
					}else if($cond_v[0] == 'or_like'){
						$q->or_like($cond_v[1], $cond_v[2],'both',false);
					}else if($cond_v[0] == 'not_like'){
						$q->not_like($cond_v[1], $cond_v[2],'both',false);
					}else if($cond_v[0] == 'or_not_like'){
						$q->or_not_like($cond_v[1], $cond_v[2],'both',false);
					}
				}
			}
		}
		
		$ret['rows'] = $q->get()->result();
		if($print_qry === TRUE && ENVIRONMENT == 'development') {
			dump($this->db->last_query());
		}
		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['having_field']) && $search_array['having_field']!='') // added by saurabh on 20200412 for multiple vendor assignment
		{
			$this->db->having($search_array['having_field']);
		}
		
		
		// if(isset($search_array['distinct']) && $search_array['distinct']!='')
		// {
			// $q = $this->db->select('COUNT(DISTINCT(account_no)) as count', FALSE)
			// ->from($search_array['table_name']);
		// }
		if(isset($search_array['distinct']) && $search_array['distinct']!='')
		{
			$q = $this->db->select('COUNT(DISTINCT('.$search_array['distinct'].')) as count', FALSE)
			->from($search_array['table_name']);
		}
		else if(isset($search_array['having_field']) && $search_array['having_field']!='')
		{
			$q = $this->db->select($search_array['table_fields'], FALSE)
			->from($search_array['table_name']);
		}
		else
		{
			$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)
			{
				/* Chnaged by sunita on 20190213 for mysqli and php version 7 update changes*/ 
				/* 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]); */
				$cond_v[0]=strtolower($cond_v[0]);
				if(isset($cond_v[3])){
					if($cond_v[0] == 'join'){
						$q->join($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'left_join'){
						$q->join($cond_v[1], $cond_v[2],$cond_v[3],'left');
					}else if($cond_v[0] == 'where'){
						$q->where($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'where_in'){
						$q->where_in($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'or_where'){
						$q->or_where($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'or_where_in'){
						$q->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'){
						$q->like($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'or_like'){
						$q->or_like($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'not_like'){
						$q->not_like($cond_v[1], $cond_v[2],$cond_v[3]);
					}else if($cond_v[0] == 'or_not_like'){
						$q->or_not_like($cond_v[1], $cond_v[2],$cond_v[3]);
					}
				}else{
					if($cond_v[0] == 'join'){
						$q->join($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'left_join'){
						$q->join($cond_v[1], $cond_v[2],'left');
					}else if($cond_v[0] == 'where'){
						$q->where($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'where_in'){
						$q->where_in($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'or_where'){
						$q->or_where($cond_v[1], $cond_v[2]);
					}else if($cond_v[0] == 'or_where_in'){
						$q->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'){
						$q->like($cond_v[1], $cond_v[2],'both',false);
					}else if($cond_v[0] == 'or_like'){
						$q->or_like($cond_v[1], $cond_v[2],'both',false);
					}else if($cond_v[0] == 'not_like'){
						$q->not_like($cond_v[1], $cond_v[2],'both',false);
					}else if($cond_v[0] == 'or_not_like'){
						$q->or_not_like($cond_v[1], $cond_v[2],'both',false);
					}
				}
			}
		}
		
		$tmp = $q->get()->result();
		
		if(isset($tmp) && !empty($tmp))
		{
			//added by saurabh on 20190923 for group_by count issue
			if(isset($search_array['group_by_field']) && $search_array['group_by_field']!='')
			{
				$ret['num_rows'] = count($tmp);
			}else{
				$ret['num_rows'] = $tmp[0]->count;	
			}
			
			// $ret['num_rows'] = $tmp[0]->count;	commented by saurabh on 20190923 for group_by count issue
			
		}
		else
		$ret['num_rows']=0;
		return $ret;
		
	}
	function get_autocomplete_response($options = array())
    {
		if(empty($options))
		return false;
		if(strlen($options['lookup_value'])<=0)
		return false;
		$select_table_fields=$options['select_table_fields'];
		$lookup_table_fields=$options['lookup_table_fields'];
		$lookup_value=$options['lookup_value'];
		$lookup_table_name=$options['lookup_table_name'];
		
		$active_column_name=$options['active_column_name'];
		$type_column_name=$options['type_column_name'];
        $this->db->select($select_table_fields);
		if(!empty($options['lookup_table_fields']))
		{
			$cond_part='';
			$cond_part.= $options['lookup_table_fields'].' like "%'.$lookup_value.'%"'; 
			$this->db->where($cond_part);
		}
		
		if(strlen($options['type_column_name'])>0){
			if(isset($options['type_column_value']) && $options['type_column_value']!='')
			{
				$this->db->where_in($options['type_column_name'],$options['type_column_value']);
			}
			else
			{
				$this->db->where($options['type_column_name'],'Working');
			}
		}
		if(strlen($options['active_column_name'])>0)
			$this->db->where($options['active_column_name'],'yes');
        $query = $this->db->get($lookup_table_name);
        return $query->result_array();

    }
	
	
	function get_query_result($query){
		if($query != ""){
			$query = $this->db->query($query);
			return $query->result_array();
		}
	}
	
	function compile_select(){
		return $this->db->_compile_select();
	}
	
	function reset_select(){
		$this->db->_reset_select();
	}
	
	function trucate_table($table_name){
		$this->db->empty_table($table_name);
	}

	public function get_last_route_order_id($route_id)
	{
		$query = $this->db->query("SELECT route_order_id FROM route_details WHERE route_id = $route_id ORDER BY route_details_id DESC LIMIT 1");
		$result = $query->result_array();

		// echo "<pre>";
		// print_r($result);exit;
		if (empty($result)) {
			$route_order_id = 1;
		}
		else{
			$route_order_id = $result[0]['route_order_id'] + 1;
		}
		
		return $route_order_id;
	}
	
}