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

$cond='LIMIT 20';
$conde='';
$perpagevalue=20;

if(isset($_POST['delete_id']))
		{
			$rowid=$_POST['rowid'];
			$then=mysqli_query($conn, "UPDATE `deduct` SET `Status`='1' WHERE `Number`='$rowid' LIMIT 1");
			$p=$_POST['p'];
			$im=$_POST['im'];
			$file_to_delete = "deduct/$im";
			unlink($file_to_delete);
		}
if(isset($_POST['perpage']))
		{
			$perpagevalue=$_POST['perpagevalue'];
			$cond="LIMIT $perpagevalue";
			$p=$_POST['p'];
		}
if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$conde="AND `Pdate` BETWEEN '$dato' AND '$datos'";
		$p=1;
		}
		
		
		// ******************* create new record ****************************
if(isset($_POST['submit_duct']))
		{
			$emplo=$_POST['emplo'];
			$amo=$_POST['amo'];
			$amo=preg_replace('/,/', '', $amo);
			$purpo=$_POST['purpo'];
			$user=$_POST['user'];
			$dato=$_POST['dato'];

	$temp1 = explode(".", $_FILES["file1"]["name"]);
$newfilename1 = round(microtime(true)) . '.' . end($temp1);
move_uploaded_file($_FILES["file1"]["tmp_name"], "deduct/" . $newfilename1);
	if(!end($temp1))
	$newfilename1='';

$so=mysqli_query($conn, "INSERT INTO `deduct` (`Employee`, `Amount`, `Purpose`, `File1`, `User`, `Status`, `Pdate`, `Driver`) VALUES ('999999', '$amo', '$purpo', '$newfilename1', '$loge', '2', '$dato', '$emplo')");
		$pto=10;
		}

		if(isset($_POST['edit_id']))
		{
			$rowid=$_POST['rowid'];
			$do=mysqli_query($conn, "SELECT *FROM `deduct` WHERE `Number`='$rowid' LIMIT 1");
				$ro=mysqli_fetch_assoc($do);
					$code=$ro['Number'];
					$amo=number_format($ro['Amount']);
					$emplo=$ro['Employee'];
					$purpo=$ro['Purpose'];
					$user=$ro['User'];
					$dato=$ro['Date'];

					$file1=$ro['File1'];
					if($file1)
				$dfile="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='down_duct.php?link=$file1'>Download File</a>";
					else
				$dfile="";

				$namb="update_duct";
				$valub="Update";
		}
		else{
			$emplo='';
			$amo='';
			$purpo='';
			$user=$loge;
			$dato=$Date;

			$dfile="";
			$rowid=0;
			
			$namb="submit_duct";
				$valub="Submit";
		}

		if(isset($_POST['update_duct']))
		{
			$rowid=$_POST['rowid'];
			$emplo=$_POST['emplo'];
			$amo=$_POST['amo'];
				$amo=preg_replace('/,/', '', $amo);
			$purpo=$_POST['purpo'];
			$dato=$_POST['dato'];
			$user=$loge;

	$temp1 = explode(".", $_FILES["file1"]["name"]);
$newfilename1 = round(microtime(true)) . '.' . end($temp1);
move_uploaded_file($_FILES["file1"]["tmp_name"], "deduct/" . $newfilename1);
	if(!end($temp1)){
	$newfilename1='';
	$upda="";
	}
	else{
	$upda=", `File1`='$newfilename1'";
	}

$so=mysqli_query($conn, "UPDATE `deduct` SET `Employee`='$emplo', `Amount`='$amo', `Purpose`='$purpo', `User`='$loge', `Date`='$dato' $upda WHERE `Number`='$rowid' LIMIT 1");
		$pto=20;

			$amo=number_format($amo);

				$namb="update_duct";
				$valub="Update";
		}		



	if($p)
$do=mysqli_query($conn, "SELECT *FROM `deduct` WHERE `Status`='2' $conde ORDER BY `Date` ASC");
	else
$do=mysqli_query($conn, "SELECT *FROM `deduct` WHERE `Status`='2' ORDER BY `Date` DESC $cond");
$fo=mysqli_num_rows($do);
?>
<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;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" 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 active" 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'>×</button>Deduction Record Has Been Created.
		</div></center>";
if($pto==20)
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'>×</button>Deduction Record Has Been Updated.
		</div></center>";
