<?php
if(basename($_SERVER['PHP_SELF']) == 'miler.php') 
  $nv=" class='current'";
$custo=$conde='';
include'connection.php';
$dato=$datos=$Date;
$p=0;

// Open a single record
if(isset($_POST['slip']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$pago=$_POST['pago'];
			$invo=$_POST['invo'];
			$dte=$_POST['dte'];
			$p=$_POST['p'];
			$i=$_POST['i'];
			
	$do=mysqli_query($conn, "SELECT `Invoice` FROM `repair` ORDER BY `Invoice` DESC LIMIT 1");
		  $ro=mysqli_fetch_assoc($do);
		      $invo=$ro['Invoice']+1;
		      
	for($a=1; $a<=$i; $a++){
			$nuo=$_POST["nuo$a"];
		    if($_POST["plate$a"])
	$so=mysqli_query($conn, "UPDATE `repair` SET `Printed`='1', `Invoice`='$invo', `Pdate`='$Date', `Done`='$loge' WHERE `Number`='$nuo' ORDER BY `Number` ASC LIMIT 1");
        	}

include'slipo.php';
		}

include'header.php';



if(isset($_POST['back']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$p=$_POST['p'];
		}

if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
				$p=1;
		}

            if($p==1)
        $conde="AND `Date` BETWEEN '$dato' AND '$datos'";
	

        if($dato==$datos)
			$mpri="ON $dato";
		else
			$mpri="FROM $dato TO $datos";
?>

<script type="text/javascript">
$(document).ready(function() {
$('input[type="checkbox"]').change(function(){
var total_checked=  $("input[type='checkbox']:checked").length 
$("#d1").html("Selected Records: <b> " + total_checked );
});
/////////////
var total_checked=  $("input[type='checkbox']:checked").length 
$("#d1").html("Selected Records: <b> " + total_checked );
///////
});


   function check_uncheck_checkbox(isChecked) {
	if(isChecked) {
		$('input[type="checkbox"]').each(function() { 
			this.checked = true; 
		});
	} else {
		$('input[type="checkbox"]').each(function() {
			this.checked = false;
		});
	}
}


function UpdateCost() {
  var sum = 0;
  var gn, elem;
  for (i=1; i<=<?php echo $fo ?>; i++) {
    gn = 'sum_m_'+i;
    elem = document.getElementById(gn);
    if (elem.checked == true) { sum += Number(elem.value); }
  }
  document.getElementById('totalcost' ).value = sum.toFixed(0);
}
window.onload=UpdateCost
</script>

<div class="container-fluid main-content">
        <div class="page-title hidden-print">
          <h2 style='margin-top:-20px; margin-bottom: 5px; color:#ffcc33'>
        Vehicles
          </h2>
                 </div>
     
         <div class="row">
  <div class="col-lg-2 hidden-print">
   
   <ul class="list-group">

	   <li class="list-group-item">
              <a href="locate.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Allocate Vehicles
                </p>
              </a></li> 
                  
			  <li class="list-group-item">
              <a href="ment.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Repair & Services
                </p>
              </a></li>

    <li class="list-group-item">
	  <a href="mainsto.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;List of Vehicles
                </p>
              </a></li>
                       
            </ul><br><br>

			<li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="createa.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Create Customers
                </p>
              </a></li>	
              
            <li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="dispatch.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Create Vehicle Trip
                </p>
              </a></li>	
        
                <?php
              if($_SESSION['Cpo']){
                  ?>
			  <li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
              <a href="purcha.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Purchase Orders
                </p>
              </a></li>
              <?php
              }
              if($_SESSION['Cpi']){
                  ?>
            <li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="profo.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Create Proforma
                </p>
              </a></li>	
              <?php
              }
              ?>
              
            <li class="list-group-item active" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="payslip.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Payment Vouchers
                </p>
              </a></li>	
              
            <li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="conterepo.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Container Dispatch
                </p>
              </a></li>	
              
            <li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="ddlist.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Mileage Deduction
                </p>
              </a></li>		
  </div>
       
        <div class="col-lg-10">
                  <div class="row">
    <div class="col-lg-4" style="padding-left:40px;">
         <form action="tmile.php" method="post" class="form-horizontal" enctype="multipart/form-data" name='myform'> 
           <div class="col-lg-6 hidden-print"> <button class="btn btn-info btn-block" type="submit"> Trip Report </button> </div></form> 
           
         <form action="milerepo.php" method="post" class="form-horizontal" enctype="multipart/form-data" name='myform'> 
           <div class="col-lg-6 hidden-print"> <button class="btn btn-success btn-block" type="submit"> Vouchers Report </button> </div></form>
        </div>   

        <form action="" method="post" class="form-horizontal ">                 
     <div class="col-lg-8 hidden-print" style="padding-right:50px;"> <div class="col-lg-4"> </div> 
                       
             <div class="col-lg-3"> 
           <div class="input-group date" data-provide="datepicker">
      <input class="form-control form-center" id="from" name="dato" type="text" value="<?php echo $dato ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
			</div></div>


		  <div class="col-lg-3"> 
           <div class="input-group date" data-provide="datepicker">	
      <input class="form-control form-center" id="to" name="datos" type="text" value="<?php echo $datos ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
              </div></div>                     
                       
                       <div class="col-lg-2"><?php echo"<input type='hidden' name='pg' value='$pg'>"; ?>
                        <button class="btn  btn-primary btn-block" type="submit" name="search"><i class="lnr lnr-magnifier"></i> Search</button>
                   
					  </div>
                         </div></form>               
            </div>
               <?php
               if($p)
		$do=mysqli_query($conn, "SELECT *FROM `repair` WHERE (`Trip`!='0' AND `Status`='0' AND `Garage`='ROAD TOLL' AND `Printed`='0' $conde) OR (`Trip`!='0' AND `Status`='0' AND `Garage`='MILEAGE' AND `Printed`='0' $conde) OR (`Trip`!='0' AND `Status`='0' AND `Garage`='OTHER EXPENSE' AND `Printed`='0' $conde) OR (`Trip`!='0' AND `Status`='0' AND `Garage`='GPS SERVICES' AND `Printed`='0' $conde) GROUP BY `Trip`, `Type`, `Date` ORDER BY `Date` ASC");
		        else
		$do=mysqli_query($conn, "SELECT *FROM `repair` WHERE (`Trip`!='0' AND `Status`='0' AND `Garage`='ROAD TOLL' AND `Printed`='0') OR (`Trip`!='0' AND `Status`='0' AND `Garage`='MILEAGE' AND `Printed`='0') OR (`Trip`!='0' AND `Status`='0' AND `Garage`='OTHER EXPENSE' AND `Printed`='0') OR (`Trip`!='0' AND `Status`='0' AND `Garage`='GPS SERVICES' AND `Printed`='0') GROUP BY `Trip`, `Type`, `Date` ORDER BY `Number` DESC LIMIT 15");
				if($fo=mysqli_num_rows($do)){
				?>
                 
            <div class="divFooter"><center><u><b> MILEAGE RECORD <?php echo $mpri ?></b></u></center></div>
             <div class="row">
            <div class="col-lg-12">
             <span> &nbsp;&nbsp; Total Records Found : <b><?php echo" $fo " ?></b></span>
			 <span class="pull-right hidden-print">&nbsp;&nbsp;
		<a href="#" onclick="return window.print()" class="hidden-print" title='Click to print' data-toggle='tooltip' data-placement='top'>
			 <i class="lnr lnr-printer"></i></a></span>
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">
                <form action='' method='post'>

				<table class="table table-striped table-hover">     
                        <thead><tr role="row">
    <th width="1%" style='padding:1px; text-align:center; width:5px;'> </th>
					 <th width='8%'><div align='center'> Date </th>
		<th style='padding:1px;'><div align='center'> Plate </th>
		<th style='padding:1px;'><div align='center'> Customer </th>
		<th style='padding:1px;'><div align='center'> Driver </th>
			<th style='padding:1px;'><div align='center'> Destination </th>
    <th style='padding:1px;'><div align='center'> Road&nbsp;Toll </th>
			<th style='padding:1px;'><div align='center'> Mileage </th>
			<th style='padding:1px;'><div align='center'> Other </th>
			<th style='padding:1px;'><div align='center'> Rate </th>
			<th style='padding:1px;'><div align='center'> Value </th></tr>
                    </thead>
                                        <tbody>
					<?php
			$i=1;		            	$tra=$tmi=$tpa=$toth=$rt=0;
	while($ro=mysqli_fetch_assoc($do)){
				$nuo=$ro['Number'];
				$trip=$ro['Trip'];
				$dte=$ro['Date'];
				$pla=$ro['Plate'];
				$typ=$ro['Type'];
				$gars=$ro['Garage'];
				$vehs=$ro['Vehicle'];
				$pr=$ro['Printed'];
				$invo=$ro['Invoice'];
				$pda=$ro['Pdate'];

                // *********************** Trip record **********************
		$dois=mysqli_query($conn, "SELECT *FROM `trips` WHERE `Number`='$trip' ORDER BY `Number` ASC");
			$rois=mysqli_fetch_assoc($dois);
				$etd=$rois['ETD'];
				$des=$rois['Destination'];
				$loc=$rois['Location'];
				$dri=$rois['Driver'];
				$fni=$rois['Final'];
				    $roa=$mil=$pay=$othe=$pa=$cut=0;
				
		        // ******************* Road Toll & Mileage ******************
		$dois=mysqli_query($conn, "SELECT `Amount`, `Garage`, `Driver`, `Rate` FROM `repair` WHERE `Status`='0' AND `Trip`='$trip' AND `Type`='$typ' AND `Date`='$dte' AND (`Garage`='ROAD TOLL' OR `Garage`='MILEAGE' OR `Garage`='OTHER EXPENSE' OR `Garage`='GPS SERVICES') ORDER BY `Number` ASC");
			    while($rois=mysqli_fetch_assoc($dois)){
			        $rate=$rois['Rate'];
				    if($rois['Garage']=='ROAD TOLL')
				$roa+=$rois['Amount'];
				    elseif($rois['Garage']=='MILEAGE')
				$mil+=$rois['Amount'];
				    else
				$othe+=$rois['Amount'];
				
				if(!$dri)
				    $dri=$rois['Driver'];
			    }
			    
			    $deso="$des / $loc - $fni";
			    
			 	// ****************** Trip customer ********************** 
	   	$doil=mysqli_query($conn, "SELECT `Custo` FROM `income` WHERE `Trip`='$trip' ORDER BY `Number` ASC");
			$roil=mysqli_fetch_assoc($doil);
				$cut=$roil['Custo'];
				
				
						$roao=number_format($roa, 2);
						$milo=number_format($mil, 2);
						$rateo=number_format($rate);
						$otheo=number_format($othe, 2);
						$to=($roa+$mil+$othe)/$rate;
						$too=number_format($to, 2);
						
			if($typ==1)
					$stl="padding:1px; font-size:13px; color:blue;";
			else
					$stl="padding:1px; font-size:13px;";
					
				//	if($rate=='1')
			//	$too=$to=0;
				
		if($pa OR $typ==1)
	$mode="disabled readonly";
		else
	$mode="data-placement='top' data-toggle='modal' data-target='#modalcut$i'";


	print("<tr><td class='text-center' style='width:5px; $stl $clr'><input id='sum_m_$i' type='checkbox' value='$to' name='plate$i' style='border:0px; height:15px; width:15px; margin:0px;' onclick='UpdateCost()'></td>
	<td style='$stl $clr'><div align='center'> $dte </td><td style='$stl $clr'> $pla </td><td style='$stl $clr'> $cut </td><td style='$stl $clr'> $dri </td><td style='$stl $clr'> $deso
	  <input type='hidden' name='i' value='$i'>
	  <input type='hidden' name='p' value='$p'>
	  <input type='hidden' name='pr' value='$pr'>
	  <input type='hidden' name='dte' value='$pda'>
	  <input type='hidden' name='invo' value='$invo'>
	  <input type='hidden' name='dato' value='$dato'>
	  <input type='hidden' name='datos' value='$datos'>
	  <input type='hidden' name='pago' value='milerepo.php'>
	  <input type='hidden' name='nuo$i' value='$nuo'> </td>
	<td style='$stl $clr'><div align='right'> $roao </td>
	<td style='$stl $clr'><div align='right'> $milo </td>
	<td style='$stl $clr'><div align='right'> $otheo </td>
	<td style='$stl $clr'><div align='right'> $rateo </td>
	<td style='$stl $clr'><div align='right'> $too </td></tr>");
	            $i++;
	 
	 $set=mysqli_query($conn, "UPDATE `repair` SET `Mileage`='$to' WHERE `Number`='$nuo' ORDER BY `Number` ASC LIMIT 1");
			}
		?>
						
                    </tbody><thead>
	<tr><th class='text-right'><input type='checkbox' name='checkall' id='checkall' style='height:18px; width:18px; margin:0px;' onClick='check_uncheck_checkbox(this.checked);'></th><th colspan="3"><button type='button' class='btn btn-xs btn-link active' id='d1' style="font-size:14px;"></button></th><th colspan="2"> </th>
	<?php
	
	 echo"<th colspan='2' class='text-right' style='padding-right:0px;'><input class='form-control text-right pull-right' type='text' name='sen' id='totalcost' value='' onchange='format(this);' style='width:160px; border:0px; font-size:18px; margin-right:10px; margin-left:10px; height:24px; background-color:transparent;'></th>
	 
	<th class='text-right' colspan='3' style='padding-right:0px;'><button type='submit' class='btn btn-sm btn-info pull-right' style='width:180px; height:26px; margin-right:5px; margin-left:10px; padding-top:4px;' name='slip' class='delete'><i class='lnr lnr-printer'></i> &nbsp;&nbsp; PRINT&nbsp;SELECTED </button></td>";
		    ?>
		    </tr></thead>
                  </table><br>

				</form>
				  

								</div></div>
								<span class="pull-right hidden-print">
			 <a href="#" onclick="return window.print()" class="hidden-print" title='Click to print' data-toggle='tooltip' data-placement='top'>
			 <i class="lnr lnr-printer"></i></a></span><br><br><br>  

			<?php
				}
				else{
						echo"<div class='row'>
            <div class='col-lg-12' style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; Total Records Found : <b> 0 </b></span>
            <div class='widget-container fluid-height clearfix'>
            <div class='widget-content padded clearfix'>
			<br><br><br><br><br><br>
	<div style='text-align:center; font-size:24px; color:#ff9999'>Report not available on selected date </div><br><br><br><br><br><br>";
					}
			
					?>
                                      
                
              </div>
            </div></div>
                  </div>
   <?php
   include'footer.php';
   ?>