<?php
session_start();
require("php_con.php");
require("check_user.php");
require("function.php");

if(isset($_POST['run_date'])&&isset($_POST['product_supplier'])){}else{ exit("Error!"); }

$st_date_qury=mysqli_query($connation,"SELECT MIN(stock_addtime) AS st_date FROM stock_details");
$st_date_resalt=mysqli_fetch_assoc($st_date_qury);
$st_date=date("Y-m-d",strtotime($st_date_resalt['st_date']));
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="favicon.ico">
<title>Report - <?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
	
<script src="data_table/js/jquery-3.5.1.js"></script>
<script src="data_table/js/jquery.dataTables.min.js"></script>
<script src="data_table/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">
	
<link rel="stylesheet" href="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
	
<link rel="stylesheet" href="alert/sweetalert2.min.css">
<script src="alert/sweetalert2.min.js"></script>
	
<style>
	.table tr td{
		font-size: 14px;
		padding: 5px;
	}
	.table tr th{
		font-size: 14px;
		padding: 5px;
	}
</style>
</head>

<body>
<script>
window.addEventListener("load",function(){
	var load_screen=document.getElementById('load_screen');
	document.body.removeChild(load_screen);
});
</script>

<div id="load_screen"><div id="loading"><img src="image/loading.gif" width="80px;"></div></div>
	
<div class="p-2 text-right"><button type="button" class="btn btn-success btn-sm" onClick="JavaScript:printDiv('print_aria');">Print Report</button></div>
	
<div class="m-4" id="print_aria">
	
<div align="center" class="mb-2">
<h4>Total Stock Report</h4>
<p>Run time: <?php echo $current_date." - ".$current_time ?> | User: <?php if($current_user_resalt){ echo $current_user_resalt['user_fname']; }else{echo "N/A";} ?><br>
Duration: <?php echo $st_date; ?> to <?php echo date("Y-m-d",strtotime($_POST['run_date'])); ?></p>
</div>	
<hr>
	
<table id="example" class="table table-bordered table-hover" style="width:100%; font-size: 14px; text-transform: capitalize;">
        <thead>
            <tr>
                <td>No.</td>
                <td>Product Name</td>
				<td>Supplier</td>
                <td>Bar./Pro. ID</td>
				<td align="right">QTY</td>
				<td align="right">Value</td>
            </tr>
        </thead>
        <tbody>
			<?php
			$count_no=0;
			$total_amount=0;
			if($_POST['product_supplier']=="all"){
			$list_qury=mysqli_query($connation,"SELECT pd.*,sd.supplier_name
			FROM product_details pd LEFT JOIN supplier_details sd ON pd.product_supplier=sd.supplier_id
			ORDER BY sd.supplier_name");
			}
			else{
			$product_supplier=mysqli_real_escape_string($connation,$_POST['product_supplier']);
			$list_qury=mysqli_query($connation,"SELECT pd.*,sd.supplier_name
			FROM product_details pd LEFT JOIN supplier_details sd ON pd.product_supplier=sd.supplier_id
			WHERE sd.supplier_id='$product_supplier'
			ORDER BY sd.supplier_name");
			}
			while($list_resalt=mysqli_fetch_array($list_qury)){
			$count_no++;
			?>
            <tr>
                <td><?php echo number_format($count_no,0); ?></td>
                <td><?php echo $list_resalt['product_name']; ?></td>
				<td><?php echo $list_resalt['supplier_name']; ?></td>
                <td><?php echo $list_resalt['product_barcode']." / ".$list_resalt['product_productid']; ?></td>
				<td align="right"><?php echo number_format(availbel_qty_date_wise($list_resalt['product_systemid'],$st_date_resalt['st_date'],$_POST['run_date']),0); ?></td>
				<td align="right"><?php $value=price_buy($list_resalt['product_systemid'])*availbel_qty_date_wise($list_resalt['product_systemid'],$st_date_resalt['st_date'],$_POST['run_date']); echo number_format($value,2); ?></td>
            </tr>
            <?php 
			$total_amount=$total_amount+$value;
			} ?>
        </tbody>
	<tfoot>
		<tr>
		<td colspan="5" align="right">Total Amount</td>
		<td align="right"><?php echo number_format($total_amount,2); ?></td>
		</tr>
	</tfoot>
    </table>
</div>

<script>
function printDiv(print_aria){
	var printcontents=document.getElementById('print_aria').innerHTML;
	var orginalcontain=document.body.innerHTML;
	document.body.innerHTML=printcontents;
	window.print();
	document.body.innerHTML=orginalcontain;

}
</script>
	
</body>
</html>
<?php @mysqli_close($connation); ?>