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

$year=date("Y");
$month=date("F");

// search for an item to be added to the chart
if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
		}
		
 if(isset($_POST['reque']))
		    {
			$code=$_POST['code'];
			$custo=$_POST['custo'];
			$rowid=$_POST['rowid'];
			$custor=$custo;
			$p=1;
			$reso=str_replace("'", "`", $_POST['reso']);
$doxe=mysqli_query($conn, "UPDATE `income` SET `Reason`='$reso', `Rby`='$loge' WHERE `Number`='$rowid' ORDER BY `Number` ASC LIMIT 1");
		    }
		
	    if(isset($_POST['requet']))
		    {
			$code=$_POST['code'];
			$custo=$_POST['custo'];
			$rowid=$_POST['rowid'];
			$custor=$custo;
			$p=1;
			$resot=str_replace("'", "`", $_POST['resot']);
$doxe=mysqli_query($conn, "UPDATE `repair` SET `Reason`='$resot', `Rby`='$loge' WHERE `Number`='$rowid' ORDER BY `Number` ASC LIMIT 1");
		    }
		    
		    	if(isset($_POST['delete_id']))
		        {
			$code=$_POST['code'];
			$rowid=$_POST['rowid'];
			$then=mysqli_query($conn, "DELETE FROM `repair` WHERE `Number`='$rowid' LIMIT 1");

	$doi=mysqli_query($conn, "SELECT *FROM `stouse` WHERE `Repair`='$rowid'");
			while($roi=mysqli_fetch_assoc($doi)){
				$numu=$roi['Number'];
				$qts=$roi['Quantity'];
				$its=$roi['Item'];
				$stor=$roi['Store'];
	$then=mysqli_query($conn, "DELETE FROM `stouse` WHERE `Number`='$numu' LIMIT 1");
	
		if($stor)
	$doin=mysqli_query($conn, "UPDATE `items` SET `Quantity`=`Quantity`+'$qts' WHERE `Number`='$its' ORDER BY `Number` DESC LIMIT 1");
			}

		}
		
		if(isset($_POST['deloge']))
		    {
			$code=$_POST['code'];
			$rowid=$_POST['rowid'];

			$numu=$_POST['numu'];
			$qts=$_POST['qts'];
			$ites=$_POST['ites'];
			$stor=$_POST['stor'];
	$then=mysqli_query($conn, "DELETE FROM `stouse` WHERE `Number`='$numu' LIMIT 1");
	
		if($stor)
	$doin=mysqli_query($conn, "UPDATE `items` SET `Quantity`=`Quantity`+'$qts' WHERE `Number`='$ites' ORDER BY `Number` DESC LIMIT 1");
		} 
		
		if(isset($_POST['delete_ids']))
		        {
			$code=$_POST['code'];
			$rowid=$_POST['rowid'];
	$then=mysqli_query($conn, "DELETE FROM `income` WHERE `Number`='$rowid' LIMIT 1");
			}
			
			$mpri="ON $dato";
			
			
	$do=mysqli_query($conn, "SELECT *FROM `repair` WHERE `Status`='0' AND `Vehicle`!='' AND `Reason`!='' ORDER BY `Date` ASC LIMIT 100");

$dol=mysqli_query($conn, "SELECT *FROM `income` WHERE `Status`='0' AND `Vehicle`!='' AND `Reason`!='' ORDER BY `Date` ASC LIMIT 100");
    $fo=mysqli_num_rows($do) + $fol=mysqli_num_rows($dol);
?>

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

    <li class="list-group-item active">
	  <a href="approve.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Logistics Approval
                </p>
              </a></li> 
                       
            </ul><br><br><br>
  </div>
       
        <form action="" method="post" class="form-horizontal "> 
        <div class="col-lg-10">
                  <div class="row hidden-print">

				  <div class="col-lg-7 text-right"> </div>        

 	 <div class="col-lg-2 hidden-print"> 
           <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-2">
        <button class="btn  btn-primary btn-block" type="submit" name="search"><i class="lnr lnr-magnifier"></i> Search</button>
                   
					 </div>
            </form> 

			</div>