if($pto==30)
echo"<center><div class='alert alert-danger' style='width:48%;text-align:center;float:center;border-radius:5px;'>
		<i class='lnr lnr-checkmark-circle'></i> &nbsp;&nbsp; <button class='close' data-dismiss='alert' type='button'>×</button>Nothing Has Been Changed.
		</div></center>";
			?>
			
           <div class="col-lg-6 "> 
           <form action="" method="POST" class="form-horizontal ">
        <input name="searchkeyword" value="" type="hidden">
        <input name="column_name" value="" type="hidden">
    
            <div class="col-lg-1"> 
         </div>    
           <div class="col-lg-4">
               <?php
             if($_SESSION['Cnt']){
                 ?>
       <button class="btn  btn-warning btn-block" type="button" data-toggle='modal' data-target='#Modaldis'> Create New Record </button>
       <?php
             }
             ?>
						</div>
                    <div class="col-lg-1">  </div> 
    
        </form>
           
           
              </div>
          
         <div class="modal fade" id="Modaldis" 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">CREATE A DEDUCTION RECORD 
        <span class="pull-right">  </span></h5>

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

        	<div class="form-group">
            <label class="control-label col-md-4 text-right">Employee</label>
            <div class="col-md-5">
           <select class="form-control" name="emplo" required>
		<option value='' selected='selected'>Select Employee</option>
			 <?php
		$doi=mysqli_query($conn, "SELECT `Fullname` FROM `employees` WHERE `Status`='0' AND `Currentp`='26' ORDER BY `Fname` ASC");
			while($roi=mysqli_fetch_assoc($doi)){
				$fna=$roi['Fullname'];
			echo"<option value='$fna'> $fna </option>";
			}
			?>    
                            </select>
            </div><div class="col-md-3"> </div> 
 </div>
  <div class="form-group"><br>
<label class="control-label col-md-4 text-right"><br>Total Amount</label>
            <div class="col-md-5"><br><div class="input-group">
<span class="input-group-addon" style="width:20%; text-align:center; padding-left:0px; padding-right:0px;"> $ </span>	
      <input class="form-control form-center" name="amo" class="form-control text-center" onkeyup="format(this);" onkeypress="return isNumberKey(event)" type="text" required></div>
            </div><div class="col-md-3"></div> 
 </div>
 <div class="form-group">
   <label class="control-label col-md-4 text-right"><br><br>
   Purpose/Issue</label><div class="col-md-5"><br>
    <textarea class="form-control" name="purpo" rows="2" required></textarea>
            </div><div class="col-md-3"> </div> 
			</div>
		

  <div class="form-group">
			  <label class="control-label col-md-4 text-right">
			      <br>Due&nbsp;Date</label>
			<div class="col-md-5 text-center"><br>
	<input name="dato" class="form-control" value="<?php echo $dato ?>" type="text" style="text-align:center; float:center" id="from">
            </div><div class="col-md-3"> </div> 
        </div><div class="row"> </div>

 <div class="form-group">
                     <div class="col-md-12 text-center"><br><br>
              <div class="fileupload fileupload-new" data-provides="fileupload"><input value="<?php echo $rowid ?>" name="rowid" type="hidden">
                <span class="btn btn-default btn-file"><span class="fileupload-new">Select</span><span class="fileupload-exists">Change</span><input name="file1" id="app_file" type="file" readonly='readonly'></span><span class="fileupload-preview"></span><button class="close fileupload-exists" data-dismiss="fileupload" style="float:none" type="button">×</button>
		                    <?php echo $dfile ?>
                     
    </div><small>Zip, txt, jpeg, png, pdf, doc (Max : 2M)</small>
            </div></div></div>
		
	</div><div class="modal-header text-right" style="height:50px; padding-top:15px;"><button type="button" class="btn btn-xs btn-danger" data-dismiss="modal" style="width:100px;"> CANCEL </button>
        <button type="submit" name="submit_duct" class="btn btn-xs btn-success" style="width:100px;"> SAVE </button></div>
      </form>
  </div></div>
</div>
         
           

        <form action="" method="post" class="form-horizontal ">
                  
                       <div class="col-lg-6"> <table border='0'><tr><th>
            <div class="col-md-4 " style='width:150px;'> 
           <div class="input-group date datepicker" style='width:140px;'>
      <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>
