<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class Mod_routes extends CI_Model
	{
		var $table = "tblroutes";
		function __construct()
		{
			parent::__construct();
		}	
		
		function getroutes()
		{
			$sql = "select a.*,(select tbllocation.locationname from tbllocation where a.locationfrom = tbllocation.id) as location_from,(select b.locationname from tbllocation as b where a.locationto = b.id) as location_to,(select c.username from tbluser as c where a.createby = c.userid)as username from tblroutes as a";
			return $this->db->query($sql);
			
		}
		
		function comproute($from, $to,$service){
		  if($service == "1"){
			  $this->db->select('*');
        	  $this->db->where('locationfrom', $from);
			  $this->db->where('locationto', $to);
			  $this->db->or_where('locationto', $from);
			  $this->db->where('locationfrom', $to);
        	  $this->db->from('tblroutes');
        	  $data=$this->db->get();
        	  if($data->num_rows()>0){
            	  $userData=$data->row_array();	
			  	  return FALSE;
        	  }else{
            	  return TRUE;
        	  }
		   }else{
			  $this->db->select('*');
			  $this->db->where('service_id', $service);
        	  $this->db->where('locationfrom', $from);
        	  $this->db->from('tblroutes');
        	  $data=$this->db->get();
        	  if($data->num_rows()>0){
            	  $userData=$data->row_array();
			  	  return FALSE;
        	  }else{
            	return TRUE;
        	  }  
		   }	
    	}
		
		
		function editcomproute($id,$from,$to,$service){
		  if($service == "1"){
			  $this->db->select('*');
        	  $this->db->where('locationfrom', $from);
			  $this->db->where('locationto', $to);
			  $this->db->where('id !=', $id);
			  $this->db->or_where('locationto', $from);
			  $this->db->where('locationfrom', $to);
			  $this->db->where('id !=', $id);
        	  $this->db->from('tblroutes');
        	  $data=$this->db->get();
        	  if($data->num_rows()>0){
            	  $userData=$data->row_array();	
			  	  return FALSE;
        	  }else{
            	  return TRUE;
        	  }
		   }else{
			  $this->db->select('*');
			  $this->db->where('service_id', $service);
        	  $this->db->where('locationfrom', $from);
			  $this->db->where('id !=', $id);
        	  $this->db->from('tblroutes');
        	  $data=$this->db->get();
        	  if($data->num_rows()>0){
            	  $userData=$data->row_array();
			  	  return FALSE;
        	  }else{
            	return TRUE;
        	  }  
		   }	
    	}
		
		
		function countAll()
		{
			return $this->db->count_all_results('tblroutes');
		}
		
			
		function add($data)
		{
			$this->db->insert($this->table,$data);
			
		}

		function update($data,$id)
		{
			$this->db->where("id",$id);
			$this->db->update($this->table,$data);
		}
	
		
		function active($id,$data)
		{
			$this->db->where("id",$id);
			$this->db->update($this->table,$data);
		}
		
		
		function getById($id)
		{
			$this->db->where("id",$id);
			$q = $this->db->get($this->table);
			if($q->num_rows() > 0)
			{
				return $q->row();
			}
			return false;
		}
		
		
		function getlocation($ID)
		{	
			$this->db->SELECT("*");
			$this->db->from('tbllocation');
			$this->db->where("id !=",$ID);
			return $this->db->get();
				
		}
			
		
		
	}