<div class="divFooter"><center><u><b>APPROVALS REPORT <?php echo"$mpri"; ?></b></u></center></div>
             <div class="row"> </div><div class="row">
            <div class="col-lg-12" style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; Total Records Found : <b><?php echo" $fo " ?></b></span>
		<span class="pull-right">&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">
               
    <?php
            if($fo){
    echo"<table class='table table-striped table-hover'>     
                    <thead><tr role='row'>
                     <th class='text-center'> No </th>
                        <th class='text-center' width='8%'> Date </th>
                        <th class='text-center'> Destination </th> 
                        <th class='text-center'> Location </th> 
                       <th class='text-center'> Vehicle </th>
						 <th class='text-center'> Purpose </th> 
                        <th class='text-center'> Customer </th> 
                        <th class='text-center'> Quantity </th>  
                        <th class='text-center'> Price </th>
						 <th class='text-center'> Amount </th>
		<th class='hidden-print text-center' width='1%'> # </th></tr></thead>
                                        <tbody>";
					
	$n=$t=1;					$tam=$tdi=$tq=$tpa=$sin=$sout=0;
					
					
					
					
					// *************** Transport Income *******************
		while($rol=mysqli_fetch_assoc($dol)){
$cods=$rol['Number'];
$emplo=$rol['Vehicle'];
$rate=$rol['Rate'];
$tm=number_format($rol['Amount']);
$amo=$rol['Amount']*$rate;
$amoo=number_format($amo, 2);
if($rate!='1')
$desin="($tm x $rate)";
else
$desin="";
$desi="Transport Income $desin";
$dte=$rol['Date'];
$pri=$rol['Amount']*$rate;
$gar=$rol['District'];
$age=$rol['Location'];
$reaso=$rol['Reason'];
$rby=$rol['Rby'];
$trip=$rol['Trip'];
$fna=$rol['Plate'];
$garage="$gar $age";
$qt=1;
$qty=number_format($qt, 2);
$caso=$rol['Customer'];
if(!$driv)
    $driv=$driver;

	$prio=number_format($pri, 2);
				
$setri=mysqli_query($conn, "SELECT `Customer` FROM `account` WHERE `Number`='$caso'");
        $file=mysqli_fetch_assoc($setri);
            $driv=$file['Customer'];
            
    $fin=mysqli_query($conn, "SELECT `Final` FROM `trips` WHERE `Number`='$trip'");
        $fine=mysqli_fetch_assoc($fin);
            $final=$fine['Final'];
				
			$tam+=$amo;	                    $sin+=$amo;				
				
$stn="style='padding:1px; color:blue;'";

$dok=mysqli_query($conn, "SELECT `income`.`User`, `income`.`Date`, `income`.`Location`, `income`.`District`, `account`.`Customer`, `income`.`Amount`, `income`.`Rate` FROM `income` INNER JOIN `account` ON `income`.`Customer`=`account`.`Number` WHERE `income`.`Status`='0' AND `income`.`Vehicle`!='' AND `income`.`Reason`='' AND `income`.`Trip`='$trip' ORDER BY `income`.`Date` ASC LIMIT 100");
if($fok=mysqli_num_rows($dok)){
    $rok=mysqli_fetch_assoc($dok);
        $gars=$rok['District'];
        $ages=$rok['Location'];
        $garages="$gars $ages";
        $dats=$rok['Date'];
        $cuss=$rok['Customer'];
        $ams=number_format($rok['Amount']);
        $rats=number_format($rok['Rate']);
        $user=$rok['User'];
        $tite="CHANGED BY:
            Customer: $cuss
            Destination: $garages
            Location: $final
            Amount: $ams x $rats
            Date: $dats
                                          $user";
}
else
    $tite="This record was not changed";
            
           
	print("<tr title='$tite' data-toggle='tooltip' data-placement='top'><td $stn class='text-center'>$n</td><td class='text-center' $stn> $dte </td>
	<td $stn> $garage </td><td $stn> $final </td><td $stn>&nbsp;$fna&nbsp;</td>
	<td $stn> $desi </td><td $stn> $driv </td><td style='text-align:center; padding:1px; color:blue;'> $qty </td><td class='text-right' $stn> $prio </td><td class='text-right' $stn> $amoo&nbsp;&nbsp;</td>");

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'>APPROVAL CONFIRMATION  
		<label style='float:right;'> $fna &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		$garag &nbsp;&nbsp; $prio </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</h5>

      </div>
      <div class='modal-body' style='height:40px;'>
        <h5 style='color:#ff0033'>Are you sure you want to approve this record?</h5>
      </div><form method='post' action=''>
      <input type='hidden' name='rowid' value='$cods'>
      <input type='hidden' name='code' value='$code'>
      <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>
        <button type='submit' name='delete_ids' class='btn btn-sm btn-danger' $disa>YES</button>
      </div></form>
    </div>
  </div>
</div>";




echo"<div class='modal fade' id='requestModal$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'>CREATE A REQUEST  
		<label style='float:right;'> $fna &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		$garag &nbsp;&nbsp; $prio </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</h5>

      </div><form method='post' action=''>
      <div class='modal-body' style='height:150px;'>
        <h5 style='color:#ff0033'>Write the reason of your request!</h5>
        <textarea class='form-control' name='reso' rows='3'>$reaso</textarea>
      </div>
      <input type='hidden' name='rowid' value='$cods'>
      <input type='hidden' name='code' value='$code'>
      <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' style='width:80px;'>CANCEL</button>
        <button type='submit' name='reque' class='btn btn-sm btn-warning' style='width:80px;'>SEND</button>
      </div></form>
    </div>
  </div>
</div>";
		
if($_SESSION['Cancel']){
$taga="#exampleModal$n";
$disa="";
$btn="success";
$lnr="checkmark-circle";
}
else{
    $taga="#requestModal$n";
    $disa="";
    $btn="danger";
    $lnr="trash";
    }				
						
						
echo"<td align='right' class='hidden-print' style='width:20px; padding:0px;'>
	<button type='button' class='btn btn-xs btn-$btn hidden-print' style='height:20px; padding:0px; margin:0px; width:25px;' data-placement='top' data-toggle='modal' data-target='$taga' $disa>
	        <i class='lnr lnr-$lnr'></i></button></td></tr>
	<tr title='$tite' data-toggle='tooltip' data-placement='top' style='height:40px'><td> </td><td colspan='9' style='vertical-align:top; color:red; font-size:16px; padding-top:1px;'> $reaso &nbsp;&nbsp;&nbsp; <font size='2' color='teal'> [$rby] </font></td><td align='right' class='hidden-print'> </td></tr>";
			$n++;			  
		}			  
						  
						  
						  
				
		// ***************** Repair & Services ************************
						  
						            $k=1;                $otp=0;
						  	while($ro=mysqli_fetch_assoc($do)){
		$sub=0;
$cods=$ro['Number'];
$emplo=$ro['Vehicle'];
$amo=$ro['Amount'];
$amoo=number_format($amo, 2);
$desi=$ro['Issue'];
$dte=$ro['Date'];
$pri=$ro['Amount'];
$garag=$ro['Garage'];
$driv=$ro['Driver'];
$pur=$ro['Purchase'];
$resot=$ro['Reason'];
$rbs=$ro['Rby'];
$fna=$ro['Plate'];
$trip=$ro['Trip'];
$setri=mysqli_query($conn, "SELECT `Destination`, `Location`, `Final` FROM `trips` WHERE `Number`='$trip'");
$retri=mysqli_fetch_assoc($setri);
$dest=$retri['Destination'];
$locs=$retri['Location'];
$fina=$retri['Final'];
$tam-=$pri;	
$sub+=$pri;
$otp+=$pri;

	$prio=number_format($pri, 2);
    $stn="style='padding:1px;'";
  if($pri){         
			print("<tr><td $stn class='text-center'>$n</td><td class='text-center' $stn> $dte </td><td $stn> [$dest]$locs </td>
			<td $stn> $fina </td><td $stn>&nbsp;$fna&nbsp;</td>
			<td $stn> $garag </td><td $stn> $driv </td>
						<td class='text-right' $stn> 1.00 </td>
						<td class='text-right' $stn> $prio </td>
						<td class='text-right' $stn> $prio&nbsp;&nbsp;</td>");

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'>APPROVAL CONFIRMATION  
		<label style='float:right;'> $fna &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		$garag &nbsp;&nbsp; $prio </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</h5>

      </div>
      <div class='modal-body' style='height:40px;'>
        <h5 style='color:#ff0033'>Are you sure you want to approve this record?</h5>
      </div><form method='post' action=''>
      <input type='hidden' name='rowid' value='$cods'>
      <input type='hidden' name='code' value='$code'>
      <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>
        <button type='submit' name='delete_id' class='btn btn-sm btn-danger' $disa>YES</button>
      </div></form>
    </div>
  </div>
</div>";


echo"<div class='modal fade' id='requestModal$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'>DELETE REQUEST  
		<label style='float:right;'> $fna &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		$garag &nbsp;&nbsp; $prio </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</h5>

      </div><form method='post' action=''>
      <div class='modal-body' style='height:150px;'>
        <h5 style='color:#ff0033'>Write the reason of your request!</h5>
    <textarea class='form-control' name='resot' rows='3'>$resot</textarea>
      </div>
      <input type='hidden' name='rowid' value='$cods'>
      <input type='hidden' name='code' value='$code'>
      <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' style='width:80px;'>CANCEL</button>
        <button type='submit' name='requet' class='btn btn-sm btn-warning' style='width:80px;'>SEND</button>
      </div></form>
    </div>
  </div>
</div>";
		
if($_SESSION['Cancel']){
$taga="#exampleModal$n";
$disa="";
$btn="success";
$lnr="checkmark-circle";
}
else{
    $taga="#requestModal$n";
    $disa="";
$btn="danger";
$lnr="trash";
}										
						
						
								echo"<td align='right' class='hidden-print' style='width:20px; padding:0px;'>
						  <button type='button' class='btn btn-xs btn-success hidden-print' style='height:20px; padding:0px; margin:0px; width:25px;' data-placement='top' data-toggle='modal' data-target='$taga' $disa>
						  <i class='lnr lnr-checkmark-circle'></i></button></td></tr>
	<tr style='height:40px'><td> </td><td colspan='9' style='vertical-align:top; color:red; font-size:16px; padding-top:1px;'> $resot &nbsp;&nbsp;&nbsp; <font size='2' color='teal'> [$rbs] </font></td><td align='right' class='hidden-print'> </td></tr>";

  }
	$dob=mysqli_query($conn, "SELECT `items`.`Item`, `stouse`.`Number`, `stouse`.`Date`, `stouse`.`Price`, `stouse`.`Quantity`, `stouse`.`Status`, `stouse`.`Store`, `stouse`.`Item` AS 'Ites' FROM `stouse` INNER JOIN `items` ON `stouse`.`Item` = `items`.`Number` WHERE `stouse`.`Status`!='1' AND `stouse`.`Repair`='$cods' ORDER BY `stouse`.`Number` ASC");
		if($fob=mysqli_num_rows($dob)){
		while($rob=mysqli_fetch_assoc($dob)){
			$stl="padding:0px; color:#6600ff;";
			$numu=$rob['Number'];
			$dati=$rob['Date'];
			$pri=$rob['Price'];
			$qty=$rob['Quantity'];
			$name=$rob['Item'];
			$status=$rob['Status'];
			$stor=$rob['Store'];
			$ites=$rob['Ites'];
			$all=$pri*$qty;

	$tam-=$all;						$sub+=$all;                 $otp+=$all;
			
			if($stor=='0')
				$garag="";
			else
				$garag="[STORE]";

			$prio=number_format($pri, 2);				
			$qto=number_format($qty, 2);			
			$allo=number_format($all, 2);

print("<tr><td style='padding:0px;'></td><td colspan='2' style='$stl $clr'>");
			
			
	echo"<div class='modal fade' id='iModal$t' 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'>APPROVAL CONFIRMATION  
		<label style='float:right;'> $name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
		$allo </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</h5>

      </div>
      <div class='modal-body' style='height:40px;'>
        <h5 style='color:#ff0033'>Are you sure you want to approve this record?</h5>
      </div><form method='post' action=''>
      <input type='hidden' name='rowid' value='$cods'>
	  <input type='hidden' name='code' value='$code'>
	  <input type='hidden' name='numu' value='$numu'>
	  <input type='hidden' name='stor' value='$stor'>
	  <input type='hidden' name='qts' value='$qty'>
	  <input type='hidden' name='ites' value='$ites'>
      <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>
        <button type='submit' name='deloge' class='btn btn-sm btn-danger' $disa>YES</button>
      </div></form>
    </div>
  </div>
</div>";

			
			print("</td><td style='$stl $clr'> </td>
			<td colspan='2' style='$stl $clr'>$name &nbsp;&nbsp; <font size='1'>$garag</font></td>
			<td class='text-right' style='$stl $clr'>$qto</td><td class='text-right' style='$stl $clr'>$prio</td>
			<td class='text-right' style='$stl $clr'>$allo&nbsp;&nbsp;</td>
			
	<td align='right' class='hidden-print' style='width:20px; padding:0px;'>
		<button type='button' class='btn btn-xs btn-$btn hidden-print' style='height:20px; padding:0px; margin:0px; width:25px;' data-placement='top' data-toggle='modal' data-target='#iModal$t' $disa>
				<i class='lnr lnr-$lnr'></i></button></td></tr>");									
				$t++;               		$k++;		
						}
						
		}
						$n++;                   $k++;							
						}					

		
                    echo"</tbody></table>";
}
else{
    echo"<br><br><br><br><br><br><font color='red' size='6'><center> There is no record to be approved </font><br><br><br><br><br><br><br><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';
   ?>
