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

class Mod_reservation extends CI_Model{
	
		var $table = "tblreservation";
		var $tblprice = "tblprice";
		var $customer = "tblreservation_detail";
		var $peaktime = "tblpeaktime";
		function __construct()
		{
			parent::__construct();
		}	
			
		function add($data)
		{
			$this->db->insert($this->table,$data);
			
		}
		
		function getById($id)
		{
			if($id=="")
			{
				redirect("home");
			}
			$q =$this->db->query("select a.*,(select tblcar.images from tblcar where a.brand_id = tblcar.brandname group by `brandname`) as imagename, (select b.locationname from tbllocation as b where a.pickup_location = b.id) as pickupname,(SELECT c.locationname from tbllocation as c where a.return_location = c.id)as dropOffname,(select tblcar.fullname from tblcar where a.brand_id = tblcar.brandname group by `brandname`)as carname,(select tblservices.typename from tblservices where a.services_id = tblservices.id)as servicename from tblbooking as a where `trn_id`=".$id."");
			
			if($q->num_rows() > 0)
			{
				return $q->row();
			}
			return false;
		}
		
		function getprice($RID,$brand_id,$service)
		{
			$this->db->where("brand_id",$brand_id);
			$this->db->where("service_id",$service);
			$this->db->where("RID",$RID);
			
			$q = $this->db->get($this->tblprice);
			if($q->num_rows() > 0)
			{
				return $q->row();
			}
			return false;	
		}
		
		function gethalf($RID,$brand_id,$service)
		{
			$this->db->where("brand_id",$brand_id);
			$this->db->where("service_id",$service);
			$this->db->where("RID",$RID);
			
			$q = $this->db->get($this->tblprice);
			if($q->num_rows() > 0)
			{
				return $q->row();
			}
			return false;	
		}
		
		function customer($data)
		{
			$this->db->insert($this->customer,$data);
			
		}
		
		function reserve($data)
		{
			$this->db->insert($this->table,$data);
			
		}
		
		
		function getpeaktime($service,$from_time,$to_time,$to_location,$from_location,$brand_id){
			//$q = $this->db->query("SELECT * FROM `tblpeaktime` WHERE (`from_location` = '".$from_location."' AND `to_location`='".$to_location."' OR `to_location` ='".$from_location."' AND `from_location`='".$to_location."') AND (`from_time` BETWEEN '".$from_time."' AND '".$to_time."') AND (`to_time` BETWEEN '".$from_time."' AND '".$to_time."') AND `typecar`=".$brand_id."");
			$q =  $this->db->query("SELECT * FROM `tblpeaktime` WHERE (`from_location` = '".$from_location."' AND `to_location`='".$to_location."' OR `to_location` ='".$from_location."' AND `from_location`='".$to_location."') AND (`from_time` <= '".$from_time."' AND `to_time` >= '".$to_time."') AND `typecar`=".$brand_id." AND `service_id`=".$service."");
			
			if($q->num_rows() > 0)
			{
				return $q->row();
			}
			return false;	
		
		}
		
		function complete($id)
		{
			if($id=="")
			{
				redirect("home");	
			}
			
			$q =$this->db->query("select a.*,(select tblcar.images from tblcar where a.brand_id = tblcar.brandname group by `brandname`) as imagename, (select b.locationname from tbllocation as b where a.pickup_location = b.id) as pickupname,(SELECT c.locationname from tbllocation as c where a.return_location = c.id)as dropOffname,(select tblcar.fullname from tblcar where a.brand_id = tblcar.brandname group by `brandname`)as carname,(select tblservices.typename from tblservices where a.services_id = tblservices.id)as servicename from tblbooking as a where `trn_id`=".$id."");
			
			if($q->num_rows() > 0)
			{
				return $q->row();
			}
			return false;
		}
		
		
		
	}