<?php
if(basename($_SERVER['PHP_SELF']) == 'tmile.php') 
  $nv=" class='current'";
include'header.php';
$custo=$conde='';
$castoi=$condi='';
$t=$p=0;

$dato=$datos=$Date;

    // *************************** Add services *******************************
    if(isset($_POST['toll']))
		{
			$custo=$_POST['custo'];
			$bulk=$_POST['bulk'];
			$n=$_POST['n'];
			$dato=$_POST['dato'];
			$pri=str_replace(',', '', $_POST['amo']);
			$curre=str_replace(',', '', $_POST['curre']);
			$amo=$pri*$curre;
			$descri=str_replace("'", "`", $_POST['descri']);
			$type=$_POST['type'];
			$rate=$_POST['curre'];
			$drive=$_POST['drive'];
			$t=1;                   $lim=1000;
			
			if($type=='OTHER EXPENSE')
			    $ty=1;
			else
			    $ty=0;
			
		for($i=1; $i<$n; $i++){
			$plat=$_POST["code$i"];
			$fna=$_POST["plat$i"];
			$dri=$_POST["driv$i"];
			$code=$_POST["trip$i"];
			$amoo=number_format($amo, 2);
				
				if($amo AND ($dri==$drive OR $drive=='ALL DRIVERS')){
	$set=mysqli_query($conn, "INSERT INTO `repair` (`Vehicle`, `Amount`, `Garage`, `Items`, `Issue`, `Date`, `Repair`, `Driver`, `Trip`, `User`, `Time`, `External`, `Plate`, `Type`, `Rate`, `Bulk`) VALUES ('$plat', '$amo', '$type', '0', '$descri', '$dato', '', '$dri', '$code', '$loge', '$Time', '1', '$fna', '$ty', '$rate', '$bulk')");
	
	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'MILEAGE', 'Paid $amoo for mileage (trip)')");	
	
	$seti=mysqli_query($conn, "UPDATE `trips` SET `Mileage`='$amo' WHERE `Number`='$code' AND `Bulk`='$bulk' ORDER BY `Number` DESC LIMIT 1");
				}
		    }
	$conde="AND (`trips`.`Bulk` = '$bulk' AND `vehicles`.`Bulk` = '$bulk')";
		}
		
		
		
		// ************************* Add vehicles ***********************	
		
    if(isset($_POST['dinco']))
		{
	
	    for($i=1; $i<=$_POST['k']; $i++){
	        
//	$so=mysqli_query($conn, "INSERT INTO `income` (`Date`, `Vehicle`, `Distance`, `Driver`, `Weight`, `District`, `Location`, `Descri`, `Amount`,  `Status`, `Rate`, `Customer`, `User`, `Time`, `Trip`, `External`, `Plate`, `Bulk`, `Custo`) VALUES ('$Date', '$pla', '$dista', '$dri', '$capa', '$final', '$loca', '$descri', '$amo', '0', '$curr', '$customer', '$loge', '$Time', '$trino', '1', '$fna', '$bulk', '$custmo')");
            }
		}
		
	// ************************* Add vehicles ***********************			
		
    if(isset($_POST['savalle']))
		{
			$desti=str_replace("'", "`", $_POST['desti']);
			$loca=str_replace("'", "`", $_POST['loca']);
			$final=str_replace("'", "`", $_POST['final']);
			$capa=str_replace(',', '', $_POST['capa']);
			$dista=str_replace(',', '', $_POST['dista']);
			$descri=str_replace("'", "`", $_POST['descri']);
			$custo=$_POST['custo'];
			$bulk=$_POST['bulk'];
			$etd=$_POST['etd'];
			$eta=$_POST['eta'];
			$a=$_POST['a'];
			$lim=1000;
			
			for($t=1; $t<=$a; $t++){
			$pla=$_POST["plate$t"];
	    if($pla){
$doi=mysqli_query($conn, "SELECT `Plate`, `Driver`, `Tripo` FROM `vehicles` WHERE `Number`='$pla'");
			$roi=mysqli_fetch_assoc($doi);
				$fna=$roi['Plate'];
				$dri=$roi['Driver'];
				$trino=$roi['Tripo'];
				
            $custmo='';
	
			$customer=$_POST['customer'];
			$amo=str_replace(',', '', $_POST['amo']);
			$curr=$_POST['currency'];
			
	     if($_POST['customer']=='290009399190'){
                $custmo="WAITING CARGO";
                $buld="99991695392760";
				
	 $eld = strtotime("+3 day", strtotime("$etd"));
				$eld=date("Y-m-d", $eld);
				
	 $ead = strtotime("+7 day", strtotime("$etd"));
				$ead=date("Y-m-d", $ead);
		
$set=mysqli_query($conn, "INSERT INTO `trips` (`Date`, `Time`, `User`, `Vehicle`, `Destination`, `Location`, `Final`, `Capacity`, `Distance`, `ETD`, `ETA`, `Description`, `Status`, `Plate`, `Driver`, `Stadate`, `Destin`, `Findes`, `ELD`, `EAD`, `Bulk`) VALUES ('$Date', '$Time', '$loge', '$pla', '$desti', '$loca', '$final', '$capa', '$dista', '$etd', '$eta', '$descri', '0', '$fna', '$dri', '$etd', '$loca', '$final', '$eld', '$ead', '$buld')");
        $trino=mysqli_insert_id($conn);
            }
            else{
    $doi=mysqli_query($conn, "SELECT `Customer` FROM `account` WHERE `Number`='$customer'");
			$roi=mysqli_fetch_assoc($doi);
				$custmo=$roi['Customer'];
				$buld=$bulk;
	    
	$so=mysqli_query($conn, "INSERT INTO `income` (`Date`, `Vehicle`, `Distance`, `Driver`, `Weight`, `District`, `Location`, `Descri`, `Amount`,  `Status`, `Rate`, `Customer`, `User`, `Time`, `Trip`, `External`, `Plate`, `Bulk`, `Custo`) VALUES ('$Date', '$pla', '$dista', '$dri', '$capa', '$final', '$loca', '$descri', '$amo', '0', '$curr', '$customer', '$loge', '$Time', '$trino', '1', '$fna', '$bulk', '$custmo')");
            }

	$then=mysqli_query($conn, "UPDATE `vehicles` SET `Tripo`='$trino', `Locate`='', `Loading`='0', `Bulk`='$buld', `Custo`='$custmo' WHERE `Number`='$pla' AND `Plate`='$fna' ORDER BY `Number` ASC LIMIT 1");
	
	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'TRIP', 'Added to $custmo')");	
	
	$then=mysqli_query($conn, "UPDATE `trips` SET `Bulk`='$buld', `Custo`='$custmo' WHERE `Number`='$trino' ORDER BY `Number` ASC LIMIT 1");
		    }
		}
	$conde="AND (`trips`.`Bulk` = '$bulk' AND `vehicles`.`Bulk` = '$bulk')";
		$t=1;
	}
	
	
	
	
	
		
	// ************************* Add customer ***********************			
		
    if(isset($_POST['addco']))
		{
			$bulk=time();
			$date=$_POST['date'];
			$descri=str_replace("'", "`", $_POST['descri']);
			$a=$_POST['a'];
			$lim=1000;
			
			for($t=1; $t<=$a; $t++){
			$pla=$_POST["plate$t"];
	    if($pla){
$doi=mysqli_query($conn, "SELECT `Number`, `Plate`, `Driver` FROM `vehicles` WHERE `Number`='$pla'");
			$roi=mysqli_fetch_assoc($doi);
				$fna=$roi['Plate'];
				$dri=$roi['Driver'];
				$nuo=$roi['Number'];
	
$dos=mysqli_query($conn, "SELECT *FROM `trips` WHERE `Vehicle`='$pla' AND `Status`='0' ORDER BY `Number` DESC LIMIT 1");
$ros=mysqli_fetch_assoc($dos);
$loca=$ros['Location'];
$final=$ros['Final'];
$dista=$ros['Distance'];
$capa=$ros['Capacity'];
$tipo=$ros['Number'];
	
            if($_POST['customer']){
			$customer=$_POST['customer'];
			$amo=str_replace(',', '', $_POST['amo']);
			$curr=$_POST['currency'];
	    
        $doi=mysqli_query($conn, "SELECT `Customer` FROM `account` WHERE `Number`='$customer'");
			$roi=mysqli_fetch_assoc($doi);
				$custmo=$roi['Customer'];
	    
	$so=mysqli_query($conn, "INSERT INTO `income` (`Date`, `Vehicle`, `Distance`, `Driver`, `Weight`, `District`, `Location`, `Descri`, `Amount`,  `Status`, `Rate`, `Customer`, `User`, `Time`, `Trip`, `External`, `Plate`, `Bulk`, `Custo`) VALUES ('$date', '$pla', '$dista', '$dri', '$capa', '$final', '$loca', '$descri', '$amo', '0', '$curr', '$customer', '$loge', '$Time', '$tipo', '1', '$fna', '$bulk', '$custmo')");
	
	
	$then=mysqli_query($conn, "UPDATE `vehicles` SET `Bulk`='$bulk', `Locate`='', `Loading`='0', `Custo`='$custmo' WHERE `Number`='$nuo' ORDER BY `Number` ASC LIMIT 1");
	
	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'CUSTOMER', 'Added to $custmo from waiting')");	
		
	$then=mysqli_query($conn, "UPDATE `trips` SET `Description`='$descri', `Bulk`='$bulk', `Custo`='$custmo' WHERE `Number`='$tipo' ORDER BY `Number` DESC LIMIT 1");
            }
		    }
		}
	$conde="AND (`trips`.`Bulk` = '$bulk' AND `vehicles`.`Bulk` = '$bulk')";
	    $t=0;	
	}
	
	
	
	
	
	
	
	
	// ************************** Create new trip ***********************		
		
