<?php
if(basename($_SERVER['PHP_SELF']) == 'config.php') 
  $nv=" class='current'";
include'header.php';
include'connection.php';
$custo='';
$conde='';

if(isset($_POST['search']))
		{
			$_SESSION['Dato']=$_POST['dato'];
		}
		
		$dato=$_SESSION['Dato'];

if(isset($_POST['adjust']))
		{
			$custo=$_POST['custo'];
			$qts=$_POST['qts'];
$see=mysqli_query($conn, "SELECT `Number`, `Quantity` FROM `items` WHERE `Item`='$custo' AND `Status`='0' LIMIT 1");
$ree=mysqli_fetch_assoc($see);
$code=$ree['Number'];
$qtin=$ree['Quantity'];

// received and used item after dato
	$rec=$use=0;
$iseen=mysqli_query($conn, "SELECT SUM(`Quantity`) AS 'New' FROM `stouse` WHERE `Item`='$code' AND `Status`='0' AND `Store`='1' AND `Date`>='$dato' AND `Action`='RECEIVE'");
					$ireen=mysqli_fetch_assoc($iseen);
						$inew=$ireen['New'];

			$iseeu=mysqli_query($conn, "SELECT SUM(`Quantity`) AS 'Use' FROM `stouse` WHERE `Item`='$code' AND `Status`='0' AND `Store`='1' AND `Date`>='$dato' AND `Action`='USED'");
					$reeu=mysqli_fetch_assoc($iseeu);
						$iuse=$ireeu['Use'];
						
		$qtin=$qtin-$inew+$iuse;
		    $bal=$qts-$qtin;
		
			$so=mysqli_query($conn, "UPDATE `items` SET `Quantity`=`Quantity`+'$bal' WHERE `Number`='$code' AND `Status`='0' LIMIT 1");
$soso=mysqli_query($conn, "INSERT INTO `count` (`Number`, `Item`, `Qtin`, `Qton`, `Date`, `User`) VALUES (NULL, '$code', '$qtin', '$qts', '$dato', '$loge')");
		}

  
    if($_SESSION['Rsi']){
$do=mysqli_query($conn, "SELECT `count`.`Date`, `count`.`Qtin`, `count`.`Qton`, `items`.`Type`,`items`.`Item`,`items`.`Price` FROM `count` INNER JOIN `items` ON `count`.`Item`=`items`.`Number` ORDER BY `count`.`Number` DESC LIMIT 15");
$fo=mysqli_num_rows($do);
}
else
$fo=0;
?>
<div class="container-fluid main-content">
        <div class="page-title hidden-print">
          <h2 style='margin-top:-20px; margin-bottom: 5px; color:#ffcc33'>
        Materials
          </h2>
                 </div>

<script>
$(document).ready(function(){
  var show_btn=$('.show-modal');
  var show_btn=$('.show-modal');
  //$("#testmodal").modal('show');
  
    show_btn.click(function(){
      $("#testmodal").modal('show');
  })
});

$(function() {
        $('#element').on('click', function( e ) {
            Custombox.open({
                target: '#testmodal-1',
                effect: 'fadein'
            });
            e.preventDefault();
        });
    });
