<?php
include('function.php');
 
 include('db_con.php');
 //stock_update($connect);
 stock_update($connect);
 $dated = date('d-m-Y h:m A');
 $html = '';
 
 $count = 0;
 $stock = 0;
 
 $tot_stock = 0;
 $item_name = $_GET['item_name'];
  $type_id = $_GET['type_id'];
  
  $stock = $_GET['stock'];
$sql = "SELECT ifnull(type_name,'ALL') from item_type where type_id='".$type_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $type_name = $statement->fetchColumn();
  

  $statement = $connect->prepare("SELECT CONCAT(t.type_name, ' ',i.item_name) as item_name,i.stock from items i,item_type t where t.type_id=i.type_id 
  AND i.status = 'active' AND stock <> '".$stock."' and i.item_name LIKE '%".$item_name."%' and i.type_id LIKE '%".$type_id."%' order by item_name ASC");
 $statement->execute();
 
 $result = $statement->fetchAll(); 
 ?>
<!DOCTYPE html>
<html>
<head>
  <title>Stock Available Report</title>
  <style>
@page { size: auto;  margin: 0mm; }
</style>
<link rel="stylesheet" type="text/css" href="css/print_style.css">
</head>
<body>
  
  
  <table width="100%" class="table_header">
  <tr>
  <td colspan="10" style="text-align:center;font-size:18px;font-weight:bold;margin:5px">Stock Available Report</td>
  </tr>
  <tr>
  <td width="5%">Type:</td><td width="15%"><?php echo $type_name;?></td>
  <td width="5%"></td><td width="15%"></td>
  <td width="5%"></td><td width="15%"></td>
    
  </tr>
  
  </table>
  <table width="100%" class="print_table">
  <tr>
  <th width="2%">#</th>
  
  <th width="22%">Item Name</th> 
   
  <th width="7%" class="rightside">Stock</th>
   
  </tr>
 
  
<?php
foreach($result as $row)
 {
  
  $stock = $row["stock"];
  
  $tot_stock = $tot_stock + $stock;
  
  
 
   $count = $count+1;
     // if($count == 1){$stock = $opening + $qty_in - $qty_out; }else{$stock = $stock + $qty_in - $qty_out;}
   
 echo '
  
    
  
  
<tr>
<td width="5%">'.$count.'</td>
  
<td width="85%">'.$row["item_name"].'</td>
    
    
    <td width="10%" class="rightside">'.number_format($stock, 2).'</td>
    
  
   
    
   </tr>
   
  
        ';
}
?>
  <div id="footer">
  <hr />
  <table width="100%" class="print_table">
  
  <td width="90%" class="rightside bor">Total : </td><td width="10%" style="border-top:1px solid black" class="rightside"><?php echo''.number_format($tot_stock, 2).'';?></td>
  
 
  </tr>
  </table>
    
  </div>
 
 </table>
 <table width="100%" class="print_table">
    <tr><td>Printed at : <?php echo $dated;?></td></tr>
  </table>
</body>
 </html>