if(isset($_POST['tridis']))
		{
			$desti=str_replace("'", "`", $_POST['desti']);
			$loca=str_replace("'", "`", $_POST['loca']);
			$final=str_replace("'", "`", $_POST['final']);
			$capa=str_replace(',', '', $_POST['capa']);
			$dista=str_replace(',', '', $_POST['dista']);
			$descri=str_replace("'", "`", $_POST['descri']);
			$etd=$_POST['etd'];
			$eta=$_POST['eta'];
			$a=$_POST['a'];
			$bulk=time();
			
			
			for($t=1; $t<=$a; $t++){
			$pla=$_POST["plate$t"];
    	if($pla){
$doi=mysqli_query($conn, "SELECT `Plate`, `Driver` FROM `vehicles` WHERE `Number`='$pla'");
			$roi=mysqli_fetch_assoc($doi);
				$fna=$roi['Plate'];
				$dri=$roi['Driver'];
				
	 $eld = strtotime("+3 day", strtotime("$etd"));
				$eld=date("Y-m-d", $eld);
				
	 $ead = strtotime("+7 day", strtotime("$etd"));
				$ead=date("Y-m-d", $ead);
		
$set=mysqli_query($conn, "INSERT INTO `trips` (`Date`, `Time`, `User`, `Vehicle`, `Destination`, `Location`, `Final`, `Capacity`, `Distance`, `ETD`, `ETA`, `Description`, `Status`, `Plate`, `Driver`, `Stadate`, `Destin`, `Findes`, `ELD`, `EAD`, `Bulk`) VALUES ('$Date', '$Time', '$loge', '$pla', '$desti', '$loca', '$final', '$capa', '$dista', '$etd', '$eta', '$descri', '0', '$fna', '$dri', '$etd', '$loca', '$final', '$eld', '$ead', '$bulk')");
        $trino=mysqli_insert_id($conn);
            $customo='';
	
	
			$customer=$_POST['customer'];
			$amo=str_replace(',', '', $_POST['amo']);
			$curr=$_POST['currency'];
			
	     if($_POST['customer']=='290009399190'){
                $customo="WAITING CARGO";
                $bulk="99991695392760";
                $addo="TRIP";
            }
            else{
$doi=mysqli_query($conn, "SELECT `Customer` FROM `account` WHERE `Number`='$customer'");
			$roi=mysqli_fetch_assoc($doi);
				$customo=$roi['Customer'];
				$addo="CUSTOMER";
	    
	$so=mysqli_query($conn, "INSERT INTO `income` (`Date`, `Vehicle`, `Distance`, `Driver`, `Weight`, `District`, `Location`, `Descri`, `Amount`,  `Status`, `Rate`, `Customer`, `User`, `Time`, `Trip`, `External`, `Plate`, `Bulk`, `Custo`) VALUES ('$Date', '$pla', '$dista', '$dri', '$capa', '$final', '$loca', '$descri', '$amo', '0', '$curr', '$customer', '$loge', '$Time', '$trino', '1', '$fna', '$bulk', '$customo')");
    	}
    	
	$then=mysqli_query($conn, "UPDATE `vehicles` SET `Tripo`='$trino', `Bulk`='$bulk', `Locate`='', `Loading`='0', `Custo`='$customo' WHERE `Number`='$pla' AND `Plate`='$fna' ORDER BY `Number` ASC LIMIT 1");
	
	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', '$addo', 'Added to new trip [$customo]')");	
		
	$then=mysqli_query($conn, "UPDATE `trips` SET `Bulk`='$bulk', `Custo`='$customo' WHERE `Number`='$trino' ORDER BY `Number` DESC LIMIT 1");
            
		}
		}
		$t=0;
		}
		
        
        if(isset($_POST['search']))
		    {
			$custoi=$_POST['custoi'];
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$limz=500;
		    
		    if($custoi){
		    $condi="AND `trips`.`Custo` LIKE '%$custoi%' AND `trips`.`Date` BETWEEN '$dato' AND '$datos'";
		    }
		    else{
		    $condi="AND `trips`.`Date` BETWEEN '$dato' AND '$datos'";
		    }
		    }
		else{
		    $condi="AND `trips`.`Offload`='0'";
		    $limz=50;
		}
		
		
	if(isset($_POST['open']))
		{
			$custo=$_POST['custo'];
			$bulk=$_POST['bulk'];
			$_SESSION['Bulk']=$bulk;
			$deloc=$_POST['deloc'];
			$t=1;               $lim=1000;
			
	$conde="AND (`trips`.`Bulk` = '$bulk' AND `vehicles`.`Bulk` = '$bulk')";
		}
		
		
    if(isset($_GET['vehi']) AND isset($_GET['trip']) AND isset($_GET['bulk']))
		{
			$vehi=$_GET['vehi'];
			$trip=$_GET['trip'];
			$bulk=$_GET['bulk'];
			$fna=$_GET['fna'];
			$custo=$_GET['custo'];
			$t=1;               $lim=1000;
			
			if($custo=='WAITING CARGO'){
	$then=mysqli_query($conn, "UPDATE `vehicles` SET `Bulk`='0', `Locate`='ALLOCATED', `Loading`='1', `Custo`='' WHERE `Number`='$vehi' ORDER BY `Number` ASC LIMIT 1");
	
	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'REMOVE', 'Removed from waiting cargo')");	
		
	$deli=mysqli_query($conn, "DELETE FROM `trips` WHERE `Number`='$trip' AND `Bulk`='$bulk' ORDER BY `Number` DESC LIMIT 1");
			}
			else{
            $bullo="99991695392760";
            $custom="WAITING CARGO";
	$then=mysqli_query($conn, "UPDATE `vehicles` SET `Bulk`='$bullo', `Locate`='', `Loading`='0', `Custo`='$custom' WHERE `Number`='$vehi' ORDER BY `Number` ASC LIMIT 1");
	
	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'REMOVE', 'Removed from customer [$custo]')");	
		
	$deli=mysqli_query($conn, "UPDATE `trips` SET `Description`='Removed for waiting', `Custo`='$custom', `Bulk`='$bullo' WHERE `Number`='$trip' AND `Bulk`='$bulk' ORDER BY `Number` DESC LIMIT 1");
			
	$dele=mysqli_query($conn, "DELETE FROM `income` WHERE `Trip`='$trip' AND `Bulk`='$bulk' AND `Vehicle`='$vehi' ORDER BY `Number` DESC LIMIT 1");
			}
			
	$conde="AND (`trips`.`Bulk` = '$bulk' AND `vehicles`.`Bulk` = '$bulk')";
		}
		
		
		
		// ******************** Offload vehicles **********************
    if(isset($_POST['savall']))
		{
			$lokete=str_replace("'", "`", $_POST['lokete']);
			$a=$_POST['a'];
			
			for($t=1; $t<=$a; $t++){
			$pla=$_POST["plate$t"];
			$fna=$_POST["fna$t"];
	if($pla){
	    if($lokete=='DEPANNAGE'){
$doi=mysqli_query($conn, "UPDATE `vehicles` SET `Depane`='1' WHERE `Number`='$pla' ORDER BY `Number` ASC LIMIT 1");

	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'ADDITIONAL FUEL', 'Added vehicle to additional fuel')");
	    }
	    else{
$doi=mysqli_query($conn, "UPDATE `vehicles` SET `Loading`='1', `Bulk`='0', `Custo`='', `Locate`='', `Doff`='$Date', `Depane`='0' WHERE `Number`='$pla' ORDER BY `Number` ASC LIMIT 1");

	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'OFFLOAD', 'Offload vehicle from trip')");	

$then=mysqli_query($conn, "UPDATE `trips` SET `Offload`='1' WHERE `Vehicle`='$pla' ORDER BY `Number` DESC LIMIT 1");
}
		    }
		        }
		}
		
		
