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

	class Mod_peaktime extends CI_Model
	{
		var $table = "tblpeaktime";
		function __construct()
		{
			parent::__construct();
		}	
		
		function getpeaktime ()
		{
			$sql = "select a.*,(select tbllocation.locationname from tbllocation where a.from_location = tbllocation.id) as locationfrom,(select b.locationname from tbllocation as b where a.to_location = b.id) as locationto,(SELECT c.fullname from tblcar as c where a.typecar=c.id)as carname,(SELECT d.username from tbluser as d where a.createby = d.userid )as username,(SELECT e.typename from tblservices as e where a.service_id = e.id )as servicename from tblpeaktime as a";
			return $this->db->query($sql);
			
		}
		
		function compeaktime($from,$to,$service,$car){
		  if($service == 1){
			  $this->db->select('*');
        	  $this->db->where('from_location', $from);
        	  $this->db->where('to_location', $to);
			  $this->db->or_where('to_location', $from);
			  $this->db->where('from_location', $to);
        	  $this->db->from('tblpeaktime');
        	  $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('typecar',$car);
        	  $this->db->where('from_location', $from);
        	  $this->db->from('tblpeaktime');
        	  $data = $this->db->get();
        	  	if($data->num_rows()>0){
            	  $userData=$data->row_array();
			  	  return FALSE;
        	  	}else{
            		return TRUE;
        	  	}  
		   }	
    	}
		 
		 
		function editcompeaktime($id,$from,$to,$service,$car){
		  if($service == 1){
			  $this->db->select('*');
        	  $this->db->where('from_location', $from);
        	  $this->db->where('to_location', $to);
			  $this->db->or_where('to_location', $from);
			  $this->db->where('from_location', $to);
			  $this->db->where('id', $id);
        	  $this->db->from('tblpeaktime');
        	  $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('typecar',$car);
        	  $this->db->where('from_location', $from);
			  $this->db->where('id', $id);
        	  $this->db->from('tblpeaktime');
        	  $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('tblpeaktime');
		}
		
			
		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 delete($id)
		{
			$this->db->where("id",$id);
			$this->db->delete($this->table);
		}
		
	
		function getById($id)
		{
			$this->db->where("id",$id);
			$q = $this->db->get($this->table);
			if($q->num_rows() > 0)
			{
				return $q->row();
			}
			return false;
		}
		
			
		
		
	}