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


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

 // *************************** Add payment *******************************
    if(isset($_POST['payall']))
		{
			$n=$_POST['n'];
			$date=$_POST['date'];
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
				$p=$_POST['p'];
			    $twy=0;
			
		for($i=1; $i<$n; $i++){
			$veh=$_POST["veh$i"];
			$pla=$_POST["plat$i"];
			$dri=$_POST["driv$i"];
			$code=$_POST["trip$i"];
			$buls=$_POST["bulk$i"];
			$pri=str_replace(',', '', $_POST["amo$i"]);
			$curre=$_POST["curre$i"];
			$roa=str_replace(',', '', $_POST["roa$i"]);
			$curres=$_POST["curres$i"];
			
			if(!$curre)
			    $curre=1;
			if(!$pri)
			    $pri=1;
			    
			$amo=$pri*$curre;
			$type="MILEAGE";
			$descri="Mileage payment";
			$amoo=number_format($amo, 2);
			
			
			$amos=$roa*$curres;
			$types="ROAD TOLL";
			$descris="Road toll payment";
				
				if($amo>1){
	$sete=mysqli_query($conn, "INSERT INTO `repair` (`Vehicle`, `Amount`, `Garage`, `Items`, `Issue`, `Date`, `Repair`, `Driver`, `Trip`, `User`, `Time`, `External`, `Plate`, `Type`, `Rate`, `Bulk`) VALUES ('$veh', '$amo', '$type', '0', '$descri', '$date', '', '$dri', '$code', '$loge', '$Time', '1', '$pla', '$twy', '$curre', '$buls')");

	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$date', '$Time', '$loge', '$pla', 'MILEAGE', 'Paid $amoo for mileage')");	
	
	$seti=mysqli_query($conn, "UPDATE `trips` SET `Mileage`='$amo' WHERE `Number`='$code' AND `Bulk`='$buls' ORDER BY `Number` DESC LIMIT 1");
	
	$pto=10;
				}
				
				
				
				if($amos>1){
	$sete=mysqli_query($conn, "INSERT INTO `repair` (`Vehicle`, `Amount`, `Garage`, `Items`, `Issue`, `Date`, `Repair`, `Driver`, `Trip`, `User`, `Time`, `External`, `Plate`, `Type`, `Rate`, `Bulk`) VALUES ('$veh', '$amos', '$types', '0', '$descris', '$date', '', '$dri', '$code', '$loge', '$Time', '1', '$pla', '$twy', '$curres', '$buls')");

	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$tate', '$Time', '$loge', '$pla', 'ROAD TOLL', 'Paid $amoo for road toll')");	
	
	$pto=10;
				}
		    }
			
		}
		

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

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

<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>	
              
            <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 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">
           
    <?php
if($pto==10)
echo"<center><div class='alert alert-danger' style='width:48%;text-align:center;float:center;background-color: #60c560;color: #ffffff;border-radius:5px;'>
		<i class='lnr lnr-checkmark-circle'></i> &nbsp;&nbsp; <button class='close' data-dismiss='alert' type='button'>x</button>Mileage has been saved successfully.
		</div></center>";
	?>
            <div class="col-lg-6"><div class="col-lg-1"> </div>
           
            <form action="miler.php" method="post"><div class="col-lg-3">
       <button class="btn  btn-success btn-block" type="submit" style='font-size:12px;'> Create New Voucher </button></div></form>
           
         <form action="tmile.php" method="post"> 
           <div class="col-lg-3 hidden-print"> <button class="btn btn-info btn-block" type="submit" style='font-size:12px;'> Trip Report </button> </div></form>
           
         <form action="milrepo.php" method="post"> 
           <div class="col-lg-3 hidden-print"> <button class="btn btn-warning btn-block" type="submit" style='font-size:12px;'> Mileage Report </button></div></form>
           
           <div class="col-lg-2"> </div>
           </div>

        <form action="" method="post" class="form-horizontal ">                 
     <div class="col-lg-6 hidden-print" style="padding-right:20px;"> 

