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

// search for an item to be added to the chart
if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$p=1;
		}

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

			if($custo)
				$condi="AND `stouse`.`Destin`='$custo'";

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

    <li class="list-group-item">
	  <a href="starepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Stations Report
                </p>
              </a></li>    

    <li class="list-group-item active">
	  <a href="purepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Purchase Report
                </p>
              </a></li>     

    <li class="list-group-item">
	  <a href="addrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Additional Report
                </p>
              </a></li>   
                         
            </ul>
  </div>
                    
           
           
    <form action="" method="post" class="form-horizontal "> 
        <div class="col-lg-10">
                  <div class="row">

				  <div class="col-lg-3"> </div>         

                     <div class="col-lg-3">
			
			<select class="form-control" name="custo">
				<option value='' selected='selected'>Select Station</option>
			 <?php
			$dois=mysqli_query($conn, "SELECT `Destin` FROM `stouse` WHERE `Status`='0' AND `Action`='PURCHASE' GROUP BY `Destin` ORDER BY `Destin` ASC");
			while($rois=mysqli_fetch_assoc($dois)){
				$fue=$rois['Destin'];
				if($fue==$custo)
					$sli="selected='selected'";
				else
					$sli='';
			echo"<option value='$fue' $sli> $fue </option>";
			}
			?>    
                            </select>
			
					   </div>
							
                       <div class="col-lg-6 hidden-print">
            <div class="col-lg-4"> 
           <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-4"> 
           <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-3">
                        <button class="btn  btn-primary btn-block" type="submit" name="search"><i class="lnr lnr-magnifier"></i> Search</button>
                   
					  </div>
                         </div> 
                      
                     
                  
            </form> 
             
               
            </div>
             <?php
		$n=1;			$tot=0;
$do=mysqli_query($conn, "SELECT `stouse`.`Number`, `stouse`.`Date`, `vehicles`.`Plate`, `vehicles`.`Driver`, `stouse`.`Quantity`, `stouse`.`Price`, `stouse`.`Destin`, `stouse`.`Comment`, `stouse`.`Trip` FROM `stouse` INNER JOIN `vehicles` ON `stouse`.`Item`=`vehicles`.`Number` WHERE `stouse`.`Status`='0' AND `stouse`.`Date` BETWEEN '$dato' AND '$datos' AND `stouse`.`Action`='PURCHASE' $condi ORDER BY `stouse`.`Number` ASC");

$fo=mysqli_num_rows($do);
					?>
					<div class="divFooter"><center><u><b>PURCHASE REPORT <?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;&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>
			 
			  <span class="pull-right hidden-print">
			 &nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="exportTableToExcel('tblData')" class="hidden-print" title='Export to excell' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-line-spacing"></i></a></span>
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">

			 <table class="table table-striped table-hover" style="font-size:12px;" id="tblData">     
                    <thead><tr role="row">
			<th width='3%'> No </th>
			<th> Date </th><th> Station </th>
			<th> VEHICLE&nbsp;ID </th>
			<th width='20%'> DRIVERS'&nbsp;NAME </th>
			<th width='20%'> DESTINATION </th>
	        <th width='5%'> QTY&nbsp;(Lts) </th>
	        <th class="text-center"> COMMENT </th></tr></thead>
                                        <tbody>
					<?php
		while($rom=mysqli_fetch_assoc($do)){
		                $nuos=$rom['Number'];
						$item=$rom['Plate'];
						$drive=$rom['Driver'];
						$qty=$rom['Quantity'];
						$com=$rom['Comment'];
						$date=$rom['Date'];
						$dest=$rom['Destin'];
			$doix=mysqli_query($conn, "SELECT `Number`,`Location`, `Final` FROM `trips` WHERE `Date`<='$date' AND `Plate`='$item' ORDER BY `Number` DESC LIMIT 1");
			$roix=mysqli_fetch_assoc($doix);
				$deso=$roix['Location'];
				$fina=$roix['Final'];
				$tipo=$roix['Number'];
				
				if(!$rom['Trip'])
$doi=mysqli_query($conn, "UPDATE `stouse` SET `Trip`='$tipo' WHERE `Number`='$nuos' ORDER BY `Number` ASC LIMIT 1");
				
					$to+=$qty;
			$too=number_format($to);				$qto=number_format($qty);
echo"<tr><td style='padding:0px; font-size:12px; text-align:right'>&nbsp;$n&nbsp;&nbsp;</td><td style='padding:0px; font-size:12px; text-align:center;'> $date </td><td style='padding:0px; font-size:12px; text-align:left;'> $dest </td><td style='padding:0px; font-size:12px; text-align:center;'> $item </td><td style='padding:0px; font-size:12px; text-align:left;'> $drive </td><td style='padding:0px; font-size:12px; text-align:left;'> $deso-$fina </td><td style='padding:0px; font-size:12px; text-align:right;'> $qto</td><td style='padding:0px 0px 0px 5px; font-size:12px; text-align:left;'> $com </td></tr>";
		$n++;				
					}
		$toto=number_format($tot);
?>			
<tr><th style='font-size:12px; text-align:left; padding-left:35px;' colspan='6'><b> TOTAL QUANTITY (LTS) </th><th style='font-size:12px; text-align:left; padding-left:35px; padding-right:0px;'><label style="font-size:12px; text-align:right; float:right; padding-right:0px;"><b><?php echo $too ?></th><th> </th></tr>
	</table><br><br>
                                      
                
              </div>
            </div></div>
                  </div>                  
                <span class="pull-right hidden-print">
			 &nbsp;&nbsp;&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> 
			 
			    <span class="pull-right hidden-print">
			 &nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="exportTableToExcel('tblData')" class="hidden-print" title='Export to excell' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-line-spacing"></i></a></span>
      
   </div></div></div>  
   <?php
   include'footer.php';
   ?>