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

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



// delete the whole requisition
if(isset($_POST['delos']))
		{
			$rowid=$_POST['rowid'];
	$then=mysqli_query($conn, "DELETE FROM `stouse` WHERE `Voucher`='$rowid' AND `Action`='REQUISE' ORDER BY `Number` DESC LIMIT 100");
		}
		
		
		// delete the whole requisition
if(isset($_POST['edits']))
		{
			$vous=$_POST['rowid'];
			$m=1;
		}


		            // create new requisition
			        if(isset($_POST['save_req']))
							{	
                                $ct=0;						
							$nx=$_POST['nx'];
							$vous=$_POST['vous'];
							$dato=$_POST['dato'];
							$coome=str_replace("'", "`", $_POST['coome']);
							$own=str_replace("'", "`", $_POST['own']);
							$phone=$_POST['phone'];
							$taker=$_POST['taker'];
		
					if(!$vous){
	  $rece=mysqli_query($conn, "SELECT `Voucher` FROM `stouse` WHERE `Voucher`!='0' AND `Action`='REQUISE' ORDER BY `Voucher` DESC LIMIT 1");
				$re=mysqli_fetch_assoc($rece);
					$vou=$re['Voucher']+1;
							}
					else{
					    $vou=$vous;
	$so=mysqli_query($conn, "DELETE FROM `stouse` WHERE `Status`='0' AND `Voucher`='$vou' AND `Action`='REQUISE'");
					}

			while($nx>0){
				$qty=preg_replace('/,/', '', $_POST["qty$nx"]);
				$pri=preg_replace('/,/', '', $_POST["pri$nx"]);
				$ite=str_replace("'", "`", $_POST["ite$nx"]);
							
				if($qty>0){
					$ct++;
	$so=mysqli_query($conn, "INSERT INTO `stouse` (`Date`, `Time`, `User`, `Item`, `Cost`, `Quantity`, `Price`, `Action`, `Destin`, `Voucher`, `Invoice`, `Status`, `Upda`, `Person`, `Comment`, `Otype`) VALUES ('$dato', '$Time', '$loge', '999999', '$pri', '$qty', '$pri', 'REQUISE', '$taker', '$vou', '$ite', '0', '0', '$own', '$coome', '$phone')");
								}
					$nx--;
						}

					}
	
	
		
    // Delete the current chart
	if(isset($_POST['delox']))
		{
	$so=mysql_query($conn, "DELETE FROM `stouse` WHERE `Status`='0' AND `Voucher`='0' AND `Action`='REQUISE' AND `User`='$loge'");
		}
		
		
		$_SESSION['Lin']="crequit.php";
		$_SESSION['Lim']="Requisition Forms";
?>
<div class="container-fluid main-content">
        <div class="page-title hidden-print">
          <h2 style='margin-top:-20px; margin-bottom: 5px; color:#ffcc33'>
         Construction
          </h2>
                 </div>
     
        <div class="row">
  <div class="col-lg-2 hidden-print">
   
   <ul class="list-group">

    <li class="list-group-item">
	  <a href="cmainsto.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Store Status
                </p>
              </a></li> 

	   <li class="list-group-item">
              <a href="ccrete.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Create New Items
                </p>
              </a></li> 	
                  
			  <li class="list-group-item">
              <a href="creceive.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Receive Items
                </p>
              </a></li>
			  
			   <li class="list-group-item">
              <a href="ctaken.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Store Consumption
                </p>
              </a></li>	    
			  
			   <li class="list-group-item active">
              <a href="crequit.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Requisition Forms
                </p>
              </a></li>	     
