<?php 

require_once("config/configDB.php"); 
require "userPermission.php";
require_once("config/constant.php");
error_reporting(0);

$sql_expense = mysqli_query($conn,"SELECT id, expense FROM expense_master where flag=1 ");
$row_expense = '';

$sql_employee = mysqli_query($conn,"SELECT id,ename FROM employee_master WHERE isdelete='1' order by ename ASC ");
$row_employee = '';






$fromdate=$_POST['fromDate'];
$todate=$_POST['toDate'];
$expenser=$_POST['expenser'];
$expensetype=$_POST['expensetype'];
?>

<!DOCTYPE html>
<html>
<head>
    <?php 
      require 'meta.php'; 
      require 'header.php'; 
    ?>
<style>
	
	@font-face
	{
	font-family:Bamini;
	src:url(fonts/Bamini.TTF);
	
	}
	.tamil
	{
	font-family:Bamini;
	
	}
	
	
	</style>
    <script>
 
 function printDiv() {
    var divToPrint = document.getElementById('a');
    var htmlToPrint = '' +
        '<style type="text/css">' +
        'table th, table td {' +
        'border:1px solid #000;' +
        'padding;0.5em;' +
        '}' +
        '</style>';
    htmlToPrint += divToPrint.outerHTML;
    newWin = window.open("");
    newWin.document.write(htmlToPrint);
    newWin.print();
    newWin.close();
}
 
 
 </script>
</head>
<body>
    <?php require 'menu.php' ?>
<div class="container-fluid">
<form action="product_stockreport.php" method="post">
<div class="container-fluid text-center col-sm-12 dashboard-add">  


<div class=" col-sm-1" style="padding-top: 24px;">
    <button type="button" class="btn-success btn-lg btn-md datewise-submit-sale" onClick="printDiv();">Print</button>
</div>
</div>

</form>

<div class="container col-sm-12">
<div id="a">

<center><h3 > Product Stock Report</h3></center>

<table id="salestockDashboard" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
        <thead>
            <tr>
             <th>S.No</th>
              <th>Product&nbsp;ID</th>
                <th>Product&nbsp;Name</th>
                 <th>Product&nbsp;Name</th>
                  <th>Qty&nbsp;Type</th>
                <th>Available&nbsp;Qty</th>
                <th>Recent&nbsp;Rate</th>
                <th>Stock&nbsp;Value</th>
				
				
               
            </tr>
        </thead>
        
        <?php
				
				
				$sql = "SELECT id, productname,product_tamil,qty_type from product_master where isdelete=1 order by productname ASC ";
				
		
	
				?>
        
        <tbody>
            <?php
			$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 1".$sql);
			$sno=0;
			$qty=0;
			$amt=0;
			while($row=mysqli_fetch_array($query))
{
	$pid=$row["id"];


$query_purchase=mysqli_query($conn,"select sum(purchase_eqty) as pqty from purchasedetails where productID='".$pid."' and status=''");
while($row_pqty=mysqli_fetch_array($query_purchase))
{
$pqty=$row_pqty['pqty'];

}

$query_sales=mysqli_query($conn,"select sum(sale_eqty) as sqty from saledetails where productID='".$pid."' and status=''");
while($row_sqty=mysqli_fetch_array($query_sales))
{
$sqty=$row_sqty['sqty'];

}

$query_sales_return=mysqli_query($conn,"select sum(sale_eqty) as rqty from sale_return_details where productID='".$pid."' and status=''");
while($row_sqty_return=mysqli_fetch_array($query_sales_return))
{
$rqty=$row_sqty_return['rqty'];

}

$available_qty=$pqty-($sqty-$rqty);

$aqty=number_format($available_qty,2,'.','');

//echo "select Max(id) as maxid from purchasedetails  where productID='".$pid."' and status=''";

$sql_max=mysqli_query($conn,"select Max(id) as maxid from purchasedetails  where productID='".$pid."' and status=''");
while($row_max=mysqli_fetch_array($sql_max))
{
$maxid=$row_max['maxid'];

}
//echo "select Max(purchase_prate) as pprice from purchasedetails where where productID='".$maxid."' and status=''";
$sql_max_rate=mysqli_query($conn,"select Max(purchase_prate) as pprice from purchasedetails  where id='".$maxid."' and status=''");
while($row_max_price=mysqli_fetch_array($sql_max_rate))
{
$max_price=$row_max_price['pprice'];

}

				
				?>
			<tr>
            <td><?php echo ++$sno;?></td>
           
			<td><?php echo $row["id"];?></td>
            <td><?php echo $row["productname"];?></td>
            <td><?php echo $row["product_tamil"];?></td>
            <td><?php echo $row["qty_type"];?></td>
          
           
          
			
             <td align="right"><?php echo number_format(round($aqty), 2, '.', '');?></td>
              <td align="right"><?php echo number_format(round($max_price), 2, '.', '');?></td>
             <td align="right"><?php echo number_format(round($aqty*$max_price), 2, '.', '');?></td>
              
			
			
           
			</tr>
            <?php
			
		$qty+=$aqty;
			$amt+=$aqty*$max_price;	
			
}


?>
            <tr style="font-weight:bold;">
        <td colspan="5">Total Qty</td>
       
       <td align="right"><?php echo number_format($qty,2,'.','');?></td>
       <td >Total Value</td>
       <td align="right"><?php echo number_format($amt,2,'.','');?></td>
       
        </tr>   
                    
           
        </tbody>
       
    </table>
    </div>
</div>

</div>


    <?php require 'footer.php' ?>
   
   
</body>
</html>