if($t){
    $doi=mysqli_query($conn, "SELECT `trips`.* FROM `trips` INNER JOIN `vehicles` ON `trips`.`Vehicle` = `vehicles`.`Number` WHERE `trips`.`Status`='0' AND `trips`.`Offload`='0' $conde GROUP BY `trips`.`Vehicle` ORDER BY `trips`.`ETA` DESC LIMIT $lim");
    }
else{
    $doi=mysqli_query($conn, "SELECT COUNT(DISTINCT(`vehicles`.`Number`)) AS `Vehi`, `trips`.* FROM `trips` INNER JOIN `vehicles` ON `trips`.`Bulk` = `vehicles`.`Bulk` WHERE `trips`.`Status`='0' $condi GROUP BY `trips`.`Bulk` ORDER BY `trips`.`Bulk` DESC LIMIT $limz");
        }
    $foi=mysqli_num_rows($doi);
?>


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

$('input[type="checkbox"]').change(function(){
var total_checked=  $("input[type='checkbox']:checked").length 
$("#d2").html("Selected Vehicles: <b> " + total_checked );
});
/////////////
var total_checked=  $("input[type='checkbox']:checked").length 
$("#d2").html("Selected Vehicles: <b> " + total_checked );
///////

$('input[type="checkbox"]').change(function(){
var total_checked=  $("input[type='checkbox']:checked").length 
$("#d3").html("Selected Vehicles: <b> " + total_checked );
});
/////////////
var total_checked=  $("input[type='checkbox']:checked").length 
$("#d3").html("Selected Vehicles: <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 check_uncheck_checkboxes(isChecked) {
	if(isChecked) {
		$('input[type="checkbox"]').each(function() { 
			this.checked = true; 
		});
	} else {
		$('input[type="checkbox"]').each(function() {
			this.checked = false;
		});
	}
}	

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

</script>

