<?php $pageno = 2;error_reporting(0); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<?php include('template/head.php'); ?>
	<link rel="stylesheet" href="<?php echo base_url(); ?>assets/datepicker/jquery-ui.css" />
	<!--<script src="<?php echo base_url(); ?>assets/datepicker/jquery-1.9.1.js"></script>-->
	<script src="<?php echo base_url(); ?>assets/datepicker/jquery-ui.js"></script>
	<script type="text/javascript">
	$(function() {
		$( "#datepicker" ).datepicker({ dateFormat: "yy-mm-dd" }).val();
		$( "#datepicker1" ).datepicker({ dateFormat: "yy-mm-dd" }).val();
	});
	</script>
	<!--DATA TABLE --> 
    <script type="text/javascript" src="<?php echo base_url(); ?>assets/data_table/js/jquery.dataTables.js"></script>
    <link href="<?php echo base_url(); ?>assets/data_table/css/demo_table_jui.css" rel="stylesheet" type="text/css"/>
    <link href="<?php echo base_url(); ?>assets/data_table/css/jquery-ui-1.8.4.custom.css" rel="stylesheet" type="text/css"/>
    <!--DATA TABLE -->   
    <script type="text/javascript" charset="utf-8">
	$(document).ready(function(){
		$('#datatables').dataTable({
			"sPaginationType":"full_numbers",
			"iDisplayLength": 100,
			"aaSorting":[[0, "asc"]],
			"bJQueryUI":true
		});
	});
    </script> 
</head>
<body>
<?php include('template/header.php'); ?> 
<div class="grid_16" id="navheader">
	<?php include('template/topnav.php'); ?>
</div>
<div class="grid_16" id="tabs">
	<div class="container">
		<ul>
			<li><a href="<?php echo base_url(); ?>index.php/Administrator/BusesList/"><span>Master Details</span></a></li>
			<li><a href="<?php echo base_url(); ?>index.php/Administrator/BusDayReport/"><span>Day Report</span></a></li>
			<li><a href="<?php echo base_url(); ?>index.php/Administrator/BusReport/" class="current"><span>Month Report</span></a></li>
		</ul>
	</div>   
</div>
<div class="container_16" id="wrapper">	
    <div class="grid_16" id="content"> 
		<div class="grid_9">
			<h1 class="user">Buses Month Report</h1>
		</div>
		<div class="grid_15" id="textcontent">
			<div id="table">
				<div id="user_profile_main_div">
					<div id="message">
					<?php 
					if($this->session->flashdata('flsh_msg')) {
						?><p id="success" class="info"><span class="info_inner"><?php echo $this->session->flashdata('flsh_msg'); ?></span></p> <?php
					} 
					if($this->session->flashdata('flsh_msg1')) {
						?><p id="error" class="info"><span class="info_inner"><?php echo $this->session->flashdata('flsh_msg1'); ?></span></p> <?php
					} 
					?>
					</div>
					<div class="clear"></div>
					<form action="<?php echo base_url(); ?>index.php/Administrator/BusReportRoute/" method="post" enctype="multipart/form-data" name="frmUsers" id="frmTransaction" >
					<table style="float: right;">
						<tr>
							<td align="right" colspan="9">
								<input type="hidden" class="input" name="tag_id"  value="<?php echo $Routeno['Routeno']; ?>"/>
								<input type="text" class="smallInput" name="from" placeholder="Date From" id="datepicker"/>
								<input type="text" class="smallInput" name="to" placeholder="Date To" id="datepicker1"/>
								<span class="button">
									<input name="showresult" class="buttoncc" type="submit" id="delete" value="Show">
								</span>
								<span class="button">
									<input name="savereport" class="buttoncc" type="submit" id="delete" value="save as excel">
								</span>
							</td>
						</tr>
					</table>
					</form>	
					<table id="datatables" class="display">
						<thead>
							<tr>
								<th>Sno</th>
								<th>Tag Id</th>
								<th>Round's in Month</th>
								<th>Vehicle Number</th> 
								<th>Route No.</th>
								<th>Route Name</th>
								<th>Vendor Name</th>
							</tr>
						</thead>
						<tbody>
							<?php
							$i=0;
							
							$curdate = date('Y-m');
							
							$current_day_start = date("Y-m-d 00:00:00");
							$current_day_end = date("Y-m-d 23:59:59");
							$f = $from['0'];
							$t = $from['1'];
							
							$routeno = $Routeno['Routeno'];
							
							$post_vehicleid = $from['2'];
							$ci =& get_instance();
							$ci->load->database();
							$query = $ci->db->query("select * from tbl_route where route_no='".$routeno."'");
							$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' ) BETWEEN '".$curdate."' and '".$curdate."'";
									}
									$sql .= " AND tbl_vehicle_master.vehicle_id = '".$vehicleid."'";
									$query = $ci->db->query($sql);
									$result = $query->result();
									foreach($result as $datarow){
										$i++;
										?>
										<tr>
											<td><?php echo $i;?></td>
											<td><?php echo $datarow->tag_no; ?></td>
											<td><?php echo $datarow->time_in; ?></td>
											<td>
												<?php 
												if(($row->time_out == '2010-01-01 01:01:01') || ($row->time_out == '2009-12-31 19:31:01') || ($row->time_out == '') ){
													echo "AUTO OUT";
												} else {
													echo $datarow->time_out;
												} 
												?>
											</td>
											<td>
												<a href="<?php echo base_url(); ?>index.php/Administrator/VehicleDetails/<?php echo $vehicleid; ?>" title="View" class="ajax"><?php echo $datarow->vehicle_number; ?></a>
											</td>
											<td><?php echo $fatchrow->route_no; ?></td>
											<td><?php echo $route_name; ?></td>
										</tr>
										<?php
									}
								}
							}
							else{
								$query = $ci->db->query("SELECT route_name, vehicle_id FROM `tbl_vehicle_master` WHERE  route_no = '".$routeno."'");
								$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' ) BETWEEN '".$curdate."' and '".$curdate."'";
								}
								$sql .= " AND tbl_vehicle_master.vehicle_id = '".$vehicle_id."'";
								$query = $ci->db->query($sql);
								$result = $query->result();
								foreach($result as $datarow){
									$i++;
									?>
									<tr>
										<td><?php echo $i;?></td>
										<td><?php echo $datarow->tag_no; ?></td>
										<td><?php echo $datarow->time_in; ?></td>
										<td>
											<?php 
											if($datarow->time_out == '2010-01-01 01:01:01'){
												echo "AUTO OUT";
											} else {
												echo $datarow->time_out;
											} 
											?>
										</td>
										<td>
											<a href="<?php echo base_url(); ?>index.php/Administrator/VehicleDetails/<?php echo $vehicleid; ?>" title="View" class="ajax"><?php echo $datarow->vehicle_number; ?></a>
										</td>
										<td><?php echo $datarow->route_no; ?></td>
										<td><?php echo $datarow->route_name; ?></td>
									</tr>
									<?php
								}
							}?>
						</tbody>
					</table>
				</div>
			</div>
		</div>
		<div class="clear"> </div>		
	</div>
</div>
<?php include('template/footer.php'); ?>
</body>
</html>