</script>
     
        <div class="row">
  <div class="col-lg-2 hidden-print">
   
   
   
   <ul class="list-group">

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

	   <li class="list-group-item">
              <a href="#" data-toggle='modal' data-target='#myModo'>
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Create New Item
                </p>
              </a></li>

	   <li class="list-group-item">
              <a href="repair.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Repair & Services
                </p>
              </a></li>  
                  
			  <li class="list-group-item">
              <a href="requit.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Requisition Forms
                </p>
              </a></li> 
              
              <li class="list-group-item">
              <a href="#" data-toggle='modal' data-target='#myModos'>
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Store Consumption
                </p>
              </a></li> 
              <?php
    if($_SESSION['Rsi']){
        ?>         
			  <li class="list-group-item">
              <a href="receive.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Receive Items
                </p>
              </a></li>
              <?php
    }
    ?>
            </ul><br><br>
        
              <?php
    if($_SESSION['Rsi']){
        ?>                   
			  <li class="list-group-item active" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
              <a href="config.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Physical Count
                </p>
              </a></li>
            <?php
    }
    ?>
            <br>
  </div>
                    
           
       
        <div class="col-lg-10">
                  <div class="row">
         
           <div class="col-lg-2 hidden-print"> </div>

        <form action="" method="post" class="form-horizontal ">                  
                       <div class="col-lg-10 hidden-print">
           <?php
    if($_SESSION['Rsi']){
                          
        if($_SESSION['Dato']==''){
        ?> 
        <div class="col-lg-6 text-right" style="padding-top:-5px; font-size:24px;"> Count Date </div>
          <div class="col-lg-3"> 
           <div class="input-group date datepicker">	
      <input class="form-control form-center" id="to" name="dato" type="text" value="<?php echo $Date ?>" onkeypress='return isNumberKey(event)' placeholder='Select an item' 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>   
        <?php    
        }
        else{
            ?>
		<div class="col-lg-3 hidden-print"> 					
					   
					   </div>
            <div class="col-lg-4 hidden-print"> 
      <input class="form-control"  name="custo" type="text" id="search" autofocus="autofocus" autocomplete="off" required>
    </div>  

		 <div class="col-lg-2 hidden-print">
		 <input name='qts' class='form-control sm' type='text' style='text-align:center;' placeholder='Quantity' required>
		 </div>
                    
                       
                       <div class="col-lg-2 hidden-print">
                        <button class="btn  btn-primary btn-block" type="submit" name="adjust"><i class="lnr lnr-plus-circle"></i> Save </button>
                   
					  </div>

<div class="col-lg-1">
                   
					  </div>
	<?php
        }
    }
        ?>
                         </div> 
                  
          </form></div> 
             
			<div class="divFooter"><center><u><b>STORE COUNT REPORT </b></u></center></div>

             <div class="row">
            <div class="col-lg-12" style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; <?php 
             if($dato)
             echo"Count Date :<b> $dato </b>";
             ?></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>
            <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> Date </th>
                        <th><div align='center'> Item&nbsp;Name </th>
                       <th><div align='center'> Item&nbsp;Type </th>
                       <th><div align='center'> Item&nbsp;Cost </th>
                        <th><div align='center'> S.Count </th>
                        <th><div align='center'> I.Count </th>
						<th><div align='center'> Balance </th></tr>
                    </thead>
                                        <tbody>
					<?php
					$n=1;		
			while($ro=mysqli_fetch_assoc($do)){
				$code=$ro['Number'];
				$item=$ro['Item'];
			$iname=$ro['Item'];
			$qt=$ro['Qtin'];				$qty=number_format($qt, 2);	
			$qo=$ro['Qton'];                $qto=number_format($qo, 2);			
			$cost=$ro['Price'];			$costo=number_format($cost, 2);
			$type=$ro['Type'];
			$cda=$ro['Date'];

	$stn="padding:1px;";                   $balo=number_format($qo-$qt, 2);

		print("<tr><td class='hidden-xs' style='$stn'>
                        <div align='center'>$n&nbsp;&nbsp;</td>
                       <td style='text-align:center; $stn'> $cda </td>
                        <td style='$stn'> $type </td>
						<td style='$stn'> $iname </td>
                        <td style='$stn'><div align='right'> $costo </td>
						<td style='$stn'><div align='right'> $qty </td>
						<td style='$stn'><div align='right'> $qto </td>
						<td style='$stn'><div align='right' style='padding-right:20px;'> $balo </td></tr>");
						  $n++;
						}
						$toto=number_format($tot);			$tco=number_format($tco);
						?>
						
                    </tbody>
                  </table>

					 <div class="col-md-12 hidden-print">
                  <div class="pull-right">
                  <ul class="pagination">
                      <li class="activei">
					  <?php
					  if($l1!=0){
						  ?>
					    <a href="#">
                        &nbsp;<<&nbsp; </a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<?php
					 }
						?>
                       
						<?php
						echo"<a href='#'>";
						?>
                        >>                        </a>
                      </li>
                    </ul>
              </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>                       
              
            </div></div>
                  </div>
      
   </div></div></div>  
   <?php
   include'footer.php';
   ?>