<div class="container-fluid main-content">
        <div class="page-title hidden-xs 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;Allocated 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 active" 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>	
              
            <li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="addinco.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Additional Services
                </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" 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></ul>
  </div>
  
   <?php
          if($_SESSION['Ovt']=='1' OR $_SESSION['Alt']=='1'){
          // *************** Offload vehicles **********************
	include'allocate.php';
          }
   
        include'trimodal.php';
    ?>           
         
        <div class="col-lg-10">
                  <div class="row hidden-print">
         
         <div class="col-lg-5" style="padding-left:30px;">
           <div class="col-lg-3">
               <?php
             if($_SESSION['Cnt']){
                 ?>
       <button class="btn  btn-warning btn-block" type="button" data-toggle='modal' data-target='#Modaldisc' style='font-size:11px;'> Create New Trip </button>
       <?php
             }
             ?>
            </div>
             
          
           
         <form action="milpay.php" method="post" class="form-horizontal" enctype="multipart/form-data" name='myform'> 
           <div class="col-lg-3 hidden-print"> <button class="btn btn-success btn-block" type="submit" style='font-size:11px;'> Mileage/RoadToll </button> </div></form> 
           
          
           <div class="col-lg-3 hidden-print"><?php
           if($_SESSION['Alt']){
               ?>
        <button class="btn  btn-warning btn-block" type="submit" data-toggle='modal' data-target='#Modaldixi' style='font-size:11px;'> Loaded Vehicles </button><?php
        }
        ?></div>
        
        <?php
        	if($_SESSION['Avt']){
        	    ?>
        <form action="offloading.php" method="post" class="form-horizontal" enctype="multipart/form-data" name='myform'> 
           <div class="col-lg-3 hidden-print"> <button class="btn btn-info btn-block" type="submit" style='font-size:11px;'> In Parking </button> </div></form> 
			<?php
        	}
        	?>
        </div>  
           

        <form action="" method="post" class="form-horizontal ">                  
        <div class="col-lg-7 hidden-print"><div class="col-lg-1 text-center"> 				
         </div>
            <div class="col-lg-3">
      <input class="form-control"  name="custoi" type="text" id="searchi" autofocus="autofocus">
			</div> 
			
			 <div class="col-lg-3 hidden-print"> 
           <div class="input-group date" data-provide="datepicker">
      <input class="form-control form-center" 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 hidden-print"> 
           <div class="input-group date" data-provide="datepicker">
      <input class="form-control form-center" 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 hidden-xs hidden-print">
        <button class="btn  btn-primary btn-block" type="submit" name="search"><i class="lnr lnr-magnifier"></i> Search</button>
                   
					  </div>
                         
                  </div>    
                     
                  
            </form> 
             
               
            </div>
             <div class="row">
            <div class="col-lg-12" style='margin-top:-20px;'>
             <span class="hidden-print"> &nbsp;&nbsp; Total Records Found : <b><?php echo" $foi " ?></b></span><span class="pull-right text-right">&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="return window.print()" class="hidden-print" title='Print' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-printer"></i></a>&nbsp;&nbsp;&nbsp;</span>
             <?php
             if($t){
                 ?>
              <span class="pull-right hidden-print">
		&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick=window.location.href='data.php' class="hidden-print" title='Export to excell' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-line-spacing"></i></a></span>
		        <?php
             }
             ?>
		
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">
               
					<?php
			if($t){
	            $t=1;
	            /*
           while($roi=mysqli_fetch_assoc($doi)){
               $loke=$roi['Locate'];
               $dte=$roi['Date'];
               $inra=number_format($roi['Rate'], 2);
               $bulko=$roi['Bulk'];
               $acco=$cust=$custo;
               
    $do=mysqli_query($conn, "SELECT `vehicles`.* FROM `vehicles` WHERE `vehicles`.`Status`='0' AND `vehicles`.`Bulk`='$bulko' ORDER BY `Number` ASC LIMIT 1000");
    */
            $fo=mysqli_num_rows($doi);
        $ipla=$inko=$ino=array();
    echo"<div class='panel panel-default' style='border:1px solid powderblue;'>
			<div class='panel-heading' style='background-color:powderblue; padding-left:30px; font-weight:bold;'> $custo &nbsp; [$deloc] &nbsp;  <label class='pull-right text-info' style='font-weight:bold; padding-right:30px;'> $fo VEHICLES </label></div>
			<form action='' method='post'>
	
			<div class='panel-body' style='padding-top:0px;'> 
			 <table class='table table-striped table-hover' id='htmltable'>     
                    <thead><tr role='row'><th width='1%'> No </th>
                       <th><div align='center'> DATE </th>
                     <th><div align='center'> TRUCK&nbsp;ID </th>
                     <th><div align='center'> TRAILER&nbsp;ID </th>
						 <th><div align='center'> DRIVER'S NAME </th>
					   <th><div align='center'> DRIVING LICENSE </th>
						 <th><div align='center'> PC/LP </th>
						 <th><div align='center'> TELEPHONE </th>
			<th class='hidden-print'><div align='center'> MILEAGE </th>
			<th class='hidden-print'><div align='center'> ROADTOLL </th>
			<th class='hidden-print'><div align='center'> FUEL </th>
						 <th class='hidden-print' width='10px'> # </th></tr>
                    </thead><tbody>";

	  $n=1;				$tm=$tf=$tro=0;                  $dris=array();
		
		 while($reti=mysqli_fetch_assoc($doi)){
			    $trino=$reti['Number'];
			    $desto=$reti['Destination'];
			    $locoo=$reti['Location'];
			    $fino=$reti['Final'];
			    $disto=$reti['Distance'];
			    $capo=$reti['Capacity'];
			    $code=$reti['Vehicle'];
			    $vid=$reti['Plate'];
			    $etao=$reti['ETA'];
			    $etdo=$reti['ETD'];
			    $descro=$reti['Description'];
			    $source=$reti['Driver'];
			    $bul=$bulko=$reti['Bulk'];
			    $cusi=$cust=$reti['Custo'];
			    $offed=$reti['Offload'];
			
		$sete=mysqli_query($conn, "SELECT `employees`.`Idno`, `employees`.`Contact1`, `employees`.`Contact2`, `employees`.`Fullname` FROM `employees` WHERE `Fullname`='$source' ORDER BY `Eid` DESC LIMIT 1");
		    if($fete=mysqli_num_rows($sete)){
			    $rete=mysqli_fetch_assoc($sete);
			        $tle=$rete['Contact1'];
			        $pcl=$rete['Contact2'];
			        $idno=$rete['Idno'];
			        $driv=$rete['Fullname'];
		            }
		        else
		            $tle=$pcl=$idno=$driv="";
			    
			 if(!$source)
			    $source=$driv;
			    $dris[$n]=$source;
			    $trip=$trino;
			    
        $seco=mysqli_query($conn, "SELECT `Number`, `Amount` FROM `income` WHERE `Vehicle`='$code' AND `Status`='0' AND `Trip`='$trino' ORDER BY `Number` ASC LIMIT 1");
               $ipla[$n]=$vid;
        if($feco=mysqli_num_rows($seco)){
            $reco=mysqli_fetch_assoc($seco);
               $inko[$n]=number_format($reco['Amount'], 2);
               $ino[$n]=$reco['Number'];
        }
        else{
            $inko[$n]=$ino[$n]=0;
        }
			    
        $sete=mysqli_query($conn, "SELECT `Bulk` FROM `vehicles` WHERE `Number`='$code' AND `Status`='0'");
                    $rete=mysqli_fetch_assoc($sete);
                            $buko=$rete['Bulk'];
			
        $sete=mysqli_query($conn, "SELECT `Trailer`, `Tripo` FROM `vehicles` WHERE `Number`='$code' AND `Status`='0'");
                    $rete=mysqli_fetch_assoc($sete);
                            $tre=$rete['Trailer'];
                            $tripo=$rete['Tripo'];
                
                if($offed==0 AND $trino==$tripo AND $custo)            
       $seco=mysqli_query($conn, "UPDATE `vehicles` SET `Locate`='', `Custo`='$custo' WHERE `Number`='$code' AND `Custo`='' AND `Bulk`='$bul'");	
        
        $seco=mysqli_query($conn, "UPDATE `repair` SET `Bulk`='$bul' WHERE `Trip`='$trino' AND `Status`='0'");
        
        $seco=mysqli_query($conn, "UPDATE `consumption` SET `Bulk`='$bul' WHERE `Trip`='$trino' AND `Status`='0'");

		$setl=mysqli_query($conn, "SELECT SUM(`Amount`/`Rate`) AS `Mile` FROM `repair` WHERE `Vehicle`='$code' AND `Trip`='$trino' AND `Garage` LIKE '%MILEAGE%' ORDER BY `Number` DESC LIMIT 100");
			    $retl=mysqli_fetch_assoc($setl);
			    $mile=$retl['Mile'];        $mileo=number_format($mile, 2);
			    
		$setr=mysqli_query($conn, "SELECT SUM(`Amount`/`Rate`) AS `Mile` FROM `repair` WHERE `Vehicle`='$code' AND `Trip`='$trino' AND `Garage` LIKE '%ROAD TOLL%' ORDER BY `Number` DESC LIMIT 100");
			    $retr=mysqli_fetch_assoc($setr);
			    $road=$retr['Mile'];        $roado=number_format($road, 2);
			
		$setf=mysqli_query($conn, "SELECT SUM(`Quantity`) AS `Fuel` FROM `consumption` WHERE `Vehicle`='$code' AND `Trip`='$trino' ORDER BY `Number` DESC LIMIT 100");
			    $retf=mysqli_fetch_assoc($setf);
			    $fuel=$retf['Fuel'];        $fuelo=number_format($fuel, 2);
			    $lts="L";
	
	$seti=mysqli_query($conn, "UPDATE `trips` SET `Mileage`='$mile' WHERE `Number`='$trino' AND `Bulk`='$bul' ORDER BY `Number` DESC LIMIT 1");

                if(!$tle OR !$pcl OR !$idno)
					$stn="style='padding:0px; color:blue;'";
				else
					$stn="style='padding:0px;'";
						
						
					if($offed=='1')
						$stn="style='padding:0px; color:red;'";
						
		if($bul!=$buko AND $cusi=='WAITING CARGO')
			$stn="style='padding:0px; color:red;'";
				
		 echo"<div class='modal fade' id='exampleModal$n' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true' style='top:220px;'>
  <div class='modal-dialog' role='document'>
    <div class='modal-content'><div class='modal-header'>
        <h5 class='modal-title' id='exampleModalLabel'>REMOVE CONFIRMATION
        <label class='pull-right'> $vid </label></h5>

      </div><form method='post' action=''>
      <div class='modal-body' style='height:40px;'>
        <h5 style='color:#ff0033'>Are you sure you want to remove this vehicle?</h5>
      </div><input type='hidden' name='vehi' value='$code'><input type='hidden' name='trip' value='$trino'><input type='hidden' name='bulk' value='$bul'><input type='hidden' name='custo' value='$custo'>
      <div class='modal-footer' style='margin-top:-10px; height:50px; padding-top:5px; border:0px solid blue;'>
        <button type='button' class='btn btn-sm btn-success' data-dismiss='modal'>&nbsp;NO&nbsp;</button><a href='tmile.php?vehi=$code&trip=$trip&bulk=$bulk&custo=$cusi&fna=$vid'><button type='button' class='btn btn-sm btn-danger'>YES</button></a>
      </div>
    </div>
  </div>
</div>";

		print("<tr title='Income: $ino'><td $stn><div align='center'> $n </td>
		<td class='text-center' $stn> $etdo </td>
		<td $stn><div align='center'> $vid </td>
		<td $stn><div align='center'> $tre </td>
			  <td $stn> $source </td>
			  <td CLASS='text-center' $stn> $idno </td>
			  <td $stn><div align='left'> $pcl </td>
			  <td $stn><div align='center'> $tle 
		
              <input type='hidden' name='code$n' value='$code'>
              <input type='hidden' name='driv$n' value='$source'>
              <input type='hidden' name='plat$n' value='$vid'>
              <input type='hidden' name='trip$n' value='$trino'>
              <input type='hidden' name='n' value='$n'></td>
              <td class='hidden-print' $stn><div align='right'> $mileo </td><td class='hidden-print' $stn><div align='right'> $roado </td>
        <td class='hidden-print' $stn><div align='right'> $fuelo$lts </td>
	<td class='hidden-print' align='right' style='width:10px; padding:0px 5px 0px 0px;'><div title='Remove' data-toggle='tooltip' data-placement='top'><button type='button' class='btn btn-xs btn-link text-danger hidden-print' style='height:18px; padding:0px; margin:0px;' data-placement='top' data-toggle='modal' data-target='#exampleModal$n'><i class='lnr lnr-trash'></i></button></div></td></tr>");
$n++;           $tm+=$mile;             $tf+=$fuel;             $tro+=$road;
						}
				$tm=number_format($tm, 2);         $tf=number_format($tf, 2);
				$tro=number_format($tro, 2);
				
	echo"</tbody><thead><tr><th style='padding:10px 0px 0px 0px;'><div align='center'><button type='button' class='btn btn-xs btn-warning hidden-print' style='height:18px; padding:0px 8px 0px 8px; margin:0px;' title='Go Back' data-placement='top' onclick=location.href='tmile.php'><i class='lnr lnr-arrow-left-circle' style='margin:0px;'></i></button></th>
		<th colspan='3' style='padding:10px 20px 0px 0px;' class='text-right'>
    <button type='button' class='btn btn-xs btn-default text-primary hidden-print' style='height:18px; padding:0px 10px 0px 10px; margin:0px; width:160px;' title='$cust' data-placement='top' data-toggle='modal' data-target='#pModal$t'> <i class='lnr lnr-license'></i> &nbsp; ADD&nbsp;SERVICES </button></th><th style='padding:10px 20px 0px 0px;' class='text-center'><button type='button' class='btn btn-xs btn-default text-success hidden-print' style='height:18px; padding:0px 10px 0px 10px; margin:0px; color:#ffffff; width:160px;' title='$cust' data-placement='top' data-toggle='modal' data-target='#vModal$t'> <i class='lnr lnr-car'></i> &nbsp; ADD&nbsp;VEHICLE </button></th>";
                
                if($custo=='WAITING CARGO')
    echo"<th colspan='3' style='padding:10px 0px 0px 0px;'><button type='button' class='btn btn-xs btn-default text-success hidden-print' style='height:18px; padding:0px 10px 0px 10px; margin:0px; width:160px; color:#1E8449;' data-toggle='modal' data-target='#cModal$t'> <i class='lnr lnr-dice'></i> &nbsp; ADD&nbsp;CUSTOMER </button></th>";
                else
     echo"<th style='padding:10px 20px 0px 0px;'><button type='button' class='btn btn-xs btn-default text-default hidden-print' style='height:18px; padding:0px 10px 0px 10px; margin:0px; width:160px; color:#000000;' title='$cust' data-placement='top' data-toggle='modal' data-target='#iModal$t'> <i class='lnr lnr-briefcase'></i> &nbsp; SHOW&nbsp;INCOME </button></th><th colspan='2'> </th>";
    
    echo"<th class='hidden-print' style='padding:10px 0px 0px 5px;'><div align='right'> $tm </th><th class='hidden-print' style='padding:10px 0px 0px 5px;'><div align='right'> $tro </th><th class='hidden-print' style='padding:10px 0px 0px 5px;'><div align='right'> $tf$lts </th><th style='padding:10px 0px 0px 0px;'> </th></tr>
    
                </thead></table>";
                
        $_SESSION['excell']='texport.php';
				
		// ******************** Mileage/Road Toll *************************
		echo"<div class='modal fade' id='pModal$t' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true'>
  <div class='modal-dialog' role='document'><div class='modal-content'>
  <div class='modal-header text-left'>
        <h5 class='modal-title' id='exampleModalLabel'> ADD SERVICES
        <span class='pull-right'> $cust </span></h5></div>
        
    <div class='modal-body text-left'>
			<div class='row'><div class='col-md-12'>

	<div class='col-md-6' style='padding-right:0px;'>
	<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Total Amount/Vehicle <br>
	<input name='amo' class='form-control text-center' type='text' onkeyup='format(this);' onkeypress='return isNumberKey(event)' title='Road Toll' data-toggle='tooltip' data-placement='top' required>
  </div>

	<div class='col-md-4' style='padding-left:0px;'>
	<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Currency <br>
    <select class='form-control' name='curre' style='font-size:14px; padding-top:4px;' required><option value='' selected='selected'> &nbsp;&nbsp; Select Currency</option>";
			 
			$doil=mysqli_query($conn, "SELECT `Code`, `Rate` FROM `currency` WHERE `Code`!='' ORDER BY `Code` ASC");
			while($roil=mysqli_fetch_assoc($doil)){
				$fna=$roil['Code'];
				$rate=$roil['Rate'];
				$rte=number_format($rate, 2);
				if($curr==$fna)
					$sc="selected";
				else
					$sc="";
			echo"<option value='$rate' $sc> $fna @ $rte </option>";
			}
		
		   echo"</select></div>
		   
		   
			<div class='col-md-2'><br>&nbsp;&nbsp;&nbsp;</div>

			</div></div>
			
			
			
				<div class='row'><div class='col-md-12'>
			<div class='col-md-6'><br>&nbsp;&nbsp;&nbsp;Driver`s Name <br>
       <select class='form-control' name='drive' style='padding-left:1px; padding-right:0px; font-size:14px; padding-top:4px;' required>
		   <option value='' selected>SELECT DRIVER</option>
	<option value='ALL DRIVERS' style='color:blue;'>ALL DRIVERS</option>";
		   
		   for($a=1; $a<$n; $a++){
		       $drv=$dris[$a];
		 echo"<option value='$drv'>$drv</option>";
		   }
		   
		   
	        echo"</select></div>
	<div class='col-md-6'><br>&nbsp;&nbsp;&nbsp;Payment Type <br>
		   <select class='form-control' name='type' required>
				<option value=''> Select Type </option>
			<option value='MILEAGE'> MILEAGE </option>
			<option value='ROAD TOLL'> ROAD TOLL </option>
			<option value='OTHER EXPENSE'> OTHER EXPENSE </option>
			<option value='GPS SERVICES'> GPS SERVICES </option>
			<option value='SALARY'> SALARY </option></select>
            </div>
            
            
            
            </div></div>
			
		<div class='row'><div class='col-md-12'><br>	
		<textarea class='form-control' name='descri' style='height:60px; font-size:14px;' placeholder='Description ...'></textarea></div>
			</div></div>
			<hr><input type='hidden' name='code' value='$code'>
      <div class='modal-footer' style='margin-top:-10px; height:50px; padding-top:5px; border:0px solid blue;'>
      
<div class='col-md-2 text-right' style='padding-top:8px;'> Due&nbsp;Date </div>
      <div class='col-md-4 text-right'>

		   <div class='input-group date' data-provide='datepicker'>
      <input class='form-control form-center' name='dato' type='text' value='$Date' onkeypress='return isNumberKey(event)' required>
	  <span class='input-group-addon'><i class='lnr lnr-calendar-full'>
	  </i></span></div>
			</div><div class='col-md-6 text-right'>
              <input type='hidden' name='custo' value='$cust'>
              <input type='hidden' name='bulk' value='$bulko'>
              <input type='hidden' name='n' value='$n'>
			
        <button type='button' class='btn btn-sm btn-danger' data-dismiss='modal' style='width:85px; height:33px;'> CANCEL </button>
        <button type='submit' name='toll' class='btn btn-sm btn-success' style='width:85px; height:33px;'> SAVE </button>
      </div></div></form>
    </div>
  </div>
</div>";



    // ********************** Show income *************************
 echo"<div class='modal fade' id='iModal$t' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true'>
  <div class='modal-dialog' role='document'>
    <div class='modal-content'><div class='modal-header'>
        <h5 class='modal-title' id='exampleModalLabel'>SHOW INCOME
        <label class='pull-right'> $cust </label></h5>

      </div>
      <div class='modal-body'>";
      
     for($k=1; $k<$n; $k++){
     $plas=$ipla[$k];
     $inos=$inko[$k];
     $nuwo=$ino[$k];
     
    echo"<div class='col-md-3 text-right'> &nbsp;&nbsp; $plas </div><div class='col-md-3'><input name='inos$k' class='form-control' type='text' style='text-align:center; height:20px; margin: 0px 0px 10px 0px; padding-top:1px; padding-bottom:1px; font-size:12px;' onkeyup='format(this);' onkeypress='return isNumberKey(event)' value='$inos'><input type='hidden' value='$inos' name='inko$k'><input type='hidden' value='$nuwo' name='ino$k'></div>";
    
    if($k%4==0)
    echo"<div class='row'> </div><hr style='margin-top:10px; margin-bottom:10px;'>";
     }
     
    echo"<div class='row'> </div></div><input type='hidden' name='vehi' value='$code'><input type='hidden' name='trip' value='$trino'><input type='hidden' name='bulk' value='$bul'><input type='hidden' name='custo' value='$custo'><input type='hidden' value='$k' name='k'>
    
      <div class='modal-header text-right' style='margin-top:0px; height:60px; padding-top:15px;'>
        <button type='button' class='btn btn-sm btn-default' data-dismiss='modal'>&nbsp;&nbsp;&nbsp;&nbsp;CLOSE&nbsp;&nbsp;&nbsp;&nbsp;</button>
      </div></form>
    </div>
  </div>
</div>";




 // *************** Add vehicle on existing trip **********************
	    echo"<div class='modal fade' id='vModal$t' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true'>
  <div class='modal-dialog' role='document' style='width:70%;'>
  <div class='modal-content'><div class='modal-header text-left'>
        <h5 class='modal-title' id='exampleModalLabel'> ADD VEHICLE 
        <span class='pull-right'> $cust </span></h5>

      </div><form action='' method='post'>
      <div class='modal-body text-left'><div class='row'>

    <div class='col-md-6 text-center' style='height:380px; overflow-x:auto;'>";
        
        $a=1;
        if($custo=='WAITING CARGO')
    $doc=mysqli_query($conn, "SELECT `Locate` FROM `vehicles` WHERE `Status`='0' AND `Custo`='' AND `Locate`!='' GROUP BY `Locate` ORDER BY `Locate` ASC");
    else
    $doc=mysqli_query($conn, "SELECT `trips`.`Destination` FROM `trips` INNER JOIN `vehicles` ON `trips`.`Vehicle` = `vehicles`.`Number` WHERE `trips`.`Status`='0' AND `trips`.`Custo`='WAITING CARGO' AND `trips`.`Offload`='0' AND `vehicles`.`Custo`='WAITING CARGO' GROUP BY `vehicles`.`Custo` ORDER BY `trips`.`Number` DESC");
           while($roc=mysqli_fetch_assoc($doc)){
               $lco=$roc['Destination'];
               $lce=$roc['Locate'];
               
        if($custo=='WAITING CARGO')
    $dov=mysqli_query($conn, "SELECT `Number`, `Plate`, `Locate` FROM `vehicles` WHERE `Status`='0' AND `Locate`='$lce' AND `Custo`='' ORDER BY `Plate` ASC");
        else{
    $dov=mysqli_query($conn, "SELECT `Number`, `Plate`, `Custo` FROM `vehicles` WHERE `Status`='0' AND `Custo`='WAITING CARGO' AND `Loading`='0' ORDER BY `Plate` ASC");
            $lco='WAITING CARGO';
        }
            $fov=mysqli_num_rows($dov);
    
    echo"<div class='panel panel-default' style='border:1px solid #d8d7d6; background-color:transparent; border-radius:5px;'><div class='panel-heading' style='background-color:#d8d7d6; padding-left:30px; text-align:left; font-weight:bold;'> $lco $lce
	<label class='pull-right text-info' style='font-weight:bold; padding-right:30px;'> $fov VEHICLES </label></div>";
	
		while($rov=mysqli_fetch_assoc($dov)){
			$numb=$rov['Vehicle'];
			if(!$numb)
			$numb=$rov['Number'];
			$plate=$rov['Plate'];
			$bd=$rov['Destination'];
			if(!$bd)
			$bd=$rov['Locate'];
			if($bd=='GARAGE'){
			$bor="border:1px solid red;";
			$tle="$bor";
			$dico="disabled";
			}
			else{
			$bor="";
			$tle="$plate";
			$dico="";
			}
	echo"<label class='btn btn-default active' for='mi$a' style='width:105px; margin:4px; $bor' title='$tle' data-toggle='tooltip' data-placement='top'>
          <input id='mi$a' type='checkbox' value='$numb' name='plate$a' style='border:0px; height:12px;' $dico> $plate </label>";
                 $a++;
		}
		 echo"<div class='clearfix'></div>
            </div>";
           }
    
    
    
    echo"</div><div class='col-md-6'>
    
        	 	
		 <div class='form-group'>	 
			  <div class='col-md-4' align='right'>
			  Trip Destination</div>	

	 <div class='col-md-8'>
          <input name='desti' class='form-control' type='text' value='$desto'>
            </div>
		</div>

		<div class='form-group'>	 
			  <div class='col-md-4' align='right'>
			  Package Location</div>	

	 <div class='col-md-8'>
          <input name='loca' class='form-control' type='text' value='$locoo'>
            </div>
		</div>


		 <div class='form-group'>
			<div class='col-md-4' align='right'> 
            Final Destination</div>
            <div class='col-md-8'>
           <input name='final' class='form-control' type='text' value='$fino'>
            </div>
		</div>


		 <div class='form-group'>
			<div class='col-md-4' align='right'> 
            Capacity / Distance</div>
            <div class='col-md-4'><div class='input-group'>
           <input name='capa' class='form-control' type='text' style='text-align:center;' onkeyup='format(this);' onkeypress='return isNumberKey(event)' value='$capo'><span class='input-group-addon text-info'>TON</span>
            </div></div>
            <div class='col-md-4'><div class='input-group'>
           <input name='dista' class='form-control' type='text' style='text-align:center;' value='$disto' onkeyup='format(this);' onkeypress='return isNumberKey(event)'><span class='input-group-addon text-info'>KM</span></div></div>
		</div>
		
		<div class='form-group'>
			<div class='col-md-4' align='right'> 
            <br class='hidden-xs'>ETD / ETA</div>
        <div class='col-md-4' style='padding-right:0px;'><br class='hidden-xs'><div class='input-group date' data-provide='datepicker'><input class='form-control form-center' name='etd' type='text' value='$etdo' onkeypress='return isNumberKey(event)' style='padding-left:2px; padding-right:2px;' required><div class='input-group-addon'><span class='lnr lnr-calendar-full'></span></div></div>
        </div>
            
        <div class='col-md-4' style='padding-left:0px;'><br class='hidden-xs'><div class='input-group date' data-provide='datepicker'><input class='form-control form-center' name='eta' type='text' value='$etao' onkeypress='return isNumberKey(event)' style='padding-left:2px; padding-right:2px;' required><div class='input-group-addon'><span class='lnr lnr-calendar-full'></span></div></div>
             </div></div>
		

		<div class='form-group'>
   <div class='col-md-4' align='right'><br class='hidden-xs'>
		Description</div>

 <div class='col-md-8'>
<textarea class='form-control' name='descri' style='height:60px; font-size:14px;'>$descro</textarea>
	</div></div>
	
	
            
		<div class='form-group'>	 
			  <div class='col-md-4' align='right'><br class='hidden-xs'><br class='hidden-xs'> Customer Name</div>	

	 <div class='col-md-8'><br class='hidden-xs'><br class='hidden-xs'>
    <select class='form-control' name='customer' style='font-size:16px; height:34px;' required><option value=''> Select a Customer </option>";
	 
	 if($cust=='WAITING CARGO'){
	 echo"<option value='290009399190' selected> WAITING CARGO </option>";
	 $amoo="0.00";               $inrate=$reite;
	 }
	 else{
	$inco=mysqli_query($conn, "SELECT `Amount`, `Rate` FROM `income` WHERE `Bulk`='$bulko' AND `Status`='0' ORDER BY `Number` DESC LIMIT 1");
	$rinco=mysqli_fetch_assoc($inco);
	    $inrate=number_format($rinco['Rate'], 2);
	    $amoo=number_format($rinco['Amount'], 2);
	    
$doz=mysqli_query($conn, "SELECT `Number`, `Customer` FROM `account` WHERE `Status`='0' AND `Customer`='$cust' ORDER BY `Customer` ASC LIMIT 1");
		$roz=mysqli_fetch_assoc($doz);
			$custe=$roz['Customer'];
			$cods=$roz['Number'];
	echo"<option value='$cods' selected> $custe </option>";
		}
		
		echo"</select>
            </div>
		</div>
		
			<div class='form-group'>	 
			  <div class='col-md-4' align='right'><br class='hidden-xs'> Amount/Vehicle </div>	

	 <div class='col-md-5'><br class='hidden-xs'>
    <input class='form-control form-center' name='amo' type='text' onkeyup='format(this);' onkeypress='return isNumberKey(event)' value='$amoo' required></div>
        
    <div class='col-md-3'><br class='hidden-xs'>
    <select class='form-control' name='currency' required>
				<option value='' selected='selected'>Currency</option>";
			 
			$doix=mysqli_query($conn, "SELECT `Code`, `Rate` FROM `currency` WHERE `Code`!='' ORDER BY `Code` ASC");
			while($roix=mysqli_fetch_assoc($doix)){
				$fna=$roix['Code'];
				$rate=$roix['Rate'];
				$rte=number_format($rate, 2);
				if($inrate==$rte)
			echo"<option value='$rate' selected> $fna @ $rte </option>";
			    else
			echo"<option value='$rate'> $fna @ $rte </option>";
			}
			
		  echo"</select>
            </div>
		</div>
		
		
		</div>
	
	</div></div>
	    
	            <input type='hidden' name='a' value='$a'>	
	<div class='modal-header text-right' style='height:50px; padding-top:15px;'>";
	
	if($a>1){
              echo"<label class='pull-left text-left' style='margin-left:40px;'><input type='hidden' name='custo' value='$cust'>
	<input type='hidden' value='$bulko' name='bulk'>
	
	<input type='checkbox' name='checkall' id='checkall' style='height:22px; width:24px; margin-top:0px;' onClick='check_uncheck_checkboxes(this.checked);'> &nbsp; <button type='button' class='btn btn-secondary' id='d2' style='margin-top:-10px;' for='checkall'></button></label>
    
		<button type='button' class='btn btn-xs btn-danger' data-dismiss='modal' style='width:80px;'> CANCEL </button>
        <button type='submit' name='savalle' class='btn btn-xs btn-success' style='width:80px;'> SAVE </button>";
	}
	else
	echo"<label class='pull-left text-left' style='margin-left:40px;'> </label><button type='button' class='btn btn-xs btn-danger' data-dismiss='modal' style='width:80px;'> CANCEL </button>";
        
        echo"</div>
      </form>
  </div></div>
</div>";










          if($custo=='WAITING CARGO'){
 // *************** Add customer on existing trip **********************
	    echo"<div class='modal fade' id='cModal$t' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true'>
  <div class='modal-dialog' role='document' style='width:70%;'>
  <div class='modal-content'><div class='modal-header text-left'>
        <h5 class='modal-title' id='exampleModalLabel'> ADD CUSTOMER 
        <span class='pull-right'> </span></h5>

      </div><form action='' method='post'>
      <div class='modal-body text-left'><div class='row'>

    <div class='col-md-6 text-center' style='height:380px; overflow-x:auto;'>";
        
        $a=1;
    $dov=mysqli_query($conn, "SELECT `Number`, `Plate`, `Locate` FROM `vehicles` WHERE `Status`='0' AND `Trip`='1' AND `Locate`='' AND `Loading`='0' AND `Custo`='WAITING CARGO' ORDER BY `Plate` ASC");
            $fov=mysqli_num_rows($dov);
    
    echo"<div class='panel panel-default' style='border:1px solid #d8d7d6; background-color:transparent; border-radius:5px;'><div class='panel-heading' style='background-color:#d8d7d6; padding-left:30px; text-align:left; font-weight:bold;'> &nbsp;
	<label class='pull-right text-info' style='font-weight:bold; padding-right:30px;'> $fov VEHICLES </label></div>";
	
		while($rov=mysqli_fetch_assoc($dov)){
			$numb=$rov['Number'];
			$plate=$rov['Plate'];
			$bd=$rov['Locate'];
			if($bd=='GARAGE'){
			$bor="border:1px solid red;";
			$tle="$bor";
			}
			else{
			$bor="";
			$tle="$plate";
			}
	echo"<label class='btn btn-default active' for='mai$a' style='width:105px; margin:4px;' title='$tle' data-toggle='tooltip' data-placement='top'>
          <input id='mai$a' type='checkbox' value='$numb' name='plate$a' style='border:0px; height:12px;'> $plate </label>";
                 $a++;
		}
		 echo"<div class='clearfix'></div>
            </div>";
           
    
    
    echo"</div><div class='col-md-6'>
    
    
    <div class='form-group'>
			<div class='col-md-4' align='right'> Due Date</div>
        <div class='col-md-8' style='padding-right:0px;'>
    <div class='input-group date' data-provide='datepicker'><input class='form-control form-center' name='date' type='text' value='$Date' onkeypress='return isNumberKey(event)' style='padding-left:2px; padding-right:2px;' required><div class='input-group-addon'><span class='lnr lnr-calendar-full'></span></div></div>
        </div>
            
		<div class='form-group'>	 
			  <div class='col-md-4' align='right'><br class='hidden-xs'> Customer Name</div>	

	 <div class='col-md-8'><br class='hidden-xs'>
          <select class='form-control' name='customer' style='font-size:16px; height:34px;'>
	 <option value=''> Select a Customer </option>";

$doz=mysqli_query($conn, "SELECT `Number`, `Customer` FROM `account` WHERE `Status`='0' ORDER BY `Customer` ASC");
		while($roz=mysqli_fetch_assoc($doz)){
			$custe=$roz['Customer'];
			$cods=$roz['Number'];
			if($cust==$custe)
	echo"<option value='$cods' selected> $custe </option>";
	        else
	echo"<option value='$cods'> $custe </option>";
		}
		
		echo"</select>
            </div>
		</div>
		
			<div class='form-group'>	 
			  <div class='col-md-4' align='right'><br class='hidden-xs'> Amount/Vehicle </div>	

	 <div class='col-md-5'><br class='hidden-xs'>
    <input class='form-control form-center' name='amo' type='text' onkeyup='format(this);' onkeypress='return isNumberKey(event)' required></div>
        
    <div class='col-md-3'><br class='hidden-xs'>
    <select class='form-control' name='currency' required>
				<option value='' selected='selected'>Currency</option>";
			 
			$doix=mysqli_query($conn, "SELECT `Code`, `Rate` FROM `currency` WHERE `Code`!='' ORDER BY `Code` ASC");
			while($roix=mysqli_fetch_assoc($doix)){
				$fna=$roix['Code'];
				$rate=$roix['Rate'];
				$rte=number_format($rate, 2);
				if($inra==$rte)
			echo"<option value='$rate' selected> $fna @ $rte </option>";
			    else
			echo"<option value='$rate'> $fna @ $rte </option>";
			}
	
		  echo"</select>
            </div>
		</div>
		

    	<div class='row'><div class='col-md-2'><br class='hidden-xs'><br class='hidden-xs'></div>
    	
    	<div class='col-md-10'><br class='hidden-xs'><br class='hidden-xs'>	
		<textarea class='form-control' name='descri' style='height:60px; font-size:14px;' placeholder='Description ...'></textarea></div>
			</div></div>
		
		
		</div>
	
	</div></div>
	    
	            <input type='hidden' name='a' value='$a'>	
	<div class='modal-header text-right' style='height:50px; padding-top:15px;'>
	
	<label class='pull-left text-left' style='margin-left:40px;'>
              <input type='hidden' name='custo' value='$cust'>
	<input type='hidden' value='$bulko' name='bulk'>
	
	<input type='checkbox' name='checkall' id='checkall' style='height:22px; width:24px; margin-top:0px;' onClick='check_uncheck_checkboxec(this.checked);'> &nbsp; <button type='button' class='btn btn-secondary' id='d3' style='margin-top:-10px;'></button></label>
    
		<button type='button' class='btn btn-xs btn-danger' data-dismiss='modal' style='width:80px;'> CANCEL </button>
        <button type='submit' name='addco' class='btn btn-xs btn-success' style='width:80px;'> SAVE </button></div>
      </form>
  </div></div>
</div>";
}
    
        echo"</form>
    <div class='clearfix'></div>
            </div>
        </div>";
        
                $t++;
  //  }
					}
					else{
	echo"<div class='table-responsive'>
<table class='table table-striped table-hover table-sm' style='font-size:8px;'>
	<thead><tr role='row'><th width='4px'> &nbsp;No&nbsp;</th>
		<th class='text-center' width='8%'> Date </th>
		<th> Customer </th><th> Vehicles </th>
    <th> Destination </th><th> Location </th><th> Final </th>
    <th> Load&nbsp;Size </th><th> Distance </th>
    <th><div align='center'> Income </th>
   <th><div align='center'> Mileage </th>
   <th><div align='center'> RoadToll </th>
   <th><div align='center'> Fuel </th>
  <th class='hidden-print' style='width:20px; text-align:center;'> # </th></tr>
                    </thead><tbody>";
		
					$n=1;									$balo="0.00";
					
	$tve=$tinco=$tmi=$tro=$tfuel=0;
		while($ro=mysqli_fetch_assoc($doi)){
$code=$ro['Number'];
$pla=$ro['Vehi'];
$loce=$ro['Location'];
$dicha=$ro['Final'];
$dese=$ro['Destination'];
$desc=$ro['Description'];
$capa=$ro['Capacity'];									
$capao=number_format($capa, 2);
$caps=number_format($capa);
$dista=$ro['Distance'];									
$distao=number_format($dista, 2);
$dists=number_format($dista);
$bulk=$ro['Bulk'];
$cusi=$ro['Custo'];
    $tco=0;
    $deloc="$dese / $loce - $dicha";

$d='KM';
$c='TON';

$etd=$ro['ETD'];
$eta=$ro['ETA'];

if($cusi=='WAITING CARGO')
$stl="style='padding:1px; font-size:12px; color:blue;'";
else
$stl="style='padding:1px; font-size:12px;'";

if($fo=='1')
	$ttl="";
else
	$ttl="title='$desc &nbsp; $bulk'";
				
	$inco=mysqli_query($conn, "SELECT SUM(`Amount`*`Rate`) AS 'Amon', `Date` FROM `income` WHERE `Status`='0' AND `Bulk`='$bulk'");
	   if($finco=mysqli_num_rows($inco)){
	$rinco=mysqli_fetch_assoc($inco);
	    $tco=$rinco['Amon'];
	    $dte=$rinco['Date'];
		}
	    $tcoo=number_format($tco, 2);
	    
	    if(!$tco)
    $dte=$ro['Date'];
    
    if($cusi=='WAITING CARGO')
        $dte=$Date;
	    
	    $tcam=$tcar=$tfe=0;
	$char=mysqli_query($conn, "SELECT SUM(IF(`repair`.`Garage`='MILEAGE', `repair`.`Amount`, 0)) AS 'Amom', SUM(IF(`repair`.`Garage`='ROAD TOLL', `repair`.`Amount`, 0)) AS 'Amor' FROM `repair` INNER JOIN `vehicles` ON `repair`.`Vehicle` = `vehicles`.`Number` WHERE `vehicles`.`Bulk`='$bulk' AND `repair`.`Bulk`='$bulk' AND `repair`.`Status`='0'");
	$rar=mysqli_fetch_assoc($char);
	    $tcam=$rar['Amom'];
	    $tcamo=number_format($tcam, 2);
	    $tcar=$rar['Amor'];
	    $tcaro=number_format($tcar, 2);
	    
    $fl=mysqli_query($conn, "SELECT SUM(`consumption`.`Quantity`) AS 'Amol' FROM `consumption` INNER JOIN `vehicles` ON `consumption`.`Vehicle` = `vehicles`.`Number` WHERE `vehicles`.`Bulk`='$bulk' AND `consumption`.`Bulk`='$bulk' AND `consumption`.`Status`='0'");
	$rl=mysqli_fetch_assoc($fl);
	    $tfe=$rl['Amol'];
	    $tfeo=number_format($tfe, 2);
	    $lts="L";
	    
print("<tr $ttl data-toggle='tooltip' data-placement='top'>
<td $stl><div align='center'> $n&nbsp;</td>
<td $stl><div align='center'>$dte</td>
<td $stl>$cusi</td><td $stl><center>$pla</td><td $stl> $dese </td>
<td $stl> $loce </td><td $stl>&nbsp;&nbsp;$dicha </td>
	<td $stl><div align='right'>&nbsp;$capao&nbsp;$c&nbsp;</td>
    <td $stl><div align='right'>&nbsp;$distao&nbsp;$d&nbsp;</td>
	<td $stl><div align='right'>&nbsp;$tcoo&nbsp;</td>
	<td $stl><div align='right'>&nbsp;$tcamo&nbsp;</td>
	<td $stl><div align='right'>&nbsp;$tcaro&nbsp;</td>
	<td $stl><div align='right'>&nbsp;$tfeo$lts&nbsp;</td>				  
			<form method=post action=''>
	<td class='hidden-print' align='right' style='width:20px; padding:0px;'>
              <input type='hidden' name='bulk' value='$bulk'>
              <input type='hidden' name='custo' value='$cusi'>
              <input type='hidden' name='deloc' value='$deloc'>
    <button type='submit' name='open' class='btn btn-xs btn-success hidden-print' style='height:18px; padding:0px; margin:3px;'>&nbsp;&nbsp;<i class='lnr lnr-download'></i>&nbsp;&nbsp;</button></td></form><tr>");
        $n++;                   $tfuel+=$tfe;               $tro+=$tcar;
    
    $tve+=$pla;             $tinco+=$tco;           $tmi+=$tcam;
		}
		$tve=number_format($tve);
		$tinco=number_format($tinco, 2);
		$tmi=number_format($tmi, 2);
		$tro=number_format($tro, 2);
		$tfuel=number_format($tfuel, 2);
		
		    if($foi>1)
		echo"<tr><th colspan='3'> </th><th><div align='center'> $tve </th>
		<th colspan='5'> </th><th style='padding-right:0px;'><div align='right'> $tinco </th><th style='padding-right:0px;'><div align='right'> $tmi </th><th style='padding-right:0px;'><div align='right'> $tro </th><th style='padding-right:0px;'><div align='right'> $tfuel$lts </th><th> </th></tr>";
		
	echo"</table></div><br>";
					    
					}
		?>
	
            
            </div></div><span class="pull-right text-right">&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="return window.print()" class="hidden-print" title='Print' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-printer"></i></a>&nbsp;&nbsp;&nbsp;</span>
             <?php
             if($t){
                 ?>
              <span class="pull-right hidden-print">
		&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick=window.location.href='data.php' class="hidden-print" title='Export to excell' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-line-spacing"></i></a></span>
		        <?php
             }
             ?>
                  </div>
      
   </div></div></div>  
   <?php
   include'footer.php';
   ?>