</th><th>

		  <div class="col-md-4 " style='width:150px;'> 
           <div class="input-group date datepicker" style='width:140px;'>	
      <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> 
          
		  </th><th>
                      
                       
                       <div class="col-lg-6 input-group">
                      <span class="input-group-btn">
                        <button class="btn  btn-primary" type="submit" name="search"><i class="lnr lnr-magnifier"></i> Search</button>
                      </span>
					  </div>
                       </th></tr></table>  </div> 
                      
                     
                  
            </form> 
             
               
            </div>
             <div class="row">
            <div class="col-lg-12">
             <span>Total Records Found : <b><?php echo" $fo " ?></b></span>
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">
               
                                 <table class="table table-striped">     
                                      <thead>
                    <tr role="row">
                     <th class="hidden-xs"> No </th>
                        <th> Due&nbsp;Date </th>
                       <th> Employee`s&nbsp;Name</th>
                        <th class="hidden-xs"> Purpose/Issue </th> 
		        <th width="8%"> Amount </th><th width="8%"> Paid </th>
		        <th width="8%"> Balance </th><th class='hidden-print' colspan='2' style="width: 10px;text-align:center;">Actions</th>
                     </tr>
                    </thead>
                                        <tbody>
					<?php
					$n=1;           $tot=0;
	while($ro=mysqli_fetch_assoc($do)){
$code=$ro['Number'];
$emplo=$ro['Driver'];
$amo=$ro['Amount'];
	$amoo=number_format($amo);
$purpo=$ro['Purpose'];
$dte=$ro['Pdate'];
$im=$ro['File1'];

$cut=mysqli_query($conn, "SELECT SUM(`Amount`) AS `Pay` FROM `cutter` WHERE `Deduct`='$code' AND `Driver`='$emplo' AND `Status`='0'");
$rut=mysqli_fetch_assoc($cut);
$pay=$rut['Pay'];               $payo=number_format($pay, 2);
$bal=$amo-$pay;                 $balo=number_format($bal, 2);

$stn="style='padding:1px;'";

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'>DELETE CONFIRMATION 
<span class='text-right pull-right'><b> $emplo : $$amoo </b></h5>

      </div>
      <div class='modal-body' style='height:40px;'>
        <h5 style='color:#ff0033'>Are you sure you want to delete this record?</h5>
      </div><form method='post' action=''><input type='hidden' name='rowid' value='$code'>
	  <input value='$p' name='p' type='hidden'><input value='$im' name='im' type='hidden'>
      <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>";
           
		print("<tr><td $stn class='text-center hidden-xs'>$n</td>
            <td $stn class=hidden-xs> $dte </td>
        <td $stn class=hidden-xs> $emplo </td><td $stn> $purpo </td>
		<td class='text-right' style='padding:0px 40px 0px 0px;'> $amoo </td>
		<td class='text-right' style='padding:0px 40px 0px 0px;'> $payo </td>
		<td class='text-right' style='padding:0px 40px 0px 0px;'> $balo </td>

            <td class='hidden-print' align='right' style='width:20px; padding:0px;'><button type='button' class='btn btn-xs btn-warning hidden-print text-center' style='height:20px; padding:0px; margin-top:2px; margin-botton:2px; width:25px; text-align:center; padding-right:5px;' disabled readonly>&nbsp;&nbsp;<i class='lnr lnr-pencil'></i></td>
						   
						   <td class='hidden-print' align='right' style='width:20px; padding:0px;'>
						  <button type='button' class='btn btn-xs btn-danger hidden-print' style='height:20px; padding:0px; margin-top:2px; margin-botton:2px; width:25px;' data-placement='top' data-toggle='modal' data-target='#exampleModal$n' $disa>
						  <i class='lnr lnr-trash'></i></button></td></form></tr>");
						  $n++;         $tot+=$amo;
						}
						$tot=number_format($tot);
				
					print("<tr><th class='hidden-xs'> </th> 
	<th colspan='3' style='align:center; padding-left:80px;'> TOTAL AMOUNT </th>
	<th class='text-right' style='padding:0px 40px 0px 0px;'> $tot </th>
		<th class='text-right' style='padding:0px 40px 0px 0px;'> 0.00 </th>
			<th class='text-right' style='padding:0px 40px 0px 0px;'> $tot </th><th colspan='2' class='text-right hidden-print' $stn>  </th></tr>");
						?>
						
                    </tbody>   
                  </table>
                    <div class="row">
                  <div class="col-lg-12">
                  <div class="pull-right">
                  <ul class="pagination">
				   <li>
                                 <a class="icon" href="#">
           <i class="lnr lnr-chevron-left"></i></a>
                                            </li>
                      <li>
                                            </li>
                      <li class="active">
                        <a href="#">
                        1                        </a>
                      </li>
                            <li>
                                              <a class="icon" href="#">
                        <i class="lnr lnr-chevron-right"></i></a>
                                           </li>
                    </ul>
              </div></div></div>                     
                
              </div>
            </div></div>
                  </div>
      
 <?php
 include'footer.php';
 ?>