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

class Administrator extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->helper('url');
		$this->load->database();
		$this->load->model('Process');
		$this->load->model('Vehicle_Model');
		$this->load->model('Report_Model');
		$this->load->helper('custom_helper');	
		error_reporting(E_ALL & ~E_NOTICE);
		ini_set('memory_limit', "1024M");
	}
	//--------------------LogOut-----------------// 
	public function LogOut()
	{
		$adminid = $this->session->userdata('adminid');
		$result = $this->Process->logoutupdate($adminid);
		$this->session->unset_userdata('adminid');
		redirect('Login/');
	}
	//--------------------Change Password-----------------// 
	public function UploadLog()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$this->load->view('upload_log');
	}
	//--------------------Change Password-----------------// 
	public function ChangePassword()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$this->load->view('change_pass');
	}
	//--------------------Dashboard-----------------// 
	public function Dashboard()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$data['busquery'] = $this->Report_Model->WeeklyReport('Bus');
		$data['cabquery'] = $this->Report_Model->WeeklyReport('Cab');
		$this->load->view('dashboard',$data);
	}
	//--------------------Add Vehicle Details-----------------// 
	public function AddBus()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$this->load->view('AddBusDetails');
	}
	public function AddCab()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$this->load->view('AddCabsDetails');
	}
	//--------------------View Buses List-----------------// 
	public function BusesList()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$vehicletype = 'Bus';
		$data['query'] = $this->Vehicle_Model->vehiclelist($vehicletype);
		$this->load->view('BusesList',$data);
	}
	//--------------------View Cabs List-----------------// 
	public function CabsList()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$vehicletype = 'Cab';
		$data['query'] = $this->Vehicle_Model->vehiclelist($vehicletype);
		$this->load->view('CabsList',$data);
	}
	//--------------------View Vehicle Details-----------------// 
	public function VehicleDetails($vehicleid)
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$data['query'] = $this->Vehicle_Model->vehicledetails($vehicleid);
		$this->load->view('VehicleDetails',$data);
	}
	public function EditBusDetails($vehicleid)
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Administrator/');
		}
		$data['query'] = $this->Vehicle_Model->vehicledetails($vehicleid);
		$this->load->view('EditBusDetails',$data);
	}
	public function EditCabDetails($vehicleid)
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Administrator/');
		}
		$data['query'] = $this->Vehicle_Model->vehicledetails($vehicleid);
		$this->load->view('EditCabDetails',$data);
	}
	
	//--------------------View Log List-----------------// 
	public function logSearch()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		if(isset($_POST['btnLogUpdate'])){
			$tag_no = $this->input->post('tag_no');
			$VehicleNumber = $this->input->post('VehicleNumber');
			$from = $this->input->post('from');
			$to = $this->input->post('to');
			$data['data'] = array($tag_no,$VehicleNumber,$from,$to);
			$this->load->view('LogList',$data);
		}
		else{
			$this->load->view('logSearch');
		}
	}
	public function AddLog()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$this->load->view('AddLog');
	}
	public function LogList()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		//$vehicletype = 'Bus';
		$data['query'] = $this->Vehicle_Model->LogEntryList();
		$this->load->view('LogList',$data);
	}
	public function EditLogDetails($logid)
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Administrator/');
		}
		$data['query'] = $this->Vehicle_Model->EditLogDetails($logid);
		$this->load->view('EditLogDetails',$data);
	}
	public function LogProcess()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		if(isset($_POST['btnLogAdd']))
		{
				$result = $this->Vehicle_Model->LogAdd();
				if($result)
				{
					
					$this->session->set_flashdata('flsh_msg', 'Entry details added sucessfully');
					redirect('Administrator/logSearch/');
				}
				else
				{
					$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
					redirect("Administrator/AddLog/");
				}
		}
		if(isset($_POST['btnLogUpdate']))
		{
			$tag_no = $this->input->post('tag_no1');
			$VehicleNumber = $this->input->post('VehicleNumber1');
			$from = $this->input->post('from1');
			$to = $this->input->post('to1');
			
			$logid = $this->input->post('logid');
			
			$result = $this->Vehicle_Model->Logeedit($logid);
			if($result)
			{
				$this->session->set_flashdata('flsh_msg', 'Details edit sucessfully');
				redirect("Administrator/LogList/$logid/$tag_no/$from/$to/$VehicleNumber");
			}
			else
			{
				$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
				redirect("Administrator/EditLogDetails/$logid/$tag_no/$from/$to/$VehicleNumber");
			}
		}	
	}
	public function DeleteLog($logid)
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$logid = $this->uri->segment(3);
		
		$tag_no = $this->uri->segment(4);
		$from = $this->uri->segment(5);
		$to = $this->uri->segment(6);
		$VehicleNumber = $this->uri->segment(7);
		
		$data['query'] = $this->Vehicle_Model->DeleteLog($logid);
		$this->session->set_flashdata('flsh_msg', 'Entry deleted sucessfully');
		redirect("Administrator/LogList/$logid/$tag_no/$from/$to/$VehicleNumber");
	}
	
	//--------------------Vehicle-----------------// 
	public function Vehicleprocess()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		if(isset($_POST['btnBusAdd']))
		{
			$vehicletype = 'Bus';
			$result = $this->Vehicle_Model->AddVehicle($vehicletype);
				if($result)
				{
					$update_by = $this->session->userdata('adminid');
					$update_type = 'S';
					$update_tab = 'V';
					$update_id = $this->Vehicle_Model->GetMaxId('vehicle_id','tbl_vehicle_master');
					$result = $this->Vehicle_Model->CreateLog($update_by,$update_type,$update_tab,$update_id);
					
					$this->session->set_flashdata('flsh_msg', 'Vehicle details added sucessfully');
					redirect('Administrator/BusesList');
				}
				else
				{
					$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
					redirect("Administrator/AddBus/");
				}
		}
		if(isset($_POST['btnBusUpdate']))
		{
			$vehicleid = $this->input->post('vehicle_id');
			$result = $this->Vehicle_Model->vehicleedit($vehicleid);
				if($result)
				{
					$update_by = $this->session->userdata('adminid');
					$update_type = 'E';
					$update_tab = 'V';
					$update_id = $vehicleid;
					$result = $this->Vehicle_Model->CreateLog($update_by,$update_type,$update_tab,$update_id);
					
					$this->session->set_flashdata('flsh_msg', 'Vehicle details edit sucessfully');
					redirect('Administrator/BusesList');
				}
				else
				{
					$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
					redirect("Administrator/EditBusDetails/$vehicleid");
				}
		}
		if(isset($_POST['btnCabAdd']))
		{
			$vehicletype = 'Cab';
			$result = $this->Vehicle_Model->AddVehicle($vehicletype);
				if($result)
				{
					$update_by = $this->session->userdata('adminid');
					$update_type = 'S';
					$update_tab = 'V';
					$update_id = $this->Vehicle_Model->GetMaxId('vehicle_id','tbl_vehicle_master');
					$result = $this->Vehicle_Model->CreateLog($update_by,$update_type,$update_tab,$update_id);
					
					$this->session->set_flashdata('flsh_msg', 'Vehicle details added sucessfully');
					redirect('Administrator/CabsList');
				}
				else
				{
					$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
					redirect("Administrator/AddCab/");
				}
		}
		if(isset($_POST['btnCabUpdate']))
		{
			$vehicleid = $this->input->post('vehicle_id');
			$result = $this->Vehicle_Model->vehicleedit($vehicleid);
				if($result)
				{
					$update_by = $this->session->userdata('adminid');
					$update_type = 'E';
					$update_tab = 'V';
					$update_id = $vehicleid;
					$result = $this->Vehicle_Model->CreateLog($update_by,$update_type,$update_tab,$update_id);
					
					$this->session->set_flashdata('flsh_msg', 'Vehicle details edit sucessfully');
					redirect('Administrator/CabsList');
				}
				else
				{
					$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
					redirect("Administrator/EditCabDetails/$vehicleid");
				}
		}
	}
	
	//--------------------View Bus Report-----------------// 
	public function BusDayReport()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$vehicletype = 'Bus';
		$data['result'] = $this->Vehicle_Model->vehiclelist($vehicletype);
		if(isset($_POST['showresult']))
		{
			$data['query'] = $this->Report_Model->FilterBusReport($vehicletype);
			$this->load->view('BusDayReport',$data);
		}
		elseif(isset($_POST['savereport']))
		{
			$contents="Tag Id, Route No., Route Name, Zone, Fuel Type, Kms -To & Fro, Seating Capacity, Vendor Name, Vehicle Number, Time In, Time Out, Model, Registration Date, Insurance Expiry Date, Pollution Expiry Date, Fitness, Permit\n";
			$data['query'] = $this->Report_Model->FilterBusReport($vehicletype);
			foreach ($data['query'] as $row)
			{
				if(($row->time_out == '2010-01-01 01:01:01') || ($row->time_out == '2009-12-31 19:31:01') || ($row->time_out == '')){
					$timeout1 =  "AUTO OUT";
				} else {
					$timeout1 = $row->time_out;
				} 
				$route_name = str_replace(",","",trim($row->route_name));
				$vender = str_replace(",","",trim($row->vender));
				$contents.=$row->tag_no.",";
				$contents.=$row->route_no.",";
				$contents.=$route_name.",";
				$contents.=$row->zone.",";
				$contents.=$row->fuel_type.",";
				$contents.=$row->km_to_fro.",";
				$contents.=$row->seating_capacity.",";
				$contents.=$vender.",";
				$contents.=$row->vehicle_number.",";
				$contents.=$row->time_in.",";
				$contents.=$timeout1.",";
				$contents.=$row->Vehicle_model.",";
				$contents.=$row->registration_date.",";
				$contents.=$row->insurance_expiry_date.",";
				$contents.=$row->pollution_expiry_date.",";
				$contents.=$row->fitness.",";
				$contents.=$row->permit."\n";
			}
			$contents = strip_tags($contents);
			$name_for_project = "BusDayReports";
			header("content-disposition: attachment;filename=".$name_for_project.".csv");
			print $contents;
		}
		else
		{
			$data['query'] = $this->Report_Model->BusDayReport($vehicletype);
			$this->load->view('BusDayReport',$data);
		}
	}
	public function BusReport()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$vehicletype = 'Bus';
		$data['result'] = $this->Vehicle_Model->vehiclelist($vehicletype);
		if(isset($_POST['showresult']))
		{
			$data['from'] = array($this->input->post('from'),$this->input->post('to'));
			$data['query'] = $this->Report_Model->FilterReport($vehicletype);
			$this->load->view('BusReport',$data);
		}
		elseif(isset($_POST['savereport']))
		{
			$contents="Tag Id, Route No., Route Name, Zone, Fuel Type, Kms -To & Fro, Seating Capacity, Vendor Name, Vehicle Number, Rounds in Month, Model, Registration Date, Insurance Expiry Date, Pollution Expiry Date, Fitness, Permit\n";
			$data['query'] = $this->Report_Model->FilterReport($vehicletype);
			
			$fromdate = $this->input->post('from');
			$todate = $this->input->post('to');
			if(($fromdate !="") && ($todate !="")){
				$create_from=date_create($fromdate);
				$from = date_format($create_from,'Y-m-d');
				$create_to=date_create($todate);
				$to = date_format($create_to,'Y-m-d');
				$curdate = date('m-Y');
			}
			else{
				$from = "novalue";
				$to = "novalue";
				$curdate = date('m-Y');
			}
			
			foreach ($data['query'] as $row)
			{
				$totalround = round_in_month($row->tag_id,$curdate,$from,$to);
				$count = $totalround['totalround'];
				$route_name = str_replace(",","",trim($row->route_name));
				$vender = str_replace(",","",trim($row->vender));

				$contents.=$row->tag_no.",";
				$contents.=$row->route_no.",";
				$contents.=$route_name.",";
				$contents.=$row->zone.",";
				$contents.=$row->fuel_type.",";
				$contents.=$row->km_to_fro.",";
				$contents.=$row->seating_capacity.",";
				$contents.=$vender.",";
				$contents.=$row->vehicle_number.",";
				$contents.=$count.",";
				$contents.=$row->Vehicle_model.",";
				$contents.=$row->registration_date.",";
				$contents.=$row->insurance_expiry_date.",";
				$contents.=$row->pollution_expiry_date.",";
				$contents.=$row->fitness.",";
				$contents.=$row->permit."\n";
				//----------------Relationship-------------------//
				$tag = Relationship($row->tag_id)['0'];
				$result = Relationship($row->tag_id)['1'];	
				foreach ($result as $fatchresult)
				{
					$totalround = round_in_month($fatchresult->tag_id,$curdate,$from,$to);
					$count = $totalround['totalround'];
					$route_name = str_replace(",","",trim($row->route_name));
					$vender = str_replace(",","",trim($row->vender));
					
					$contents.=$tag.",";
					$contents.=$row->route_no.",";
					$contents.=$route_name.",";
					$contents.=$row->zone.",";
					$contents.=$row->fuel_type.",";
					$contents.=$row->km_to_fro.",";
					$contents.=$row->seating_capacity.",";
					$contents.=$vender.",";
					$contents.=$row->vehicle_number.",";
					$contents.=$count.",";
					$contents.=$row->Vehicle_model.",";
					$contents.=$row->registration_date.",";
					$contents.=$row->insurance_expiry_date.",";
					$contents.=$row->pollution_expiry_date.",";
					$contents.=$row->fitness.",";
					$contents.=$row->permit."\n";
				}
				//----------------Relationship-------------------//
			}
			$contents = strip_tags($contents);
			$name_for_project = "BusReport";
			header("content-disposition: attachment;filename=".$name_for_project.".csv");
			print $contents;
		}
		else
		{
			$data['query'] = $this->Report_Model->BusReport($vehicletype);
			$this->load->view('BusReport',$data);
		}
	}
	public function IndivisualBusReport($tagid)
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$tagid = trim($this->uri->segment(3));
		$data['tagid'] = $tagid;
		$data['query'] = $this->Report_Model->VehicleBusReport($tagid);
		$this->load->view('IndivisualBusReport',$data);
	}
	public function IndivisualBusReports()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		if(isset($_POST['showresult']))
		{
			$data['from'] = array($this->input->post('from'),$this->input->post('to'));
			$tagid = trim($this->input->post('tag_id'));
			$data['tagid'] = $tagid;
			$data['query'] = $this->Report_Model->VehicleBusReportByDate();
			$this->load->view('IndivisualBusReport',$data);
		}
		elseif(isset($_POST['savereport']))
		{
			$contents="Tag Id, Time In, Time Out, Vehicle Number, Insurance Expiry Date, Pollution Expiry Date, Zone, Model, Registration Date, Fitness, Permit, Route No., Route Name, Vendor Name\n";
			$data['query'] = $this->Report_Model->VehicleBusReportByDate();
			foreach ($data['query'] as $row)
			{
				
				$Dates = strtotime($row->time_in);
				$Date = date('Y-m-d', $Dates);
				$route_result = bus_route($row->vehicle_id,$row->time_in);
				if($route_result[1] == 'till' ){
					$route_no = $route_result[0]['route_no'];
					
					$ci =& get_instance();
					$ci->load->database();
					$query = $ci->db->query("SELECT route_name FROM `tbl_vehicle_master` WHERE  route_no = '".$route_no."'");
					$fatchrow = $query->row_array();
					$route_name =  $fatchrow['route_name'];
				}
				else if($route_result == "noresult"){
					$route_no = $row->route_no;
					$route_name = $row->route_name;
				}
				else{
					$route_no = $route_result['new_route_no'];
					$route_name = $row->route_name;
				}
				
				if(($row->time_out == '2010-01-01 01:01:01') || ($row->time_out == '2009-12-31 19:31:01') || ($row->time_out == '')){
					$timeout1 =  "AUTO OUT";
				} else {
					$timeout1 = $row->time_out;
				}
				
				$contents.=$row->tag_no.",";
				$contents.=$row->time_in.",";
				$contents.=$timeout1.",";
				$contents.=$row->vehicle_number.",";
				$contents.=$row->insurance_expiry_date.",";
				$contents.=$row->pollution_expiry_date.",";
				$contents.=$row->zone.",";
				$contents.=$row->Vehicle_model.",";
				$contents.=$row->registration_date.",";
				$contents.=$row->fitness.",";
				$contents.=$row->permit.",";
				$contents.=$route_no.",";
				$contents.=$route_name.",";
				$contents.=$row->vender."\n";
			}
			//----------------Relationship-------------------//
			$tagid = $row->tag_id;
			$fromdate = trim($this->input->post('from'));
			$todate = trim($this->input->post('to'));
			$vehicletype = "Bus";
			
			//get main CodeIgniter object
			$ci =& get_instance();
			//load databse library
			$ci->load->database();
			$query = $ci->db->query("select tag, tag_id, ntag, ntag_id from tbl_relationship where ntag_id='".$tagid."'");
			$fatchResult = $query->result();
			$totalrow = $query->num_rows();
			if($totalrow != "0"){
				foreach($fatchResult as $fatchRow){
					$curdate = date('m-Y');
					$sql = "SELECT tag_id, time_in, time_out FROM log WHERE tag_id = '".$fatchRow->tag_id."'";
					if((trim($fromdate) != "") && (trim($todate) != ""))
					{
						$sql .= " AND DATE_FORMAT(time_in, '%Y-%m-%d') BETWEEN '".trim($fromdate)."' and '".trim($todate)."'"; 
					}
					else
					{
						$sql .= " AND DATE_FORMAT( time_in, '%m-%Y' ) = '".$curdate."'";
					}
					$query = $ci->db->query($sql);
					$result = $query->result();	
					foreach($result as $datarow)
					{
						$i++;
						$query = $ci->db->query("select * from tbl_vehicle_master where tag_id = '".$tagid."'");
						$row = $query->row_array();
						
						if(($row->time_out == '2010-01-01 01:01:01') || ($row->time_out == '2009-12-31 19:31:01') || ($row->time_out == '') ){
							$timeout1 =  "AUTO OUT";
						} else {
							$timeout1 = $datarow->time_out;
						}
						$contents.=$fatchRow->tag.",";
						$contents.=$datarow->time_in.",";
						$contents.=$timeout1.",";
						$contents.=$row['vehicle_number'].",";
						$contents.=$row['insurance_expiry_date'].",";
						$contents.=$row['pollution_expiry_date'].",";
						$contents.=$row['zone'].",";
						$contents.=$row['Vehicle_model'].",";
						$contents.=$row['registration_date'].",";
						$contents.=$row['fitness'].",";
						$contents.=$row['permit'].",";
						$contents.=$row['route_no'].",";
						$contents.=$row['route_name'].",";
						$contents.=$row['vender']."\n";
					}
				}	
			}
			
			//----------------Relationship-------------------//
			$contents = $contents;
			$name_for_project = "IndivisualBusReports";
			header("content-disposition: attachment;filename=".$name_for_project.".csv");
			print $contents;
		}
	}
	
	public function BusReportRoute()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		//$data['from'] = array($this->input->post('from'),$this->input->post('to'),$this->input->post('vehicleid'));
		//$this->load->view('BusReportRoute', $data);
		if(isset($_POST['savereport'])){
			$contents="Tag Id, Time In, Time Out, Vehicle Number, Insurance Expiry Date, Pollution Expiry Date, Zone, Model, Registration Date, Fitness, Permit, Route No., Route Name, Vendor Name\n";	
			
			$f = $this->input->post('from');
			$t = $this->input->post('to');
			$result = BusRoute();
			
			foreach ($result as $row){
				$ci =& get_instance();
				$ci->load->database();
				$query = $ci->db->query("select * from tbl_route where route_no='".$row->route_no."'");
				$fatchresult = $query->result();
				$totalrow = $query->num_rows();
				if($totalrow != 0){
					foreach ($fatchresult as $fatchrow){
						$vehicleid = $fatchrow->vehicle_id;
						$change_date = $fatchrow->change_date;
						
						$query = $ci->db->query("SELECT route_name FROM `tbl_vehicle_master` WHERE  route_no = '".$fatchrow->route_no."'");
						$fatchdate = $query->row_array();
						$route_name =  $fatchdate['route_name'];
						
						$sql = "SELECT log.id, log.tag_id, log.time_in, log.time_out, tbl_vehicle_master.vehicle_id, tbl_vehicle_master.tag_no, tbl_vehicle_master.tag_id, tbl_vehicle_master.alias_tag, tbl_vehicle_master.vehicle_number, tbl_vehicle_master.insurance_expiry_date, tbl_vehicle_master.pollution_expiry_date, tbl_vehicle_master.charges, tbl_vehicle_master.route_no, tbl_vehicle_master.route_name, tbl_vehicle_master.zone, tbl_vehicle_master.fuel_type, tbl_vehicle_master.km_to_fro, tbl_vehicle_master.seating_capacity, tbl_vehicle_master.acutal_seating_capacity, tbl_vehicle_master.cab_type, tbl_vehicle_master.vender, tbl_vehicle_master.Vehicle_model, tbl_vehicle_master.registration_date, tbl_vehicle_master.fitness, tbl_vehicle_master.permit, tbl_vehicle_master.driver_name, tbl_vehicle_master.contact_number, tbl_vehicle_master.license_number, tbl_vehicle_master.status
						FROM log, tbl_vehicle_master
						WHERE tbl_vehicle_master.vehicle_type = 'Bus'
						AND (log.tag_id = tbl_vehicle_master.tag_id or log.tag_id = tbl_vehicle_master.alias_tag)";
						
						if(($f != "") && ($t != "")){
							$from = date("$f 00:00:00");
							$to = date("$t 23:59:59");
							$sql .= " AND (DATE_FORMAT( log.time_in, '%Y-%m-%d %H:%i:%s' ) BETWEEN '".trim($from)."' and '".trim($change_date)."')";
						}								
						else{
							$sql .= " AND DATE_FORMAT( log.time_in, '%Y-%m-%d %H:%i:%s' ) BETWEEN '".$current_day_start."' and '".$current_day_end."'";
						}
						$sql .= " AND tbl_vehicle_master.vehicle_id = '".$vehicleid."'";
						$query = $ci->db->query($sql);
						$result = $query->result();
						foreach($result as $datarow){
							if($datarow->time_out == '2010-01-01 01:01:01'){
								$timeout1 =  "AUTO OUT";
							} else {
								$timeout1 = $datarow->time_out;
							}
							$contents.=$datarow->tag_no.",";
							$contents.=$datarow->time_in.",";
							$contents.=$timeout1.",";
							$contents.=$datarow->vehicle_number.",";
							$contents.=$datarow->insurance_expiry_date.",";
							$contents.=$datarow->pollution_expiry_date.",";
							$contents.=$datarow->zone.",";
							$contents.=$datarow->Vehicle_model.",";
							$contents.=$datarow->registration_date.",";
							$contents.=$datarow->fitness.",";
							$contents.=$datarow->permit.",";
							$contents.=$fatchrow->route_no.",";
							$contents.=$route_name.",";
							$contents.=$datarow->vender."\n";
						}
					}
					foreach ($fatchresult as $fatchrow){
						$change_date = $fatchrow->change_date;
						
						$sql = "SELECT log.id, log.tag_id, log.time_in, log.time_out, tbl_vehicle_master.vehicle_id, tbl_vehicle_master.tag_no, tbl_vehicle_master.tag_id, tbl_vehicle_master.alias_tag, tbl_vehicle_master.vehicle_number, tbl_vehicle_master.insurance_expiry_date, tbl_vehicle_master.pollution_expiry_date, tbl_vehicle_master.charges, tbl_vehicle_master.route_no, tbl_vehicle_master.route_name, tbl_vehicle_master.zone, tbl_vehicle_master.fuel_type, tbl_vehicle_master.km_to_fro, tbl_vehicle_master.seating_capacity, tbl_vehicle_master.acutal_seating_capacity, tbl_vehicle_master.cab_type, tbl_vehicle_master.vender, tbl_vehicle_master.Vehicle_model, tbl_vehicle_master.registration_date, tbl_vehicle_master.fitness, tbl_vehicle_master.permit, tbl_vehicle_master.driver_name, tbl_vehicle_master.contact_number, tbl_vehicle_master.license_number, tbl_vehicle_master.status
						FROM log, tbl_vehicle_master
						WHERE tbl_vehicle_master.vehicle_type = 'Bus'
						AND (log.tag_id = tbl_vehicle_master.tag_id or log.tag_id = tbl_vehicle_master.alias_tag)";
						if($change_date >= $from){
							if(($f != "") && ($t != "")){
							$from = date("$f 00:00:00");
							$to = date("$t 23:59:59");
							$sql .= " AND (DATE_FORMAT( log.time_in, '%Y-%m-%d %H:%i:%s' ) BETWEEN '".trim($change_date)."' and '".trim($to)."')";
							}
						}
						else if($change_date <= $from){
							if(($f != "") && ($t != "")){
							$from = date("$f 00:00:00");
							$to = date("$t 23:59:59");
							$sql .= " AND (DATE_FORMAT( log.time_in, '%Y-%m-%d %H:%i:%s' ) BETWEEN '".trim($from)."' and '".trim($to)."')";
							}
						}								
						else{
							$sql .= " AND DATE_FORMAT( log.time_in, '%Y-%m-%d %H:%i:%s' ) BETWEEN '".$current_day_start."' and '".$current_day_end."'";
						}
						$sql .= " AND tbl_vehicle_master.route_no = '".$fatchrow->route_no."'";
						$query = $ci->db->query($sql);
						$result1 = $query->result();
						foreach($result1 as $datarow1){
							if(($row->time_out == '2010-01-01 01:01:01') || ($row->time_out == '2009-12-31 19:31:01') || ($row->time_out == '') ){
								$timeout1 =  "AUTO OUT";
							} else {
								$timeout1 = $datarow1->time_out;
							}
							$contents.=$datarow1->tag_no.",";
							$contents.=$datarow1->time_in.",";
							$contents.=$timeout1.",";
							$contents.=$datarow1->vehicle_number.",";
							$contents.=$datarow1->insurance_expiry_date.",";
							$contents.=$datarow1->pollution_expiry_date.",";
							$contents.=$datarow1->zone.",";
							$contents.=$datarow1->Vehicle_model.",";
							$contents.=$datarow1->registration_date.",";
							$contents.=$datarow1->fitness.",";
							$contents.=$datarow1->permit.",";
							$contents.=$datarow1->route_no.",";
							$contents.=$datarow1->route_name.",";
							$contents.=$datarow1->vender."\n";
						}
					}
				}
				else{
					$query = $ci->db->query("SELECT route_name, vehicle_id FROM `tbl_vehicle_master` WHERE  route_no = '".$row->route_no."'");
					$fatchdata = $query->row_array();
					$route_name =  $fatchdata['route_name'];
					$vehicle_id =  $fatchdata['vehicle_id'];
					
					$sql = "SELECT log.id, log.tag_id, log.time_in, log.time_out, tbl_vehicle_master.vehicle_id, tbl_vehicle_master.tag_no, tbl_vehicle_master.tag_id, tbl_vehicle_master.alias_tag, tbl_vehicle_master.vehicle_number, tbl_vehicle_master.insurance_expiry_date, tbl_vehicle_master.pollution_expiry_date, tbl_vehicle_master.charges, tbl_vehicle_master.route_no, tbl_vehicle_master.route_name, tbl_vehicle_master.zone, tbl_vehicle_master.fuel_type, tbl_vehicle_master.km_to_fro, tbl_vehicle_master.seating_capacity, tbl_vehicle_master.acutal_seating_capacity, tbl_vehicle_master.cab_type, tbl_vehicle_master.vender, tbl_vehicle_master.Vehicle_model, tbl_vehicle_master.registration_date, tbl_vehicle_master.fitness, tbl_vehicle_master.permit, tbl_vehicle_master.driver_name, tbl_vehicle_master.contact_number, tbl_vehicle_master.license_number, tbl_vehicle_master.status
					FROM log, tbl_vehicle_master
					WHERE tbl_vehicle_master.vehicle_type = 'Bus'
					AND (log.tag_id = tbl_vehicle_master.tag_id or log.tag_id = tbl_vehicle_master.alias_tag)";
					if(($f != "") && ($t != ""))
					{
						$from = date("$f 00:00:00");
						$to = date("$t 23:59:59");
						$sql .= " AND DATE_FORMAT( log.time_in, '%Y-%m-%d %H:%i:%s' ) BETWEEN '".trim($from)."' and '".trim($to)."'";
					}								
					else
					{
						$sql .= " AND DATE_FORMAT( log.time_in, '%Y-%m-%d %H:%i:%s' ) BETWEEN '".$current_day_start."' and '".$current_day_end."'";
					}
					$sql .= " AND tbl_vehicle_master.vehicle_id = '".$vehicle_id."'";
					$query = $ci->db->query($sql);
					$result = $query->result();
					foreach($result as $datarow){
						if(($row->time_out == '2010-01-01 01:01:01') || ($row->time_out == '2009-12-31 19:31:01') || ($row->time_out == '') ){
								$timeout1 =  "AUTO OUT";
							} else {
								$timeout1 = $datarow->time_out;
							}
							$contents.=$datarow->tag_no.",";
							$contents.=$datarow->time_in.",";
							$contents.=$timeout1.",";
							$contents.=$datarow->vehicle_number.",";
							$contents.=$datarow->insurance_expiry_date.",";
							$contents.=$datarow->pollution_expiry_date.",";
							$contents.=$datarow->zone.",";
							$contents.=$datarow->Vehicle_model.",";
							$contents.=$datarow->registration_date.",";
							$contents.=$datarow->fitness.",";
							$contents.=$datarow->permit.",";
							$contents.=$datarow->route_no.",";
							$contents.=$datarow->route_name.",";
							$contents.=$datarow->vender."\n";
					}
				}
			}
			$contents = $contents;
			//print_r($contents);
			$name_for_project = "IndivisualBusReports";
			header("content-disposition: attachment;filename=".$name_for_project.".csv");
			print $contents;
		}
		else{
			$data['from'] = array($this->input->post('from'),$this->input->post('to'),$this->input->post('vehicleid'));
			$this->load->view('BusReportRoute', $data);
		}
	}
	
	public function IndivisualBusRoute($Routeno)
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$Routeno = trim($this->uri->segment(3));
		$data['Routeno'] = $Routeno;
		//$data['query'] = $this->Report_Model->VehicleBusReport($Routeno);
		$this->load->view('IndivisualBusRoute',$data);
	}
	public function IndivisualBusRoute1()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		if(isset($_POST['showresult']))
		{
			$data['from'] = array($this->input->post('from'),$this->input->post('to'));
			$tagid = trim($this->input->post('tag_id'));
			$data['tagid'] = $tagid;
			$data['query'] = $this->Report_Model->VehicleBusReportByDate();
			$this->load->view('IndivisualBusReport',$data);
		}
		elseif(isset($_POST['savereport']))
		{
			$contents="Tag Id, Time In, Time Out, Vehicle Number, Insurance Expiry Date, Pollution Expiry Date, Zone, Model, Registration Date, Fitness, Permit, Route No., Route Name, Vendor Name\n";
			$data['query'] = $this->Report_Model->VehicleBusReportByDate();
			foreach ($data['query'] as $row)
			{
				
				$Dates = strtotime($row->time_in);
				$Date = date('Y-m-d', $Dates);
				$route_result = bus_route($row->vehicle_id,$row->time_in);
				if($route_result[1] == 'till' ){
					$route_no = $route_result[0]['route_no'];
					
					$ci =& get_instance();
					$ci->load->database();
					$query = $ci->db->query("SELECT route_name FROM `tbl_vehicle_master` WHERE  route_no = '".$route_no."'");
					$fatchrow = $query->row_array();
					$route_name =  $fatchrow['route_name'];
				}
				else if($route_result == "noresult"){
					$route_no = $row->route_no;
					$route_name = $row->route_name;
				}
				else{
					$route_no = $route_result['new_route_no'];
					$route_name = $row->route_name;
				}
				
			if(($row->time_out == '2010-01-01 01:01:01') || ($row->time_out == '2009-12-31 19:31:01') || ($row->time_out == '') ){
					$timeout1 =  "AUTO OUT";
				} else {
					$timeout1 = $row->time_out;
				}
				
				$contents.=$row->tag_no.",";
				$contents.=$row->time_in.",";
				$contents.=$timeout1.",";
				$contents.=$row->vehicle_number.",";
				$contents.=$row->insurance_expiry_date.",";
				$contents.=$row->pollution_expiry_date.",";
				$contents.=$row->zone.",";
				$contents.=$row->Vehicle_model.",";
				$contents.=$row->registration_date.",";
				$contents.=$row->fitness.",";
				$contents.=$row->permit.",";
				$contents.=$route_no.",";
				$contents.=$route_name.",";
				$contents.=$row->vender."\n";
			}
			//----------------Relationship-------------------//
			$tagid = $row->tag_id;
			$fromdate = trim($this->input->post('from'));
			$todate = trim($this->input->post('to'));
			$vehicletype = "Bus";
			
			//get main CodeIgniter object
			$ci =& get_instance();
			//load databse library
			$ci->load->database();
			$query = $ci->db->query("select tag, tag_id, ntag, ntag_id from tbl_relationship where ntag_id='".$tagid."'");
			$fatchResult = $query->result();
			$totalrow = $query->num_rows();
			if($totalrow != "0"){
				foreach($fatchResult as $fatchRow){
					$curdate = date('m-Y');
					$sql = "SELECT tag_id, time_in, time_out FROM log WHERE tag_id = '".$fatchRow->tag_id."'";
					if((trim($fromdate) != "") && (trim($todate) != ""))
					{
						$sql .= " AND DATE_FORMAT(time_in, '%Y-%m-%d') BETWEEN '".trim($fromdate)."' and '".trim($todate)."'"; 
					}
					else
					{
						$sql .= " AND DATE_FORMAT( time_in, '%m-%Y' ) = '".$curdate."'";
					}
					$query = $ci->db->query($sql);
					$result = $query->result();	
					foreach($result as $datarow)
					{
						$i++;
						$query = $ci->db->query("select * from tbl_vehicle_master where tag_id = '".$tagid."'");
						$row = $query->row_array();
						
						if(($row->time_out == '2010-01-01 01:01:01') || ($row->time_out == '2009-12-31 19:31:01') || ($row->time_out == '') ){
							$timeout1 =  "AUTO OUT";
						} else {
							$timeout1 = $datarow->time_out;
						}
						$contents.=$fatchRow->tag.",";
						$contents.=$datarow->time_in.",";
						$contents.=$timeout1.",";
						$contents.=$row['vehicle_number'].",";
						$contents.=$row['insurance_expiry_date'].",";
						$contents.=$row['pollution_expiry_date'].",";
						$contents.=$row['zone'].",";
						$contents.=$row['Vehicle_model'].",";
						$contents.=$row['registration_date'].",";
						$contents.=$row['fitness'].",";
						$contents.=$row['permit'].",";
						$contents.=$row['route_no'].",";
						$contents.=$row['route_name'].",";
						$contents.=$row['vender']."\n";
					}
				}	
			}
			
			//----------------Relationship-------------------//
			$contents = $contents;
			$name_for_project = "IndivisualBusReports";
			header("content-disposition: attachment;filename=".$name_for_project.".csv");
			print $contents;
		}
	}
	
	
	
	//--------------------View Cab Report-----------------// 
	public function CabDayReport()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$vehicletype = 'Cab';
		$data['result'] = $this->Vehicle_Model->vehiclelist($vehicletype);
		if(isset($_POST['showresult']))
		{
			$data['query'] = $this->Report_Model->FilterCabReportDay($vehicletype);
			$this->load->view('CabDayReport',$data);
		}
		elseif(isset($_POST['savereport']))
		{
			$contents="Tag Id, Date, Vehicle Number, Vehicle Type, Time In, Time Out, Total Stay Time, Vendor Name,  Model, Registration Date, Insurance Expiry Date, Pollution Expiry Date, Fitness, Permit.\n";
			$data['query'] = $this->Report_Model->FilterCabReportDay($vehicletype);
			foreach ($data['query'] as $row)
			{
				if(($row->time_out == '2010-01-01 01:01:01') || ($row->time_out == '2009-12-31 19:31:01') || ($row->time_out == '') ){	
					$timeoutd =  "AUTO OUT";
				} else {
					$timeoutd = $row->time_out;
				} 
				
				$Dates = strtotime($row->time_in);
				$Date = date('Y-m-d', $Dates);
				$contents.=$row->tag_no.",";
				$contents.=$Date.",";
				$contents.=$row->vehicle_number.",";
				$contents.=$row->cab_type.",";
				$contents.=$row->time_in.",";
				$contents.=$timeoutd.",";
				
				$time_in = new DateTime($row->time_in);
				$time_out = new DateTime($row->time_out);
				$difference = $time_in->diff($time_out);
				if(($row->time_out == '2010-01-01 01:01:01') || ($row->time_out == '2009-12-31 19:31:01') || ($row->time_out == '') ){	
					$time_difference = "--";	
				}
				else{	
					$time_difference = format_interval($difference);	
				}
				$contents.=$time_difference.",";
				
				$contents.=$row->vender.",";
				$contents.=$row->Vehicle_model.",";
				$contents.=$row->registration_date.",";
				$contents.=$row->insurance_expiry_date.",";
				$contents.=$row->pollution_expiry_date.",";
				$contents.=$row->fitness.",";
				$contents.=$row->permit."\n";
			}
			$contents = strip_tags($contents);
			$name_for_project = "CabDayReports";
			header("content-disposition: attachment;filename=".$name_for_project.".csv");
			print $contents;
		}
		else
		{
			$data['query'] = $this->Report_Model->CabDayReport($vehicletype);
			$this->load->view('CabDayReport',$data);
		}
	}
	public function CabReport()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$vehicletype = 'Cab';
		$data['result'] = $this->Vehicle_Model->vehiclelist($vehicletype);
		if(isset($_POST['showresult']))
		{
			$data['from'] = array($this->input->post('from'),$this->input->post('to'));
			$data['query'] = $this->Report_Model->FilterCabReport($vehicletype);
			$this->load->view('CabReport',$data);
		}
		elseif(isset($_POST['savereport']))
		{
			$contents="Tag Id, Date, Vehicle Number, Vehicle Type, Rounds in Month, Vendor Name,  Model, Registration Date, Insurance Expiry Date, Pollution Expiry Date, Fitness, Permit.\n";
			$data['query'] = $this->Report_Model->FilterCabReport($vehicletype);
			$fromdate = $this->input->post('from');
			$todate = $this->input->post('to');
			if(($fromdate !="") && ($todate !="")){
				$create_from=date_create($fromdate);
				$from = date_format($create_from,'Y-m-d');
				$create_to=date_create($todate);
				$to = date_format($create_to,'Y-m-d');
				$curdate = date('m-Y');
			}
			else{
				$from = "novalue";
				$to = "novalue";
				$curdate = date('m-Y');
			}
			foreach ($data['query'] as $row)
			{
				$totalround = round_in_month($row->tag_id,$curdate,$from,$to);
				$count1 = $totalround['totalround'];
				if(($count1 == '0') or  ($count1 != '0'))
				{
					$totalround = round_in_month($row->alias_tag,$curdate,$from,$to);
					$count2 = $totalround['totalround'];
				}
				$count = $count1+$count2;
				$Dates = strtotime($row->time_in);
				$Date = date('Y-m-d', $Dates);
				
				$contents.=$row->tag_no.",";
				$contents.=$Date.",";
				$contents.=$row->vehicle_number.",";
				$contents.=$row->cab_type.",";
				$contents.=$count.",";
				$contents.=$row->vender.",";
				$contents.=$row->Vehicle_model.",";
				$contents.=$row->registration_date.",";
				$contents.=$row->insurance_expiry_date.",";
				$contents.=$row->pollution_expiry_date.",";
				$contents.=$row->fitness.",";
				$contents.=$row->permit."\n";
				//----------------Relationship-------------------//
				/*$tag = Relationship($row->tag_id)['0'];
				$result = Relationship($row->tag_id)['1'];	
				foreach ($result as $fatchresult)
				{
					$totalround = round_in_month($fatchresult->tag_id,$curdate,$from,$to);
					$count = $totalround['totalround'];
					$contents.=$tag.",";
					$contents.=$count.",";
					$contents.=$row->vehicle_number.",";
					$contents.=$row->cab_type.",";
					$contents.=$row->insurance_expiry_date.",";
					$contents.=$row->pollution_expiry_date.",";
					$contents.=$row->registration_date.",";
					$contents.=$row->fitness.",";
					$contents.=$row->permit.",";
					$contents.=$row->vender."\n";
				}*/
				//----------------Relationship-------------------//
			}
			
			
			$contents = strip_tags($contents);
			$name_for_project = "CabReport";
			header("content-disposition: attachment;filename=".$name_for_project.".csv");
			print $contents;
		}
		else
		{
			$data['query'] = $this->Report_Model->CabReport($vehicletype);
			$this->load->view('CabReport',$data);
		}
	}
	
	public function CabConsolidatedReport()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$vehicletype = 'Cab';
		$data['result'] = $this->Vehicle_Model->vehiclelist($vehicletype);
		if(isset($_POST['showresult']))
		{
			$data['from'] = array($this->input->post('from'),$this->input->post('to'));
			$data['query'] = $this->Report_Model->ConsolidatedCabReport($vehicletype);
			$this->load->view('CabConsolidatedReport',$data);
		}
		elseif(isset($_POST['savereport']))
		{
			$contents="Tag Id, Date, Vehicle Number, Vehicle Type, Time In, Time Out, Vendor Name,  Model, Registration Date, Insurance Expiry Date, Pollution Expiry Date, Fitness, Permit.\n";
			$data['query'] = $this->Report_Model->ConsolidatedCabReport($vehicletype);
			$fromdate = $this->input->post('from');
			$todate = $this->input->post('to');
			if(($fromdate !="") && ($todate !="")){
				$create_from=date_create($fromdate);
				$from = date_format($create_from,'Y-m-d');
				$create_to=date_create($todate);
				$to = date_format($create_to,'Y-m-d');
				$curdate = date('m-Y');
			}
			else{
				$from = "novalue";
				$to = "novalue";
				$curdate = date('m-Y');
			}
			foreach ($data['query'] as $row)
			{
				$totalround = round_in_month($row->tag_id,$curdate,$from,$to);
				$count1 = $totalround['totalround'];
				if(($count1 == '0') or  ($count1 != '0'))
				{
					$totalround = round_in_month($row->alias_tag,$curdate,$from,$to);
					$count2 = $totalround['totalround'];
				}
				$count = $count1+$count2;
				
				$Dates = strtotime($row->firstin);
				$Date = date('Y-m-d', $Dates);
				$timein = date('H:i:s', $Dates);
				$out = strtotime($row->lastout);
				$timeout = date('H:i:s', $out);
				$timet = date('H:i:s', $out);
				$lastin = strtotime($row->lastin);
				$lasttimein = date('H:i:s', $lastin);
				$lasttim = date('H:i:s', $lastin);
				if($timeout <= $lasttimein){
					$exittime = $lasttim;
				} else {
					$exittime = $timet;
				}
				
				$contents.=$row->tag_no.",";
				$contents.=$Date.",";
				$contents.=$row->vehicle_number.",";
				$contents.=$row->cab_type.",";
				$contents.=$timein.",";
				$contents.=$exittime.",";
				$contents.=$row->vender.",";
				$contents.=$row->Vehicle_model.",";
				$contents.=$row->registration_date.",";
				$contents.=$row->insurance_expiry_date.",";
				$contents.=$row->pollution_expiry_date.",";
				$contents.=$row->fitness.",";
				$contents.=$row->permit."\n";
				//----------------Relationship-------------------//
				/*$tag = Relationship($row->tag_id)['0'];
				$result = Relationship($row->tag_id)['1'];	
				foreach ($result as $fatchresult)
				{
					$contents.=$tag.",";
					$contents.=$Date.",";
					$contents.=$row->vehicle_number.",";
					$contents.=$row->cab_type.",";
					$contents.=$timein.",";
					$contents.=$exittime.",";
					$contents.=$row->vender.",";
					$contents.=$row->Vehicle_model.",";
					$contents.=$row->registration_date.",";
					$contents.=$row->insurance_expiry_date.",";
					$contents.=$row->pollution_expiry_date.",";
					$contents.=$row->fitness.",";
					$contents.=$row->permit."\n";
					
				}*/
				//----------------Relationship-------------------//
			}
			
			
			$contents = strip_tags($contents);
			$name_for_project = "CabConsolidatedReport";
			header("content-disposition: attachment;filename=".$name_for_project.".csv");
			print $contents;
		}
		else
		{
			$data['query'] = $this->Report_Model->ConsolidatedCabReport($vehicletype);
			$this->load->view('CabConsolidatedReport',$data);
		}
	}
	
	public function IndivisualCabReport($tagid)
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$data['query'] = $this->Report_Model->VehicleCabReport($tagid);
		$this->load->view('IndivisualCabReport',$data);
	}
	public function IndivisualCabReports()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		if(isset($_POST['showresult']))
		{
			$data['from'] = array($this->input->post('from'),$this->input->post('to'));
			$tagid = trim($this->input->post('tag_id'));
			$data['tagid'] = $tagid;
			$data['query'] = $this->Report_Model->VehicleCabReportByDate();
			$this->load->view('IndivisualCabReport',$data);
		}
		elseif(isset($_POST['savereport']))
		{
			$contents="Tag Id, Time In, Time Out, Total Stay Time, Vehicle Number, Vehicle Type, Insurance Expiry Date, Pollution Expiry Date, Registration Date, Fitness, Permit, Vendor Name.\n";
			$data['query'] = $this->Report_Model->VehicleCabReportByDate();
			foreach ($data['query'] as $row)
			{
				if($row->time_out == '2010-01-01 01:01:01'){
					$timeout1 =  "AUTO OUT";
				} else {
					$timeout1 = $row->time_out;
				}
				
				$contents.=$row->tag_no.",";
				$contents.=$row->time_in.",";
				$contents.=$timeout1.",";
				
				$time_in = new DateTime($row->time_in);
				$time_out = new DateTime($row->time_out);
				$difference = $time_in->diff($time_out);
				if($row->time_out == ''){	
					$time_difference = " ";	
				}
				else{	
					$time_difference = format_interval($difference);	
				}
				$contents.=$time_difference.",";
				$contents.=$row->vehicle_number.",";
				$contents.=$row->cab_type.",";
				$contents.=$row->insurance_expiry_date.",";
				$contents.=$row->pollution_expiry_date.",";
				$contents.=$row->registration_date.",";
				$contents.=$row->fitness.",";
				$contents.=$row->permit.",";
				$contents.=$row->vender."\n";
			}
			//----------------Relationship-------------------//
			$tagid = $row->tag_id;
			$fromdate = trim($this->input->post('from'));
			$todate = trim($this->input->post('to'));
			$vehicletype = "Cab";
			
			//get main CodeIgniter object
			$ci =& get_instance();
			//load databse library
			$ci->load->database();
			$query = $ci->db->query("select tag, tag_id, ntag, ntag_id from tbl_relationship where ntag_id='".$tagid."'");
			$fatchResult = $query->result();
			$totalrow = $query->num_rows();
			if($totalrow != "0"){
				foreach($fatchResult as $fatchRow){
					$curdate = date('m-Y');
					$sql = "SELECT tag_id, time_in, time_out FROM log WHERE tag_id = '".$fatchRow->tag_id."'";
					if((trim($fromdate) != "") && (trim($todate) != ""))
					{
						$sql .= " AND DATE_FORMAT(time_in, '%Y-%m-%d') BETWEEN '".trim($fromdate)."' and '".trim($todate)."'"; 
					}
					else
					{
						$sql .= " AND DATE_FORMAT( time_in, '%m-%Y' ) = '".$curdate."'";
					}
					$query = $ci->db->query($sql);
					$result = $query->result();	
					foreach($result as $datarow)
					{
						$i++;
						$query = $ci->db->query("select * from tbl_vehicle_master where tag_id = '".$tagid."'");
						$row = $query->row_array();
						
						if($datarow->time_out == '2010-01-01 01:01:01'){
							$timeout1 =  "AUTO OUT";
						} else {
							$timeout1 = $datarow->time_out;
						}
						
						$contents.=$fatchRow->tag.",";
						$contents.=$datarow->time_in.",";
						$contents.=$timeout1.",";
						
						$time_in = new DateTime($datarow->time_in);
						$time_out = new DateTime($datarow->time_out);
						$difference = $time_in->diff($time_out);
						if($datarow->time_out == ''){	
							$time_difference = " ";	
						}
						else{	
							$time_difference = format_interval($difference);	
						}
						$contents.=$time_difference.",";
						$contents.=$row->vehicle_number.",";
						$contents.=$row->cab_type.",";
						$contents.=$row->insurance_expiry_date.",";
						$contents.=$row->pollution_expiry_date.",";
						$contents.=$row->registration_date.",";
						$contents.=$row->fitness.",";
						$contents.=$row->permit.",";
						$contents.=$row->vender."\n";
					}
				}	
			}
			
			//----------------Relationship-------------------//
			
			
			//----------------Relationship-------------------//
			$tag = Ind_Relationship($row->tag_id)['0'];
			$result = Ind_Relationship($row->tag_id)['1'];
			foreach ($result as $fatchresult)
			{
				$contents.=$tag.",";
				$contents.=$fatchresult->time_in.",";
				$contents.=$fatchresult->time_out.",";
				
				$time_in = new DateTime($fatchresult->time_in);
				$time_out = new DateTime($fatchresult->time_out);
				$difference = $time_in->diff($time_out);
				if($fatchresult->time_out == ''){	
					$time_difference = " ";	
				}
				else{	
					$time_difference = format_interval($difference);	
				}
				$contents.=$time_difference.",";
				$contents.=$row->vehicle_number.",";
				$contents.=$row->cab_type.",";
				$contents.=$row->insurance_expiry_date.",";
				$contents.=$row->pollution_expiry_date.",";
				$contents.=$row->registration_date.",";
				$contents.=$row->fitness.",";
				$contents.=$row->permit.",";
				$contents.=$row->vender."\n";
			}
			//----------------Relationship-------------------//
			$contents = strip_tags($contents);
			$name_for_project = "IndivisualCabReports";
			header("content-disposition: attachment;filename=".$name_for_project.".csv");
			print $contents;
		}	
	}
	
	//--------------------View Log Details-----------------// 
	public function LogDetails()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$vehicletype = 'Bus';
		//$data['query'] = $this->Vehicle_Model->vehiclelist($vehicletype);
		$data['query'] = $this->Vehicle_Model->LogList();
		$this->load->view('LogDetails',$data);
	}
	
	//--------------------Billing-----------------// 
	public function Billing()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$vehicletype = 'Bus';
		$data['query'] = $this->Vehicle_Model->vehiclelist($vehicletype);
		$this->load->view('Billing',$data);
	}
	public function BillingReport()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$vehicletype  = 'Bus';
		if(isset($_POST['savereport']))
		{
			$contents="Date From, Date To, Tag Id, Vehicle Number, Charges (Rs.), Total Trip, Net Payable (Rs.), Route No., Vendor Name.\n";
			$check_delete=$this->input->post('checkbox');
			$from=$this->input->post('from');
			$to=$this->input->post('to');
			$trip=$this->input->post('trip');
			
			$date1 = new DateTime($from);
			$date2 = new DateTime($to);
			$diff = $date2->diff($date1)->format("%a");
			$numberFoDays = $diff + 1;
			foreach ($check_delete as $checkedUserId)
			{
				//get main CodeIgniter object
				$ci =& get_instance();
				//load databse library
				$ci->load->database();
				$query = $ci->db->query("select * from tbl_vehicle_master where vehicle_id='".$checkedUserId."'");
				$fatchResult = $query->row_array();
				$totalrow = $query->num_rows();
				if($totalrow != "0"){
					$charges = $fatchResult['charges'];
					$amount = ($charges/60)*$trip;
					$contents.=$from.",";
					$contents.=$to.",";
					$contents.=$fatchResult['tag_no'].",";
					$contents.=$fatchResult['vehicle_number'].",";
					$contents.=$fatchResult['charges'].",";
					$contents.=$trip.",";
					$contents.=$amount.",";
					$contents.=$fatchResult['route_no'].",";
					$contents.=$fatchResult['vender']."\n";
				}
			}
			$contents = strip_tags($contents);
			//print_r($contents);
			$name_for_project = "BillingReport";
			header("content-disposition: attachment;filename=".$name_for_project.".csv");
			print $contents;
		}
	}
	public function PenaltyList()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$vehicletype = 'Bus';
		$data['result'] = $this->Vehicle_Model->vehiclelist($vehicletype);
		$data['query'] = $this->Vehicle_Model->PenaltyList();
		$this->load->view('PenaltyList',$data);
	}
	public function PenaltyDetails($VehicleId)
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$data['query'] = $this->Vehicle_Model->VehiclePenaltyDetails($VehicleId);
		$this->load->view('PenaltyDetails',$data);
	}
	public function AddPenalty()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$this->load->view('AddPenalty');
	}
	public function EditPenalty($PenaltyId)
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Administrator/');
		}
		$data['query'] = $this->Vehicle_Model->PenaltyDetails($PenaltyId);
		$this->load->view('EditPenalty',$data);
	}
	public function PenaltyProcess()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		if(isset($_POST['btnPenaltyAdd']))
		{
			$result = $this->Vehicle_Model->AddPenalty();
			if($result)
			{
				$update_by = $this->session->userdata('adminid');
				$update_type = 'S';
				$update_tab = 'P';
				$update_id = $this->Vehicle_Model->GetMaxId('vehicle_id','tbl_penalty');
				$result = $this->Vehicle_Model->CreateLog($update_by,$update_type,$update_tab,$update_id);
				
				$this->session->set_flashdata('flsh_msg', 'Vehicle penalty added sucessfully');
				redirect('Administrator/PenaltyList');
			}
			else
			{
				$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
				redirect("Administrator/AddPenalty/");
			}
		}
		if(isset($_POST['btnPenaltyUpdate']))
		{
			$PenaltyId = $this->input->post('penalty_id');
			$result = $this->Vehicle_Model->EditPenalty($PenaltyId);
				if($result)
				{
					$update_by = $this->session->userdata('adminid');
					$update_type = 'E';
					$update_tab = 'P';
					$update_id = $PenaltyId;
					$result = $this->Vehicle_Model->CreateLog($update_by,$update_type,$update_tab,$update_id);
					
					$this->session->set_flashdata('flsh_msg', 'Penalty details edit sucessfully');
					redirect('Administrator/PenaltyList');
				}
				else
				{
					$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
					redirect("Administrator/EditPenalty/$PenaltyId");
				}
		}
	}
	
	//--------------------Notification-----------------// 
	public function Notification()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$data['query'] = $this->Vehicle_Model->NotificationList();
		$this->load->view('Notification',$data);
	}
	public function NotifyList()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$data['query'] = $this->Vehicle_Model->AllVehicleList();
		$data['result'] = $this->Vehicle_Model->NotificationList();
		$this->load->view('NotifyList',$data);
	}
	public function AddNotification()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$this->load->view('AddNotification');
	}
	public function EditNotification($NotificationId)
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Administrator/');
		}
		$data['query'] = $this->Vehicle_Model->NotificationDetails($NotificationId);
		$this->load->view('EditNotification',$data);
	}
	public function NotificationProcess()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		if(isset($_POST['btnNotificationAdd']))
		{
			$result = $this->Vehicle_Model->AddNotification();
			if($result)
			{
				$this->session->set_flashdata('flsh_msg', 'Notification added sucessfully');
				redirect('Administrator/Notification');
			}
			else
			{
				$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
				redirect("Administrator/AddNotification/");
			}
		}
		if(isset($_POST['btnNotificationUpdate']))
		{
			$NotificationId = $this->input->post('NotificationId');
			$result = $this->Vehicle_Model->EditNotification($NotificationId);
				if($result)
				{
					$this->session->set_flashdata('flsh_msg', 'Notification edit sucessfully');
					redirect('Administrator/Notification');
				}
				else
				{
					$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
					redirect("Administrator/EditNotification/$NotificationId");
				}
		}
	}
	//--------------------Handheld data-----------------// 
	public function HandheldProcess()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		if(isset($_POST['btnNotificationAdd']))
		{
			$result = $this->Process->UploadHandheldData();
			if($result)
			{
				$this->session->set_flashdata('flsh_msg', 'Data Uploaded successfully');
				redirect('Administrator/UploadLog');
			}
			else
			{
				$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
				redirect("Administrator/UploadLog/");
			}
		}
	}
	
	//--------------------Cal data-----------------//
	public function BusRoundList()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$data['result'] = $this->Vehicle_Model->BusRoundList();
		$this->load->view('BusRoundList',$data);
	}
	public function AddBusRound()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$this->load->view('AddBusRound');
	}
	public function EditBusRound($Id)
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Administrator/');
		}
		$data['query'] = $this->Vehicle_Model->BusRoundDetails($Id);
		$this->load->view('EditBusRound',$data);
	}
	public function BusRoundProcess()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		if(isset($_POST['btnPenaltyAdd']))
		{
			$result = $this->Vehicle_Model->AddBusRound();
			if($result)
			{
				$update_by = $this->session->userdata('adminid');
				$update_type = 'S';
				$update_tab = 'R';
				$update_id = $this->Vehicle_Model->GetMaxId('id','tbl_monthly_round');
				$result = $this->Vehicle_Model->CreateLog($update_by,$update_type,$update_tab,$update_id);
				
				$this->session->set_flashdata('flsh_msg', 'Monthly round added sucessfully');
				redirect('Administrator/BusRoundList');
			}
			else
			{
				$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
				redirect("Administrator/AddBusRound/");
			}
		}
		if(isset($_POST['btnPenaltyUpdate']))
		{
			$Id = $this->input->post('Id');
			$result = $this->Vehicle_Model->EditBusRound($Id);
				if($result)
				{
					$update_by = $this->session->userdata('adminid');
					$update_type = 'E';
					$update_tab = 'R';
					$update_id = $Id;
					$result = $this->Vehicle_Model->CreateLog($update_by,$update_type,$update_tab,$update_id);
					
					$this->session->set_flashdata('flsh_msg', 'Monthly round edit sucessfully');
					redirect('Administrator/BusRoundList');
				}
				else
				{
					$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
					redirect("Administrator/EditPenalty/$PenaltyId");
				}
		}
	}
	
	public function EditRoute()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		$this->load->view('EditRoute');
	}
	/*public function EditNotification($NotificationId)
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Administrator/');
		}
		$data['query'] = $this->Vehicle_Model->NotificationDetails($NotificationId);
		$this->load->view('EditNotification',$data);
	}*/
	public function RouteProcess()
	{
		if($this->session->userdata('adminid') == '')
		{
			redirect('Login/');
		}
		if(isset($_POST['btnNotificationAdd']))
		{
			$result = $this->Vehicle_Model->AddRoute();
			if($result)
			{
				$this->session->set_flashdata('flsh_msg', 'Route added sucessfully');
				redirect('Administrator/BusesList');
			}
			else
			{
				$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
				redirect("Administrator/EditRoute/");
			}
		}
		/*if(isset($_POST['btnNotificationUpdate']))
		{
			$NotificationId = $this->input->post('NotificationId');
			$result = $this->Vehicle_Model->EditNotification($NotificationId);
				if($result)
				{
					$this->session->set_flashdata('flsh_msg', 'Notification edit sucessfully');
					redirect('Administrator/Notification');
				}
				else
				{
					$this->session->set_flashdata('flsh_msg1', 'Please Try Again!');
					redirect("Administrator/EditNotification/$NotificationId");
				}
		}*/
	}
}