<?php
if($_SESSION['Spay']){
    ?>
			  <li class="list-group-item">
              <a href="cbillpay.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Supplier Payment
                </p>
              </a></li> 
              <?php
}
?>
                       
         </ul><br><br>
  
				 <ul class="list-group">

   <li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="ccreatesu.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Create New Supplier
                </p>
              </a></li>
			  
			  </ul>
  </div>
              <?php
        // create a new requisition
        if(isset($_POST['new_req']))
		    $m=1;
	        ?>
       
        <div class="col-lg-10" style="margin-top:-20px;">
                  <div class="row hidden-print">
         <form action="" method="post" class="form-horizontal"> 
            <div class="col-md-1"> </div><div class="col-md-2"> 
            <?php
            if(!$m){
            ?>
      <button class="btn btn-info btn-block" type="submit" name="new_req"><i class="lnr lnr-layers"></i> &nbsp; CREATE NEW </button>
        <?php
        }
        else{
            if($vous){
if($_SESSION['Cancel'] OR $_SESSION['Eccr']){
$tags="#exampleModals";
$disa="";
}
else{
    $tags="#";
    $disa="disabled";
}
     echo"<div class='modal fade' id='exampleModals' 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 
    <label class='pull-right text-right'>$vous </label></h5>

      </div>
      <div class='modal-body' style='height:40px;'>
        <h5 style='color:#ff0033'>Are you sure you want to delete this requisition?</h5>
      </div><form method='post' action=''><input type='hidden' name='rowid' value='$vous'>
      <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='delos' class='btn btn-sm btn-danger'>YES</button>
      </div></form>
    </div>
  </div>
</div>";
    echo"<button type='button' class='btn btn-block btn-danger hidden-print' name='delos' title='Delete this requisition' data-placement='top' style='font-size:12px;' data-toggle='modal' data-target='$tags' $disa><i class='lnr lnr-trash'></i> &nbsp; DELETE REQUISITION </button>";
        }
        }
        ?>
        </div>
        
            <div class="col-md-2"> </div><div class="col-lg-2"> 
           <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)" style="padding-left:2px; padding-right:2px;" required><span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
			</div></div>


		  <div class="col-lg-2"> 
           <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)" style="padding-left:2px; padding-right:2px;" 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>
        
        
        
        <div class="col-md-1"> </div></form>
        
            </div>
               <?php
        // create a new requisition
    if($m)
		{
	?>	    
	<hr><div class="row">
            <div class="col-lg-12">
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix"><div class="col-lg-12">
    <div class="col-lg-12"><br>
         <form action="" method="post" class="form-horizontal">
        <table class="table table-hover" style="border:1px solid #f9f9f9; border:0px; margin-top:-40px;"><thead>	   
		 <?php  
	echo"<tr><th width='5%' style='border:0px;'> &nbsp; </th>
		<th style='border:0px; padding-left:60px;' colspan='3'> ITEM NAME </th>
		<th width='15%' class='text-center' style='border:0px;'>  QUANTITY </th>
		<th width='15%' class='text-center' style='border:0px;'> PRICE </th>
		    </tr></thead><tbody>";

        $nx=1;                   $coome=$own=$phon=''; 
		$stl="style='border:0px; padding:2px 5px 2px 5px;'";	  
    $do=mysqli_query($conn, "SELECT *FROM `stouse` WHERE `Voucher`='$vous' AND `Action`='REQUISE' ORDER BY `Number` DESC");
            $fo=mysqli_num_rows($do);
		while($ro=mysqli_fetch_assoc($do)){
	$qto=number_format($ro['Quantity'], 2);
	$itom=$ro['Invoice'];
	$pct=number_format($ro['Price'], 2);
	$Date=$ro['Date'];
	$coome=$ro['Comment'];
	$taker=$ro['Destin'];
	$phone=$ro['Otype'];
	$own=$ro['Person'];
	$l=5;

print("<tr title='$itom'><td $stl><div align='center'> $nx </td>
<td colspan='3' $stl><div align='left'><input type='text' class='form-control' name='ite$nx' style='border:1px solid teal; height:26px;' value='$itom' list='ites' autocomplete='off'><datalist style='border:1px solid blue;' id='ites'>");
		
$doi=mysqli_query($conn, "SELECT `Iname` FROM `citems` UNION DISTINCT SELECT `Invoice` AS `Iname` FROM `stouse` WHERE (`Action`='REQUISE' AND `Date`>='$dase') GROUP BY `Iname` ORDER BY `Iname` ASC");
		while($roi=mysqli_fetch_assoc($doi)){
			$iname=$roi['Iname'];
	echo"<option value='$iname'></option>";
		}
		
					echo("</datalist></div></td>
<td $stl><INPUT TYPE=hidden NAME='nx' VALUE='$nx'><input type='text' class='form-control text-right' name='qty$nx' style='text-align:right; background-color:Transparent; height:26px;' onkeypress='return isNumberKey(event)' onkeyup='format(this);' value='$qto'></td>
<td $stl><div align='right'><input type='text' class='form-control text-right' name='pri$nx' style='background-color:Transparent; height:26px;' onkeypress='return isNumberKey(event)' onkeyup='format(this);' value='$pct'></td></tr>");	
					$nx++;

		}
			   
			   if(($nx+$l)<11)
			        $l=11-$nx;
		
    	for($k=1; $k<=$l; $k++){		
	print("<tr title='$itom'><td $stl><div align='center'> $nx </td>
	<td colspan='3' $stl><input type='text' name='ite$nx' class='form-control' style='background-color:Transparent; height:26px;' list='ites' maxlenght='60' autocomplete='off'><datalist style='border:1px solid blue;' id='ites'>");
		
$doi=mysqli_query($conn, "SELECT `Iname` FROM `citems` UNION DISTINCT SELECT `Invoice` AS `Iname` FROM `stouse` WHERE (`Action`='REQUISE' AND `Date`>='$dase') GROUP BY `Iname` ORDER BY `Iname` ASC");
		while($roi=mysqli_fetch_assoc($doi)){
			$iname=$roi['Iname'];
	echo"<option value='$iname'></option>";
		}
		
					echo("</datalist></div></td>
	
    <td $stl><INPUT TYPE=hidden NAME='nx' VALUE='$nx'><input type='text' name='qty$nx' class='form-control text-right' style='background-color:Transparent; height:26px;' onkeypress='return isNumberKey(event)' onkeyup='format(this);'></td>
    
    <td $stl><div align='right'><input type='text' class='form-control text-right' name='pri$nx' style='background-color:Transparent; height:26px;' onkeypress='return isNumberKey(event)' onkeyup='format(this);'></td></tr>");	
					$nx++;
		}
	
	if($fo)
	$bt="UPDATE";
	else
	$bt="SAVE";
	print("</tbody><thead><tr style='height:20px;'>
	<td colspan='6' style='border:0px;'> <hr> </td></tr>
	
	
	<tr>
	<td align='right' style='border:0px;' colspan='2' width='10%'><b> Contact </b></td>
	
	<td style='border:0px;' colspan='2'><input type='text' name='own' class='form-control' style='background-color:Transparent; height:36px;' value='$own' placeholder='Requested By (Fullname Here)' required></td>
	
	<td colspan='2' style='border:0px;'><input type='text' name='phone' class='form-control' style='text-align:center; background-color:Transparent; height:36px;' value='$phone' placeholder='Telephone'></td></tr>
	
	<tr><td align='right' style='border:0px;' colspan='2' width='10%'><b> Purpose </b></td>
	<td colspan='4' style='border:0px;' colspan='2'><input type='text' class='form-control' name='coome' style='background-color:Transparent; height:36px;' value='$coome' title='Write Comment here' placeholder='Write Comment Here'></td></tr>


    <tr><td colspan='2' style='border:0px;'> </td><td style='border:0px;'>
    <select name='taker' class='form-control' style='width:80%;' required>
        <option value=''> SELECT A SUPPLIER </option>");
 
    $top=mysqli_query($conn, "SELECT `Supplier` FROM `suppliers` WHERE `Status`='3' AND `Supplier`!='' GROUP BY `Supplier` ORDER BY `Supplier` ASC LIMIT 2000");
				while($rop=mysqli_fetch_assoc($top)){
							$sup=$rop['Supplier'];
				if($taker==$sup)
					$t='selected';
				else
					$t='';
			echo"<option value='$sup' $t> $sup </option>";
						}
					
    print("</select></td>
    <td style='border:0px;'><div align='right'><b>
    	Due Date &nbsp;&nbsp; </td><td style='border:0px;' colspan='2'><center> 
        <input type='text' name='dato' class='form-control date' data-provide='datepicker' style='text-align:center; background-color:Transparent; height:36px;' value='$Date' title='Due date' onkeypress='return isNumberKey(event)'></td></tr></thead></table>");   
		   
		 
		 
   echo"<div class='form-group'>
  <div class='col-md-1'></div>
  <div class='col-md-10'><INPUT TYPE=hidden NAME='vous' VALUE='$vous'><br><br>  
    <button class='btn btn-lg btn-block btn-primary' type='submit' name='save_req'><i class='lnr lnr-chevron-up-circle'></i> $bt </button>
    <br><br><br></div><div class='col-md-1'></div></div><br><br><br></form>";  
		    
    		    
		}
		else{
		    if($p)
		$dor=mysqli_query($conn, "SELECT `stouse`.*, SUM(`Quantity`*`Price`) AS 'Tot', COUNT(DISTINCT(`Invoice`)) AS 'Ite', COUNT(DISTINCT(`Item`)) AS 'Ites' FROM `stouse` WHERE `Date` BETWEEN '$dato' AND '$datos' AND `Status`='0' AND `Voucher`!='0' AND `Action`='REQUISE' GROUP BY `Voucher` ORDER BY `Number` DESC LIMIT 1500");
		    else
		$dor=mysqli_query($conn, "SELECT `stouse`.*, SUM(`Quantity`*`Price`) AS 'Tot', COUNT(DISTINCT(`Invoice`)) AS 'Ite', COUNT(DISTINCT(`Item`)) AS 'Ites' FROM `stouse` WHERE `Status`='0' AND `Voucher`!='0' AND `Action`='REQUISE' GROUP BY `Voucher` ORDER BY `Number` DESC LIMIT 15");
				if($for=mysqli_num_rows($dor)){
?>
 <div class="row">
            <div class="col-lg-12" style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; Total Records Found : <b><?php echo" $for " ?></b></span> <span class="pull-right hidden-print"><?php echo $bra ?>&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">

<table class="table table-striped table-hover">     
                                      <thead>
                    <tr role="row">
                     <th class="hidden-xs"> No </th>
                       <th class='text-center'> Due&nbsp;Date </th>
                        <th> System&nbsp;User </th>
                       <th> Supplier </th><th> Items </th>
						<th class='text-right'>Total&nbsp;Amount&nbsp;&nbsp;&nbsp;&nbsp;</th>
						<th>Paid&nbsp;Amount</th>
						<th>Total&nbsp;Balance</th>
                        <th class="hidden-xs hidden-print" style="width:40px; text-align:center;" colspan='3'> Options </th>
                     </tr>
                    </thead>
                                        <tbody>
										<?php
	$n=1;			$tol=0;
					while($ror=mysqli_fetch_assoc($dor)){
						$code=$ror['Voucher'];
						$date=$ror['Date'];
						$user=$ror['User'];
						$tot=$ror['Tot'];
						if($ror['Ite']>$ror['Ites'])
						    $ite=$ror['Ite'];
						else
						    $ite=$ror['Ites'];
						$des=$ror['Destin'];
						
						$toto=number_format($tot, 2);			$stl="style='padding:1px;'";
		  
		print("<tr><td $stl class=hidden-xs><div align='center'>$code&nbsp;&nbsp;</td>
						<td class='text-center' $stl> $date </td><td $stl> $user </td><td $stl> $des </td><td class='text-center' $stl> $ite </td>
						<td $stl><div align='right'> $toto&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
						<td $stl><div align='right'> 0.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
						<td $stl><div align='right'> $toto&nbsp;&nbsp;&nbsp;&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'>DELETE CONFIRMATION 
        <label class='pull-right text-right'> $toto </h5>

      </div>
      <div class='modal-body' style='height:40px;'>
        <h5 style='color:#ff0033'>Are you sure you want to delete this requisition?</h5>
      </div><form method='post' action=''><input type='hidden' name='rowid' 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='$dbutn' name='delos' class='btn btn-sm btn-danger'>YES</button>
      </div></form>
    </div>
  </div>
</div>";
			
if($_SESSION['Cancel'] OR $_SESSION['Eccr']){
$tags="#exampleModal$n";
$disa="";
}
else{
    $tags="#";
    $disa="disabled";
}
						
						print("<td class='hidden-xs hidden-print' align='right' style='width:10px; padding:0px;'><button type='button' class='btn btn-xs btn-danger hidden-print' style='height:24px; padding:0px; margin:2px;' data-toggle='modal' data-target='$tags' $disa>
						  &nbsp;&nbsp;<i class='lnr lnr-trash'></i>&nbsp;&nbsp;</button></td>
						
								<form action='' method='post'>
			<td class='hidden-xs hidden-print' align='right' style='width:10px; padding:0px;'><input type='hidden' name='rowid' value='$code'>
			<button type='submit' class='btn btn-xs btn-warning hidden-print' name='edits' style='height:24px; padding:0px; margin:2px;' title='Edit' data-toggle='tooltip' data-placement='top'>
						  &nbsp;&nbsp;<i class='lnr lnr-pencil'></i>&nbsp;&nbsp;</button></td></form>
						  
		<form action='sition.php' method='post'><td class='hidden-xs hidden-print' align='right' style='width:10px; padding:0px;'>
                <input type='hidden' name='rowid' value='$code'>
                <input type='hidden' name='date' value='$date'>
							  <input type='hidden' name='p' value='0'>
							  <input type='hidden' name='user' value='$user'>
					<input type='hidden' name='pago' value='crequit.php'>
				<button type='submit' class='btn btn-xs btn-success hidden-print' name='opens' style='height:24px; padding:0px; margin:2px;' title='Open' data-toggle='tooltip' data-placement='top'>
						  &nbsp;&nbsp;<i class='lnr lnr-download'></i>&nbsp;&nbsp;</button></td></form></tr>");
						  $n++;				$tol+=$tot;
						}
						$toto=number_format($tol, 2);
						$tos="0.00";
						?>
						
                    </tbody>

					 <thead>
					<tr><th class="hidden-xs"> </th><th colspan='3'><div align='center'> Grand Total </th>
					<th colspan='2'><div align='right'><?php echo $toto ?>&nbsp;&nbsp;&nbsp;</th>
					<th><div align='right'><?php echo $tos ?>&nbsp;&nbsp;&nbsp;</th>
					<th><div align='right'><?php echo $toto ?>&nbsp;&nbsp;&nbsp;</th>
					<th colspan='3' class="hidden-print"><div align='center'> -- </th></tr>
                  </table><br><br>
                    
                        </div></div>
                  
                  <span class="pull-right hidden-print"><?php echo $bra ?>&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><br><br>

				  <?php




				}
				else
						echo"<div class='row'>
            <div class='col-lg-12' style='margin-top:-20px;'>
          <span class='pull-right hidden-print'> $bra &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'>
			<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><br>";
					}
			
			
					?>
                                      
                
              </div>
            </div></div>
                  </div>

				 
      </form>
   </div></div></div>  
   <?php
   include'footer.php';
   ?>
