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

?>
<!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>Product wise sale report</h4>
<p>Run time: <?php echo $current_date." - ".$current_time ?> | User: <?php echo $current_user_resalt['user_fname']; ?></p>
</div>	
<hr>
	
<table id="example" class="table table-bordered table-hover" style="width:100%; font-size: 14px; text-transform: capitalize;">
        <thead>
            <tr class="table-active">
                <td>No.</td>
                <td>Date</td>
				<td>Total Product</td>
				<td align="right">Cost</td>
				<td align="right">Income</td>
				<td align="right">Profit</td>
            </tr>
        </thead>
        <tbody>
			<?php
			$count_no=0;
			$cost=0;
			$income=0;
			$product=0;
			$stock_qury=mysqli_query($connation,"SELECT sd.sale_details_date,SUM(sl.sale_qty) AS qty,SUM(sl.sale_buy_price*sl.sale_qty) AS cost,SUM(sl.sale_saleprice*sl.sale_qty) AS sale_amount
			FROM sale_details sd RIGHT JOIN sale_list sl ON sd.sale_details_billid=sl.sale_billid
			INNER JOIN product_details pd ON sl.sale_barcode=pd.product_barcode
			WHERE sd.sale_details_billid IS NOT NULL AND pd.product_id='$_POST[product_id]' AND sd.sale_details_date BETWEEN '$_POST[st_date]' AND '$_POST[en_date]'
			GROUP BY sd.sale_details_date
			ORDER BY sd.sale_details_date ASC");
			while($stock_resalt=mysqli_fetch_array($stock_qury)){
			$count_no++;
				
			$cost+=$stock_resalt['cost'];
			$income+=$stock_resalt['sale_amount'];
			$product+=$stock_resalt['qty'];
			?>
            <tr>
                <td><?php echo number_format((float)$count_no); ?></td>
                <td><?php echo $stock_resalt['sale_details_date'] ?></td>
				<td><?php echo number_format((float)$stock_resalt['qty']); ?></td>
				<td align="right"><?php echo number_format((float)$stock_resalt['cost'],2); ?></td>
				<td align="right"><?php echo number_format((float)$stock_resalt['sale_amount'],2); ?></td>
				<td align="right"><?php echo number_format((float)$stock_resalt['sale_amount']-$stock_resalt['cost'],2); ?></td>
            </tr>
            <?php } ?>
        </tbody>
	<tfoot>
		<tr class="table-active">
                <td colspan="2">Total Amount</td>
				<td><?php echo number_format((float)$product); ?></td>
				<td align="right"><?php echo number_format((float)$cost,2); ?></td>
				<td align="right"><?php echo number_format((float)$income,2); ?></td>
				<td align="right"><?php echo number_format((float)$income-$cost,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); ?>