<div class="col-lg-2"> </div> 
                       
             <div class="col-lg-4"> 
           <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-4"> 
           <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 `trips` WHERE `Status`='0' AND `Mileage`='0' $conde ORDER BY `Date` ASC");
		        else
		$do=mysqli_query($conn, "SELECT *FROM `trips` WHERE `Status`='0' AND `Mileage`='0' ORDER BY `Number` DESC LIMIT 200");
		
		$sede=mysqli_query($conn, "SELECT `Number`, `Tripo` FROM `vehicles` WHERE `Depane`='1'");
				if($fo=mysqli_num_rows($do)+mysqli_num_rows($sede)){
				?>
                 
            <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;'> No </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; width:8%;'><div align='center'> Mileage </th>
	<th style='padding:1px; width:5%;'><div align='center'> Currency </th>
	<th style='padding:1px; width:8%;'><div align='center'> RoadToll </th>
	<th style='padding:1px; width:5%;'><div align='center'> Currency </th>
                </tr>
                    </thead>
                                        <tbody>
					<?php
			$i=1;		            	$tra=$tmi=$tpa=$toth=$rt=0;
	while($ro=mysqli_fetch_assoc($do)){
				$nuo=$ro['Number'];
				$trip=$ro['Number'];
				$dte=$ro['Date'];
				$pla=$ro['Plate'];
				$vehs=$ro['Vehicle'];
				$etd=$ro['ETD'];
				$des=$ro['Destination'];
				$loce=$ro['Location'];
				$dri=$ro['Driver'];
				$fni=$ro['Final'];
				$cut=$ro['Custo'];
				$bul=$ro['Bulk'];
				$roa=$mil=$val=$tic=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']=='MILEAGE')
				$mil+=$rois['Amount']*$rate;
			if($rois['Garage']=='ROAD TOLL')
				$roa+=$rois['Amount']*$rate;
				
				if(!$dri)
				    $dri=$rois['Driver'];
			    }
			    
			    $deso="$des / $loce - $fni";
			
			
						$valo=number_format($val, 2);
						$milo=number_format($mil, 2);
						$rato=number_format($rat, 2);
						$bal=$val-$mil;
						$balo=number_format($bal, 2);
						$too=number_format($to, 2);
						$tico=number_format($tic, 2);
						$roao=number_format($roa, 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'";


	    echo"<tr><td class='text-center' style='width:5px; $stl $clr'> $i </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='plat$i' value='$pla'>
	  <input type='hidden' name='veh$i' value='$vehs'>
	  <input type='hidden' name='driv$i' value='$dri'>
	  <input type='hidden' name='trip$i' value='$trip'>
	  <input type='hidden' name='bulk$i' value='$bul'></td>
	<td style='$stl $clr'><div align='right'> 
	<input name='amo$i' class='amount form-control' type='text' onkeyup='format(this); findTotal();' onkeypress='return isNumberKey(event);' style='text-align:right; width:100%; height:24px; color:blue; margin:0px 0px 0px 0px; padding:0px 10px 0px 0px; margin:0px;' autocomplete='off'></td>
	<td style='$stl $clr'><select class='form-control' name='curre$i' style='padding-left:2px; padding-top:2px; margin:0px; height:24px; width:80px;'><option value='1' selected='selected'> &nbsp;&nbsp; Select</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></td>
		   
		   
		   
		   
	<td style='$stl $clr'><div align='right'> 
	<input name='roa$i' class='amounts form-control' type='text' onkeyup='format(this); findTotal();' onkeypress='return isNumberKey(event);' style='text-align:right; width:100%; height:24px; margin:0px 0px 0px 0px; padding:0px 10px 0px 0px; margin:0px;' autocomplete='off'></td>
	<td style='$stl $clr'><select class='form-control' name='curres$i' style='padding-left:2px; padding-top:2px; margin:0px; height:24px; width:80px;'><option value='1' selected='selected'> &nbsp;&nbsp; Select</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></td></tr>";
	            $i++;
			}
		?>
						
                    </tbody><thead>
	<tr><th class='text-right'> </th><th colspan="4">
	Total Amount </th><?php
	
	echo"<th class='text-right'><div class='input-group date' data-provide='datepicker' style='width:80%;'><input class='form-control form-center' name='date' type='text' value='$Date' onkeypress='return isNumberKey(event)' required>
	  <span class='input-group-addon'><i class='lnr lnr-calendar-full'>
	  </i></span></div></th>
	
	<th class='text-right text-right' style='padding-right:0px; padding-left:0px;' colspan='2'><input name='toro' id='total1' class='form-control' type='text' readonly style='text-align:right; width:100%; height:28px; padding:0px 10px 0px 0px; margin:0px; color:black; float:right;' onChange='format(this);'></th>
	<th class='text-right text-right' style='padding-right:0px; padding-left:0px;' colspan='2'>
	 <input name='toco' id='total2' class='form-control' type='text' readonly style='text-align:right; width:100%; height:28px; padding:0px 10px 0px 0px; margin:0px; color:blue; float:right;' onChange='format(this);'> 
	 <input type='hidden' name='n' value='$i'>
	  <input type='hidden' name='p' value='$p'>
	  <input type='hidden' name='dato' value='$dato'>
	  <input type='hidden' name='datos' value='$datos'></th></tr>
	  
	  
	  <tr><th colspan='6'> </th>
	 
	<th class='text-right' colspan='4' style='padding-right:2px;'><button type='submit' class='btn btn-sm btn-block btn-info pull-right' style='height:28px; margin-right:5px; padding:0px 10px 0px 0px; margin:0px;' name='payall'><i class='lnr lnr-printer'></i> &nbsp; PAY&nbsp;SELECTED </button></th>";
		    ?></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{
		if($pto==10){
			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:blue'>All records are saved successfully </div><br><br><br><br><br><br>";
		}
		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';
   ?>